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
13 #include <assert.h>
14 #include <limits.h>
15 #include <math.h>
16 #include <stdio.h>
17
18 #include "av1/common/reconinter.h"
19 #include "av1/common/reconintra.h"
20
21 #include "av1/encoder/encodemv.h"
22 #include "av1/encoder/intra_mode_search.h"
23 #include "av1/encoder/model_rd.h"
24 #include "av1/encoder/motion_search_facade.h"
25 #include "av1/encoder/nonrd_opt.h"
26 #include "av1/encoder/palette.h"
27 #include "av1/encoder/reconinter_enc.h"
28 #include "av1/encoder/var_based_part.h"
29
early_term_inter_search_with_sse(int early_term_idx,BLOCK_SIZE bsize,int64_t this_sse,int64_t best_sse,PREDICTION_MODE this_mode)30 static inline int early_term_inter_search_with_sse(int early_term_idx,
31 BLOCK_SIZE bsize,
32 int64_t this_sse,
33 int64_t best_sse,
34 PREDICTION_MODE this_mode) {
35 // Aggressiveness to terminate inter mode search early is adjusted based on
36 // speed and block size.
37 static const double early_term_thresh[4][4] = { { 0.65, 0.65, 0.65, 0.7 },
38 { 0.6, 0.65, 0.85, 0.9 },
39 { 0.5, 0.5, 0.55, 0.6 },
40 { 0.6, 0.75, 0.85, 0.85 } };
41 static const double early_term_thresh_newmv_nearestmv[4] = { 0.3, 0.3, 0.3,
42 0.3 };
43
44 const int size_group = size_group_lookup[bsize];
45 assert(size_group < 4);
46 assert((early_term_idx > 0) && (early_term_idx < EARLY_TERM_INDICES));
47 const double threshold =
48 ((early_term_idx == EARLY_TERM_IDX_4) &&
49 (this_mode == NEWMV || this_mode == NEARESTMV))
50 ? early_term_thresh_newmv_nearestmv[size_group]
51 : early_term_thresh[early_term_idx - 1][size_group];
52
53 // Terminate inter mode search early based on best sse so far.
54 if ((early_term_idx > 0) && (threshold * this_sse > best_sse)) {
55 return 1;
56 }
57 return 0;
58 }
59
init_best_pickmode(BEST_PICKMODE * bp)60 static inline void init_best_pickmode(BEST_PICKMODE *bp) {
61 bp->best_sse = INT64_MAX;
62 bp->best_mode = NEARESTMV;
63 bp->best_ref_frame = LAST_FRAME;
64 bp->best_second_ref_frame = NONE_FRAME;
65 bp->best_tx_size = TX_8X8;
66 bp->tx_type = DCT_DCT;
67 bp->best_pred_filter = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
68 bp->best_mode_skip_txfm = 0;
69 bp->best_mode_initial_skip_flag = 0;
70 bp->best_pred = NULL;
71 bp->best_motion_mode = SIMPLE_TRANSLATION;
72 bp->num_proj_ref = 0;
73 av1_zero(bp->wm_params);
74 av1_zero(bp->pmi);
75 }
76
77 // Copy best inter mode parameters to best_pickmode
update_search_state_nonrd(InterModeSearchStateNonrd * search_state,MB_MODE_INFO * const mi,TxfmSearchInfo * txfm_info,RD_STATS * nonskip_rdc,PICK_MODE_CONTEXT * ctx,PREDICTION_MODE this_best_mode,const int64_t sse_y)78 static inline void update_search_state_nonrd(
79 InterModeSearchStateNonrd *search_state, MB_MODE_INFO *const mi,
80 TxfmSearchInfo *txfm_info, RD_STATS *nonskip_rdc, PICK_MODE_CONTEXT *ctx,
81 PREDICTION_MODE this_best_mode, const int64_t sse_y) {
82 BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode;
83
84 best_pickmode->best_sse = sse_y;
85 best_pickmode->best_mode = this_best_mode;
86 best_pickmode->best_motion_mode = mi->motion_mode;
87 best_pickmode->wm_params = mi->wm_params;
88 best_pickmode->num_proj_ref = mi->num_proj_ref;
89 best_pickmode->best_pred_filter = mi->interp_filters;
90 best_pickmode->best_tx_size = mi->tx_size;
91 best_pickmode->best_ref_frame = mi->ref_frame[0];
92 best_pickmode->best_second_ref_frame = mi->ref_frame[1];
93 best_pickmode->best_mode_skip_txfm = search_state->this_rdc.skip_txfm;
94 best_pickmode->best_mode_initial_skip_flag =
95 (nonskip_rdc->rate == INT_MAX && search_state->this_rdc.skip_txfm);
96 if (!best_pickmode->best_mode_skip_txfm) {
97 memcpy(ctx->blk_skip, txfm_info->blk_skip,
98 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
99 }
100 }
101
subpel_select(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int_mv * mv,MV ref_mv,FULLPEL_MV start_mv,bool fullpel_performed_well)102 static inline int subpel_select(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
103 int_mv *mv, MV ref_mv, FULLPEL_MV start_mv,
104 bool fullpel_performed_well) {
105 const int frame_lowmotion = cpi->rc.avg_frame_low_motion;
106 const int reduce_mv_pel_precision_highmotion =
107 cpi->sf.rt_sf.reduce_mv_pel_precision_highmotion;
108
109 // Reduce MV precision for higher int MV value & frame-level motion
110 if (reduce_mv_pel_precision_highmotion >= 3) {
111 int mv_thresh = 4;
112 const int is_low_resoln =
113 (cpi->common.width * cpi->common.height <= 320 * 240);
114 mv_thresh = (bsize > BLOCK_32X32) ? 2 : (bsize > BLOCK_16X16) ? 4 : 6;
115 if (frame_lowmotion > 0 && frame_lowmotion < 40) mv_thresh = 12;
116 mv_thresh = (is_low_resoln) ? mv_thresh >> 1 : mv_thresh;
117 if (abs(mv->as_fullmv.row) >= mv_thresh ||
118 abs(mv->as_fullmv.col) >= mv_thresh)
119 return HALF_PEL;
120 } else if (reduce_mv_pel_precision_highmotion >= 1) {
121 int mv_thresh;
122 const int th_vals[2][3] = { { 4, 8, 10 }, { 4, 6, 8 } };
123 const int th_idx = reduce_mv_pel_precision_highmotion - 1;
124 assert(th_idx >= 0 && th_idx < 2);
125 if (frame_lowmotion > 0 && frame_lowmotion < 40)
126 mv_thresh = 12;
127 else
128 mv_thresh = (bsize >= BLOCK_32X32) ? th_vals[th_idx][0]
129 : (bsize >= BLOCK_16X16) ? th_vals[th_idx][1]
130 : th_vals[th_idx][2];
131 if (abs(mv->as_fullmv.row) >= (mv_thresh << 1) ||
132 abs(mv->as_fullmv.col) >= (mv_thresh << 1))
133 return FULL_PEL;
134 else if (abs(mv->as_fullmv.row) >= mv_thresh ||
135 abs(mv->as_fullmv.col) >= mv_thresh)
136 return HALF_PEL;
137 }
138 // Reduce MV precision for relatively static (e.g. background), low-complex
139 // large areas
140 if (cpi->sf.rt_sf.reduce_mv_pel_precision_lowcomplex >= 2) {
141 const int qband = x->qindex >> (QINDEX_BITS - 2);
142 assert(qband < 4);
143 if (x->content_state_sb.source_sad_nonrd <= kVeryLowSad &&
144 bsize > BLOCK_16X16 && qband != 0) {
145 if (x->source_variance < 500)
146 return FULL_PEL;
147 else if (x->source_variance < 5000)
148 return HALF_PEL;
149 }
150 } else if (cpi->sf.rt_sf.reduce_mv_pel_precision_lowcomplex >= 1) {
151 if (fullpel_performed_well && ref_mv.row == 0 && ref_mv.col == 0 &&
152 start_mv.row == 0 && start_mv.col == 0)
153 return HALF_PEL;
154 }
155 return cpi->sf.mv_sf.subpel_force_stop;
156 }
157
use_aggressive_subpel_search_method(MACROBLOCK * x,bool use_adaptive_subpel_search,bool fullpel_performed_well)158 static bool use_aggressive_subpel_search_method(MACROBLOCK *x,
159 bool use_adaptive_subpel_search,
160 bool fullpel_performed_well) {
161 if (!use_adaptive_subpel_search) return false;
162 const int qband = x->qindex >> (QINDEX_BITS - 2);
163 assert(qband < 4);
164 if ((qband > 0) && (fullpel_performed_well ||
165 (x->content_state_sb.source_sad_nonrd <= kLowSad) ||
166 (x->source_variance < 100)))
167 return true;
168 return false;
169 }
170
171 /*!\brief Runs Motion Estimation for a specific block and specific ref frame.
172 *
173 * \ingroup nonrd_mode_search
174 * \callgraph
175 * \callergraph
176 * Finds the best Motion Vector by running Motion Estimation for a specific
177 * block and a specific reference frame. Exits early if RDCost of Full Pel part
178 * exceeds best RD Cost fund so far
179 * \param[in] cpi Top-level encoder structure
180 * \param[in] x Pointer to structure holding all the
181 * data for the current macroblock
182 * \param[in] bsize Current block size
183 * \param[in] tmp_mv Pointer to best found New MV
184 * \param[in] rate_mv Pointer to Rate of the best new MV
185 * \param[in] best_rd_sofar RD Cost of the best mode found so far
186 * \param[in] use_base_mv Flag, indicating that tmp_mv holds
187 * specific MV to start the search with
188 *
189 * \return Returns 0 if ME was terminated after Full Pel Search because too
190 * high RD Cost. Otherwise returns 1. Best New MV is placed into \c tmp_mv.
191 * Rate estimation for this vector is placed to \c rate_mv
192 */
combined_motion_search(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int_mv * tmp_mv,int * rate_mv,int64_t best_rd_sofar,int use_base_mv)193 static int combined_motion_search(AV1_COMP *cpi, MACROBLOCK *x,
194 BLOCK_SIZE bsize, int_mv *tmp_mv,
195 int *rate_mv, int64_t best_rd_sofar,
196 int use_base_mv) {
197 MACROBLOCKD *xd = &x->e_mbd;
198 const AV1_COMMON *cm = &cpi->common;
199 const SPEED_FEATURES *sf = &cpi->sf;
200 MB_MODE_INFO *mi = xd->mi[0];
201 int step_param = (sf->rt_sf.fullpel_search_step_param)
202 ? sf->rt_sf.fullpel_search_step_param
203 : cpi->mv_search_params.mv_step_param;
204 FULLPEL_MV start_mv;
205 const int ref = mi->ref_frame[0];
206 const MV ref_mv = av1_get_ref_mv(x, mi->ref_mv_idx).as_mv;
207 MV center_mv;
208 int dis;
209 int rv = 0;
210 int cost_list[5];
211 int search_subpel = 1;
212
213 start_mv = get_fullmv_from_mv(&ref_mv);
214
215 if (!use_base_mv)
216 center_mv = ref_mv;
217 else
218 center_mv = tmp_mv->as_mv;
219
220 const SEARCH_METHODS search_method =
221 av1_get_default_mv_search_method(x, &cpi->sf.mv_sf, bsize);
222 const search_site_config *src_search_sites =
223 av1_get_search_site_config(cpi, x, search_method);
224 FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
225 FULLPEL_MV_STATS best_mv_stats;
226 av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, ¢er_mv,
227 start_mv, src_search_sites, search_method,
228 /*fine_search_interval=*/0);
229
230 const unsigned int full_var_rd = av1_full_pixel_search(
231 start_mv, &full_ms_params, step_param, cond_cost_list(cpi, cost_list),
232 &tmp_mv->as_fullmv, &best_mv_stats, NULL);
233
234 // calculate the bit cost on motion vector
235 MV mvp_full = get_mv_from_fullmv(&tmp_mv->as_fullmv);
236
237 *rate_mv = av1_mv_bit_cost(&mvp_full, &ref_mv, x->mv_costs->nmv_joint_cost,
238 x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
239
240 // TODO(kyslov) Account for Rate Mode!
241 rv = !(RDCOST(x->rdmult, (*rate_mv), 0) > best_rd_sofar);
242
243 if (rv && search_subpel) {
244 SUBPEL_MOTION_SEARCH_PARAMS ms_params;
245 av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
246 cost_list);
247 const bool fullpel_performed_well =
248 (bsize == BLOCK_64X64 && full_var_rd * 40 < 62267 * 7) ||
249 (bsize == BLOCK_32X32 && full_var_rd * 8 < 42380) ||
250 (bsize == BLOCK_16X16 && full_var_rd * 8 < 10127);
251 if (sf->rt_sf.reduce_mv_pel_precision_highmotion ||
252 sf->rt_sf.reduce_mv_pel_precision_lowcomplex)
253 ms_params.forced_stop = subpel_select(cpi, x, bsize, tmp_mv, ref_mv,
254 start_mv, fullpel_performed_well);
255
256 MV subpel_start_mv = get_mv_from_fullmv(&tmp_mv->as_fullmv);
257 assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, subpel_start_mv));
258 // adaptively downgrade subpel search method based on block properties
259 if (use_aggressive_subpel_search_method(
260 x, sf->rt_sf.use_adaptive_subpel_search, fullpel_performed_well))
261 av1_find_best_sub_pixel_tree_pruned_more(
262 xd, cm, &ms_params, subpel_start_mv, &best_mv_stats, &tmp_mv->as_mv,
263 &dis, &x->pred_sse[ref], NULL);
264 else
265 cpi->mv_search_params.find_fractional_mv_step(
266 xd, cm, &ms_params, subpel_start_mv, &best_mv_stats, &tmp_mv->as_mv,
267 &dis, &x->pred_sse[ref], NULL);
268 *rate_mv =
269 av1_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->mv_costs->nmv_joint_cost,
270 x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
271 }
272 // The final MV can not be equal to the reference MV as this will trigger an
273 // assert later. This can happen if both NEAREST and NEAR modes were skipped.
274 rv = (tmp_mv->as_mv.col != ref_mv.col || tmp_mv->as_mv.row != ref_mv.row);
275 return rv;
276 }
277
278 /*!\brief Searches for the best New Motion Vector.
279 *
280 * \ingroup nonrd_mode_search
281 * \callgraph
282 * \callergraph
283 * Finds the best Motion Vector by doing Motion Estimation. Uses reduced
284 * complexity ME for non-LAST frames or calls \c combined_motion_search
285 * for LAST reference frame
286 * \param[in] cpi Top-level encoder structure
287 * \param[in] x Pointer to structure holding all the
288 * data for the current macroblock
289 * \param[in] frame_mv Array that holds MVs for all modes
290 * and ref frames
291 * \param[in] ref_frame Reference frame for which to find
292 * the best New MVs
293 * \param[in] gf_temporal_ref Flag, indicating temporal reference
294 * for GOLDEN frame
295 * \param[in] bsize Current block size
296 * \param[in] mi_row Row index in 4x4 units
297 * \param[in] mi_col Column index in 4x4 units
298 * \param[in] rate_mv Pointer to Rate of the best new MV
299 * \param[in] best_rdc Pointer to the RD Cost for the best
300 * mode found so far
301 *
302 * \return Returns -1 if the search was not done, otherwise returns 0.
303 * Best New MV is placed into \c frame_mv array, Rate estimation for this
304 * vector is placed to \c rate_mv
305 */
search_new_mv(AV1_COMP * cpi,MACROBLOCK * x,int_mv frame_mv[][REF_FRAMES],MV_REFERENCE_FRAME ref_frame,int gf_temporal_ref,BLOCK_SIZE bsize,int mi_row,int mi_col,int * rate_mv,RD_STATS * best_rdc)306 static int search_new_mv(AV1_COMP *cpi, MACROBLOCK *x,
307 int_mv frame_mv[][REF_FRAMES],
308 MV_REFERENCE_FRAME ref_frame, int gf_temporal_ref,
309 BLOCK_SIZE bsize, int mi_row, int mi_col, int *rate_mv,
310 RD_STATS *best_rdc) {
311 MACROBLOCKD *const xd = &x->e_mbd;
312 MB_MODE_INFO *const mi = xd->mi[0];
313 AV1_COMMON *cm = &cpi->common;
314 int_mv *this_ref_frm_newmv = &frame_mv[NEWMV][ref_frame];
315 unsigned int y_sad_zero;
316 if (ref_frame > LAST_FRAME && cpi->oxcf.rc_cfg.mode == AOM_CBR &&
317 gf_temporal_ref) {
318 int tmp_sad;
319 int dis;
320
321 if (bsize < BLOCK_16X16) return -1;
322
323 int me_search_size_col = block_size_wide[bsize] >> 1;
324 int me_search_size_row = block_size_high[bsize] >> 1;
325 tmp_sad = av1_int_pro_motion_estimation(
326 cpi, x, bsize, mi_row, mi_col,
327 &x->mbmi_ext.ref_mv_stack[ref_frame][0].this_mv.as_mv, &y_sad_zero,
328 me_search_size_col, me_search_size_row);
329
330 if (tmp_sad > x->pred_mv_sad[LAST_FRAME]) return -1;
331
332 this_ref_frm_newmv->as_int = mi->mv[0].as_int;
333 int_mv best_mv = mi->mv[0];
334 best_mv.as_mv.row >>= 3;
335 best_mv.as_mv.col >>= 3;
336 MV ref_mv = av1_get_ref_mv(x, 0).as_mv;
337 this_ref_frm_newmv->as_mv.row >>= 3;
338 this_ref_frm_newmv->as_mv.col >>= 3;
339
340 SUBPEL_MOTION_SEARCH_PARAMS ms_params;
341 av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv, NULL);
342 if (cpi->sf.rt_sf.reduce_mv_pel_precision_highmotion ||
343 cpi->sf.rt_sf.reduce_mv_pel_precision_lowcomplex) {
344 FULLPEL_MV start_mv = { .row = 0, .col = 0 };
345 ms_params.forced_stop =
346 subpel_select(cpi, x, bsize, &best_mv, ref_mv, start_mv, false);
347 }
348 MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
349 assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, start_mv));
350 cpi->mv_search_params.find_fractional_mv_step(
351 xd, cm, &ms_params, start_mv, NULL, &best_mv.as_mv, &dis,
352 &x->pred_sse[ref_frame], NULL);
353 this_ref_frm_newmv->as_int = best_mv.as_int;
354
355 // When NEWMV is same as ref_mv from the drl, it is preferred to code the
356 // MV as NEARESTMV or NEARMV. In this case, NEWMV needs to be skipped to
357 // avoid an assert failure at a later stage. The scenario can occur if
358 // NEARESTMV was not evaluated for ALTREF.
359 if (this_ref_frm_newmv->as_mv.col == ref_mv.col &&
360 this_ref_frm_newmv->as_mv.row == ref_mv.row)
361 return -1;
362
363 *rate_mv = av1_mv_bit_cost(&this_ref_frm_newmv->as_mv, &ref_mv,
364 x->mv_costs->nmv_joint_cost,
365 x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
366 } else if (!combined_motion_search(cpi, x, bsize, &frame_mv[NEWMV][ref_frame],
367 rate_mv, best_rdc->rdcost, 0)) {
368 return -1;
369 }
370
371 return 0;
372 }
373
estimate_single_ref_frame_costs(const AV1_COMMON * cm,const MACROBLOCKD * xd,const ModeCosts * mode_costs,int segment_id,BLOCK_SIZE bsize,unsigned int * ref_costs_single)374 static void estimate_single_ref_frame_costs(const AV1_COMMON *cm,
375 const MACROBLOCKD *xd,
376 const ModeCosts *mode_costs,
377 int segment_id, BLOCK_SIZE bsize,
378 unsigned int *ref_costs_single) {
379 int seg_ref_active =
380 segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME);
381 if (seg_ref_active) {
382 memset(ref_costs_single, 0, REF_FRAMES * sizeof(*ref_costs_single));
383 } else {
384 int intra_inter_ctx = av1_get_intra_inter_context(xd);
385 ref_costs_single[INTRA_FRAME] =
386 mode_costs->intra_inter_cost[intra_inter_ctx][0];
387 unsigned int base_cost = mode_costs->intra_inter_cost[intra_inter_ctx][1];
388 if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT &&
389 is_comp_ref_allowed(bsize)) {
390 const int comp_ref_type_ctx = av1_get_comp_reference_type_context(xd);
391 base_cost += mode_costs->comp_ref_type_cost[comp_ref_type_ctx][1];
392 }
393 ref_costs_single[LAST_FRAME] = base_cost;
394 ref_costs_single[GOLDEN_FRAME] = base_cost;
395 ref_costs_single[ALTREF_FRAME] = base_cost;
396 // add cost for last, golden, altref
397 ref_costs_single[LAST_FRAME] += mode_costs->single_ref_cost[0][0][0];
398 ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][0][1];
399 ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][1][0];
400 ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][0][1];
401 ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][2][0];
402 }
403 }
404
set_force_skip_flag(const AV1_COMP * const cpi,MACROBLOCK * const x,unsigned int sse,int * force_skip)405 static inline void set_force_skip_flag(const AV1_COMP *const cpi,
406 MACROBLOCK *const x, unsigned int sse,
407 int *force_skip) {
408 if (x->txfm_search_params.tx_mode_search_type == TX_MODE_SELECT &&
409 cpi->sf.rt_sf.tx_size_level_based_on_qstep &&
410 cpi->sf.rt_sf.tx_size_level_based_on_qstep >= 2) {
411 const int qstep = x->plane[AOM_PLANE_Y].dequant_QTX[1] >> (x->e_mbd.bd - 5);
412 const unsigned int qstep_sq = qstep * qstep;
413 // If the sse is low for low source variance blocks, mark those as
414 // transform skip.
415 // Note: Though qstep_sq is based on ac qstep, the threshold is kept
416 // low so that reliable early estimate of tx skip can be obtained
417 // through its comparison with sse.
418 if (sse < qstep_sq && x->source_variance < qstep_sq &&
419 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
420 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 0)
421 *force_skip = 1;
422 }
423 }
424
425 #define CAP_TX_SIZE_FOR_BSIZE_GT32(tx_mode_search_type, bsize) \
426 (((tx_mode_search_type) != ONLY_4X4 && (bsize) > BLOCK_32X32) ? true : false)
427 #define TX_SIZE_FOR_BSIZE_GT32 (TX_16X16)
428
calculate_tx_size(const AV1_COMP * const cpi,BLOCK_SIZE bsize,MACROBLOCK * const x,unsigned int var,unsigned int sse,int * force_skip)429 static TX_SIZE calculate_tx_size(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
430 MACROBLOCK *const x, unsigned int var,
431 unsigned int sse, int *force_skip) {
432 MACROBLOCKD *const xd = &x->e_mbd;
433 TX_SIZE tx_size;
434 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
435 if (txfm_params->tx_mode_search_type == TX_MODE_SELECT) {
436 int multiplier = 8;
437 unsigned int var_thresh = 0;
438 unsigned int is_high_var = 1;
439 // Use quantizer based thresholds to determine transform size.
440 if (cpi->sf.rt_sf.tx_size_level_based_on_qstep) {
441 const int qband = x->qindex >> (QINDEX_BITS - 2);
442 const int mult[4] = { 8, 7, 6, 5 };
443 assert(qband < 4);
444 multiplier = mult[qband];
445 const int qstep = x->plane[AOM_PLANE_Y].dequant_QTX[1] >> (xd->bd - 5);
446 const unsigned int qstep_sq = qstep * qstep;
447 var_thresh = qstep_sq * 2;
448 if (cpi->sf.rt_sf.tx_size_level_based_on_qstep >= 2) {
449 // If the sse is low for low source variance blocks, mark those as
450 // transform skip.
451 // Note: Though qstep_sq is based on ac qstep, the threshold is kept
452 // low so that reliable early estimate of tx skip can be obtained
453 // through its comparison with sse.
454 if (sse < qstep_sq && x->source_variance < qstep_sq &&
455 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
456 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 0)
457 *force_skip = 1;
458 // Further lower transform size based on aq mode only if residual
459 // variance is high.
460 is_high_var = (var >= var_thresh);
461 }
462 }
463 // Choose larger transform size for blocks where dc component is dominant or
464 // the ac component is low.
465 if (sse > ((var * multiplier) >> 2) || (var < var_thresh))
466 tx_size =
467 AOMMIN(max_txsize_lookup[bsize],
468 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
469 else
470 tx_size = TX_8X8;
471
472 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
473 cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) && is_high_var)
474 tx_size = TX_8X8;
475 else if (tx_size > TX_16X16)
476 tx_size = TX_16X16;
477 } else {
478 tx_size =
479 AOMMIN(max_txsize_lookup[bsize],
480 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
481 }
482
483 if (CAP_TX_SIZE_FOR_BSIZE_GT32(txfm_params->tx_mode_search_type, bsize))
484 tx_size = TX_SIZE_FOR_BSIZE_GT32;
485
486 return AOMMIN(tx_size, TX_16X16);
487 }
488
block_variance(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int w,int h,unsigned int * sse,int * sum,int block_size,uint32_t * sse8x8,int * sum8x8,uint32_t * var8x8)489 static void block_variance(const uint8_t *src, int src_stride,
490 const uint8_t *ref, int ref_stride, int w, int h,
491 unsigned int *sse, int *sum, int block_size,
492 uint32_t *sse8x8, int *sum8x8, uint32_t *var8x8) {
493 int k = 0;
494 *sse = 0;
495 *sum = 0;
496
497 // This function is called for block sizes >= BLOCK_32x32. As per the design
498 // the aom_get_var_sse_sum_8x8_quad() processes four 8x8 blocks (in a 8x32)
499 // per call. Hence the width and height of the block need to be at least 8 and
500 // 32 samples respectively.
501 assert(w >= 32);
502 assert(h >= 8);
503 for (int row = 0; row < h; row += block_size) {
504 for (int col = 0; col < w; col += 32) {
505 aom_get_var_sse_sum_8x8_quad(src + src_stride * row + col, src_stride,
506 ref + ref_stride * row + col, ref_stride,
507 &sse8x8[k], &sum8x8[k], sse, sum,
508 &var8x8[k]);
509 k += 4;
510 }
511 }
512 }
513
block_variance_16x16_dual(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int w,int h,unsigned int * sse,int * sum,int block_size,uint32_t * sse16x16,uint32_t * var16x16)514 static void block_variance_16x16_dual(const uint8_t *src, int src_stride,
515 const uint8_t *ref, int ref_stride, int w,
516 int h, unsigned int *sse, int *sum,
517 int block_size, uint32_t *sse16x16,
518 uint32_t *var16x16) {
519 int k = 0;
520 *sse = 0;
521 *sum = 0;
522 // This function is called for block sizes >= BLOCK_32x32. As per the design
523 // the aom_get_var_sse_sum_16x16_dual() processes four 16x16 blocks (in a
524 // 16x32) per call. Hence the width and height of the block need to be at
525 // least 16 and 32 samples respectively.
526 assert(w >= 32);
527 assert(h >= 16);
528 for (int row = 0; row < h; row += block_size) {
529 for (int col = 0; col < w; col += 32) {
530 aom_get_var_sse_sum_16x16_dual(src + src_stride * row + col, src_stride,
531 ref + ref_stride * row + col, ref_stride,
532 &sse16x16[k], sse, sum, &var16x16[k]);
533 k += 2;
534 }
535 }
536 }
537
calculate_variance(int bw,int bh,TX_SIZE tx_size,unsigned int * sse_i,int * sum_i,unsigned int * var_o,unsigned int * sse_o,int * sum_o)538 static void calculate_variance(int bw, int bh, TX_SIZE tx_size,
539 unsigned int *sse_i, int *sum_i,
540 unsigned int *var_o, unsigned int *sse_o,
541 int *sum_o) {
542 const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size];
543 const int nw = 1 << (bw - b_width_log2_lookup[unit_size]);
544 const int nh = 1 << (bh - b_height_log2_lookup[unit_size]);
545 int row, col, k = 0;
546
547 for (row = 0; row < nh; row += 2) {
548 for (col = 0; col < nw; col += 2) {
549 sse_o[k] = sse_i[row * nw + col] + sse_i[row * nw + col + 1] +
550 sse_i[(row + 1) * nw + col] + sse_i[(row + 1) * nw + col + 1];
551 sum_o[k] = sum_i[row * nw + col] + sum_i[row * nw + col + 1] +
552 sum_i[(row + 1) * nw + col] + sum_i[(row + 1) * nw + col + 1];
553 var_o[k] = sse_o[k] - (uint32_t)(((int64_t)sum_o[k] * sum_o[k]) >>
554 (b_width_log2_lookup[unit_size] +
555 b_height_log2_lookup[unit_size] + 6));
556 k++;
557 }
558 }
559 }
560
561 // Adjust the ac_thr according to speed, width, height and normalized sum
ac_thr_factor(int speed,int width,int height,int norm_sum)562 static int ac_thr_factor(int speed, int width, int height, int norm_sum) {
563 if (speed >= 8 && norm_sum < 5) {
564 if (width <= 640 && height <= 480)
565 return 4;
566 else
567 return 2;
568 }
569 return 1;
570 }
571
572 // Sets early_term flag based on chroma planes prediction
set_early_term_based_on_uv_plane(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,MACROBLOCKD * xd,int mi_row,int mi_col,int * early_term,int num_blk,const unsigned int * sse_tx,const unsigned int * var_tx,int sum,unsigned int var,unsigned int sse)573 static inline void set_early_term_based_on_uv_plane(
574 AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, MACROBLOCKD *xd, int mi_row,
575 int mi_col, int *early_term, int num_blk, const unsigned int *sse_tx,
576 const unsigned int *var_tx, int sum, unsigned int var, unsigned int sse) {
577 AV1_COMMON *const cm = &cpi->common;
578 struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
579 const uint32_t dc_quant = p->dequant_QTX[0];
580 const uint32_t ac_quant = p->dequant_QTX[1];
581 int64_t dc_thr = dc_quant * dc_quant >> 6;
582 int64_t ac_thr = ac_quant * ac_quant >> 6;
583 const int bw = b_width_log2_lookup[bsize];
584 const int bh = b_height_log2_lookup[bsize];
585 int ac_test = 1;
586 int dc_test = 1;
587 const int norm_sum = abs(sum) >> (bw + bh);
588
589 #if CONFIG_AV1_TEMPORAL_DENOISING
590 if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
591 cpi->oxcf.speed > 5)
592 ac_thr = av1_scale_acskip_thresh(ac_thr, cpi->denoiser.denoising_level,
593 norm_sum, cpi->svc.temporal_layer_id);
594 else
595 ac_thr *= ac_thr_factor(cpi->oxcf.speed, cm->width, cm->height, norm_sum);
596 #else
597 ac_thr *= ac_thr_factor(cpi->oxcf.speed, cm->width, cm->height, norm_sum);
598
599 #endif
600
601 if (cpi->sf.rt_sf.increase_source_sad_thresh) {
602 dc_thr = dc_thr << 1;
603 ac_thr = ac_thr << 2;
604 }
605
606 for (int k = 0; k < num_blk; k++) {
607 // Check if all ac coefficients can be quantized to zero.
608 if (!(var_tx[k] < ac_thr || var == 0)) {
609 ac_test = 0;
610 break;
611 }
612 // Check if dc coefficient can be quantized to zero.
613 if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) {
614 dc_test = 0;
615 break;
616 }
617 }
618
619 // Check if chroma can be skipped based on ac and dc test flags.
620 if (ac_test && dc_test) {
621 int skip_uv[2] = { 0 };
622 unsigned int var_uv[2];
623 unsigned int sse_uv[2];
624 // Transform skipping test in UV planes.
625 for (int plane = AOM_PLANE_U; plane <= AOM_PLANE_V; plane++) {
626 int j = plane - 1;
627 skip_uv[j] = 1;
628 if (x->color_sensitivity[COLOR_SENS_IDX(plane)]) {
629 skip_uv[j] = 0;
630 struct macroblock_plane *const puv = &x->plane[plane];
631 struct macroblockd_plane *const puvd = &xd->plane[plane];
632 const BLOCK_SIZE uv_bsize = get_plane_block_size(
633 bsize, puvd->subsampling_x, puvd->subsampling_y);
634 // Adjust these thresholds for UV.
635 const int shift_ac = cpi->sf.rt_sf.increase_source_sad_thresh ? 5 : 3;
636 const int shift_dc = cpi->sf.rt_sf.increase_source_sad_thresh ? 4 : 3;
637 const int64_t uv_dc_thr =
638 (puv->dequant_QTX[0] * puv->dequant_QTX[0]) >> shift_dc;
639 const int64_t uv_ac_thr =
640 (puv->dequant_QTX[1] * puv->dequant_QTX[1]) >> shift_ac;
641 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
642 plane, plane);
643 var_uv[j] = cpi->ppi->fn_ptr[uv_bsize].vf(puv->src.buf, puv->src.stride,
644 puvd->dst.buf,
645 puvd->dst.stride, &sse_uv[j]);
646 if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) &&
647 (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j]))
648 skip_uv[j] = 1;
649 else
650 break;
651 }
652 }
653 if (skip_uv[0] & skip_uv[1]) {
654 *early_term = 1;
655 }
656 }
657 }
658
calc_rate_dist_block_param(AV1_COMP * cpi,MACROBLOCK * x,RD_STATS * rd_stats,int calculate_rd,int * early_term,BLOCK_SIZE bsize,unsigned int sse)659 static inline void calc_rate_dist_block_param(AV1_COMP *cpi, MACROBLOCK *x,
660 RD_STATS *rd_stats,
661 int calculate_rd, int *early_term,
662 BLOCK_SIZE bsize,
663 unsigned int sse) {
664 if (calculate_rd) {
665 if (!*early_term) {
666 const int bw = block_size_wide[bsize];
667 const int bh = block_size_high[bsize];
668
669 model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, rd_stats->sse, bw * bh,
670 &rd_stats->rate, &rd_stats->dist);
671 }
672
673 if (*early_term) {
674 rd_stats->rate = 0;
675 rd_stats->dist = sse << 4;
676 }
677 }
678 }
679
model_skip_for_sb_y_large_64(AV1_COMP * cpi,BLOCK_SIZE bsize,int mi_row,int mi_col,MACROBLOCK * x,MACROBLOCKD * xd,RD_STATS * rd_stats,int * early_term,int calculate_rd,int64_t best_sse,unsigned int * var_output,unsigned int var_prune_threshold)680 static void model_skip_for_sb_y_large_64(AV1_COMP *cpi, BLOCK_SIZE bsize,
681 int mi_row, int mi_col, MACROBLOCK *x,
682 MACROBLOCKD *xd, RD_STATS *rd_stats,
683 int *early_term, int calculate_rd,
684 int64_t best_sse,
685 unsigned int *var_output,
686 unsigned int var_prune_threshold) {
687 // Note our transform coeffs are 8 times an orthogonal transform.
688 // Hence quantizer step is also 8 times. To get effective quantizer
689 // we need to divide by 8 before sending to modeling function.
690 unsigned int sse;
691 struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
692 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
693 int test_skip = 1;
694 unsigned int var;
695 int sum;
696 const int bw = b_width_log2_lookup[bsize];
697 const int bh = b_height_log2_lookup[bsize];
698 unsigned int sse16x16[64] = { 0 };
699 unsigned int var16x16[64] = { 0 };
700 assert(xd->mi[0]->tx_size == TX_16X16);
701 assert(bsize > BLOCK_32X32);
702
703 // Calculate variance for whole partition, and also save 16x16 blocks'
704 // variance to be used in following transform skipping test.
705 block_variance_16x16_dual(p->src.buf, p->src.stride, pd->dst.buf,
706 pd->dst.stride, 4 << bw, 4 << bh, &sse, &sum, 16,
707 sse16x16, var16x16);
708
709 var = sse - (unsigned int)(((int64_t)sum * sum) >> (bw + bh + 4));
710 if (var_output) {
711 *var_output = var;
712 if (*var_output > var_prune_threshold) {
713 return;
714 }
715 }
716
717 rd_stats->sse = sse;
718 // Skipping test
719 *early_term = 0;
720 set_force_skip_flag(cpi, x, sse, early_term);
721 // The code below for setting skip flag assumes transform size of at least
722 // 8x8, so force this lower limit on transform.
723 MB_MODE_INFO *const mi = xd->mi[0];
724 if (!calculate_rd && cpi->sf.rt_sf.sse_early_term_inter_search &&
725 early_term_inter_search_with_sse(
726 cpi->sf.rt_sf.sse_early_term_inter_search, bsize, sse, best_sse,
727 mi->mode))
728 test_skip = 0;
729
730 if (*early_term) test_skip = 0;
731
732 // Evaluate if the partition block is a skippable block in Y plane.
733 if (test_skip) {
734 const unsigned int *sse_tx = sse16x16;
735 const unsigned int *var_tx = var16x16;
736 const unsigned int num_block = (1 << (bw + bh - 2)) >> 2;
737 set_early_term_based_on_uv_plane(cpi, x, bsize, xd, mi_row, mi_col,
738 early_term, num_block, sse_tx, var_tx, sum,
739 var, sse);
740 }
741 calc_rate_dist_block_param(cpi, x, rd_stats, calculate_rd, early_term, bsize,
742 sse);
743 }
744
model_skip_for_sb_y_large(AV1_COMP * cpi,BLOCK_SIZE bsize,int mi_row,int mi_col,MACROBLOCK * x,MACROBLOCKD * xd,RD_STATS * rd_stats,int * early_term,int calculate_rd,int64_t best_sse,unsigned int * var_output,unsigned int var_prune_threshold)745 static void model_skip_for_sb_y_large(AV1_COMP *cpi, BLOCK_SIZE bsize,
746 int mi_row, int mi_col, MACROBLOCK *x,
747 MACROBLOCKD *xd, RD_STATS *rd_stats,
748 int *early_term, int calculate_rd,
749 int64_t best_sse,
750 unsigned int *var_output,
751 unsigned int var_prune_threshold) {
752 if (x->force_zeromv_skip_for_blk) {
753 *early_term = 1;
754 rd_stats->rate = 0;
755 rd_stats->dist = 0;
756 rd_stats->sse = 0;
757 return;
758 }
759
760 // For block sizes greater than 32x32, the transform size is always 16x16.
761 // This function avoids calling calculate_variance() for tx_size 16x16 cases
762 // by directly populating variance at tx_size level from
763 // block_variance_16x16_dual() function.
764 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
765 if (CAP_TX_SIZE_FOR_BSIZE_GT32(txfm_params->tx_mode_search_type, bsize)) {
766 xd->mi[0]->tx_size = TX_SIZE_FOR_BSIZE_GT32;
767 model_skip_for_sb_y_large_64(cpi, bsize, mi_row, mi_col, x, xd, rd_stats,
768 early_term, calculate_rd, best_sse, var_output,
769 var_prune_threshold);
770 return;
771 }
772
773 // Note our transform coeffs are 8 times an orthogonal transform.
774 // Hence quantizer step is also 8 times. To get effective quantizer
775 // we need to divide by 8 before sending to modeling function.
776 unsigned int sse;
777 struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
778 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
779 int test_skip = 1;
780 unsigned int var;
781 int sum;
782
783 const int bw = b_width_log2_lookup[bsize];
784 const int bh = b_height_log2_lookup[bsize];
785 unsigned int sse8x8[256] = { 0 };
786 int sum8x8[256] = { 0 };
787 unsigned int var8x8[256] = { 0 };
788 TX_SIZE tx_size;
789
790 // Calculate variance for whole partition, and also save 8x8 blocks' variance
791 // to be used in following transform skipping test.
792 block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
793 4 << bw, 4 << bh, &sse, &sum, 8, sse8x8, sum8x8, var8x8);
794 var = sse - (unsigned int)(((int64_t)sum * sum) >> (bw + bh + 4));
795 if (var_output) {
796 *var_output = var;
797 if (*var_output > var_prune_threshold) {
798 return;
799 }
800 }
801
802 rd_stats->sse = sse;
803 // Skipping test
804 *early_term = 0;
805 tx_size = calculate_tx_size(cpi, bsize, x, var, sse, early_term);
806 assert(tx_size <= TX_16X16);
807 // The code below for setting skip flag assumes transform size of at least
808 // 8x8, so force this lower limit on transform.
809 if (tx_size < TX_8X8) tx_size = TX_8X8;
810 xd->mi[0]->tx_size = tx_size;
811
812 MB_MODE_INFO *const mi = xd->mi[0];
813 if (!calculate_rd && cpi->sf.rt_sf.sse_early_term_inter_search &&
814 early_term_inter_search_with_sse(
815 cpi->sf.rt_sf.sse_early_term_inter_search, bsize, sse, best_sse,
816 mi->mode))
817 test_skip = 0;
818
819 if (*early_term) test_skip = 0;
820
821 // Evaluate if the partition block is a skippable block in Y plane.
822 if (test_skip) {
823 unsigned int sse16x16[64] = { 0 };
824 int sum16x16[64] = { 0 };
825 unsigned int var16x16[64] = { 0 };
826 const unsigned int *sse_tx = sse8x8;
827 const unsigned int *var_tx = var8x8;
828 unsigned int num_blks = 1 << (bw + bh - 2);
829
830 if (tx_size >= TX_16X16) {
831 calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16,
832 sum16x16);
833 sse_tx = sse16x16;
834 var_tx = var16x16;
835 num_blks = num_blks >> 2;
836 }
837 set_early_term_based_on_uv_plane(cpi, x, bsize, xd, mi_row, mi_col,
838 early_term, num_blks, sse_tx, var_tx, sum,
839 var, sse);
840 }
841 calc_rate_dist_block_param(cpi, x, rd_stats, calculate_rd, early_term, bsize,
842 sse);
843 }
844
model_rd_for_sb_y(const AV1_COMP * const cpi,BLOCK_SIZE bsize,MACROBLOCK * x,MACROBLOCKD * xd,RD_STATS * rd_stats,unsigned int * var_out,int calculate_rd,int * early_term)845 static void model_rd_for_sb_y(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
846 MACROBLOCK *x, MACROBLOCKD *xd,
847 RD_STATS *rd_stats, unsigned int *var_out,
848 int calculate_rd, int *early_term) {
849 if (x->force_zeromv_skip_for_blk && early_term != NULL) {
850 *early_term = 1;
851 rd_stats->rate = 0;
852 rd_stats->dist = 0;
853 rd_stats->sse = 0;
854 }
855
856 // Note our transform coeffs are 8 times an orthogonal transform.
857 // Hence quantizer step is also 8 times. To get effective quantizer
858 // we need to divide by 8 before sending to modeling function.
859 const int ref = xd->mi[0]->ref_frame[0];
860
861 assert(bsize < BLOCK_SIZES_ALL);
862
863 struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
864 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
865 unsigned int sse;
866 int rate;
867 int64_t dist;
868
869 unsigned int var = cpi->ppi->fn_ptr[bsize].vf(
870 p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, &sse);
871 int force_skip = 0;
872 xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, x, var, sse, &force_skip);
873 if (var_out) {
874 *var_out = var;
875 }
876
877 if (calculate_rd && (!force_skip || ref == INTRA_FRAME)) {
878 const int bwide = block_size_wide[bsize];
879 const int bhigh = block_size_high[bsize];
880 model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh, &rate,
881 &dist);
882 } else {
883 rate = INT_MAX; // this will be overwritten later with av1_block_yrd
884 dist = INT_MAX;
885 }
886 rd_stats->sse = sse;
887 x->pred_sse[ref] = (unsigned int)AOMMIN(sse, UINT_MAX);
888
889 if (force_skip && ref > INTRA_FRAME) {
890 rate = 0;
891 dist = (int64_t)sse << 4;
892 }
893
894 assert(rate >= 0);
895
896 rd_stats->skip_txfm = (rate == 0);
897 rate = AOMMIN(rate, INT_MAX);
898 rd_stats->rate = rate;
899 rd_stats->dist = dist;
900 }
901
get_drl_cost(PREDICTION_MODE this_mode,int ref_mv_idx,const MB_MODE_INFO_EXT * mbmi_ext,const int (* const drl_mode_cost0)[2],int8_t ref_frame_type)902 static inline int get_drl_cost(PREDICTION_MODE this_mode, int ref_mv_idx,
903 const MB_MODE_INFO_EXT *mbmi_ext,
904 const int (*const drl_mode_cost0)[2],
905 int8_t ref_frame_type) {
906 int cost = 0;
907 if (this_mode == NEWMV || this_mode == NEW_NEWMV) {
908 for (int idx = 0; idx < 2; ++idx) {
909 if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
910 uint8_t drl_ctx = av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx);
911 cost += drl_mode_cost0[drl_ctx][ref_mv_idx != idx];
912 if (ref_mv_idx == idx) return cost;
913 }
914 }
915 return cost;
916 }
917
918 if (have_nearmv_in_inter_mode(this_mode)) {
919 for (int idx = 1; idx < 3; ++idx) {
920 if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
921 uint8_t drl_ctx = av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx);
922 cost += drl_mode_cost0[drl_ctx][ref_mv_idx != (idx - 1)];
923 if (ref_mv_idx == (idx - 1)) return cost;
924 }
925 }
926 return cost;
927 }
928 return cost;
929 }
930
cost_mv_ref(const ModeCosts * const mode_costs,PREDICTION_MODE mode,int16_t mode_context)931 static int cost_mv_ref(const ModeCosts *const mode_costs, PREDICTION_MODE mode,
932 int16_t mode_context) {
933 if (is_inter_compound_mode(mode)) {
934 return mode_costs
935 ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)];
936 }
937
938 int mode_cost = 0;
939 int16_t mode_ctx = mode_context & NEWMV_CTX_MASK;
940
941 assert(is_inter_mode(mode));
942
943 if (mode == NEWMV) {
944 mode_cost = mode_costs->newmv_mode_cost[mode_ctx][0];
945 return mode_cost;
946 } else {
947 mode_cost = mode_costs->newmv_mode_cost[mode_ctx][1];
948 mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
949
950 if (mode == GLOBALMV) {
951 mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][0];
952 return mode_cost;
953 } else {
954 mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][1];
955 mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
956 mode_cost += mode_costs->refmv_mode_cost[mode_ctx][mode != NEARESTMV];
957 return mode_cost;
958 }
959 }
960 }
961
newmv_diff_bias(MACROBLOCKD * xd,PREDICTION_MODE this_mode,RD_STATS * this_rdc,BLOCK_SIZE bsize,int mv_row,int mv_col,int speed,uint32_t spatial_variance,CONTENT_STATE_SB content_state_sb)962 static void newmv_diff_bias(MACROBLOCKD *xd, PREDICTION_MODE this_mode,
963 RD_STATS *this_rdc, BLOCK_SIZE bsize, int mv_row,
964 int mv_col, int speed, uint32_t spatial_variance,
965 CONTENT_STATE_SB content_state_sb) {
966 // Bias against MVs associated with NEWMV mode that are very different from
967 // top/left neighbors.
968 if (this_mode == NEWMV) {
969 int al_mv_average_row;
970 int al_mv_average_col;
971 int row_diff, col_diff;
972 int above_mv_valid = 0;
973 int left_mv_valid = 0;
974 int above_row = INVALID_MV_ROW_COL, above_col = INVALID_MV_ROW_COL;
975 int left_row = INVALID_MV_ROW_COL, left_col = INVALID_MV_ROW_COL;
976 if (bsize >= BLOCK_64X64 && content_state_sb.source_sad_nonrd != kHighSad &&
977 spatial_variance < 300 &&
978 (mv_row > 16 || mv_row < -16 || mv_col > 16 || mv_col < -16)) {
979 this_rdc->rdcost = this_rdc->rdcost << 2;
980 return;
981 }
982 if (xd->above_mbmi) {
983 above_mv_valid = xd->above_mbmi->mv[0].as_int != INVALID_MV;
984 above_row = xd->above_mbmi->mv[0].as_mv.row;
985 above_col = xd->above_mbmi->mv[0].as_mv.col;
986 }
987 if (xd->left_mbmi) {
988 left_mv_valid = xd->left_mbmi->mv[0].as_int != INVALID_MV;
989 left_row = xd->left_mbmi->mv[0].as_mv.row;
990 left_col = xd->left_mbmi->mv[0].as_mv.col;
991 }
992 if (above_mv_valid && left_mv_valid) {
993 al_mv_average_row = (above_row + left_row + 1) >> 1;
994 al_mv_average_col = (above_col + left_col + 1) >> 1;
995 } else if (above_mv_valid) {
996 al_mv_average_row = above_row;
997 al_mv_average_col = above_col;
998 } else if (left_mv_valid) {
999 al_mv_average_row = left_row;
1000 al_mv_average_col = left_col;
1001 } else {
1002 al_mv_average_row = al_mv_average_col = 0;
1003 }
1004 row_diff = al_mv_average_row - mv_row;
1005 col_diff = al_mv_average_col - mv_col;
1006 if (row_diff > 80 || row_diff < -80 || col_diff > 80 || col_diff < -80) {
1007 if (bsize >= BLOCK_32X32)
1008 this_rdc->rdcost = this_rdc->rdcost << 1;
1009 else
1010 this_rdc->rdcost = 5 * this_rdc->rdcost >> 2;
1011 }
1012 } else {
1013 // Bias for speed >= 8 for low spatial variance.
1014 if (speed >= 8 && spatial_variance < 150 &&
1015 (mv_row > 64 || mv_row < -64 || mv_col > 64 || mv_col < -64))
1016 this_rdc->rdcost = 5 * this_rdc->rdcost >> 2;
1017 }
1018 }
1019
update_thresh_freq_fact(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,MV_REFERENCE_FRAME ref_frame,THR_MODES best_mode_idx,PREDICTION_MODE mode)1020 static inline void update_thresh_freq_fact(AV1_COMP *cpi, MACROBLOCK *x,
1021 BLOCK_SIZE bsize,
1022 MV_REFERENCE_FRAME ref_frame,
1023 THR_MODES best_mode_idx,
1024 PREDICTION_MODE mode) {
1025 const THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
1026 const BLOCK_SIZE min_size = AOMMAX(bsize - 3, BLOCK_4X4);
1027 const BLOCK_SIZE max_size = AOMMIN(bsize + 6, BLOCK_128X128);
1028 for (BLOCK_SIZE bs = min_size; bs <= max_size; bs += 3) {
1029 int *freq_fact = &x->thresh_freq_fact[bs][thr_mode_idx];
1030 if (thr_mode_idx == best_mode_idx) {
1031 *freq_fact -= (*freq_fact >> 4);
1032 } else {
1033 *freq_fact =
1034 AOMMIN(*freq_fact + RD_THRESH_INC,
1035 cpi->sf.inter_sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
1036 }
1037 }
1038 }
1039
1040 #if CONFIG_AV1_TEMPORAL_DENOISING
av1_pickmode_ctx_den_update(AV1_PICKMODE_CTX_DEN * ctx_den,int64_t zero_last_cost_orig,unsigned int ref_frame_cost[REF_FRAMES],int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],int reuse_inter_pred,BEST_PICKMODE * bp)1041 static void av1_pickmode_ctx_den_update(
1042 AV1_PICKMODE_CTX_DEN *ctx_den, int64_t zero_last_cost_orig,
1043 unsigned int ref_frame_cost[REF_FRAMES],
1044 int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], int reuse_inter_pred,
1045 BEST_PICKMODE *bp) {
1046 ctx_den->zero_last_cost_orig = zero_last_cost_orig;
1047 ctx_den->ref_frame_cost = ref_frame_cost;
1048 ctx_den->frame_mv = frame_mv;
1049 ctx_den->reuse_inter_pred = reuse_inter_pred;
1050 ctx_den->best_tx_size = bp->best_tx_size;
1051 ctx_den->best_mode = bp->best_mode;
1052 ctx_den->best_ref_frame = bp->best_ref_frame;
1053 ctx_den->best_pred_filter = bp->best_pred_filter;
1054 ctx_den->best_mode_skip_txfm = bp->best_mode_skip_txfm;
1055 }
1056
recheck_zeromv_after_denoising(AV1_COMP * cpi,MB_MODE_INFO * const mi,MACROBLOCK * x,MACROBLOCKD * const xd,AV1_DENOISER_DECISION decision,AV1_PICKMODE_CTX_DEN * ctx_den,struct buf_2d yv12_mb[4][MAX_MB_PLANE],RD_STATS * best_rdc,BEST_PICKMODE * best_pickmode,BLOCK_SIZE bsize,int mi_row,int mi_col)1057 static void recheck_zeromv_after_denoising(
1058 AV1_COMP *cpi, MB_MODE_INFO *const mi, MACROBLOCK *x, MACROBLOCKD *const xd,
1059 AV1_DENOISER_DECISION decision, AV1_PICKMODE_CTX_DEN *ctx_den,
1060 struct buf_2d yv12_mb[4][MAX_MB_PLANE], RD_STATS *best_rdc,
1061 BEST_PICKMODE *best_pickmode, BLOCK_SIZE bsize, int mi_row, int mi_col) {
1062 // If INTRA or GOLDEN reference was selected, re-evaluate ZEROMV on
1063 // denoised result. Only do this under noise conditions, and if rdcost of
1064 // ZEROMV on original source is not significantly higher than rdcost of best
1065 // mode.
1066 if (cpi->noise_estimate.enabled && cpi->noise_estimate.level > kLow &&
1067 ctx_den->zero_last_cost_orig < (best_rdc->rdcost << 3) &&
1068 ((ctx_den->best_ref_frame == INTRA_FRAME && decision >= FILTER_BLOCK) ||
1069 (ctx_den->best_ref_frame == GOLDEN_FRAME &&
1070 cpi->svc.number_spatial_layers == 1 &&
1071 decision == FILTER_ZEROMV_BLOCK))) {
1072 // Check if we should pick ZEROMV on denoised signal.
1073 AV1_COMMON *const cm = &cpi->common;
1074 RD_STATS this_rdc;
1075 const ModeCosts *mode_costs = &x->mode_costs;
1076 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
1077 MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
1078
1079 mi->mode = GLOBALMV;
1080 mi->ref_frame[0] = LAST_FRAME;
1081 mi->ref_frame[1] = NONE_FRAME;
1082 set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME);
1083 mi->mv[0].as_int = 0;
1084 mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
1085 xd->plane[AOM_PLANE_Y].pre[0] = yv12_mb[LAST_FRAME][AOM_PLANE_Y];
1086 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1087 unsigned int var;
1088 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, &var, 1, NULL);
1089
1090 const int16_t mode_ctx =
1091 av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame);
1092 this_rdc.rate += cost_mv_ref(mode_costs, GLOBALMV, mode_ctx);
1093
1094 this_rdc.rate += ctx_den->ref_frame_cost[LAST_FRAME];
1095 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1096 txfm_info->skip_txfm = this_rdc.skip_txfm;
1097 // Don't switch to ZEROMV if the rdcost for ZEROMV on denoised source
1098 // is higher than best_ref mode (on original source).
1099 if (this_rdc.rdcost > best_rdc->rdcost) {
1100 this_rdc = *best_rdc;
1101 mi->mode = best_pickmode->best_mode;
1102 mi->ref_frame[0] = best_pickmode->best_ref_frame;
1103 set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME);
1104 mi->interp_filters = best_pickmode->best_pred_filter;
1105 if (best_pickmode->best_ref_frame == INTRA_FRAME) {
1106 mi->mv[0].as_int = INVALID_MV;
1107 } else {
1108 mi->mv[0].as_int = ctx_den
1109 ->frame_mv[best_pickmode->best_mode]
1110 [best_pickmode->best_ref_frame]
1111 .as_int;
1112 if (ctx_den->reuse_inter_pred) {
1113 xd->plane[AOM_PLANE_Y].pre[0] = yv12_mb[GOLDEN_FRAME][AOM_PLANE_Y];
1114 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1115 }
1116 }
1117 mi->tx_size = best_pickmode->best_tx_size;
1118 txfm_info->skip_txfm = best_pickmode->best_mode_skip_txfm;
1119 } else {
1120 ctx_den->best_ref_frame = LAST_FRAME;
1121 *best_rdc = this_rdc;
1122 }
1123 }
1124 }
1125 #endif // CONFIG_AV1_TEMPORAL_DENOISING
1126
1127 /*!\brief Searches for the best interpolation filter
1128 *
1129 * \ingroup nonrd_mode_search
1130 * \callgraph
1131 * \callergraph
1132 * Iterates through subset of possible interpolation filters (EIGHTTAP_REGULAR,
1133 * EIGTHTAP_SMOOTH, MULTITAP_SHARP, depending on FILTER_SEARCH_SIZE) and selects
1134 * the one that gives lowest RD cost. RD cost is calculated using curvfit model.
1135 * Support for dual filters (different filters in the x & y directions) is
1136 * allowed if sf.interp_sf.disable_dual_filter = 0.
1137 *
1138 * \param[in] cpi Top-level encoder structure
1139 * \param[in] x Pointer to structure holding all the
1140 * data for the current macroblock
1141 * \param[in] this_rdc Pointer to calculated RD Cost
1142 * \param[in] inter_pred_params_sr Pointer to structure holding parameters of
1143 inter prediction for single reference
1144 * \param[in] mi_row Row index in 4x4 units
1145 * \param[in] mi_col Column index in 4x4 units
1146 * \param[in] tmp_buffer Pointer to a temporary buffer for
1147 * prediction re-use
1148 * \param[in] bsize Current block size
1149 * \param[in] reuse_inter_pred Flag, indicating prediction re-use
1150 * \param[out] this_mode_pred Pointer to store prediction buffer
1151 * for prediction re-use
1152 * \param[out] this_early_term Flag, indicating that transform can be
1153 * skipped
1154 * \param[out] var The residue variance of the current
1155 * predictor.
1156 * \param[in] use_model_yrd_large Flag, indicating special logic to handle
1157 * large blocks
1158 * \param[in] best_sse Best sse so far.
1159 * \param[in] is_single_pred Flag, indicating single mode.
1160 *
1161 * \remark Nothing is returned. Instead, calculated RD cost is placed to
1162 * \c this_rdc and best filter is placed to \c mi->interp_filters. In case
1163 * \c reuse_inter_pred flag is set, this function also outputs
1164 * \c this_mode_pred. Also \c this_early_temp is set if transform can be
1165 * skipped
1166 */
search_filter_ref(AV1_COMP * cpi,MACROBLOCK * x,RD_STATS * this_rdc,InterPredParams * inter_pred_params_sr,int mi_row,int mi_col,PRED_BUFFER * tmp_buffer,BLOCK_SIZE bsize,int reuse_inter_pred,PRED_BUFFER ** this_mode_pred,int * this_early_term,unsigned int * var,int use_model_yrd_large,int64_t best_sse,int is_single_pred)1167 static void search_filter_ref(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc,
1168 InterPredParams *inter_pred_params_sr, int mi_row,
1169 int mi_col, PRED_BUFFER *tmp_buffer,
1170 BLOCK_SIZE bsize, int reuse_inter_pred,
1171 PRED_BUFFER **this_mode_pred,
1172 int *this_early_term, unsigned int *var,
1173 int use_model_yrd_large, int64_t best_sse,
1174 int is_single_pred) {
1175 AV1_COMMON *const cm = &cpi->common;
1176 MACROBLOCKD *const xd = &x->e_mbd;
1177 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
1178 MB_MODE_INFO *const mi = xd->mi[0];
1179 const int bw = block_size_wide[bsize];
1180 int dim_factor =
1181 (cpi->sf.interp_sf.disable_dual_filter == 0) ? FILTER_SEARCH_SIZE : 1;
1182 RD_STATS pf_rd_stats[FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE] = { 0 };
1183 TX_SIZE pf_tx_size[FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE] = { 0 };
1184 PRED_BUFFER *current_pred = *this_mode_pred;
1185 int best_skip = 0;
1186 int best_early_term = 0;
1187 int64_t best_cost = INT64_MAX;
1188 int best_filter_index = -1;
1189
1190 SubpelParams subpel_params;
1191 // Initialize inter prediction params at mode level for single reference
1192 // mode.
1193 if (is_single_pred)
1194 init_inter_mode_params(&mi->mv[0].as_mv, inter_pred_params_sr,
1195 &subpel_params, xd->block_ref_scale_factors[0],
1196 pd->pre->width, pd->pre->height);
1197 for (int filter_idx = 0; filter_idx < FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE;
1198 ++filter_idx) {
1199 int64_t cost;
1200 if (cpi->sf.interp_sf.disable_dual_filter &&
1201 filters_ref_set[filter_idx].as_filters.x_filter !=
1202 filters_ref_set[filter_idx].as_filters.y_filter)
1203 continue;
1204
1205 mi->interp_filters.as_int = filters_ref_set[filter_idx].as_int;
1206 if (is_single_pred)
1207 av1_enc_build_inter_predictor_y_nonrd(xd, inter_pred_params_sr,
1208 &subpel_params);
1209 else
1210 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1211 AOM_PLANE_Y, AOM_PLANE_Y);
1212 unsigned int curr_var = UINT_MAX;
1213 if (use_model_yrd_large)
1214 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
1215 &pf_rd_stats[filter_idx], this_early_term, 1,
1216 best_sse, &curr_var, UINT_MAX);
1217 else
1218 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[filter_idx], &curr_var,
1219 1, NULL);
1220 pf_rd_stats[filter_idx].rate += av1_get_switchable_rate(
1221 x, xd, cm->features.interp_filter, cm->seq_params->enable_dual_filter);
1222 cost = RDCOST(x->rdmult, pf_rd_stats[filter_idx].rate,
1223 pf_rd_stats[filter_idx].dist);
1224 pf_tx_size[filter_idx] = mi->tx_size;
1225 if (cost < best_cost) {
1226 *var = curr_var;
1227 best_filter_index = filter_idx;
1228 best_cost = cost;
1229 best_skip = pf_rd_stats[filter_idx].skip_txfm;
1230 best_early_term = *this_early_term;
1231 if (reuse_inter_pred) {
1232 if (*this_mode_pred != current_pred) {
1233 free_pred_buffer(*this_mode_pred);
1234 *this_mode_pred = current_pred;
1235 }
1236 current_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
1237 pd->dst.buf = current_pred->data;
1238 pd->dst.stride = bw;
1239 }
1240 }
1241 }
1242 assert(best_filter_index >= 0 &&
1243 best_filter_index < dim_factor * FILTER_SEARCH_SIZE);
1244 if (reuse_inter_pred && *this_mode_pred != current_pred)
1245 free_pred_buffer(current_pred);
1246
1247 mi->interp_filters.as_int = filters_ref_set[best_filter_index].as_int;
1248 mi->tx_size = pf_tx_size[best_filter_index];
1249 this_rdc->rate = pf_rd_stats[best_filter_index].rate;
1250 this_rdc->dist = pf_rd_stats[best_filter_index].dist;
1251 this_rdc->sse = pf_rd_stats[best_filter_index].sse;
1252 this_rdc->skip_txfm = (best_skip || best_early_term);
1253 *this_early_term = best_early_term;
1254 if (reuse_inter_pred) {
1255 pd->dst.buf = (*this_mode_pred)->data;
1256 pd->dst.stride = (*this_mode_pred)->stride;
1257 } else if (best_filter_index < dim_factor * FILTER_SEARCH_SIZE - 1) {
1258 if (is_single_pred)
1259 av1_enc_build_inter_predictor_y_nonrd(xd, inter_pred_params_sr,
1260 &subpel_params);
1261 else
1262 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1263 AOM_PLANE_Y, AOM_PLANE_Y);
1264 }
1265 }
1266 #if !CONFIG_REALTIME_ONLY
1267
is_warped_mode_allowed(const AV1_COMP * cpi,MACROBLOCK * const x,const MB_MODE_INFO * mbmi)1268 static inline int is_warped_mode_allowed(const AV1_COMP *cpi,
1269 MACROBLOCK *const x,
1270 const MB_MODE_INFO *mbmi) {
1271 const FeatureFlags *const features = &cpi->common.features;
1272 const MACROBLOCKD *xd = &x->e_mbd;
1273
1274 if (cpi->sf.inter_sf.extra_prune_warped) return 0;
1275 if (has_second_ref(mbmi)) return 0;
1276 MOTION_MODE last_motion_mode_allowed = SIMPLE_TRANSLATION;
1277
1278 if (features->switchable_motion_mode) {
1279 // Determine which motion modes to search if more than SIMPLE_TRANSLATION
1280 // is allowed.
1281 last_motion_mode_allowed = motion_mode_allowed(
1282 xd->global_motion, xd, mbmi, features->allow_warped_motion);
1283 }
1284
1285 if (last_motion_mode_allowed == WARPED_CAUSAL) {
1286 return 1;
1287 }
1288
1289 return 0;
1290 }
1291
calc_num_proj_ref(AV1_COMP * cpi,MACROBLOCK * x,MB_MODE_INFO * mi)1292 static void calc_num_proj_ref(AV1_COMP *cpi, MACROBLOCK *x, MB_MODE_INFO *mi) {
1293 AV1_COMMON *const cm = &cpi->common;
1294 MACROBLOCKD *const xd = &x->e_mbd;
1295 const FeatureFlags *const features = &cm->features;
1296
1297 mi->num_proj_ref = 1;
1298 WARP_SAMPLE_INFO *const warp_sample_info =
1299 &x->warp_sample_info[mi->ref_frame[0]];
1300 int *pts0 = warp_sample_info->pts;
1301 int *pts_inref0 = warp_sample_info->pts_inref;
1302 MOTION_MODE last_motion_mode_allowed = SIMPLE_TRANSLATION;
1303
1304 if (features->switchable_motion_mode) {
1305 // Determine which motion modes to search if more than SIMPLE_TRANSLATION
1306 // is allowed.
1307 last_motion_mode_allowed = motion_mode_allowed(
1308 xd->global_motion, xd, mi, features->allow_warped_motion);
1309 }
1310
1311 if (last_motion_mode_allowed == WARPED_CAUSAL) {
1312 if (warp_sample_info->num < 0) {
1313 warp_sample_info->num = av1_findSamples(cm, xd, pts0, pts_inref0);
1314 }
1315 mi->num_proj_ref = warp_sample_info->num;
1316 }
1317 }
1318
search_motion_mode(AV1_COMP * cpi,MACROBLOCK * x,RD_STATS * this_rdc,int mi_row,int mi_col,BLOCK_SIZE bsize,int * this_early_term,int use_model_yrd_large,int * rate_mv,int64_t best_sse)1319 static void search_motion_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc,
1320 int mi_row, int mi_col, BLOCK_SIZE bsize,
1321 int *this_early_term, int use_model_yrd_large,
1322 int *rate_mv, int64_t best_sse) {
1323 AV1_COMMON *const cm = &cpi->common;
1324 MACROBLOCKD *const xd = &x->e_mbd;
1325 const FeatureFlags *const features = &cm->features;
1326 MB_MODE_INFO *const mi = xd->mi[0];
1327 RD_STATS pf_rd_stats[MOTION_MODE_SEARCH_SIZE] = { 0 };
1328 int best_skip = 0;
1329 int best_early_term = 0;
1330 int64_t best_cost = INT64_MAX;
1331 int best_mode_index = -1;
1332 const int interp_filter = features->interp_filter;
1333
1334 const MOTION_MODE motion_modes[MOTION_MODE_SEARCH_SIZE] = {
1335 SIMPLE_TRANSLATION, WARPED_CAUSAL
1336 };
1337 int mode_search_size = is_warped_mode_allowed(cpi, x, mi) ? 2 : 1;
1338
1339 WARP_SAMPLE_INFO *const warp_sample_info =
1340 &x->warp_sample_info[mi->ref_frame[0]];
1341 int *pts0 = warp_sample_info->pts;
1342 int *pts_inref0 = warp_sample_info->pts_inref;
1343
1344 const int total_samples = mi->num_proj_ref;
1345 if (total_samples == 0) {
1346 // Do not search WARPED_CAUSAL if there are no samples to use to determine
1347 // warped parameters.
1348 mode_search_size = 1;
1349 }
1350
1351 const MB_MODE_INFO base_mbmi = *mi;
1352 MB_MODE_INFO best_mbmi;
1353
1354 for (int mode_index = 0; mode_index < mode_search_size; ++mode_index) {
1355 int64_t cost = INT64_MAX;
1356 MOTION_MODE motion_mode = motion_modes[mode_index];
1357 *mi = base_mbmi;
1358 mi->motion_mode = motion_mode;
1359 if (motion_mode == SIMPLE_TRANSLATION) {
1360 mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
1361
1362 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1363 AOM_PLANE_Y, AOM_PLANE_Y);
1364 if (use_model_yrd_large)
1365 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
1366 &pf_rd_stats[mode_index], this_early_term, 1,
1367 best_sse, NULL, UINT_MAX);
1368 else
1369 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[mode_index], NULL, 1,
1370 NULL);
1371 pf_rd_stats[mode_index].rate +=
1372 av1_get_switchable_rate(x, xd, cm->features.interp_filter,
1373 cm->seq_params->enable_dual_filter);
1374 cost = RDCOST(x->rdmult, pf_rd_stats[mode_index].rate,
1375 pf_rd_stats[mode_index].dist);
1376 } else if (motion_mode == WARPED_CAUSAL) {
1377 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
1378 const ModeCosts *mode_costs = &x->mode_costs;
1379 mi->wm_params.wmtype = DEFAULT_WMTYPE;
1380 mi->interp_filters =
1381 av1_broadcast_interp_filter(av1_unswitchable_filter(interp_filter));
1382
1383 memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0));
1384 memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0));
1385 // Select the samples according to motion vector difference
1386 if (mi->num_proj_ref > 1) {
1387 mi->num_proj_ref = av1_selectSamples(&mi->mv[0].as_mv, pts, pts_inref,
1388 mi->num_proj_ref, bsize);
1389 }
1390
1391 // Compute the warped motion parameters with a least squares fit
1392 // using the collected samples
1393 if (!av1_find_projection(mi->num_proj_ref, pts, pts_inref, bsize,
1394 mi->mv[0].as_mv.row, mi->mv[0].as_mv.col,
1395 &mi->wm_params, mi_row, mi_col)) {
1396 if (mi->mode == NEWMV) {
1397 const int_mv mv0 = mi->mv[0];
1398 const WarpedMotionParams wm_params0 = mi->wm_params;
1399 const int num_proj_ref0 = mi->num_proj_ref;
1400
1401 const int_mv ref_mv = av1_get_ref_mv(x, 0);
1402 SUBPEL_MOTION_SEARCH_PARAMS ms_params;
1403 av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize,
1404 &ref_mv.as_mv, NULL);
1405
1406 // Refine MV in a small range.
1407 av1_refine_warped_mv(xd, cm, &ms_params, bsize, pts0, pts_inref0,
1408 total_samples, cpi->sf.mv_sf.warp_search_method,
1409 cpi->sf.mv_sf.warp_search_iters);
1410 if (mi->mv[0].as_int == ref_mv.as_int) {
1411 continue;
1412 }
1413
1414 if (mv0.as_int != mi->mv[0].as_int) {
1415 // Keep the refined MV and WM parameters.
1416 int tmp_rate_mv = av1_mv_bit_cost(
1417 &mi->mv[0].as_mv, &ref_mv.as_mv, x->mv_costs->nmv_joint_cost,
1418 x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
1419 *rate_mv = tmp_rate_mv;
1420 } else {
1421 // Restore the old MV and WM parameters.
1422 mi->mv[0] = mv0;
1423 mi->wm_params = wm_params0;
1424 mi->num_proj_ref = num_proj_ref0;
1425 }
1426 }
1427 // Build the warped predictor
1428 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1429 AOM_PLANE_Y, av1_num_planes(cm) - 1);
1430 if (use_model_yrd_large)
1431 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
1432 &pf_rd_stats[mode_index], this_early_term,
1433 1, best_sse, NULL, UINT_MAX);
1434 else
1435 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[mode_index], NULL,
1436 1, NULL);
1437
1438 pf_rd_stats[mode_index].rate +=
1439 mode_costs->motion_mode_cost[bsize][mi->motion_mode];
1440 cost = RDCOST(x->rdmult, pf_rd_stats[mode_index].rate,
1441 pf_rd_stats[mode_index].dist);
1442 } else {
1443 cost = INT64_MAX;
1444 }
1445 }
1446 if (cost < best_cost) {
1447 best_mode_index = mode_index;
1448 best_cost = cost;
1449 best_skip = pf_rd_stats[mode_index].skip_txfm;
1450 best_early_term = *this_early_term;
1451 best_mbmi = *mi;
1452 }
1453 }
1454 assert(best_mode_index >= 0 && best_mode_index < FILTER_SEARCH_SIZE);
1455
1456 *mi = best_mbmi;
1457 this_rdc->rate = pf_rd_stats[best_mode_index].rate;
1458 this_rdc->dist = pf_rd_stats[best_mode_index].dist;
1459 this_rdc->sse = pf_rd_stats[best_mode_index].sse;
1460 this_rdc->skip_txfm = (best_skip || best_early_term);
1461 *this_early_term = best_early_term;
1462 if (best_mode_index < FILTER_SEARCH_SIZE - 1) {
1463 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1464 AOM_PLANE_Y, AOM_PLANE_Y);
1465 }
1466 }
1467 #endif // !CONFIG_REALTIME_ONLY
1468
1469 #define COLLECT_NON_SQR_STAT 0
1470
1471 #if COLLECT_NONRD_PICK_MODE_STAT
1472
print_stage_time(const char * stage_name,int64_t stage_time,int64_t total_time)1473 static inline void print_stage_time(const char *stage_name, int64_t stage_time,
1474 int64_t total_time) {
1475 printf(" %s: %ld (%f%%)\n", stage_name, stage_time,
1476 100 * stage_time / (float)total_time);
1477 }
1478
print_time(const mode_search_stat_nonrd * const ms_stat,BLOCK_SIZE bsize,int mi_rows,int mi_cols,int mi_row,int mi_col)1479 static void print_time(const mode_search_stat_nonrd *const ms_stat,
1480 BLOCK_SIZE bsize, int mi_rows, int mi_cols, int mi_row,
1481 int mi_col) {
1482 if ((mi_row + mi_size_high[bsize] >= mi_rows) &&
1483 (mi_col + mi_size_wide[bsize] >= mi_cols)) {
1484 int64_t total_time = 0l;
1485 int32_t total_blocks = 0;
1486 for (BLOCK_SIZE bs = 0; bs < BLOCK_SIZES; bs++) {
1487 total_time += ms_stat->total_block_times[bs];
1488 total_blocks += ms_stat->num_blocks[bs];
1489 }
1490
1491 printf("\n");
1492 for (BLOCK_SIZE bs = 0; bs < BLOCK_SIZES; bs++) {
1493 if (ms_stat->num_blocks[bs] == 0) {
1494 continue;
1495 }
1496 if (!COLLECT_NON_SQR_STAT && block_size_wide[bs] != block_size_high[bs]) {
1497 continue;
1498 }
1499
1500 printf("BLOCK_%dX%d Num %d, Time: %ld (%f%%), Avg_time %f:\n",
1501 block_size_wide[bs], block_size_high[bs], ms_stat->num_blocks[bs],
1502 ms_stat->total_block_times[bs],
1503 100 * ms_stat->total_block_times[bs] / (float)total_time,
1504 (float)ms_stat->total_block_times[bs] / ms_stat->num_blocks[bs]);
1505 for (int j = 0; j < MB_MODE_COUNT; j++) {
1506 if (ms_stat->nonskipped_search_times[bs][j] == 0) {
1507 continue;
1508 }
1509
1510 int64_t total_mode_time = ms_stat->nonskipped_search_times[bs][j];
1511 printf(" Mode %d, %d/%d tps %f\n", j,
1512 ms_stat->num_nonskipped_searches[bs][j],
1513 ms_stat->num_searches[bs][j],
1514 ms_stat->num_nonskipped_searches[bs][j] > 0
1515 ? (float)ms_stat->nonskipped_search_times[bs][j] /
1516 ms_stat->num_nonskipped_searches[bs][j]
1517 : 0l);
1518 if (j >= INTER_MODE_START) {
1519 total_mode_time = ms_stat->ms_time[bs][j] + ms_stat->ifs_time[bs][j] +
1520 ms_stat->model_rd_time[bs][j] +
1521 ms_stat->txfm_time[bs][j];
1522 print_stage_time("Motion Search Time", ms_stat->ms_time[bs][j],
1523 total_time);
1524 print_stage_time("Filter Search Time", ms_stat->ifs_time[bs][j],
1525 total_time);
1526 print_stage_time("Model RD Time", ms_stat->model_rd_time[bs][j],
1527 total_time);
1528 print_stage_time("Tranfm Search Time", ms_stat->txfm_time[bs][j],
1529 total_time);
1530 }
1531 print_stage_time("Total Mode Time", total_mode_time, total_time);
1532 }
1533 printf("\n");
1534 }
1535 printf("Total time = %ld. Total blocks = %d\n", total_time, total_blocks);
1536 }
1537 }
1538 #endif // COLLECT_NONRD_PICK_MODE_STAT
1539
should_prune_intra_modes_using_neighbors(const MACROBLOCKD * xd,bool enable_intra_mode_pruning_using_neighbors,PREDICTION_MODE this_mode,PREDICTION_MODE above_mode,PREDICTION_MODE left_mode)1540 static bool should_prune_intra_modes_using_neighbors(
1541 const MACROBLOCKD *xd, bool enable_intra_mode_pruning_using_neighbors,
1542 PREDICTION_MODE this_mode, PREDICTION_MODE above_mode,
1543 PREDICTION_MODE left_mode) {
1544 if (!enable_intra_mode_pruning_using_neighbors) return false;
1545
1546 // Avoid pruning of DC_PRED as it is the most probable mode to win as per the
1547 // statistics generated for nonrd intra mode evaluations.
1548 if (this_mode == DC_PRED) return false;
1549
1550 // Enable the pruning for current mode only if it is not the winner mode of
1551 // both the neighboring blocks (left/top).
1552 return xd->up_available && this_mode != above_mode && xd->left_available &&
1553 this_mode != left_mode;
1554 }
1555
av1_nonrd_pick_intra_mode(AV1_COMP * cpi,MACROBLOCK * x,RD_STATS * rd_cost,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)1556 void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
1557 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1558 AV1_COMMON *const cm = &cpi->common;
1559 MACROBLOCKD *const xd = &x->e_mbd;
1560 MB_MODE_INFO *const mi = xd->mi[0];
1561 RD_STATS this_rdc, best_rdc;
1562 struct estimate_block_intra_args args;
1563 init_estimate_block_intra_args(&args, cpi, x);
1564 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
1565 mi->tx_size =
1566 AOMMIN(max_txsize_lookup[bsize],
1567 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
1568 assert(IMPLIES(xd->lossless[mi->segment_id], mi->tx_size == TX_4X4));
1569 const BLOCK_SIZE tx_bsize = txsize_to_bsize[mi->tx_size];
1570
1571 // If the current block size is the same as the transform block size, enable
1572 // mode pruning based on the best SAD so far.
1573 if (cpi->sf.rt_sf.prune_intra_mode_using_best_sad_so_far && bsize == tx_bsize)
1574 args.prune_mode_based_on_sad = true;
1575
1576 int *bmode_costs;
1577 PREDICTION_MODE best_mode = DC_PRED;
1578 const MB_MODE_INFO *above_mi = xd->above_mbmi;
1579 const MB_MODE_INFO *left_mi = xd->left_mbmi;
1580 const PREDICTION_MODE A = av1_above_block_mode(above_mi);
1581 const PREDICTION_MODE L = av1_left_block_mode(left_mi);
1582 const int above_ctx = intra_mode_context[A];
1583 const int left_ctx = intra_mode_context[L];
1584 const unsigned int source_variance = x->source_variance;
1585 bmode_costs = x->mode_costs.y_mode_costs[above_ctx][left_ctx];
1586
1587 av1_invalid_rd_stats(&best_rdc);
1588 av1_invalid_rd_stats(&this_rdc);
1589
1590 init_mbmi_nonrd(mi, DC_PRED, INTRA_FRAME, NONE_FRAME, cm);
1591 mi->mv[0].as_int = mi->mv[1].as_int = INVALID_MV;
1592
1593 // Change the limit of this loop to add other intra prediction
1594 // mode tests.
1595 for (int mode_index = 0; mode_index < RTC_INTRA_MODES; ++mode_index) {
1596 PREDICTION_MODE this_mode = intra_mode_list[mode_index];
1597
1598 // As per the statistics generated for intra mode evaluation in the nonrd
1599 // path, it is found that the probability of H_PRED mode being the winner is
1600 // very low when the best mode so far is V_PRED (out of DC_PRED and V_PRED).
1601 // If V_PRED is the winner mode out of DC_PRED and V_PRED, it could imply
1602 // the presence of a vertically dominant pattern. Hence, H_PRED mode is not
1603 // evaluated.
1604 if (cpi->sf.rt_sf.prune_h_pred_using_best_mode_so_far &&
1605 this_mode == H_PRED && best_mode == V_PRED)
1606 continue;
1607
1608 if (should_prune_intra_modes_using_neighbors(
1609 xd, cpi->sf.rt_sf.enable_intra_mode_pruning_using_neighbors,
1610 this_mode, A, L)) {
1611 // Prune V_PRED and H_PRED if source variance of the block is less than
1612 // or equal to 50. The source variance threshold is obtained empirically.
1613 if ((this_mode == V_PRED || this_mode == H_PRED) && source_variance <= 50)
1614 continue;
1615
1616 // As per the statistics, probability of SMOOTH_PRED being the winner is
1617 // low when best mode so far is DC_PRED (out of DC_PRED, V_PRED and
1618 // H_PRED). Hence, SMOOTH_PRED mode is not evaluated.
1619 if (best_mode == DC_PRED && this_mode == SMOOTH_PRED) continue;
1620 }
1621
1622 this_rdc.dist = this_rdc.rate = 0;
1623 args.mode = this_mode;
1624 args.skippable = 1;
1625 args.rdc = &this_rdc;
1626 mi->mode = this_mode;
1627 av1_foreach_transformed_block_in_plane(xd, bsize, AOM_PLANE_Y,
1628 av1_estimate_block_intra, &args);
1629
1630 if (this_rdc.rate == INT_MAX) continue;
1631
1632 const int skip_ctx = av1_get_skip_txfm_context(xd);
1633 if (args.skippable) {
1634 this_rdc.rate = x->mode_costs.skip_txfm_cost[skip_ctx][1];
1635 } else {
1636 this_rdc.rate += x->mode_costs.skip_txfm_cost[skip_ctx][0];
1637 }
1638 this_rdc.rate += bmode_costs[this_mode];
1639 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1640
1641 if (this_rdc.rdcost < best_rdc.rdcost) {
1642 best_rdc = this_rdc;
1643 best_mode = this_mode;
1644 if (!this_rdc.skip_txfm) {
1645 memset(ctx->blk_skip, 0,
1646 sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
1647 }
1648 }
1649 }
1650
1651 const unsigned int thresh_sad =
1652 cpi->sf.rt_sf.prune_palette_search_nonrd > 1 ? 100 : 20;
1653 const unsigned int best_sad_norm =
1654 args.best_sad >>
1655 (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
1656
1657 // Try palette if it's enabled.
1658 bool try_palette =
1659 cpi->oxcf.tool_cfg.enable_palette &&
1660 av1_allow_palette(cpi->common.features.allow_screen_content_tools,
1661 mi->bsize);
1662 if (cpi->sf.rt_sf.prune_palette_search_nonrd > 0) {
1663 bool prune =
1664 (!args.prune_mode_based_on_sad || best_sad_norm > thresh_sad) &&
1665 bsize <= BLOCK_16X16 && x->source_variance > 200;
1666 try_palette &= prune;
1667 }
1668 if (try_palette) {
1669 const TxfmSearchInfo *txfm_info = &x->txfm_search_info;
1670 const unsigned int intra_ref_frame_cost = 0;
1671 x->color_palette_thresh = (best_sad_norm < 500) ? 32 : 64;
1672
1673 // Search palette mode for Luma plane in intra frame.
1674 av1_search_palette_mode_luma(cpi, x, bsize, intra_ref_frame_cost, ctx,
1675 &this_rdc, best_rdc.rdcost);
1676 // Update best mode data.
1677 if (this_rdc.rdcost < best_rdc.rdcost) {
1678 best_mode = DC_PRED;
1679 mi->mv[0].as_int = INVALID_MV;
1680 mi->mv[1].as_int = INVALID_MV;
1681 best_rdc.rate = this_rdc.rate;
1682 best_rdc.dist = this_rdc.dist;
1683 best_rdc.rdcost = this_rdc.rdcost;
1684 if (!this_rdc.skip_txfm) {
1685 memcpy(ctx->blk_skip, txfm_info->blk_skip,
1686 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
1687 }
1688 if (xd->tx_type_map[0] != DCT_DCT)
1689 av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
1690 } else {
1691 av1_zero(mi->palette_mode_info);
1692 }
1693 }
1694
1695 mi->mode = best_mode;
1696 // Keep DC for UV since mode test is based on Y channel only.
1697 mi->uv_mode = UV_DC_PRED;
1698 *rd_cost = best_rdc;
1699
1700 // For lossless: always force the skip flags off.
1701 // Even though the blk_skip is set to 0 above in the rdcost comparison,
1702 // do it here again in case the above logic changes.
1703 if (is_lossless_requested(&cpi->oxcf.rc_cfg)) {
1704 x->txfm_search_info.skip_txfm = 0;
1705 memset(ctx->blk_skip, 0,
1706 sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
1707 }
1708
1709 #if CONFIG_INTERNAL_STATS
1710 store_coding_context_nonrd(x, ctx, mi->mode);
1711 #else
1712 store_coding_context_nonrd(x, ctx);
1713 #endif // CONFIG_INTERNAL_STATS
1714 }
1715
is_same_gf_and_last_scale(AV1_COMMON * cm)1716 static inline int is_same_gf_and_last_scale(AV1_COMMON *cm) {
1717 struct scale_factors *const sf_last = get_ref_scale_factors(cm, LAST_FRAME);
1718 struct scale_factors *const sf_golden =
1719 get_ref_scale_factors(cm, GOLDEN_FRAME);
1720 return ((sf_last->x_scale_fp == sf_golden->x_scale_fp) &&
1721 (sf_last->y_scale_fp == sf_golden->y_scale_fp));
1722 }
1723
get_ref_frame_use_mask(AV1_COMP * cpi,MACROBLOCK * x,MB_MODE_INFO * mi,int mi_row,int mi_col,BLOCK_SIZE bsize,int gf_temporal_ref,int use_ref_frame[],int * force_skip_low_temp_var)1724 static inline void get_ref_frame_use_mask(AV1_COMP *cpi, MACROBLOCK *x,
1725 MB_MODE_INFO *mi, int mi_row,
1726 int mi_col, BLOCK_SIZE bsize,
1727 int gf_temporal_ref,
1728 int use_ref_frame[],
1729 int *force_skip_low_temp_var) {
1730 AV1_COMMON *const cm = &cpi->common;
1731 const struct segmentation *const seg = &cm->seg;
1732 const int is_small_sb = (cm->seq_params->sb_size == BLOCK_64X64);
1733
1734 // When the ref_frame_config is used to set the reference frame structure
1735 // then the usage of alt_ref is determined by the ref_frame_flags
1736 // (and not the speed feature use_nonrd_altref_frame).
1737 int use_alt_ref_frame = cpi->ppi->rtc_ref.set_ref_frame_config ||
1738 cpi->sf.rt_sf.use_nonrd_altref_frame;
1739
1740 int use_golden_ref_frame = 1;
1741 int use_last_ref_frame = 1;
1742
1743 // When the ref_frame_config is used to set the reference frame structure:
1744 // check if LAST is used as a reference. And only remove golden and altref
1745 // references below if last is used as a reference.
1746 if (cpi->ppi->rtc_ref.set_ref_frame_config)
1747 use_last_ref_frame =
1748 cpi->ref_frame_flags & AOM_LAST_FLAG ? use_last_ref_frame : 0;
1749
1750 // frame_since_golden is not used when user sets the referene structure.
1751 if (!cpi->ppi->rtc_ref.set_ref_frame_config && use_last_ref_frame &&
1752 cpi->rc.frames_since_golden == 0 && gf_temporal_ref) {
1753 use_golden_ref_frame = 0;
1754 }
1755
1756 if (use_last_ref_frame && cpi->sf.rt_sf.short_circuit_low_temp_var &&
1757 x->nonrd_prune_ref_frame_search) {
1758 if (is_small_sb)
1759 *force_skip_low_temp_var = av1_get_force_skip_low_temp_var_small_sb(
1760 &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
1761 else
1762 *force_skip_low_temp_var = av1_get_force_skip_low_temp_var(
1763 &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
1764 // If force_skip_low_temp_var is set, skip golden reference.
1765 if (*force_skip_low_temp_var) {
1766 use_golden_ref_frame = 0;
1767 use_alt_ref_frame = 0;
1768 }
1769 }
1770
1771 if (use_last_ref_frame &&
1772 (x->nonrd_prune_ref_frame_search > 2 || x->force_zeromv_skip_for_blk ||
1773 (x->nonrd_prune_ref_frame_search > 1 && bsize > BLOCK_64X64))) {
1774 use_golden_ref_frame = 0;
1775 use_alt_ref_frame = 0;
1776 }
1777
1778 if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
1779 get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) {
1780 use_golden_ref_frame = 1;
1781 use_alt_ref_frame = 0;
1782 }
1783
1784 // Skip golden/altref reference if color is set, on flat blocks with motion.
1785 // For screen: always skip golden/alt (if color_sensitivity_sb_g/alt is set)
1786 // except when x->nonrd_prune_ref_frame_search = 0. This latter flag
1787 // may be set in the variance partition when golden is a much better
1788 // reference than last, in which case it may not be worth skipping
1789 // golden/altref completely.
1790 // Condition on use_last_ref to make sure there remains at least one
1791 // reference.
1792 if (use_last_ref_frame &&
1793 ((cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
1794 x->nonrd_prune_ref_frame_search != 0) ||
1795 (x->source_variance < 200 &&
1796 x->content_state_sb.source_sad_nonrd >= kLowSad))) {
1797 if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
1798 x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
1799 use_golden_ref_frame = 0;
1800 if (x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
1801 x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
1802 use_alt_ref_frame = 0;
1803 }
1804
1805 // For non-screen: if golden and altref are not being selected as references
1806 // (use_golden_ref_frame/use_alt_ref_frame = 0) check to allow golden back
1807 // based on the sad of nearest/nearmv of LAST ref. If this block sad is large,
1808 // keep golden as reference. Only do this for the agrressive pruning mode and
1809 // avoid it when color is set for golden reference.
1810 if (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN &&
1811 (cpi->ref_frame_flags & AOM_LAST_FLAG) && !use_golden_ref_frame &&
1812 !use_alt_ref_frame && x->pred_mv_sad[LAST_FRAME] != INT_MAX &&
1813 x->nonrd_prune_ref_frame_search > 2 &&
1814 x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
1815 x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) {
1816 int thr = (cm->width * cm->height > RESOLUTION_288P) ? 100 : 150;
1817 int pred = x->pred_mv_sad[LAST_FRAME] >>
1818 (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
1819 if (pred > thr) use_golden_ref_frame = 1;
1820 }
1821
1822 use_alt_ref_frame =
1823 cpi->ref_frame_flags & AOM_ALT_FLAG ? use_alt_ref_frame : 0;
1824 use_golden_ref_frame =
1825 cpi->ref_frame_flags & AOM_GOLD_FLAG ? use_golden_ref_frame : 0;
1826
1827 // For spatial layers: enable golden ref if it is set by user and
1828 // corresponds to the lower spatial layer.
1829 if (cpi->svc.spatial_layer_id > 0 && (cpi->ref_frame_flags & AOM_GOLD_FLAG) &&
1830 x->content_state_sb.source_sad_nonrd < kHighSad) {
1831 const int buffslot_golden =
1832 cpi->ppi->rtc_ref.ref_idx[GOLDEN_FRAME - LAST_FRAME];
1833 if (cpi->ppi->rtc_ref.buffer_time_index[buffslot_golden] ==
1834 cpi->svc.current_superframe)
1835 use_golden_ref_frame = 1;
1836 }
1837
1838 use_ref_frame[ALTREF_FRAME] = use_alt_ref_frame;
1839 use_ref_frame[GOLDEN_FRAME] = use_golden_ref_frame;
1840 use_ref_frame[LAST_FRAME] = use_last_ref_frame;
1841 // Keep this assert on, as only 3 references are used in nonrd_pickmode
1842 // (LAST, GOLDEN, ALTREF), and if all 3 are not set by user then this
1843 // frame must be an intra-only frame and hence should never enter the
1844 // pickmode here for inter frames.
1845 assert(use_last_ref_frame || use_golden_ref_frame || use_alt_ref_frame);
1846 }
1847
is_filter_search_enabled_blk(AV1_COMP * cpi,MACROBLOCK * x,int mi_row,int mi_col,BLOCK_SIZE bsize,int segment_id,int cb_pred_filter_search,InterpFilter * filt_select)1848 static inline int is_filter_search_enabled_blk(AV1_COMP *cpi, MACROBLOCK *x,
1849 int mi_row, int mi_col,
1850 BLOCK_SIZE bsize, int segment_id,
1851 int cb_pred_filter_search,
1852 InterpFilter *filt_select) {
1853 const AV1_COMMON *const cm = &cpi->common;
1854 // filt search disabled
1855 if (!cpi->sf.rt_sf.use_nonrd_filter_search) return 0;
1856 // filt search purely based on mode properties
1857 if (!cb_pred_filter_search) return 1;
1858 MACROBLOCKD *const xd = &x->e_mbd;
1859 int enable_interp_search = 0;
1860 if (!(xd->left_mbmi && xd->above_mbmi)) {
1861 // neighbors info unavailable
1862 enable_interp_search = 2;
1863 } else if (!(is_inter_block(xd->left_mbmi) &&
1864 is_inter_block(xd->above_mbmi))) {
1865 // neighbor is INTRA
1866 enable_interp_search = 2;
1867 } else if (xd->left_mbmi->interp_filters.as_int !=
1868 xd->above_mbmi->interp_filters.as_int) {
1869 // filters are different
1870 enable_interp_search = 2;
1871 } else if ((cb_pred_filter_search == 1) &&
1872 (xd->left_mbmi->interp_filters.as_filters.x_filter !=
1873 EIGHTTAP_REGULAR)) {
1874 // not regular
1875 enable_interp_search = 2;
1876 } else {
1877 // enable prediction based on chessboard pattern
1878 if (xd->left_mbmi->interp_filters.as_filters.x_filter == EIGHTTAP_SMOOTH)
1879 *filt_select = EIGHTTAP_SMOOTH;
1880 const int bsl = mi_size_wide_log2[bsize];
1881 enable_interp_search =
1882 (bool)((((mi_row + mi_col) >> bsl) +
1883 get_chessboard_index(cm->current_frame.frame_number)) &
1884 0x1);
1885 if (cyclic_refresh_segment_id_boosted(segment_id)) enable_interp_search = 1;
1886 }
1887 return enable_interp_search;
1888 }
1889
skip_mode_by_threshold(PREDICTION_MODE mode,MV_REFERENCE_FRAME ref_frame,int_mv mv,int frames_since_golden,const int * const rd_threshes,const int * const rd_thresh_freq_fact,int64_t best_cost,int best_skip,int extra_shift)1890 static inline int skip_mode_by_threshold(PREDICTION_MODE mode,
1891 MV_REFERENCE_FRAME ref_frame,
1892 int_mv mv, int frames_since_golden,
1893 const int *const rd_threshes,
1894 const int *const rd_thresh_freq_fact,
1895 int64_t best_cost, int best_skip,
1896 int extra_shift) {
1897 int skip_this_mode = 0;
1898 const THR_MODES mode_index = mode_idx[ref_frame][INTER_OFFSET(mode)];
1899 int64_t mode_rd_thresh =
1900 best_skip ? ((int64_t)rd_threshes[mode_index]) << (extra_shift + 1)
1901 : ((int64_t)rd_threshes[mode_index]) << extra_shift;
1902
1903 // Increase mode_rd_thresh value for non-LAST for improved encoding
1904 // speed
1905 if (ref_frame != LAST_FRAME) {
1906 mode_rd_thresh = mode_rd_thresh << 1;
1907 if (ref_frame == GOLDEN_FRAME && frames_since_golden > 4)
1908 mode_rd_thresh = mode_rd_thresh << (extra_shift + 1);
1909 }
1910
1911 if (rd_less_than_thresh(best_cost, mode_rd_thresh,
1912 rd_thresh_freq_fact[mode_index]))
1913 if (mv.as_int != 0) skip_this_mode = 1;
1914
1915 return skip_this_mode;
1916 }
1917
skip_mode_by_low_temp(PREDICTION_MODE mode,MV_REFERENCE_FRAME ref_frame,BLOCK_SIZE bsize,CONTENT_STATE_SB content_state_sb,int_mv mv,int force_skip_low_temp_var)1918 static inline int skip_mode_by_low_temp(
1919 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
1920 CONTENT_STATE_SB content_state_sb, int_mv mv, int force_skip_low_temp_var) {
1921 // Skip non-zeromv mode search for non-LAST frame if force_skip_low_temp_var
1922 // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped
1923 // later.
1924 if (force_skip_low_temp_var && ref_frame != LAST_FRAME && mv.as_int != 0) {
1925 return 1;
1926 }
1927
1928 if (content_state_sb.source_sad_nonrd != kHighSad && bsize >= BLOCK_64X64 &&
1929 force_skip_low_temp_var && mode == NEWMV) {
1930 return 1;
1931 }
1932 return 0;
1933 }
1934
skip_mode_by_bsize_and_ref_frame(PREDICTION_MODE mode,MV_REFERENCE_FRAME ref_frame,BLOCK_SIZE bsize,int extra_prune,unsigned int sse_zeromv_norm,int more_prune,int skip_nearmv)1935 static inline int skip_mode_by_bsize_and_ref_frame(
1936 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
1937 int extra_prune, unsigned int sse_zeromv_norm, int more_prune,
1938 int skip_nearmv) {
1939 const unsigned int thresh_skip_golden = 500;
1940
1941 if (ref_frame != LAST_FRAME && sse_zeromv_norm < thresh_skip_golden &&
1942 mode == NEWMV)
1943 return 1;
1944
1945 if ((bsize == BLOCK_128X128 && mode == NEWMV) ||
1946 (skip_nearmv && mode == NEARMV))
1947 return 1;
1948
1949 // Skip testing non-LAST if this flag is set.
1950 if (extra_prune) {
1951 if (extra_prune > 1 && ref_frame != LAST_FRAME &&
1952 (bsize > BLOCK_16X16 && mode == NEWMV))
1953 return 1;
1954
1955 if (ref_frame != LAST_FRAME && mode == NEARMV) return 1;
1956
1957 if (more_prune && bsize >= BLOCK_32X32 && mode == NEARMV) return 1;
1958 }
1959 return 0;
1960 }
1961
set_block_source_sad(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,struct buf_2d * yv12_mb)1962 static void set_block_source_sad(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
1963 struct buf_2d *yv12_mb) {
1964 struct macroblock_plane *const p = &x->plane[0];
1965 const int y_sad = cpi->ppi->fn_ptr[bsize].sdf(p->src.buf, p->src.stride,
1966 yv12_mb->buf, yv12_mb->stride);
1967 if (y_sad == 0) x->block_is_zero_sad = 1;
1968 }
1969
set_color_sensitivity(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int y_sad,unsigned int source_variance,struct buf_2d yv12_mb[MAX_MB_PLANE])1970 static void set_color_sensitivity(AV1_COMP *cpi, MACROBLOCK *x,
1971 BLOCK_SIZE bsize, int y_sad,
1972 unsigned int source_variance,
1973 struct buf_2d yv12_mb[MAX_MB_PLANE]) {
1974 const int subsampling_x = cpi->common.seq_params->subsampling_x;
1975 const int subsampling_y = cpi->common.seq_params->subsampling_y;
1976 const int source_sad_nonrd = x->content_state_sb.source_sad_nonrd;
1977 const int high_res = cpi->common.width * cpi->common.height >= 640 * 360;
1978 if (bsize == cpi->common.seq_params->sb_size) {
1979 // At superblock level color_sensitivity is already set to 0, 1, or 2.
1980 // 2 is middle/uncertain level. To avoid additional sad
1981 // computations when bsize = sb_size force level 2 to 1 (certain color)
1982 // for motion areas.
1983 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 2) {
1984 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] =
1985 source_sad_nonrd >= kMedSad ? 1 : 0;
1986 }
1987 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 2) {
1988 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] =
1989 source_sad_nonrd >= kMedSad ? 1 : 0;
1990 }
1991 return;
1992 }
1993 // Divide factor for comparing uv_sad to y_sad.
1994 int shift = 3;
1995 // Threshold for the block spatial source variance.
1996 unsigned int source_var_thr = 50;
1997 // Thresholds for normalized uv_sad, the first one is used for
1998 // low source_varaince.
1999 int norm_uv_sad_thresh = 100;
2000 int norm_uv_sad_thresh2 = 40;
2001 if (source_sad_nonrd >= kMedSad && x->source_variance > 0 && high_res)
2002 shift = 4;
2003 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
2004 if (cpi->rc.high_source_sad) shift = 6;
2005 if (source_sad_nonrd > kMedSad) {
2006 source_var_thr = 1200;
2007 norm_uv_sad_thresh = 10;
2008 }
2009 if (cpi->rc.percent_blocks_with_motion > 90 &&
2010 cpi->rc.frame_source_sad > 10000 && source_sad_nonrd > kLowSad) {
2011 // Aggressive setting for color_sensitiivty for this content.
2012 shift = 10;
2013 norm_uv_sad_thresh = 0;
2014 norm_uv_sad_thresh2 = 0;
2015 }
2016 }
2017 NOISE_LEVEL noise_level = kLow;
2018 int norm_sad =
2019 y_sad >> (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
2020 unsigned int thresh_spatial = (cpi->common.width > 1920) ? 5000 : 1000;
2021 // If the spatial source variance is high and the normalized y_sad
2022 // is low, then y-channel is likely good for mode estimation, so keep
2023 // color_sensitivity off. For low noise content for now, since there is
2024 // some bdrate regression for noisy color clip.
2025 if (cpi->noise_estimate.enabled)
2026 noise_level = av1_noise_estimate_extract_level(&cpi->noise_estimate);
2027 if (noise_level == kLow && source_variance > thresh_spatial &&
2028 cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN && norm_sad < 50) {
2029 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] = 0;
2030 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] = 0;
2031 return;
2032 }
2033 const int num_planes = av1_num_planes(&cpi->common);
2034
2035 for (int plane = AOM_PLANE_U; plane < num_planes; ++plane) {
2036 // Always check if level = 2. If level = 0 check again for
2037 // motion areas for higher resolns, where color artifacts
2038 // are more noticeable.
2039 if (x->color_sensitivity[COLOR_SENS_IDX(plane)] == 2 ||
2040 (x->color_sensitivity[COLOR_SENS_IDX(plane)] == 0 &&
2041 source_sad_nonrd >= kMedSad && high_res)) {
2042 struct macroblock_plane *const p = &x->plane[plane];
2043 const BLOCK_SIZE bs =
2044 get_plane_block_size(bsize, subsampling_x, subsampling_y);
2045
2046 const int uv_sad = cpi->ppi->fn_ptr[bs].sdf(
2047 p->src.buf, p->src.stride, yv12_mb[plane].buf, yv12_mb[plane].stride);
2048
2049 const int norm_uv_sad =
2050 uv_sad >> (b_width_log2_lookup[bs] + b_height_log2_lookup[bs]);
2051 x->color_sensitivity[COLOR_SENS_IDX(plane)] =
2052 uv_sad > (y_sad >> shift) && norm_uv_sad > norm_uv_sad_thresh2;
2053 if (source_variance < source_var_thr && norm_uv_sad > norm_uv_sad_thresh)
2054 x->color_sensitivity[COLOR_SENS_IDX(plane)] = 1;
2055 }
2056 }
2057 }
2058
setup_compound_prediction(const AV1_COMMON * cm,MACROBLOCK * x,struct buf_2d yv12_mb[8][MAX_MB_PLANE],const int * use_ref_frame_mask,const MV_REFERENCE_FRAME * rf,int * ref_mv_idx)2059 static void setup_compound_prediction(const AV1_COMMON *cm, MACROBLOCK *x,
2060 struct buf_2d yv12_mb[8][MAX_MB_PLANE],
2061 const int *use_ref_frame_mask,
2062 const MV_REFERENCE_FRAME *rf,
2063 int *ref_mv_idx) {
2064 MACROBLOCKD *const xd = &x->e_mbd;
2065 MB_MODE_INFO *const mbmi = xd->mi[0];
2066 MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
2067 MV_REFERENCE_FRAME ref_frame_comp;
2068 if (!use_ref_frame_mask[rf[1]]) {
2069 // Need to setup pred_block, if it hasn't been done in find_predictors.
2070 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, rf[1]);
2071 const int num_planes = av1_num_planes(cm);
2072 if (yv12 != NULL) {
2073 const struct scale_factors *const sf =
2074 get_ref_scale_factors_const(cm, rf[1]);
2075 av1_setup_pred_block(xd, yv12_mb[rf[1]], yv12, sf, sf, num_planes);
2076 }
2077 }
2078 ref_frame_comp = av1_ref_frame_type(rf);
2079 mbmi_ext->mode_context[ref_frame_comp] = 0;
2080 mbmi_ext->ref_mv_count[ref_frame_comp] = UINT8_MAX;
2081 av1_find_mv_refs(cm, xd, mbmi, ref_frame_comp, mbmi_ext->ref_mv_count,
2082 xd->ref_mv_stack, xd->weight, NULL, mbmi_ext->global_mvs,
2083 mbmi_ext->mode_context);
2084 av1_copy_usable_ref_mv_stack_and_weight(xd, mbmi_ext, ref_frame_comp);
2085 *ref_mv_idx = mbmi->ref_mv_idx + 1;
2086 }
2087
set_compound_mode(MACROBLOCK * x,MV_REFERENCE_FRAME ref_frame,MV_REFERENCE_FRAME ref_frame2,int ref_mv_idx,int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],PREDICTION_MODE this_mode)2088 static void set_compound_mode(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame,
2089 MV_REFERENCE_FRAME ref_frame2, int ref_mv_idx,
2090 int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],
2091 PREDICTION_MODE this_mode) {
2092 MACROBLOCKD *const xd = &x->e_mbd;
2093 MB_MODE_INFO *const mi = xd->mi[0];
2094 mi->ref_frame[0] = ref_frame;
2095 mi->ref_frame[1] = ref_frame2;
2096 mi->compound_idx = 1;
2097 mi->comp_group_idx = 0;
2098 mi->interinter_comp.type = COMPOUND_AVERAGE;
2099 MV_REFERENCE_FRAME ref_frame_comp = av1_ref_frame_type(mi->ref_frame);
2100 if (this_mode == GLOBAL_GLOBALMV) {
2101 frame_mv[this_mode][ref_frame].as_int = 0;
2102 frame_mv[this_mode][ref_frame2].as_int = 0;
2103 } else if (this_mode == NEAREST_NEARESTMV) {
2104 frame_mv[this_mode][ref_frame].as_int =
2105 xd->ref_mv_stack[ref_frame_comp][0].this_mv.as_int;
2106 frame_mv[this_mode][ref_frame2].as_int =
2107 xd->ref_mv_stack[ref_frame_comp][0].comp_mv.as_int;
2108 } else if (this_mode == NEAR_NEARMV) {
2109 frame_mv[this_mode][ref_frame].as_int =
2110 xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].this_mv.as_int;
2111 frame_mv[this_mode][ref_frame2].as_int =
2112 xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].comp_mv.as_int;
2113 }
2114 }
2115
2116 // Prune compound mode if the single mode variance is lower than a fixed
2117 // percentage of the median value.
skip_comp_based_on_var(const unsigned int (* single_vars)[REF_FRAMES],BLOCK_SIZE bsize)2118 static bool skip_comp_based_on_var(
2119 const unsigned int (*single_vars)[REF_FRAMES], BLOCK_SIZE bsize) {
2120 unsigned int best_var = UINT_MAX;
2121 for (int cur_mode_idx = 0; cur_mode_idx < RTC_INTER_MODES; cur_mode_idx++) {
2122 for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
2123 best_var = AOMMIN(best_var, single_vars[cur_mode_idx][ref_idx]);
2124 }
2125 }
2126 const unsigned int thresh_64 = (unsigned int)(0.57356805f * 8659);
2127 const unsigned int thresh_32 = (unsigned int)(0.23964763f * 4281);
2128
2129 // Currently, the thresh for 128 and 16 are not well-tuned. We are using the
2130 // results from 64 and 32 as an heuristic.
2131 switch (bsize) {
2132 case BLOCK_128X128: return best_var < 4 * thresh_64;
2133 case BLOCK_64X64: return best_var < thresh_64;
2134 case BLOCK_32X32: return best_var < thresh_32;
2135 case BLOCK_16X16: return best_var < thresh_32 / 4;
2136 default: return false;
2137 }
2138 }
2139
fill_single_inter_mode_costs(int (* single_inter_mode_costs)[REF_FRAMES],int num_inter_modes,const REF_MODE * reference_mode_set,const ModeCosts * mode_costs,const int16_t * mode_context)2140 static AOM_FORCE_INLINE void fill_single_inter_mode_costs(
2141 int (*single_inter_mode_costs)[REF_FRAMES], int num_inter_modes,
2142 const REF_MODE *reference_mode_set, const ModeCosts *mode_costs,
2143 const int16_t *mode_context) {
2144 bool ref_frame_used[REF_FRAMES] = { false };
2145 for (int idx = 0; idx < num_inter_modes; idx++) {
2146 ref_frame_used[reference_mode_set[idx].ref_frame] = true;
2147 }
2148
2149 for (int this_ref_frame = LAST_FRAME; this_ref_frame < REF_FRAMES;
2150 this_ref_frame++) {
2151 if (!ref_frame_used[this_ref_frame]) {
2152 continue;
2153 }
2154
2155 const MV_REFERENCE_FRAME rf[2] = { this_ref_frame, NONE_FRAME };
2156 const int16_t mode_ctx = av1_mode_context_analyzer(mode_context, rf);
2157 for (PREDICTION_MODE this_mode = NEARESTMV; this_mode <= NEWMV;
2158 this_mode++) {
2159 single_inter_mode_costs[INTER_OFFSET(this_mode)][this_ref_frame] =
2160 cost_mv_ref(mode_costs, this_mode, mode_ctx);
2161 }
2162 }
2163 }
2164
is_globalmv_better(PREDICTION_MODE this_mode,MV_REFERENCE_FRAME ref_frame,int rate_mv,const ModeCosts * mode_costs,const int (* single_inter_mode_costs)[REF_FRAMES],const MB_MODE_INFO_EXT * mbmi_ext)2165 static inline bool is_globalmv_better(
2166 PREDICTION_MODE this_mode, MV_REFERENCE_FRAME ref_frame, int rate_mv,
2167 const ModeCosts *mode_costs,
2168 const int (*single_inter_mode_costs)[REF_FRAMES],
2169 const MB_MODE_INFO_EXT *mbmi_ext) {
2170 const int globalmv_mode_cost =
2171 single_inter_mode_costs[INTER_OFFSET(GLOBALMV)][ref_frame];
2172 int this_mode_cost =
2173 rate_mv + single_inter_mode_costs[INTER_OFFSET(this_mode)][ref_frame];
2174 if (this_mode == NEWMV || this_mode == NEARMV) {
2175 const MV_REFERENCE_FRAME rf[2] = { ref_frame, NONE_FRAME };
2176 this_mode_cost += get_drl_cost(
2177 NEWMV, 0, mbmi_ext, mode_costs->drl_mode_cost0, av1_ref_frame_type(rf));
2178 }
2179 return this_mode_cost > globalmv_mode_cost;
2180 }
2181
2182 // Set up the mv/ref_frames etc based on the comp_index. Returns 1 if it
2183 // succeeds, 0 if it fails.
setup_compound_params_from_comp_idx(const AV1_COMP * cpi,MACROBLOCK * x,struct buf_2d yv12_mb[8][MAX_MB_PLANE],PREDICTION_MODE * this_mode,MV_REFERENCE_FRAME * ref_frame,MV_REFERENCE_FRAME * ref_frame2,int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],const int * use_ref_frame_mask,int comp_index,bool comp_use_zero_zeromv_only,MV_REFERENCE_FRAME * last_comp_ref_frame,BLOCK_SIZE bsize)2184 static inline int setup_compound_params_from_comp_idx(
2185 const AV1_COMP *cpi, MACROBLOCK *x, struct buf_2d yv12_mb[8][MAX_MB_PLANE],
2186 PREDICTION_MODE *this_mode, MV_REFERENCE_FRAME *ref_frame,
2187 MV_REFERENCE_FRAME *ref_frame2, int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],
2188 const int *use_ref_frame_mask, int comp_index,
2189 bool comp_use_zero_zeromv_only, MV_REFERENCE_FRAME *last_comp_ref_frame,
2190 BLOCK_SIZE bsize) {
2191 const MV_REFERENCE_FRAME *rf = comp_ref_mode_set[comp_index].ref_frame;
2192 int skip_gf = 0;
2193 int skip_alt = 0;
2194 *this_mode = comp_ref_mode_set[comp_index].pred_mode;
2195 *ref_frame = rf[0];
2196 *ref_frame2 = rf[1];
2197 assert(*ref_frame == LAST_FRAME);
2198 assert(*this_mode == GLOBAL_GLOBALMV || *this_mode == NEAREST_NEARESTMV);
2199 if (x->source_variance < 50 && bsize > BLOCK_16X16) {
2200 if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2201 x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
2202 skip_gf = 1;
2203 if (x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2204 x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
2205 skip_alt = 1;
2206 }
2207 if (comp_use_zero_zeromv_only && *this_mode != GLOBAL_GLOBALMV) {
2208 return 0;
2209 }
2210 if (*ref_frame2 == GOLDEN_FRAME &&
2211 (cpi->sf.rt_sf.ref_frame_comp_nonrd[0] == 0 || skip_gf ||
2212 !(cpi->ref_frame_flags & AOM_GOLD_FLAG))) {
2213 return 0;
2214 } else if (*ref_frame2 == LAST2_FRAME &&
2215 (cpi->sf.rt_sf.ref_frame_comp_nonrd[1] == 0 ||
2216 !(cpi->ref_frame_flags & AOM_LAST2_FLAG))) {
2217 return 0;
2218 } else if (*ref_frame2 == ALTREF_FRAME &&
2219 (cpi->sf.rt_sf.ref_frame_comp_nonrd[2] == 0 || skip_alt ||
2220 !(cpi->ref_frame_flags & AOM_ALT_FLAG))) {
2221 return 0;
2222 }
2223 int ref_mv_idx = 0;
2224 if (*last_comp_ref_frame != rf[1]) {
2225 // Only needs to be done once per reference pair.
2226 setup_compound_prediction(&cpi->common, x, yv12_mb, use_ref_frame_mask, rf,
2227 &ref_mv_idx);
2228 *last_comp_ref_frame = rf[1];
2229 }
2230 set_compound_mode(x, *ref_frame, *ref_frame2, ref_mv_idx, frame_mv,
2231 *this_mode);
2232 if (*this_mode != GLOBAL_GLOBALMV &&
2233 frame_mv[*this_mode][*ref_frame].as_int == 0 &&
2234 frame_mv[*this_mode][*ref_frame2].as_int == 0) {
2235 return 0;
2236 }
2237
2238 return 1;
2239 }
2240
previous_mode_performed_poorly(PREDICTION_MODE mode,MV_REFERENCE_FRAME ref_frame,const unsigned int (* vars)[REF_FRAMES],const int64_t (* uv_dist)[REF_FRAMES])2241 static inline bool previous_mode_performed_poorly(
2242 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame,
2243 const unsigned int (*vars)[REF_FRAMES],
2244 const int64_t (*uv_dist)[REF_FRAMES]) {
2245 unsigned int best_var = UINT_MAX;
2246 int64_t best_uv_dist = INT64_MAX;
2247 for (int midx = 0; midx < RTC_INTER_MODES; midx++) {
2248 best_var = AOMMIN(best_var, vars[midx][ref_frame]);
2249 best_uv_dist = AOMMIN(best_uv_dist, uv_dist[midx][ref_frame]);
2250 }
2251 assert(best_var != UINT_MAX && "Invalid variance data.");
2252 const float mult = 1.125f;
2253 bool var_bad = mult * best_var < vars[INTER_OFFSET(mode)][ref_frame];
2254 if (uv_dist[INTER_OFFSET(mode)][ref_frame] < INT64_MAX &&
2255 best_uv_dist != uv_dist[INTER_OFFSET(mode)][ref_frame]) {
2256 // If we have chroma info, then take it into account
2257 var_bad &= mult * best_uv_dist < uv_dist[INTER_OFFSET(mode)][ref_frame];
2258 }
2259 return var_bad;
2260 }
2261
prune_compoundmode_with_singlemode_var(PREDICTION_MODE compound_mode,MV_REFERENCE_FRAME ref_frame,MV_REFERENCE_FRAME ref_frame2,const int_mv (* frame_mv)[REF_FRAMES],const uint8_t (* mode_checked)[REF_FRAMES],const unsigned int (* vars)[REF_FRAMES],const int64_t (* uv_dist)[REF_FRAMES])2262 static inline bool prune_compoundmode_with_singlemode_var(
2263 PREDICTION_MODE compound_mode, MV_REFERENCE_FRAME ref_frame,
2264 MV_REFERENCE_FRAME ref_frame2, const int_mv (*frame_mv)[REF_FRAMES],
2265 const uint8_t (*mode_checked)[REF_FRAMES],
2266 const unsigned int (*vars)[REF_FRAMES],
2267 const int64_t (*uv_dist)[REF_FRAMES]) {
2268 const PREDICTION_MODE single_mode0 = compound_ref0_mode(compound_mode);
2269 const PREDICTION_MODE single_mode1 = compound_ref1_mode(compound_mode);
2270
2271 bool first_ref_valid = false, second_ref_valid = false;
2272 bool first_ref_bad = false, second_ref_bad = false;
2273 if (mode_checked[single_mode0][ref_frame] &&
2274 frame_mv[single_mode0][ref_frame].as_int ==
2275 frame_mv[compound_mode][ref_frame].as_int &&
2276 vars[INTER_OFFSET(single_mode0)][ref_frame] < UINT_MAX) {
2277 first_ref_valid = true;
2278 first_ref_bad =
2279 previous_mode_performed_poorly(single_mode0, ref_frame, vars, uv_dist);
2280 }
2281 if (mode_checked[single_mode1][ref_frame2] &&
2282 frame_mv[single_mode1][ref_frame2].as_int ==
2283 frame_mv[compound_mode][ref_frame2].as_int &&
2284 vars[INTER_OFFSET(single_mode1)][ref_frame2] < UINT_MAX) {
2285 second_ref_valid = true;
2286 second_ref_bad =
2287 previous_mode_performed_poorly(single_mode1, ref_frame2, vars, uv_dist);
2288 }
2289 if (first_ref_valid && second_ref_valid) {
2290 return first_ref_bad && second_ref_bad;
2291 } else if (first_ref_valid || second_ref_valid) {
2292 return first_ref_bad || second_ref_bad;
2293 }
2294 return false;
2295 }
2296
2297 // Function to setup parameters used for inter mode evaluation in non-rd.
set_params_nonrd_pick_inter_mode(AV1_COMP * cpi,MACROBLOCK * x,InterModeSearchStateNonrd * search_state,RD_STATS * rd_cost,int * force_skip_low_temp_var,int mi_row,int mi_col,int gf_temporal_ref,unsigned char segment_id,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx,int denoise_svc_pickmode)2298 static AOM_FORCE_INLINE void set_params_nonrd_pick_inter_mode(
2299 AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
2300 RD_STATS *rd_cost, int *force_skip_low_temp_var, int mi_row, int mi_col,
2301 int gf_temporal_ref, unsigned char segment_id, BLOCK_SIZE bsize
2302 #if CONFIG_AV1_TEMPORAL_DENOISING
2303 ,
2304 PICK_MODE_CONTEXT *ctx, int denoise_svc_pickmode
2305 #endif
2306 ) {
2307 AV1_COMMON *const cm = &cpi->common;
2308 MACROBLOCKD *const xd = &x->e_mbd;
2309 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
2310 MB_MODE_INFO *const mi = xd->mi[0];
2311 const ModeCosts *mode_costs = &x->mode_costs;
2312 int skip_pred_mv = 0;
2313
2314 // Initialize variance and distortion (chroma) for all modes and reference
2315 // frames
2316 for (int idx = 0; idx < RTC_INTER_MODES; idx++) {
2317 for (int ref = 0; ref < REF_FRAMES; ref++) {
2318 search_state->vars[idx][ref] = UINT_MAX;
2319 search_state->uv_dist[idx][ref] = INT64_MAX;
2320 }
2321 }
2322
2323 // Initialize values of color sensitivity with sb level color sensitivity
2324 av1_copy(x->color_sensitivity, x->color_sensitivity_sb);
2325
2326 init_best_pickmode(&search_state->best_pickmode);
2327
2328 // Estimate cost for single reference frames
2329 estimate_single_ref_frame_costs(cm, xd, mode_costs, segment_id, bsize,
2330 search_state->ref_costs_single);
2331
2332 // Reset flag to indicate modes evaluated
2333 av1_zero(search_state->mode_checked);
2334
2335 txfm_info->skip_txfm = 0;
2336
2337 // Initialize mode decisions
2338 av1_invalid_rd_stats(&search_state->best_rdc);
2339 av1_invalid_rd_stats(&search_state->this_rdc);
2340 av1_invalid_rd_stats(rd_cost);
2341 for (int ref_idx = 0; ref_idx < REF_FRAMES; ++ref_idx) {
2342 x->warp_sample_info[ref_idx].num = -1;
2343 }
2344
2345 mi->bsize = bsize;
2346 mi->ref_frame[0] = NONE_FRAME;
2347 mi->ref_frame[1] = NONE_FRAME;
2348
2349 #if CONFIG_AV1_TEMPORAL_DENOISING
2350 if (cpi->oxcf.noise_sensitivity > 0) {
2351 // if (cpi->ppi->use_svc) denoise_svc_pickmode =
2352 // av1_denoise_svc_non_key(cpi);
2353 if (cpi->denoiser.denoising_level > kDenLowLow && denoise_svc_pickmode)
2354 av1_denoiser_reset_frame_stats(ctx);
2355 }
2356 #endif
2357
2358 // Populate predicated motion vectors for LAST_FRAME
2359 if (cpi->ref_frame_flags & AOM_LAST_FLAG) {
2360 find_predictors(cpi, x, LAST_FRAME, search_state->frame_mv,
2361 search_state->yv12_mb, bsize, *force_skip_low_temp_var,
2362 x->force_zeromv_skip_for_blk,
2363 &search_state->use_scaled_ref_frame[LAST_FRAME]);
2364 }
2365 // Update mask to use all reference frame
2366 get_ref_frame_use_mask(cpi, x, mi, mi_row, mi_col, bsize, gf_temporal_ref,
2367 search_state->use_ref_frame_mask,
2368 force_skip_low_temp_var);
2369
2370 skip_pred_mv = x->force_zeromv_skip_for_blk ||
2371 (x->nonrd_prune_ref_frame_search > 2 &&
2372 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] != 2 &&
2373 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] != 2);
2374
2375 // Populate predicated motion vectors for other single reference frame
2376 // Start at LAST_FRAME + 1.
2377 for (MV_REFERENCE_FRAME ref_frame_iter = LAST_FRAME + 1;
2378 ref_frame_iter <= ALTREF_FRAME; ++ref_frame_iter) {
2379 if (search_state->use_ref_frame_mask[ref_frame_iter]) {
2380 find_predictors(cpi, x, ref_frame_iter, search_state->frame_mv,
2381 search_state->yv12_mb, bsize, *force_skip_low_temp_var,
2382 skip_pred_mv,
2383 &search_state->use_scaled_ref_frame[ref_frame_iter]);
2384 }
2385 }
2386 }
2387
2388 // Function to check the inter mode can be skipped based on mode statistics and
2389 // speed features settings.
skip_inter_mode_nonrd(AV1_COMP * cpi,MACROBLOCK * x,InterModeSearchStateNonrd * search_state,int64_t * thresh_sad_pred,int * force_mv_inter_layer,int * is_single_pred,PREDICTION_MODE * this_mode,MV_REFERENCE_FRAME * last_comp_ref_frame,MV_REFERENCE_FRAME * ref_frame,MV_REFERENCE_FRAME * ref_frame2,int idx,int_mv svc_mv,int force_skip_low_temp_var,unsigned int sse_zeromv_norm,int num_inter_modes,unsigned char segment_id,BLOCK_SIZE bsize,bool comp_use_zero_zeromv_only,bool check_globalmv)2390 static AOM_FORCE_INLINE bool skip_inter_mode_nonrd(
2391 AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
2392 int64_t *thresh_sad_pred, int *force_mv_inter_layer, int *is_single_pred,
2393 PREDICTION_MODE *this_mode, MV_REFERENCE_FRAME *last_comp_ref_frame,
2394 MV_REFERENCE_FRAME *ref_frame, MV_REFERENCE_FRAME *ref_frame2, int idx,
2395 int_mv svc_mv, int force_skip_low_temp_var, unsigned int sse_zeromv_norm,
2396 int num_inter_modes, unsigned char segment_id, BLOCK_SIZE bsize,
2397 bool comp_use_zero_zeromv_only, bool check_globalmv) {
2398 AV1_COMMON *const cm = &cpi->common;
2399 const struct segmentation *const seg = &cm->seg;
2400 const SVC *const svc = &cpi->svc;
2401 MACROBLOCKD *const xd = &x->e_mbd;
2402 MB_MODE_INFO *const mi = xd->mi[0];
2403 const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
2404
2405 // Skip compound mode based on reference frame mask and type of the mode and
2406 // for allowed compound modes, setup ref mv stack and reference frame.
2407 if (idx >= num_inter_modes) {
2408 const int comp_index = idx - num_inter_modes;
2409 if (!setup_compound_params_from_comp_idx(
2410 cpi, x, search_state->yv12_mb, this_mode, ref_frame, ref_frame2,
2411 search_state->frame_mv, search_state->use_ref_frame_mask,
2412 comp_index, comp_use_zero_zeromv_only, last_comp_ref_frame,
2413 bsize)) {
2414 return true;
2415 }
2416 *is_single_pred = 0;
2417 } else {
2418 *this_mode = ref_mode_set[idx].pred_mode;
2419 *ref_frame = ref_mode_set[idx].ref_frame;
2420 *ref_frame2 = NONE_FRAME;
2421 }
2422
2423 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) &&
2424 (*this_mode != GLOBALMV || *ref_frame != LAST_FRAME))
2425 return true;
2426
2427 // Skip the mode if use reference frame mask flag is not set.
2428 if (!search_state->use_ref_frame_mask[*ref_frame]) return true;
2429
2430 // Skip mode for some modes and reference frames when
2431 // force_zeromv_skip_for_blk flag is true.
2432 if (x->force_zeromv_skip_for_blk &&
2433 ((!(*this_mode == NEARESTMV &&
2434 search_state->frame_mv[*this_mode][*ref_frame].as_int == 0) &&
2435 *this_mode != GLOBALMV) ||
2436 *ref_frame != LAST_FRAME))
2437 return true;
2438
2439 if (x->sb_me_block && *ref_frame == LAST_FRAME) {
2440 // We want to make sure to test the superblock MV:
2441 // so don't skip (return false) for NEAREST_LAST or NEAR_LAST if they
2442 // have this sb MV. And don't skip NEWMV_LAST: this will be set to
2443 // sb MV in handle_inter_mode_nonrd(), in case NEAREST or NEAR don't
2444 // have it.
2445 if (*this_mode == NEARESTMV &&
2446 search_state->frame_mv[NEARESTMV][LAST_FRAME].as_int ==
2447 x->sb_me_mv.as_int) {
2448 return false;
2449 }
2450 if (*this_mode == NEARMV &&
2451 search_state->frame_mv[NEARMV][LAST_FRAME].as_int ==
2452 x->sb_me_mv.as_int) {
2453 return false;
2454 }
2455 if (*this_mode == NEWMV) {
2456 return false;
2457 }
2458 }
2459
2460 // Skip the single reference mode for which mode check flag is set.
2461 if (*is_single_pred && search_state->mode_checked[*this_mode][*ref_frame]) {
2462 return true;
2463 }
2464
2465 // Skip GLOBALMV mode if check_globalmv flag is not enabled.
2466 if (!check_globalmv && *this_mode == GLOBALMV) {
2467 return true;
2468 }
2469
2470 #if COLLECT_NONRD_PICK_MODE_STAT
2471 aom_usec_timer_start(&x->ms_stat_nonrd.timer1);
2472 x->ms_stat_nonrd.num_searches[bsize][*this_mode]++;
2473 #endif
2474 mi->mode = *this_mode;
2475 mi->ref_frame[0] = *ref_frame;
2476 mi->ref_frame[1] = *ref_frame2;
2477
2478 // Skip compound mode based on variance of previously evaluated single
2479 // reference modes.
2480 if (rt_sf->prune_compoundmode_with_singlemode_var && !*is_single_pred &&
2481 prune_compoundmode_with_singlemode_var(
2482 *this_mode, *ref_frame, *ref_frame2, search_state->frame_mv,
2483 search_state->mode_checked, search_state->vars,
2484 search_state->uv_dist)) {
2485 return true;
2486 }
2487
2488 *force_mv_inter_layer = 0;
2489 if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
2490 ((*ref_frame == LAST_FRAME && svc->skip_mvsearch_last) ||
2491 (*ref_frame == GOLDEN_FRAME && svc->skip_mvsearch_gf) ||
2492 (*ref_frame == ALTREF_FRAME && svc->skip_mvsearch_altref))) {
2493 // Only test mode if NEARESTMV/NEARMV is (svc_mv.mv.col, svc_mv.mv.row),
2494 // otherwise set NEWMV to (svc_mv.mv.col, svc_mv.mv.row).
2495 // Skip newmv and filter search.
2496 *force_mv_inter_layer = 1;
2497 if (*this_mode == NEWMV) {
2498 search_state->frame_mv[*this_mode][*ref_frame] = svc_mv;
2499 } else if (search_state->frame_mv[*this_mode][*ref_frame].as_int !=
2500 svc_mv.as_int) {
2501 return true;
2502 }
2503 }
2504
2505 // If the segment reference frame feature is enabled then do nothing if the
2506 // current ref frame is not allowed.
2507 if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
2508 get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)(*ref_frame))
2509 return true;
2510
2511 // For screen content: skip mode testing based on source_sad.
2512 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
2513 !x->force_zeromv_skip_for_blk) {
2514 // If source_sad is computed: skip non-zero motion
2515 // check for stationary (super)blocks. Otherwise if superblock
2516 // has motion skip the modes with zero motion on last reference
2517 // for flat blocks, and color is not set.
2518 // For the latter condition: the same condition should apply
2519 // to newmv if (0, 0), so this latter condition is repeated
2520 // below after search_new_mv.
2521 if (rt_sf->source_metrics_sb_nonrd) {
2522 if ((search_state->frame_mv[*this_mode][*ref_frame].as_int != 0 &&
2523 x->content_state_sb.source_sad_nonrd == kZeroSad) ||
2524 (search_state->frame_mv[*this_mode][*ref_frame].as_int == 0 &&
2525 x->block_is_zero_sad == 0 && *ref_frame == LAST_FRAME &&
2526 ((x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
2527 x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) ||
2528 cpi->rc.high_source_sad) &&
2529 x->source_variance == 0))
2530 return true;
2531 }
2532 // Skip NEWMV search for flat blocks.
2533 if (rt_sf->skip_newmv_flat_blocks_screen && *this_mode == NEWMV &&
2534 x->source_variance < 100)
2535 return true;
2536 // Skip non-LAST for color on flat blocks.
2537 if (*ref_frame > LAST_FRAME && x->source_variance == 0 &&
2538 (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2539 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 1))
2540 return true;
2541 }
2542
2543 // Skip mode based on block size, reference frame mode and other block
2544 // properties.
2545 if (skip_mode_by_bsize_and_ref_frame(
2546 *this_mode, *ref_frame, bsize, x->nonrd_prune_ref_frame_search,
2547 sse_zeromv_norm, rt_sf->nonrd_aggressive_skip,
2548 rt_sf->increase_source_sad_thresh))
2549 return true;
2550
2551 // Skip mode based on low temporal variance and souce sad.
2552 if (skip_mode_by_low_temp(*this_mode, *ref_frame, bsize, x->content_state_sb,
2553 search_state->frame_mv[*this_mode][*ref_frame],
2554 force_skip_low_temp_var))
2555 return true;
2556
2557 // Disable this drop out case if the ref frame segment level feature is
2558 // enabled for this segment. This is to prevent the possibility that we
2559 // end up unable to pick any mode.
2560 if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
2561 // Check for skipping GOLDEN and ALTREF based pred_mv_sad.
2562 if (rt_sf->nonrd_prune_ref_frame_search > 0 &&
2563 x->pred_mv_sad[*ref_frame] != INT_MAX && *ref_frame != LAST_FRAME) {
2564 if ((int64_t)(x->pred_mv_sad[*ref_frame]) > *thresh_sad_pred) return true;
2565 }
2566 }
2567
2568 // Check for skipping NEARMV based on pred_mv_sad.
2569 if (*this_mode == NEARMV && x->pred_mv1_sad[*ref_frame] != INT_MAX &&
2570 x->pred_mv1_sad[*ref_frame] > (x->pred_mv0_sad[*ref_frame] << 1))
2571 return true;
2572
2573 // Skip single reference mode based on rd threshold.
2574 if (*is_single_pred) {
2575 if (skip_mode_by_threshold(
2576 *this_mode, *ref_frame,
2577 search_state->frame_mv[*this_mode][*ref_frame],
2578 cpi->rc.frames_since_golden, cpi->rd.threshes[segment_id][bsize],
2579 x->thresh_freq_fact[bsize], search_state->best_rdc.rdcost,
2580 search_state->best_pickmode.best_mode_skip_txfm,
2581 (rt_sf->nonrd_aggressive_skip ? 1 : 0)))
2582 return true;
2583 }
2584 return false;
2585 }
2586
2587 // Function to perform inter mode evaluation for non-rd
handle_inter_mode_nonrd(AV1_COMP * cpi,MACROBLOCK * x,InterModeSearchStateNonrd * search_state,PICK_MODE_CONTEXT * ctx,PRED_BUFFER ** this_mode_pred,PRED_BUFFER * tmp_buffer,InterPredParams inter_pred_params_sr,int * best_early_term,unsigned int * sse_zeromv_norm,bool * check_globalmv,int64_t * zero_last_cost_orig,int denoise_svc_pickmode,int idx,int force_mv_inter_layer,int is_single_pred,int gf_temporal_ref,int use_model_yrd_large,int filter_search_enabled_blk,BLOCK_SIZE bsize,PREDICTION_MODE this_mode,InterpFilter filt_select,int cb_pred_filter_search,int reuse_inter_pred,int * sb_me_has_been_tested)2588 static AOM_FORCE_INLINE bool handle_inter_mode_nonrd(
2589 AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
2590 PICK_MODE_CONTEXT *ctx, PRED_BUFFER **this_mode_pred,
2591 PRED_BUFFER *tmp_buffer, InterPredParams inter_pred_params_sr,
2592 int *best_early_term, unsigned int *sse_zeromv_norm, bool *check_globalmv,
2593 #if CONFIG_AV1_TEMPORAL_DENOISING
2594 int64_t *zero_last_cost_orig, int denoise_svc_pickmode,
2595 #endif
2596 int idx, int force_mv_inter_layer, int is_single_pred, int gf_temporal_ref,
2597 int use_model_yrd_large, int filter_search_enabled_blk, BLOCK_SIZE bsize,
2598 PREDICTION_MODE this_mode, InterpFilter filt_select,
2599 int cb_pred_filter_search, int reuse_inter_pred,
2600 int *sb_me_has_been_tested) {
2601 AV1_COMMON *const cm = &cpi->common;
2602 MACROBLOCKD *const xd = &x->e_mbd;
2603 MB_MODE_INFO *const mi = xd->mi[0];
2604 const MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
2605 const int mi_row = xd->mi_row;
2606 const int mi_col = xd->mi_col;
2607 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
2608 const int bw = block_size_wide[bsize];
2609 const InterpFilter filter_ref = cm->features.interp_filter;
2610 const InterpFilter default_interp_filter = EIGHTTAP_REGULAR;
2611 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
2612 const ModeCosts *mode_costs = &x->mode_costs;
2613 const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
2614 BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode;
2615
2616 MV_REFERENCE_FRAME ref_frame = mi->ref_frame[0];
2617 MV_REFERENCE_FRAME ref_frame2 = mi->ref_frame[1];
2618 int_mv *const this_mv = &search_state->frame_mv[this_mode][ref_frame];
2619 unsigned int var = UINT_MAX;
2620 int this_early_term = 0;
2621 int rate_mv = 0;
2622 int is_skippable;
2623 int skip_this_mv = 0;
2624 unsigned int var_threshold = UINT_MAX;
2625 PREDICTION_MODE this_best_mode;
2626 RD_STATS nonskip_rdc;
2627 av1_invalid_rd_stats(&nonskip_rdc);
2628
2629 if (x->sb_me_block && this_mode == NEWMV && ref_frame == LAST_FRAME) {
2630 // Set the NEWMV_LAST to the sb MV.
2631 search_state->frame_mv[NEWMV][LAST_FRAME].as_int = x->sb_me_mv.as_int;
2632 } else if (this_mode == NEWMV && !force_mv_inter_layer) {
2633 #if COLLECT_NONRD_PICK_MODE_STAT
2634 aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
2635 #endif
2636 // Find the best motion vector for single/compound mode.
2637 const bool skip_newmv = search_new_mv(
2638 cpi, x, search_state->frame_mv, ref_frame, gf_temporal_ref, bsize,
2639 mi_row, mi_col, &rate_mv, &search_state->best_rdc);
2640 #if COLLECT_NONRD_PICK_MODE_STAT
2641 aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
2642 x->ms_stat_nonrd.ms_time[bsize][this_mode] +=
2643 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
2644 #endif
2645 // Skip NEWMV mode,
2646 // (i). For bsize smaller than 16X16
2647 // (ii). Based on sad of the predicted mv w.r.t LAST_FRAME
2648 // (iii). When motion vector is same as that of reference mv
2649 if (skip_newmv) {
2650 return true;
2651 }
2652 }
2653
2654 // Check the current motion vector is same as that of previously evaluated
2655 // motion vectors.
2656 for (PREDICTION_MODE inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV;
2657 inter_mv_mode++) {
2658 if (inter_mv_mode == this_mode) continue;
2659 if (is_single_pred &&
2660 search_state->mode_checked[inter_mv_mode][ref_frame] &&
2661 this_mv->as_int ==
2662 search_state->frame_mv[inter_mv_mode][ref_frame].as_int) {
2663 skip_this_mv = 1;
2664 break;
2665 }
2666 }
2667
2668 // Skip single mode if current motion vector is same that of previously
2669 // evaluated motion vectors.
2670 if (skip_this_mv && is_single_pred) return true;
2671
2672 // For screen: for spatially flat blocks with non-zero motion,
2673 // skip newmv if the motion vector is (0, 0)-LAST, and color is not set.
2674 if (this_mode == NEWMV && cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
2675 cpi->svc.spatial_layer_id == 0 && rt_sf->source_metrics_sb_nonrd) {
2676 if (this_mv->as_int == 0 && ref_frame == LAST_FRAME &&
2677 x->block_is_zero_sad == 0 &&
2678 ((x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
2679 x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) ||
2680 cpi->rc.high_source_sad) &&
2681 x->source_variance == 0)
2682 return true;
2683 }
2684
2685 mi->mode = this_mode;
2686 mi->mv[0].as_int = this_mv->as_int;
2687 mi->mv[1].as_int = 0;
2688 if (!is_single_pred)
2689 mi->mv[1].as_int = search_state->frame_mv[this_mode][ref_frame2].as_int;
2690
2691 // Set buffers to store predicted samples for reuse
2692 if (reuse_inter_pred) {
2693 if (!*this_mode_pred) {
2694 *this_mode_pred = &tmp_buffer[3];
2695 } else {
2696 *this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
2697 pd->dst.buf = (*this_mode_pred)->data;
2698 pd->dst.stride = bw;
2699 }
2700 }
2701
2702 mi->motion_mode = SIMPLE_TRANSLATION;
2703 #if !CONFIG_REALTIME_ONLY
2704 if (cpi->oxcf.motion_mode_cfg.allow_warped_motion) {
2705 calc_num_proj_ref(cpi, x, mi);
2706 }
2707 #endif
2708 // set variance threshold for compound mode pruning
2709 if (rt_sf->prune_compoundmode_with_singlecompound_var && !is_single_pred &&
2710 use_model_yrd_large) {
2711 const PREDICTION_MODE single_mode0 = compound_ref0_mode(this_mode);
2712 const PREDICTION_MODE single_mode1 = compound_ref1_mode(this_mode);
2713 var_threshold =
2714 AOMMIN(var_threshold,
2715 search_state->vars[INTER_OFFSET(single_mode0)][ref_frame]);
2716 var_threshold =
2717 AOMMIN(var_threshold,
2718 search_state->vars[INTER_OFFSET(single_mode1)][ref_frame2]);
2719 }
2720
2721 // decide interpolation filter, build prediction signal, get sse
2722 const bool is_mv_subpel =
2723 (mi->mv[0].as_mv.row & 0x07) || (mi->mv[0].as_mv.col & 0x07);
2724 const bool enable_filt_search_this_mode =
2725 (filter_search_enabled_blk == 2)
2726 ? true
2727 : (filter_search_enabled_blk && !force_mv_inter_layer &&
2728 is_single_pred &&
2729 (ref_frame == LAST_FRAME || !x->nonrd_prune_ref_frame_search));
2730 if (is_mv_subpel && enable_filt_search_this_mode) {
2731 #if COLLECT_NONRD_PICK_MODE_STAT
2732 aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
2733 #endif
2734 search_filter_ref(
2735 cpi, x, &search_state->this_rdc, &inter_pred_params_sr, mi_row, mi_col,
2736 tmp_buffer, bsize, reuse_inter_pred, this_mode_pred, &this_early_term,
2737 &var, use_model_yrd_large, best_pickmode->best_sse, is_single_pred);
2738 #if COLLECT_NONRD_PICK_MODE_STAT
2739 aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
2740 x->ms_stat_nonrd.ifs_time[bsize][this_mode] +=
2741 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
2742 #endif
2743 #if !CONFIG_REALTIME_ONLY
2744 } else if (cpi->oxcf.motion_mode_cfg.allow_warped_motion &&
2745 this_mode == NEWMV) {
2746 // Find the best motion mode when current mode is NEWMV
2747 search_motion_mode(cpi, x, &search_state->this_rdc, mi_row, mi_col, bsize,
2748 &this_early_term, use_model_yrd_large, &rate_mv,
2749 best_pickmode->best_sse);
2750 if (this_mode == NEWMV) {
2751 this_mv[0] = mi->mv[0];
2752 }
2753 #endif
2754 } else {
2755 mi->interp_filters =
2756 (filter_ref == SWITCHABLE)
2757 ? av1_broadcast_interp_filter(default_interp_filter)
2758 : av1_broadcast_interp_filter(filter_ref);
2759 if (force_mv_inter_layer)
2760 mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
2761
2762 // If it is sub-pel motion and cb_pred_filter_search is enabled, select
2763 // the pre-decided filter
2764 if (is_mv_subpel && cb_pred_filter_search)
2765 mi->interp_filters = av1_broadcast_interp_filter(filt_select);
2766
2767 #if COLLECT_NONRD_PICK_MODE_STAT
2768 aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
2769 #endif
2770 if (is_single_pred) {
2771 SubpelParams subpel_params;
2772 // Initialize inter mode level params for single reference mode.
2773 init_inter_mode_params(&mi->mv[0].as_mv, &inter_pred_params_sr,
2774 &subpel_params, xd->block_ref_scale_factors[0],
2775 pd->pre->width, pd->pre->height);
2776 av1_enc_build_inter_predictor_y_nonrd(xd, &inter_pred_params_sr,
2777 &subpel_params);
2778 } else {
2779 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2780 AOM_PLANE_Y, AOM_PLANE_Y);
2781 }
2782
2783 if (use_model_yrd_large) {
2784 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
2785 &search_state->this_rdc, &this_early_term, 0,
2786 best_pickmode->best_sse, &var, var_threshold);
2787 } else {
2788 model_rd_for_sb_y(cpi, bsize, x, xd, &search_state->this_rdc, &var, 0,
2789 &this_early_term);
2790 }
2791 #if COLLECT_NONRD_PICK_MODE_STAT
2792 aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
2793 x->ms_stat_nonrd.model_rd_time[bsize][this_mode] +=
2794 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
2795 #endif
2796 }
2797
2798 // update variance for single mode
2799 if (is_single_pred) {
2800 search_state->vars[INTER_OFFSET(this_mode)][ref_frame] = var;
2801 if (this_mv->as_int == 0) {
2802 search_state->vars[INTER_OFFSET(GLOBALMV)][ref_frame] = var;
2803 }
2804 }
2805 // prune compound mode based on single mode var threshold
2806 if (!is_single_pred && var > var_threshold) {
2807 if (reuse_inter_pred) free_pred_buffer(*this_mode_pred);
2808 return true;
2809 }
2810
2811 if (ref_frame == LAST_FRAME && this_mv->as_int == 0) {
2812 *sse_zeromv_norm = (unsigned int)(search_state->this_rdc.sse >>
2813 (b_width_log2_lookup[bsize] +
2814 b_height_log2_lookup[bsize]));
2815 }
2816
2817 // Perform early termination based on sse.
2818 if (rt_sf->sse_early_term_inter_search &&
2819 early_term_inter_search_with_sse(rt_sf->sse_early_term_inter_search,
2820 bsize, search_state->this_rdc.sse,
2821 best_pickmode->best_sse, this_mode)) {
2822 if (reuse_inter_pred) free_pred_buffer(*this_mode_pred);
2823 return true;
2824 }
2825
2826 #if COLLECT_NONRD_PICK_MODE_STAT
2827 x->ms_stat_nonrd.num_nonskipped_searches[bsize][this_mode]++;
2828 #endif
2829
2830 const int skip_ctx = av1_get_skip_txfm_context(xd);
2831 const int skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][1];
2832 const int no_skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][0];
2833 const int64_t sse_y = search_state->this_rdc.sse;
2834
2835 if (this_early_term) {
2836 search_state->this_rdc.skip_txfm = 1;
2837 search_state->this_rdc.rate = skip_txfm_cost;
2838 search_state->this_rdc.dist = search_state->this_rdc.sse << 4;
2839 } else {
2840 #if COLLECT_NONRD_PICK_MODE_STAT
2841 aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
2842 #endif
2843 // Calculates RD Cost using Hadamard transform.
2844 av1_block_yrd(x, &search_state->this_rdc, &is_skippable, bsize,
2845 mi->tx_size);
2846 if (search_state->this_rdc.skip_txfm ||
2847 RDCOST(x->rdmult, search_state->this_rdc.rate,
2848 search_state->this_rdc.dist) >=
2849 RDCOST(x->rdmult, 0, search_state->this_rdc.sse)) {
2850 if (!search_state->this_rdc.skip_txfm) {
2851 // Need to store "real" rdc for possible future use if UV rdc
2852 // disallows tx skip
2853 nonskip_rdc = search_state->this_rdc;
2854 nonskip_rdc.rate += no_skip_txfm_cost;
2855 }
2856 search_state->this_rdc.rate = skip_txfm_cost;
2857 search_state->this_rdc.skip_txfm = 1;
2858 search_state->this_rdc.dist = search_state->this_rdc.sse;
2859 } else {
2860 search_state->this_rdc.rate += no_skip_txfm_cost;
2861 }
2862
2863 // Populate predicted sample for chroma planes based on color sensitivity.
2864 if ((x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
2865 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])) {
2866 RD_STATS rdc_uv;
2867 const BLOCK_SIZE uv_bsize =
2868 get_plane_block_size(bsize, xd->plane[AOM_PLANE_U].subsampling_x,
2869 xd->plane[AOM_PLANE_U].subsampling_y);
2870 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)]) {
2871 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2872 AOM_PLANE_U, AOM_PLANE_U);
2873 }
2874 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) {
2875 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2876 AOM_PLANE_V, AOM_PLANE_V);
2877 }
2878 // Compute sse for chroma planes.
2879 const int64_t sse_uv = av1_model_rd_for_sb_uv(
2880 cpi, uv_bsize, x, xd, &rdc_uv, AOM_PLANE_U, AOM_PLANE_V);
2881 if (rdc_uv.dist < x->min_dist_inter_uv)
2882 x->min_dist_inter_uv = rdc_uv.dist;
2883 search_state->this_rdc.sse += sse_uv;
2884 // Restore Y rdc if UV rdc disallows txfm skip
2885 if (search_state->this_rdc.skip_txfm && !rdc_uv.skip_txfm &&
2886 nonskip_rdc.rate != INT_MAX)
2887 search_state->this_rdc = nonskip_rdc;
2888 if (is_single_pred) {
2889 search_state->uv_dist[INTER_OFFSET(this_mode)][ref_frame] = rdc_uv.dist;
2890 }
2891 search_state->this_rdc.rate += rdc_uv.rate;
2892 search_state->this_rdc.dist += rdc_uv.dist;
2893 search_state->this_rdc.skip_txfm =
2894 search_state->this_rdc.skip_txfm && rdc_uv.skip_txfm;
2895 }
2896 #if COLLECT_NONRD_PICK_MODE_STAT
2897 aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
2898 x->ms_stat_nonrd.txfm_time[bsize][this_mode] +=
2899 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
2900 #endif
2901 }
2902
2903 this_best_mode = this_mode;
2904 // TODO(kyslov) account for UV prediction cost
2905 search_state->this_rdc.rate += rate_mv;
2906 if (!is_single_pred) {
2907 const int16_t mode_ctx =
2908 av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame);
2909 search_state->this_rdc.rate += cost_mv_ref(mode_costs, this_mode, mode_ctx);
2910 } else {
2911 // If the current mode has zeromv but is not GLOBALMV, compare the rate
2912 // cost. If GLOBALMV is cheaper, use GLOBALMV instead.
2913 if (this_mode != GLOBALMV &&
2914 this_mv->as_int == search_state->frame_mv[GLOBALMV][ref_frame].as_int) {
2915 if (is_globalmv_better(this_mode, ref_frame, rate_mv, mode_costs,
2916 search_state->single_inter_mode_costs, mbmi_ext)) {
2917 this_best_mode = GLOBALMV;
2918 }
2919 }
2920
2921 search_state->this_rdc.rate +=
2922 search_state
2923 ->single_inter_mode_costs[INTER_OFFSET(this_best_mode)][ref_frame];
2924 }
2925
2926 if (is_single_pred && this_mv->as_int == 0 && var < UINT_MAX) {
2927 search_state->vars[INTER_OFFSET(GLOBALMV)][ref_frame] = var;
2928 }
2929
2930 search_state->this_rdc.rate += search_state->ref_costs_single[ref_frame];
2931
2932 search_state->this_rdc.rdcost = RDCOST(x->rdmult, search_state->this_rdc.rate,
2933 search_state->this_rdc.dist);
2934 if (cpi->oxcf.rc_cfg.mode == AOM_CBR && is_single_pred) {
2935 newmv_diff_bias(xd, this_best_mode, &search_state->this_rdc, bsize,
2936 search_state->frame_mv[this_best_mode][ref_frame].as_mv.row,
2937 search_state->frame_mv[this_best_mode][ref_frame].as_mv.col,
2938 cpi->speed, x->source_variance, x->content_state_sb);
2939 }
2940
2941 #if CONFIG_AV1_TEMPORAL_DENOISING
2942 if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc_pickmode &&
2943 cpi->denoiser.denoising_level > kDenLowLow) {
2944 av1_denoiser_update_frame_stats(mi, sse_y, this_mode, ctx);
2945 // Keep track of zero_last cost.
2946 if (ref_frame == LAST_FRAME && this_mv->as_int == 0)
2947 *zero_last_cost_orig = search_state->this_rdc.rdcost;
2948 }
2949 #else
2950 (void)(sse_y);
2951 #endif
2952
2953 search_state->mode_checked[this_mode][ref_frame] = 1;
2954 search_state->mode_checked[this_best_mode][ref_frame] = 1;
2955
2956 if (*check_globalmv) {
2957 int32_t abs_mv =
2958 abs(search_state->frame_mv[this_best_mode][ref_frame].as_mv.row) +
2959 abs(search_state->frame_mv[this_best_mode][ref_frame].as_mv.col);
2960 // Early exit check: if the magnitude of this_best_mode's mv is small
2961 // enough, we skip GLOBALMV check in the next loop iteration.
2962 if (abs_mv < 2) {
2963 *check_globalmv = false;
2964 }
2965 }
2966 #if COLLECT_NONRD_PICK_MODE_STAT
2967 aom_usec_timer_mark(&x->ms_stat_nonrd.timer1);
2968 x->ms_stat_nonrd.nonskipped_search_times[bsize][this_mode] +=
2969 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer1);
2970 #endif
2971
2972 if (x->sb_me_block && ref_frame == LAST_FRAME &&
2973 search_state->frame_mv[this_best_mode][ref_frame].as_int ==
2974 x->sb_me_mv.as_int)
2975 *sb_me_has_been_tested = 1;
2976
2977 // Copy best mode params to search state
2978 if (search_state->this_rdc.rdcost < search_state->best_rdc.rdcost) {
2979 search_state->best_rdc = search_state->this_rdc;
2980 *best_early_term = this_early_term;
2981 update_search_state_nonrd(search_state, mi, txfm_info, &nonskip_rdc, ctx,
2982 this_best_mode, sse_y);
2983
2984 // This is needed for the compound modes.
2985 search_state->frame_mv_best[this_best_mode][ref_frame].as_int =
2986 search_state->frame_mv[this_best_mode][ref_frame].as_int;
2987 if (ref_frame2 > NONE_FRAME) {
2988 search_state->frame_mv_best[this_best_mode][ref_frame2].as_int =
2989 search_state->frame_mv[this_best_mode][ref_frame2].as_int;
2990 }
2991
2992 if (reuse_inter_pred) {
2993 free_pred_buffer(best_pickmode->best_pred);
2994 best_pickmode->best_pred = *this_mode_pred;
2995 }
2996 } else {
2997 if (reuse_inter_pred) free_pred_buffer(*this_mode_pred);
2998 }
2999
3000 if (*best_early_term && (idx > 0 || rt_sf->nonrd_aggressive_skip)) {
3001 txfm_info->skip_txfm = 1;
3002 if (!x->sb_me_block || *sb_me_has_been_tested) return false;
3003 }
3004 return true;
3005 }
3006
3007 // Function to perform screen content mode evaluation for non-rd
handle_screen_content_mode_nonrd(AV1_COMP * cpi,MACROBLOCK * x,InterModeSearchStateNonrd * search_state,PRED_BUFFER * this_mode_pred,PICK_MODE_CONTEXT * ctx,PRED_BUFFER * tmp_buffer,struct buf_2d * orig_dst,int skip_idtx_palette,int try_palette,BLOCK_SIZE bsize,int reuse_inter_pred,int mi_col,int mi_row)3008 static AOM_FORCE_INLINE void handle_screen_content_mode_nonrd(
3009 AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
3010 PRED_BUFFER *this_mode_pred, PICK_MODE_CONTEXT *ctx,
3011 PRED_BUFFER *tmp_buffer, struct buf_2d *orig_dst, int skip_idtx_palette,
3012 int try_palette, BLOCK_SIZE bsize, int reuse_inter_pred, int mi_col,
3013 int mi_row) {
3014 AV1_COMMON *const cm = &cpi->common;
3015 const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
3016 MACROBLOCKD *const xd = &x->e_mbd;
3017 MB_MODE_INFO *const mi = xd->mi[0];
3018 struct macroblockd_plane *const pd = &xd->plane[0];
3019 const int bw = block_size_wide[bsize];
3020 const int bh = block_size_high[bsize];
3021 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
3022 BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode;
3023
3024 // TODO(marpan): Only allow for 8 bit-depth for now, re-enable for 10/12 bit
3025 // when issue 3359 is fixed.
3026 if (cm->seq_params->bit_depth == 8 && rt_sf->use_idtx_nonrd &&
3027 !skip_idtx_palette && !cpi->oxcf.txfm_cfg.use_inter_dct_only &&
3028 !x->force_zeromv_skip_for_blk &&
3029 is_inter_mode(best_pickmode->best_mode) &&
3030 best_pickmode->best_pred != NULL &&
3031 (!rt_sf->prune_idtx_nonrd ||
3032 (rt_sf->prune_idtx_nonrd && bsize <= BLOCK_32X32 &&
3033 best_pickmode->best_mode_skip_txfm != 1 && x->source_variance > 200))) {
3034 RD_STATS idtx_rdc;
3035 av1_init_rd_stats(&idtx_rdc);
3036 int is_skippable;
3037 this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
3038 pd->dst.buf = this_mode_pred->data;
3039 pd->dst.stride = bw;
3040 const PRED_BUFFER *const best_pred = best_pickmode->best_pred;
3041 av1_block_yrd_idtx(x, best_pred->data, best_pred->stride, &idtx_rdc,
3042 &is_skippable, bsize, mi->tx_size);
3043 int64_t idx_rdcost_y = RDCOST(x->rdmult, idtx_rdc.rate, idtx_rdc.dist);
3044 int allow_idtx = 1;
3045 // Incorporate color into rd cost.
3046 if ((x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3047 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])) {
3048 RD_STATS rdc_uv;
3049 const BLOCK_SIZE uv_bsize =
3050 get_plane_block_size(bsize, xd->plane[AOM_PLANE_U].subsampling_x,
3051 xd->plane[AOM_PLANE_U].subsampling_y);
3052 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)]) {
3053 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3054 AOM_PLANE_U, AOM_PLANE_U);
3055 }
3056 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) {
3057 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3058 AOM_PLANE_V, AOM_PLANE_V);
3059 }
3060 av1_model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, AOM_PLANE_U,
3061 AOM_PLANE_V);
3062 if (rdc_uv.dist < x->min_dist_inter_uv)
3063 x->min_dist_inter_uv = rdc_uv.dist;
3064 idtx_rdc.rate += rdc_uv.rate;
3065 idtx_rdc.dist += rdc_uv.dist;
3066 idtx_rdc.skip_txfm = idtx_rdc.skip_txfm && rdc_uv.skip_txfm;
3067 if (idx_rdcost_y == 0 && rdc_uv.dist > 0 && x->source_variance < 3000 &&
3068 x->content_state_sb.source_sad_nonrd > kMedSad)
3069 allow_idtx = 0;
3070 }
3071 int64_t idx_rdcost = RDCOST(x->rdmult, idtx_rdc.rate, idtx_rdc.dist);
3072 if (allow_idtx && idx_rdcost < search_state->best_rdc.rdcost) {
3073 best_pickmode->tx_type = IDTX;
3074 search_state->best_rdc.rdcost = idx_rdcost;
3075 best_pickmode->best_mode_skip_txfm = idtx_rdc.skip_txfm;
3076 if (!idtx_rdc.skip_txfm) {
3077 memcpy(ctx->blk_skip, txfm_info->blk_skip,
3078 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
3079 }
3080 xd->tx_type_map[0] = best_pickmode->tx_type;
3081 memset(ctx->tx_type_map, best_pickmode->tx_type, ctx->num_4x4_blk);
3082 memset(xd->tx_type_map, best_pickmode->tx_type, ctx->num_4x4_blk);
3083 }
3084 pd->dst = *orig_dst;
3085 }
3086
3087 if (!try_palette) return;
3088 const unsigned int intra_ref_frame_cost =
3089 search_state->ref_costs_single[INTRA_FRAME];
3090
3091 if (!is_mode_intra(best_pickmode->best_mode)) {
3092 PRED_BUFFER *const best_pred = best_pickmode->best_pred;
3093 if (reuse_inter_pred && best_pred != NULL) {
3094 if (best_pred->data == orig_dst->buf) {
3095 this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
3096 aom_convolve_copy(best_pred->data, best_pred->stride,
3097 this_mode_pred->data, this_mode_pred->stride, bw, bh);
3098 best_pickmode->best_pred = this_mode_pred;
3099 }
3100 }
3101 pd->dst = *orig_dst;
3102 }
3103 // Search palette mode for Luma plane in inter frame.
3104 av1_search_palette_mode_luma(cpi, x, bsize, intra_ref_frame_cost, ctx,
3105 &search_state->this_rdc,
3106 search_state->best_rdc.rdcost);
3107 // Update best mode data in search_state
3108 if (search_state->this_rdc.rdcost < search_state->best_rdc.rdcost) {
3109 best_pickmode->pmi = mi->palette_mode_info;
3110 best_pickmode->best_mode = DC_PRED;
3111 mi->mv[0].as_int = INVALID_MV;
3112 mi->mv[1].as_int = INVALID_MV;
3113 best_pickmode->best_ref_frame = INTRA_FRAME;
3114 best_pickmode->best_second_ref_frame = NONE;
3115 search_state->best_rdc.rate = search_state->this_rdc.rate;
3116 search_state->best_rdc.dist = search_state->this_rdc.dist;
3117 search_state->best_rdc.rdcost = search_state->this_rdc.rdcost;
3118 best_pickmode->best_mode_skip_txfm = search_state->this_rdc.skip_txfm;
3119 // Keep the skip_txfm off if the color_sensitivity is set.
3120 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3121 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])
3122 search_state->this_rdc.skip_txfm = 0;
3123 if (!search_state->this_rdc.skip_txfm) {
3124 memcpy(ctx->blk_skip, txfm_info->blk_skip,
3125 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
3126 }
3127 if (xd->tx_type_map[0] != DCT_DCT)
3128 av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
3129 }
3130 }
3131
enable_palette(AV1_COMP * cpi,bool is_mode_intra,BLOCK_SIZE bsize,unsigned int source_variance,int force_zeromv_skip,int skip_idtx_palette,int force_palette_test)3132 static inline bool enable_palette(AV1_COMP *cpi, bool is_mode_intra,
3133 BLOCK_SIZE bsize,
3134 unsigned int source_variance,
3135 int force_zeromv_skip, int skip_idtx_palette,
3136 int force_palette_test) {
3137 if (!cpi->oxcf.tool_cfg.enable_palette) return false;
3138 if (!av1_allow_palette(cpi->common.features.allow_screen_content_tools,
3139 bsize)) {
3140 return false;
3141 }
3142 if (skip_idtx_palette) return false;
3143
3144 if (cpi->sf.rt_sf.prune_palette_search_nonrd > 1 &&
3145 ((cpi->rc.high_source_sad && cpi->ppi->rtc_ref.non_reference_frame) ||
3146 bsize > BLOCK_16X16)) {
3147 return false;
3148 }
3149
3150 if ((is_mode_intra || force_palette_test) && source_variance > 0 &&
3151 !force_zeromv_skip &&
3152 (cpi->rc.high_source_sad || source_variance > 300)) {
3153 return true;
3154 } else {
3155 return false;
3156 }
3157 }
3158
3159 /*!\brief AV1 inter mode selection based on Non-RD optimized model.
3160 *
3161 * \ingroup nonrd_mode_search
3162 * \callgraph
3163 * Top level function for Non-RD optimized inter mode selection.
3164 * This finction will loop over subset of inter modes and select the best one
3165 * based on calculated modelled RD cost. While making decisions which modes to
3166 * check, this function applies heuristics based on previously checked modes,
3167 * block residual variance, block size, and other factors to prune certain
3168 * modes and reference frames. Currently only single reference frame modes
3169 * are checked. Additional heuristics are applied to decide if intra modes
3170 * need to be checked.
3171 * *
3172 * \param[in] cpi Top-level encoder structure
3173 * \param[in] tile_data Pointer to struct holding adaptive
3174 data/contexts/models for the tile during
3175 encoding
3176 * \param[in] x Pointer to structure holding all the data for
3177 the current macroblock
3178 * \param[in] rd_cost Struct to keep track of the RD information
3179 * \param[in] bsize Current block size
3180 * \param[in] ctx Structure to hold snapshot of coding context
3181 during the mode picking process
3182 *
3183 * \remark Nothing is returned. Instead, the MB_MODE_INFO struct inside x
3184 * is modified to store information about the best mode computed
3185 * in this function. The rd_cost struct is also updated with the RD stats
3186 * corresponding to the best mode found.
3187 */
av1_nonrd_pick_inter_mode_sb(AV1_COMP * cpi,TileDataEnc * tile_data,MACROBLOCK * x,RD_STATS * rd_cost,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)3188 void av1_nonrd_pick_inter_mode_sb(AV1_COMP *cpi, TileDataEnc *tile_data,
3189 MACROBLOCK *x, RD_STATS *rd_cost,
3190 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
3191 AV1_COMMON *const cm = &cpi->common;
3192 SVC *const svc = &cpi->svc;
3193 MACROBLOCKD *const xd = &x->e_mbd;
3194 MB_MODE_INFO *const mi = xd->mi[0];
3195 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
3196 const MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
3197 MV_REFERENCE_FRAME ref_frame, ref_frame2;
3198 const unsigned char segment_id = mi->segment_id;
3199 int best_early_term = 0;
3200 int force_skip_low_temp_var = 0;
3201 unsigned int sse_zeromv_norm = UINT_MAX;
3202 const int num_inter_modes = NUM_INTER_MODES;
3203 const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
3204 bool check_globalmv = rt_sf->check_globalmv_on_single_ref;
3205 PRED_BUFFER tmp_buffer[4];
3206 DECLARE_ALIGNED(16, uint8_t, pred_buf[MAX_MB_PLANE * MAX_SB_SQUARE]);
3207 PRED_BUFFER *this_mode_pred = NULL;
3208 const int reuse_inter_pred =
3209 rt_sf->reuse_inter_pred_nonrd && cm->seq_params->bit_depth == AOM_BITS_8;
3210 InterModeSearchStateNonrd search_state;
3211 av1_zero(search_state.use_ref_frame_mask);
3212 av1_zero(search_state.use_scaled_ref_frame);
3213 BEST_PICKMODE *const best_pickmode = &search_state.best_pickmode;
3214 (void)tile_data;
3215
3216 const int bh = block_size_high[bsize];
3217 const int bw = block_size_wide[bsize];
3218 const int pixels_in_block = bh * bw;
3219 struct buf_2d orig_dst = pd->dst;
3220 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
3221 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
3222 #if COLLECT_NONRD_PICK_MODE_STAT
3223 // Mode statistics can be collected only when num_workers is 1
3224 assert(cpi->mt_info.num_workers <= 1);
3225 aom_usec_timer_start(&x->ms_stat_nonrd.bsize_timer);
3226 #endif
3227 int64_t thresh_sad_pred = INT64_MAX;
3228 const int mi_row = xd->mi_row;
3229 const int mi_col = xd->mi_col;
3230 int_mv svc_mv = { .as_int = 0 };
3231 int force_mv_inter_layer = 0;
3232 bool comp_use_zero_zeromv_only = 0;
3233 int tot_num_comp_modes = NUM_COMP_INTER_MODES_RT;
3234 #if CONFIG_AV1_TEMPORAL_DENOISING
3235 const int denoise_recheck_zeromv = 1;
3236 AV1_PICKMODE_CTX_DEN ctx_den;
3237 int64_t zero_last_cost_orig = INT64_MAX;
3238 int denoise_svc_pickmode = 1;
3239 const int resize_pending = is_frame_resize_pending(cpi);
3240 #endif
3241 const ModeCosts *mode_costs = &x->mode_costs;
3242 struct scale_factors sf_no_scale;
3243 av1_setup_scale_factors_for_frame(&sf_no_scale, cm->width, cm->height,
3244 cm->width, cm->height);
3245 if (reuse_inter_pred) {
3246 for (int buf_idx = 0; buf_idx < 3; buf_idx++) {
3247 tmp_buffer[buf_idx].data = &pred_buf[pixels_in_block * buf_idx];
3248 tmp_buffer[buf_idx].stride = bw;
3249 tmp_buffer[buf_idx].in_use = 0;
3250 }
3251 tmp_buffer[3].data = pd->dst.buf;
3252 tmp_buffer[3].stride = pd->dst.stride;
3253 tmp_buffer[3].in_use = 0;
3254 }
3255
3256 const int gf_temporal_ref = is_same_gf_and_last_scale(cm);
3257
3258 // If the lower spatial layer uses an averaging filter for downsampling
3259 // (phase = 8), the target decimated pixel is shifted by (1/2, 1/2) relative
3260 // to source, so use subpel motion vector to compensate. The nonzero motion
3261 // is half pixel shifted to left and top, so (-4, -4). This has more effect
3262 // on higher resolutions, so condition it on that for now.
3263 // Exclude quality layers, which have the same resolution and hence no shift.
3264 if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
3265 !svc->has_lower_quality_layer &&
3266 svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 &&
3267 cm->width * cm->height > 640 * 480) {
3268 svc_mv.as_mv.row = -4;
3269 svc_mv.as_mv.col = -4;
3270 }
3271
3272 // Setup parameters used for inter mode evaluation.
3273 set_params_nonrd_pick_inter_mode(cpi, x, &search_state, rd_cost,
3274 &force_skip_low_temp_var, mi_row, mi_col,
3275 gf_temporal_ref, segment_id, bsize
3276 #if CONFIG_AV1_TEMPORAL_DENOISING
3277 ,
3278 ctx, denoise_svc_pickmode
3279 #endif
3280 );
3281
3282 if (rt_sf->use_comp_ref_nonrd && is_comp_ref_allowed(bsize)) {
3283 // Only search compound if bsize \gt BLOCK_16X16.
3284 if (bsize > BLOCK_16X16) {
3285 comp_use_zero_zeromv_only = rt_sf->check_only_zero_zeromv_on_large_blocks;
3286 } else {
3287 tot_num_comp_modes = 0;
3288 }
3289 } else {
3290 tot_num_comp_modes = 0;
3291 }
3292
3293 if (x->pred_mv_sad[LAST_FRAME] != INT_MAX) {
3294 thresh_sad_pred = ((int64_t)x->pred_mv_sad[LAST_FRAME]) << 1;
3295 // Increase threshold for less aggressive pruning.
3296 if (rt_sf->nonrd_prune_ref_frame_search == 1)
3297 thresh_sad_pred += (x->pred_mv_sad[LAST_FRAME] >> 2);
3298 }
3299
3300 const int use_model_yrd_large = get_model_rd_flag(cpi, xd, bsize);
3301
3302 // decide block-level interp filter search flags:
3303 // filter_search_enabled_blk:
3304 // 0: disabled
3305 // 1: filter search depends on mode properties
3306 // 2: filter search forced since prediction is unreliable
3307 // cb_pred_filter_search 0: disabled cb prediction
3308 InterpFilter filt_select = EIGHTTAP_REGULAR;
3309 const int cb_pred_filter_search =
3310 x->content_state_sb.source_sad_nonrd > kVeryLowSad
3311 ? cpi->sf.interp_sf.cb_pred_filter_search
3312 : 0;
3313 const int filter_search_enabled_blk =
3314 is_filter_search_enabled_blk(cpi, x, mi_row, mi_col, bsize, segment_id,
3315 cb_pred_filter_search, &filt_select);
3316
3317 #if COLLECT_NONRD_PICK_MODE_STAT
3318 x->ms_stat_nonrd.num_blocks[bsize]++;
3319 #endif
3320 init_mbmi_nonrd(mi, DC_PRED, NONE_FRAME, NONE_FRAME, cm);
3321 mi->tx_size = AOMMIN(
3322 AOMMIN(max_txsize_lookup[bsize],
3323 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]),
3324 TX_16X16);
3325
3326 fill_single_inter_mode_costs(search_state.single_inter_mode_costs,
3327 num_inter_modes, ref_mode_set, mode_costs,
3328 mbmi_ext->mode_context);
3329
3330 MV_REFERENCE_FRAME last_comp_ref_frame = NONE_FRAME;
3331
3332 // Initialize inter prediction params at block level for single reference
3333 // mode.
3334 InterPredParams inter_pred_params_sr;
3335 init_inter_block_params(&inter_pred_params_sr, pd->width, pd->height,
3336 mi_row * MI_SIZE, mi_col * MI_SIZE, pd->subsampling_x,
3337 pd->subsampling_y, xd->bd, is_cur_buf_hbd(xd),
3338 /*is_intrabc=*/0);
3339 inter_pred_params_sr.conv_params =
3340 get_conv_params(/*do_average=*/0, AOM_PLANE_Y, xd->bd);
3341
3342 x->block_is_zero_sad = x->content_state_sb.source_sad_nonrd == kZeroSad ||
3343 segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
3344 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
3345 !x->force_zeromv_skip_for_blk &&
3346 x->content_state_sb.source_sad_nonrd != kZeroSad &&
3347 x->source_variance == 0 && bsize < cm->seq_params->sb_size &&
3348 search_state.yv12_mb[LAST_FRAME][0].width == cm->width &&
3349 search_state.yv12_mb[LAST_FRAME][0].height == cm->height) {
3350 set_block_source_sad(cpi, x, bsize, &search_state.yv12_mb[LAST_FRAME][0]);
3351 }
3352
3353 int sb_me_has_been_tested = 0;
3354 x->sb_me_block = x->sb_me_partition;
3355 // Only use this feature (force testing of superblock motion) if coding
3356 // block size is large.
3357 if (x->sb_me_block) {
3358 if (cm->seq_params->sb_size == BLOCK_128X128 && bsize < BLOCK_64X64)
3359 x->sb_me_block = 0;
3360 else if (cm->seq_params->sb_size == BLOCK_64X64 && bsize < BLOCK_32X32)
3361 x->sb_me_block = 0;
3362 }
3363
3364 x->min_dist_inter_uv = INT64_MAX;
3365 for (int idx = 0; idx < num_inter_modes + tot_num_comp_modes; ++idx) {
3366 // If we are at the first compound mode, and the single modes already
3367 // perform well, then end the search.
3368 if (rt_sf->skip_compound_based_on_var && idx == num_inter_modes &&
3369 skip_comp_based_on_var(search_state.vars, bsize)) {
3370 break;
3371 }
3372
3373 int is_single_pred = 1;
3374 PREDICTION_MODE this_mode;
3375
3376 if (idx == 0 && !x->force_zeromv_skip_for_blk) {
3377 // Set color sensitivity on first tested mode only.
3378 // Use y-sad already computed in find_predictors: take the sad with motion
3379 // vector closest to 0; the uv-sad computed below in set_color_sensitivity
3380 // is for zeromv.
3381 // For screen: first check if golden reference is being used, if so,
3382 // force color_sensitivity on (=1) if the color sensitivity for sb_g is 1.
3383 // The check in set_color_sensitivity() will then follow and check for
3384 // setting the flag if the level is still 2 or 0.
3385 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
3386 search_state.use_ref_frame_mask[GOLDEN_FRAME]) {
3387 if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1)
3388 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] = 1;
3389 if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
3390 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] = 1;
3391 }
3392 if (search_state.use_ref_frame_mask[LAST_FRAME] &&
3393 x->pred_mv0_sad[LAST_FRAME] != INT_MAX) {
3394 int y_sad = x->pred_mv0_sad[LAST_FRAME];
3395 if (x->pred_mv1_sad[LAST_FRAME] != INT_MAX &&
3396 (abs(search_state.frame_mv[NEARMV][LAST_FRAME].as_mv.col) +
3397 abs(search_state.frame_mv[NEARMV][LAST_FRAME].as_mv.row)) <
3398 (abs(search_state.frame_mv[NEARESTMV][LAST_FRAME].as_mv.col) +
3399 abs(search_state.frame_mv[NEARESTMV][LAST_FRAME].as_mv.row)))
3400 y_sad = x->pred_mv1_sad[LAST_FRAME];
3401 set_color_sensitivity(cpi, x, bsize, y_sad, x->source_variance,
3402 search_state.yv12_mb[LAST_FRAME]);
3403 }
3404 }
3405
3406 // Check the inter mode can be skipped based on mode statistics and speed
3407 // features settings.
3408 if (skip_inter_mode_nonrd(cpi, x, &search_state, &thresh_sad_pred,
3409 &force_mv_inter_layer, &is_single_pred,
3410 &this_mode, &last_comp_ref_frame, &ref_frame,
3411 &ref_frame2, idx, svc_mv, force_skip_low_temp_var,
3412 sse_zeromv_norm, num_inter_modes, segment_id,
3413 bsize, comp_use_zero_zeromv_only, check_globalmv))
3414 continue;
3415
3416 // Select prediction reference frames.
3417 for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
3418 xd->plane[plane].pre[0] = search_state.yv12_mb[ref_frame][plane];
3419 if (!is_single_pred)
3420 xd->plane[plane].pre[1] = search_state.yv12_mb[ref_frame2][plane];
3421 }
3422
3423 mi->ref_frame[0] = ref_frame;
3424 mi->ref_frame[1] = ref_frame2;
3425 set_ref_ptrs(cm, xd, ref_frame, ref_frame2);
3426
3427 // Check if the scaled reference frame should be used. This is set in the
3428 // find_predictors() for each usable reference. If so, set the
3429 // block_ref_scale_factors[] to no reference scaling.
3430 if (search_state.use_scaled_ref_frame[ref_frame]) {
3431 xd->block_ref_scale_factors[0] = &sf_no_scale;
3432 }
3433 if (!is_single_pred && search_state.use_scaled_ref_frame[ref_frame2]) {
3434 xd->block_ref_scale_factors[1] = &sf_no_scale;
3435 }
3436
3437 // Perform inter mode evaluation for non-rd
3438 if (!handle_inter_mode_nonrd(
3439 cpi, x, &search_state, ctx, &this_mode_pred, tmp_buffer,
3440 inter_pred_params_sr, &best_early_term, &sse_zeromv_norm,
3441 &check_globalmv,
3442 #if CONFIG_AV1_TEMPORAL_DENOISING
3443 &zero_last_cost_orig, denoise_svc_pickmode,
3444 #endif
3445 idx, force_mv_inter_layer, is_single_pred, gf_temporal_ref,
3446 use_model_yrd_large, filter_search_enabled_blk, bsize, this_mode,
3447 filt_select, cb_pred_filter_search, reuse_inter_pred,
3448 &sb_me_has_been_tested)) {
3449 break;
3450 }
3451 }
3452
3453 // Restore mode data of best inter mode
3454 mi->mode = best_pickmode->best_mode;
3455 mi->motion_mode = best_pickmode->best_motion_mode;
3456 mi->wm_params = best_pickmode->wm_params;
3457 mi->num_proj_ref = best_pickmode->num_proj_ref;
3458 mi->interp_filters = best_pickmode->best_pred_filter;
3459 mi->tx_size = best_pickmode->best_tx_size;
3460 memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size));
3461 mi->ref_frame[0] = best_pickmode->best_ref_frame;
3462 mi->mv[0].as_int = search_state
3463 .frame_mv_best[best_pickmode->best_mode]
3464 [best_pickmode->best_ref_frame]
3465 .as_int;
3466 mi->mv[1].as_int = 0;
3467 if (best_pickmode->best_second_ref_frame > INTRA_FRAME) {
3468 mi->ref_frame[1] = best_pickmode->best_second_ref_frame;
3469 mi->mv[1].as_int = search_state
3470 .frame_mv_best[best_pickmode->best_mode]
3471 [best_pickmode->best_second_ref_frame]
3472 .as_int;
3473 }
3474 // Perform intra prediction search, if the best SAD is above a certain
3475 // threshold.
3476 mi->angle_delta[PLANE_TYPE_Y] = 0;
3477 mi->angle_delta[PLANE_TYPE_UV] = 0;
3478 mi->filter_intra_mode_info.use_filter_intra = 0;
3479
3480 #if COLLECT_NONRD_PICK_MODE_STAT
3481 aom_usec_timer_start(&x->ms_stat_nonrd.timer1);
3482 x->ms_stat_nonrd.num_searches[bsize][DC_PRED]++;
3483 x->ms_stat_nonrd.num_nonskipped_searches[bsize][DC_PRED]++;
3484 #endif
3485
3486 int force_palette_test = 0;
3487 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
3488 x->content_state_sb.source_sad_nonrd != kZeroSad &&
3489 bsize <= BLOCK_16X16) {
3490 unsigned int thresh_sse = cpi->rc.high_source_sad ? 15000 : 200000;
3491 unsigned int thresh_source_var = cpi->rc.high_source_sad ? 50 : 200;
3492 unsigned int best_sse_inter_motion =
3493 (unsigned int)(search_state.best_rdc.sse >>
3494 (b_width_log2_lookup[bsize] +
3495 b_height_log2_lookup[bsize]));
3496 if (best_sse_inter_motion > thresh_sse &&
3497 x->source_variance > thresh_source_var)
3498 force_palette_test = 1;
3499 }
3500
3501 // Evaluate Intra modes in inter frame
3502 if (!x->force_zeromv_skip_for_blk)
3503 av1_estimate_intra_mode(cpi, x, bsize, best_early_term,
3504 search_state.ref_costs_single[INTRA_FRAME],
3505 reuse_inter_pred, &orig_dst, tmp_buffer,
3506 &this_mode_pred, &search_state.best_rdc,
3507 best_pickmode, ctx);
3508
3509 int skip_idtx_palette = (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3510 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) &&
3511 x->content_state_sb.source_sad_nonrd != kZeroSad &&
3512 !cpi->rc.high_source_sad;
3513
3514 bool try_palette = enable_palette(
3515 cpi, is_mode_intra(best_pickmode->best_mode), bsize, x->source_variance,
3516 x->force_zeromv_skip_for_blk, skip_idtx_palette, force_palette_test);
3517
3518 // Perform screen content mode evaluation for non-rd
3519 handle_screen_content_mode_nonrd(
3520 cpi, x, &search_state, this_mode_pred, ctx, tmp_buffer, &orig_dst,
3521 skip_idtx_palette, try_palette, bsize, reuse_inter_pred, mi_col, mi_row);
3522
3523 #if COLLECT_NONRD_PICK_MODE_STAT
3524 aom_usec_timer_mark(&x->ms_stat_nonrd.timer1);
3525 x->ms_stat_nonrd.nonskipped_search_times[bsize][DC_PRED] +=
3526 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer1);
3527 #endif
3528
3529 pd->dst = orig_dst;
3530 // Best mode is finalized. Restore the mode data to mbmi
3531 if (try_palette) mi->palette_mode_info = best_pickmode->pmi;
3532 mi->mode = best_pickmode->best_mode;
3533 mi->ref_frame[0] = best_pickmode->best_ref_frame;
3534 mi->ref_frame[1] = best_pickmode->best_second_ref_frame;
3535 // For lossless: always force the skip flags off.
3536 if (is_lossless_requested(&cpi->oxcf.rc_cfg)) {
3537 txfm_info->skip_txfm = 0;
3538 memset(ctx->blk_skip, 0, sizeof(ctx->blk_skip[0]) * ctx->num_4x4_blk);
3539 } else {
3540 txfm_info->skip_txfm = best_pickmode->best_mode_skip_txfm;
3541 }
3542 if (has_second_ref(mi)) {
3543 mi->comp_group_idx = 0;
3544 mi->compound_idx = 1;
3545 mi->interinter_comp.type = COMPOUND_AVERAGE;
3546 }
3547
3548 if (!is_inter_block(mi)) {
3549 mi->interp_filters = av1_broadcast_interp_filter(SWITCHABLE_FILTERS);
3550 } else {
3551 // If inter mode is selected and ref_frame was one that uses the
3552 // scaled reference frame, then we can't use reuse_inter_pred.
3553 if (search_state.use_scaled_ref_frame[best_pickmode->best_ref_frame] ||
3554 (has_second_ref(mi) &&
3555 search_state
3556 .use_scaled_ref_frame[best_pickmode->best_second_ref_frame]))
3557 x->reuse_inter_pred = 0;
3558 }
3559
3560 // Restore the predicted samples of best mode to final buffer
3561 if (reuse_inter_pred && best_pickmode->best_pred != NULL) {
3562 PRED_BUFFER *const best_pred = best_pickmode->best_pred;
3563 if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) {
3564 aom_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
3565 pd->dst.stride, bw, bh);
3566 }
3567 }
3568
3569 #if CONFIG_AV1_TEMPORAL_DENOISING
3570 if (cpi->oxcf.noise_sensitivity > 0 && resize_pending == 0 &&
3571 denoise_svc_pickmode && cpi->denoiser.denoising_level > kDenLowLow &&
3572 cpi->denoiser.reset == 0) {
3573 AV1_DENOISER_DECISION decision = COPY_BLOCK;
3574 ctx->sb_skip_denoising = 0;
3575 av1_pickmode_ctx_den_update(
3576 &ctx_den, zero_last_cost_orig, search_state.ref_costs_single,
3577 search_state.frame_mv, reuse_inter_pred, best_pickmode);
3578 av1_denoiser_denoise(cpi, x, mi_row, mi_col, bsize, ctx, &decision,
3579 gf_temporal_ref);
3580 if (denoise_recheck_zeromv)
3581 recheck_zeromv_after_denoising(
3582 cpi, mi, x, xd, decision, &ctx_den, search_state.yv12_mb,
3583 &search_state.best_rdc, best_pickmode, bsize, mi_row, mi_col);
3584 best_pickmode->best_ref_frame = ctx_den.best_ref_frame;
3585 }
3586 #endif
3587
3588 // Update the factors used for RD thresholding for all modes.
3589 if (cpi->sf.inter_sf.adaptive_rd_thresh && !has_second_ref(mi)) {
3590 THR_MODES best_mode_idx =
3591 mode_idx[best_pickmode->best_ref_frame][mode_offset(mi->mode)];
3592 if (best_pickmode->best_ref_frame == INTRA_FRAME) {
3593 // Only consider the modes that are included in the intra_mode_list.
3594 int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE);
3595 for (int mode_index = 0; mode_index < intra_modes; mode_index++) {
3596 update_thresh_freq_fact(cpi, x, bsize, INTRA_FRAME, best_mode_idx,
3597 intra_mode_list[mode_index]);
3598 }
3599 } else {
3600 PREDICTION_MODE this_mode;
3601 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
3602 update_thresh_freq_fact(cpi, x, bsize, best_pickmode->best_ref_frame,
3603 best_mode_idx, this_mode);
3604 }
3605 }
3606 }
3607
3608 #if CONFIG_INTERNAL_STATS
3609 store_coding_context_nonrd(x, ctx, mi->mode);
3610 #else
3611 store_coding_context_nonrd(x, ctx);
3612 #endif // CONFIG_INTERNAL_STATS
3613
3614 #if COLLECT_NONRD_PICK_MODE_STAT
3615 aom_usec_timer_mark(&x->ms_stat_nonrd.bsize_timer);
3616 x->ms_stat_nonrd.total_block_times[bsize] +=
3617 aom_usec_timer_elapsed(&x->ms_stat_nonrd.bsize_timer);
3618 print_time(&x->ms_stat_nonrd, bsize, cm->mi_params.mi_rows,
3619 cm->mi_params.mi_cols, mi_row, mi_col);
3620 #endif // COLLECT_NONRD_PICK_MODE_STAT
3621
3622 *rd_cost = search_state.best_rdc;
3623
3624 // Reset the xd->block_ref_scale_factors[i], as they may have
3625 // been set to pointer &sf_no_scale, which becomes invalid afer
3626 // this function.
3627 set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
3628 }
3629