1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #ifndef AOM_AV1_ENCODER_RD_H_
13 #define AOM_AV1_ENCODER_RD_H_
14
15 #include <limits.h>
16
17 #include "av1/common/blockd.h"
18
19 #include "av1/encoder/block.h"
20 #include "av1/encoder/context_tree.h"
21 #include "av1/encoder/cost.h"
22 #include "av1/encoder/ratectrl.h"
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #define RDDIV_BITS 7
29 #define RD_EPB_SHIFT 6
30
31 #define RDCOST(RM, R, D) \
32 (ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), AV1_PROB_COST_SHIFT) + \
33 ((D) * (1 << RDDIV_BITS)))
34
35 #define RDCOST_NEG_R(RM, R, D) \
36 (((D) * (1 << RDDIV_BITS)) - \
37 ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), AV1_PROB_COST_SHIFT))
38
39 #define RDCOST_DBL_WITH_NATIVE_BD_DIST(RM, R, D, BD) \
40 (((((double)(R)) * (RM)) / (double)(1 << AV1_PROB_COST_SHIFT)) + \
41 ((double)((D) >> (2 * (BD - 8))) * (1 << RDDIV_BITS)))
42
43 #define QIDX_SKIP_THRESH 115
44
45 #define MV_COST_WEIGHT 108
46 #define MV_COST_WEIGHT_SUB 120
47
48 // The fractional part of rd_thresh factor is stored with 5 bits. The maximum
49 // factor that we allow is two, which is stored as 2 ** (5+1) = 64
50 #define RD_THRESH_FAC_FRAC_BITS (5)
51 #define RD_THRESH_FAC_FRAC_VAL (1 << (RD_THRESH_FAC_FRAC_BITS))
52 #define RD_THRESH_MAX_FACT ((RD_THRESH_FAC_FRAC_VAL) << 1)
53 #define RD_THRESH_LOG_DEC_FACTOR (4)
54 #define RD_THRESH_INC (1)
55
56 // Factor to weigh the rate for switchable interp filters.
57 #define SWITCHABLE_INTERP_RATE_FACTOR 1
58
59 #define RTC_REFS 4
60 static const MV_REFERENCE_FRAME real_time_ref_combos[RTC_REFS][2] = {
61 { LAST_FRAME, NONE_FRAME },
62 { ALTREF_FRAME, NONE_FRAME },
63 { GOLDEN_FRAME, NONE_FRAME },
64 { INTRA_FRAME, NONE_FRAME }
65 };
66
mode_offset(const PREDICTION_MODE mode)67 static INLINE int mode_offset(const PREDICTION_MODE mode) {
68 if (mode >= NEARESTMV) {
69 return INTER_OFFSET(mode);
70 } else {
71 switch (mode) {
72 case DC_PRED: return 0;
73 case V_PRED: return 1;
74 case H_PRED: return 2;
75 case SMOOTH_PRED: return 3;
76 default: assert(0); return -1;
77 }
78 }
79 }
80
81 enum {
82 // Default initialization when we are not using winner mode framework. e.g.
83 // intrabc
84 DEFAULT_EVAL = 0,
85 // Initialization for selecting winner mode
86 MODE_EVAL,
87 // Initialization for winner mode evaluation
88 WINNER_MODE_EVAL,
89 // All mode evaluation types
90 MODE_EVAL_TYPES,
91 } UENUM1BYTE(MODE_EVAL_TYPE);
92
93 typedef struct RD_OPT {
94 // Thresh_mult is used to set a threshold for the rd score. A higher value
95 // means that we will accept the best mode so far more often. This number
96 // is used in combination with the current block size, and thresh_freq_fact
97 // to pick a threshold.
98 int thresh_mult[MAX_MODES];
99
100 int threshes[MAX_SEGMENTS][BLOCK_SIZES_ALL][MAX_MODES];
101
102 int RDMULT;
103
104 double r0;
105 } RD_OPT;
106
av1_init_rd_stats(RD_STATS * rd_stats)107 static INLINE void av1_init_rd_stats(RD_STATS *rd_stats) {
108 #if CONFIG_RD_DEBUG
109 int plane;
110 #endif
111 rd_stats->rate = 0;
112 rd_stats->dist = 0;
113 rd_stats->rdcost = 0;
114 rd_stats->sse = 0;
115 rd_stats->skip_txfm = 1;
116 rd_stats->zero_rate = 0;
117 #if CONFIG_RD_DEBUG
118 // This may run into problems when monochrome video is
119 // encoded, as there will only be 1 plane
120 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
121 rd_stats->txb_coeff_cost[plane] = 0;
122 }
123 #endif
124 }
125
av1_invalid_rd_stats(RD_STATS * rd_stats)126 static INLINE void av1_invalid_rd_stats(RD_STATS *rd_stats) {
127 #if CONFIG_RD_DEBUG
128 int plane;
129 #endif
130 rd_stats->rate = INT_MAX;
131 rd_stats->dist = INT64_MAX;
132 rd_stats->rdcost = INT64_MAX;
133 rd_stats->sse = INT64_MAX;
134 rd_stats->skip_txfm = 0;
135 rd_stats->zero_rate = 0;
136 #if CONFIG_RD_DEBUG
137 // This may run into problems when monochrome video is
138 // encoded, as there will only be 1 plane
139 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
140 rd_stats->txb_coeff_cost[plane] = INT_MAX;
141 }
142 #endif
143 }
144
av1_merge_rd_stats(RD_STATS * rd_stats_dst,const RD_STATS * rd_stats_src)145 static INLINE void av1_merge_rd_stats(RD_STATS *rd_stats_dst,
146 const RD_STATS *rd_stats_src) {
147 if (rd_stats_dst->rate == INT_MAX || rd_stats_src->rate == INT_MAX) {
148 // If rd_stats_dst or rd_stats_src has invalid rate, we will make
149 // rd_stats_dst invalid.
150 av1_invalid_rd_stats(rd_stats_dst);
151 return;
152 }
153 rd_stats_dst->rate = (int)AOMMIN(
154 ((int64_t)rd_stats_dst->rate + (int64_t)rd_stats_src->rate), INT_MAX);
155 if (!rd_stats_dst->zero_rate)
156 rd_stats_dst->zero_rate = rd_stats_src->zero_rate;
157 rd_stats_dst->dist += rd_stats_src->dist;
158 if (rd_stats_dst->sse < INT64_MAX && rd_stats_src->sse < INT64_MAX) {
159 rd_stats_dst->sse += rd_stats_src->sse;
160 }
161 rd_stats_dst->skip_txfm &= rd_stats_src->skip_txfm;
162 #if CONFIG_RD_DEBUG
163 // This may run into problems when monochrome video is
164 // encoded, as there will only be 1 plane
165 for (int plane = 0; plane < MAX_MB_PLANE; ++plane) {
166 rd_stats_dst->txb_coeff_cost[plane] += rd_stats_src->txb_coeff_cost[plane];
167 }
168 #endif
169 }
170
av1_accumulate_rd_stats(RD_STATS * rd_stats,int64_t dist,int rate,int skip_txfm,int64_t sse,int zero_rate)171 static INLINE void av1_accumulate_rd_stats(RD_STATS *rd_stats, int64_t dist,
172 int rate, int skip_txfm, int64_t sse,
173 int zero_rate) {
174 assert(rd_stats->rate != INT_MAX && rate != INT_MAX);
175 rd_stats->rate += rate;
176 if (!rd_stats->zero_rate) rd_stats->zero_rate = zero_rate;
177 rd_stats->dist += dist;
178 rd_stats->skip_txfm &= skip_txfm;
179 rd_stats->sse += sse;
180 }
181
av1_calculate_rd_cost(int mult,int rate,int64_t dist)182 static INLINE int64_t av1_calculate_rd_cost(int mult, int rate, int64_t dist) {
183 assert(mult >= 0);
184 if (rate >= 0) {
185 return RDCOST(mult, rate, dist);
186 }
187 return RDCOST_NEG_R(mult, -rate, dist);
188 }
189
av1_rd_cost_update(int mult,RD_STATS * rd_cost)190 static INLINE void av1_rd_cost_update(int mult, RD_STATS *rd_cost) {
191 if (rd_cost->rate < INT_MAX && rd_cost->dist < INT64_MAX &&
192 rd_cost->rdcost < INT64_MAX) {
193 rd_cost->rdcost = av1_calculate_rd_cost(mult, rd_cost->rate, rd_cost->dist);
194 } else {
195 av1_invalid_rd_stats(rd_cost);
196 }
197 }
198
av1_rd_stats_subtraction(int mult,const RD_STATS * const left,const RD_STATS * const right,RD_STATS * result)199 static INLINE void av1_rd_stats_subtraction(int mult,
200 const RD_STATS *const left,
201 const RD_STATS *const right,
202 RD_STATS *result) {
203 if (left->rate == INT_MAX || right->rate == INT_MAX ||
204 left->dist == INT64_MAX || right->dist == INT64_MAX ||
205 left->rdcost == INT64_MAX || right->rdcost == INT64_MAX) {
206 av1_invalid_rd_stats(result);
207 } else {
208 result->rate = left->rate - right->rate;
209 result->dist = left->dist - right->dist;
210 result->rdcost = av1_calculate_rd_cost(mult, result->rate, result->dist);
211 }
212 }
213
214 struct TileInfo;
215 struct TileDataEnc;
216 struct AV1_COMP;
217 struct macroblock;
218
219 /*!\brief Compute rdmult based on q index and frame update type
220 *
221 * \param[in] bit_depth bit depth
222 * \param[in] update_type frame update type
223 * \param[in] qindex q index
224 *
225 * \return rdmult
226 */
227 int av1_compute_rd_mult_based_on_qindex(aom_bit_depth_t bit_depth,
228 FRAME_UPDATE_TYPE update_type,
229 int qindex);
230
231 int av1_compute_rd_mult(const int qindex, const aom_bit_depth_t bit_depth,
232 const FRAME_UPDATE_TYPE update_type,
233 const int layer_depth, const int boost_index,
234 const FRAME_TYPE frame_type,
235 const int use_fixed_qp_offsets,
236 const int is_stat_consumption_stage);
237
238 void av1_initialize_rd_consts(struct AV1_COMP *cpi);
239
240 // Sets the multiplier to convert mv cost to l1 error during motion search.
241 void av1_set_sad_per_bit(const struct AV1_COMP *cpi, int *sadperbit,
242 int qindex);
243
244 void av1_model_rd_from_var_lapndz(int64_t var, unsigned int n,
245 unsigned int qstep, int *rate, int64_t *dist);
246
247 void av1_model_rd_curvfit(BLOCK_SIZE bsize, double sse_norm, double xqr,
248 double *rate_f, double *distbysse_f);
249 void av1_model_rd_surffit(BLOCK_SIZE bsize, double sse_norm, double xm,
250 double yl, double *rate_f, double *distbysse_f);
251
252 int av1_get_switchable_rate(const MACROBLOCK *x, const MACROBLOCKD *xd,
253 InterpFilter interp_filter, int dual_filter);
254
255 YV12_BUFFER_CONFIG *av1_get_scaled_ref_frame(const struct AV1_COMP *cpi,
256 int ref_frame);
257
258 void av1_init_me_luts(void);
259
260 void av1_set_mvcost(MACROBLOCK *x, int ref, int ref_mv_idx);
261
262 void av1_get_entropy_contexts(BLOCK_SIZE plane_bsize,
263 const struct macroblockd_plane *pd,
264 ENTROPY_CONTEXT t_above[MAX_MIB_SIZE],
265 ENTROPY_CONTEXT t_left[MAX_MIB_SIZE]);
266
267 void av1_set_rd_speed_thresholds(struct AV1_COMP *cpi);
268
269 void av1_update_rd_thresh_fact(const AV1_COMMON *const cm,
270 int (*fact)[MAX_MODES], int rd_thresh,
271 BLOCK_SIZE bsize, THR_MODES best_mode_index,
272 THR_MODES inter_mode_start,
273 THR_MODES inter_mode_end,
274 THR_MODES intra_mode_start,
275 THR_MODES intra_mode_end);
276
reset_thresh_freq_fact(MACROBLOCK * const x)277 static INLINE void reset_thresh_freq_fact(MACROBLOCK *const x) {
278 for (int i = 0; i < BLOCK_SIZES_ALL; ++i) {
279 for (int j = 0; j < MAX_MODES; ++j) {
280 x->thresh_freq_fact[i][j] = RD_THRESH_FAC_FRAC_VAL;
281 }
282 }
283 }
284
rd_less_than_thresh(int64_t best_rd,int64_t thresh,int thresh_fact)285 static INLINE int rd_less_than_thresh(int64_t best_rd, int64_t thresh,
286 int thresh_fact) {
287 return best_rd < (thresh * thresh_fact >> 5) || thresh == INT_MAX;
288 }
289
290 void av1_mv_pred(const struct AV1_COMP *cpi, MACROBLOCK *x,
291 uint8_t *ref_y_buffer, int ref_y_stride, int ref_frame,
292 BLOCK_SIZE block_size);
293
294 // Sets the multiplier to convert mv cost to l2 error during motion search.
av1_set_error_per_bit(int * errorperbit,int rdmult)295 static INLINE void av1_set_error_per_bit(int *errorperbit, int rdmult) {
296 *errorperbit = AOMMAX(rdmult >> RD_EPB_SHIFT, 1);
297 }
298
299 // Get the threshold for R-D optimization of coefficients depending upon mode
300 // decision/winner mode processing
get_rd_opt_coeff_thresh(const uint32_t (* const coeff_opt_threshold)[2],TxfmSearchParams * txfm_params,int enable_winner_mode_for_coeff_opt,int is_winner_mode)301 static INLINE void get_rd_opt_coeff_thresh(
302 const uint32_t (*const coeff_opt_threshold)[2],
303 TxfmSearchParams *txfm_params, int enable_winner_mode_for_coeff_opt,
304 int is_winner_mode) {
305 if (!enable_winner_mode_for_coeff_opt) {
306 // Default initialization of threshold
307 txfm_params->coeff_opt_thresholds[0] = coeff_opt_threshold[DEFAULT_EVAL][0];
308 txfm_params->coeff_opt_thresholds[1] = coeff_opt_threshold[DEFAULT_EVAL][1];
309 return;
310 }
311 // TODO(any): Experiment with coeff_opt_dist_threshold values when
312 // enable_winner_mode_for_coeff_opt is ON
313 // TODO(any): Skip the winner mode processing for blocks with lower residual
314 // energy as R-D optimization of coefficients would have been enabled during
315 // mode decision
316
317 // Use conservative threshold during mode decision and perform R-D
318 // optimization of coeffs always for winner modes
319 if (is_winner_mode) {
320 txfm_params->coeff_opt_thresholds[0] =
321 coeff_opt_threshold[WINNER_MODE_EVAL][0];
322 txfm_params->coeff_opt_thresholds[1] =
323 coeff_opt_threshold[WINNER_MODE_EVAL][1];
324 } else {
325 txfm_params->coeff_opt_thresholds[0] = coeff_opt_threshold[MODE_EVAL][0];
326 txfm_params->coeff_opt_thresholds[1] = coeff_opt_threshold[MODE_EVAL][1];
327 }
328 }
329
330 // Used to reset the state of mb rd hash information
reset_mb_rd_record(MB_RD_RECORD * const mb_rd_record)331 static INLINE void reset_mb_rd_record(MB_RD_RECORD *const mb_rd_record) {
332 if (!mb_rd_record) return;
333
334 // Reset the state for use_mb_rd_hash
335 mb_rd_record->num = mb_rd_record->index_start = 0;
336 }
337
338 void av1_setup_pred_block(const MACROBLOCKD *xd,
339 struct buf_2d dst[MAX_MB_PLANE],
340 const YV12_BUFFER_CONFIG *src,
341 const struct scale_factors *scale,
342 const struct scale_factors *scale_uv,
343 const int num_planes);
344
345 int av1_get_intra_cost_penalty(int qindex, int qdelta,
346 aom_bit_depth_t bit_depth);
347
348 void av1_fill_mode_rates(AV1_COMMON *const cm, ModeCosts *mode_costs,
349 FRAME_CONTEXT *fc);
350
351 void av1_fill_lr_rates(ModeCosts *mode_costs, FRAME_CONTEXT *fc);
352
353 void av1_fill_coeff_costs(CoeffCosts *coeff_costs, FRAME_CONTEXT *fc,
354 const int num_planes);
355
356 void av1_fill_mv_costs(const nmv_context *nmvc, int integer_mv, int usehp,
357 MvCosts *mv_costs);
358
359 void av1_fill_dv_costs(const nmv_context *ndvc, IntraBCMVCosts *dv_costs);
360
361 int av1_get_adaptive_rdmult(const struct AV1_COMP *cpi, double beta);
362
363 int av1_get_deltaq_offset(aom_bit_depth_t bit_depth, int qindex, double beta);
364
365 /*!\brief Adjust current superblock's q_index based on delta q resolution
366 *
367 * \param[in] delta_q_res delta q resolution
368 * \param[in] prev_qindex previous superblock's q index
369 * \param[in] curr_qindex current superblock's q index
370 *
371 * \return the current superblock's adjusted q_index
372 */
373 int av1_adjust_q_from_delta_q_res(int delta_q_res, int prev_qindex,
374 int curr_qindex);
375
376 #ifdef __cplusplus
377 } // extern "C"
378 #endif
379
380 #endif // AOM_AV1_ENCODER_RD_H_
381