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