blob: 7fdc546b2c8f85615d979093d43aa5ab3d9775b5 [file] [log] [blame]
kyslove3c05a82019-03-28 11:06:09 -07001/*
James Zernb7c05bd2024-06-11 19:15:10 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
kyslove3c05a82019-03-28 11:06:09 -07003 *
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
kyslove3c05a82019-03-28 11:06:09 -070018#include "av1/common/reconinter.h"
kyslov82449d12019-05-02 13:36:50 -070019#include "av1/common/reconintra.h"
kyslove3c05a82019-03-28 11:06:09 -070020
21#include "av1/encoder/encodemv.h"
chiyotsai901f4062022-07-06 14:57:53 -070022#include "av1/encoder/intra_mode_search.h"
23#include "av1/encoder/model_rd.h"
24#include "av1/encoder/motion_search_facade.h"
Yunqing Wang3135d772022-03-17 23:31:04 -070025#include "av1/encoder/nonrd_opt.h"
Cheng Chen407b9862024-05-08 15:25:17 -070026#include "av1/encoder/palette.h"
kyslove3c05a82019-03-28 11:06:09 -070027#include "av1/encoder/reconinter_enc.h"
Yunqing Wang61a75982021-10-13 12:01:11 -070028#include "av1/encoder/var_based_part.h"
kyslove3c05a82019-03-28 11:06:09 -070029
Aasaipriyaae4ecc22022-02-16 17:56:06 +053030static INLINE int early_term_inter_search_with_sse(int early_term_idx,
31 BLOCK_SIZE bsize,
32 int64_t this_sse,
Aasaipriya24dd26b2022-03-04 17:28:18 +053033 int64_t best_sse,
34 PREDICTION_MODE this_mode) {
Aasaipriyaae4ecc22022-02-16 17:56:06 +053035 // Aggressiveness to terminate inter mode search early is adjusted based on
36 // speed and block size.
Angie Chiang06c3fc62022-03-08 16:02:06 -080037 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 };
Aasaipriya24dd26b2022-03-04 17:28:18 +053043
Aasaipriyaae4ecc22022-02-16 17:56:06 +053044 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));
Aasaipriya24dd26b2022-03-04 17:28:18 +053047 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];
Aasaipriyaae4ecc22022-02-16 17:56:06 +053052
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
kyslove3c05a82019-03-28 11:06:09 -070060static INLINE void init_best_pickmode(BEST_PICKMODE *bp) {
Aasaipriyaae4ecc22022-02-16 17:56:06 +053061 bp->best_sse = INT64_MAX;
kyslove3c05a82019-03-28 11:06:09 -070062 bp->best_mode = NEARESTMV;
63 bp->best_ref_frame = LAST_FRAME;
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -080064 bp->best_second_ref_frame = NONE_FRAME;
kyslovbab94fd2019-05-03 11:25:17 -070065 bp->best_tx_size = TX_8X8;
Jingning Hanf90062a2022-02-25 11:37:38 -080066 bp->tx_type = DCT_DCT;
Sachin Kumar Garg22258532019-06-18 16:45:06 +053067 bp->best_pred_filter = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
kyslove3c05a82019-03-28 11:06:09 -070068 bp->best_mode_skip_txfm = 0;
Fyodor Kyslove0be17e2020-05-13 14:20:50 -070069 bp->best_mode_initial_skip_flag = 0;
kyslove3c05a82019-03-28 11:06:09 -070070 bp->best_pred = NULL;
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -070071 bp->best_motion_mode = SIMPLE_TRANSLATION;
72 bp->num_proj_ref = 0;
Anupam Pandeya080bf62022-12-15 16:52:25 +053073 av1_zero(bp->wm_params);
Anupam Pandeya080bf62022-12-15 16:52:25 +053074 av1_zero(bp->pmi);
kyslove3c05a82019-03-28 11:06:09 -070075}
76
Anupam Pandey73179372022-12-21 16:07:14 +053077// Copy best inter mode parameters to best_pickmode
78static 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;
Anupam Pandey73179372022-12-21 16:07:14 +053083
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) {
Marco Paniconid77e40b2023-05-15 14:02:06 -070097 memcpy(ctx->blk_skip, txfm_info->blk_skip,
Marco Paniconide6f57b2023-03-19 23:42:40 -070098 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
Anupam Pandey73179372022-12-21 16:07:14 +053099 }
100}
101
Neeraj Gadgil6ad85e82022-08-18 14:38:01 +0530102static INLINE int subpel_select(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
Neeraj Gadgil14b2f3f2022-08-29 09:42:17 +0530103 int_mv *mv, MV ref_mv, FULLPEL_MV start_mv,
104 bool fullpel_performed_well) {
Neeraj Gadgilc99499a2022-09-05 10:51:24 +0530105 const int frame_lowmotion = cpi->rc.avg_frame_low_motion;
Anupam Pandeyab370452022-12-14 11:23:34 +0530106 const int reduce_mv_pel_precision_highmotion =
107 cpi->sf.rt_sf.reduce_mv_pel_precision_highmotion;
108
Neeraj Gadgilc99499a2022-09-05 10:51:24 +0530109 // Reduce MV precision for higher int MV value & frame-level motion
Anupam Pandeyab370452022-12-14 11:23:34 +0530110 if (reduce_mv_pel_precision_highmotion >= 3) {
Neeraj Gadgil6ad85e82022-08-18 14:38:01 +0530111 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;
Neeraj Gadgilc99499a2022-09-05 10:51:24 +0530115 if (frame_lowmotion > 0 && frame_lowmotion < 40) mv_thresh = 12;
Neeraj Gadgil6ad85e82022-08-18 14:38:01 +0530116 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;
Anupam Pandeyab370452022-12-14 11:23:34 +0530120 } else if (reduce_mv_pel_precision_highmotion >= 1) {
Neeraj Gadgilc99499a2022-09-05 10:51:24 +0530121 int mv_thresh;
Anupam Pandeya5356dd2022-09-20 18:34:19 +0530122 const int th_vals[2][3] = { { 4, 8, 10 }, { 4, 6, 8 } };
Anupam Pandeyab370452022-12-14 11:23:34 +0530123 const int th_idx = reduce_mv_pel_precision_highmotion - 1;
Anupam Pandeya5356dd2022-09-20 18:34:19 +0530124 assert(th_idx >= 0 && th_idx < 2);
Neeraj Gadgilc99499a2022-09-05 10:51:24 +0530125 if (frame_lowmotion > 0 && frame_lowmotion < 40)
126 mv_thresh = 12;
127 else
Anupam Pandeya5356dd2022-09-20 18:34:19 +0530128 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];
Neeraj Gadgilc99499a2022-09-05 10:51:24 +0530131 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;
Neeraj Gadgil14b2f3f2022-08-29 09:42:17 +0530137 }
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) {
Neeraj Gadgil6ad85e82022-08-18 14:38:01 +0530141 const int qband = x->qindex >> (QINDEX_BITS - 2);
142 assert(qband < 4);
Neeraj Gadgil710cc542022-08-22 12:05:25 +0530143 if (x->content_state_sb.source_sad_nonrd <= kVeryLowSad &&
Neeraj Gadgil6ad85e82022-08-18 14:38:01 +0530144 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 }
Neeraj Gadgil14b2f3f2022-08-29 09:42:17 +0530150 } 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;
Neeraj Gadgil6ad85e82022-08-18 14:38:01 +0530154 }
155 return cpi->sf.mv_sf.subpel_force_stop;
Marco Paniconi59a7a0d2021-11-04 00:09:01 -0700156}
157
Yunqing Wangddc0bf92022-12-15 11:17:40 -0800158static bool use_aggressive_subpel_search_method(MACROBLOCK *x,
159 bool use_adaptive_subpel_search,
160 bool fullpel_performed_well) {
Neeraj Gadgil98c3e0d2022-10-13 12:22:42 +0530161 if (!use_adaptive_subpel_search) return false;
Anupam Pandey4a8ade22022-10-11 17:57:27 +0530162 const int qband = x->qindex >> (QINDEX_BITS - 2);
163 assert(qband < 4);
Neeraj Gadgil98c3e0d2022-10-13 12:22:42 +0530164 if ((qband > 0) && (fullpel_performed_well ||
165 (x->content_state_sb.source_sad_nonrd <= kLowSad) ||
166 (x->source_variance < 100)))
Anupam Pandey4a8ade22022-10-11 17:57:27 +0530167 return true;
James Zernfa7042e2022-10-27 18:25:57 -0700168 return false;
Anupam Pandey4a8ade22022-10-11 17:57:27 +0530169}
170
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -0700171/*!\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
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -0700183 * \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 */
kyslove3c05a82019-03-28 11:06:09 -0700193static int combined_motion_search(AV1_COMP *cpi, MACROBLOCK *x,
Marco Paniconia1f29682023-09-17 20:09:51 -0700194 BLOCK_SIZE bsize, int_mv *tmp_mv,
195 int *rate_mv, int64_t best_rd_sofar,
196 int use_base_mv) {
kyslove3c05a82019-03-28 11:06:09 -0700197 MACROBLOCKD *xd = &x->e_mbd;
198 const AV1_COMMON *cm = &cpi->common;
chiyotsai7addef92022-08-25 12:12:06 -0700199 const SPEED_FEATURES *sf = &cpi->sf;
kyslove3c05a82019-03-28 11:06:09 -0700200 MB_MODE_INFO *mi = xd->mi[0];
chiyotsai7addef92022-08-25 12:12:06 -0700201 int step_param = (sf->rt_sf.fullpel_search_step_param)
202 ? sf->rt_sf.fullpel_search_step_param
Marco Paniconid182da42020-07-28 09:05:43 -0700203 : cpi->mv_search_params.mv_step_param;
chiyotsaie46cff72020-02-05 15:03:34 -0800204 FULLPEL_MV start_mv;
kyslove3c05a82019-03-28 11:06:09 -0700205 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;
kyslove3c05a82019-03-28 11:06:09 -0700209 int rv = 0;
210 int cost_list[5];
211 int search_subpel = 1;
kyslove3c05a82019-03-28 11:06:09 -0700212
chiyotsaie46cff72020-02-05 15:03:34 -0800213 start_mv = get_fullmv_from_mv(&ref_mv);
kyslove3c05a82019-03-28 11:06:09 -0700214
215 if (!use_base_mv)
216 center_mv = ref_mv;
217 else
218 center_mv = tmp_mv->as_mv;
chiyotsaifbebe9d2022-05-23 13:31:41 -0700219
chiyotsaif7acaa72023-07-24 11:10:40 -0700220 const SEARCH_METHODS search_method =
221 av1_get_default_mv_search_method(x, &cpi->sf.mv_sf, bsize);
chiyotsai945edd62023-01-24 15:50:09 -0800222 const search_site_config *src_search_sites =
223 av1_get_search_site_config(cpi, x, search_method);
chiyotsai2e42a662020-02-26 17:39:03 -0800224 FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
Deepa K Gdc6842e2023-06-07 15:44:56 +0530225 FULLPEL_MV_STATS best_mv_stats;
chiyotsai7430da62020-07-27 14:06:26 -0700226 av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, &center_mv,
chiyotsaif7acaa72023-07-24 11:10:40 -0700227 start_mv, src_search_sites, search_method,
chiyotsai7430da62020-07-27 14:06:26 -0700228 /*fine_search_interval=*/0);
chiyotsai2e42a662020-02-26 17:39:03 -0800229
chiyotsai271d9542022-04-13 14:47:06 -0700230 const unsigned int full_var_rd = av1_full_pixel_search(
231 start_mv, &full_ms_params, step_param, cond_cost_list(cpi, cost_list),
Deepa K Gdc6842e2023-06-07 15:44:56 +0530232 &tmp_mv->as_fullmv, &best_mv_stats, NULL);
kyslove3c05a82019-03-28 11:06:09 -0700233
kyslove3c05a82019-03-28 11:06:09 -0700234 // calculate the bit cost on motion vector
chiyotsai87bb8052020-02-12 16:56:33 -0800235 MV mvp_full = get_mv_from_fullmv(&tmp_mv->as_fullmv);
kyslove3c05a82019-03-28 11:06:09 -0700236
Fyodor Kyslov648c6502021-02-02 18:41:10 -0800237 *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);
kyslove3c05a82019-03-28 11:06:09 -0700239
240 // TODO(kyslov) Account for Rate Mode!
241 rv = !(RDCOST(x->rdmult, (*rate_mv), 0) > best_rd_sofar);
242
243 if (rv && search_subpel) {
chiyotsai2aac3002020-02-13 17:02:01 -0800244 SUBPEL_MOTION_SEARCH_PARAMS ms_params;
245 av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
chiyotsai4d1b7d92020-04-07 13:56:22 -0700246 cost_list);
chiyotsai7addef92022-08-25 12:12:06 -0700247 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);
Neeraj Gadgil14b2f3f2022-08-29 09:42:17 +0530251 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);
chiyotsai271d9542022-04-13 14:47:06 -0700255
chiyotsaie4d0d852020-04-07 15:21:09 -0700256 MV subpel_start_mv = get_mv_from_fullmv(&tmp_mv->as_fullmv);
Rachel Barkerfa59a652023-02-16 19:58:56 +0000257 assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, subpel_start_mv));
Anupam Pandey4a8ade22022-10-11 17:57:27 +0530258 // adaptively downgrade subpel search method based on block properties
259 if (use_aggressive_subpel_search_method(
Neeraj Gadgil98c3e0d2022-10-13 12:22:42 +0530260 x, sf->rt_sf.use_adaptive_subpel_search, fullpel_performed_well))
Deepa K Gdc6842e2023-06-07 15:44:56 +0530261 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);
Anupam Pandey4a8ade22022-10-11 17:57:27 +0530264 else
chiyotsai7addef92022-08-25 12:12:06 -0700265 cpi->mv_search_params.find_fractional_mv_step(
Deepa K Gdc6842e2023-06-07 15:44:56 +0530266 xd, cm, &ms_params, subpel_start_mv, &best_mv_stats, &tmp_mv->as_mv,
267 &dis, &x->pred_sse[ref], NULL);
chiyotsaic95e3642020-04-10 13:17:06 -0700268 *rate_mv =
Fyodor Kyslov648c6502021-02-02 18:41:10 -0800269 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);
kyslove3c05a82019-03-28 11:06:09 -0700271 }
James Zern286e07b2022-09-29 15:44:58 -0700272 // 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.
Fyodor Kyslov29430902020-11-18 13:44:30 -0800274 rv = (tmp_mv->as_mv.col != ref_mv.col || tmp_mv->as_mv.row != ref_mv.row);
kyslove3c05a82019-03-28 11:06:09 -0700275 return rv;
276}
277
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -0700278/*!\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
Wan-Teh Changfbff8a32022-03-31 18:43:49 -0700291 * \param[in] ref_frame Reference frame for which to find
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -0700292 * 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 */
kyslove3c05a82019-03-28 11:06:09 -0700306static 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,
Fyodor Kyslovf5c96902020-04-27 17:08:56 -0700309 BLOCK_SIZE bsize, int mi_row, int mi_col, int *rate_mv,
310 RD_STATS *best_rdc) {
kyslove3c05a82019-03-28 11:06:09 -0700311 MACROBLOCKD *const xd = &x->e_mbd;
312 MB_MODE_INFO *const mi = xd->mi[0];
313 AV1_COMMON *cm = &cpi->common;
Anupam Pandeyab370452022-12-14 11:23:34 +0530314 int_mv *this_ref_frm_newmv = &frame_mv[NEWMV][ref_frame];
Marco Paniconi5a4ea092023-08-09 14:36:15 -0700315 unsigned int y_sad_zero;
Vishesh39e74092020-06-16 17:13:48 +0530316 if (ref_frame > LAST_FRAME && cpi->oxcf.rc_cfg.mode == AOM_CBR &&
Fyodor Kyslovf5c96902020-04-27 17:08:56 -0700317 gf_temporal_ref) {
kyslove3c05a82019-03-28 11:06:09 -0700318 int tmp_sad;
319 int dis;
kyslove3c05a82019-03-28 11:06:09 -0700320
321 if (bsize < BLOCK_16X16) return -1;
322
Marco Paniconi83cb7a52023-08-26 20:49:53 -0700323 int me_search_size_col = block_size_wide[bsize] >> 1;
324 int me_search_size_row = block_size_high[bsize] >> 1;
kyslove3c05a82019-03-28 11:06:09 -0700325 tmp_sad = av1_int_pro_motion_estimation(
326 cpi, x, bsize, mi_row, mi_col,
Marco Paniconi83cb7a52023-08-26 20:49:53 -0700327 &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);
kyslove3c05a82019-03-28 11:06:09 -0700329
330 if (tmp_sad > x->pred_mv_sad[LAST_FRAME]) return -1;
kyslove3c05a82019-03-28 11:06:09 -0700331
Anupam Pandeyab370452022-12-14 11:23:34 +0530332 this_ref_frm_newmv->as_int = mi->mv[0].as_int;
chiyotsaie4d0d852020-04-07 15:21:09 -0700333 int_mv best_mv = mi->mv[0];
334 best_mv.as_mv.row >>= 3;
335 best_mv.as_mv.col >>= 3;
kyslove3c05a82019-03-28 11:06:09 -0700336 MV ref_mv = av1_get_ref_mv(x, 0).as_mv;
Anupam Pandeyab370452022-12-14 11:23:34 +0530337 this_ref_frm_newmv->as_mv.row >>= 3;
338 this_ref_frm_newmv->as_mv.col >>= 3;
kyslove3c05a82019-03-28 11:06:09 -0700339
chiyotsai2aac3002020-02-13 17:02:01 -0800340 SUBPEL_MOTION_SEARCH_PARAMS ms_params;
chiyotsai94f491f2022-03-22 15:28:43 -0700341 av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv, NULL);
Neeraj Gadgil14b2f3f2022-08-29 09:42:17 +0530342 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 }
chiyotsaie4d0d852020-04-07 15:21:09 -0700348 MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
Rachel Barkerfa59a652023-02-16 19:58:56 +0000349 assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, start_mv));
Nithya V Sace29f32020-04-07 16:15:12 +0530350 cpi->mv_search_params.find_fractional_mv_step(
Deepa K Gdc6842e2023-06-07 15:44:56 +0530351 xd, cm, &ms_params, start_mv, NULL, &best_mv.as_mv, &dis,
Nithya V Sace29f32020-04-07 16:15:12 +0530352 &x->pred_sse[ref_frame], NULL);
Anupam Pandeyab370452022-12-14 11:23:34 +0530353 this_ref_frm_newmv->as_int = best_mv.as_int;
chiyotsai94f491f2022-03-22 15:28:43 -0700354
Neeraj Gadgil90ab84e2022-11-08 17:13:14 +0530355 // 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.
Anupam Pandeyab370452022-12-14 11:23:34 +0530359 if (this_ref_frm_newmv->as_mv.col == ref_mv.col &&
360 this_ref_frm_newmv->as_mv.row == ref_mv.row)
Neeraj Gadgil90ab84e2022-11-08 17:13:14 +0530361 return -1;
362
Anupam Pandeyab370452022-12-14 11:23:34 +0530363 *rate_mv = av1_mv_bit_cost(&this_ref_frm_newmv->as_mv, &ref_mv,
chiyotsai94f491f2022-03-22 15:28:43 -0700364 x->mv_costs->nmv_joint_cost,
365 x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
Marco Paniconia1f29682023-09-17 20:09:51 -0700366 } else if (!combined_motion_search(cpi, x, bsize, &frame_mv[NEWMV][ref_frame],
367 rate_mv, best_rdc->rdcost, 0)) {
kyslove3c05a82019-03-28 11:06:09 -0700368 return -1;
369 }
370
371 return 0;
372}
373
Jerome Jiang906a94f2019-05-01 19:02:58 -0700374static void estimate_single_ref_frame_costs(const AV1_COMMON *cm,
375 const MACROBLOCKD *xd,
chiyotsai9a06d182020-05-01 17:12:12 -0700376 const ModeCosts *mode_costs,
chiyotsai6d5197a2022-07-26 14:28:30 -0700377 int segment_id, BLOCK_SIZE bsize,
Jerome Jiang906a94f2019-05-01 19:02:58 -0700378 unsigned int *ref_costs_single) {
kyslove3c05a82019-03-28 11:06:09 -0700379 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));
kyslove3c05a82019-03-28 11:06:09 -0700383 } else {
384 int intra_inter_ctx = av1_get_intra_inter_context(xd);
chiyotsai9a06d182020-05-01 17:12:12 -0700385 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];
chiyotsai6d5197a2022-07-26 14:28:30 -0700388 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 }
Marco Paniconi62b66dd2020-08-27 11:30:37 -0700393 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];
kyslove3c05a82019-03-28 11:06:09 -0700402 }
403}
404
Anupam Pandey65c7e1b2022-11-23 12:52:52 +0530405static 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) {
Anupam Pandey45caf952022-12-15 11:16:15 +0530411 const int qstep = x->plane[AOM_PLANE_Y].dequant_QTX[1] >> (x->e_mbd.bd - 5);
Anupam Pandey65c7e1b2022-11-23 12:52:52 +0530412 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 &&
Anupam Pandey45caf952022-12-15 11:16:15 +0530419 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
420 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 0)
Anupam Pandey65c7e1b2022-11-23 12:52:52 +0530421 *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
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700429static TX_SIZE calculate_tx_size(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
Cherma Rajan A3ed030f2019-08-16 18:56:00 +0530430 MACROBLOCK *const x, unsigned int var,
Nithya V S58795222022-03-30 14:34:52 +0530431 unsigned int sse, int *force_skip) {
Cherma Rajan A3ed030f2019-08-16 18:56:00 +0530432 MACROBLOCKD *const xd = &x->e_mbd;
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700433 TX_SIZE tx_size;
chiyotsaia36d9002020-04-29 16:48:21 -0700434 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
435 if (txfm_params->tx_mode_search_type == TX_MODE_SELECT) {
Nithya V S58795222022-03-30 14:34:52 +0530436 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];
Anupam Pandey45caf952022-12-15 11:16:15 +0530445 const int qstep = x->plane[AOM_PLANE_Y].dequant_QTX[1] >> (xd->bd - 5);
Nithya V S58795222022-03-30 14:34:52 +0530446 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 &&
Anupam Pandey45caf952022-12-15 11:16:15 +0530455 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
456 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 0)
Nithya V S58795222022-03-30 14:34:52 +0530457 *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))
chiyotsaia36d9002020-04-29 16:48:21 -0700466 tx_size =
467 AOMMIN(max_txsize_lookup[bsize],
468 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700469 else
470 tx_size = TX_8X8;
471
Vishesh734eff92020-06-20 21:46:36 +0530472 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
Nithya V S58795222022-03-30 14:34:52 +0530473 cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) && is_high_var)
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700474 tx_size = TX_8X8;
475 else if (tx_size > TX_16X16)
476 tx_size = TX_16X16;
477 } else {
chiyotsaia36d9002020-04-29 16:48:21 -0700478 tx_size =
479 AOMMIN(max_txsize_lookup[bsize],
480 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700481 }
Jerome Jiangd4e351d2020-01-30 15:13:11 -0800482
Anupam Pandey65c7e1b2022-11-23 12:52:52 +0530483 if (CAP_TX_SIZE_FOR_BSIZE_GT32(txfm_params->tx_mode_search_type, bsize))
484 tx_size = TX_SIZE_FOR_BSIZE_GT32;
Jerome Jiangd4e351d2020-01-30 15:13:11 -0800485
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700486 return AOMMIN(tx_size, TX_16X16);
487}
488
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700489static 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) {
venkat sanampudicc538222022-03-07 12:57:37 +0530493 int k = 0;
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700494 *sse = 0;
495 *sum = 0;
496
venkat sanampudicc538222022-03-07 12:57:37 +0530497 // This function is called for block sizes >= BLOCK_32x32. As per the design
Diksha Singh369372e2022-10-21 07:58:27 +0530498 // 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.
venkat sanampudicc538222022-03-07 12:57:37 +0530501 assert(w >= 32);
502 assert(h >= 8);
Anupam Pandeycbe4eb42022-12-21 12:44:47 +0530503 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]);
venkat sanampudicc538222022-03-07 12:57:37 +0530509 k += 4;
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700510 }
511 }
512}
513
Anupam Pandey65c7e1b2022-11-23 12:52:52 +0530514static 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);
Anupam Pandeycbe4eb42022-12-21 12:44:47 +0530528 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,
Anupam Pandey65c7e1b2022-11-23 12:52:52 +0530532 &sse16x16[k], sse, sum, &var16x16[k]);
533 k += 2;
534 }
535 }
536}
537
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700538static 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]);
Anupam Pandeycbe4eb42022-12-21 12:44:47 +0530545 int row, col, k = 0;
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700546
Anupam Pandeycbe4eb42022-12-21 12:44:47 +0530547 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];
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700553 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
Yunqing Wangddc0bf92022-12-15 11:17:40 -0800562static int ac_thr_factor(int speed, int width, int height, int norm_sum) {
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700563 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
Anupam Pandey93048ff2022-11-23 11:16:51 +0530572// Sets early_term flag based on chroma planes prediction
573static 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;
Anupam Pandey45caf952022-12-15 11:16:15 +0530578 struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
Anupam Pandey93048ff2022-11-23 11:16:51 +0530579 const uint32_t dc_quant = p->dequant_QTX[0];
580 const uint32_t ac_quant = p->dequant_QTX[1];
Marco Paniconi0152ba92023-10-03 14:51:56 -0700581 int64_t dc_thr = dc_quant * dc_quant >> 6;
Anupam Pandey93048ff2022-11-23 11:16:51 +0530582 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
Marco Paniconi0152ba92023-10-03 14:51:56 -0700601 if (cpi->sf.rt_sf.increase_source_sad_thresh) {
602 dc_thr = dc_thr << 1;
603 ac_thr = ac_thr << 2;
604 }
605
Anupam Pandey93048ff2022-11-23 11:16:51 +0530606 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.
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +0530625 for (int plane = AOM_PLANE_U; plane <= AOM_PLANE_V; plane++) {
626 int j = plane - 1;
Anupam Pandey93048ff2022-11-23 11:16:51 +0530627 skip_uv[j] = 1;
Anupam Pandey8b325e72022-12-22 01:51:39 +0530628 if (x->color_sensitivity[COLOR_SENS_IDX(plane)]) {
Anupam Pandey93048ff2022-11-23 11:16:51 +0530629 skip_uv[j] = 0;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +0530630 struct macroblock_plane *const puv = &x->plane[plane];
631 struct macroblockd_plane *const puvd = &xd->plane[plane];
Anupam Pandey93048ff2022-11-23 11:16:51 +0530632 const BLOCK_SIZE uv_bsize = get_plane_block_size(
633 bsize, puvd->subsampling_x, puvd->subsampling_y);
634 // Adjust these thresholds for UV.
Marco Paniconi0152ba92023-10-03 14:51:56 -0700635 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;
Anupam Pandey93048ff2022-11-23 11:16:51 +0530637 const int64_t uv_dc_thr =
Marco Paniconi0152ba92023-10-03 14:51:56 -0700638 (puv->dequant_QTX[0] * puv->dequant_QTX[0]) >> shift_dc;
Anupam Pandey93048ff2022-11-23 11:16:51 +0530639 const int64_t uv_ac_thr =
Marco Paniconi0152ba92023-10-03 14:51:56 -0700640 (puv->dequant_QTX[1] * puv->dequant_QTX[1]) >> shift_ac;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +0530641 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
642 plane, plane);
Anupam Pandey93048ff2022-11-23 11:16:51 +0530643 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
659static 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
Anupam Pandey65c7e1b2022-11-23 12:52:52 +0530680static 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;
Anupam Pandey45caf952022-12-15 11:16:15 +0530691 struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
692 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
Anupam Pandey65c7e1b2022-11-23 12:52:52 +0530693 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
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700745static void model_skip_for_sb_y_large(AV1_COMP *cpi, BLOCK_SIZE bsize,
Marco Paniconi4d932222019-11-03 21:41:46 -0800746 int mi_row, int mi_col, MACROBLOCK *x,
Fyodor Kyslove8692412020-05-04 17:47:13 -0700747 MACROBLOCKD *xd, RD_STATS *rd_stats,
Marco Paniconi493cf672022-02-25 00:09:45 -0800748 int *early_term, int calculate_rd,
chiyotsaia1fa8772022-06-22 16:57:59 -0700749 int64_t best_sse,
750 unsigned int *var_output,
751 unsigned int var_prune_threshold) {
Ranjit Kumar Tulabanducd4a3072022-09-02 15:53:57 +0530752 if (x->force_zeromv_skip_for_blk) {
Marco Paniconi7ae16352022-03-08 00:19:24 -0800753 *early_term = 1;
754 rd_stats->rate = 0;
755 rd_stats->dist = 0;
756 rd_stats->sse = 0;
757 return;
758 }
759
Anupam Pandey65c7e1b2022-11-23 12:52:52 +0530760 // 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
Anupam Pandey93048ff2022-11-23 11:16:51 +0530773 // 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;
Anupam Pandey45caf952022-12-15 11:16:15 +0530777 struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
778 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
Anupam Pandey93048ff2022-11-23 11:16:51 +0530779 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
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700790 // 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));
chiyotsaia1fa8772022-06-22 16:57:59 -0700795 if (var_output) {
796 *var_output = var;
797 if (*var_output > var_prune_threshold) {
798 return;
799 }
800 }
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700801
Fyodor Kyslove8692412020-05-04 17:47:13 -0700802 rd_stats->sse = sse;
Nithya V S58795222022-03-30 14:34:52 +0530803 // Skipping test
804 *early_term = 0;
805 tx_size = calculate_tx_size(cpi, bsize, x, var, sse, early_term);
Anupam Pandey93048ff2022-11-23 11:16:51 +0530806 assert(tx_size <= TX_16X16);
James Zern286e07b2022-09-29 15:44:58 -0700807 // The code below for setting skip flag assumes transform size of at least
808 // 8x8, so force this lower limit on transform.
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700809 if (tx_size < TX_8X8) tx_size = TX_8X8;
810 xd->mi[0]->tx_size = tx_size;
811
Aasaipriya24dd26b2022-03-04 17:28:18 +0530812 MB_MODE_INFO *const mi = xd->mi[0];
Marco Paniconi493cf672022-02-25 00:09:45 -0800813 if (!calculate_rd && cpi->sf.rt_sf.sse_early_term_inter_search &&
814 early_term_inter_search_with_sse(
Aasaipriya24dd26b2022-03-04 17:28:18 +0530815 cpi->sf.rt_sf.sse_early_term_inter_search, bsize, sse, best_sse,
816 mi->mode))
Marco Paniconi493cf672022-02-25 00:09:45 -0800817 test_skip = 0;
818
Nithya V S58795222022-03-30 14:34:52 +0530819 if (*early_term) test_skip = 0;
820
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700821 // Evaluate if the partition block is a skippable block in Y plane.
Marco Paniconi493cf672022-02-25 00:09:45 -0800822 if (test_skip) {
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700823 unsigned int sse16x16[64] = { 0 };
824 int sum16x16[64] = { 0 };
825 unsigned int var16x16[64] = { 0 };
Anupam Pandey93048ff2022-11-23 11:16:51 +0530826 const unsigned int *sse_tx = sse8x8;
827 const unsigned int *var_tx = var8x8;
828 unsigned int num_blks = 1 << (bw + bh - 2);
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700829
Anupam Pandey93048ff2022-11-23 11:16:51 +0530830 if (tx_size >= TX_16X16) {
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700831 calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16,
832 sum16x16);
Anupam Pandey93048ff2022-11-23 11:16:51 +0530833 sse_tx = sse16x16;
834 var_tx = var16x16;
835 num_blks = num_blks >> 2;
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700836 }
Anupam Pandey93048ff2022-11-23 11:16:51 +0530837 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);
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700840 }
Anupam Pandey93048ff2022-11-23 11:16:51 +0530841 calc_rate_dist_block_param(cpi, x, rd_stats, calculate_rd, early_term, bsize,
842 sse);
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700843}
844
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700845static void model_rd_for_sb_y(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
Fyodor Kyslove8692412020-05-04 17:47:13 -0700846 MACROBLOCK *x, MACROBLOCKD *xd,
chiyotsai1538b072022-07-26 10:25:14 -0700847 RD_STATS *rd_stats, unsigned int *var_out,
Ranjit Kumar Tulabanducd4a3072022-09-02 15:53:57 +0530848 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
kyslove3c05a82019-03-28 11:06:09 -0700856 // 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.
kyslove3c05a82019-03-28 11:06:09 -0700859 const int ref = xd->mi[0]->ref_frame[0];
860
kyslove7ff3b62019-04-05 14:15:03 -0700861 assert(bsize < BLOCK_SIZES_ALL);
kyslove3c05a82019-03-28 11:06:09 -0700862
Anupam Pandey45caf952022-12-15 11:16:15 +0530863 struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y];
864 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700865 unsigned int sse;
866 int rate;
867 int64_t dist;
kyslove3c05a82019-03-28 11:06:09 -0700868
Mufaddal Chakera16796272021-02-24 14:03:29 +0530869 unsigned int var = cpi->ppi->fn_ptr[bsize].vf(
870 p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, &sse);
Nithya V S58795222022-03-30 14:34:52 +0530871 int force_skip = 0;
872 xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, x, var, sse, &force_skip);
chiyotsai1538b072022-07-26 10:25:14 -0700873 if (var_out) {
874 *var_out = var;
875 }
kyslove3c05a82019-03-28 11:06:09 -0700876
Nithya V S58795222022-03-30 14:34:52 +0530877 if (calculate_rd && (!force_skip || ref == INTRA_FRAME)) {
Fyodor Kyslov1dc01022019-07-02 16:47:44 -0700878 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 {
Anupam Pandeyd68ec612023-01-03 14:15:58 +0530883 rate = INT_MAX; // this will be overwritten later with av1_block_yrd
Fyodor Kyslov1dc01022019-07-02 16:47:44 -0700884 dist = INT_MAX;
885 }
Fyodor Kyslove8692412020-05-04 17:47:13 -0700886 rd_stats->sse = sse;
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700887 x->pred_sse[ref] = (unsigned int)AOMMIN(sse, UINT_MAX);
kyslove3c05a82019-03-28 11:06:09 -0700888
Nithya V S58795222022-03-30 14:34:52 +0530889 if (force_skip && ref > INTRA_FRAME) {
890 rate = 0;
891 dist = (int64_t)sse << 4;
892 }
893
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700894 assert(rate >= 0);
kyslove3c05a82019-03-28 11:06:09 -0700895
Fyodor Kyslove8692412020-05-04 17:47:13 -0700896 rd_stats->skip_txfm = (rate == 0);
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700897 rate = AOMMIN(rate, INT_MAX);
Fyodor Kyslove8692412020-05-04 17:47:13 -0700898 rd_stats->rate = rate;
899 rd_stats->dist = dist;
kyslove3c05a82019-03-28 11:06:09 -0700900}
901
Yunqing Wangddc0bf92022-12-15 11:17:40 -0800902static INLINE int get_drl_cost(PREDICTION_MODE this_mode, int ref_mv_idx,
chiyotsai901f4062022-07-06 14:57:53 -0700903 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
chiyotsai9a06d182020-05-01 17:12:12 -0700931static int cost_mv_ref(const ModeCosts *const mode_costs, PREDICTION_MODE mode,
kyslove3c05a82019-03-28 11:06:09 -0700932 int16_t mode_context) {
933 if (is_inter_compound_mode(mode)) {
chiyotsai9a06d182020-05-01 17:12:12 -0700934 return mode_costs
kyslove3c05a82019-03-28 11:06:09 -0700935 ->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) {
chiyotsai9a06d182020-05-01 17:12:12 -0700944 mode_cost = mode_costs->newmv_mode_cost[mode_ctx][0];
kyslove3c05a82019-03-28 11:06:09 -0700945 return mode_cost;
946 } else {
chiyotsai9a06d182020-05-01 17:12:12 -0700947 mode_cost = mode_costs->newmv_mode_cost[mode_ctx][1];
kyslove3c05a82019-03-28 11:06:09 -0700948 mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
949
950 if (mode == GLOBALMV) {
chiyotsai9a06d182020-05-01 17:12:12 -0700951 mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][0];
kyslove3c05a82019-03-28 11:06:09 -0700952 return mode_cost;
953 } else {
chiyotsai9a06d182020-05-01 17:12:12 -0700954 mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][1];
kyslove3c05a82019-03-28 11:06:09 -0700955 mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
chiyotsai9a06d182020-05-01 17:12:12 -0700956 mode_cost += mode_costs->refmv_mode_cost[mode_ctx][mode != NEARESTMV];
kyslove3c05a82019-03-28 11:06:09 -0700957 return mode_cost;
958 }
959 }
960}
961
962static void newmv_diff_bias(MACROBLOCKD *xd, PREDICTION_MODE this_mode,
963 RD_STATS *this_rdc, BLOCK_SIZE bsize, int mv_row,
Marco Paniconi221b8992020-10-01 11:14:41 -0700964 int mv_col, int speed, uint32_t spatial_variance,
Marco Paniconi988b34a2020-11-09 12:41:13 -0800965 CONTENT_STATE_SB content_state_sb) {
kyslove3c05a82019-03-28 11:06:09 -0700966 // 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;
kyslove3c05a82019-03-28 11:06:09 -0700971 int row_diff, col_diff;
972 int above_mv_valid = 0;
973 int left_mv_valid = 0;
Wan-Teh Changc739f212021-10-18 15:57:11 -0700974 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;
Neeraj Gadgil710cc542022-08-22 12:05:25 +0530976 if (bsize >= BLOCK_64X64 && content_state_sb.source_sad_nonrd != kHighSad &&
Marco Paniconi221b8992020-10-01 11:14:41 -0700977 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 }
kyslove3c05a82019-03-28 11:06:09 -0700982 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 }
Jerome Jiangc480d822019-05-01 18:30:37 -07001004 row_diff = al_mv_average_row - mv_row;
1005 col_diff = al_mv_average_col - mv_col;
Fyodor Kyslov990da972019-11-15 11:15:38 -08001006 if (row_diff > 80 || row_diff < -80 || col_diff > 80 || col_diff < -80) {
1007 if (bsize >= BLOCK_32X32)
kyslove3c05a82019-03-28 11:06:09 -07001008 this_rdc->rdcost = this_rdc->rdcost << 1;
1009 else
1010 this_rdc->rdcost = 5 * this_rdc->rdcost >> 2;
1011 }
Marco Paniconi6a966f12020-02-13 12:14:28 -08001012 } 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;
kyslove3c05a82019-03-28 11:06:09 -07001017 }
kyslove3c05a82019-03-28 11:06:09 -07001018}
1019
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07001020static 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) {
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08001025 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) {
Fyodor Kyslov29430902020-11-18 13:44:30 -08001029 int *freq_fact = &x->thresh_freq_fact[bs][thr_mode_idx];
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08001030 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 }
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07001037 }
1038}
1039
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08001040#if CONFIG_AV1_TEMPORAL_DENOISING
1041static 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
1057static 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
James Zern286e07b2022-09-29 15:44:58 -07001064 // ZEROMV on original source is not significantly higher than rdcost of best
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08001065 // 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);
Anupam Pandey45caf952022-12-15 11:16:15 +05301085 xd->plane[AOM_PLANE_Y].pre[0] = yv12_mb[LAST_FRAME][AOM_PLANE_Y];
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08001086 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
chiyotsai1538b072022-07-26 10:25:14 -07001087 unsigned int var;
Ranjit Kumar Tulabanducd4a3072022-09-02 15:53:57 +05301088 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, &var, 1, NULL);
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08001089
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) {
Anupam Pandey45caf952022-12-15 11:16:15 +05301113 xd->plane[AOM_PLANE_Y].pre[0] = yv12_mb[GOLDEN_FRAME][AOM_PLANE_Y];
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08001114 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
James Zern286e07b2022-09-29 15:44:58 -07001127/*!\brief Searches for the best interpolation filter
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001128 *
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -07001129 * \ingroup nonrd_mode_search
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001130 * \callgraph
1131 * \callergraph
Marco Paniconi010b1a82021-08-24 22:41:50 -07001132 * 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.
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001137 *
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
Cherma Rajan Ac8327552022-11-11 10:36:09 +05301142 * \param[in] inter_pred_params_sr Pointer to structure holding parameters of
1143 inter prediction for single reference
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001144 * \param[in] mi_row Row index in 4x4 units
1145 * \param[in] mi_col Column index in 4x4 units
Cheng Chen94634492022-11-16 17:37:52 -08001146 * \param[in] tmp_buffer Pointer to a temporary buffer for
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001147 * 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
chiyotsai1538b072022-07-26 10:25:14 -07001154 * \param[out] var The residue variance of the current
1155 * predictor.
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001156 * \param[in] use_model_yrd_large Flag, indicating special logic to handle
1157 * large blocks
Marco Paniconi493cf672022-02-25 00:09:45 -08001158 * \param[in] best_sse Best sse so far.
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301159 * \param[in] is_single_pred Flag, indicating single mode.
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001160 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -07001161 * \remark Nothing is returned. Instead, calculated RD cost is placed to
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001162 * \c this_rdc and best filter is placed to \c mi->interp_filters. In case
James Zern286e07b2022-09-29 15:44:58 -07001163 * \c reuse_inter_pred flag is set, this function also outputs
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001164 * \c this_mode_pred. Also \c this_early_temp is set if transform can be
1165 * skipped
1166 */
Fyodor Kyslov26473052019-08-26 17:09:30 -07001167static void search_filter_ref(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc,
Cherma Rajan Ac8327552022-11-11 10:36:09 +05301168 InterPredParams *inter_pred_params_sr, int mi_row,
Cheng Chen94634492022-11-16 17:37:52 -08001169 int mi_col, PRED_BUFFER *tmp_buffer,
1170 BLOCK_SIZE bsize, int reuse_inter_pred,
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07001171 PRED_BUFFER **this_mode_pred,
chiyotsai1538b072022-07-26 10:25:14 -07001172 int *this_early_term, unsigned int *var,
Neeraj Gadgil1513bf02022-10-28 23:43:22 +05301173 int use_model_yrd_large, int64_t best_sse,
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301174 int is_single_pred) {
Fyodor Kyslov26473052019-08-26 17:09:30 -07001175 AV1_COMMON *const cm = &cpi->common;
1176 MACROBLOCKD *const xd = &x->e_mbd;
Anupam Pandey45caf952022-12-15 11:16:15 +05301177 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
Fyodor Kyslov26473052019-08-26 17:09:30 -07001178 MB_MODE_INFO *const mi = xd->mi[0];
Marco Paniconi966ec042019-10-08 09:33:18 -07001179 const int bw = block_size_wide[bsize];
Marco Paniconi010b1a82021-08-24 22:41:50 -07001180 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 };
Marco Paniconi966ec042019-10-08 09:33:18 -07001184 PRED_BUFFER *current_pred = *this_mode_pred;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001185 int best_skip = 0;
1186 int best_early_term = 0;
1187 int64_t best_cost = INT64_MAX;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001188 int best_filter_index = -1;
Cherma Rajan Ac8327552022-11-11 10:36:09 +05301189
1190 SubpelParams subpel_params;
1191 // Initialize inter prediction params at mode level for single reference
1192 // mode.
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301193 if (is_single_pred)
Cherma Rajan Ac8327552022-11-11 10:36:09 +05301194 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);
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301197 for (int filter_idx = 0; filter_idx < FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE;
1198 ++filter_idx) {
Fyodor Kyslov26473052019-08-26 17:09:30 -07001199 int64_t cost;
Marco Paniconi010b1a82021-08-24 22:41:50 -07001200 if (cpi->sf.interp_sf.disable_dual_filter &&
Anupam Pandey50b881e2022-11-14 16:49:27 +05301201 filters_ref_set[filter_idx].as_filters.x_filter !=
1202 filters_ref_set[filter_idx].as_filters.y_filter)
Marco Paniconi010b1a82021-08-24 22:41:50 -07001203 continue;
Anupam Pandey50b881e2022-11-14 16:49:27 +05301204
1205 mi->interp_filters.as_int = filters_ref_set[filter_idx].as_int;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301206 if (is_single_pred)
Cherma Rajan Ac8327552022-11-11 10:36:09 +05301207 av1_enc_build_inter_predictor_y_nonrd(xd, inter_pred_params_sr,
1208 &subpel_params);
Neeraj Gadgil1513bf02022-10-28 23:43:22 +05301209 else
Anupam Pandey45caf952022-12-15 11:16:15 +05301210 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1211 AOM_PLANE_Y, AOM_PLANE_Y);
chiyotsai1538b072022-07-26 10:25:14 -07001212 unsigned int curr_var = UINT_MAX;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001213 if (use_model_yrd_large)
Fyodor Kyslove8692412020-05-04 17:47:13 -07001214 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301215 &pf_rd_stats[filter_idx], this_early_term, 1,
1216 best_sse, &curr_var, UINT_MAX);
Fyodor Kyslov26473052019-08-26 17:09:30 -07001217 else
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301218 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(
Tarundeep Singh4243e622021-04-20 16:10:22 +05301221 x, xd, cm->features.interp_filter, cm->seq_params->enable_dual_filter);
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301222 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;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001225 if (cost < best_cost) {
chiyotsai1538b072022-07-26 10:25:14 -07001226 *var = curr_var;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301227 best_filter_index = filter_idx;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001228 best_cost = cost;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301229 best_skip = pf_rd_stats[filter_idx].skip_txfm;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001230 best_early_term = *this_early_term;
Marco Paniconi966ec042019-10-08 09:33:18 -07001231 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 }
Cheng Chen94634492022-11-16 17:37:52 -08001236 current_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
Marco Paniconi966ec042019-10-08 09:33:18 -07001237 pd->dst.buf = current_pred->data;
1238 pd->dst.stride = bw;
1239 }
Fyodor Kyslov26473052019-08-26 17:09:30 -07001240 }
1241 }
Marco Paniconi010b1a82021-08-24 22:41:50 -07001242 assert(best_filter_index >= 0 &&
1243 best_filter_index < dim_factor * FILTER_SEARCH_SIZE);
Marco Paniconi966ec042019-10-08 09:33:18 -07001244 if (reuse_inter_pred && *this_mode_pred != current_pred)
1245 free_pred_buffer(current_pred);
Fyodor Kyslov26473052019-08-26 17:09:30 -07001246
Anupam Pandey50b881e2022-11-14 16:49:27 +05301247 mi->interp_filters.as_int = filters_ref_set[best_filter_index].as_int;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001248 mi->tx_size = pf_tx_size[best_filter_index];
Fyodor Kyslove8692412020-05-04 17:47:13 -07001249 this_rdc->rate = pf_rd_stats[best_filter_index].rate;
1250 this_rdc->dist = pf_rd_stats[best_filter_index].dist;
Fyodor Kyslove8692412020-05-04 17:47:13 -07001251 this_rdc->sse = pf_rd_stats[best_filter_index].sse;
chiyotsai8c004e12020-04-17 15:52:08 -07001252 this_rdc->skip_txfm = (best_skip || best_early_term);
Fyodor Kyslov26473052019-08-26 17:09:30 -07001253 *this_early_term = best_early_term;
Marco Paniconi966ec042019-10-08 09:33:18 -07001254 if (reuse_inter_pred) {
1255 pd->dst.buf = (*this_mode_pred)->data;
1256 pd->dst.stride = (*this_mode_pred)->stride;
Marco Paniconi010b1a82021-08-24 22:41:50 -07001257 } else if (best_filter_index < dim_factor * FILTER_SEARCH_SIZE - 1) {
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301258 if (is_single_pred)
Cherma Rajan Ac8327552022-11-11 10:36:09 +05301259 av1_enc_build_inter_predictor_y_nonrd(xd, inter_pred_params_sr,
1260 &subpel_params);
Neeraj Gadgil9fd7a842022-11-07 11:22:44 +05301261 else
Anupam Pandey45caf952022-12-15 11:16:15 +05301262 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1263 AOM_PLANE_Y, AOM_PLANE_Y);
Marco Paniconiecf1cc62019-10-07 19:17:37 -07001264 }
Fyodor Kyslov26473052019-08-26 17:09:30 -07001265}
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001266#if !CONFIG_REALTIME_ONLY
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001267
Jingning Handd4faa82022-03-23 11:53:13 -07001268static AOM_INLINE int is_warped_mode_allowed(const AV1_COMP *cpi,
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001269 MACROBLOCK *const x,
1270 const MB_MODE_INFO *mbmi) {
Jingning Handd4faa82022-03-23 11:53:13 -07001271 const FeatureFlags *const features = &cpi->common.features;
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001272 const MACROBLOCKD *xd = &x->e_mbd;
1273
Jingning Handd4faa82022-03-23 11:53:13 -07001274 if (cpi->sf.inter_sf.extra_prune_warped) return 0;
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001275 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
1292static 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
1319static 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,
Marco Paniconi493cf672022-02-25 00:09:45 -08001322 int *rate_mv, int64_t best_sse) {
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001323 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 };
Jingning Handd4faa82022-03-23 11:53:13 -07001337 int mode_search_size = is_warped_mode_allowed(cpi, x, mi) ? 2 : 1;
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001338
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
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301354 for (int mode_index = 0; mode_index < mode_search_size; ++mode_index) {
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001355 int64_t cost = INT64_MAX;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301356 MOTION_MODE motion_mode = motion_modes[mode_index];
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001357 *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
Anupam Pandey45caf952022-12-15 11:16:15 +05301362 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1363 AOM_PLANE_Y, AOM_PLANE_Y);
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001364 if (use_model_yrd_large)
1365 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301366 &pf_rd_stats[mode_index], this_early_term, 1,
1367 best_sse, NULL, UINT_MAX);
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001368 else
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301369 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 +=
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001372 av1_get_switchable_rate(x, xd, cm->features.interp_filter,
1373 cm->seq_params->enable_dual_filter);
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301374 cost = RDCOST(x->rdmult, pf_rd_stats[mode_index].rate,
1375 pf_rd_stats[mode_index].dist);
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001376 } 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,
Rachel Barkerc6878ab2023-01-09 17:03:54 +00001408 total_samples, cpi->sf.mv_sf.warp_search_method,
1409 cpi->sf.mv_sf.warp_search_iters);
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001410 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
Anupam Pandey45caf952022-12-15 11:16:15 +05301428 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1429 AOM_PLANE_Y, av1_num_planes(cm) - 1);
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001430 if (use_model_yrd_large)
1431 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301432 &pf_rd_stats[mode_index], this_early_term,
1433 1, best_sse, NULL, UINT_MAX);
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001434 else
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301435 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[mode_index], NULL,
1436 1, NULL);
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001437
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301438 pf_rd_stats[mode_index].rate +=
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001439 mode_costs->motion_mode_cost[bsize][mi->motion_mode];
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301440 cost = RDCOST(x->rdmult, pf_rd_stats[mode_index].rate,
1441 pf_rd_stats[mode_index].dist);
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001442 } else {
1443 cost = INT64_MAX;
1444 }
1445 }
1446 if (cost < best_cost) {
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301447 best_mode_index = mode_index;
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001448 best_cost = cost;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301449 best_skip = pf_rd_stats[mode_index].skip_txfm;
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001450 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) {
Anupam Pandey45caf952022-12-15 11:16:15 +05301463 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1464 AOM_PLANE_Y, AOM_PLANE_Y);
Fyodor Kyslov70d2ea62021-07-15 17:12:33 -07001465 }
1466}
1467#endif // !CONFIG_REALTIME_ONLY
Fyodor Kyslov26473052019-08-26 17:09:30 -07001468
chiyotsai1d465d82022-08-10 11:55:29 -07001469#define COLLECT_NON_SQR_STAT 0
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07001470
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05301471#if COLLECT_NONRD_PICK_MODE_STAT
Anupam Pandeya61e10a2022-12-06 16:49:32 +05301472
Cheng Chen82c61342022-10-18 16:24:06 -07001473static AOM_INLINE void print_stage_time(const char *stage_name,
chiyotsai1d465d82022-08-10 11:55:29 -07001474 int64_t stage_time,
1475 int64_t total_time) {
1476 printf(" %s: %ld (%f%%)\n", stage_name, stage_time,
1477 100 * stage_time / (float)total_time);
1478}
Cheng Chen82c61342022-10-18 16:24:06 -07001479
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05301480static void print_time(const mode_search_stat_nonrd *const ms_stat,
1481 BLOCK_SIZE bsize, int mi_rows, int mi_cols, int mi_row,
1482 int mi_col) {
Cheng Chen82c61342022-10-18 16:24:06 -07001483 if ((mi_row + mi_size_high[bsize] >= mi_rows) &&
1484 (mi_col + mi_size_wide[bsize] >= mi_cols)) {
1485 int64_t total_time = 0l;
1486 int32_t total_blocks = 0;
1487 for (BLOCK_SIZE bs = 0; bs < BLOCK_SIZES; bs++) {
1488 total_time += ms_stat->total_block_times[bs];
1489 total_blocks += ms_stat->num_blocks[bs];
1490 }
1491
1492 printf("\n");
1493 for (BLOCK_SIZE bs = 0; bs < BLOCK_SIZES; bs++) {
1494 if (ms_stat->num_blocks[bs] == 0) {
1495 continue;
1496 }
1497 if (!COLLECT_NON_SQR_STAT && block_size_wide[bs] != block_size_high[bs]) {
1498 continue;
1499 }
1500
1501 printf("BLOCK_%dX%d Num %d, Time: %ld (%f%%), Avg_time %f:\n",
1502 block_size_wide[bs], block_size_high[bs], ms_stat->num_blocks[bs],
1503 ms_stat->total_block_times[bs],
1504 100 * ms_stat->total_block_times[bs] / (float)total_time,
1505 (float)ms_stat->total_block_times[bs] / ms_stat->num_blocks[bs]);
1506 for (int j = 0; j < MB_MODE_COUNT; j++) {
1507 if (ms_stat->nonskipped_search_times[bs][j] == 0) {
1508 continue;
1509 }
1510
1511 int64_t total_mode_time = ms_stat->nonskipped_search_times[bs][j];
1512 printf(" Mode %d, %d/%d tps %f\n", j,
1513 ms_stat->num_nonskipped_searches[bs][j],
1514 ms_stat->num_searches[bs][j],
1515 ms_stat->num_nonskipped_searches[bs][j] > 0
1516 ? (float)ms_stat->nonskipped_search_times[bs][j] /
1517 ms_stat->num_nonskipped_searches[bs][j]
1518 : 0l);
1519 if (j >= INTER_MODE_START) {
1520 total_mode_time = ms_stat->ms_time[bs][j] + ms_stat->ifs_time[bs][j] +
1521 ms_stat->model_rd_time[bs][j] +
1522 ms_stat->txfm_time[bs][j];
1523 print_stage_time("Motion Search Time", ms_stat->ms_time[bs][j],
1524 total_time);
1525 print_stage_time("Filter Search Time", ms_stat->ifs_time[bs][j],
1526 total_time);
1527 print_stage_time("Model RD Time", ms_stat->model_rd_time[bs][j],
1528 total_time);
1529 print_stage_time("Tranfm Search Time", ms_stat->txfm_time[bs][j],
1530 total_time);
1531 }
1532 print_stage_time("Total Mode Time", total_mode_time, total_time);
1533 }
1534 printf("\n");
1535 }
1536 printf("Total time = %ld. Total blocks = %d\n", total_time, total_blocks);
1537 }
1538}
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05301539#endif // COLLECT_NONRD_PICK_MODE_STAT
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07001540
Mudassir Galagnath431349c2022-08-02 12:31:42 +05301541static bool should_prune_intra_modes_using_neighbors(
1542 const MACROBLOCKD *xd, bool enable_intra_mode_pruning_using_neighbors,
1543 PREDICTION_MODE this_mode, PREDICTION_MODE above_mode,
1544 PREDICTION_MODE left_mode) {
1545 if (!enable_intra_mode_pruning_using_neighbors) return false;
1546
Mudassir Galaganath237404c2022-12-21 13:02:19 +05301547 // Avoid pruning of DC_PRED as it is the most probable mode to win as per the
1548 // statistics generated for nonrd intra mode evaluations.
Mudassir Galagnath431349c2022-08-02 12:31:42 +05301549 if (this_mode == DC_PRED) return false;
1550
1551 // Enable the pruning for current mode only if it is not the winner mode of
1552 // both the neighboring blocks (left/top).
1553 return xd->up_available && this_mode != above_mode && xd->left_available &&
1554 this_mode != left_mode;
1555}
1556
chiyotsai73ddf442020-06-01 15:42:23 -07001557void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
1558 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001559 AV1_COMMON *const cm = &cpi->common;
1560 MACROBLOCKD *const xd = &x->e_mbd;
1561 MB_MODE_INFO *const mi = xd->mi[0];
1562 RD_STATS this_rdc, best_rdc;
Mudassir Galaganath7e4b0332023-01-27 13:07:50 +05301563 struct estimate_block_intra_args args;
1564 init_estimate_block_intra_args(&args, cpi, x);
chiyotsaia36d9002020-04-29 16:48:21 -07001565 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
Mudassir Galaganath7e4b0332023-01-27 13:07:50 +05301566 mi->tx_size =
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001567 AOMMIN(max_txsize_lookup[bsize],
chiyotsaia36d9002020-04-29 16:48:21 -07001568 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
Mudassir Galaganathf5c7d6d2023-02-03 12:21:22 +05301569 assert(IMPLIES(xd->lossless[mi->segment_id], mi->tx_size == TX_4X4));
Mudassir Galaganath7e4b0332023-01-27 13:07:50 +05301570 const BLOCK_SIZE tx_bsize = txsize_to_bsize[mi->tx_size];
1571
1572 // If the current block size is the same as the transform block size, enable
1573 // mode pruning based on the best SAD so far.
1574 if (cpi->sf.rt_sf.prune_intra_mode_using_best_sad_so_far && bsize == tx_bsize)
1575 args.prune_mode_based_on_sad = true;
1576
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001577 int *bmode_costs;
Marco Paniconif38266b2021-03-07 22:43:23 -08001578 PREDICTION_MODE best_mode = DC_PRED;
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001579 const MB_MODE_INFO *above_mi = xd->above_mbmi;
1580 const MB_MODE_INFO *left_mi = xd->left_mbmi;
1581 const PREDICTION_MODE A = av1_above_block_mode(above_mi);
1582 const PREDICTION_MODE L = av1_left_block_mode(left_mi);
Hui Su1b6af722021-04-08 17:56:50 -07001583 const int above_ctx = intra_mode_context[A];
1584 const int left_ctx = intra_mode_context[L];
Mudassir Galagnath431349c2022-08-02 12:31:42 +05301585 const unsigned int source_variance = x->source_variance;
Hui Su1b6af722021-04-08 17:56:50 -07001586 bmode_costs = x->mode_costs.y_mode_costs[above_ctx][left_ctx];
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001587
1588 av1_invalid_rd_stats(&best_rdc);
1589 av1_invalid_rd_stats(&this_rdc);
1590
Anupam Pandeyd68ec612023-01-03 14:15:58 +05301591 init_mbmi_nonrd(mi, DC_PRED, INTRA_FRAME, NONE_FRAME, cm);
Jerome Jiangcde6f4a2020-01-06 11:14:58 -08001592 mi->mv[0].as_int = mi->mv[1].as_int = INVALID_MV;
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001593
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001594 // Change the limit of this loop to add other intra prediction
1595 // mode tests.
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05301596 for (int mode_index = 0; mode_index < RTC_INTRA_MODES; ++mode_index) {
1597 PREDICTION_MODE this_mode = intra_mode_list[mode_index];
Mudassir Galagnath02ff9702022-02-16 14:03:26 +05301598
1599 // As per the statistics generated for intra mode evaluation in the nonrd
1600 // path, it is found that the probability of H_PRED mode being the winner is
Mudassir Galaganath237404c2022-12-21 13:02:19 +05301601 // very low when the best mode so far is V_PRED (out of DC_PRED and V_PRED).
1602 // If V_PRED is the winner mode out of DC_PRED and V_PRED, it could imply
1603 // the presence of a vertically dominant pattern. Hence, H_PRED mode is not
1604 // evaluated.
Mudassir Galagnath02ff9702022-02-16 14:03:26 +05301605 if (cpi->sf.rt_sf.prune_h_pred_using_best_mode_so_far &&
1606 this_mode == H_PRED && best_mode == V_PRED)
1607 continue;
1608
Mudassir Galagnath431349c2022-08-02 12:31:42 +05301609 if (should_prune_intra_modes_using_neighbors(
1610 xd, cpi->sf.rt_sf.enable_intra_mode_pruning_using_neighbors,
1611 this_mode, A, L)) {
1612 // Prune V_PRED and H_PRED if source variance of the block is less than
1613 // or equal to 50. The source variance threshold is obtained empirically.
1614 if ((this_mode == V_PRED || this_mode == H_PRED) && source_variance <= 50)
1615 continue;
1616
Mudassir Galaganath237404c2022-12-21 13:02:19 +05301617 // As per the statistics, probability of SMOOTH_PRED being the winner is
1618 // low when best mode so far is DC_PRED (out of DC_PRED, V_PRED and
Mudassir Galagnath431349c2022-08-02 12:31:42 +05301619 // H_PRED). Hence, SMOOTH_PRED mode is not evaluated.
1620 if (best_mode == DC_PRED && this_mode == SMOOTH_PRED) continue;
1621 }
1622
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001623 this_rdc.dist = this_rdc.rate = 0;
1624 args.mode = this_mode;
1625 args.skippable = 1;
1626 args.rdc = &this_rdc;
Marco Paniconif38266b2021-03-07 22:43:23 -08001627 mi->mode = this_mode;
Anupam Pandey45caf952022-12-15 11:16:15 +05301628 av1_foreach_transformed_block_in_plane(xd, bsize, AOM_PLANE_Y,
Anupam Pandeyd68ec612023-01-03 14:15:58 +05301629 av1_estimate_block_intra, &args);
Mudassir Galaganath7e4b0332023-01-27 13:07:50 +05301630
1631 if (this_rdc.rate == INT_MAX) continue;
1632
Deepa K Gc990ca22021-02-22 17:11:37 +05301633 const int skip_ctx = av1_get_skip_txfm_context(xd);
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001634 if (args.skippable) {
Deepa K Gc990ca22021-02-22 17:11:37 +05301635 this_rdc.rate = x->mode_costs.skip_txfm_cost[skip_ctx][1];
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001636 } else {
Deepa K Gc990ca22021-02-22 17:11:37 +05301637 this_rdc.rate += x->mode_costs.skip_txfm_cost[skip_ctx][0];
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001638 }
1639 this_rdc.rate += bmode_costs[this_mode];
1640 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1641
1642 if (this_rdc.rdcost < best_rdc.rdcost) {
1643 best_rdc = this_rdc;
Marco Paniconif38266b2021-03-07 22:43:23 -08001644 best_mode = this_mode;
Fyodor Kyslovca638fa2021-12-08 19:49:13 -08001645 if (!this_rdc.skip_txfm) {
1646 memset(ctx->blk_skip, 0,
1647 sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
1648 }
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001649 }
1650 }
1651
Marco Paniconi86d1aeb2024-05-15 05:59:35 +00001652 const unsigned int thresh_sad =
1653 cpi->sf.rt_sf.prune_palette_search_nonrd > 1 ? 100 : 20;
Marco Paniconic612b962024-05-08 23:46:26 -07001654 const unsigned int best_sad_norm =
1655 args.best_sad >>
1656 (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
Marco Paniconid97381f2024-05-02 11:35:58 -07001657
1658 // Try palette if it's enabled.
1659 bool try_palette =
Cheng Chen407b9862024-05-08 15:25:17 -07001660 cpi->oxcf.tool_cfg.enable_palette &&
Marco Paniconid97381f2024-05-02 11:35:58 -07001661 av1_allow_palette(cpi->common.features.allow_screen_content_tools,
1662 mi->bsize);
Marco Paniconi86d1aeb2024-05-15 05:59:35 +00001663 if (cpi->sf.rt_sf.prune_palette_search_nonrd > 0) {
Cheng Chen407b9862024-05-08 15:25:17 -07001664 bool prune =
1665 (!args.prune_mode_based_on_sad || best_sad_norm > thresh_sad) &&
1666 bsize <= BLOCK_16X16 && x->source_variance > 200;
1667 try_palette &= prune;
1668 }
Marco Paniconid97381f2024-05-02 11:35:58 -07001669 if (try_palette) {
1670 const TxfmSearchInfo *txfm_info = &x->txfm_search_info;
1671 const unsigned int intra_ref_frame_cost = 0;
Marco Paniconi06f3b3b2024-05-09 22:26:27 -07001672 x->color_palette_thresh = (best_sad_norm < 500) ? 32 : 64;
1673
Marco Paniconid97381f2024-05-02 11:35:58 -07001674 // Search palette mode for Luma plane in intra frame.
1675 av1_search_palette_mode_luma(cpi, x, bsize, intra_ref_frame_cost, ctx,
1676 &this_rdc, best_rdc.rdcost);
1677 // Update best mode data.
Marco Paniconi3944d9b2024-05-08 10:58:55 -07001678 if (this_rdc.rdcost < best_rdc.rdcost) {
Marco Paniconid97381f2024-05-02 11:35:58 -07001679 best_mode = DC_PRED;
1680 mi->mv[0].as_int = INVALID_MV;
1681 mi->mv[1].as_int = INVALID_MV;
1682 best_rdc.rate = this_rdc.rate;
1683 best_rdc.dist = this_rdc.dist;
1684 best_rdc.rdcost = this_rdc.rdcost;
1685 if (!this_rdc.skip_txfm) {
1686 memcpy(ctx->blk_skip, txfm_info->blk_skip,
1687 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
1688 }
1689 if (xd->tx_type_map[0] != DCT_DCT)
1690 av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
1691 } else {
1692 av1_zero(mi->palette_mode_info);
1693 }
1694 }
1695
Marco Paniconif38266b2021-03-07 22:43:23 -08001696 mi->mode = best_mode;
1697 // Keep DC for UV since mode test is based on Y channel only.
Fyodor Kyslovdc9bddb2022-01-12 16:30:33 -08001698 mi->uv_mode = UV_DC_PRED;
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001699 *rd_cost = best_rdc;
1700
Marco Paniconi1a16d1e2023-03-22 14:11:13 -07001701 // For lossless: always force the skip flags off.
1702 // Even though the blk_skip is set to 0 above in the rdcost comparison,
1703 // do it here again in case the above logic changes.
1704 if (is_lossless_requested(&cpi->oxcf.rc_cfg)) {
1705 x->txfm_search_info.skip_txfm = 0;
1706 memset(ctx->blk_skip, 0,
1707 sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
1708 }
1709
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001710#if CONFIG_INTERNAL_STATS
Anupam Pandeyd68ec612023-01-03 14:15:58 +05301711 store_coding_context_nonrd(x, ctx, mi->mode);
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001712#else
Anupam Pandeyd68ec612023-01-03 14:15:58 +05301713 store_coding_context_nonrd(x, ctx);
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001714#endif // CONFIG_INTERNAL_STATS
1715}
1716
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001717static AOM_INLINE int is_same_gf_and_last_scale(AV1_COMMON *cm) {
1718 struct scale_factors *const sf_last = get_ref_scale_factors(cm, LAST_FRAME);
1719 struct scale_factors *const sf_golden =
1720 get_ref_scale_factors(cm, GOLDEN_FRAME);
1721 return ((sf_last->x_scale_fp == sf_golden->x_scale_fp) &&
1722 (sf_last->y_scale_fp == sf_golden->y_scale_fp));
1723}
1724
1725static AOM_INLINE void get_ref_frame_use_mask(AV1_COMP *cpi, MACROBLOCK *x,
1726 MB_MODE_INFO *mi, int mi_row,
Wan-Teh Chang604883a2023-10-18 19:24:21 -07001727 int mi_col, BLOCK_SIZE bsize,
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07001728 int gf_temporal_ref,
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001729 int use_ref_frame[],
1730 int *force_skip_low_temp_var) {
1731 AV1_COMMON *const cm = &cpi->common;
1732 const struct segmentation *const seg = &cm->seg;
Tarundeep Singh4243e622021-04-20 16:10:22 +05301733 const int is_small_sb = (cm->seq_params->sb_size == BLOCK_64X64);
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001734
Marco Paniconi52ee0ed2022-09-28 12:01:47 -07001735 // When the ref_frame_config is used to set the reference frame structure
1736 // then the usage of alt_ref is determined by the ref_frame_flags
1737 // (and not the speed feature use_nonrd_altref_frame).
Marco Paniconi53218432022-10-10 13:00:27 -07001738 int use_alt_ref_frame = cpi->ppi->rtc_ref.set_ref_frame_config ||
1739 cpi->sf.rt_sf.use_nonrd_altref_frame;
Marco Paniconi52ee0ed2022-09-28 12:01:47 -07001740
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001741 int use_golden_ref_frame = 1;
Marco Paniconi691b0de2021-11-17 15:58:18 -08001742 int use_last_ref_frame = 1;
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001743
Marco Paniconi52ee0ed2022-09-28 12:01:47 -07001744 // When the ref_frame_config is used to set the reference frame structure:
1745 // check if LAST is used as a reference. And only remove golden and altref
1746 // references below if last is used as a reference.
Marco Paniconi53218432022-10-10 13:00:27 -07001747 if (cpi->ppi->rtc_ref.set_ref_frame_config)
Marco Paniconi8a28c202021-12-02 00:12:58 -08001748 use_last_ref_frame =
1749 cpi->ref_frame_flags & AOM_LAST_FLAG ? use_last_ref_frame : 0;
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001750
Marco Paniconi52ee0ed2022-09-28 12:01:47 -07001751 // frame_since_golden is not used when user sets the referene structure.
Marco Paniconi53218432022-10-10 13:00:27 -07001752 if (!cpi->ppi->rtc_ref.set_ref_frame_config && use_last_ref_frame &&
Marco Paniconi52ee0ed2022-09-28 12:01:47 -07001753 cpi->rc.frames_since_golden == 0 && gf_temporal_ref) {
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001754 use_golden_ref_frame = 0;
1755 }
Marco Paniconi52ee0ed2022-09-28 12:01:47 -07001756
Marco Paniconi8a28c202021-12-02 00:12:58 -08001757 if (use_last_ref_frame && cpi->sf.rt_sf.short_circuit_low_temp_var &&
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001758 x->nonrd_prune_ref_frame_search) {
1759 if (is_small_sb)
Yunqing Wang61a75982021-10-13 12:01:11 -07001760 *force_skip_low_temp_var = av1_get_force_skip_low_temp_var_small_sb(
chiyotsai68eefbe2020-05-01 15:07:58 -07001761 &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001762 else
Yunqing Wang61a75982021-10-13 12:01:11 -07001763 *force_skip_low_temp_var = av1_get_force_skip_low_temp_var(
chiyotsai68eefbe2020-05-01 15:07:58 -07001764 &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001765 // If force_skip_low_temp_var is set, skip golden reference.
1766 if (*force_skip_low_temp_var) {
1767 use_golden_ref_frame = 0;
1768 use_alt_ref_frame = 0;
1769 }
1770 }
1771
Marco Paniconi8a28c202021-12-02 00:12:58 -08001772 if (use_last_ref_frame &&
Ranjit Kumar Tulabanducd4a3072022-09-02 15:53:57 +05301773 (x->nonrd_prune_ref_frame_search > 2 || x->force_zeromv_skip_for_blk ||
Marco Paniconi8a28c202021-12-02 00:12:58 -08001774 (x->nonrd_prune_ref_frame_search > 1 && bsize > BLOCK_64X64))) {
Marco Paniconi9d6cef02021-10-25 00:02:48 -07001775 use_golden_ref_frame = 0;
1776 use_alt_ref_frame = 0;
1777 }
1778
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001779 if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
1780 get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) {
1781 use_golden_ref_frame = 1;
1782 use_alt_ref_frame = 0;
1783 }
1784
Marco Paniconi555b3aa2023-03-03 09:40:28 -08001785 // Skip golden/altref reference if color is set, on flat blocks with motion.
1786 // For screen: always skip golden/alt (if color_sensitivity_sb_g/alt is set)
Marco Paniconi09569452022-09-07 15:05:25 -07001787 // except when x->nonrd_prune_ref_frame_search = 0. This latter flag
James Zern286e07b2022-09-29 15:44:58 -07001788 // may be set in the variance partition when golden is a much better
Marco Paniconi09569452022-09-07 15:05:25 -07001789 // reference than last, in which case it may not be worth skipping
Marco Paniconi555b3aa2023-03-03 09:40:28 -08001790 // golden/altref completely.
1791 // Condition on use_last_ref to make sure there remains at least one
1792 // reference.
1793 if (use_last_ref_frame &&
1794 ((cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
Marco Paniconi09569452022-09-07 15:05:25 -07001795 x->nonrd_prune_ref_frame_search != 0) ||
Marco Paniconi555b3aa2023-03-03 09:40:28 -08001796 (x->source_variance < 200 &&
1797 x->content_state_sb.source_sad_nonrd >= kLowSad))) {
1798 if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
1799 x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
1800 use_golden_ref_frame = 0;
1801 if (x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
1802 x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
1803 use_alt_ref_frame = 0;
1804 }
Marco Paniconic6a503f2022-08-15 23:16:21 -07001805
Marco Paniconie26457d2022-10-11 15:42:17 -07001806 // For non-screen: if golden and altref are not being selected as references
1807 // (use_golden_ref_frame/use_alt_ref_frame = 0) check to allow golden back
1808 // based on the sad of nearest/nearmv of LAST ref. If this block sad is large,
1809 // keep golden as reference. Only do this for the agrressive pruning mode and
1810 // avoid it when color is set for golden reference.
1811 if (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN &&
1812 (cpi->ref_frame_flags & AOM_LAST_FLAG) && !use_golden_ref_frame &&
1813 !use_alt_ref_frame && x->pred_mv_sad[LAST_FRAME] != INT_MAX &&
1814 x->nonrd_prune_ref_frame_search > 2 &&
Anupam Pandey45caf952022-12-15 11:16:15 +05301815 x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
1816 x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) {
Marco Paniconia5804712023-12-12 16:01:27 -08001817 int thr = (cm->width * cm->height > RESOLUTION_288P) ? 100 : 150;
Marco Paniconie26457d2022-10-11 15:42:17 -07001818 int pred = x->pred_mv_sad[LAST_FRAME] >>
1819 (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
1820 if (pred > thr) use_golden_ref_frame = 1;
1821 }
1822
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001823 use_alt_ref_frame =
1824 cpi->ref_frame_flags & AOM_ALT_FLAG ? use_alt_ref_frame : 0;
1825 use_golden_ref_frame =
1826 cpi->ref_frame_flags & AOM_GOLD_FLAG ? use_golden_ref_frame : 0;
1827
Marco Paniconi6dfcab22022-10-31 00:41:42 -07001828 // For spatial layers: enable golden ref if it is set by user and
1829 // corresponds to the lower spatial layer.
1830 if (cpi->svc.spatial_layer_id > 0 && (cpi->ref_frame_flags & AOM_GOLD_FLAG) &&
1831 x->content_state_sb.source_sad_nonrd < kHighSad) {
1832 const int buffslot_golden =
1833 cpi->ppi->rtc_ref.ref_idx[GOLDEN_FRAME - LAST_FRAME];
Marco Paniconi42b92732023-02-07 00:07:13 -08001834 if (cpi->ppi->rtc_ref.buffer_time_index[buffslot_golden] ==
Marco Paniconi6dfcab22022-10-31 00:41:42 -07001835 cpi->svc.current_superframe)
1836 use_golden_ref_frame = 1;
1837 }
1838
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001839 use_ref_frame[ALTREF_FRAME] = use_alt_ref_frame;
1840 use_ref_frame[GOLDEN_FRAME] = use_golden_ref_frame;
Marco Paniconi691b0de2021-11-17 15:58:18 -08001841 use_ref_frame[LAST_FRAME] = use_last_ref_frame;
Marco Paniconifca9d402023-06-29 15:58:51 -07001842 // Keep this assert on, as only 3 references are used in nonrd_pickmode
1843 // (LAST, GOLDEN, ALTREF), and if all 3 are not set by user then this
1844 // frame must be an intra-only frame and hence should never enter the
1845 // pickmode here for inter frames.
Marco Paniconi8a28c202021-12-02 00:12:58 -08001846 assert(use_last_ref_frame || use_golden_ref_frame || use_alt_ref_frame);
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001847}
1848
Neeraj Gadgil1513bf02022-10-28 23:43:22 +05301849static AOM_INLINE int is_filter_search_enabled_blk(
1850 AV1_COMP *cpi, MACROBLOCK *x, int mi_row, int mi_col, BLOCK_SIZE bsize,
1851 int segment_id, int cb_pred_filter_search, InterpFilter *filt_select) {
chiyotsai7f6e21a2020-06-16 14:52:30 -07001852 const AV1_COMMON *const cm = &cpi->common;
Neeraj Gadgil1513bf02022-10-28 23:43:22 +05301853 // filt search disabled
1854 if (!cpi->sf.rt_sf.use_nonrd_filter_search) return 0;
1855 // filt search purely based on mode properties
1856 if (!cb_pred_filter_search) return 1;
1857 MACROBLOCKD *const xd = &x->e_mbd;
1858 int enable_interp_search = 0;
1859 if (!(xd->left_mbmi && xd->above_mbmi)) {
1860 // neighbors info unavailable
1861 enable_interp_search = 2;
1862 } else if (!(is_inter_block(xd->left_mbmi) &&
1863 is_inter_block(xd->above_mbmi))) {
1864 // neighbor is INTRA
1865 enable_interp_search = 2;
1866 } else if (xd->left_mbmi->interp_filters.as_int !=
1867 xd->above_mbmi->interp_filters.as_int) {
1868 // filters are different
1869 enable_interp_search = 2;
1870 } else if ((cb_pred_filter_search == 1) &&
1871 (xd->left_mbmi->interp_filters.as_filters.x_filter !=
1872 EIGHTTAP_REGULAR)) {
1873 // not regular
1874 enable_interp_search = 2;
1875 } else {
1876 // enable prediction based on chessboard pattern
1877 if (xd->left_mbmi->interp_filters.as_filters.x_filter == EIGHTTAP_SMOOTH)
1878 *filt_select = EIGHTTAP_SMOOTH;
1879 const int bsl = mi_size_wide_log2[bsize];
1880 enable_interp_search =
1881 (bool)((((mi_row + mi_col) >> bsl) +
1882 get_chessboard_index(cm->current_frame.frame_number)) &
1883 0x1);
1884 if (cyclic_refresh_segment_id_boosted(segment_id)) enable_interp_search = 1;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001885 }
Neeraj Gadgil1513bf02022-10-28 23:43:22 +05301886 return enable_interp_search;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001887}
1888
1889static AOM_INLINE int skip_mode_by_threshold(
1890 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, int_mv mv,
1891 int frames_since_golden, const int *const rd_threshes,
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08001892 const int *const rd_thresh_freq_fact, int64_t best_cost, int best_skip,
1893 int extra_shift) {
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001894 int skip_this_mode = 0;
1895 const THR_MODES mode_index = mode_idx[ref_frame][INTER_OFFSET(mode)];
Fyodor Kyslov29430902020-11-18 13:44:30 -08001896 int64_t mode_rd_thresh =
1897 best_skip ? ((int64_t)rd_threshes[mode_index]) << (extra_shift + 1)
1898 : ((int64_t)rd_threshes[mode_index]) << extra_shift;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001899
1900 // Increase mode_rd_thresh value for non-LAST for improved encoding
1901 // speed
1902 if (ref_frame != LAST_FRAME) {
1903 mode_rd_thresh = mode_rd_thresh << 1;
1904 if (ref_frame == GOLDEN_FRAME && frames_since_golden > 4)
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08001905 mode_rd_thresh = mode_rd_thresh << (extra_shift + 1);
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001906 }
1907
1908 if (rd_less_than_thresh(best_cost, mode_rd_thresh,
1909 rd_thresh_freq_fact[mode_index]))
1910 if (mv.as_int != 0) skip_this_mode = 1;
1911
1912 return skip_this_mode;
1913}
1914
Marco Paniconi988b34a2020-11-09 12:41:13 -08001915static AOM_INLINE int skip_mode_by_low_temp(
1916 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
1917 CONTENT_STATE_SB content_state_sb, int_mv mv, int force_skip_low_temp_var) {
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001918 // Skip non-zeromv mode search for non-LAST frame if force_skip_low_temp_var
1919 // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped
1920 // later.
1921 if (force_skip_low_temp_var && ref_frame != LAST_FRAME && mv.as_int != 0) {
1922 return 1;
1923 }
1924
Neeraj Gadgil710cc542022-08-22 12:05:25 +05301925 if (content_state_sb.source_sad_nonrd != kHighSad && bsize >= BLOCK_64X64 &&
1926 force_skip_low_temp_var && mode == NEWMV) {
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001927 return 1;
1928 }
1929 return 0;
1930}
1931
1932static AOM_INLINE int skip_mode_by_bsize_and_ref_frame(
1933 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
Marco Paniconif9690602024-04-08 13:53:02 -07001934 int extra_prune, unsigned int sse_zeromv_norm, int more_prune,
1935 int skip_nearmv) {
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001936 const unsigned int thresh_skip_golden = 500;
1937
1938 if (ref_frame != LAST_FRAME && sse_zeromv_norm < thresh_skip_golden &&
1939 mode == NEWMV)
1940 return 1;
1941
Marco Paniconif9690602024-04-08 13:53:02 -07001942 if ((bsize == BLOCK_128X128 && mode == NEWMV) ||
1943 (skip_nearmv && mode == NEARMV))
1944 return 1;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001945
1946 // Skip testing non-LAST if this flag is set.
1947 if (extra_prune) {
1948 if (extra_prune > 1 && ref_frame != LAST_FRAME &&
Marco Paniconi9d6cef02021-10-25 00:02:48 -07001949 (bsize > BLOCK_16X16 && mode == NEWMV))
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001950 return 1;
1951
1952 if (ref_frame != LAST_FRAME && mode == NEARMV) return 1;
Fyodor Kyslov29430902020-11-18 13:44:30 -08001953
1954 if (more_prune && bsize >= BLOCK_32X32 && mode == NEARMV) return 1;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001955 }
1956 return 0;
1957}
1958
Marco Paniconibdef9a92023-07-06 15:20:46 -07001959static void set_block_source_sad(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
1960 struct buf_2d *yv12_mb) {
1961 struct macroblock_plane *const p = &x->plane[0];
1962 const int y_sad = cpi->ppi->fn_ptr[bsize].sdf(p->src.buf, p->src.stride,
1963 yv12_mb->buf, yv12_mb->stride);
1964 if (y_sad == 0) x->block_is_zero_sad = 1;
1965}
1966
James Zernfa7042e2022-10-27 18:25:57 -07001967static void set_color_sensitivity(AV1_COMP *cpi, MACROBLOCK *x,
1968 BLOCK_SIZE bsize, int y_sad,
1969 unsigned int source_variance,
1970 struct buf_2d yv12_mb[MAX_MB_PLANE]) {
Deepa K G309d0af2022-07-15 00:17:17 +05301971 const int subsampling_x = cpi->common.seq_params->subsampling_x;
1972 const int subsampling_y = cpi->common.seq_params->subsampling_y;
Marco Paniconid4b429f2023-07-27 16:06:22 -07001973 const int source_sad_nonrd = x->content_state_sb.source_sad_nonrd;
Marco Paniconidcba6b22023-08-03 16:21:09 -07001974 const int high_res = cpi->common.width * cpi->common.height >= 640 * 360;
1975 if (bsize == cpi->common.seq_params->sb_size) {
1976 // At superblock level color_sensitivity is already set to 0, 1, or 2.
1977 // 2 is middle/uncertain level. To avoid additional sad
1978 // computations when bsize = sb_size force level 2 to 1 (certain color)
1979 // for motion areas.
1980 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 2) {
1981 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] =
1982 source_sad_nonrd >= kMedSad ? 1 : 0;
1983 }
1984 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 2) {
1985 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] =
1986 source_sad_nonrd >= kMedSad ? 1 : 0;
1987 }
1988 return;
1989 }
Marco Paniconi35c281a2022-06-08 12:19:03 -07001990 int shift = 3;
Marco Paniconiaea4a5c2023-11-14 10:25:08 -08001991 unsigned int source_var_thr = 50;
1992 int uv_sad_thr = 100;
Marco Paniconidcba6b22023-08-03 16:21:09 -07001993 if (source_sad_nonrd >= kMedSad && x->source_variance > 0 && high_res)
Marco Paniconid4b429f2023-07-27 16:06:22 -07001994 shift = 4;
Marco Paniconiaea4a5c2023-11-14 10:25:08 -08001995 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
1996 if (cpi->rc.high_source_sad) shift = 6;
1997 if (source_sad_nonrd > kMedSad) {
1998 source_var_thr = 1200;
1999 uv_sad_thr = 10;
2000 }
Marco Paniconi35c281a2022-06-08 12:19:03 -07002001 }
Marco Paniconi737820e2021-05-24 11:03:38 -07002002 NOISE_LEVEL noise_level = kLow;
2003 int norm_sad =
2004 y_sad >> (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
Marco Paniconi42223ee2022-06-20 22:25:02 -07002005 unsigned int thresh_spatial = (cpi->common.width > 1920) ? 5000 : 1000;
Marco Paniconi737820e2021-05-24 11:03:38 -07002006 // If the spatial source variance is high and the normalized y_sad
2007 // is low, then y-channel is likely good for mode estimation, so keep
2008 // color_sensitivity off. For low noise content for now, since there is
2009 // some bdrate regression for noisy color clip.
2010 if (cpi->noise_estimate.enabled)
2011 noise_level = av1_noise_estimate_extract_level(&cpi->noise_estimate);
Marco Paniconi42223ee2022-06-20 22:25:02 -07002012 if (noise_level == kLow && source_variance > thresh_spatial &&
Marco Paniconic57620a2022-10-19 14:14:39 -07002013 cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN && norm_sad < 50) {
Anupam Pandey45caf952022-12-15 11:16:15 +05302014 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] = 0;
2015 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] = 0;
Marco Paniconi737820e2021-05-24 11:03:38 -07002016 return;
2017 }
James Zernbf9d1f82022-10-27 18:17:47 -07002018 const int num_planes = av1_num_planes(&cpi->common);
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302019
2020 for (int plane = AOM_PLANE_U; plane < num_planes; ++plane) {
Marco Paniconidcba6b22023-08-03 16:21:09 -07002021 // Always check if level = 2. If level = 0 check again for
2022 // motion areas for higher resolns, where color artifacts
2023 // are more noticeable.
Anupam Pandey8b325e72022-12-22 01:51:39 +05302024 if (x->color_sensitivity[COLOR_SENS_IDX(plane)] == 2 ||
Marco Paniconidcba6b22023-08-03 16:21:09 -07002025 (x->color_sensitivity[COLOR_SENS_IDX(plane)] == 0 &&
2026 source_sad_nonrd >= kMedSad && high_res)) {
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302027 struct macroblock_plane *const p = &x->plane[plane];
Marco Paniconi737820e2021-05-24 11:03:38 -07002028 const BLOCK_SIZE bs =
Deepa K G309d0af2022-07-15 00:17:17 +05302029 get_plane_block_size(bsize, subsampling_x, subsampling_y);
2030
2031 const int uv_sad = cpi->ppi->fn_ptr[bs].sdf(
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302032 p->src.buf, p->src.stride, yv12_mb[plane].buf, yv12_mb[plane].stride);
Deepa K G309d0af2022-07-15 00:17:17 +05302033
Marco Paniconi737820e2021-05-24 11:03:38 -07002034 const int norm_uv_sad =
2035 uv_sad >> (b_width_log2_lookup[bs] + b_height_log2_lookup[bs]);
Anupam Pandey8b325e72022-12-22 01:51:39 +05302036 x->color_sensitivity[COLOR_SENS_IDX(plane)] =
Marco Paniconi058ced72023-02-22 23:28:46 -08002037 uv_sad > (y_sad >> shift) && norm_uv_sad > 40;
Marco Paniconiaea4a5c2023-11-14 10:25:08 -08002038 if (source_variance < source_var_thr && norm_uv_sad > uv_sad_thr)
Anupam Pandey8b325e72022-12-22 01:51:39 +05302039 x->color_sensitivity[COLOR_SENS_IDX(plane)] = 1;
Marco Paniconi737820e2021-05-24 11:03:38 -07002040 }
2041 }
2042}
2043
James Zernfa7042e2022-10-27 18:25:57 -07002044static void setup_compound_prediction(const AV1_COMMON *cm, MACROBLOCK *x,
2045 struct buf_2d yv12_mb[8][MAX_MB_PLANE],
2046 const int *use_ref_frame_mask,
2047 const MV_REFERENCE_FRAME *rf,
2048 int *ref_mv_idx) {
Marco Paniconi3c87aae2021-07-28 10:55:25 -07002049 MACROBLOCKD *const xd = &x->e_mbd;
2050 MB_MODE_INFO *const mbmi = xd->mi[0];
2051 MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
Marco Paniconi3c87aae2021-07-28 10:55:25 -07002052 MV_REFERENCE_FRAME ref_frame_comp;
Marco Paniconi3c87aae2021-07-28 10:55:25 -07002053 if (!use_ref_frame_mask[rf[1]]) {
2054 // Need to setup pred_block, if it hasn't been done in find_predictors.
2055 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, rf[1]);
2056 const int num_planes = av1_num_planes(cm);
2057 if (yv12 != NULL) {
2058 const struct scale_factors *const sf =
2059 get_ref_scale_factors_const(cm, rf[1]);
2060 av1_setup_pred_block(xd, yv12_mb[rf[1]], yv12, sf, sf, num_planes);
2061 }
2062 }
2063 ref_frame_comp = av1_ref_frame_type(rf);
2064 mbmi_ext->mode_context[ref_frame_comp] = 0;
2065 mbmi_ext->ref_mv_count[ref_frame_comp] = UINT8_MAX;
2066 av1_find_mv_refs(cm, xd, mbmi, ref_frame_comp, mbmi_ext->ref_mv_count,
2067 xd->ref_mv_stack, xd->weight, NULL, mbmi_ext->global_mvs,
2068 mbmi_ext->mode_context);
2069 av1_copy_usable_ref_mv_stack_and_weight(xd, mbmi_ext, ref_frame_comp);
2070 *ref_mv_idx = mbmi->ref_mv_idx + 1;
2071}
2072
Anupam Pandeycf5a52e2022-12-14 18:25:44 +05302073static void set_compound_mode(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame,
2074 MV_REFERENCE_FRAME ref_frame2, int ref_mv_idx,
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08002075 int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],
chiyotsaid4e5cef2022-07-19 14:48:24 -07002076 PREDICTION_MODE this_mode) {
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08002077 MACROBLOCKD *const xd = &x->e_mbd;
2078 MB_MODE_INFO *const mi = xd->mi[0];
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08002079 mi->ref_frame[0] = ref_frame;
2080 mi->ref_frame[1] = ref_frame2;
2081 mi->compound_idx = 1;
2082 mi->comp_group_idx = 0;
2083 mi->interinter_comp.type = COMPOUND_AVERAGE;
Marco Paniconi3c87aae2021-07-28 10:55:25 -07002084 MV_REFERENCE_FRAME ref_frame_comp = av1_ref_frame_type(mi->ref_frame);
chiyotsaid4e5cef2022-07-19 14:48:24 -07002085 if (this_mode == GLOBAL_GLOBALMV) {
2086 frame_mv[this_mode][ref_frame].as_int = 0;
2087 frame_mv[this_mode][ref_frame2].as_int = 0;
2088 } else if (this_mode == NEAREST_NEARESTMV) {
2089 frame_mv[this_mode][ref_frame].as_int =
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08002090 xd->ref_mv_stack[ref_frame_comp][0].this_mv.as_int;
chiyotsaid4e5cef2022-07-19 14:48:24 -07002091 frame_mv[this_mode][ref_frame2].as_int =
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08002092 xd->ref_mv_stack[ref_frame_comp][0].comp_mv.as_int;
chiyotsaid4e5cef2022-07-19 14:48:24 -07002093 } else if (this_mode == NEAR_NEARMV) {
2094 frame_mv[this_mode][ref_frame].as_int =
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08002095 xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].this_mv.as_int;
chiyotsaid4e5cef2022-07-19 14:48:24 -07002096 frame_mv[this_mode][ref_frame2].as_int =
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08002097 xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].comp_mv.as_int;
2098 }
2099}
2100
chiyotsaia82b98f2022-08-17 15:59:47 -07002101// Prune compound mode if the single mode variance is lower than a fixed
2102// percentage of the median value.
2103static bool skip_comp_based_on_var(
2104 const unsigned int (*single_vars)[REF_FRAMES], BLOCK_SIZE bsize) {
2105 unsigned int best_var = UINT_MAX;
2106 for (int cur_mode_idx = 0; cur_mode_idx < RTC_INTER_MODES; cur_mode_idx++) {
2107 for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
2108 best_var = AOMMIN(best_var, single_vars[cur_mode_idx][ref_idx]);
2109 }
2110 }
2111 const unsigned int thresh_64 = (unsigned int)(0.57356805f * 8659);
2112 const unsigned int thresh_32 = (unsigned int)(0.23964763f * 4281);
2113
chiyotsai2effced2022-08-31 13:15:08 -07002114 // Currently, the thresh for 128 and 16 are not well-tuned. We are using the
2115 // results from 64 and 32 as an heuristic.
2116 switch (bsize) {
2117 case BLOCK_128X128: return best_var < 4 * thresh_64;
2118 case BLOCK_64X64: return best_var < thresh_64;
2119 case BLOCK_32X32: return best_var < thresh_32;
2120 case BLOCK_16X16: return best_var < thresh_32 / 4;
2121 default: return false;
chiyotsaia82b98f2022-08-17 15:59:47 -07002122 }
chiyotsaia82b98f2022-08-17 15:59:47 -07002123}
2124
chiyotsai901f4062022-07-06 14:57:53 -07002125static AOM_FORCE_INLINE void fill_single_inter_mode_costs(
Yunqing Wangddc0bf92022-12-15 11:17:40 -08002126 int (*single_inter_mode_costs)[REF_FRAMES], int num_inter_modes,
linzhene4954c22022-09-20 18:27:52 +00002127 const REF_MODE *reference_mode_set, const ModeCosts *mode_costs,
chiyotsai901f4062022-07-06 14:57:53 -07002128 const int16_t *mode_context) {
2129 bool ref_frame_used[REF_FRAMES] = { false };
2130 for (int idx = 0; idx < num_inter_modes; idx++) {
linzhene4954c22022-09-20 18:27:52 +00002131 ref_frame_used[reference_mode_set[idx].ref_frame] = true;
chiyotsai901f4062022-07-06 14:57:53 -07002132 }
2133
2134 for (int this_ref_frame = LAST_FRAME; this_ref_frame < REF_FRAMES;
2135 this_ref_frame++) {
2136 if (!ref_frame_used[this_ref_frame]) {
2137 continue;
2138 }
2139
2140 const MV_REFERENCE_FRAME rf[2] = { this_ref_frame, NONE_FRAME };
2141 const int16_t mode_ctx = av1_mode_context_analyzer(mode_context, rf);
2142 for (PREDICTION_MODE this_mode = NEARESTMV; this_mode <= NEWMV;
2143 this_mode++) {
2144 single_inter_mode_costs[INTER_OFFSET(this_mode)][this_ref_frame] =
2145 cost_mv_ref(mode_costs, this_mode, mode_ctx);
2146 }
2147 }
2148}
2149
2150static AOM_INLINE bool is_globalmv_better(
2151 PREDICTION_MODE this_mode, MV_REFERENCE_FRAME ref_frame, int rate_mv,
2152 const ModeCosts *mode_costs,
2153 const int (*single_inter_mode_costs)[REF_FRAMES],
2154 const MB_MODE_INFO_EXT *mbmi_ext) {
2155 const int globalmv_mode_cost =
2156 single_inter_mode_costs[INTER_OFFSET(GLOBALMV)][ref_frame];
2157 int this_mode_cost =
2158 rate_mv + single_inter_mode_costs[INTER_OFFSET(this_mode)][ref_frame];
2159 if (this_mode == NEWMV || this_mode == NEARMV) {
2160 const MV_REFERENCE_FRAME rf[2] = { ref_frame, NONE_FRAME };
2161 this_mode_cost += get_drl_cost(
2162 NEWMV, 0, mbmi_ext, mode_costs->drl_mode_cost0, av1_ref_frame_type(rf));
2163 }
2164 return this_mode_cost > globalmv_mode_cost;
2165}
2166
chiyotsaid4e5cef2022-07-19 14:48:24 -07002167// Set up the mv/ref_frames etc based on the comp_index. Returns 1 if it
2168// succeeds, 0 if it fails.
2169static AOM_INLINE int setup_compound_params_from_comp_idx(
2170 const AV1_COMP *cpi, MACROBLOCK *x, struct buf_2d yv12_mb[8][MAX_MB_PLANE],
2171 PREDICTION_MODE *this_mode, MV_REFERENCE_FRAME *ref_frame,
2172 MV_REFERENCE_FRAME *ref_frame2, int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],
2173 const int *use_ref_frame_mask, int comp_index,
Marco Paniconi555b3aa2023-03-03 09:40:28 -08002174 bool comp_use_zero_zeromv_only, MV_REFERENCE_FRAME *last_comp_ref_frame,
2175 BLOCK_SIZE bsize) {
chiyotsaid4e5cef2022-07-19 14:48:24 -07002176 const MV_REFERENCE_FRAME *rf = comp_ref_mode_set[comp_index].ref_frame;
Marco Paniconi555b3aa2023-03-03 09:40:28 -08002177 int skip_gf = 0;
2178 int skip_alt = 0;
chiyotsaid4e5cef2022-07-19 14:48:24 -07002179 *this_mode = comp_ref_mode_set[comp_index].pred_mode;
2180 *ref_frame = rf[0];
2181 *ref_frame2 = rf[1];
2182 assert(*ref_frame == LAST_FRAME);
2183 assert(*this_mode == GLOBAL_GLOBALMV || *this_mode == NEAREST_NEARESTMV);
Marco Paniconi555b3aa2023-03-03 09:40:28 -08002184 if (x->source_variance < 50 && bsize > BLOCK_16X16) {
2185 if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2186 x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
2187 skip_gf = 1;
2188 if (x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2189 x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
2190 skip_alt = 1;
2191 }
chiyotsaid4e5cef2022-07-19 14:48:24 -07002192 if (comp_use_zero_zeromv_only && *this_mode != GLOBAL_GLOBALMV) {
2193 return 0;
2194 }
2195 if (*ref_frame2 == GOLDEN_FRAME &&
Marco Paniconi555b3aa2023-03-03 09:40:28 -08002196 (cpi->sf.rt_sf.ref_frame_comp_nonrd[0] == 0 || skip_gf ||
chiyotsaid4e5cef2022-07-19 14:48:24 -07002197 !(cpi->ref_frame_flags & AOM_GOLD_FLAG))) {
2198 return 0;
2199 } else if (*ref_frame2 == LAST2_FRAME &&
2200 (cpi->sf.rt_sf.ref_frame_comp_nonrd[1] == 0 ||
2201 !(cpi->ref_frame_flags & AOM_LAST2_FLAG))) {
2202 return 0;
2203 } else if (*ref_frame2 == ALTREF_FRAME &&
Marco Paniconi555b3aa2023-03-03 09:40:28 -08002204 (cpi->sf.rt_sf.ref_frame_comp_nonrd[2] == 0 || skip_alt ||
chiyotsaid4e5cef2022-07-19 14:48:24 -07002205 !(cpi->ref_frame_flags & AOM_ALT_FLAG))) {
2206 return 0;
2207 }
2208 int ref_mv_idx = 0;
2209 if (*last_comp_ref_frame != rf[1]) {
2210 // Only needs to be done once per reference pair.
2211 setup_compound_prediction(&cpi->common, x, yv12_mb, use_ref_frame_mask, rf,
2212 &ref_mv_idx);
2213 *last_comp_ref_frame = rf[1];
2214 }
2215 set_compound_mode(x, *ref_frame, *ref_frame2, ref_mv_idx, frame_mv,
2216 *this_mode);
2217 if (*this_mode != GLOBAL_GLOBALMV &&
2218 frame_mv[*this_mode][*ref_frame].as_int == 0 &&
2219 frame_mv[*this_mode][*ref_frame2].as_int == 0) {
2220 return 0;
2221 }
2222
2223 return 1;
2224}
2225
chiyotsai1538b072022-07-26 10:25:14 -07002226static AOM_INLINE bool previous_mode_performed_poorly(
2227 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame,
2228 const unsigned int (*vars)[REF_FRAMES],
2229 const int64_t (*uv_dist)[REF_FRAMES]) {
2230 unsigned int best_var = UINT_MAX;
2231 int64_t best_uv_dist = INT64_MAX;
2232 for (int midx = 0; midx < RTC_INTER_MODES; midx++) {
2233 best_var = AOMMIN(best_var, vars[midx][ref_frame]);
2234 best_uv_dist = AOMMIN(best_uv_dist, uv_dist[midx][ref_frame]);
2235 }
2236 assert(best_var != UINT_MAX && "Invalid variance data.");
2237 const float mult = 1.125f;
2238 bool var_bad = mult * best_var < vars[INTER_OFFSET(mode)][ref_frame];
2239 if (uv_dist[INTER_OFFSET(mode)][ref_frame] < INT64_MAX &&
2240 best_uv_dist != uv_dist[INTER_OFFSET(mode)][ref_frame]) {
2241 // If we have chroma info, then take it into account
2242 var_bad &= mult * best_uv_dist < uv_dist[INTER_OFFSET(mode)][ref_frame];
2243 }
2244 return var_bad;
2245}
2246
2247static AOM_INLINE bool prune_compoundmode_with_singlemode_var(
2248 PREDICTION_MODE compound_mode, MV_REFERENCE_FRAME ref_frame,
2249 MV_REFERENCE_FRAME ref_frame2, const int_mv (*frame_mv)[REF_FRAMES],
2250 const uint8_t (*mode_checked)[REF_FRAMES],
2251 const unsigned int (*vars)[REF_FRAMES],
2252 const int64_t (*uv_dist)[REF_FRAMES]) {
2253 const PREDICTION_MODE single_mode0 = compound_ref0_mode(compound_mode);
2254 const PREDICTION_MODE single_mode1 = compound_ref1_mode(compound_mode);
2255
2256 bool first_ref_valid = false, second_ref_valid = false;
2257 bool first_ref_bad = false, second_ref_bad = false;
2258 if (mode_checked[single_mode0][ref_frame] &&
2259 frame_mv[single_mode0][ref_frame].as_int ==
2260 frame_mv[compound_mode][ref_frame].as_int &&
2261 vars[INTER_OFFSET(single_mode0)][ref_frame] < UINT_MAX) {
2262 first_ref_valid = true;
2263 first_ref_bad =
2264 previous_mode_performed_poorly(single_mode0, ref_frame, vars, uv_dist);
2265 }
2266 if (mode_checked[single_mode1][ref_frame2] &&
2267 frame_mv[single_mode1][ref_frame2].as_int ==
2268 frame_mv[compound_mode][ref_frame2].as_int &&
2269 vars[INTER_OFFSET(single_mode1)][ref_frame2] < UINT_MAX) {
2270 second_ref_valid = true;
2271 second_ref_bad =
2272 previous_mode_performed_poorly(single_mode1, ref_frame2, vars, uv_dist);
2273 }
2274 if (first_ref_valid && second_ref_valid) {
2275 return first_ref_bad && second_ref_bad;
2276 } else if (first_ref_valid || second_ref_valid) {
2277 return first_ref_bad || second_ref_bad;
2278 }
2279 return false;
2280}
2281
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302282// Function to setup parameters used for inter mode evaluation in non-rd.
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302283static AOM_FORCE_INLINE void set_params_nonrd_pick_inter_mode(
2284 AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
Marco Paniconidcba6b22023-08-03 16:21:09 -07002285 RD_STATS *rd_cost, int *force_skip_low_temp_var, int mi_row, int mi_col,
2286 int gf_temporal_ref, unsigned char segment_id, BLOCK_SIZE bsize
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302287#if CONFIG_AV1_TEMPORAL_DENOISING
2288 ,
Anupam Pandey23cd3392022-12-22 11:34:09 +05302289 PICK_MODE_CONTEXT *ctx, int denoise_svc_pickmode
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302290#endif
2291) {
2292 AV1_COMMON *const cm = &cpi->common;
2293 MACROBLOCKD *const xd = &x->e_mbd;
2294 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
2295 MB_MODE_INFO *const mi = xd->mi[0];
2296 const ModeCosts *mode_costs = &x->mode_costs;
Marco Paniconidcba6b22023-08-03 16:21:09 -07002297 int skip_pred_mv = 0;
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302298
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302299 // Initialize variance and distortion (chroma) for all modes and reference
2300 // frames
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302301 for (int idx = 0; idx < RTC_INTER_MODES; idx++) {
2302 for (int ref = 0; ref < REF_FRAMES; ref++) {
2303 search_state->vars[idx][ref] = UINT_MAX;
2304 search_state->uv_dist[idx][ref] = INT64_MAX;
2305 }
2306 }
2307
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302308 // Initialize values of color sensitivity with sb level color sensitivity
Anupam Pandey45caf952022-12-15 11:16:15 +05302309 av1_copy(x->color_sensitivity, x->color_sensitivity_sb);
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302310
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302311 init_best_pickmode(&search_state->best_pickmode);
2312
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302313 // Estimate cost for single reference frames
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302314 estimate_single_ref_frame_costs(cm, xd, mode_costs, segment_id, bsize,
2315 search_state->ref_costs_single);
2316
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302317 // Reset flag to indicate modes evaluated
Anupam Pandeya080bf62022-12-15 16:52:25 +05302318 av1_zero(search_state->mode_checked);
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302319
2320 txfm_info->skip_txfm = 0;
2321
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302322 // Initialize mode decisions
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302323 av1_invalid_rd_stats(&search_state->best_rdc);
2324 av1_invalid_rd_stats(&search_state->this_rdc);
2325 av1_invalid_rd_stats(rd_cost);
Anupam Pandeycbe4eb42022-12-21 12:44:47 +05302326 for (int ref_idx = 0; ref_idx < REF_FRAMES; ++ref_idx) {
2327 x->warp_sample_info[ref_idx].num = -1;
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302328 }
2329
2330 mi->bsize = bsize;
2331 mi->ref_frame[0] = NONE_FRAME;
2332 mi->ref_frame[1] = NONE_FRAME;
2333
2334#if CONFIG_AV1_TEMPORAL_DENOISING
2335 if (cpi->oxcf.noise_sensitivity > 0) {
2336 // if (cpi->ppi->use_svc) denoise_svc_pickmode =
2337 // av1_denoise_svc_non_key(cpi);
2338 if (cpi->denoiser.denoising_level > kDenLowLow && denoise_svc_pickmode)
2339 av1_denoiser_reset_frame_stats(ctx);
2340 }
2341#endif
2342
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302343 // Populate predicated motion vectors for LAST_FRAME
Marco Paniconia1f29682023-09-17 20:09:51 -07002344 if (cpi->ref_frame_flags & AOM_LAST_FLAG) {
Anupam Pandey05c81f82022-12-21 12:58:34 +05302345 find_predictors(cpi, x, LAST_FRAME, search_state->frame_mv,
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302346 search_state->yv12_mb, bsize, *force_skip_low_temp_var,
Marco Paniconi0aeecfe2023-10-04 00:06:46 -07002347 x->force_zeromv_skip_for_blk,
2348 &search_state->use_scaled_ref_frame[LAST_FRAME]);
Marco Paniconia1f29682023-09-17 20:09:51 -07002349 }
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302350 // Update mask to use all reference frame
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302351 get_ref_frame_use_mask(cpi, x, mi, mi_row, mi_col, bsize, gf_temporal_ref,
2352 search_state->use_ref_frame_mask,
2353 force_skip_low_temp_var);
2354
Marco Paniconidcba6b22023-08-03 16:21:09 -07002355 skip_pred_mv = x->force_zeromv_skip_for_blk ||
2356 (x->nonrd_prune_ref_frame_search > 2 &&
2357 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] != 2 &&
2358 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] != 2);
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302359
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302360 // Populate predicated motion vectors for other single reference frame
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302361 // Start at LAST_FRAME + 1.
2362 for (MV_REFERENCE_FRAME ref_frame_iter = LAST_FRAME + 1;
2363 ref_frame_iter <= ALTREF_FRAME; ++ref_frame_iter) {
2364 if (search_state->use_ref_frame_mask[ref_frame_iter]) {
Anupam Pandey05c81f82022-12-21 12:58:34 +05302365 find_predictors(cpi, x, ref_frame_iter, search_state->frame_mv,
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302366 search_state->yv12_mb, bsize, *force_skip_low_temp_var,
Marco Paniconi0aeecfe2023-10-04 00:06:46 -07002367 skip_pred_mv,
2368 &search_state->use_scaled_ref_frame[ref_frame_iter]);
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05302369 }
2370 }
2371}
2372
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302373// Function to check the inter mode can be skipped based on mode statistics and
2374// speed features settings.
2375static AOM_FORCE_INLINE bool skip_inter_mode_nonrd(
2376 AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302377 int64_t *thresh_sad_pred, int *force_mv_inter_layer, int *is_single_pred,
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302378 PREDICTION_MODE *this_mode, MV_REFERENCE_FRAME *last_comp_ref_frame,
2379 MV_REFERENCE_FRAME *ref_frame, MV_REFERENCE_FRAME *ref_frame2, int idx,
Anupam Pandeycf5a52e2022-12-14 18:25:44 +05302380 int_mv svc_mv, int force_skip_low_temp_var, unsigned int sse_zeromv_norm,
Yunqing Wangddc0bf92022-12-15 11:17:40 -08002381 int num_inter_modes, unsigned char segment_id, BLOCK_SIZE bsize,
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302382 bool comp_use_zero_zeromv_only, bool check_globalmv) {
2383 AV1_COMMON *const cm = &cpi->common;
2384 const struct segmentation *const seg = &cm->seg;
2385 const SVC *const svc = &cpi->svc;
2386 MACROBLOCKD *const xd = &x->e_mbd;
2387 MB_MODE_INFO *const mi = xd->mi[0];
Anupam Pandeyab370452022-12-14 11:23:34 +05302388 const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302389
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302390 // Skip compound mode based on reference frame mask and type of the mode and
2391 // for allowed compound modes, setup ref mv stack and reference frame.
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302392 if (idx >= num_inter_modes) {
2393 const int comp_index = idx - num_inter_modes;
2394 if (!setup_compound_params_from_comp_idx(
2395 cpi, x, search_state->yv12_mb, this_mode, ref_frame, ref_frame2,
2396 search_state->frame_mv, search_state->use_ref_frame_mask,
Marco Paniconi555b3aa2023-03-03 09:40:28 -08002397 comp_index, comp_use_zero_zeromv_only, last_comp_ref_frame,
2398 bsize)) {
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302399 return true;
2400 }
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302401 *is_single_pred = 0;
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302402 } else {
2403 *this_mode = ref_mode_set[idx].pred_mode;
2404 *ref_frame = ref_mode_set[idx].ref_frame;
2405 *ref_frame2 = NONE_FRAME;
2406 }
2407
Marco Paniconid4043472024-03-28 15:18:58 -07002408 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) &&
2409 (*this_mode != GLOBALMV || *ref_frame != LAST_FRAME))
2410 return true;
2411
Marco Paniconi608f9242024-04-08 10:34:11 -07002412 // Skip the mode if use reference frame mask flag is not set.
2413 if (!search_state->use_ref_frame_mask[*ref_frame]) return true;
2414
2415 // Skip mode for some modes and reference frames when
2416 // force_zeromv_skip_for_blk flag is true.
2417 if (x->force_zeromv_skip_for_blk &&
2418 ((!(*this_mode == NEARESTMV &&
2419 search_state->frame_mv[*this_mode][*ref_frame].as_int == 0) &&
2420 *this_mode != GLOBALMV) ||
2421 *ref_frame != LAST_FRAME))
2422 return true;
2423
Marco Paniconi5a4ea092023-08-09 14:36:15 -07002424 if (x->sb_me_block && *ref_frame == LAST_FRAME) {
2425 // We want to make sure to test the superblock MV:
2426 // so don't skip (return false) for NEAREST_LAST or NEAR_LAST if they
2427 // have this sb MV. And don't skip NEWMV_LAST: this will be set to
2428 // sb MV in handle_inter_mode_nonrd(), in case NEAREST or NEAR don't
2429 // have it.
2430 if (*this_mode == NEARESTMV &&
2431 search_state->frame_mv[NEARESTMV][LAST_FRAME].as_int ==
2432 x->sb_me_mv.as_int) {
2433 return false;
2434 }
2435 if (*this_mode == NEARMV &&
2436 search_state->frame_mv[NEARMV][LAST_FRAME].as_int ==
2437 x->sb_me_mv.as_int) {
2438 return false;
2439 }
2440 if (*this_mode == NEWMV) {
2441 return false;
2442 }
2443 }
2444
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302445 // Skip the single reference mode for which mode check flag is set.
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302446 if (*is_single_pred && search_state->mode_checked[*this_mode][*ref_frame]) {
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302447 return true;
2448 }
2449
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302450 // Skip GLOBALMV mode if check_globalmv flag is not enabled.
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302451 if (!check_globalmv && *this_mode == GLOBALMV) {
2452 return true;
2453 }
2454
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302455#if COLLECT_NONRD_PICK_MODE_STAT
2456 aom_usec_timer_start(&x->ms_stat_nonrd.timer1);
2457 x->ms_stat_nonrd.num_searches[bsize][*this_mode]++;
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302458#endif
2459 mi->mode = *this_mode;
2460 mi->ref_frame[0] = *ref_frame;
2461 mi->ref_frame[1] = *ref_frame2;
2462
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302463 // Skip compound mode based on variance of previously evaluated single
2464 // reference modes.
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302465 if (rt_sf->prune_compoundmode_with_singlemode_var && !*is_single_pred &&
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302466 prune_compoundmode_with_singlemode_var(
2467 *this_mode, *ref_frame, *ref_frame2, search_state->frame_mv,
2468 search_state->mode_checked, search_state->vars,
2469 search_state->uv_dist)) {
2470 return true;
2471 }
2472
2473 *force_mv_inter_layer = 0;
2474 if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
2475 ((*ref_frame == LAST_FRAME && svc->skip_mvsearch_last) ||
2476 (*ref_frame == GOLDEN_FRAME && svc->skip_mvsearch_gf) ||
2477 (*ref_frame == ALTREF_FRAME && svc->skip_mvsearch_altref))) {
Anupam Pandeycf5a52e2022-12-14 18:25:44 +05302478 // Only test mode if NEARESTMV/NEARMV is (svc_mv.mv.col, svc_mv.mv.row),
2479 // otherwise set NEWMV to (svc_mv.mv.col, svc_mv.mv.row).
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302480 // Skip newmv and filter search.
2481 *force_mv_inter_layer = 1;
2482 if (*this_mode == NEWMV) {
Anupam Pandeycf5a52e2022-12-14 18:25:44 +05302483 search_state->frame_mv[*this_mode][*ref_frame] = svc_mv;
2484 } else if (search_state->frame_mv[*this_mode][*ref_frame].as_int !=
2485 svc_mv.as_int) {
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302486 return true;
2487 }
2488 }
2489
2490 // If the segment reference frame feature is enabled then do nothing if the
2491 // current ref frame is not allowed.
2492 if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
2493 get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)(*ref_frame))
2494 return true;
2495
Marco Paniconia33c8412023-09-29 14:25:46 -07002496 // For screen content: skip mode testing based on source_sad.
Marco Paniconibdada5c2024-05-22 14:30:55 -07002497 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
2498 !x->force_zeromv_skip_for_blk) {
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302499 // If source_sad is computed: skip non-zero motion
2500 // check for stationary (super)blocks. Otherwise if superblock
Marco Paniconia33c8412023-09-29 14:25:46 -07002501 // has motion skip the modes with zero motion on last reference
2502 // for flat blocks, and color is not set.
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302503 // For the latter condition: the same condition should apply
2504 // to newmv if (0, 0), so this latter condition is repeated
2505 // below after search_new_mv.
Anupam Pandeyab370452022-12-14 11:23:34 +05302506 if (rt_sf->source_metrics_sb_nonrd) {
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302507 if ((search_state->frame_mv[*this_mode][*ref_frame].as_int != 0 &&
2508 x->content_state_sb.source_sad_nonrd == kZeroSad) ||
2509 (search_state->frame_mv[*this_mode][*ref_frame].as_int == 0 &&
Marco Paniconia33c8412023-09-29 14:25:46 -07002510 x->block_is_zero_sad == 0 && *ref_frame == LAST_FRAME &&
Marco Paniconi9a3b7682023-07-10 10:33:13 -07002511 ((x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
2512 x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) ||
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302513 cpi->rc.high_source_sad) &&
2514 x->source_variance == 0))
2515 return true;
2516 }
2517 // Skip NEWMV search for flat blocks.
Marco Paniconi62732c82024-04-30 15:24:49 -07002518 if (rt_sf->skip_newmv_flat_blocks_screen && *this_mode == NEWMV &&
2519 x->source_variance < 100)
2520 return true;
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302521 // Skip non-LAST for color on flat blocks.
2522 if (*ref_frame > LAST_FRAME && x->source_variance == 0 &&
Anupam Pandey45caf952022-12-15 11:16:15 +05302523 (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2524 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 1))
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302525 return true;
2526 }
2527
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302528 // Skip mode based on block size, reference frame mode and other block
2529 // properties.
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302530 if (skip_mode_by_bsize_and_ref_frame(
2531 *this_mode, *ref_frame, bsize, x->nonrd_prune_ref_frame_search,
Marco Paniconif9690602024-04-08 13:53:02 -07002532 sse_zeromv_norm, rt_sf->nonrd_aggressive_skip,
2533 rt_sf->increase_source_sad_thresh))
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302534 return true;
2535
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302536 // Skip mode based on low temporal variance and souce sad.
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302537 if (skip_mode_by_low_temp(*this_mode, *ref_frame, bsize, x->content_state_sb,
2538 search_state->frame_mv[*this_mode][*ref_frame],
2539 force_skip_low_temp_var))
2540 return true;
2541
2542 // Disable this drop out case if the ref frame segment level feature is
2543 // enabled for this segment. This is to prevent the possibility that we
2544 // end up unable to pick any mode.
2545 if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
2546 // Check for skipping GOLDEN and ALTREF based pred_mv_sad.
Anupam Pandeyab370452022-12-14 11:23:34 +05302547 if (rt_sf->nonrd_prune_ref_frame_search > 0 &&
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302548 x->pred_mv_sad[*ref_frame] != INT_MAX && *ref_frame != LAST_FRAME) {
2549 if ((int64_t)(x->pred_mv_sad[*ref_frame]) > *thresh_sad_pred) return true;
2550 }
2551 }
2552
2553 // Check for skipping NEARMV based on pred_mv_sad.
2554 if (*this_mode == NEARMV && x->pred_mv1_sad[*ref_frame] != INT_MAX &&
2555 x->pred_mv1_sad[*ref_frame] > (x->pred_mv0_sad[*ref_frame] << 1))
2556 return true;
2557
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302558 // Skip single reference mode based on rd threshold.
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302559 if (*is_single_pred) {
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302560 if (skip_mode_by_threshold(
2561 *this_mode, *ref_frame,
2562 search_state->frame_mv[*this_mode][*ref_frame],
2563 cpi->rc.frames_since_golden, cpi->rd.threshes[segment_id][bsize],
2564 x->thresh_freq_fact[bsize], search_state->best_rdc.rdcost,
2565 search_state->best_pickmode.best_mode_skip_txfm,
Anupam Pandeyab370452022-12-14 11:23:34 +05302566 (rt_sf->nonrd_aggressive_skip ? 1 : 0)))
Anupam Pandeya61e10a2022-12-06 16:49:32 +05302567 return true;
2568 }
2569 return false;
2570}
2571
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302572// Function to perform inter mode evaluation for non-rd
2573static AOM_FORCE_INLINE bool handle_inter_mode_nonrd(
2574 AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
2575 PICK_MODE_CONTEXT *ctx, PRED_BUFFER **this_mode_pred,
2576 PRED_BUFFER *tmp_buffer, InterPredParams inter_pred_params_sr,
2577 int *best_early_term, unsigned int *sse_zeromv_norm, bool *check_globalmv,
2578#if CONFIG_AV1_TEMPORAL_DENOISING
2579 int64_t *zero_last_cost_orig, int denoise_svc_pickmode,
2580#endif
Marco Paniconi9a3b7682023-07-10 10:33:13 -07002581 int idx, int force_mv_inter_layer, int is_single_pred, int gf_temporal_ref,
2582 int use_model_yrd_large, int filter_search_enabled_blk, BLOCK_SIZE bsize,
2583 PREDICTION_MODE this_mode, InterpFilter filt_select,
Marco Paniconi5a4ea092023-08-09 14:36:15 -07002584 int cb_pred_filter_search, int reuse_inter_pred,
2585 int *sb_me_has_been_tested) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302586 AV1_COMMON *const cm = &cpi->common;
2587 MACROBLOCKD *const xd = &x->e_mbd;
2588 MB_MODE_INFO *const mi = xd->mi[0];
2589 const MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
2590 const int mi_row = xd->mi_row;
2591 const int mi_col = xd->mi_col;
Anupam Pandey45caf952022-12-15 11:16:15 +05302592 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302593 const int bw = block_size_wide[bsize];
2594 const InterpFilter filter_ref = cm->features.interp_filter;
2595 const InterpFilter default_interp_filter = EIGHTTAP_REGULAR;
2596 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
2597 const ModeCosts *mode_costs = &x->mode_costs;
Anupam Pandeyab370452022-12-14 11:23:34 +05302598 const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
Anupam Pandey2b4957c2022-12-19 16:39:52 +05302599 BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode;
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302600
2601 MV_REFERENCE_FRAME ref_frame = mi->ref_frame[0];
2602 MV_REFERENCE_FRAME ref_frame2 = mi->ref_frame[1];
Anupam Pandeyab370452022-12-14 11:23:34 +05302603 int_mv *const this_mv = &search_state->frame_mv[this_mode][ref_frame];
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302604 unsigned int var = UINT_MAX;
2605 int this_early_term = 0;
2606 int rate_mv = 0;
2607 int is_skippable;
2608 int skip_this_mv = 0;
2609 unsigned int var_threshold = UINT_MAX;
2610 PREDICTION_MODE this_best_mode;
2611 RD_STATS nonskip_rdc;
2612 av1_invalid_rd_stats(&nonskip_rdc);
2613
Marco Paniconi5a4ea092023-08-09 14:36:15 -07002614 if (x->sb_me_block && this_mode == NEWMV && ref_frame == LAST_FRAME) {
2615 // Set the NEWMV_LAST to the sb MV.
2616 search_state->frame_mv[NEWMV][LAST_FRAME].as_int = x->sb_me_mv.as_int;
2617 } else if (this_mode == NEWMV && !force_mv_inter_layer) {
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302618#if COLLECT_NONRD_PICK_MODE_STAT
2619 aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302620#endif
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302621 // Find the best motion vector for single/compound mode.
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302622 const bool skip_newmv = search_new_mv(
2623 cpi, x, search_state->frame_mv, ref_frame, gf_temporal_ref, bsize,
2624 mi_row, mi_col, &rate_mv, &search_state->best_rdc);
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302625#if COLLECT_NONRD_PICK_MODE_STAT
2626 aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
2627 x->ms_stat_nonrd.ms_time[bsize][this_mode] +=
2628 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302629#endif
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302630 // Skip NEWMV mode,
2631 // (i). For bsize smaller than 16X16
2632 // (ii). Based on sad of the predicted mv w.r.t LAST_FRAME
2633 // (iii). When motion vector is same as that of reference mv
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302634 if (skip_newmv) {
2635 return true;
2636 }
2637 }
2638
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302639 // Check the current motion vector is same as that of previously evaluated
2640 // motion vectors.
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302641 for (PREDICTION_MODE inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV;
2642 inter_mv_mode++) {
2643 if (inter_mv_mode == this_mode) continue;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302644 if (is_single_pred &&
2645 search_state->mode_checked[inter_mv_mode][ref_frame] &&
Anupam Pandeyab370452022-12-14 11:23:34 +05302646 this_mv->as_int ==
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302647 search_state->frame_mv[inter_mv_mode][ref_frame].as_int) {
2648 skip_this_mv = 1;
2649 break;
2650 }
2651 }
2652
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302653 // Skip single mode if current motion vector is same that of previously
2654 // evaluated motion vectors.
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302655 if (skip_this_mv && is_single_pred) return true;
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302656
2657 // For screen: for spatially flat blocks with non-zero motion,
Marco Paniconia33c8412023-09-29 14:25:46 -07002658 // skip newmv if the motion vector is (0, 0)-LAST, and color is not set.
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302659 if (this_mode == NEWMV && cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
Anupam Pandeyab370452022-12-14 11:23:34 +05302660 cpi->svc.spatial_layer_id == 0 && rt_sf->source_metrics_sb_nonrd) {
Marco Paniconia33c8412023-09-29 14:25:46 -07002661 if (this_mv->as_int == 0 && ref_frame == LAST_FRAME &&
2662 x->block_is_zero_sad == 0 &&
Marco Paniconi9a3b7682023-07-10 10:33:13 -07002663 ((x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
2664 x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) ||
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302665 cpi->rc.high_source_sad) &&
2666 x->source_variance == 0)
2667 return true;
2668 }
2669
2670 mi->mode = this_mode;
Anupam Pandeyab370452022-12-14 11:23:34 +05302671 mi->mv[0].as_int = this_mv->as_int;
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302672 mi->mv[1].as_int = 0;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302673 if (!is_single_pred)
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302674 mi->mv[1].as_int = search_state->frame_mv[this_mode][ref_frame2].as_int;
2675
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302676 // Set buffers to store predicted samples for reuse
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302677 if (reuse_inter_pred) {
2678 if (!*this_mode_pred) {
2679 *this_mode_pred = &tmp_buffer[3];
2680 } else {
2681 *this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
2682 pd->dst.buf = (*this_mode_pred)->data;
2683 pd->dst.stride = bw;
2684 }
2685 }
2686
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302687 mi->motion_mode = SIMPLE_TRANSLATION;
2688#if !CONFIG_REALTIME_ONLY
2689 if (cpi->oxcf.motion_mode_cfg.allow_warped_motion) {
2690 calc_num_proj_ref(cpi, x, mi);
2691 }
2692#endif
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302693 // set variance threshold for compound mode pruning
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302694 if (rt_sf->prune_compoundmode_with_singlecompound_var && !is_single_pred &&
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302695 use_model_yrd_large) {
2696 const PREDICTION_MODE single_mode0 = compound_ref0_mode(this_mode);
2697 const PREDICTION_MODE single_mode1 = compound_ref1_mode(this_mode);
2698 var_threshold =
2699 AOMMIN(var_threshold,
2700 search_state->vars[INTER_OFFSET(single_mode0)][ref_frame]);
2701 var_threshold =
2702 AOMMIN(var_threshold,
2703 search_state->vars[INTER_OFFSET(single_mode1)][ref_frame2]);
2704 }
2705
2706 // decide interpolation filter, build prediction signal, get sse
2707 const bool is_mv_subpel =
2708 (mi->mv[0].as_mv.row & 0x07) || (mi->mv[0].as_mv.col & 0x07);
2709 const bool enable_filt_search_this_mode =
2710 (filter_search_enabled_blk == 2)
2711 ? true
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302712 : (filter_search_enabled_blk && !force_mv_inter_layer &&
2713 is_single_pred &&
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302714 (ref_frame == LAST_FRAME || !x->nonrd_prune_ref_frame_search));
2715 if (is_mv_subpel && enable_filt_search_this_mode) {
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302716#if COLLECT_NONRD_PICK_MODE_STAT
2717 aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302718#endif
Anupam Pandey2b4957c2022-12-19 16:39:52 +05302719 search_filter_ref(
2720 cpi, x, &search_state->this_rdc, &inter_pred_params_sr, mi_row, mi_col,
2721 tmp_buffer, bsize, reuse_inter_pred, this_mode_pred, &this_early_term,
2722 &var, use_model_yrd_large, best_pickmode->best_sse, is_single_pred);
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302723#if COLLECT_NONRD_PICK_MODE_STAT
2724 aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
2725 x->ms_stat_nonrd.ifs_time[bsize][this_mode] +=
2726 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302727#endif
2728#if !CONFIG_REALTIME_ONLY
2729 } else if (cpi->oxcf.motion_mode_cfg.allow_warped_motion &&
2730 this_mode == NEWMV) {
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302731 // Find the best motion mode when current mode is NEWMV
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302732 search_motion_mode(cpi, x, &search_state->this_rdc, mi_row, mi_col, bsize,
2733 &this_early_term, use_model_yrd_large, &rate_mv,
Anupam Pandey2b4957c2022-12-19 16:39:52 +05302734 best_pickmode->best_sse);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302735 if (this_mode == NEWMV) {
Anupam Pandeyab370452022-12-14 11:23:34 +05302736 this_mv[0] = mi->mv[0];
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302737 }
2738#endif
2739 } else {
2740 mi->interp_filters =
2741 (filter_ref == SWITCHABLE)
2742 ? av1_broadcast_interp_filter(default_interp_filter)
2743 : av1_broadcast_interp_filter(filter_ref);
2744 if (force_mv_inter_layer)
2745 mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
2746
2747 // If it is sub-pel motion and cb_pred_filter_search is enabled, select
2748 // the pre-decided filter
2749 if (is_mv_subpel && cb_pred_filter_search)
2750 mi->interp_filters = av1_broadcast_interp_filter(filt_select);
2751
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302752#if COLLECT_NONRD_PICK_MODE_STAT
2753 aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302754#endif
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302755 if (is_single_pred) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302756 SubpelParams subpel_params;
2757 // Initialize inter mode level params for single reference mode.
2758 init_inter_mode_params(&mi->mv[0].as_mv, &inter_pred_params_sr,
2759 &subpel_params, xd->block_ref_scale_factors[0],
2760 pd->pre->width, pd->pre->height);
2761 av1_enc_build_inter_predictor_y_nonrd(xd, &inter_pred_params_sr,
2762 &subpel_params);
2763 } else {
Anupam Pandey45caf952022-12-15 11:16:15 +05302764 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2765 AOM_PLANE_Y, AOM_PLANE_Y);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302766 }
2767
2768 if (use_model_yrd_large) {
2769 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
2770 &search_state->this_rdc, &this_early_term, 0,
Anupam Pandey2b4957c2022-12-19 16:39:52 +05302771 best_pickmode->best_sse, &var, var_threshold);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302772 } else {
2773 model_rd_for_sb_y(cpi, bsize, x, xd, &search_state->this_rdc, &var, 0,
2774 &this_early_term);
2775 }
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302776#if COLLECT_NONRD_PICK_MODE_STAT
2777 aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
2778 x->ms_stat_nonrd.model_rd_time[bsize][this_mode] +=
2779 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302780#endif
2781 }
2782
2783 // update variance for single mode
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302784 if (is_single_pred) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302785 search_state->vars[INTER_OFFSET(this_mode)][ref_frame] = var;
Anupam Pandeyab370452022-12-14 11:23:34 +05302786 if (this_mv->as_int == 0) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302787 search_state->vars[INTER_OFFSET(GLOBALMV)][ref_frame] = var;
2788 }
2789 }
2790 // prune compound mode based on single mode var threshold
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302791 if (!is_single_pred && var > var_threshold) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302792 if (reuse_inter_pred) free_pred_buffer(*this_mode_pred);
2793 return true;
2794 }
2795
Anupam Pandeyab370452022-12-14 11:23:34 +05302796 if (ref_frame == LAST_FRAME && this_mv->as_int == 0) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302797 *sse_zeromv_norm = (unsigned int)(search_state->this_rdc.sse >>
2798 (b_width_log2_lookup[bsize] +
2799 b_height_log2_lookup[bsize]));
2800 }
2801
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302802 // Perform early termination based on sse.
Anupam Pandeyab370452022-12-14 11:23:34 +05302803 if (rt_sf->sse_early_term_inter_search &&
Anupam Pandey2b4957c2022-12-19 16:39:52 +05302804 early_term_inter_search_with_sse(rt_sf->sse_early_term_inter_search,
2805 bsize, search_state->this_rdc.sse,
2806 best_pickmode->best_sse, this_mode)) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302807 if (reuse_inter_pred) free_pred_buffer(*this_mode_pred);
2808 return true;
2809 }
2810
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302811#if COLLECT_NONRD_PICK_MODE_STAT
2812 x->ms_stat_nonrd.num_nonskipped_searches[bsize][this_mode]++;
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302813#endif
2814
2815 const int skip_ctx = av1_get_skip_txfm_context(xd);
2816 const int skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][1];
2817 const int no_skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][0];
2818 const int64_t sse_y = search_state->this_rdc.sse;
2819
2820 if (this_early_term) {
2821 search_state->this_rdc.skip_txfm = 1;
2822 search_state->this_rdc.rate = skip_txfm_cost;
2823 search_state->this_rdc.dist = search_state->this_rdc.sse << 4;
2824 } else {
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302825#if COLLECT_NONRD_PICK_MODE_STAT
2826 aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302827#endif
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302828 // Calculates RD Cost using Hadamard transform.
Marco Paniconide6f57b2023-03-19 23:42:40 -07002829 av1_block_yrd(x, &search_state->this_rdc, &is_skippable, bsize,
2830 mi->tx_size);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302831 if (search_state->this_rdc.skip_txfm ||
2832 RDCOST(x->rdmult, search_state->this_rdc.rate,
2833 search_state->this_rdc.dist) >=
2834 RDCOST(x->rdmult, 0, search_state->this_rdc.sse)) {
2835 if (!search_state->this_rdc.skip_txfm) {
2836 // Need to store "real" rdc for possible future use if UV rdc
2837 // disallows tx skip
2838 nonskip_rdc = search_state->this_rdc;
2839 nonskip_rdc.rate += no_skip_txfm_cost;
2840 }
2841 search_state->this_rdc.rate = skip_txfm_cost;
2842 search_state->this_rdc.skip_txfm = 1;
2843 search_state->this_rdc.dist = search_state->this_rdc.sse;
2844 } else {
2845 search_state->this_rdc.rate += no_skip_txfm_cost;
2846 }
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302847
2848 // Populate predicted sample for chroma planes based on color sensitivity.
Anupam Pandey45caf952022-12-15 11:16:15 +05302849 if ((x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
2850 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302851 RD_STATS rdc_uv;
Anupam Pandey45caf952022-12-15 11:16:15 +05302852 const BLOCK_SIZE uv_bsize =
2853 get_plane_block_size(bsize, xd->plane[AOM_PLANE_U].subsampling_x,
2854 xd->plane[AOM_PLANE_U].subsampling_y);
2855 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)]) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302856 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2857 AOM_PLANE_U, AOM_PLANE_U);
2858 }
Anupam Pandey45caf952022-12-15 11:16:15 +05302859 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302860 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2861 AOM_PLANE_V, AOM_PLANE_V);
2862 }
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302863 // Compute sse for chroma planes.
Anupam Pandeyd68ec612023-01-03 14:15:58 +05302864 const int64_t sse_uv = av1_model_rd_for_sb_uv(
2865 cpi, uv_bsize, x, xd, &rdc_uv, AOM_PLANE_U, AOM_PLANE_V);
Marco Paniconif289cc92023-06-27 21:31:04 -07002866 if (rdc_uv.dist < x->min_dist_inter_uv)
2867 x->min_dist_inter_uv = rdc_uv.dist;
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302868 search_state->this_rdc.sse += sse_uv;
2869 // Restore Y rdc if UV rdc disallows txfm skip
2870 if (search_state->this_rdc.skip_txfm && !rdc_uv.skip_txfm &&
2871 nonskip_rdc.rate != INT_MAX)
2872 search_state->this_rdc = nonskip_rdc;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302873 if (is_single_pred) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302874 search_state->uv_dist[INTER_OFFSET(this_mode)][ref_frame] = rdc_uv.dist;
2875 }
2876 search_state->this_rdc.rate += rdc_uv.rate;
2877 search_state->this_rdc.dist += rdc_uv.dist;
2878 search_state->this_rdc.skip_txfm =
2879 search_state->this_rdc.skip_txfm && rdc_uv.skip_txfm;
2880 }
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302881#if COLLECT_NONRD_PICK_MODE_STAT
2882 aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
2883 x->ms_stat_nonrd.txfm_time[bsize][this_mode] +=
2884 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302885#endif
2886 }
2887
2888 this_best_mode = this_mode;
2889 // TODO(kyslov) account for UV prediction cost
2890 search_state->this_rdc.rate += rate_mv;
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302891 if (!is_single_pred) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302892 const int16_t mode_ctx =
2893 av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame);
2894 search_state->this_rdc.rate += cost_mv_ref(mode_costs, this_mode, mode_ctx);
2895 } else {
2896 // If the current mode has zeromv but is not GLOBALMV, compare the rate
2897 // cost. If GLOBALMV is cheaper, use GLOBALMV instead.
2898 if (this_mode != GLOBALMV &&
Anupam Pandeyab370452022-12-14 11:23:34 +05302899 this_mv->as_int == search_state->frame_mv[GLOBALMV][ref_frame].as_int) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302900 if (is_globalmv_better(this_mode, ref_frame, rate_mv, mode_costs,
2901 search_state->single_inter_mode_costs, mbmi_ext)) {
2902 this_best_mode = GLOBALMV;
2903 }
2904 }
2905
2906 search_state->this_rdc.rate +=
2907 search_state
2908 ->single_inter_mode_costs[INTER_OFFSET(this_best_mode)][ref_frame];
2909 }
2910
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302911 if (is_single_pred && this_mv->as_int == 0 && var < UINT_MAX) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302912 search_state->vars[INTER_OFFSET(GLOBALMV)][ref_frame] = var;
2913 }
2914
2915 search_state->this_rdc.rate += search_state->ref_costs_single[ref_frame];
2916
2917 search_state->this_rdc.rdcost = RDCOST(x->rdmult, search_state->this_rdc.rate,
2918 search_state->this_rdc.dist);
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05302919 if (cpi->oxcf.rc_cfg.mode == AOM_CBR && is_single_pred) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302920 newmv_diff_bias(xd, this_best_mode, &search_state->this_rdc, bsize,
2921 search_state->frame_mv[this_best_mode][ref_frame].as_mv.row,
2922 search_state->frame_mv[this_best_mode][ref_frame].as_mv.col,
2923 cpi->speed, x->source_variance, x->content_state_sb);
2924 }
2925
2926#if CONFIG_AV1_TEMPORAL_DENOISING
2927 if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc_pickmode &&
2928 cpi->denoiser.denoising_level > kDenLowLow) {
2929 av1_denoiser_update_frame_stats(mi, sse_y, this_mode, ctx);
2930 // Keep track of zero_last cost.
Anupam Pandeyab370452022-12-14 11:23:34 +05302931 if (ref_frame == LAST_FRAME && this_mv->as_int == 0)
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302932 *zero_last_cost_orig = search_state->this_rdc.rdcost;
2933 }
2934#else
2935 (void)(sse_y);
2936#endif
2937
2938 search_state->mode_checked[this_mode][ref_frame] = 1;
2939 search_state->mode_checked[this_best_mode][ref_frame] = 1;
2940
2941 if (*check_globalmv) {
2942 int32_t abs_mv =
2943 abs(search_state->frame_mv[this_best_mode][ref_frame].as_mv.row) +
2944 abs(search_state->frame_mv[this_best_mode][ref_frame].as_mv.col);
2945 // Early exit check: if the magnitude of this_best_mode's mv is small
2946 // enough, we skip GLOBALMV check in the next loop iteration.
2947 if (abs_mv < 2) {
2948 *check_globalmv = false;
2949 }
2950 }
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05302951#if COLLECT_NONRD_PICK_MODE_STAT
2952 aom_usec_timer_mark(&x->ms_stat_nonrd.timer1);
2953 x->ms_stat_nonrd.nonskipped_search_times[bsize][this_mode] +=
2954 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer1);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302955#endif
2956
Marco Paniconi5a4ea092023-08-09 14:36:15 -07002957 if (x->sb_me_block && ref_frame == LAST_FRAME &&
2958 search_state->frame_mv[this_best_mode][ref_frame].as_int ==
2959 x->sb_me_mv.as_int)
2960 *sb_me_has_been_tested = 1;
2961
Anupam Pandeyb7d36762022-12-19 12:03:01 +05302962 // Copy best mode params to search state
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302963 if (search_state->this_rdc.rdcost < search_state->best_rdc.rdcost) {
2964 search_state->best_rdc = search_state->this_rdc;
2965 *best_early_term = this_early_term;
Anupam Pandey73179372022-12-21 16:07:14 +05302966 update_search_state_nonrd(search_state, mi, txfm_info, &nonskip_rdc, ctx,
2967 this_best_mode, sse_y);
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302968
2969 // This is needed for the compound modes.
2970 search_state->frame_mv_best[this_best_mode][ref_frame].as_int =
2971 search_state->frame_mv[this_best_mode][ref_frame].as_int;
2972 if (ref_frame2 > NONE_FRAME) {
2973 search_state->frame_mv_best[this_best_mode][ref_frame2].as_int =
2974 search_state->frame_mv[this_best_mode][ref_frame2].as_int;
2975 }
2976
2977 if (reuse_inter_pred) {
Anupam Pandey2b4957c2022-12-19 16:39:52 +05302978 free_pred_buffer(best_pickmode->best_pred);
2979 best_pickmode->best_pred = *this_mode_pred;
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302980 }
2981 } else {
2982 if (reuse_inter_pred) free_pred_buffer(*this_mode_pred);
2983 }
2984
Anupam Pandeyab370452022-12-14 11:23:34 +05302985 if (*best_early_term && (idx > 0 || rt_sf->nonrd_aggressive_skip)) {
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302986 txfm_info->skip_txfm = 1;
Marco Paniconi5a4ea092023-08-09 14:36:15 -07002987 if (!x->sb_me_block || *sb_me_has_been_tested) return false;
Anupam Pandey3e797ee2022-12-12 14:53:03 +05302988 }
2989 return true;
2990}
2991
Anupam Pandeyab578282022-12-14 21:11:31 +05302992// Function to perform screen content mode evaluation for non-rd
2993static AOM_FORCE_INLINE void handle_screen_content_mode_nonrd(
2994 AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
2995 PRED_BUFFER *this_mode_pred, PICK_MODE_CONTEXT *ctx,
2996 PRED_BUFFER *tmp_buffer, struct buf_2d *orig_dst, int skip_idtx_palette,
Marco Paniconi92960a82023-05-23 15:32:16 -07002997 int try_palette, BLOCK_SIZE bsize, int reuse_inter_pred, int mi_col,
2998 int mi_row) {
Anupam Pandeyab578282022-12-14 21:11:31 +05302999 AV1_COMMON *const cm = &cpi->common;
3000 const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
3001 MACROBLOCKD *const xd = &x->e_mbd;
3002 MB_MODE_INFO *const mi = xd->mi[0];
3003 struct macroblockd_plane *const pd = &xd->plane[0];
Anupam Pandeyab578282022-12-14 21:11:31 +05303004 const int bw = block_size_wide[bsize];
Marco Paniconifffd4e82023-01-24 00:32:07 -08003005 const int bh = block_size_high[bsize];
Anupam Pandeyab578282022-12-14 21:11:31 +05303006 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303007 BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode;
Anupam Pandeyab578282022-12-14 21:11:31 +05303008
Anupam Pandeyab578282022-12-14 21:11:31 +05303009 // TODO(marpan): Only allow for 8 bit-depth for now, re-enable for 10/12 bit
3010 // when issue 3359 is fixed.
Marco Paniconi62732c82024-04-30 15:24:49 -07003011 if (cm->seq_params->bit_depth == 8 && rt_sf->use_idtx_nonrd &&
3012 !skip_idtx_palette && !cpi->oxcf.txfm_cfg.use_inter_dct_only &&
3013 !x->force_zeromv_skip_for_blk &&
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303014 is_inter_mode(best_pickmode->best_mode) &&
Marco Paniconi92dcb192023-03-21 09:46:02 -07003015 best_pickmode->best_pred != NULL &&
Anupam Pandeyab578282022-12-14 21:11:31 +05303016 (!rt_sf->prune_idtx_nonrd ||
3017 (rt_sf->prune_idtx_nonrd && bsize <= BLOCK_32X32 &&
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303018 best_pickmode->best_mode_skip_txfm != 1 && x->source_variance > 200))) {
Anupam Pandeyab578282022-12-14 21:11:31 +05303019 RD_STATS idtx_rdc;
3020 av1_init_rd_stats(&idtx_rdc);
3021 int is_skippable;
3022 this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
3023 pd->dst.buf = this_mode_pred->data;
3024 pd->dst.stride = bw;
Marco Paniconi92dcb192023-03-21 09:46:02 -07003025 const PRED_BUFFER *const best_pred = best_pickmode->best_pred;
3026 av1_block_yrd_idtx(x, best_pred->data, best_pred->stride, &idtx_rdc,
3027 &is_skippable, bsize, mi->tx_size);
Marco Paniconiead4fc32023-06-05 11:36:52 -07003028 int64_t idx_rdcost_y = RDCOST(x->rdmult, idtx_rdc.rate, idtx_rdc.dist);
3029 int allow_idtx = 1;
Marco Paniconi92960a82023-05-23 15:32:16 -07003030 // Incorporate color into rd cost.
3031 if ((x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3032 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])) {
3033 RD_STATS rdc_uv;
3034 const BLOCK_SIZE uv_bsize =
3035 get_plane_block_size(bsize, xd->plane[AOM_PLANE_U].subsampling_x,
3036 xd->plane[AOM_PLANE_U].subsampling_y);
3037 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)]) {
3038 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3039 AOM_PLANE_U, AOM_PLANE_U);
3040 }
3041 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) {
3042 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3043 AOM_PLANE_V, AOM_PLANE_V);
3044 }
3045 av1_model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, AOM_PLANE_U,
3046 AOM_PLANE_V);
Marco Paniconif289cc92023-06-27 21:31:04 -07003047 if (rdc_uv.dist < x->min_dist_inter_uv)
3048 x->min_dist_inter_uv = rdc_uv.dist;
Marco Paniconi92960a82023-05-23 15:32:16 -07003049 idtx_rdc.rate += rdc_uv.rate;
3050 idtx_rdc.dist += rdc_uv.dist;
3051 idtx_rdc.skip_txfm = idtx_rdc.skip_txfm && rdc_uv.skip_txfm;
Marco Paniconiead4fc32023-06-05 11:36:52 -07003052 if (idx_rdcost_y == 0 && rdc_uv.dist > 0 && x->source_variance < 3000 &&
3053 x->content_state_sb.source_sad_nonrd > kMedSad)
3054 allow_idtx = 0;
Marco Paniconi92960a82023-05-23 15:32:16 -07003055 }
Anupam Pandeyab578282022-12-14 21:11:31 +05303056 int64_t idx_rdcost = RDCOST(x->rdmult, idtx_rdc.rate, idtx_rdc.dist);
Marco Paniconiead4fc32023-06-05 11:36:52 -07003057 if (allow_idtx && idx_rdcost < search_state->best_rdc.rdcost) {
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303058 best_pickmode->tx_type = IDTX;
Anupam Pandeyab578282022-12-14 21:11:31 +05303059 search_state->best_rdc.rdcost = idx_rdcost;
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303060 best_pickmode->best_mode_skip_txfm = idtx_rdc.skip_txfm;
Anupam Pandeyab578282022-12-14 21:11:31 +05303061 if (!idtx_rdc.skip_txfm) {
Marco Paniconid77e40b2023-05-15 14:02:06 -07003062 memcpy(ctx->blk_skip, txfm_info->blk_skip,
Marco Paniconide6f57b2023-03-19 23:42:40 -07003063 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
Anupam Pandeyab578282022-12-14 21:11:31 +05303064 }
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303065 xd->tx_type_map[0] = best_pickmode->tx_type;
3066 memset(ctx->tx_type_map, best_pickmode->tx_type, ctx->num_4x4_blk);
3067 memset(xd->tx_type_map, best_pickmode->tx_type, ctx->num_4x4_blk);
Anupam Pandeyab578282022-12-14 21:11:31 +05303068 }
3069 pd->dst = *orig_dst;
3070 }
3071
3072 if (!try_palette) return;
3073 const unsigned int intra_ref_frame_cost =
3074 search_state->ref_costs_single[INTRA_FRAME];
3075
Marco Paniconifffd4e82023-01-24 00:32:07 -08003076 if (!is_mode_intra(best_pickmode->best_mode)) {
3077 PRED_BUFFER *const best_pred = best_pickmode->best_pred;
3078 if (reuse_inter_pred && best_pred != NULL) {
3079 if (best_pred->data == orig_dst->buf) {
3080 this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
3081 aom_convolve_copy(best_pred->data, best_pred->stride,
3082 this_mode_pred->data, this_mode_pred->stride, bw, bh);
3083 best_pickmode->best_pred = this_mode_pred;
3084 }
3085 }
3086 pd->dst = *orig_dst;
3087 }
Anupam Pandeybce4e9a2022-12-22 14:45:05 +05303088 // Search palette mode for Luma plane in inter frame.
Anupam Pandeyab578282022-12-14 21:11:31 +05303089 av1_search_palette_mode_luma(cpi, x, bsize, intra_ref_frame_cost, ctx,
3090 &search_state->this_rdc,
3091 search_state->best_rdc.rdcost);
Anupam Pandeybce4e9a2022-12-22 14:45:05 +05303092 // Update best mode data in search_state
Anupam Pandeyab578282022-12-14 21:11:31 +05303093 if (search_state->this_rdc.rdcost < search_state->best_rdc.rdcost) {
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303094 best_pickmode->pmi = mi->palette_mode_info;
3095 best_pickmode->best_mode = DC_PRED;
Marco Paniconifffd4e82023-01-24 00:32:07 -08003096 mi->mv[0].as_int = INVALID_MV;
3097 mi->mv[1].as_int = INVALID_MV;
3098 best_pickmode->best_ref_frame = INTRA_FRAME;
3099 best_pickmode->best_second_ref_frame = NONE;
Anupam Pandeyab578282022-12-14 21:11:31 +05303100 search_state->best_rdc.rate = search_state->this_rdc.rate;
3101 search_state->best_rdc.dist = search_state->this_rdc.dist;
3102 search_state->best_rdc.rdcost = search_state->this_rdc.rdcost;
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303103 best_pickmode->best_mode_skip_txfm = search_state->this_rdc.skip_txfm;
Anupam Pandeyab578282022-12-14 21:11:31 +05303104 // Keep the skip_txfm off if the color_sensitivity is set.
Anupam Pandey45caf952022-12-15 11:16:15 +05303105 if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3106 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])
Anupam Pandeyab578282022-12-14 21:11:31 +05303107 search_state->this_rdc.skip_txfm = 0;
3108 if (!search_state->this_rdc.skip_txfm) {
3109 memcpy(ctx->blk_skip, txfm_info->blk_skip,
3110 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
3111 }
3112 if (xd->tx_type_map[0] != DCT_DCT)
3113 av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
3114 }
3115}
3116
Marco Paniconibdada5c2024-05-22 14:30:55 -07003117static AOM_INLINE bool enable_palette(AV1_COMP *cpi, bool is_mode_intra,
3118 BLOCK_SIZE bsize,
3119 unsigned int source_variance,
3120 int force_zeromv_skip,
3121 int skip_idtx_palette,
3122 int force_palette_test) {
3123 if (!cpi->oxcf.tool_cfg.enable_palette) return false;
3124 if (!av1_allow_palette(cpi->common.features.allow_screen_content_tools,
3125 bsize)) {
3126 return false;
3127 }
3128 if (skip_idtx_palette) return false;
3129
3130 if (cpi->sf.rt_sf.prune_palette_search_nonrd > 1 &&
3131 ((cpi->rc.high_source_sad && cpi->ppi->rtc_ref.non_reference_frame) ||
3132 bsize > BLOCK_16X16)) {
3133 return false;
3134 }
3135
3136 if ((is_mode_intra || force_palette_test) && source_variance > 0 &&
3137 !force_zeromv_skip &&
3138 (cpi->rc.high_source_sad || source_variance > 300)) {
3139 return true;
3140 } else {
3141 return false;
3142 }
3143}
3144
Anupam Pandeyb7d36762022-12-19 12:03:01 +05303145/*!\brief AV1 inter mode selection based on Non-RD optimized model.
3146 *
3147 * \ingroup nonrd_mode_search
3148 * \callgraph
3149 * Top level function for Non-RD optimized inter mode selection.
3150 * This finction will loop over subset of inter modes and select the best one
3151 * based on calculated modelled RD cost. While making decisions which modes to
3152 * check, this function applies heuristics based on previously checked modes,
3153 * block residual variance, block size, and other factors to prune certain
3154 * modes and reference frames. Currently only single reference frame modes
3155 * are checked. Additional heuristics are applied to decide if intra modes
3156 * need to be checked.
3157 * *
3158 * \param[in] cpi Top-level encoder structure
3159 * \param[in] tile_data Pointer to struct holding adaptive
3160 data/contexts/models for the tile during
3161 encoding
3162 * \param[in] x Pointer to structure holding all the data for
3163 the current macroblock
3164 * \param[in] rd_cost Struct to keep track of the RD information
3165 * \param[in] bsize Current block size
3166 * \param[in] ctx Structure to hold snapshot of coding context
3167 during the mode picking process
3168 *
3169 * \remark Nothing is returned. Instead, the MB_MODE_INFO struct inside x
3170 * is modified to store information about the best mode computed
3171 * in this function. The rd_cost struct is also updated with the RD stats
3172 * corresponding to the best mode found.
3173 */
Fyodor Kyslov915b9b82019-11-11 11:53:03 -08003174void av1_nonrd_pick_inter_mode_sb(AV1_COMP *cpi, TileDataEnc *tile_data,
Hui Sub94cd5e2019-11-06 12:05:47 -08003175 MACROBLOCK *x, RD_STATS *rd_cost,
Marco Paniconi11067db2020-05-18 11:11:42 -07003176 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
kyslove3c05a82019-03-28 11:06:09 -07003177 AV1_COMMON *const cm = &cpi->common;
Marco Paniconi84cb8b32021-01-19 14:02:19 -08003178 SVC *const svc = &cpi->svc;
kyslove3c05a82019-03-28 11:06:09 -07003179 MACROBLOCKD *const xd = &x->e_mbd;
3180 MB_MODE_INFO *const mi = xd->mi[0];
Anupam Pandey45caf952022-12-15 11:16:15 +05303181 struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
chiyotsai901f4062022-07-06 14:57:53 -07003182 const MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08003183 MV_REFERENCE_FRAME ref_frame, ref_frame2;
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07003184 const unsigned char segment_id = mi->segment_id;
kyslove3c05a82019-03-28 11:06:09 -07003185 int best_early_term = 0;
kyslove3c05a82019-03-28 11:06:09 -07003186 int force_skip_low_temp_var = 0;
Fyodor Kyslov84abaea2019-06-25 14:44:14 -07003187 unsigned int sse_zeromv_norm = UINT_MAX;
linzhene4954c22022-09-20 18:27:52 +00003188 const int num_inter_modes = NUM_INTER_MODES;
Anupam Pandeyab370452022-12-14 11:23:34 +05303189 const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
3190 bool check_globalmv = rt_sf->check_globalmv_on_single_ref;
Cheng Chen94634492022-11-16 17:37:52 -08003191 PRED_BUFFER tmp_buffer[4];
Anupam Pandey45caf952022-12-15 11:16:15 +05303192 DECLARE_ALIGNED(16, uint8_t, pred_buf[MAX_MB_PLANE * MAX_SB_SQUARE]);
kyslove3c05a82019-03-28 11:06:09 -07003193 PRED_BUFFER *this_mode_pred = NULL;
Anupam Pandeyab370452022-12-14 11:23:34 +05303194 const int reuse_inter_pred =
3195 rt_sf->reuse_inter_pred_nonrd && cm->seq_params->bit_depth == AOM_BITS_8;
Anupam Pandey7dfb6d52022-12-05 14:19:10 +05303196 InterModeSearchStateNonrd search_state;
3197 av1_zero(search_state.use_ref_frame_mask);
Marco Paniconia1f29682023-09-17 20:09:51 -07003198 av1_zero(search_state.use_scaled_ref_frame);
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303199 BEST_PICKMODE *const best_pickmode = &search_state.best_pickmode;
Anupam Pandey05c81f82022-12-21 12:58:34 +05303200 (void)tile_data;
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08003201
kyslove3c05a82019-03-28 11:06:09 -07003202 const int bh = block_size_high[bsize];
3203 const int bw = block_size_wide[bsize];
3204 const int pixels_in_block = bh * bw;
3205 struct buf_2d orig_dst = pd->dst;
chiyotsaia36d9002020-04-29 16:48:21 -07003206 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
chiyotsai4c1e5c62020-04-30 17:54:14 -07003207 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05303208#if COLLECT_NONRD_PICK_MODE_STAT
3209 // Mode statistics can be collected only when num_workers is 1
3210 assert(cpi->mt_info.num_workers <= 1);
3211 aom_usec_timer_start(&x->ms_stat_nonrd.bsize_timer);
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07003212#endif
Marco Paniconib3565a22020-03-05 15:29:08 -08003213 int64_t thresh_sad_pred = INT64_MAX;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07003214 const int mi_row = xd->mi_row;
3215 const int mi_col = xd->mi_col;
Anupam Pandeycf5a52e2022-12-14 18:25:44 +05303216 int_mv svc_mv = { .as_int = 0 };
Marco Paniconi84cb8b32021-01-19 14:02:19 -08003217 int force_mv_inter_layer = 0;
chiyotsaid4e5cef2022-07-19 14:48:24 -07003218 bool comp_use_zero_zeromv_only = 0;
3219 int tot_num_comp_modes = NUM_COMP_INTER_MODES_RT;
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08003220#if CONFIG_AV1_TEMPORAL_DENOISING
3221 const int denoise_recheck_zeromv = 1;
3222 AV1_PICKMODE_CTX_DEN ctx_den;
3223 int64_t zero_last_cost_orig = INT64_MAX;
3224 int denoise_svc_pickmode = 1;
Yunqing Wanga6276ca2021-09-17 12:01:24 -07003225 const int resize_pending = is_frame_resize_pending(cpi);
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08003226#endif
chiyotsai9a06d182020-05-01 17:12:12 -07003227 const ModeCosts *mode_costs = &x->mode_costs;
Marco Paniconia1f29682023-09-17 20:09:51 -07003228 struct scale_factors sf_no_scale;
3229 av1_setup_scale_factors_for_frame(&sf_no_scale, cm->width, cm->height,
3230 cm->width, cm->height);
kyslove3c05a82019-03-28 11:06:09 -07003231 if (reuse_inter_pred) {
Anupam Pandeycbe4eb42022-12-21 12:44:47 +05303232 for (int buf_idx = 0; buf_idx < 3; buf_idx++) {
3233 tmp_buffer[buf_idx].data = &pred_buf[pixels_in_block * buf_idx];
3234 tmp_buffer[buf_idx].stride = bw;
3235 tmp_buffer[buf_idx].in_use = 0;
kyslove3c05a82019-03-28 11:06:09 -07003236 }
Cheng Chen94634492022-11-16 17:37:52 -08003237 tmp_buffer[3].data = pd->dst.buf;
3238 tmp_buffer[3].stride = pd->dst.stride;
3239 tmp_buffer[3].in_use = 0;
kyslove3c05a82019-03-28 11:06:09 -07003240 }
3241
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07003242 const int gf_temporal_ref = is_same_gf_and_last_scale(cm);
3243
Marco Paniconi84cb8b32021-01-19 14:02:19 -08003244 // If the lower spatial layer uses an averaging filter for downsampling
3245 // (phase = 8), the target decimated pixel is shifted by (1/2, 1/2) relative
3246 // to source, so use subpel motion vector to compensate. The nonzero motion
3247 // is half pixel shifted to left and top, so (-4, -4). This has more effect
James Zern286e07b2022-09-29 15:44:58 -07003248 // on higher resolutions, so condition it on that for now.
Marco Paniconi0d594182023-09-27 12:17:52 -07003249 // Exclude quality layers, which have the same resolution and hence no shift.
Tarundeep Singh15eb4de2021-04-21 15:53:10 +05303250 if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
Marco Paniconi0d594182023-09-27 12:17:52 -07003251 !svc->has_lower_quality_layer &&
Marco Paniconi84cb8b32021-01-19 14:02:19 -08003252 svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 &&
3253 cm->width * cm->height > 640 * 480) {
Anupam Pandeycf5a52e2022-12-14 18:25:44 +05303254 svc_mv.as_mv.row = -4;
3255 svc_mv.as_mv.col = -4;
Marco Paniconi84cb8b32021-01-19 14:02:19 -08003256 }
3257
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05303258 // Setup parameters used for inter mode evaluation.
Marco Paniconidcba6b22023-08-03 16:21:09 -07003259 set_params_nonrd_pick_inter_mode(cpi, x, &search_state, rd_cost,
3260 &force_skip_low_temp_var, mi_row, mi_col,
3261 gf_temporal_ref, segment_id, bsize
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05303262#if CONFIG_AV1_TEMPORAL_DENOISING
Marco Paniconidcba6b22023-08-03 16:21:09 -07003263 ,
3264 ctx, denoise_svc_pickmode
Anupam Pandey1e62cfb2022-12-06 11:22:47 +05303265#endif
3266 );
Marco Paniconid456b6f2021-10-26 09:32:03 -07003267
Anupam Pandeyab370452022-12-14 11:23:34 +05303268 if (rt_sf->use_comp_ref_nonrd && is_comp_ref_allowed(bsize)) {
Marco Paniconi857d12f2022-07-26 09:41:37 -07003269 // Only search compound if bsize \gt BLOCK_16X16.
3270 if (bsize > BLOCK_16X16) {
Anupam Pandeyab370452022-12-14 11:23:34 +05303271 comp_use_zero_zeromv_only = rt_sf->check_only_zero_zeromv_on_large_blocks;
Marco Paniconi857d12f2022-07-26 09:41:37 -07003272 } else {
3273 tot_num_comp_modes = 0;
chiyotsaid4e5cef2022-07-19 14:48:24 -07003274 }
Marco Paniconif190c952021-08-02 23:07:46 -07003275 } else {
3276 tot_num_comp_modes = 0;
Marco Paniconi49f39342021-07-22 12:06:45 -07003277 }
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08003278
Marco Paniconie26457d2022-10-11 15:42:17 -07003279 if (x->pred_mv_sad[LAST_FRAME] != INT_MAX) {
3280 thresh_sad_pred = ((int64_t)x->pred_mv_sad[LAST_FRAME]) << 1;
3281 // Increase threshold for less aggressive pruning.
Anupam Pandeyab370452022-12-14 11:23:34 +05303282 if (rt_sf->nonrd_prune_ref_frame_search == 1)
Marco Paniconie26457d2022-10-11 15:42:17 -07003283 thresh_sad_pred += (x->pred_mv_sad[LAST_FRAME] >> 2);
3284 }
Marco Paniconib3565a22020-03-05 15:29:08 -08003285
Ranjit Kumar Tulabandueaf39d32022-11-16 17:40:24 +05303286 const int use_model_yrd_large = get_model_rd_flag(cpi, xd, bsize);
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07003287
Neeraj Gadgil1513bf02022-10-28 23:43:22 +05303288 // decide block-level interp filter search flags:
3289 // filter_search_enabled_blk:
3290 // 0: disabled
3291 // 1: filter search depends on mode properties
3292 // 2: filter search forced since prediction is unreliable
3293 // cb_pred_filter_search 0: disabled cb prediction
3294 InterpFilter filt_select = EIGHTTAP_REGULAR;
3295 const int cb_pred_filter_search =
3296 x->content_state_sb.source_sad_nonrd > kVeryLowSad
3297 ? cpi->sf.interp_sf.cb_pred_filter_search
3298 : 0;
3299 const int filter_search_enabled_blk =
3300 is_filter_search_enabled_blk(cpi, x, mi_row, mi_col, bsize, segment_id,
3301 cb_pred_filter_search, &filt_select);
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07003302
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05303303#if COLLECT_NONRD_PICK_MODE_STAT
3304 x->ms_stat_nonrd.num_blocks[bsize]++;
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07003305#endif
Anupam Pandeyd68ec612023-01-03 14:15:58 +05303306 init_mbmi_nonrd(mi, DC_PRED, NONE_FRAME, NONE_FRAME, cm);
chiyotsaia36d9002020-04-29 16:48:21 -07003307 mi->tx_size = AOMMIN(
3308 AOMMIN(max_txsize_lookup[bsize],
3309 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]),
3310 TX_16X16);
Neeraj Gadgil50207d42022-03-31 16:26:12 +05303311
Anupam Pandey7dfb6d52022-12-05 14:19:10 +05303312 fill_single_inter_mode_costs(search_state.single_inter_mode_costs,
3313 num_inter_modes, ref_mode_set, mode_costs,
linzhene4954c22022-09-20 18:27:52 +00003314 mbmi_ext->mode_context);
chiyotsai901f4062022-07-06 14:57:53 -07003315
chiyotsaid4e5cef2022-07-19 14:48:24 -07003316 MV_REFERENCE_FRAME last_comp_ref_frame = NONE_FRAME;
3317
Cherma Rajan Ac8327552022-11-11 10:36:09 +05303318 // Initialize inter prediction params at block level for single reference
3319 // mode.
3320 InterPredParams inter_pred_params_sr;
3321 init_inter_block_params(&inter_pred_params_sr, pd->width, pd->height,
3322 mi_row * MI_SIZE, mi_col * MI_SIZE, pd->subsampling_x,
3323 pd->subsampling_y, xd->bd, is_cur_buf_hbd(xd),
3324 /*is_intrabc=*/0);
3325 inter_pred_params_sr.conv_params =
3326 get_conv_params(/*do_average=*/0, AOM_PLANE_Y, xd->bd);
3327
Marco Paniconi65d659962024-03-09 21:29:46 -08003328 x->block_is_zero_sad = x->content_state_sb.source_sad_nonrd == kZeroSad ||
3329 segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
Marco Paniconibdef9a92023-07-06 15:20:46 -07003330 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
3331 !x->force_zeromv_skip_for_blk &&
3332 x->content_state_sb.source_sad_nonrd != kZeroSad &&
Marco Paniconi89795ce2023-09-13 10:09:32 -07003333 x->source_variance == 0 && bsize < cm->seq_params->sb_size &&
3334 search_state.yv12_mb[LAST_FRAME][0].width == cm->width &&
3335 search_state.yv12_mb[LAST_FRAME][0].height == cm->height) {
Marco Paniconibdef9a92023-07-06 15:20:46 -07003336 set_block_source_sad(cpi, x, bsize, &search_state.yv12_mb[LAST_FRAME][0]);
3337 }
3338
Marco Paniconi5a4ea092023-08-09 14:36:15 -07003339 int sb_me_has_been_tested = 0;
3340 x->sb_me_block = x->sb_me_partition;
3341 // Only use this feature (force testing of superblock motion) if coding
3342 // block size is large.
3343 if (x->sb_me_block) {
3344 if (cm->seq_params->sb_size == BLOCK_128X128 && bsize < BLOCK_64X64)
3345 x->sb_me_block = 0;
3346 else if (cm->seq_params->sb_size == BLOCK_64X64 && bsize < BLOCK_32X32)
3347 x->sb_me_block = 0;
3348 }
3349
Marco Paniconif289cc92023-06-27 21:31:04 -07003350 x->min_dist_inter_uv = INT64_MAX;
Marco Paniconif190c952021-08-02 23:07:46 -07003351 for (int idx = 0; idx < num_inter_modes + tot_num_comp_modes; ++idx) {
chiyotsaia82b98f2022-08-17 15:59:47 -07003352 // If we are at the first compound mode, and the single modes already
3353 // perform well, then end the search.
Anupam Pandeyab370452022-12-14 11:23:34 +05303354 if (rt_sf->skip_compound_based_on_var && idx == num_inter_modes &&
Anupam Pandey7dfb6d52022-12-05 14:19:10 +05303355 skip_comp_based_on_var(search_state.vars, bsize)) {
chiyotsaia82b98f2022-08-17 15:59:47 -07003356 break;
3357 }
3358
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05303359 int is_single_pred = 1;
kyslove3c05a82019-03-28 11:06:09 -07003360 PREDICTION_MODE this_mode;
kyslove3c05a82019-03-28 11:06:09 -07003361
Marco Paniconi05f2a532023-08-10 12:16:22 -07003362 if (idx == 0 && !x->force_zeromv_skip_for_blk) {
Marco Paniconi9a3b7682023-07-10 10:33:13 -07003363 // Set color sensitivity on first tested mode only.
3364 // Use y-sad already computed in find_predictors: take the sad with motion
3365 // vector closest to 0; the uv-sad computed below in set_color_sensitivity
3366 // is for zeromv.
3367 // For screen: first check if golden reference is being used, if so,
Marco Paniconidcba6b22023-08-03 16:21:09 -07003368 // force color_sensitivity on (=1) if the color sensitivity for sb_g is 1.
3369 // The check in set_color_sensitivity() will then follow and check for
3370 // setting the flag if the level is still 2 or 0.
Marco Paniconi9a3b7682023-07-10 10:33:13 -07003371 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
3372 search_state.use_ref_frame_mask[GOLDEN_FRAME]) {
3373 if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1)
3374 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] = 1;
3375 if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
3376 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] = 1;
Marco Paniconidcba6b22023-08-03 16:21:09 -07003377 }
3378 if (search_state.use_ref_frame_mask[LAST_FRAME] &&
3379 x->pred_mv0_sad[LAST_FRAME] != INT_MAX) {
Marco Paniconi9a3b7682023-07-10 10:33:13 -07003380 int y_sad = x->pred_mv0_sad[LAST_FRAME];
3381 if (x->pred_mv1_sad[LAST_FRAME] != INT_MAX &&
3382 (abs(search_state.frame_mv[NEARMV][LAST_FRAME].as_mv.col) +
3383 abs(search_state.frame_mv[NEARMV][LAST_FRAME].as_mv.row)) <
3384 (abs(search_state.frame_mv[NEARESTMV][LAST_FRAME].as_mv.col) +
3385 abs(search_state.frame_mv[NEARESTMV][LAST_FRAME].as_mv.row)))
3386 y_sad = x->pred_mv1_sad[LAST_FRAME];
3387 set_color_sensitivity(cpi, x, bsize, y_sad, x->source_variance,
3388 search_state.yv12_mb[LAST_FRAME]);
3389 }
3390 }
3391
Anupam Pandeya61e10a2022-12-06 16:49:32 +05303392 // Check the inter mode can be skipped based on mode statistics and speed
3393 // features settings.
Anupam Pandeycf5a52e2022-12-14 18:25:44 +05303394 if (skip_inter_mode_nonrd(cpi, x, &search_state, &thresh_sad_pred,
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05303395 &force_mv_inter_layer, &is_single_pred,
3396 &this_mode, &last_comp_ref_frame, &ref_frame,
3397 &ref_frame2, idx, svc_mv, force_skip_low_temp_var,
Anupam Pandeycf5a52e2022-12-14 18:25:44 +05303398 sse_zeromv_norm, num_inter_modes, segment_id,
3399 bsize, comp_use_zero_zeromv_only, check_globalmv))
chiyotsai901f4062022-07-06 14:57:53 -07003400 continue;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07003401
kyslove3c05a82019-03-28 11:06:09 -07003402 // Select prediction reference frames.
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05303403 for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
3404 xd->plane[plane].pre[0] = search_state.yv12_mb[ref_frame][plane];
3405 if (!is_single_pred)
3406 xd->plane[plane].pre[1] = search_state.yv12_mb[ref_frame2][plane];
kyslove3c05a82019-03-28 11:06:09 -07003407 }
3408
3409 mi->ref_frame[0] = ref_frame;
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08003410 mi->ref_frame[1] = ref_frame2;
3411 set_ref_ptrs(cm, xd, ref_frame, ref_frame2);
kyslove3c05a82019-03-28 11:06:09 -07003412
Marco Paniconia1f29682023-09-17 20:09:51 -07003413 // Check if the scaled reference frame should be used. This is set in the
3414 // find_predictors() for each usable reference. If so, set the
3415 // block_ref_scale_factors[] to no reference scaling.
3416 if (search_state.use_scaled_ref_frame[ref_frame]) {
3417 xd->block_ref_scale_factors[0] = &sf_no_scale;
3418 }
3419 if (!is_single_pred && search_state.use_scaled_ref_frame[ref_frame2]) {
3420 xd->block_ref_scale_factors[1] = &sf_no_scale;
3421 }
3422
Anupam Pandey3e797ee2022-12-12 14:53:03 +05303423 // Perform inter mode evaluation for non-rd
3424 if (!handle_inter_mode_nonrd(
3425 cpi, x, &search_state, ctx, &this_mode_pred, tmp_buffer,
3426 inter_pred_params_sr, &best_early_term, &sse_zeromv_norm,
3427 &check_globalmv,
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08003428#if CONFIG_AV1_TEMPORAL_DENOISING
Anupam Pandey3e797ee2022-12-12 14:53:03 +05303429 &zero_last_cost_orig, denoise_svc_pickmode,
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08003430#endif
Marco Paniconi9a3b7682023-07-10 10:33:13 -07003431 idx, force_mv_inter_layer, is_single_pred, gf_temporal_ref,
3432 use_model_yrd_large, filter_search_enabled_blk, bsize, this_mode,
Marco Paniconi5a4ea092023-08-09 14:36:15 -07003433 filt_select, cb_pred_filter_search, reuse_inter_pred,
3434 &sb_me_has_been_tested)) {
kyslove3c05a82019-03-28 11:06:09 -07003435 break;
3436 }
3437 }
3438
Anupam Pandeybce4e9a2022-12-22 14:45:05 +05303439 // Restore mode data of best inter mode
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303440 mi->mode = best_pickmode->best_mode;
3441 mi->motion_mode = best_pickmode->best_motion_mode;
3442 mi->wm_params = best_pickmode->wm_params;
3443 mi->num_proj_ref = best_pickmode->num_proj_ref;
3444 mi->interp_filters = best_pickmode->best_pred_filter;
3445 mi->tx_size = best_pickmode->best_tx_size;
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -07003446 memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size));
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303447 mi->ref_frame[0] = best_pickmode->best_ref_frame;
3448 mi->mv[0].as_int = search_state
3449 .frame_mv_best[best_pickmode->best_mode]
3450 [best_pickmode->best_ref_frame]
3451 .as_int;
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08003452 mi->mv[1].as_int = 0;
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303453 if (best_pickmode->best_second_ref_frame > INTRA_FRAME) {
3454 mi->ref_frame[1] = best_pickmode->best_second_ref_frame;
3455 mi->mv[1].as_int = search_state
3456 .frame_mv_best[best_pickmode->best_mode]
3457 [best_pickmode->best_second_ref_frame]
3458 .as_int;
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08003459 }
kyslov82449d12019-05-02 13:36:50 -07003460 // Perform intra prediction search, if the best SAD is above a certain
3461 // threshold.
3462 mi->angle_delta[PLANE_TYPE_Y] = 0;
3463 mi->angle_delta[PLANE_TYPE_UV] = 0;
3464 mi->filter_intra_mode_info.use_filter_intra = 0;
Marco Paniconi5b5b19f2019-11-19 11:31:48 -08003465
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05303466#if COLLECT_NONRD_PICK_MODE_STAT
3467 aom_usec_timer_start(&x->ms_stat_nonrd.timer1);
3468 x->ms_stat_nonrd.num_searches[bsize][DC_PRED]++;
3469 x->ms_stat_nonrd.num_nonskipped_searches[bsize][DC_PRED]++;
chiyotsai4f6d2e32022-05-17 14:26:29 -07003470#endif
3471
Marco Paniconifffd4e82023-01-24 00:32:07 -08003472 int force_palette_test = 0;
3473 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
Marco Paniconifffd4e82023-01-24 00:32:07 -08003474 x->content_state_sb.source_sad_nonrd != kZeroSad &&
Marco Paniconic7cbcee2023-03-13 11:43:20 -07003475 bsize <= BLOCK_16X16) {
Marco Paniconif289cc92023-06-27 21:31:04 -07003476 unsigned int thresh_sse = cpi->rc.high_source_sad ? 15000 : 200000;
3477 unsigned int thresh_source_var = cpi->rc.high_source_sad ? 50 : 200;
Marco Paniconifffd4e82023-01-24 00:32:07 -08003478 unsigned int best_sse_inter_motion =
3479 (unsigned int)(search_state.best_rdc.sse >>
3480 (b_width_log2_lookup[bsize] +
3481 b_height_log2_lookup[bsize]));
Marco Paniconic7cbcee2023-03-13 11:43:20 -07003482 if (best_sse_inter_motion > thresh_sse &&
3483 x->source_variance > thresh_source_var)
3484 force_palette_test = 1;
Marco Paniconifffd4e82023-01-24 00:32:07 -08003485 }
3486
Anupam Pandeybce4e9a2022-12-22 14:45:05 +05303487 // Evaluate Intra modes in inter frame
Ranjit Kumar Tulabanducd4a3072022-09-02 15:53:57 +05303488 if (!x->force_zeromv_skip_for_blk)
Anupam Pandeyd68ec612023-01-03 14:15:58 +05303489 av1_estimate_intra_mode(cpi, x, bsize, best_early_term,
3490 search_state.ref_costs_single[INTRA_FRAME],
3491 reuse_inter_pred, &orig_dst, tmp_buffer,
3492 &this_mode_pred, &search_state.best_rdc,
3493 best_pickmode, ctx);
kyslov82449d12019-05-02 13:36:50 -07003494
Anupam Pandey45caf952022-12-15 11:16:15 +05303495 int skip_idtx_palette = (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3496 x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) &&
3497 x->content_state_sb.source_sad_nonrd != kZeroSad &&
3498 !cpi->rc.high_source_sad;
Marco Paniconi452d6da2022-08-23 00:33:09 -07003499
Marco Paniconibdada5c2024-05-22 14:30:55 -07003500 bool try_palette = enable_palette(
3501 cpi, is_mode_intra(best_pickmode->best_mode), bsize, x->source_variance,
3502 x->force_zeromv_skip_for_blk, skip_idtx_palette, force_palette_test);
Marco Paniconi9f9918d2023-01-09 12:44:47 -08003503
Anupam Pandeyab578282022-12-14 21:11:31 +05303504 // Perform screen content mode evaluation for non-rd
Marco Paniconi92960a82023-05-23 15:32:16 -07003505 handle_screen_content_mode_nonrd(
3506 cpi, x, &search_state, this_mode_pred, ctx, tmp_buffer, &orig_dst,
3507 skip_idtx_palette, try_palette, bsize, reuse_inter_pred, mi_col, mi_row);
Fyodor Kyslovdc9bddb2022-01-12 16:30:33 -08003508
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05303509#if COLLECT_NONRD_PICK_MODE_STAT
3510 aom_usec_timer_mark(&x->ms_stat_nonrd.timer1);
3511 x->ms_stat_nonrd.nonskipped_search_times[bsize][DC_PRED] +=
3512 aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer1);
chiyotsai4f6d2e32022-05-17 14:26:29 -07003513#endif
3514
kyslove3c05a82019-03-28 11:06:09 -07003515 pd->dst = orig_dst;
Anupam Pandeybce4e9a2022-12-22 14:45:05 +05303516 // Best mode is finalized. Restore the mode data to mbmi
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303517 if (try_palette) mi->palette_mode_info = best_pickmode->pmi;
3518 mi->mode = best_pickmode->best_mode;
3519 mi->ref_frame[0] = best_pickmode->best_ref_frame;
3520 mi->ref_frame[1] = best_pickmode->best_second_ref_frame;
Marco Paniconi1a16d1e2023-03-22 14:11:13 -07003521 // For lossless: always force the skip flags off.
3522 if (is_lossless_requested(&cpi->oxcf.rc_cfg)) {
Marco Paniconi59806d42023-03-21 12:12:38 -07003523 txfm_info->skip_txfm = 0;
3524 memset(ctx->blk_skip, 0, sizeof(ctx->blk_skip[0]) * ctx->num_4x4_blk);
3525 } else {
3526 txfm_info->skip_txfm = best_pickmode->best_mode_skip_txfm;
Fyodor Kyslovca638fa2021-12-08 19:49:13 -08003527 }
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08003528 if (has_second_ref(mi)) {
3529 mi->comp_group_idx = 0;
3530 mi->compound_idx = 1;
3531 mi->interinter_comp.type = COMPOUND_AVERAGE;
3532 }
kyslov82449d12019-05-02 13:36:50 -07003533
3534 if (!is_inter_block(mi)) {
3535 mi->interp_filters = av1_broadcast_interp_filter(SWITCHABLE_FILTERS);
Marco Paniconia1f29682023-09-17 20:09:51 -07003536 } else {
3537 // If inter mode is selected and ref_frame was one that uses the
3538 // scaled reference frame, then we can't use reuse_inter_pred.
3539 if (search_state.use_scaled_ref_frame[best_pickmode->best_ref_frame] ||
3540 (has_second_ref(mi) &&
3541 search_state
3542 .use_scaled_ref_frame[best_pickmode->best_second_ref_frame]))
3543 x->reuse_inter_pred = 0;
kyslov82449d12019-05-02 13:36:50 -07003544 }
3545
Anupam Pandeybce4e9a2022-12-22 14:45:05 +05303546 // Restore the predicted samples of best mode to final buffer
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303547 if (reuse_inter_pred && best_pickmode->best_pred != NULL) {
3548 PRED_BUFFER *const best_pred = best_pickmode->best_pred;
kyslove3c05a82019-03-28 11:06:09 -07003549 if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) {
3550 aom_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
Elliott Karpilovskyebe812f2020-04-13 18:48:50 -07003551 pd->dst.stride, bw, bh);
kyslove3c05a82019-03-28 11:06:09 -07003552 }
3553 }
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08003554
3555#if CONFIG_AV1_TEMPORAL_DENOISING
3556 if (cpi->oxcf.noise_sensitivity > 0 && resize_pending == 0 &&
3557 denoise_svc_pickmode && cpi->denoiser.denoising_level > kDenLowLow &&
3558 cpi->denoiser.reset == 0) {
3559 AV1_DENOISER_DECISION decision = COPY_BLOCK;
3560 ctx->sb_skip_denoising = 0;
Anupam Pandey7dfb6d52022-12-05 14:19:10 +05303561 av1_pickmode_ctx_den_update(
3562 &ctx_den, zero_last_cost_orig, search_state.ref_costs_single,
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303563 search_state.frame_mv, reuse_inter_pred, best_pickmode);
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08003564 av1_denoiser_denoise(cpi, x, mi_row, mi_col, bsize, ctx, &decision,
3565 gf_temporal_ref);
3566 if (denoise_recheck_zeromv)
Anupam Pandey7dfb6d52022-12-05 14:19:10 +05303567 recheck_zeromv_after_denoising(
3568 cpi, mi, x, xd, decision, &ctx_den, search_state.yv12_mb,
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303569 &search_state.best_rdc, best_pickmode, bsize, mi_row, mi_col);
3570 best_pickmode->best_ref_frame = ctx_den.best_ref_frame;
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08003571 }
3572#endif
3573
Anupam Pandeybce4e9a2022-12-22 14:45:05 +05303574 // Update the factors used for RD thresholding for all modes.
Marco Paniconi9ebbc8b2020-11-29 22:05:39 -08003575 if (cpi->sf.inter_sf.adaptive_rd_thresh && !has_second_ref(mi)) {
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07003576 THR_MODES best_mode_idx =
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303577 mode_idx[best_pickmode->best_ref_frame][mode_offset(mi->mode)];
3578 if (best_pickmode->best_ref_frame == INTRA_FRAME) {
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07003579 // Only consider the modes that are included in the intra_mode_list.
3580 int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE);
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05303581 for (int mode_index = 0; mode_index < intra_modes; mode_index++) {
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07003582 update_thresh_freq_fact(cpi, x, bsize, INTRA_FRAME, best_mode_idx,
Anupam Pandeyd3ea12a2022-12-16 12:41:55 +05303583 intra_mode_list[mode_index]);
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07003584 }
3585 } else {
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07003586 PREDICTION_MODE this_mode;
3587 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
Anupam Pandey2b4957c2022-12-19 16:39:52 +05303588 update_thresh_freq_fact(cpi, x, bsize, best_pickmode->best_ref_frame,
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07003589 best_mode_idx, this_mode);
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07003590 }
3591 }
3592 }
kyslove3c05a82019-03-28 11:06:09 -07003593
chiyotsai03c48a82019-10-18 16:48:59 -07003594#if CONFIG_INTERNAL_STATS
Anupam Pandeyd68ec612023-01-03 14:15:58 +05303595 store_coding_context_nonrd(x, ctx, mi->mode);
chiyotsai03c48a82019-10-18 16:48:59 -07003596#else
Anupam Pandeyd68ec612023-01-03 14:15:58 +05303597 store_coding_context_nonrd(x, ctx);
chiyotsai03c48a82019-10-18 16:48:59 -07003598#endif // CONFIG_INTERNAL_STATS
Cheng Chen82c61342022-10-18 16:24:06 -07003599
Anupam Pandeyc5ee9a02023-01-06 12:15:48 +05303600#if COLLECT_NONRD_PICK_MODE_STAT
3601 aom_usec_timer_mark(&x->ms_stat_nonrd.bsize_timer);
3602 x->ms_stat_nonrd.total_block_times[bsize] +=
3603 aom_usec_timer_elapsed(&x->ms_stat_nonrd.bsize_timer);
3604 print_time(&x->ms_stat_nonrd, bsize, cm->mi_params.mi_rows,
3605 cm->mi_params.mi_cols, mi_row, mi_col);
3606#endif // COLLECT_NONRD_PICK_MODE_STAT
Cheng Chen82c61342022-10-18 16:24:06 -07003607
Anupam Pandey7dfb6d52022-12-05 14:19:10 +05303608 *rd_cost = search_state.best_rdc;
Marco Paniconi0aeecfe2023-10-04 00:06:46 -07003609
3610 // Reset the xd->block_ref_scale_factors[i], as they may have
3611 // been set to pointer &sf_no_scale, which becomes invalid afer
3612 // this function.
3613 set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
kyslove3c05a82019-03-28 11:06:09 -07003614}