• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef VPX_VP9_ENCODER_VP9_RD_H_
12 #define VPX_VP9_ENCODER_VP9_RD_H_
13 
14 #include <limits.h>
15 
16 #include "vp9/common/vp9_blockd.h"
17 
18 #include "vp9/encoder/vp9_block.h"
19 #include "vp9/encoder/vp9_context_tree.h"
20 #include "vp9/encoder/vp9_cost.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define RDDIV_BITS 7
27 #define RD_EPB_SHIFT 6
28 
29 #define RDCOST(RM, DM, R, D) \
30   ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), VP9_PROB_COST_SHIFT) + ((D) << (DM))
31 #define RDCOST_NEG_R(RM, DM, R, D) \
32   ((D) << (DM)) - ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), VP9_PROB_COST_SHIFT)
33 #define RDCOST_NEG_D(RM, DM, R, D) \
34   ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), VP9_PROB_COST_SHIFT) - ((D) << (DM))
35 
36 #define QIDX_SKIP_THRESH 115
37 
38 #define MV_COST_WEIGHT 108
39 #define MV_COST_WEIGHT_SUB 120
40 
41 #define MAX_MODES 30
42 #define MAX_REFS 6
43 
44 #define RD_THRESH_INIT_FACT 32
45 #define RD_THRESH_MAX_FACT 64
46 #define RD_THRESH_INC 1
47 
48 #define VP9_DIST_SCALE_LOG2 4
49 #define VP9_DIST_SCALE (1 << VP9_DIST_SCALE_LOG2)
50 
51 // This enumerator type needs to be kept aligned with the mode order in
52 // const MODE_DEFINITION vp9_mode_order[MAX_MODES] used in the rd code.
53 typedef enum {
54   THR_NEARESTMV,
55   THR_NEARESTA,
56   THR_NEARESTG,
57 
58   THR_DC,
59 
60   THR_NEWMV,
61   THR_NEWA,
62   THR_NEWG,
63 
64   THR_NEARMV,
65   THR_NEARA,
66   THR_NEARG,
67 
68   THR_ZEROMV,
69   THR_ZEROG,
70   THR_ZEROA,
71 
72   THR_COMP_NEARESTLA,
73   THR_COMP_NEARESTGA,
74 
75   THR_TM,
76 
77   THR_COMP_NEARLA,
78   THR_COMP_NEWLA,
79   THR_COMP_NEARGA,
80   THR_COMP_NEWGA,
81 
82   THR_COMP_ZEROLA,
83   THR_COMP_ZEROGA,
84 
85   THR_H_PRED,
86   THR_V_PRED,
87   THR_D135_PRED,
88   THR_D207_PRED,
89   THR_D153_PRED,
90   THR_D63_PRED,
91   THR_D117_PRED,
92   THR_D45_PRED,
93 } THR_MODES;
94 
95 typedef enum {
96   THR_LAST,
97   THR_GOLD,
98   THR_ALTR,
99   THR_COMP_LA,
100   THR_COMP_GA,
101   THR_INTRA,
102 } THR_MODES_SUB8X8;
103 
104 typedef struct {
105   // RD multiplier control factors added for Vizier project.
106   double rd_mult_inter_qp_fac;
107   double rd_mult_arf_qp_fac;
108   double rd_mult_key_qp_fac;
109 } RD_CONTROL;
110 
111 typedef struct RD_OPT {
112   // Thresh_mult is used to set a threshold for the rd score. A higher value
113   // means that we will accept the best mode so far more often. This number
114   // is used in combination with the current block size, and thresh_freq_fact to
115   // pick a threshold.
116   int thresh_mult[MAX_MODES];
117   int thresh_mult_sub8x8[MAX_REFS];
118 
119   int threshes[MAX_SEGMENTS][BLOCK_SIZES][MAX_MODES];
120 
121   int64_t prediction_type_threshes[MAX_REF_FRAMES][REFERENCE_MODES];
122 
123   int64_t filter_threshes[MAX_REF_FRAMES][SWITCHABLE_FILTER_CONTEXTS];
124 #if CONFIG_CONSISTENT_RECODE || CONFIG_RATE_CTRL
125   int64_t prediction_type_threshes_prev[MAX_REF_FRAMES][REFERENCE_MODES];
126 
127   int64_t filter_threshes_prev[MAX_REF_FRAMES][SWITCHABLE_FILTER_CONTEXTS];
128 #endif  // CONFIG_CONSISTENT_RECODE || CONFIG_RATE_CTRL
129   int RDMULT;
130   int RDDIV;
131   double r0;
132 } RD_OPT;
133 
134 typedef struct RD_COST {
135   int rate;
136   int64_t dist;
137   int64_t rdcost;
138 } RD_COST;
139 
140 // Reset the rate distortion cost values to maximum (invalid) value.
141 void vp9_rd_cost_reset(RD_COST *rd_cost);
142 // Initialize the rate distortion cost values to zero.
143 void vp9_rd_cost_init(RD_COST *rd_cost);
144 // It supports negative rate and dist, which is different from RDCOST().
145 int64_t vp9_calculate_rd_cost(int mult, int div, int rate, int64_t dist);
146 // Update the cost value based on its rate and distortion.
147 void vp9_rd_cost_update(int mult, int div, RD_COST *rd_cost);
148 
149 struct TileInfo;
150 struct TileDataEnc;
151 struct VP9_COMP;
152 struct macroblock;
153 
154 void vp9_init_rd_parameters(struct VP9_COMP *cpi);
155 
156 int vp9_compute_rd_mult_based_on_qindex(const struct VP9_COMP *cpi, int qindex);
157 
158 int vp9_compute_rd_mult(const struct VP9_COMP *cpi, int qindex);
159 
160 int vp9_get_adaptive_rdmult(const struct VP9_COMP *cpi, double beta);
161 
162 void vp9_initialize_rd_consts(struct VP9_COMP *cpi);
163 
164 void vp9_initialize_me_consts(struct VP9_COMP *cpi, MACROBLOCK *x, int qindex);
165 
166 void vp9_model_rd_from_var_lapndz(unsigned int var, unsigned int n_log2,
167                                   unsigned int qstep, int *rate, int64_t *dist);
168 
169 void vp9_model_rd_from_var_lapndz_vec(unsigned int var[MAX_MB_PLANE],
170                                       unsigned int n_log2[MAX_MB_PLANE],
171                                       unsigned int qstep[MAX_MB_PLANE],
172                                       int64_t *rate_sum, int64_t *dist_sum);
173 
174 int vp9_get_switchable_rate(const struct VP9_COMP *cpi,
175                             const MACROBLOCKD *const xd);
176 
177 int vp9_raster_block_offset(BLOCK_SIZE plane_bsize, int raster_block,
178                             int stride);
179 
180 int16_t *vp9_raster_block_offset_int16(BLOCK_SIZE plane_bsize, int raster_block,
181                                        int16_t *base);
182 
183 YV12_BUFFER_CONFIG *vp9_get_scaled_ref_frame(const struct VP9_COMP *cpi,
184                                              int ref_frame);
185 
186 void vp9_init_me_luts(void);
187 
188 void vp9_get_entropy_contexts(BLOCK_SIZE bsize, TX_SIZE tx_size,
189                               const struct macroblockd_plane *pd,
190                               ENTROPY_CONTEXT t_above[16],
191                               ENTROPY_CONTEXT t_left[16]);
192 
193 void vp9_set_rd_speed_thresholds(struct VP9_COMP *cpi);
194 
195 void vp9_set_rd_speed_thresholds_sub8x8(struct VP9_COMP *cpi);
196 
197 void vp9_update_rd_thresh_fact(int (*factor_buf)[MAX_MODES], int rd_thresh,
198                                int bsize, int best_mode_index);
199 
rd_less_than_thresh(int64_t best_rd,int thresh,const int * const thresh_fact)200 static INLINE int rd_less_than_thresh(int64_t best_rd, int thresh,
201                                       const int *const thresh_fact) {
202   return best_rd < ((int64_t)thresh * (*thresh_fact) >> 5) || thresh == INT_MAX;
203 }
204 
set_error_per_bit(MACROBLOCK * x,int rdmult)205 static INLINE void set_error_per_bit(MACROBLOCK *x, int rdmult) {
206   x->errorperbit = rdmult >> RD_EPB_SHIFT;
207   x->errorperbit += (x->errorperbit == 0);
208 }
209 
210 void vp9_mv_pred(struct VP9_COMP *cpi, MACROBLOCK *x, uint8_t *ref_y_buffer,
211                  int ref_y_stride, int ref_frame, BLOCK_SIZE block_size);
212 
213 void vp9_setup_pred_block(const MACROBLOCKD *xd,
214                           struct buf_2d dst[MAX_MB_PLANE],
215                           const YV12_BUFFER_CONFIG *src, int mi_row, int mi_col,
216                           const struct scale_factors *scale,
217                           const struct scale_factors *scale_uv);
218 
219 int vp9_get_intra_cost_penalty(const struct VP9_COMP *const cpi,
220                                BLOCK_SIZE bsize, int qindex, int qdelta);
221 
222 unsigned int vp9_get_sby_variance(struct VP9_COMP *cpi,
223                                   const struct buf_2d *ref, BLOCK_SIZE bs);
224 unsigned int vp9_get_sby_perpixel_variance(struct VP9_COMP *cpi,
225                                            const struct buf_2d *ref,
226                                            BLOCK_SIZE bs);
227 #if CONFIG_VP9_HIGHBITDEPTH
228 unsigned int vp9_high_get_sby_variance(struct VP9_COMP *cpi,
229                                        const struct buf_2d *ref, BLOCK_SIZE bs,
230                                        int bd);
231 unsigned int vp9_high_get_sby_perpixel_variance(struct VP9_COMP *cpi,
232                                                 const struct buf_2d *ref,
233                                                 BLOCK_SIZE bs, int bd);
234 #endif
235 
236 void vp9_build_inter_mode_cost(struct VP9_COMP *cpi);
237 
238 #ifdef __cplusplus
239 }  // extern "C"
240 #endif
241 
242 #endif  // VPX_VP9_ENCODER_VP9_RD_H_
243