1 /*
2 * Copyright (c) 2019, 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_RDOPT_UTILS_H_
13 #define AOM_AV1_ENCODER_RDOPT_UTILS_H_
14
15 #include "aom/aom_integer.h"
16 #include "av1/encoder/block.h"
17 #include "av1/common/cfl.h"
18 #include "av1/common/pred_common.h"
19 #include "av1/encoder/rdopt_data_defs.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 #define MAX_REF_MV_SEARCH 3
26 #define INTER_INTRA_RD_THRESH_SCALE 9
27 #define INTER_INTRA_RD_THRESH_SHIFT 4
28
29 typedef struct {
30 PREDICTION_MODE mode;
31 MV_REFERENCE_FRAME ref_frame[2];
32 } MODE_DEFINITION;
33
34 // This array defines the mapping from the enums in THR_MODES to the actual
35 // prediction modes and refrence frames
36 static const MODE_DEFINITION av1_mode_defs[MAX_MODES] = {
37 { NEARESTMV, { LAST_FRAME, NONE_FRAME } },
38 { NEARESTMV, { LAST2_FRAME, NONE_FRAME } },
39 { NEARESTMV, { LAST3_FRAME, NONE_FRAME } },
40 { NEARESTMV, { BWDREF_FRAME, NONE_FRAME } },
41 { NEARESTMV, { ALTREF2_FRAME, NONE_FRAME } },
42 { NEARESTMV, { ALTREF_FRAME, NONE_FRAME } },
43 { NEARESTMV, { GOLDEN_FRAME, NONE_FRAME } },
44
45 { NEWMV, { LAST_FRAME, NONE_FRAME } },
46 { NEWMV, { LAST2_FRAME, NONE_FRAME } },
47 { NEWMV, { LAST3_FRAME, NONE_FRAME } },
48 { NEWMV, { BWDREF_FRAME, NONE_FRAME } },
49 { NEWMV, { ALTREF2_FRAME, NONE_FRAME } },
50 { NEWMV, { ALTREF_FRAME, NONE_FRAME } },
51 { NEWMV, { GOLDEN_FRAME, NONE_FRAME } },
52
53 { NEARMV, { LAST_FRAME, NONE_FRAME } },
54 { NEARMV, { LAST2_FRAME, NONE_FRAME } },
55 { NEARMV, { LAST3_FRAME, NONE_FRAME } },
56 { NEARMV, { BWDREF_FRAME, NONE_FRAME } },
57 { NEARMV, { ALTREF2_FRAME, NONE_FRAME } },
58 { NEARMV, { ALTREF_FRAME, NONE_FRAME } },
59 { NEARMV, { GOLDEN_FRAME, NONE_FRAME } },
60
61 { GLOBALMV, { LAST_FRAME, NONE_FRAME } },
62 { GLOBALMV, { LAST2_FRAME, NONE_FRAME } },
63 { GLOBALMV, { LAST3_FRAME, NONE_FRAME } },
64 { GLOBALMV, { BWDREF_FRAME, NONE_FRAME } },
65 { GLOBALMV, { ALTREF2_FRAME, NONE_FRAME } },
66 { GLOBALMV, { ALTREF_FRAME, NONE_FRAME } },
67 { GLOBALMV, { GOLDEN_FRAME, NONE_FRAME } },
68
69 // TODO(zoeliu): May need to reconsider the order on the modes to check
70
71 { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } },
72 { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } },
73 { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } },
74 { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } },
75 { NEAREST_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } },
76 { NEAREST_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } },
77 { NEAREST_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } },
78 { NEAREST_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } },
79 { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } },
80 { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } },
81 { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } },
82 { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
83
84 { NEAREST_NEARESTMV, { LAST_FRAME, LAST2_FRAME } },
85 { NEAREST_NEARESTMV, { LAST_FRAME, LAST3_FRAME } },
86 { NEAREST_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } },
87 { NEAREST_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } },
88
89 { NEAR_NEARMV, { LAST_FRAME, BWDREF_FRAME } },
90 { NEW_NEWMV, { LAST_FRAME, BWDREF_FRAME } },
91 { NEW_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } },
92 { NEAREST_NEWMV, { LAST_FRAME, BWDREF_FRAME } },
93 { NEW_NEARMV, { LAST_FRAME, BWDREF_FRAME } },
94 { NEAR_NEWMV, { LAST_FRAME, BWDREF_FRAME } },
95 { GLOBAL_GLOBALMV, { LAST_FRAME, BWDREF_FRAME } },
96
97 { NEAR_NEARMV, { LAST_FRAME, ALTREF_FRAME } },
98 { NEW_NEWMV, { LAST_FRAME, ALTREF_FRAME } },
99 { NEW_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } },
100 { NEAREST_NEWMV, { LAST_FRAME, ALTREF_FRAME } },
101 { NEW_NEARMV, { LAST_FRAME, ALTREF_FRAME } },
102 { NEAR_NEWMV, { LAST_FRAME, ALTREF_FRAME } },
103 { GLOBAL_GLOBALMV, { LAST_FRAME, ALTREF_FRAME } },
104
105 { NEAR_NEARMV, { LAST2_FRAME, ALTREF_FRAME } },
106 { NEW_NEWMV, { LAST2_FRAME, ALTREF_FRAME } },
107 { NEW_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } },
108 { NEAREST_NEWMV, { LAST2_FRAME, ALTREF_FRAME } },
109 { NEW_NEARMV, { LAST2_FRAME, ALTREF_FRAME } },
110 { NEAR_NEWMV, { LAST2_FRAME, ALTREF_FRAME } },
111 { GLOBAL_GLOBALMV, { LAST2_FRAME, ALTREF_FRAME } },
112
113 { NEAR_NEARMV, { LAST3_FRAME, ALTREF_FRAME } },
114 { NEW_NEWMV, { LAST3_FRAME, ALTREF_FRAME } },
115 { NEW_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } },
116 { NEAREST_NEWMV, { LAST3_FRAME, ALTREF_FRAME } },
117 { NEW_NEARMV, { LAST3_FRAME, ALTREF_FRAME } },
118 { NEAR_NEWMV, { LAST3_FRAME, ALTREF_FRAME } },
119 { GLOBAL_GLOBALMV, { LAST3_FRAME, ALTREF_FRAME } },
120
121 { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } },
122 { NEW_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } },
123 { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } },
124 { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } },
125 { NEW_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } },
126 { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } },
127 { GLOBAL_GLOBALMV, { GOLDEN_FRAME, ALTREF_FRAME } },
128
129 { NEAR_NEARMV, { LAST2_FRAME, BWDREF_FRAME } },
130 { NEW_NEWMV, { LAST2_FRAME, BWDREF_FRAME } },
131 { NEW_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } },
132 { NEAREST_NEWMV, { LAST2_FRAME, BWDREF_FRAME } },
133 { NEW_NEARMV, { LAST2_FRAME, BWDREF_FRAME } },
134 { NEAR_NEWMV, { LAST2_FRAME, BWDREF_FRAME } },
135 { GLOBAL_GLOBALMV, { LAST2_FRAME, BWDREF_FRAME } },
136
137 { NEAR_NEARMV, { LAST3_FRAME, BWDREF_FRAME } },
138 { NEW_NEWMV, { LAST3_FRAME, BWDREF_FRAME } },
139 { NEW_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } },
140 { NEAREST_NEWMV, { LAST3_FRAME, BWDREF_FRAME } },
141 { NEW_NEARMV, { LAST3_FRAME, BWDREF_FRAME } },
142 { NEAR_NEWMV, { LAST3_FRAME, BWDREF_FRAME } },
143 { GLOBAL_GLOBALMV, { LAST3_FRAME, BWDREF_FRAME } },
144
145 { NEAR_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } },
146 { NEW_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } },
147 { NEW_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } },
148 { NEAREST_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } },
149 { NEW_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } },
150 { NEAR_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } },
151 { GLOBAL_GLOBALMV, { GOLDEN_FRAME, BWDREF_FRAME } },
152
153 { NEAR_NEARMV, { LAST_FRAME, ALTREF2_FRAME } },
154 { NEW_NEWMV, { LAST_FRAME, ALTREF2_FRAME } },
155 { NEW_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } },
156 { NEAREST_NEWMV, { LAST_FRAME, ALTREF2_FRAME } },
157 { NEW_NEARMV, { LAST_FRAME, ALTREF2_FRAME } },
158 { NEAR_NEWMV, { LAST_FRAME, ALTREF2_FRAME } },
159 { GLOBAL_GLOBALMV, { LAST_FRAME, ALTREF2_FRAME } },
160
161 { NEAR_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } },
162 { NEW_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } },
163 { NEW_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } },
164 { NEAREST_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } },
165 { NEW_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } },
166 { NEAR_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } },
167 { GLOBAL_GLOBALMV, { LAST2_FRAME, ALTREF2_FRAME } },
168
169 { NEAR_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } },
170 { NEW_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } },
171 { NEW_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } },
172 { NEAREST_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } },
173 { NEW_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } },
174 { NEAR_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } },
175 { GLOBAL_GLOBALMV, { LAST3_FRAME, ALTREF2_FRAME } },
176
177 { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
178 { NEW_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
179 { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
180 { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
181 { NEW_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
182 { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
183 { GLOBAL_GLOBALMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
184
185 { NEAR_NEARMV, { LAST_FRAME, LAST2_FRAME } },
186 { NEW_NEWMV, { LAST_FRAME, LAST2_FRAME } },
187 { NEW_NEARESTMV, { LAST_FRAME, LAST2_FRAME } },
188 { NEAREST_NEWMV, { LAST_FRAME, LAST2_FRAME } },
189 { NEW_NEARMV, { LAST_FRAME, LAST2_FRAME } },
190 { NEAR_NEWMV, { LAST_FRAME, LAST2_FRAME } },
191 { GLOBAL_GLOBALMV, { LAST_FRAME, LAST2_FRAME } },
192
193 { NEAR_NEARMV, { LAST_FRAME, LAST3_FRAME } },
194 { NEW_NEWMV, { LAST_FRAME, LAST3_FRAME } },
195 { NEW_NEARESTMV, { LAST_FRAME, LAST3_FRAME } },
196 { NEAREST_NEWMV, { LAST_FRAME, LAST3_FRAME } },
197 { NEW_NEARMV, { LAST_FRAME, LAST3_FRAME } },
198 { NEAR_NEWMV, { LAST_FRAME, LAST3_FRAME } },
199 { GLOBAL_GLOBALMV, { LAST_FRAME, LAST3_FRAME } },
200
201 { NEAR_NEARMV, { LAST_FRAME, GOLDEN_FRAME } },
202 { NEW_NEWMV, { LAST_FRAME, GOLDEN_FRAME } },
203 { NEW_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } },
204 { NEAREST_NEWMV, { LAST_FRAME, GOLDEN_FRAME } },
205 { NEW_NEARMV, { LAST_FRAME, GOLDEN_FRAME } },
206 { NEAR_NEWMV, { LAST_FRAME, GOLDEN_FRAME } },
207 { GLOBAL_GLOBALMV, { LAST_FRAME, GOLDEN_FRAME } },
208
209 { NEAR_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } },
210 { NEW_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } },
211 { NEW_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } },
212 { NEAREST_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } },
213 { NEW_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } },
214 { NEAR_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } },
215 { GLOBAL_GLOBALMV, { BWDREF_FRAME, ALTREF_FRAME } },
216
217 // intra modes
218 { DC_PRED, { INTRA_FRAME, NONE_FRAME } },
219 { PAETH_PRED, { INTRA_FRAME, NONE_FRAME } },
220 { SMOOTH_PRED, { INTRA_FRAME, NONE_FRAME } },
221 { SMOOTH_V_PRED, { INTRA_FRAME, NONE_FRAME } },
222 { SMOOTH_H_PRED, { INTRA_FRAME, NONE_FRAME } },
223 { H_PRED, { INTRA_FRAME, NONE_FRAME } },
224 { V_PRED, { INTRA_FRAME, NONE_FRAME } },
225 { D135_PRED, { INTRA_FRAME, NONE_FRAME } },
226 { D203_PRED, { INTRA_FRAME, NONE_FRAME } },
227 { D157_PRED, { INTRA_FRAME, NONE_FRAME } },
228 { D67_PRED, { INTRA_FRAME, NONE_FRAME } },
229 { D113_PRED, { INTRA_FRAME, NONE_FRAME } },
230 { D45_PRED, { INTRA_FRAME, NONE_FRAME } },
231 };
232
restore_dst_buf(MACROBLOCKD * xd,const BUFFER_SET dst,const int num_planes)233 static AOM_INLINE void restore_dst_buf(MACROBLOCKD *xd, const BUFFER_SET dst,
234 const int num_planes) {
235 for (int i = 0; i < num_planes; i++) {
236 xd->plane[i].dst.buf = dst.plane[i];
237 xd->plane[i].dst.stride = dst.stride[i];
238 }
239 }
240
241 /* clang-format on */
242 // Calculate rd threshold based on ref best rd and relevant scaling factors
get_rd_thresh_from_best_rd(int64_t ref_best_rd,int mul_factor,int div_factor)243 static AOM_INLINE int64_t get_rd_thresh_from_best_rd(int64_t ref_best_rd,
244 int mul_factor,
245 int div_factor) {
246 int64_t rd_thresh = ref_best_rd;
247 if (div_factor != 0) {
248 rd_thresh = ref_best_rd < (div_factor * (INT64_MAX / mul_factor))
249 ? ((ref_best_rd / div_factor) * mul_factor)
250 : INT64_MAX;
251 }
252 return rd_thresh;
253 }
254
255 static AOM_INLINE THR_MODES
get_prediction_mode_idx(PREDICTION_MODE this_mode,MV_REFERENCE_FRAME ref_frame,MV_REFERENCE_FRAME second_ref_frame)256 get_prediction_mode_idx(PREDICTION_MODE this_mode, MV_REFERENCE_FRAME ref_frame,
257 MV_REFERENCE_FRAME second_ref_frame) {
258 if (this_mode < INTRA_MODE_END) {
259 assert(ref_frame == INTRA_FRAME);
260 assert(second_ref_frame == NONE_FRAME);
261 return intra_to_mode_idx[this_mode - INTRA_MODE_START];
262 }
263 if (this_mode >= SINGLE_INTER_MODE_START &&
264 this_mode < SINGLE_INTER_MODE_END) {
265 assert((ref_frame > INTRA_FRAME) && (ref_frame <= ALTREF_FRAME));
266 return single_inter_to_mode_idx[this_mode - SINGLE_INTER_MODE_START]
267 [ref_frame];
268 }
269 if (this_mode >= COMP_INTER_MODE_START && this_mode < COMP_INTER_MODE_END &&
270 second_ref_frame != NONE_FRAME) {
271 assert((ref_frame > INTRA_FRAME) && (ref_frame <= ALTREF_FRAME));
272 assert((second_ref_frame > INTRA_FRAME) &&
273 (second_ref_frame <= ALTREF_FRAME));
274 return comp_inter_to_mode_idx[this_mode - COMP_INTER_MODE_START][ref_frame]
275 [second_ref_frame];
276 }
277 assert(0);
278 return THR_INVALID;
279 }
280
inter_mode_data_block_idx(BLOCK_SIZE bsize)281 static AOM_INLINE int inter_mode_data_block_idx(BLOCK_SIZE bsize) {
282 if (bsize == BLOCK_4X4 || bsize == BLOCK_4X8 || bsize == BLOCK_8X4 ||
283 bsize == BLOCK_4X16 || bsize == BLOCK_16X4) {
284 return -1;
285 }
286 return 1;
287 }
288
289 // Get transform block visible dimensions cropped to the MI units.
get_txb_dimensions(const MACROBLOCKD * xd,int plane,BLOCK_SIZE plane_bsize,int blk_row,int blk_col,BLOCK_SIZE tx_bsize,int * width,int * height,int * visible_width,int * visible_height)290 static AOM_INLINE void get_txb_dimensions(const MACROBLOCKD *xd, int plane,
291 BLOCK_SIZE plane_bsize, int blk_row,
292 int blk_col, BLOCK_SIZE tx_bsize,
293 int *width, int *height,
294 int *visible_width,
295 int *visible_height) {
296 assert(tx_bsize <= plane_bsize);
297 const int txb_height = block_size_high[tx_bsize];
298 const int txb_width = block_size_wide[tx_bsize];
299 const struct macroblockd_plane *const pd = &xd->plane[plane];
300
301 // TODO(aconverse@google.com): Investigate using crop_width/height here rather
302 // than the MI size
303 if (xd->mb_to_bottom_edge >= 0) {
304 *visible_height = txb_height;
305 } else {
306 const int block_height = block_size_high[plane_bsize];
307 const int block_rows =
308 (xd->mb_to_bottom_edge >> (3 + pd->subsampling_y)) + block_height;
309 *visible_height =
310 clamp(block_rows - (blk_row << MI_SIZE_LOG2), 0, txb_height);
311 }
312 if (height) *height = txb_height;
313
314 if (xd->mb_to_right_edge >= 0) {
315 *visible_width = txb_width;
316 } else {
317 const int block_width = block_size_wide[plane_bsize];
318 const int block_cols =
319 (xd->mb_to_right_edge >> (3 + pd->subsampling_x)) + block_width;
320 *visible_width =
321 clamp(block_cols - (blk_col << MI_SIZE_LOG2), 0, txb_width);
322 }
323 if (width) *width = txb_width;
324 }
325
bsize_to_num_blk(BLOCK_SIZE bsize)326 static AOM_INLINE int bsize_to_num_blk(BLOCK_SIZE bsize) {
327 int num_blk = 1 << (num_pels_log2_lookup[bsize] - 2 * MI_SIZE_LOG2);
328 return num_blk;
329 }
330
check_txfm_eval(MACROBLOCK * const x,BLOCK_SIZE bsize,int64_t best_skip_rd,int64_t skip_rd,int level,int is_luma_only)331 static INLINE int check_txfm_eval(MACROBLOCK *const x, BLOCK_SIZE bsize,
332 int64_t best_skip_rd, int64_t skip_rd,
333 int level, int is_luma_only) {
334 int eval_txfm = 1;
335 // Derive aggressiveness factor for gating the transform search
336 // Lower value indicates more aggressiveness. Be more conservative (high
337 // value) for (i) low quantizers (ii) regions where prediction is poor
338 const int scale[5] = { INT_MAX, 4, 3, 2, 2 };
339 const int qslope = 2 * (!is_luma_only);
340 const int level_to_qindex_map[5] = { 0, 0, 0, 80, 100 };
341 int aggr_factor = 4;
342 const int pred_qindex_thresh = level_to_qindex_map[level];
343 if (!is_luma_only && level <= 2) {
344 aggr_factor = 4 * AOMMAX(1, ROUND_POWER_OF_TWO((MAXQ - x->qindex) * qslope,
345 QINDEX_BITS));
346 }
347 if ((best_skip_rd >
348 (x->source_variance << (num_pels_log2_lookup[bsize] + RDDIV_BITS))) &&
349 (x->qindex >= pred_qindex_thresh))
350 aggr_factor *= scale[level];
351 // For level setting 1, be more conservative for non-luma-only case even when
352 // prediction is good.
353 else if ((level <= 1) && !is_luma_only)
354 aggr_factor = (aggr_factor >> 2) * 6;
355
356 // Be more conservative for luma only cases (called from compound type rd)
357 // since best_skip_rd is computed after and skip_rd is computed (with 8-bit
358 // prediction signals blended for WEDGE/DIFFWTD rather than 16-bit) before
359 // interpolation filter search
360 const int luma_mul[5] = { INT_MAX, 32, 29, 17, 17 };
361 int mul_factor = is_luma_only ? luma_mul[level] : 16;
362 int64_t rd_thresh =
363 (best_skip_rd == INT64_MAX)
364 ? best_skip_rd
365 : (int64_t)(best_skip_rd * aggr_factor * mul_factor >> 6);
366 if (skip_rd > rd_thresh) eval_txfm = 0;
367 return eval_txfm;
368 }
369
select_tx_mode(const AV1_COMMON * cm,const TX_SIZE_SEARCH_METHOD tx_size_search_method)370 static TX_MODE select_tx_mode(
371 const AV1_COMMON *cm, const TX_SIZE_SEARCH_METHOD tx_size_search_method) {
372 if (cm->features.coded_lossless) return ONLY_4X4;
373 if (tx_size_search_method == USE_LARGESTALL) {
374 return TX_MODE_LARGEST;
375 } else {
376 assert(tx_size_search_method == USE_FULL_RD ||
377 tx_size_search_method == USE_FAST_RD);
378 return TX_MODE_SELECT;
379 }
380 }
381 // Checks the conditions to enable winner mode processing
is_winner_mode_processing_enabled(const struct AV1_COMP * cpi,MB_MODE_INFO * const mbmi,const PREDICTION_MODE best_mode)382 static INLINE int is_winner_mode_processing_enabled(
383 const struct AV1_COMP *cpi, MB_MODE_INFO *const mbmi,
384 const PREDICTION_MODE best_mode) {
385 const SPEED_FEATURES *sf = &cpi->sf;
386
387 // TODO(any): Move block independent condition checks to frame level
388 if (is_inter_block(mbmi)) {
389 if (is_inter_mode(best_mode) &&
390 (sf->tx_sf.tx_type_search.fast_inter_tx_type_prob_thresh != INT_MAX) &&
391 !cpi->oxcf.txfm_cfg.use_inter_dct_only)
392 return 1;
393 } else {
394 if (sf->tx_sf.tx_type_search.fast_intra_tx_type_search &&
395 !cpi->oxcf.txfm_cfg.use_intra_default_tx_only &&
396 !cpi->oxcf.txfm_cfg.use_intra_dct_only)
397 return 1;
398 }
399
400 // Check speed feature related to winner mode processing
401 if (sf->winner_mode_sf.enable_winner_mode_for_coeff_opt &&
402 cpi->optimize_seg_arr[mbmi->segment_id] != NO_TRELLIS_OPT &&
403 cpi->optimize_seg_arr[mbmi->segment_id] != FINAL_PASS_TRELLIS_OPT)
404 return 1;
405 if (sf->winner_mode_sf.enable_winner_mode_for_tx_size_srch) return 1;
406
407 return 0;
408 }
409
set_tx_size_search_method(const AV1_COMMON * cm,const WinnerModeParams * winner_mode_params,TxfmSearchParams * txfm_params,int enable_winner_mode_for_tx_size_srch,int is_winner_mode)410 static INLINE void set_tx_size_search_method(
411 const AV1_COMMON *cm, const WinnerModeParams *winner_mode_params,
412 TxfmSearchParams *txfm_params, int enable_winner_mode_for_tx_size_srch,
413 int is_winner_mode) {
414 // Populate transform size search method/transform mode appropriately
415 txfm_params->tx_size_search_method =
416 winner_mode_params->tx_size_search_methods[DEFAULT_EVAL];
417 if (enable_winner_mode_for_tx_size_srch) {
418 if (is_winner_mode)
419 txfm_params->tx_size_search_method =
420 winner_mode_params->tx_size_search_methods[WINNER_MODE_EVAL];
421 else
422 txfm_params->tx_size_search_method =
423 winner_mode_params->tx_size_search_methods[MODE_EVAL];
424 }
425 txfm_params->tx_mode_search_type =
426 select_tx_mode(cm, txfm_params->tx_size_search_method);
427 }
428
set_tx_type_prune(const SPEED_FEATURES * sf,TxfmSearchParams * txfm_params,int winner_mode_tx_type_pruning,int is_winner_mode)429 static INLINE void set_tx_type_prune(const SPEED_FEATURES *sf,
430 TxfmSearchParams *txfm_params,
431 int winner_mode_tx_type_pruning,
432 int is_winner_mode) {
433 // Populate prune transform mode appropriately
434 txfm_params->prune_2d_txfm_mode = sf->tx_sf.tx_type_search.prune_2d_txfm_mode;
435 if (!winner_mode_tx_type_pruning) return;
436
437 const int prune_mode[4][2] = { { TX_TYPE_PRUNE_3, TX_TYPE_PRUNE_0 },
438 { TX_TYPE_PRUNE_4, TX_TYPE_PRUNE_0 },
439 { TX_TYPE_PRUNE_5, TX_TYPE_PRUNE_2 },
440 { TX_TYPE_PRUNE_5, TX_TYPE_PRUNE_3 } };
441 txfm_params->prune_2d_txfm_mode =
442 prune_mode[winner_mode_tx_type_pruning - 1][is_winner_mode];
443 }
444
set_tx_domain_dist_params(const WinnerModeParams * winner_mode_params,TxfmSearchParams * txfm_params,int enable_winner_mode_for_tx_domain_dist,int is_winner_mode)445 static INLINE void set_tx_domain_dist_params(
446 const WinnerModeParams *winner_mode_params, TxfmSearchParams *txfm_params,
447 int enable_winner_mode_for_tx_domain_dist, int is_winner_mode) {
448 if (!enable_winner_mode_for_tx_domain_dist) {
449 txfm_params->use_transform_domain_distortion =
450 winner_mode_params->use_transform_domain_distortion[DEFAULT_EVAL];
451 txfm_params->tx_domain_dist_threshold =
452 winner_mode_params->tx_domain_dist_threshold[DEFAULT_EVAL];
453 return;
454 }
455
456 if (is_winner_mode) {
457 txfm_params->use_transform_domain_distortion =
458 winner_mode_params->use_transform_domain_distortion[WINNER_MODE_EVAL];
459 txfm_params->tx_domain_dist_threshold =
460 winner_mode_params->tx_domain_dist_threshold[WINNER_MODE_EVAL];
461 } else {
462 txfm_params->use_transform_domain_distortion =
463 winner_mode_params->use_transform_domain_distortion[MODE_EVAL];
464 txfm_params->tx_domain_dist_threshold =
465 winner_mode_params->tx_domain_dist_threshold[MODE_EVAL];
466 }
467 }
468
469 // This function sets mode parameters for different mode evaluation stages
set_mode_eval_params(const struct AV1_COMP * cpi,MACROBLOCK * x,MODE_EVAL_TYPE mode_eval_type)470 static INLINE void set_mode_eval_params(const struct AV1_COMP *cpi,
471 MACROBLOCK *x,
472 MODE_EVAL_TYPE mode_eval_type) {
473 const AV1_COMMON *cm = &cpi->common;
474 const SPEED_FEATURES *sf = &cpi->sf;
475 const WinnerModeParams *winner_mode_params = &cpi->winner_mode_params;
476 TxfmSearchParams *txfm_params = &x->txfm_search_params;
477 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
478
479 switch (mode_eval_type) {
480 case DEFAULT_EVAL:
481 txfm_params->default_inter_tx_type_prob_thresh = INT_MAX;
482 txfm_params->use_default_intra_tx_type = 0;
483 txfm_params->skip_txfm_level =
484 winner_mode_params->skip_txfm_level[DEFAULT_EVAL];
485 txfm_params->predict_dc_level =
486 winner_mode_params->predict_dc_level[DEFAULT_EVAL];
487 // Set default transform domain distortion type
488 set_tx_domain_dist_params(winner_mode_params, txfm_params, 0, 0);
489
490 // Get default threshold for R-D optimization of coefficients
491 get_rd_opt_coeff_thresh(winner_mode_params->coeff_opt_thresholds,
492 txfm_params, 0, 0);
493
494 // Set default transform size search method
495 set_tx_size_search_method(cm, winner_mode_params, txfm_params, 0, 0);
496 // Set default transform type prune
497 set_tx_type_prune(sf, txfm_params, 0, 0);
498 break;
499 case MODE_EVAL:
500 txfm_params->use_default_intra_tx_type =
501 (cpi->sf.tx_sf.tx_type_search.fast_intra_tx_type_search ||
502 cpi->oxcf.txfm_cfg.use_intra_default_tx_only);
503 txfm_params->default_inter_tx_type_prob_thresh =
504 cpi->sf.tx_sf.tx_type_search.fast_inter_tx_type_prob_thresh;
505 txfm_params->skip_txfm_level =
506 winner_mode_params->skip_txfm_level[MODE_EVAL];
507 txfm_params->predict_dc_level =
508 winner_mode_params->predict_dc_level[MODE_EVAL];
509 // Set transform domain distortion type for mode evaluation
510 set_tx_domain_dist_params(
511 winner_mode_params, txfm_params,
512 sf->winner_mode_sf.enable_winner_mode_for_use_tx_domain_dist, 0);
513
514 // Get threshold for R-D optimization of coefficients during mode
515 // evaluation
516 get_rd_opt_coeff_thresh(
517 winner_mode_params->coeff_opt_thresholds, txfm_params,
518 sf->winner_mode_sf.enable_winner_mode_for_coeff_opt, 0);
519
520 // Set the transform size search method for mode evaluation
521 set_tx_size_search_method(
522 cm, winner_mode_params, txfm_params,
523 sf->winner_mode_sf.enable_winner_mode_for_tx_size_srch, 0);
524 // Set transform type prune for mode evaluation
525 set_tx_type_prune(sf, txfm_params,
526 sf->tx_sf.tx_type_search.winner_mode_tx_type_pruning,
527 0);
528 break;
529 case WINNER_MODE_EVAL:
530 txfm_params->default_inter_tx_type_prob_thresh = INT_MAX;
531 txfm_params->use_default_intra_tx_type = 0;
532 txfm_params->skip_txfm_level =
533 winner_mode_params->skip_txfm_level[WINNER_MODE_EVAL];
534 txfm_params->predict_dc_level =
535 winner_mode_params->predict_dc_level[WINNER_MODE_EVAL];
536
537 // Set transform domain distortion type for winner mode evaluation
538 set_tx_domain_dist_params(
539 winner_mode_params, txfm_params,
540 sf->winner_mode_sf.enable_winner_mode_for_use_tx_domain_dist, 1);
541
542 // Get threshold for R-D optimization of coefficients for winner mode
543 // evaluation
544 get_rd_opt_coeff_thresh(
545 winner_mode_params->coeff_opt_thresholds, txfm_params,
546 sf->winner_mode_sf.enable_winner_mode_for_coeff_opt, 1);
547
548 // Set the transform size search method for winner mode evaluation
549 set_tx_size_search_method(
550 cm, winner_mode_params, txfm_params,
551 sf->winner_mode_sf.enable_winner_mode_for_tx_size_srch, 1);
552 // Set default transform type prune mode for winner mode evaluation
553 set_tx_type_prune(sf, txfm_params,
554 sf->tx_sf.tx_type_search.winner_mode_tx_type_pruning,
555 1);
556
557 // Reset hash state for winner mode processing. Winner mode and subsequent
558 // transform/mode evaluations (palette/IntraBC) cann't reuse old data as
559 // the decisions would have been sub-optimal
560 // TODO(any): Move the evaluation of palette/IntraBC modes before winner
561 // mode is processed and clean-up the code below
562 reset_hash_records(txfm_info, cpi->sf.tx_sf.use_inter_txb_hash);
563
564 break;
565 default: assert(0);
566 }
567 }
568
569 // Similar to store_cfl_required(), but for use during the RDO process,
570 // where we haven't yet determined whether this block uses CfL.
store_cfl_required_rdo(const AV1_COMMON * cm,const MACROBLOCK * x)571 static INLINE CFL_ALLOWED_TYPE store_cfl_required_rdo(const AV1_COMMON *cm,
572 const MACROBLOCK *x) {
573 const MACROBLOCKD *xd = &x->e_mbd;
574
575 if (cm->seq_params->monochrome || !xd->is_chroma_ref) return CFL_DISALLOWED;
576
577 if (!xd->is_chroma_ref) {
578 // For non-chroma-reference blocks, we should always store the luma pixels,
579 // in case the corresponding chroma-reference block uses CfL.
580 // Note that this can only happen for block sizes which are <8 on
581 // their shortest side, as otherwise they would be chroma reference
582 // blocks.
583 return CFL_ALLOWED;
584 }
585
586 // For chroma reference blocks, we should store data in the encoder iff we're
587 // allowed to try out CfL.
588 return is_cfl_allowed(xd);
589 }
590
init_sbuv_mode(MB_MODE_INFO * const mbmi)591 static AOM_INLINE void init_sbuv_mode(MB_MODE_INFO *const mbmi) {
592 mbmi->uv_mode = UV_DC_PRED;
593 mbmi->palette_mode_info.palette_size[1] = 0;
594 }
595
596 // Store best mode stats for winner mode processing
store_winner_mode_stats(const AV1_COMMON * const cm,MACROBLOCK * x,const MB_MODE_INFO * mbmi,RD_STATS * rd_cost,RD_STATS * rd_cost_y,RD_STATS * rd_cost_uv,THR_MODES mode_index,uint8_t * color_map,BLOCK_SIZE bsize,int64_t this_rd,int multi_winner_mode_type,int txfm_search_done)597 static INLINE void store_winner_mode_stats(
598 const AV1_COMMON *const cm, MACROBLOCK *x, const MB_MODE_INFO *mbmi,
599 RD_STATS *rd_cost, RD_STATS *rd_cost_y, RD_STATS *rd_cost_uv,
600 THR_MODES mode_index, uint8_t *color_map, BLOCK_SIZE bsize, int64_t this_rd,
601 int multi_winner_mode_type, int txfm_search_done) {
602 WinnerModeStats *winner_mode_stats = x->winner_mode_stats;
603 int mode_idx = 0;
604 int is_palette_mode = mbmi->palette_mode_info.palette_size[PLANE_TYPE_Y] > 0;
605 // Mode stat is not required when multiwinner mode processing is disabled
606 if (multi_winner_mode_type == MULTI_WINNER_MODE_OFF) return;
607 // Ignore mode with maximum rd
608 if (this_rd == INT64_MAX) return;
609 // TODO(any): Winner mode processing is currently not applicable for palette
610 // mode in Inter frames. Clean-up the following code, once support is added
611 if (!frame_is_intra_only(cm) && is_palette_mode) return;
612
613 int max_winner_mode_count = frame_is_intra_only(cm)
614 ? MAX_WINNER_MODE_COUNT_INTRA
615 : MAX_WINNER_MODE_COUNT_INTER;
616 max_winner_mode_count = (multi_winner_mode_type == MULTI_WINNER_MODE_FAST)
617 ? AOMMIN(max_winner_mode_count, 2)
618 : max_winner_mode_count;
619 assert(x->winner_mode_count >= 0 &&
620 x->winner_mode_count <= max_winner_mode_count);
621
622 if (x->winner_mode_count) {
623 // Find the mode which has higher rd cost than this_rd
624 for (mode_idx = 0; mode_idx < x->winner_mode_count; mode_idx++)
625 if (winner_mode_stats[mode_idx].rd > this_rd) break;
626
627 if (mode_idx == max_winner_mode_count) {
628 // No mode has higher rd cost than this_rd
629 return;
630 } else if (mode_idx < max_winner_mode_count - 1) {
631 // Create a slot for current mode and move others to the next slot
632 memmove(
633 &winner_mode_stats[mode_idx + 1], &winner_mode_stats[mode_idx],
634 (max_winner_mode_count - mode_idx - 1) * sizeof(*winner_mode_stats));
635 }
636 }
637 // Add a mode stat for winner mode processing
638 winner_mode_stats[mode_idx].mbmi = *mbmi;
639 winner_mode_stats[mode_idx].rd = this_rd;
640 winner_mode_stats[mode_idx].mode_index = mode_index;
641
642 // Update rd stats required for inter frame
643 if (!frame_is_intra_only(cm) && rd_cost && rd_cost_y && rd_cost_uv) {
644 const MACROBLOCKD *xd = &x->e_mbd;
645 const int skip_ctx = av1_get_skip_txfm_context(xd);
646 const int is_intra_mode = av1_mode_defs[mode_index].mode < INTRA_MODE_END;
647 const int skip_txfm = mbmi->skip_txfm && !is_intra_mode;
648
649 winner_mode_stats[mode_idx].rd_cost = *rd_cost;
650 if (txfm_search_done) {
651 winner_mode_stats[mode_idx].rate_y =
652 rd_cost_y->rate +
653 x->mode_costs
654 .skip_txfm_cost[skip_ctx][rd_cost->skip_txfm || skip_txfm];
655 winner_mode_stats[mode_idx].rate_uv = rd_cost_uv->rate;
656 }
657 }
658
659 if (color_map) {
660 // Store color_index_map for palette mode
661 const MACROBLOCKD *const xd = &x->e_mbd;
662 int block_width, block_height;
663 av1_get_block_dimensions(bsize, AOM_PLANE_Y, xd, &block_width,
664 &block_height, NULL, NULL);
665 memcpy(winner_mode_stats[mode_idx].color_index_map, color_map,
666 block_width * block_height * sizeof(color_map[0]));
667 }
668
669 x->winner_mode_count =
670 AOMMIN(x->winner_mode_count + 1, max_winner_mode_count);
671 }
672
673 unsigned int av1_get_sby_perpixel_variance(const struct AV1_COMP *cpi,
674 const struct buf_2d *ref,
675 BLOCK_SIZE bs);
676
677 unsigned int av1_high_get_sby_perpixel_variance(const struct AV1_COMP *cpi,
678 const struct buf_2d *ref,
679 BLOCK_SIZE bs, int bd);
680
is_mode_intra(PREDICTION_MODE mode)681 static INLINE int is_mode_intra(PREDICTION_MODE mode) {
682 return mode < INTRA_MODE_END;
683 }
684
685 #ifdef __cplusplus
686 } // extern "C"
687 #endif
688
689 #endif // AOM_AV1_ENCODER_RDOPT_UTILS_H_
690