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_blockd.h"
17 
18 #include "vp9_block.h"
19 #include "vp9_context_tree.h"
20 #include "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) + ((int64_t)D << DM))
31 
32 #define QIDX_SKIP_THRESH 115
33 
34 #define MV_COST_WEIGHT 108
35 #define MV_COST_WEIGHT_SUB 120
36 
37 #define INVALID_MV 0x80008000
38 
39 #define MAX_MODES 30
40 #define MAX_REFS 6
41 
42 #define RD_THRESH_INIT_FACT 32
43 #define RD_THRESH_MAX_FACT 64
44 #define RD_THRESH_INC 1
45 
46 // This enumerator type needs to be kept aligned with the mode order in
47 // const MODE_DEFINITION vp9_mode_order[MAX_MODES] used in the rd code.
48 typedef enum {
49   THR_NEARESTMV,
50   THR_NEARESTA,
51   THR_NEARESTG,
52 
53   THR_DC,
54 
55   THR_NEWMV,
56   THR_NEWA,
57   THR_NEWG,
58 
59   THR_NEARMV,
60   THR_NEARA,
61   THR_NEARG,
62 
63   THR_ZEROMV,
64   THR_ZEROG,
65   THR_ZEROA,
66 
67   THR_COMP_NEARESTLA,
68   THR_COMP_NEARESTGA,
69 
70   THR_TM,
71 
72   THR_COMP_NEARLA,
73   THR_COMP_NEWLA,
74   THR_COMP_NEARGA,
75   THR_COMP_NEWGA,
76 
77   THR_COMP_ZEROLA,
78   THR_COMP_ZEROGA,
79 
80   THR_H_PRED,
81   THR_V_PRED,
82   THR_D135_PRED,
83   THR_D207_PRED,
84   THR_D153_PRED,
85   THR_D63_PRED,
86   THR_D117_PRED,
87   THR_D45_PRED,
88 } THR_MODES;
89 
90 typedef enum {
91   THR_LAST,
92   THR_GOLD,
93   THR_ALTR,
94   THR_COMP_LA,
95   THR_COMP_GA,
96   THR_INTRA,
97 } THR_MODES_SUB8X8;
98 
99 typedef struct RD_OPT {
100   // Thresh_mult is used to set a threshold for the rd score. A higher value
101   // means that we will accept the best mode so far more often. This number
102   // is used in combination with the current block size, and thresh_freq_fact
103   // to pick a threshold.
104   int thresh_mult[MAX_MODES];
105   int thresh_mult_sub8x8[MAX_REFS];
106 #if 0
107   int threshes[MAX_SEGMENTS][BLOCK_SIZES][MAX_MODES];
108 #endif
109   int64_t prediction_type_threshes[MAX_REF_FRAMES][REFERENCE_MODES];
110 
111   int64_t filter_threshes[MAX_REF_FRAMES][SWITCHABLE_FILTER_CONTEXTS];
112 #if CONFIG_CONSISTENT_RECODE
113   int64_t prediction_type_threshes_prev[MAX_REF_FRAMES][REFERENCE_MODES];
114 
115   int64_t filter_threshes_prev[MAX_REF_FRAMES][SWITCHABLE_FILTER_CONTEXTS];
116 #endif
117   int RDMULT;
118   int rd_mult_sad;
119   int RDDIV;
120   double r0;
121 } RD_OPT;
122 
123 typedef struct RD_COST {
124   int rate;
125   int64_t dist;
126   int64_t rdcost;
127 } RD_COST;
128 
129 // Reset the rate distortion cost values to maximum (invalid) value.
130 void eb_vp9_rd_cost_reset(RD_COST *rd_cost);
131 // Initialize the rate distortion cost values to zero.
132 void eb_vp9_rd_cost_init(RD_COST *rd_cost);
133 
134 struct TileInfo;
135 struct TileDataEnc;
136 struct VP9_COMP;
137 struct macroblock;
138 
139 int64_t eb_vp9_compute_rd_mult_based_on_qindex(const struct VP9_COMP *cpi,
140                                             int qindex);
141 
142 int eb_vp9_compute_rd_mult(const struct VP9_COMP *cpi, int qindex);
143 
144 int eb_vp9_get_adaptive_rdmult(const struct VP9_COMP *cpi, double beta);
145 
146 void eb_vp9_initialize_rd_consts(struct VP9_COMP *cpi);
147 
148 void eb_vp9_initialize_me_consts(struct VP9_COMP *cpi, MACROBLOCK *x, int qindex);
149 
150 void eb_vp9_model_rd_from_var_lapndz(unsigned int var, unsigned int n,
151                                   unsigned int qstep, int *rate, int64_t *dist);
152 
153 void eb_vp9_model_rd_from_var_lapndz_vec(unsigned int var[MAX_MB_PLANE],
154                                       unsigned int n_log2[MAX_MB_PLANE],
155                                       unsigned int qstep[MAX_MB_PLANE],
156                                       int64_t *rate_sum, int64_t *dist_sum);
157 
158 int vp9_get_switchable_rate(const struct VP9_COMP *cpi,
159                             const MACROBLOCKD *const xd);
160 
161 int vp9_raster_block_offset(BLOCK_SIZE plane_bsize, int raster_block,
162                             int stride);
163 
164 int16_t *vp9_raster_block_offset_int16(BLOCK_SIZE plane_bsize, int raster_block,
165                                        int16_t *base);
166 
167 YV12_BUFFER_CONFIG *vp9_get_scaled_ref_frame(const struct VP9_COMP *cpi,
168                                              int ref_frame);
169 
170 void eb_vp9_init_me_luts(void);
171 
172 void eb_vp9_get_entropy_contexts(BLOCK_SIZE bsize, TX_SIZE tx_size,
173                               const struct macroblockd_plane *pd,
174                               ENTROPY_CONTEXT t_above[16],
175                               ENTROPY_CONTEXT t_left[16]);
176 
177 void vp9_set_rd_speed_thresholds(struct VP9_COMP *cpi);
178 
179 void vp9_set_rd_speed_thresholds_sub8x8(struct VP9_COMP *cpi);
180 
181 void vp9_update_rd_thresh_fact(int (*fact)[MAX_MODES], int rd_thresh, int bsize,
182                                int best_mode_index);
183 
rd_less_than_thresh(int64_t best_rd,int thresh,const int * const thresh_fact)184 static INLINE int rd_less_than_thresh(int64_t best_rd, int thresh,
185                                       const int *const thresh_fact) {
186   return best_rd < ((int64_t)thresh * (*thresh_fact) >> 5) || thresh == INT_MAX;
187 }
188 
set_error_per_bit(MACROBLOCK * x,int rdmult)189 static INLINE void set_error_per_bit(MACROBLOCK *x, int rdmult) {
190   x->errorperbit = rdmult >> RD_EPB_SHIFT;
191   x->errorperbit += (x->errorperbit == 0);
192 }
193 
194 void vp9_mv_pred(struct VP9_COMP *cpi, MACROBLOCK *x, uint8_t *ref_y_buffer,
195                  int ref_y_stride, int ref_frame, BLOCK_SIZE block_size);
196 
197 void vp9_setup_pred_block(const MACROBLOCKD *xd,
198                           struct buf_2d dst[MAX_MB_PLANE],
199                           const YV12_BUFFER_CONFIG *src, int mi_row, int mi_col,
200                           const struct scale_factors *scale,
201                           const struct scale_factors *scale_uv);
202 #if 0
203 int vp9_get_intra_cost_penalty(const struct VP9_COMP *const cpi,
204                                BLOCK_SIZE bsize, int qindex, int qdelta);
205 #else
206 int vp9_get_intra_cost_penalty(BLOCK_SIZE bsize, int qindex, int qdelta, int is_flat_noise);
207 #endif
208 unsigned int vp9_get_sby_variance(struct VP9_COMP *cpi,
209                                   const struct buf_2d *ref, BLOCK_SIZE bs);
210 unsigned int vp9_get_sby_perpixel_variance(struct VP9_COMP *cpi,
211                                            const struct buf_2d *ref,
212                                            BLOCK_SIZE bs);
213 #if CONFIG_VP9_HIGHBITDEPTH
214 unsigned int vp9_high_get_sby_variance(struct VP9_COMP *cpi,
215                                        const struct buf_2d *ref, BLOCK_SIZE bs,
216                                        int bd);
217 unsigned int vp9_high_get_sby_perpixel_variance(struct VP9_COMP *cpi,
218                                                 const struct buf_2d *ref,
219                                                 BLOCK_SIZE bs, int bd);
220 #endif
221 
222 #ifdef __cplusplus
223 }  // extern "C"
224 #endif
225 
226 #endif  // VPX_VP9_ENCODER_VP9_RD_H_
227