• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
233 // Number of winner modes allowed for different values of the speed feature
234 // multi_winner_mode_type.
235 static const int winner_mode_count_allowed[MULTI_WINNER_MODE_LEVELS] = {
236   1,  // MULTI_WINNER_MODE_OFF
237   2,  // MULTI_WINNER_MODE_FAST
238   3   // MULTI_WINNER_MODE_DEFAULT
239 };
240 
restore_dst_buf(MACROBLOCKD * xd,const BUFFER_SET dst,const int num_planes)241 static AOM_INLINE void restore_dst_buf(MACROBLOCKD *xd, const BUFFER_SET dst,
242                                        const int num_planes) {
243   for (int i = 0; i < num_planes; i++) {
244     xd->plane[i].dst.buf = dst.plane[i];
245     xd->plane[i].dst.stride = dst.stride[i];
246   }
247 }
248 
swap_dst_buf(MACROBLOCKD * xd,const BUFFER_SET * dst_bufs[2],int num_planes)249 static AOM_INLINE void swap_dst_buf(MACROBLOCKD *xd,
250                                     const BUFFER_SET *dst_bufs[2],
251                                     int num_planes) {
252   const BUFFER_SET *buf0 = dst_bufs[0];
253   dst_bufs[0] = dst_bufs[1];
254   dst_bufs[1] = buf0;
255   restore_dst_buf(xd, *dst_bufs[0], num_planes);
256 }
257 
258 /* clang-format on */
259 // 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)260 static AOM_INLINE int64_t get_rd_thresh_from_best_rd(int64_t ref_best_rd,
261                                                      int mul_factor,
262                                                      int div_factor) {
263   int64_t rd_thresh = ref_best_rd;
264   if (div_factor != 0) {
265     rd_thresh = ref_best_rd < (div_factor * (INT64_MAX / mul_factor))
266                     ? ((ref_best_rd / div_factor) * mul_factor)
267                     : INT64_MAX;
268   }
269   return rd_thresh;
270 }
271 
272 static AOM_INLINE THR_MODES
get_prediction_mode_idx(PREDICTION_MODE this_mode,MV_REFERENCE_FRAME ref_frame,MV_REFERENCE_FRAME second_ref_frame)273 get_prediction_mode_idx(PREDICTION_MODE this_mode, MV_REFERENCE_FRAME ref_frame,
274                         MV_REFERENCE_FRAME second_ref_frame) {
275   if (this_mode < INTRA_MODE_END) {
276     assert(ref_frame == INTRA_FRAME);
277     assert(second_ref_frame == NONE_FRAME);
278     return intra_to_mode_idx[this_mode - INTRA_MODE_START];
279   }
280   if (this_mode >= SINGLE_INTER_MODE_START &&
281       this_mode < SINGLE_INTER_MODE_END) {
282     assert((ref_frame > INTRA_FRAME) && (ref_frame <= ALTREF_FRAME));
283     return single_inter_to_mode_idx[this_mode - SINGLE_INTER_MODE_START]
284                                    [ref_frame];
285   }
286   if (this_mode >= COMP_INTER_MODE_START && this_mode < COMP_INTER_MODE_END &&
287       second_ref_frame != NONE_FRAME) {
288     assert((ref_frame > INTRA_FRAME) && (ref_frame <= ALTREF_FRAME));
289     assert((second_ref_frame > INTRA_FRAME) &&
290            (second_ref_frame <= ALTREF_FRAME));
291     return comp_inter_to_mode_idx[this_mode - COMP_INTER_MODE_START][ref_frame]
292                                  [second_ref_frame];
293   }
294   assert(0);
295   return THR_INVALID;
296 }
297 
inter_mode_data_block_idx(BLOCK_SIZE bsize)298 static AOM_INLINE int inter_mode_data_block_idx(BLOCK_SIZE bsize) {
299   if (bsize == BLOCK_4X4 || bsize == BLOCK_4X8 || bsize == BLOCK_8X4 ||
300       bsize == BLOCK_4X16 || bsize == BLOCK_16X4) {
301     return -1;
302   }
303   return 1;
304 }
305 
306 // 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)307 static AOM_INLINE void get_txb_dimensions(const MACROBLOCKD *xd, int plane,
308                                           BLOCK_SIZE plane_bsize, int blk_row,
309                                           int blk_col, BLOCK_SIZE tx_bsize,
310                                           int *width, int *height,
311                                           int *visible_width,
312                                           int *visible_height) {
313   assert(tx_bsize <= plane_bsize);
314   const int txb_height = block_size_high[tx_bsize];
315   const int txb_width = block_size_wide[tx_bsize];
316   const struct macroblockd_plane *const pd = &xd->plane[plane];
317 
318   // TODO(aconverse@google.com): Investigate using crop_width/height here rather
319   // than the MI size
320   if (xd->mb_to_bottom_edge >= 0) {
321     *visible_height = txb_height;
322   } else {
323     const int block_height = block_size_high[plane_bsize];
324     const int block_rows =
325         (xd->mb_to_bottom_edge >> (3 + pd->subsampling_y)) + block_height;
326     *visible_height =
327         clamp(block_rows - (blk_row << MI_SIZE_LOG2), 0, txb_height);
328   }
329   if (height) *height = txb_height;
330 
331   if (xd->mb_to_right_edge >= 0) {
332     *visible_width = txb_width;
333   } else {
334     const int block_width = block_size_wide[plane_bsize];
335     const int block_cols =
336         (xd->mb_to_right_edge >> (3 + pd->subsampling_x)) + block_width;
337     *visible_width =
338         clamp(block_cols - (blk_col << MI_SIZE_LOG2), 0, txb_width);
339   }
340   if (width) *width = txb_width;
341 }
342 
bsize_to_num_blk(BLOCK_SIZE bsize)343 static AOM_INLINE int bsize_to_num_blk(BLOCK_SIZE bsize) {
344   int num_blk = 1 << (num_pels_log2_lookup[bsize] - 2 * MI_SIZE_LOG2);
345   return num_blk;
346 }
347 
check_txfm_eval(MACROBLOCK * const x,BLOCK_SIZE bsize,int64_t best_skip_rd,int64_t skip_rd,int level,int is_luma_only)348 static INLINE int check_txfm_eval(MACROBLOCK *const x, BLOCK_SIZE bsize,
349                                   int64_t best_skip_rd, int64_t skip_rd,
350                                   int level, int is_luma_only) {
351   int eval_txfm = 1;
352   // Derive aggressiveness factor for gating the transform search
353   // Lower value indicates more aggressiveness. Be more conservative (high
354   // value) for (i) low quantizers (ii) regions where prediction is poor
355   const int scale[5] = { INT_MAX, 4, 3, 2, 2 };
356   const int qslope = 2 * (!is_luma_only);
357   const int level_to_qindex_map[5] = { 0, 0, 0, 80, 100 };
358   int aggr_factor = 4;
359   const int pred_qindex_thresh = level_to_qindex_map[level];
360   if (!is_luma_only && level <= 2) {
361     aggr_factor = 4 * AOMMAX(1, ROUND_POWER_OF_TWO((MAXQ - x->qindex) * qslope,
362                                                    QINDEX_BITS));
363   }
364   if ((best_skip_rd >
365        (x->source_variance << (num_pels_log2_lookup[bsize] + RDDIV_BITS))) &&
366       (x->qindex >= pred_qindex_thresh))
367     aggr_factor *= scale[level];
368   // For level setting 1, be more conservative for non-luma-only case even when
369   // prediction is good.
370   else if ((level <= 1) && !is_luma_only)
371     aggr_factor = (aggr_factor >> 2) * 6;
372 
373   // Be more conservative for luma only cases (called from compound type rd)
374   // since best_skip_rd is computed after and skip_rd is computed (with 8-bit
375   // prediction signals blended for WEDGE/DIFFWTD rather than 16-bit) before
376   // interpolation filter search
377   const int luma_mul[5] = { INT_MAX, 32, 29, 17, 17 };
378   int mul_factor = is_luma_only ? luma_mul[level] : 16;
379   int64_t rd_thresh =
380       (best_skip_rd == INT64_MAX)
381           ? best_skip_rd
382           : (int64_t)(best_skip_rd * aggr_factor * mul_factor >> 6);
383   if (skip_rd > rd_thresh) eval_txfm = 0;
384   return eval_txfm;
385 }
386 
select_tx_mode(const AV1_COMMON * cm,const TX_SIZE_SEARCH_METHOD tx_size_search_method)387 static TX_MODE select_tx_mode(
388     const AV1_COMMON *cm, const TX_SIZE_SEARCH_METHOD tx_size_search_method) {
389   if (cm->features.coded_lossless) return ONLY_4X4;
390   if (tx_size_search_method == USE_LARGESTALL) {
391     return TX_MODE_LARGEST;
392   } else {
393     assert(tx_size_search_method == USE_FULL_RD ||
394            tx_size_search_method == USE_FAST_RD);
395     return TX_MODE_SELECT;
396   }
397 }
398 
399 // Checks the conditions to disable winner mode processing
bypass_winner_mode_processing(const MACROBLOCK * const x,const SPEED_FEATURES * sf,int use_txfm_skip,int actual_txfm_skip,PREDICTION_MODE best_mode)400 static INLINE int bypass_winner_mode_processing(const MACROBLOCK *const x,
401                                                 const SPEED_FEATURES *sf,
402                                                 int use_txfm_skip,
403                                                 int actual_txfm_skip,
404                                                 PREDICTION_MODE best_mode) {
405   const int prune_winner_mode_eval_level =
406       sf->winner_mode_sf.prune_winner_mode_eval_level;
407 
408   // Disable winner mode processing for blocks with low source variance.
409   // The aggressiveness of this pruning logic reduces as qindex increases.
410   // The threshold decreases linearly from 64 as qindex varies from 0 to 255.
411   if (prune_winner_mode_eval_level == 1) {
412     const unsigned int src_var_thresh = 64 - 48 * x->qindex / (MAXQ + 1);
413     if (x->source_variance < src_var_thresh) return 1;
414   } else if (prune_winner_mode_eval_level == 2) {
415     // Skip winner mode processing of blocks for which transform turns out to be
416     // skip due to nature of eob alone except NEWMV mode.
417     if (!have_newmv_in_inter_mode(best_mode) && actual_txfm_skip) return 1;
418   } else if (prune_winner_mode_eval_level == 3) {
419     // Skip winner mode processing of blocks for which transform turns out to be
420     // skip except NEWMV mode and considered based on the quantizer.
421     // At high quantizers: Take conservative approach by considering transform
422     // skip based on eob alone.
423     // At low quantizers: Consider transform skip based on eob nature or RD cost
424     // evaluation.
425     const int is_txfm_skip =
426         x->qindex > 127 ? actual_txfm_skip : actual_txfm_skip || use_txfm_skip;
427 
428     if (!have_newmv_in_inter_mode(best_mode) && is_txfm_skip) return 1;
429   } else if (prune_winner_mode_eval_level >= 4) {
430     // Do not skip winner mode evaluation at low quantizers if normal mode's
431     // transform search was too aggressive.
432     if (sf->rd_sf.perform_coeff_opt >= 5 && x->qindex <= 70) return 0;
433 
434     if (use_txfm_skip || actual_txfm_skip) return 1;
435   }
436 
437   return 0;
438 }
439 
440 // Checks the conditions to enable winner mode processing
is_winner_mode_processing_enabled(const struct AV1_COMP * cpi,const MACROBLOCK * const x,MB_MODE_INFO * const mbmi,int actual_txfm_skip)441 static INLINE int is_winner_mode_processing_enabled(const struct AV1_COMP *cpi,
442                                                     const MACROBLOCK *const x,
443                                                     MB_MODE_INFO *const mbmi,
444                                                     int actual_txfm_skip) {
445   const SPEED_FEATURES *sf = &cpi->sf;
446   const PREDICTION_MODE best_mode = mbmi->mode;
447 
448   if (bypass_winner_mode_processing(x, sf, mbmi->skip_txfm, actual_txfm_skip,
449                                     best_mode))
450     return 0;
451 
452   // TODO(any): Move block independent condition checks to frame level
453   if (is_inter_block(mbmi)) {
454     if (is_inter_mode(best_mode) &&
455         (sf->tx_sf.tx_type_search.fast_inter_tx_type_prob_thresh != INT_MAX) &&
456         !cpi->oxcf.txfm_cfg.use_inter_dct_only)
457       return 1;
458   } else {
459     if (sf->tx_sf.tx_type_search.fast_intra_tx_type_search &&
460         !cpi->oxcf.txfm_cfg.use_intra_default_tx_only &&
461         !cpi->oxcf.txfm_cfg.use_intra_dct_only)
462       return 1;
463   }
464 
465   // Check speed feature related to winner mode processing
466   if (sf->winner_mode_sf.enable_winner_mode_for_coeff_opt &&
467       cpi->optimize_seg_arr[mbmi->segment_id] != NO_TRELLIS_OPT &&
468       cpi->optimize_seg_arr[mbmi->segment_id] != FINAL_PASS_TRELLIS_OPT)
469     return 1;
470   if (sf->winner_mode_sf.enable_winner_mode_for_tx_size_srch) return 1;
471 
472   return 0;
473 }
474 
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)475 static INLINE void set_tx_size_search_method(
476     const AV1_COMMON *cm, const WinnerModeParams *winner_mode_params,
477     TxfmSearchParams *txfm_params, int enable_winner_mode_for_tx_size_srch,
478     int is_winner_mode) {
479   // Populate transform size search method/transform mode appropriately
480   txfm_params->tx_size_search_method =
481       winner_mode_params->tx_size_search_methods[DEFAULT_EVAL];
482   if (enable_winner_mode_for_tx_size_srch) {
483     if (is_winner_mode)
484       txfm_params->tx_size_search_method =
485           winner_mode_params->tx_size_search_methods[WINNER_MODE_EVAL];
486     else
487       txfm_params->tx_size_search_method =
488           winner_mode_params->tx_size_search_methods[MODE_EVAL];
489   }
490   txfm_params->tx_mode_search_type =
491       select_tx_mode(cm, txfm_params->tx_size_search_method);
492 }
493 
set_tx_type_prune(const SPEED_FEATURES * sf,TxfmSearchParams * txfm_params,int winner_mode_tx_type_pruning,int is_winner_mode)494 static INLINE void set_tx_type_prune(const SPEED_FEATURES *sf,
495                                      TxfmSearchParams *txfm_params,
496                                      int winner_mode_tx_type_pruning,
497                                      int is_winner_mode) {
498   // Populate prune transform mode appropriately
499   txfm_params->prune_2d_txfm_mode = sf->tx_sf.tx_type_search.prune_2d_txfm_mode;
500   if (!winner_mode_tx_type_pruning) return;
501 
502   const int prune_mode[4][2] = { { TX_TYPE_PRUNE_3, TX_TYPE_PRUNE_0 },
503                                  { TX_TYPE_PRUNE_4, TX_TYPE_PRUNE_0 },
504                                  { TX_TYPE_PRUNE_5, TX_TYPE_PRUNE_2 },
505                                  { TX_TYPE_PRUNE_5, TX_TYPE_PRUNE_3 } };
506   txfm_params->prune_2d_txfm_mode =
507       prune_mode[winner_mode_tx_type_pruning - 1][is_winner_mode];
508 }
509 
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)510 static INLINE void set_tx_domain_dist_params(
511     const WinnerModeParams *winner_mode_params, TxfmSearchParams *txfm_params,
512     int enable_winner_mode_for_tx_domain_dist, int is_winner_mode) {
513   if (txfm_params->use_qm_dist_metric) {
514     // QM-weighted PSNR is computed in transform space, so we need to forcibly
515     // enable the use of tx domain distortion.
516     txfm_params->use_transform_domain_distortion = 1;
517     txfm_params->tx_domain_dist_threshold = 0;
518     return;
519   }
520 
521   if (!enable_winner_mode_for_tx_domain_dist) {
522     txfm_params->use_transform_domain_distortion =
523         winner_mode_params->use_transform_domain_distortion[DEFAULT_EVAL];
524     txfm_params->tx_domain_dist_threshold =
525         winner_mode_params->tx_domain_dist_threshold[DEFAULT_EVAL];
526     return;
527   }
528 
529   if (is_winner_mode) {
530     txfm_params->use_transform_domain_distortion =
531         winner_mode_params->use_transform_domain_distortion[WINNER_MODE_EVAL];
532     txfm_params->tx_domain_dist_threshold =
533         winner_mode_params->tx_domain_dist_threshold[WINNER_MODE_EVAL];
534   } else {
535     txfm_params->use_transform_domain_distortion =
536         winner_mode_params->use_transform_domain_distortion[MODE_EVAL];
537     txfm_params->tx_domain_dist_threshold =
538         winner_mode_params->tx_domain_dist_threshold[MODE_EVAL];
539   }
540 }
541 
542 // 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)543 static INLINE void set_mode_eval_params(const struct AV1_COMP *cpi,
544                                         MACROBLOCK *x,
545                                         MODE_EVAL_TYPE mode_eval_type) {
546   const AV1_COMMON *cm = &cpi->common;
547   const SPEED_FEATURES *sf = &cpi->sf;
548   const WinnerModeParams *winner_mode_params = &cpi->winner_mode_params;
549   TxfmSearchParams *txfm_params = &x->txfm_search_params;
550 
551   txfm_params->use_qm_dist_metric =
552       cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR;
553 
554   switch (mode_eval_type) {
555     case DEFAULT_EVAL:
556       txfm_params->default_inter_tx_type_prob_thresh = INT_MAX;
557       txfm_params->use_default_intra_tx_type = 0;
558       txfm_params->skip_txfm_level =
559           winner_mode_params->skip_txfm_level[DEFAULT_EVAL];
560       txfm_params->predict_dc_level =
561           winner_mode_params->predict_dc_level[DEFAULT_EVAL];
562       // Set default transform domain distortion type
563       set_tx_domain_dist_params(winner_mode_params, txfm_params, 0, 0);
564 
565       // Get default threshold for R-D optimization of coefficients
566       get_rd_opt_coeff_thresh(winner_mode_params->coeff_opt_thresholds,
567                               txfm_params, 0, 0);
568 
569       // Set default transform size search method
570       set_tx_size_search_method(cm, winner_mode_params, txfm_params, 0, 0);
571       // Set default transform type prune
572       set_tx_type_prune(sf, txfm_params, 0, 0);
573       break;
574     case MODE_EVAL:
575       txfm_params->use_default_intra_tx_type =
576           (cpi->sf.tx_sf.tx_type_search.fast_intra_tx_type_search ||
577            cpi->oxcf.txfm_cfg.use_intra_default_tx_only);
578       txfm_params->default_inter_tx_type_prob_thresh =
579           cpi->sf.tx_sf.tx_type_search.fast_inter_tx_type_prob_thresh;
580       txfm_params->skip_txfm_level =
581           winner_mode_params->skip_txfm_level[MODE_EVAL];
582       txfm_params->predict_dc_level =
583           winner_mode_params->predict_dc_level[MODE_EVAL];
584       // Set transform domain distortion type for mode evaluation
585       set_tx_domain_dist_params(
586           winner_mode_params, txfm_params,
587           sf->winner_mode_sf.enable_winner_mode_for_use_tx_domain_dist, 0);
588 
589       // Get threshold for R-D optimization of coefficients during mode
590       // evaluation
591       get_rd_opt_coeff_thresh(
592           winner_mode_params->coeff_opt_thresholds, txfm_params,
593           sf->winner_mode_sf.enable_winner_mode_for_coeff_opt, 0);
594 
595       // Set the transform size search method for mode evaluation
596       set_tx_size_search_method(
597           cm, winner_mode_params, txfm_params,
598           sf->winner_mode_sf.enable_winner_mode_for_tx_size_srch, 0);
599       // Set transform type prune for mode evaluation
600       set_tx_type_prune(sf, txfm_params,
601                         sf->tx_sf.tx_type_search.winner_mode_tx_type_pruning,
602                         0);
603       break;
604     case WINNER_MODE_EVAL:
605       txfm_params->default_inter_tx_type_prob_thresh = INT_MAX;
606       txfm_params->use_default_intra_tx_type = 0;
607       txfm_params->skip_txfm_level =
608           winner_mode_params->skip_txfm_level[WINNER_MODE_EVAL];
609       txfm_params->predict_dc_level =
610           winner_mode_params->predict_dc_level[WINNER_MODE_EVAL];
611 
612       // Set transform domain distortion type for winner mode evaluation
613       set_tx_domain_dist_params(
614           winner_mode_params, txfm_params,
615           sf->winner_mode_sf.enable_winner_mode_for_use_tx_domain_dist, 1);
616 
617       // Get threshold for R-D optimization of coefficients for winner mode
618       // evaluation
619       get_rd_opt_coeff_thresh(
620           winner_mode_params->coeff_opt_thresholds, txfm_params,
621           sf->winner_mode_sf.enable_winner_mode_for_coeff_opt, 1);
622 
623       // Set the transform size search method for winner mode evaluation
624       set_tx_size_search_method(
625           cm, winner_mode_params, txfm_params,
626           sf->winner_mode_sf.enable_winner_mode_for_tx_size_srch, 1);
627       // Set default transform type prune mode for winner mode evaluation
628       set_tx_type_prune(sf, txfm_params,
629                         sf->tx_sf.tx_type_search.winner_mode_tx_type_pruning,
630                         1);
631       break;
632     default: assert(0);
633   }
634 
635   // Rd record collected at a specific mode evaluation stage can not be used
636   // across other evaluation stages as the transform parameters are different.
637   // Hence, reset mb rd record whenever mode evaluation stage type changes.
638   if (txfm_params->mode_eval_type != mode_eval_type)
639     reset_mb_rd_record(x->txfm_search_info.mb_rd_record);
640 
641   txfm_params->mode_eval_type = mode_eval_type;
642 }
643 
644 // Similar to store_cfl_required(), but for use during the RDO process,
645 // where we haven't yet determined whether this block uses CfL.
store_cfl_required_rdo(const AV1_COMMON * cm,const MACROBLOCK * x)646 static INLINE CFL_ALLOWED_TYPE store_cfl_required_rdo(const AV1_COMMON *cm,
647                                                       const MACROBLOCK *x) {
648   const MACROBLOCKD *xd = &x->e_mbd;
649 
650   if (cm->seq_params->monochrome || !xd->is_chroma_ref) return CFL_DISALLOWED;
651 
652   if (!xd->is_chroma_ref) {
653     // For non-chroma-reference blocks, we should always store the luma pixels,
654     // in case the corresponding chroma-reference block uses CfL.
655     // Note that this can only happen for block sizes which are <8 on
656     // their shortest side, as otherwise they would be chroma reference
657     // blocks.
658     return CFL_ALLOWED;
659   }
660 
661   // For chroma reference blocks, we should store data in the encoder iff we're
662   // allowed to try out CfL.
663   return is_cfl_allowed(xd);
664 }
665 
init_sbuv_mode(MB_MODE_INFO * const mbmi)666 static AOM_INLINE void init_sbuv_mode(MB_MODE_INFO *const mbmi) {
667   mbmi->uv_mode = UV_DC_PRED;
668   mbmi->palette_mode_info.palette_size[1] = 0;
669 }
670 
671 // 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)672 static INLINE void store_winner_mode_stats(
673     const AV1_COMMON *const cm, MACROBLOCK *x, const MB_MODE_INFO *mbmi,
674     RD_STATS *rd_cost, RD_STATS *rd_cost_y, RD_STATS *rd_cost_uv,
675     THR_MODES mode_index, uint8_t *color_map, BLOCK_SIZE bsize, int64_t this_rd,
676     int multi_winner_mode_type, int txfm_search_done) {
677   WinnerModeStats *winner_mode_stats = x->winner_mode_stats;
678   int mode_idx = 0;
679   int is_palette_mode = mbmi->palette_mode_info.palette_size[PLANE_TYPE_Y] > 0;
680   // Mode stat is not required when multiwinner mode processing is disabled
681   if (multi_winner_mode_type == MULTI_WINNER_MODE_OFF) return;
682   // Ignore mode with maximum rd
683   if (this_rd == INT64_MAX) return;
684   // TODO(any): Winner mode processing is currently not applicable for palette
685   // mode in Inter frames. Clean-up the following code, once support is added
686   if (!frame_is_intra_only(cm) && is_palette_mode) return;
687 
688   int max_winner_mode_count = winner_mode_count_allowed[multi_winner_mode_type];
689   assert(x->winner_mode_count >= 0 &&
690          x->winner_mode_count <= max_winner_mode_count);
691 
692   if (x->winner_mode_count) {
693     // Find the mode which has higher rd cost than this_rd
694     for (mode_idx = 0; mode_idx < x->winner_mode_count; mode_idx++)
695       if (winner_mode_stats[mode_idx].rd > this_rd) break;
696 
697     if (mode_idx == max_winner_mode_count) {
698       // No mode has higher rd cost than this_rd
699       return;
700     } else if (mode_idx < max_winner_mode_count - 1) {
701       // Create a slot for current mode and move others to the next slot
702       memmove(
703           &winner_mode_stats[mode_idx + 1], &winner_mode_stats[mode_idx],
704           (max_winner_mode_count - mode_idx - 1) * sizeof(*winner_mode_stats));
705     }
706   }
707   // Add a mode stat for winner mode processing
708   winner_mode_stats[mode_idx].mbmi = *mbmi;
709   winner_mode_stats[mode_idx].rd = this_rd;
710   winner_mode_stats[mode_idx].mode_index = mode_index;
711 
712   // Update rd stats required for inter frame
713   if (!frame_is_intra_only(cm) && rd_cost && rd_cost_y && rd_cost_uv) {
714     const MACROBLOCKD *xd = &x->e_mbd;
715     const int skip_ctx = av1_get_skip_txfm_context(xd);
716     const int is_intra_mode = av1_mode_defs[mode_index].mode < INTRA_MODE_END;
717     const int skip_txfm = mbmi->skip_txfm && !is_intra_mode;
718 
719     winner_mode_stats[mode_idx].rd_cost = *rd_cost;
720     if (txfm_search_done) {
721       winner_mode_stats[mode_idx].rate_y =
722           rd_cost_y->rate +
723           x->mode_costs
724               .skip_txfm_cost[skip_ctx][rd_cost->skip_txfm || skip_txfm];
725       winner_mode_stats[mode_idx].rate_uv = rd_cost_uv->rate;
726     }
727   }
728 
729   if (color_map) {
730     // Store color_index_map for palette mode
731     const MACROBLOCKD *const xd = &x->e_mbd;
732     int block_width, block_height;
733     av1_get_block_dimensions(bsize, AOM_PLANE_Y, xd, &block_width,
734                              &block_height, NULL, NULL);
735     memcpy(winner_mode_stats[mode_idx].color_index_map, color_map,
736            block_width * block_height * sizeof(color_map[0]));
737   }
738 
739   x->winner_mode_count =
740       AOMMIN(x->winner_mode_count + 1, max_winner_mode_count);
741 }
742 
743 unsigned int av1_get_perpixel_variance(const AV1_COMP *cpi,
744                                        const MACROBLOCKD *xd,
745                                        const struct buf_2d *ref,
746                                        BLOCK_SIZE bsize, int plane,
747                                        int use_hbd);
748 
749 unsigned int av1_get_perpixel_variance_facade(const struct AV1_COMP *cpi,
750                                               const MACROBLOCKD *xd,
751                                               const struct buf_2d *ref,
752                                               BLOCK_SIZE bsize, int plane);
753 
is_mode_intra(PREDICTION_MODE mode)754 static INLINE int is_mode_intra(PREDICTION_MODE mode) {
755   return mode < INTRA_MODE_END;
756 }
757 
758 // This function will copy usable ref_mv_stack[ref_frame][4] and
759 // weight[ref_frame][4] information from ref_mv_stack[ref_frame][8] and
760 // weight[ref_frame][8].
av1_copy_usable_ref_mv_stack_and_weight(const MACROBLOCKD * xd,MB_MODE_INFO_EXT * const mbmi_ext,MV_REFERENCE_FRAME ref_frame)761 static INLINE void av1_copy_usable_ref_mv_stack_and_weight(
762     const MACROBLOCKD *xd, MB_MODE_INFO_EXT *const mbmi_ext,
763     MV_REFERENCE_FRAME ref_frame) {
764   memcpy(mbmi_ext->weight[ref_frame], xd->weight[ref_frame],
765          USABLE_REF_MV_STACK_SIZE * sizeof(xd->weight[0][0]));
766   memcpy(mbmi_ext->ref_mv_stack[ref_frame], xd->ref_mv_stack[ref_frame],
767          USABLE_REF_MV_STACK_SIZE * sizeof(xd->ref_mv_stack[0][0]));
768 }
769 
770 #ifdef __cplusplus
771 }  // extern "C"
772 #endif
773 
774 #endif  // AOM_AV1_ENCODER_RDOPT_UTILS_H_
775