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