blob: bc7ee02245dac2813fdde707970d5bca2efd276a [file] [log] [blame]
kyslove3c05a82019-03-28 11:06:09 -07001/*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
11 */
12
13#include <assert.h>
14#include <limits.h>
15#include <math.h>
16#include <stdio.h>
17
18#include "config/aom_dsp_rtcd.h"
19#include "config/av1_rtcd.h"
20
21#include "aom_dsp/aom_dsp_common.h"
22#include "aom_dsp/blend.h"
23#include "aom_mem/aom_mem.h"
24#include "aom_ports/aom_timer.h"
25#include "aom_ports/mem.h"
26#include "aom_ports/system_state.h"
27
Marco Paniconi46f4cb82020-02-20 11:18:59 -080028#include "av1/encoder/model_rd.h"
kyslove3c05a82019-03-28 11:06:09 -070029#include "av1/common/mvref_common.h"
30#include "av1/common/pred_common.h"
31#include "av1/common/reconinter.h"
kyslov82449d12019-05-02 13:36:50 -070032#include "av1/common/reconintra.h"
kyslove3c05a82019-03-28 11:06:09 -070033
34#include "av1/encoder/encodemv.h"
35#include "av1/encoder/rdopt.h"
36#include "av1/encoder/reconinter_enc.h"
37
38extern int g_pick_inter_mode_cnt;
Fyodor Kyslov30824012020-07-14 13:48:08 -070039/*!\cond */
kyslove3c05a82019-03-28 11:06:09 -070040typedef struct {
41 uint8_t *data;
42 int stride;
43 int in_use;
44} PRED_BUFFER;
45
46typedef struct {
47 PRED_BUFFER *best_pred;
48 PREDICTION_MODE best_mode;
49 TX_SIZE best_tx_size;
kyslove3c05a82019-03-28 11:06:09 -070050 MV_REFERENCE_FRAME best_ref_frame;
kyslove3c05a82019-03-28 11:06:09 -070051 uint8_t best_mode_skip_txfm;
Fyodor Kyslove0be17e2020-05-13 14:20:50 -070052 uint8_t best_mode_initial_skip_flag;
Ravi Chaudhary1e4f94b2019-06-20 16:19:49 +053053 int_interpfilters best_pred_filter;
kyslove3c05a82019-03-28 11:06:09 -070054} BEST_PICKMODE;
55
56typedef struct {
57 MV_REFERENCE_FRAME ref_frame;
58 PREDICTION_MODE pred_mode;
59} REF_MODE;
Fyodor Kyslov30824012020-07-14 13:48:08 -070060/*!\endcond */
kyslove3c05a82019-03-28 11:06:09 -070061
Jerome Jiang0d58dae2020-03-04 14:32:41 -080062static const int pos_shift_16x16[4][4] = {
63 { 9, 10, 13, 14 }, { 11, 12, 15, 16 }, { 17, 18, 21, 22 }, { 19, 20, 23, 24 }
64};
65
Fyodor Kyslov29430902020-11-18 13:44:30 -080066#define NUM_INTER_MODES_RT 9
67#define NUM_INTER_MODES_REDUCED 8
68
69static const REF_MODE ref_mode_set_rt[NUM_INTER_MODES_RT] = {
kyslove3c05a82019-03-28 11:06:09 -070070 { LAST_FRAME, NEARESTMV }, { LAST_FRAME, NEARMV },
71 { LAST_FRAME, NEWMV }, { GOLDEN_FRAME, NEARESTMV },
72 { GOLDEN_FRAME, NEARMV }, { GOLDEN_FRAME, NEWMV },
73 { ALTREF_FRAME, NEARESTMV }, { ALTREF_FRAME, NEARMV },
74 { ALTREF_FRAME, NEWMV }
75};
76
Fyodor Kyslov29430902020-11-18 13:44:30 -080077// GLOBALMV in the set below is in fact ZEROMV as we don't do global ME in RT
78// mode
79static const REF_MODE ref_mode_set_reduced[NUM_INTER_MODES_REDUCED] = {
80 { LAST_FRAME, GLOBALMV }, { LAST_FRAME, NEARESTMV },
81 { GOLDEN_FRAME, GLOBALMV }, { LAST_FRAME, NEARMV },
82 { LAST_FRAME, NEWMV }, { GOLDEN_FRAME, NEARESTMV },
83 { GOLDEN_FRAME, NEARMV }, { GOLDEN_FRAME, NEWMV }
84};
85
kyslove3c05a82019-03-28 11:06:09 -070086static const THR_MODES mode_idx[REF_FRAMES][4] = {
87 { THR_DC, THR_V_PRED, THR_H_PRED, THR_SMOOTH },
88 { THR_NEARESTMV, THR_NEARMV, THR_GLOBALMV, THR_NEWMV },
Fyodor Kyslov67691bd2019-07-25 14:28:34 -070089 { THR_NEARESTL2, THR_NEARL2, THR_GLOBALL2, THR_NEWL2 },
90 { THR_NEARESTL3, THR_NEARL3, THR_GLOBALL3, THR_NEWL3 },
kyslove3c05a82019-03-28 11:06:09 -070091 { THR_NEARESTG, THR_NEARG, THR_GLOBALMV, THR_NEWG },
kyslove3c05a82019-03-28 11:06:09 -070092};
93
kyslov82449d12019-05-02 13:36:50 -070094static const PREDICTION_MODE intra_mode_list[] = { DC_PRED, V_PRED, H_PRED,
95 SMOOTH_PRED };
96
97static INLINE int mode_offset(const PREDICTION_MODE mode) {
98 if (mode >= NEARESTMV) {
99 return INTER_OFFSET(mode);
100 } else {
101 switch (mode) {
102 case DC_PRED: return 0;
103 case V_PRED: return 1;
104 case H_PRED: return 2;
105 case SMOOTH_PRED: return 3;
106 default: assert(0); return -1;
107 }
108 }
109}
110
kyslove3c05a82019-03-28 11:06:09 -0700111enum {
112 // INTER_ALL = (1 << NEARESTMV) | (1 << NEARMV) | (1 << NEWMV),
113 INTER_NEAREST = (1 << NEARESTMV),
114 INTER_NEAREST_NEW = (1 << NEARESTMV) | (1 << NEWMV),
115 INTER_NEAREST_NEAR = (1 << NEARESTMV) | (1 << NEARMV),
116 INTER_NEAR_NEW = (1 << NEARMV) | (1 << NEWMV),
117};
118
119static INLINE void init_best_pickmode(BEST_PICKMODE *bp) {
120 bp->best_mode = NEARESTMV;
121 bp->best_ref_frame = LAST_FRAME;
kyslovbab94fd2019-05-03 11:25:17 -0700122 bp->best_tx_size = TX_8X8;
Sachin Kumar Garg22258532019-06-18 16:45:06 +0530123 bp->best_pred_filter = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
kyslove3c05a82019-03-28 11:06:09 -0700124 bp->best_mode_skip_txfm = 0;
Fyodor Kyslove0be17e2020-05-13 14:20:50 -0700125 bp->best_mode_initial_skip_flag = 0;
kyslove3c05a82019-03-28 11:06:09 -0700126 bp->best_pred = NULL;
127}
128
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -0700129/*!\brief Runs Motion Estimation for a specific block and specific ref frame.
130 *
131 * \ingroup nonrd_mode_search
132 * \callgraph
133 * \callergraph
134 * Finds the best Motion Vector by running Motion Estimation for a specific
135 * block and a specific reference frame. Exits early if RDCost of Full Pel part
136 * exceeds best RD Cost fund so far
137 * \param[in] cpi Top-level encoder structure
138 * \param[in] x Pointer to structure holding all the
139 * data for the current macroblock
140 * \param[in] bsize Current block size
141 * \param[in] mi_row Row index in 4x4 units
142 * \param[in] mi_col Column index in 4x4 units
143 * \param[in] tmp_mv Pointer to best found New MV
144 * \param[in] rate_mv Pointer to Rate of the best new MV
145 * \param[in] best_rd_sofar RD Cost of the best mode found so far
146 * \param[in] use_base_mv Flag, indicating that tmp_mv holds
147 * specific MV to start the search with
148 *
149 * \return Returns 0 if ME was terminated after Full Pel Search because too
150 * high RD Cost. Otherwise returns 1. Best New MV is placed into \c tmp_mv.
151 * Rate estimation for this vector is placed to \c rate_mv
152 */
kyslove3c05a82019-03-28 11:06:09 -0700153static int combined_motion_search(AV1_COMP *cpi, MACROBLOCK *x,
154 BLOCK_SIZE bsize, int mi_row, int mi_col,
155 int_mv *tmp_mv, int *rate_mv,
156 int64_t best_rd_sofar, int use_base_mv) {
157 MACROBLOCKD *xd = &x->e_mbd;
158 const AV1_COMMON *cm = &cpi->common;
159 const int num_planes = av1_num_planes(cm);
160 MB_MODE_INFO *mi = xd->mi[0];
161 struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } };
Marco Paniconid182da42020-07-28 09:05:43 -0700162 int step_param = (cpi->sf.rt_sf.fullpel_search_step_param)
163 ? cpi->sf.rt_sf.fullpel_search_step_param
164 : cpi->mv_search_params.mv_step_param;
chiyotsaie46cff72020-02-05 15:03:34 -0800165 FULLPEL_MV start_mv;
kyslove3c05a82019-03-28 11:06:09 -0700166 const int ref = mi->ref_frame[0];
167 const MV ref_mv = av1_get_ref_mv(x, mi->ref_mv_idx).as_mv;
168 MV center_mv;
169 int dis;
kyslove3c05a82019-03-28 11:06:09 -0700170 int rv = 0;
171 int cost_list[5];
172 int search_subpel = 1;
173 const YV12_BUFFER_CONFIG *scaled_ref_frame =
174 av1_get_scaled_ref_frame(cpi, ref);
175
kyslove3c05a82019-03-28 11:06:09 -0700176 if (scaled_ref_frame) {
177 int i;
178 // Swap out the reference frame for a version that's been scaled to
179 // match the resolution of the current frame, allowing the existing
180 // motion search code to be used without additional modifications.
181 for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
182 av1_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL,
183 num_planes);
184 }
kyslove3c05a82019-03-28 11:06:09 -0700185
chiyotsaie46cff72020-02-05 15:03:34 -0800186 start_mv = get_fullmv_from_mv(&ref_mv);
kyslove3c05a82019-03-28 11:06:09 -0700187
188 if (!use_base_mv)
189 center_mv = ref_mv;
190 else
191 center_mv = tmp_mv->as_mv;
Nithya V Sace29f32020-04-07 16:15:12 +0530192 const search_site_config *src_search_sites =
venkat sanampudi14affd02020-07-23 07:41:05 +0530193 cpi->mv_search_params.search_site_cfg[SS_CFG_SRC];
chiyotsai2e42a662020-02-26 17:39:03 -0800194 FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
chiyotsai7430da62020-07-27 14:06:26 -0700195 av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, &center_mv,
196 src_search_sites,
197 /*fine_search_interval=*/0);
chiyotsai2e42a662020-02-26 17:39:03 -0800198
199 av1_full_pixel_search(start_mv, &full_ms_params, step_param,
chiyotsaie4d0d852020-04-07 15:21:09 -0700200 cond_cost_list(cpi, cost_list), &tmp_mv->as_fullmv,
chiyotsai2e42a662020-02-26 17:39:03 -0800201 NULL);
kyslove3c05a82019-03-28 11:06:09 -0700202
kyslove3c05a82019-03-28 11:06:09 -0700203 // calculate the bit cost on motion vector
chiyotsai87bb8052020-02-12 16:56:33 -0800204 MV mvp_full = get_mv_from_fullmv(&tmp_mv->as_fullmv);
kyslove3c05a82019-03-28 11:06:09 -0700205
Fyodor Kyslov648c6502021-02-02 18:41:10 -0800206 *rate_mv = av1_mv_bit_cost(&mvp_full, &ref_mv, x->mv_costs->nmv_joint_cost,
207 x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
kyslove3c05a82019-03-28 11:06:09 -0700208
209 // TODO(kyslov) Account for Rate Mode!
210 rv = !(RDCOST(x->rdmult, (*rate_mv), 0) > best_rd_sofar);
211
212 if (rv && search_subpel) {
chiyotsai2aac3002020-02-13 17:02:01 -0800213 SUBPEL_MOTION_SEARCH_PARAMS ms_params;
214 av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
chiyotsai4d1b7d92020-04-07 13:56:22 -0700215 cost_list);
chiyotsaie4d0d852020-04-07 15:21:09 -0700216 MV subpel_start_mv = get_mv_from_fullmv(&tmp_mv->as_fullmv);
Nithya V Sace29f32020-04-07 16:15:12 +0530217 cpi->mv_search_params.find_fractional_mv_step(
chiyotsaie4d0d852020-04-07 15:21:09 -0700218 xd, cm, &ms_params, subpel_start_mv, &tmp_mv->as_mv, &dis,
Nithya V Sace29f32020-04-07 16:15:12 +0530219 &x->pred_sse[ref], NULL);
chiyotsai2aac3002020-02-13 17:02:01 -0800220
chiyotsaic95e3642020-04-10 13:17:06 -0700221 *rate_mv =
Fyodor Kyslov648c6502021-02-02 18:41:10 -0800222 av1_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->mv_costs->nmv_joint_cost,
223 x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
kyslove3c05a82019-03-28 11:06:09 -0700224 }
225
226 if (scaled_ref_frame) {
227 int i;
228 for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
229 }
Fyodor Kyslov29430902020-11-18 13:44:30 -0800230 // Final MV can not be equal to referance MV as this will trigger assert
231 // later. This can happen if both NEAREST and NEAR modes were skipped
232 rv = (tmp_mv->as_mv.col != ref_mv.col || tmp_mv->as_mv.row != ref_mv.row);
kyslove3c05a82019-03-28 11:06:09 -0700233 return rv;
234}
235
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -0700236/*!\brief Searches for the best New Motion Vector.
237 *
238 * \ingroup nonrd_mode_search
239 * \callgraph
240 * \callergraph
241 * Finds the best Motion Vector by doing Motion Estimation. Uses reduced
242 * complexity ME for non-LAST frames or calls \c combined_motion_search
243 * for LAST reference frame
244 * \param[in] cpi Top-level encoder structure
245 * \param[in] x Pointer to structure holding all the
246 * data for the current macroblock
247 * \param[in] frame_mv Array that holds MVs for all modes
248 * and ref frames
249 * \param[in] ref_frame Reference freme for which to find
250 * the best New MVs
251 * \param[in] gf_temporal_ref Flag, indicating temporal reference
252 * for GOLDEN frame
253 * \param[in] bsize Current block size
254 * \param[in] mi_row Row index in 4x4 units
255 * \param[in] mi_col Column index in 4x4 units
256 * \param[in] rate_mv Pointer to Rate of the best new MV
257 * \param[in] best_rdc Pointer to the RD Cost for the best
258 * mode found so far
259 *
260 * \return Returns -1 if the search was not done, otherwise returns 0.
261 * Best New MV is placed into \c frame_mv array, Rate estimation for this
262 * vector is placed to \c rate_mv
263 */
kyslove3c05a82019-03-28 11:06:09 -0700264static int search_new_mv(AV1_COMP *cpi, MACROBLOCK *x,
265 int_mv frame_mv[][REF_FRAMES],
266 MV_REFERENCE_FRAME ref_frame, int gf_temporal_ref,
Fyodor Kyslovf5c96902020-04-27 17:08:56 -0700267 BLOCK_SIZE bsize, int mi_row, int mi_col, int *rate_mv,
268 RD_STATS *best_rdc) {
kyslove3c05a82019-03-28 11:06:09 -0700269 MACROBLOCKD *const xd = &x->e_mbd;
270 MB_MODE_INFO *const mi = xd->mi[0];
271 AV1_COMMON *cm = &cpi->common;
Vishesh39e74092020-06-16 17:13:48 +0530272 if (ref_frame > LAST_FRAME && cpi->oxcf.rc_cfg.mode == AOM_CBR &&
Fyodor Kyslovf5c96902020-04-27 17:08:56 -0700273 gf_temporal_ref) {
kyslove3c05a82019-03-28 11:06:09 -0700274 int tmp_sad;
275 int dis;
276 int cost_list[5] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX };
277
278 if (bsize < BLOCK_16X16) return -1;
279
280 tmp_sad = av1_int_pro_motion_estimation(
281 cpi, x, bsize, mi_row, mi_col,
chiyotsai0b90c412020-09-29 14:48:16 -0700282 &x->mbmi_ext.ref_mv_stack[ref_frame][0].this_mv.as_mv);
kyslove3c05a82019-03-28 11:06:09 -0700283
284 if (tmp_sad > x->pred_mv_sad[LAST_FRAME]) return -1;
kyslove3c05a82019-03-28 11:06:09 -0700285
286 frame_mv[NEWMV][ref_frame].as_int = mi->mv[0].as_int;
chiyotsaie4d0d852020-04-07 15:21:09 -0700287 int_mv best_mv = mi->mv[0];
288 best_mv.as_mv.row >>= 3;
289 best_mv.as_mv.col >>= 3;
kyslove3c05a82019-03-28 11:06:09 -0700290 MV ref_mv = av1_get_ref_mv(x, 0).as_mv;
291
chiyotsaic95e3642020-04-10 13:17:06 -0700292 *rate_mv = av1_mv_bit_cost(&frame_mv[NEWMV][ref_frame].as_mv, &ref_mv,
Fyodor Kyslov648c6502021-02-02 18:41:10 -0800293 x->mv_costs->nmv_joint_cost,
294 x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
kyslove3c05a82019-03-28 11:06:09 -0700295 frame_mv[NEWMV][ref_frame].as_mv.row >>= 3;
296 frame_mv[NEWMV][ref_frame].as_mv.col >>= 3;
297
chiyotsai2aac3002020-02-13 17:02:01 -0800298 SUBPEL_MOTION_SEARCH_PARAMS ms_params;
299 av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
chiyotsai4d1b7d92020-04-07 13:56:22 -0700300 cost_list);
chiyotsaie4d0d852020-04-07 15:21:09 -0700301 MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
Nithya V Sace29f32020-04-07 16:15:12 +0530302 cpi->mv_search_params.find_fractional_mv_step(
chiyotsaie4d0d852020-04-07 15:21:09 -0700303 xd, cm, &ms_params, start_mv, &best_mv.as_mv, &dis,
Nithya V Sace29f32020-04-07 16:15:12 +0530304 &x->pred_sse[ref_frame], NULL);
chiyotsaie4d0d852020-04-07 15:21:09 -0700305 frame_mv[NEWMV][ref_frame].as_int = best_mv.as_int;
kyslove3c05a82019-03-28 11:06:09 -0700306 } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
307 &frame_mv[NEWMV][ref_frame], rate_mv,
308 best_rdc->rdcost, 0)) {
309 return -1;
310 }
311
312 return 0;
313}
314
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -0700315/*!\brief Finds predicted motion vectors for a block.
316 *
317 * \ingroup nonrd_mode_search
318 * \callgraph
319 * \callergraph
320 * Finds predicted motion vectors for a block from a certain reference frame.
321 * First, it fills reference MV stack, then picks the test from the stack and
322 * predicts the final MV for a block for each mode.
323 * \param[in] cpi Top-level encoder structure
324 * \param[in] x Pointer to structure holding all the
325 * data for the current macroblock
326 * \param[in] ref_frame Reference freme for which to find
327 * ref MVs
328 * \param[in] frame_mv Predicted MVs for a block
329 * \param[in] tile_data Pointer to struct holding adaptive
330 * data/contexts/models for the tile
331 * during encoding
332 * \param[in] yv12_mb Buffer to hold predicted block
333 * \param[in] bsize Current block size
334 * \param[in] force_skip_low_temp_var Flag indicating possible mode search
335 * prune for low temporal variace block
336 *
337 * \return Nothing is returned. Instead, predicted MVs are placed into
338 * \c frame_mv array
339 */
Fyodor Kyslov9fa800d2020-05-05 13:56:35 -0700340static INLINE void find_predictors(AV1_COMP *cpi, MACROBLOCK *x,
341 MV_REFERENCE_FRAME ref_frame,
342 int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],
343 TileDataEnc *tile_data,
344 struct buf_2d yv12_mb[8][MAX_MB_PLANE],
345 BLOCK_SIZE bsize,
346 int force_skip_low_temp_var) {
kyslove3c05a82019-03-28 11:06:09 -0700347 AV1_COMMON *const cm = &cpi->common;
348 MACROBLOCKD *const xd = &x->e_mbd;
349 MB_MODE_INFO *const mbmi = xd->mi[0];
chiyotsai0b90c412020-09-29 14:48:16 -0700350 MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
kyslove3c05a82019-03-28 11:06:09 -0700351 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, ref_frame);
352 const int num_planes = av1_num_planes(cm);
353 (void)tile_data;
kyslove3c05a82019-03-28 11:06:09 -0700354
355 x->pred_mv_sad[ref_frame] = INT_MAX;
356 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
357 // TODO(kyslov) this needs various further optimizations. to be continued..
Fyodor Kyslov9fa800d2020-05-05 13:56:35 -0700358 assert(yv12 != NULL);
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -0700359 if (yv12 != NULL) {
kyslove3c05a82019-03-28 11:06:09 -0700360 const struct scale_factors *const sf =
361 get_ref_scale_factors_const(cm, ref_frame);
Hui Sub94cd5e2019-11-06 12:05:47 -0800362 av1_setup_pred_block(xd, yv12_mb[ref_frame], yv12, sf, sf, num_planes);
kyslove3c05a82019-03-28 11:06:09 -0700363 av1_find_mv_refs(cm, xd, mbmi, ref_frame, mbmi_ext->ref_mv_count,
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530364 xd->ref_mv_stack, xd->weight, NULL, mbmi_ext->global_mvs,
Hui Sub94cd5e2019-11-06 12:05:47 -0800365 mbmi_ext->mode_context);
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530366 // TODO(Ravi): Populate mbmi_ext->ref_mv_stack[ref_frame][4] and
367 // mbmi_ext->weight[ref_frame][4] inside av1_find_mv_refs.
368 av1_copy_usable_ref_mv_stack_and_weight(xd, mbmi_ext, ref_frame);
Urvang Joshib6409e92020-03-23 11:23:27 -0700369 av1_find_best_ref_mvs_from_stack(
370 cm->features.allow_high_precision_mv, mbmi_ext, ref_frame,
371 &frame_mv[NEARESTMV][ref_frame], &frame_mv[NEARMV][ref_frame], 0);
Fyodor Kyslov29430902020-11-18 13:44:30 -0800372 frame_mv[GLOBALMV][ref_frame] = mbmi_ext->global_mvs[ref_frame];
Marco Paniconi7da95ed2020-03-05 16:20:53 -0800373 // Early exit for non-LAST frame if force_skip_low_temp_var is set.
kyslove3c05a82019-03-28 11:06:09 -0700374 if (!av1_is_scaled(sf) && bsize >= BLOCK_8X8 &&
Marco Paniconi7da95ed2020-03-05 16:20:53 -0800375 !(force_skip_low_temp_var && ref_frame != LAST_FRAME)) {
kyslove3c05a82019-03-28 11:06:09 -0700376 av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
377 bsize);
378 }
kyslove3c05a82019-03-28 11:06:09 -0700379 }
Hui Sub94cd5e2019-11-06 12:05:47 -0800380 av1_count_overlappable_neighbors(cm, xd);
kyslove3c05a82019-03-28 11:06:09 -0700381 mbmi->num_proj_ref = 1;
382}
383
Jerome Jiang906a94f2019-05-01 19:02:58 -0700384static void estimate_single_ref_frame_costs(const AV1_COMMON *cm,
385 const MACROBLOCKD *xd,
chiyotsai9a06d182020-05-01 17:12:12 -0700386 const ModeCosts *mode_costs,
387 int segment_id,
Jerome Jiang906a94f2019-05-01 19:02:58 -0700388 unsigned int *ref_costs_single) {
kyslove3c05a82019-03-28 11:06:09 -0700389 int seg_ref_active =
390 segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME);
391 if (seg_ref_active) {
392 memset(ref_costs_single, 0, REF_FRAMES * sizeof(*ref_costs_single));
kyslove3c05a82019-03-28 11:06:09 -0700393 } else {
394 int intra_inter_ctx = av1_get_intra_inter_context(xd);
chiyotsai9a06d182020-05-01 17:12:12 -0700395 ref_costs_single[INTRA_FRAME] =
396 mode_costs->intra_inter_cost[intra_inter_ctx][0];
397 unsigned int base_cost = mode_costs->intra_inter_cost[intra_inter_ctx][1];
Marco Paniconi62b66dd2020-08-27 11:30:37 -0700398 ref_costs_single[LAST_FRAME] = base_cost;
399 ref_costs_single[GOLDEN_FRAME] = base_cost;
400 ref_costs_single[ALTREF_FRAME] = base_cost;
401 // add cost for last, golden, altref
402 ref_costs_single[LAST_FRAME] += mode_costs->single_ref_cost[0][0][0];
403 ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][0][1];
404 ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][1][0];
405 ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][0][1];
406 ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][2][0];
kyslove3c05a82019-03-28 11:06:09 -0700407 }
408}
409
Jerome Jiang037f5d22019-05-02 19:32:42 -0700410static void estimate_comp_ref_frame_costs(
chiyotsai9a06d182020-05-01 17:12:12 -0700411 const AV1_COMMON *cm, const MACROBLOCKD *xd, const ModeCosts *mode_costs,
Jerome Jiang037f5d22019-05-02 19:32:42 -0700412 int segment_id, unsigned int (*ref_costs_comp)[REF_FRAMES]) {
413 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
414 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame)
415 memset(ref_costs_comp[ref_frame], 0,
416 REF_FRAMES * sizeof((*ref_costs_comp)[0]));
417 } else {
418 int intra_inter_ctx = av1_get_intra_inter_context(xd);
chiyotsai9a06d182020-05-01 17:12:12 -0700419 unsigned int base_cost = mode_costs->intra_inter_cost[intra_inter_ctx][1];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700420
421 if (cm->current_frame.reference_mode != SINGLE_REFERENCE) {
422 // Similar to single ref, determine cost of compound ref frames.
423 // cost_compound_refs = cost_first_ref + cost_second_ref
424 const int bwdref_comp_ctx_p = av1_get_pred_context_comp_bwdref_p(xd);
425 const int bwdref_comp_ctx_p1 = av1_get_pred_context_comp_bwdref_p1(xd);
426 const int ref_comp_ctx_p = av1_get_pred_context_comp_ref_p(xd);
427 const int ref_comp_ctx_p1 = av1_get_pred_context_comp_ref_p1(xd);
428 const int ref_comp_ctx_p2 = av1_get_pred_context_comp_ref_p2(xd);
429
430 const int comp_ref_type_ctx = av1_get_comp_reference_type_context(xd);
431 unsigned int ref_bicomp_costs[REF_FRAMES] = { 0 };
432
433 ref_bicomp_costs[LAST_FRAME] = ref_bicomp_costs[LAST2_FRAME] =
434 ref_bicomp_costs[LAST3_FRAME] = ref_bicomp_costs[GOLDEN_FRAME] =
chiyotsai9a06d182020-05-01 17:12:12 -0700435 base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][1];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700436 ref_bicomp_costs[BWDREF_FRAME] = ref_bicomp_costs[ALTREF2_FRAME] = 0;
437 ref_bicomp_costs[ALTREF_FRAME] = 0;
438
439 // cost of first ref frame
chiyotsai9a06d182020-05-01 17:12:12 -0700440 ref_bicomp_costs[LAST_FRAME] +=
441 mode_costs->comp_ref_cost[ref_comp_ctx_p][0][0];
442 ref_bicomp_costs[LAST2_FRAME] +=
443 mode_costs->comp_ref_cost[ref_comp_ctx_p][0][0];
444 ref_bicomp_costs[LAST3_FRAME] +=
445 mode_costs->comp_ref_cost[ref_comp_ctx_p][0][1];
446 ref_bicomp_costs[GOLDEN_FRAME] +=
447 mode_costs->comp_ref_cost[ref_comp_ctx_p][0][1];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700448
chiyotsai9a06d182020-05-01 17:12:12 -0700449 ref_bicomp_costs[LAST_FRAME] +=
450 mode_costs->comp_ref_cost[ref_comp_ctx_p1][1][0];
451 ref_bicomp_costs[LAST2_FRAME] +=
452 mode_costs->comp_ref_cost[ref_comp_ctx_p1][1][1];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700453
chiyotsai9a06d182020-05-01 17:12:12 -0700454 ref_bicomp_costs[LAST3_FRAME] +=
455 mode_costs->comp_ref_cost[ref_comp_ctx_p2][2][0];
456 ref_bicomp_costs[GOLDEN_FRAME] +=
457 mode_costs->comp_ref_cost[ref_comp_ctx_p2][2][1];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700458
459 // cost of second ref frame
460 ref_bicomp_costs[BWDREF_FRAME] +=
chiyotsai9a06d182020-05-01 17:12:12 -0700461 mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p][0][0];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700462 ref_bicomp_costs[ALTREF2_FRAME] +=
chiyotsai9a06d182020-05-01 17:12:12 -0700463 mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p][0][0];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700464 ref_bicomp_costs[ALTREF_FRAME] +=
chiyotsai9a06d182020-05-01 17:12:12 -0700465 mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p][0][1];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700466
467 ref_bicomp_costs[BWDREF_FRAME] +=
chiyotsai9a06d182020-05-01 17:12:12 -0700468 mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p1][1][0];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700469 ref_bicomp_costs[ALTREF2_FRAME] +=
chiyotsai9a06d182020-05-01 17:12:12 -0700470 mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p1][1][1];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700471
472 // cost: if one ref frame is forward ref, the other ref is backward ref
473 for (int ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) {
474 for (int ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) {
475 ref_costs_comp[ref0][ref1] =
476 ref_bicomp_costs[ref0] + ref_bicomp_costs[ref1];
477 }
478 }
479
480 // cost: if both ref frames are the same side.
481 const int uni_comp_ref_ctx_p = av1_get_pred_context_uni_comp_ref_p(xd);
482 const int uni_comp_ref_ctx_p1 = av1_get_pred_context_uni_comp_ref_p1(xd);
483 const int uni_comp_ref_ctx_p2 = av1_get_pred_context_uni_comp_ref_p2(xd);
484 ref_costs_comp[LAST_FRAME][LAST2_FRAME] =
chiyotsai9a06d182020-05-01 17:12:12 -0700485 base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][0] +
486 mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][0] +
487 mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p1][1][0];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700488 ref_costs_comp[LAST_FRAME][LAST3_FRAME] =
chiyotsai9a06d182020-05-01 17:12:12 -0700489 base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][0] +
490 mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][0] +
491 mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p1][1][1] +
492 mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p2][2][0];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700493 ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] =
chiyotsai9a06d182020-05-01 17:12:12 -0700494 base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][0] +
495 mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][0] +
496 mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p1][1][1] +
497 mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p2][2][1];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700498 ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] =
chiyotsai9a06d182020-05-01 17:12:12 -0700499 base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][0] +
500 mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][1];
Jerome Jiang037f5d22019-05-02 19:32:42 -0700501 } else {
502 for (int ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) {
503 for (int ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1)
504 ref_costs_comp[ref0][ref1] = 512;
505 }
506 ref_costs_comp[LAST_FRAME][LAST2_FRAME] = 512;
507 ref_costs_comp[LAST_FRAME][LAST3_FRAME] = 512;
508 ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = 512;
509 ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = 512;
510 }
511 }
512}
513
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700514static TX_SIZE calculate_tx_size(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
Cherma Rajan A3ed030f2019-08-16 18:56:00 +0530515 MACROBLOCK *const x, unsigned int var,
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700516 unsigned int sse) {
Cherma Rajan A3ed030f2019-08-16 18:56:00 +0530517 MACROBLOCKD *const xd = &x->e_mbd;
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700518 TX_SIZE tx_size;
chiyotsaia36d9002020-04-29 16:48:21 -0700519 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
520 if (txfm_params->tx_mode_search_type == TX_MODE_SELECT) {
Fyodor Kyslovbb367862021-03-17 12:50:11 -0700521 if (sse > (var << 1))
chiyotsaia36d9002020-04-29 16:48:21 -0700522 tx_size =
523 AOMMIN(max_txsize_lookup[bsize],
524 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700525 else
526 tx_size = TX_8X8;
527
Vishesh734eff92020-06-20 21:46:36 +0530528 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700529 cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id))
530 tx_size = TX_8X8;
531 else if (tx_size > TX_16X16)
532 tx_size = TX_16X16;
533 } else {
chiyotsaia36d9002020-04-29 16:48:21 -0700534 tx_size =
535 AOMMIN(max_txsize_lookup[bsize],
536 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700537 }
Jerome Jiangd4e351d2020-01-30 15:13:11 -0800538
chiyotsaia36d9002020-04-29 16:48:21 -0700539 if (txfm_params->tx_mode_search_type != ONLY_4X4 && bsize > BLOCK_32X32)
Jerome Jiangd4e351d2020-01-30 15:13:11 -0800540 tx_size = TX_16X16;
541
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700542 return AOMMIN(tx_size, TX_16X16);
543}
544
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700545static const uint8_t b_width_log2_lookup[BLOCK_SIZES] = { 0, 0, 1, 1, 1, 2,
546 2, 2, 3, 3, 3, 4,
547 4, 4, 5, 5 };
548static const uint8_t b_height_log2_lookup[BLOCK_SIZES] = { 0, 1, 0, 1, 2, 1,
549 2, 3, 2, 3, 4, 3,
550 4, 5, 4, 5 };
551
552static void block_variance(const uint8_t *src, int src_stride,
553 const uint8_t *ref, int ref_stride, int w, int h,
554 unsigned int *sse, int *sum, int block_size,
555 uint32_t *sse8x8, int *sum8x8, uint32_t *var8x8) {
556 int i, j, k = 0;
557
558 *sse = 0;
559 *sum = 0;
560
561 for (i = 0; i < h; i += block_size) {
562 for (j = 0; j < w; j += block_size) {
563 aom_get8x8var(src + src_stride * i + j, src_stride,
564 ref + ref_stride * i + j, ref_stride, &sse8x8[k],
565 &sum8x8[k]);
566 *sse += sse8x8[k];
567 *sum += sum8x8[k];
568 var8x8[k] = sse8x8[k] - (uint32_t)(((int64_t)sum8x8[k] * sum8x8[k]) >> 6);
569 k++;
570 }
571 }
572}
573
574static void calculate_variance(int bw, int bh, TX_SIZE tx_size,
575 unsigned int *sse_i, int *sum_i,
576 unsigned int *var_o, unsigned int *sse_o,
577 int *sum_o) {
578 const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size];
579 const int nw = 1 << (bw - b_width_log2_lookup[unit_size]);
580 const int nh = 1 << (bh - b_height_log2_lookup[unit_size]);
581 int i, j, k = 0;
582
583 for (i = 0; i < nh; i += 2) {
584 for (j = 0; j < nw; j += 2) {
585 sse_o[k] = sse_i[i * nw + j] + sse_i[i * nw + j + 1] +
586 sse_i[(i + 1) * nw + j] + sse_i[(i + 1) * nw + j + 1];
587 sum_o[k] = sum_i[i * nw + j] + sum_i[i * nw + j + 1] +
588 sum_i[(i + 1) * nw + j] + sum_i[(i + 1) * nw + j + 1];
589 var_o[k] = sse_o[k] - (uint32_t)(((int64_t)sum_o[k] * sum_o[k]) >>
590 (b_width_log2_lookup[unit_size] +
591 b_height_log2_lookup[unit_size] + 6));
592 k++;
593 }
594 }
595}
596
597// Adjust the ac_thr according to speed, width, height and normalized sum
598static int ac_thr_factor(const int speed, const int width, const int height,
599 const int norm_sum) {
600 if (speed >= 8 && norm_sum < 5) {
601 if (width <= 640 && height <= 480)
602 return 4;
603 else
604 return 2;
605 }
606 return 1;
607}
608
609static void model_skip_for_sb_y_large(AV1_COMP *cpi, BLOCK_SIZE bsize,
Marco Paniconi4d932222019-11-03 21:41:46 -0800610 int mi_row, int mi_col, MACROBLOCK *x,
Fyodor Kyslove8692412020-05-04 17:47:13 -0700611 MACROBLOCKD *xd, RD_STATS *rd_stats,
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -0700612 int *early_term, int calculate_rd) {
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700613 // Note our transform coeffs are 8 times an orthogonal transform.
614 // Hence quantizer step is also 8 times. To get effective quantizer
615 // we need to divide by 8 before sending to modeling function.
616 unsigned int sse;
617 struct macroblock_plane *const p = &x->plane[0];
618 struct macroblockd_plane *const pd = &xd->plane[0];
Ravi Chaudharyfb237132019-06-06 15:50:36 +0530619 const uint32_t dc_quant = p->dequant_QTX[0];
620 const uint32_t ac_quant = p->dequant_QTX[1];
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700621 const int64_t dc_thr = dc_quant * dc_quant >> 6;
622 int64_t ac_thr = ac_quant * ac_quant >> 6;
623 unsigned int var;
624 int sum;
625
626 const int bw = b_width_log2_lookup[bsize];
627 const int bh = b_height_log2_lookup[bsize];
628 const int num8x8 = 1 << (bw + bh - 2);
629 unsigned int sse8x8[256] = { 0 };
630 int sum8x8[256] = { 0 };
631 unsigned int var8x8[256] = { 0 };
632 TX_SIZE tx_size;
633 int k;
634 // Calculate variance for whole partition, and also save 8x8 blocks' variance
635 // to be used in following transform skipping test.
636 block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
637 4 << bw, 4 << bh, &sse, &sum, 8, sse8x8, sum8x8, var8x8);
638 var = sse - (unsigned int)(((int64_t)sum * sum) >> (bw + bh + 4));
639
Fyodor Kyslove8692412020-05-04 17:47:13 -0700640 rd_stats->sse = sse;
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700641
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -0800642#if CONFIG_AV1_TEMPORAL_DENOISING
643 if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
644 cpi->oxcf.speed > 5)
645 ac_thr = av1_scale_acskip_thresh(ac_thr, cpi->denoiser.denoising_level,
646 (abs(sum) >> (bw + bh)),
647 cpi->svc.temporal_layer_id);
648 else
649 ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width,
650 cpi->common.height, abs(sum) >> (bw + bh));
651#else
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700652 ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width,
653 cpi->common.height, abs(sum) >> (bw + bh));
654
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -0800655#endif
Cherma Rajan A3ed030f2019-08-16 18:56:00 +0530656 tx_size = calculate_tx_size(cpi, bsize, x, var, sse);
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700657 // The code below for setting skip flag assumes tranform size of at least 8x8,
658 // so force this lower limit on transform.
659 if (tx_size < TX_8X8) tx_size = TX_8X8;
660 xd->mi[0]->tx_size = tx_size;
661
662 // Evaluate if the partition block is a skippable block in Y plane.
663 {
664 unsigned int sse16x16[64] = { 0 };
665 int sum16x16[64] = { 0 };
666 unsigned int var16x16[64] = { 0 };
667 const int num16x16 = num8x8 >> 2;
668
669 unsigned int sse32x32[16] = { 0 };
670 int sum32x32[16] = { 0 };
671 unsigned int var32x32[16] = { 0 };
672 const int num32x32 = num8x8 >> 4;
673
674 int ac_test = 1;
675 int dc_test = 1;
676 const int num = (tx_size == TX_8X8)
677 ? num8x8
678 : ((tx_size == TX_16X16) ? num16x16 : num32x32);
679 const unsigned int *sse_tx =
680 (tx_size == TX_8X8) ? sse8x8
681 : ((tx_size == TX_16X16) ? sse16x16 : sse32x32);
682 const unsigned int *var_tx =
683 (tx_size == TX_8X8) ? var8x8
684 : ((tx_size == TX_16X16) ? var16x16 : var32x32);
685
686 // Calculate variance if tx_size > TX_8X8
687 if (tx_size >= TX_16X16)
688 calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16,
689 sum16x16);
690 if (tx_size == TX_32X32)
691 calculate_variance(bw, bh, TX_16X16, sse16x16, sum16x16, var32x32,
692 sse32x32, sum32x32);
693
694 // Skipping test
695 *early_term = 0;
696 for (k = 0; k < num; k++)
697 // Check if all ac coefficients can be quantized to zero.
698 if (!(var_tx[k] < ac_thr || var == 0)) {
699 ac_test = 0;
700 break;
701 }
702
703 for (k = 0; k < num; k++)
704 // Check if dc coefficient can be quantized to zero.
705 if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) {
706 dc_test = 0;
707 break;
708 }
709
710 if (ac_test && dc_test) {
Marco Paniconi4d932222019-11-03 21:41:46 -0800711 int skip_uv[2] = { 0 };
712 unsigned int var_uv[2];
713 unsigned int sse_uv[2];
714 AV1_COMMON *const cm = &cpi->common;
715 // Transform skipping test in UV planes.
716 for (int i = 1; i <= 2; i++) {
717 int j = i - 1;
718 skip_uv[j] = 1;
719 if (x->color_sensitivity[j]) {
720 skip_uv[j] = 0;
721 struct macroblock_plane *const puv = &x->plane[i];
722 struct macroblockd_plane *const puvd = &xd->plane[i];
723 const BLOCK_SIZE uv_bsize = get_plane_block_size(
724 bsize, puvd->subsampling_x, puvd->subsampling_y);
725 // Adjust these thresholds for UV.
726 const int64_t uv_dc_thr =
727 (puv->dequant_QTX[0] * puv->dequant_QTX[0]) >> 3;
728 const int64_t uv_ac_thr =
729 (puv->dequant_QTX[1] * puv->dequant_QTX[1]) >> 3;
730 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, i,
731 i);
732 var_uv[j] = cpi->fn_ptr[uv_bsize].vf(puv->src.buf, puv->src.stride,
733 puvd->dst.buf, puvd->dst.stride,
734 &sse_uv[j]);
735 if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) &&
736 (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j]))
737 skip_uv[j] = 1;
738 else
739 break;
740 }
741 }
742 if (skip_uv[0] & skip_uv[1]) {
743 *early_term = 1;
744 }
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700745 }
746 }
Fyodor Kyslove8692412020-05-04 17:47:13 -0700747 if (calculate_rd) {
Fyodor Kyslov26473052019-08-26 17:09:30 -0700748 if (!*early_term) {
749 const int bwide = block_size_wide[bsize];
750 const int bhigh = block_size_high[bsize];
751
752 model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh,
Fyodor Kyslove8692412020-05-04 17:47:13 -0700753 &rd_stats->rate, &rd_stats->dist);
Fyodor Kyslov26473052019-08-26 17:09:30 -0700754 }
755
756 if (*early_term) {
Fyodor Kyslove8692412020-05-04 17:47:13 -0700757 rd_stats->rate = 0;
758 rd_stats->dist = sse << 4;
Fyodor Kyslov26473052019-08-26 17:09:30 -0700759 }
760 }
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -0700761}
762
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700763static void model_rd_for_sb_y(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
Fyodor Kyslove8692412020-05-04 17:47:13 -0700764 MACROBLOCK *x, MACROBLOCKD *xd,
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -0700765 RD_STATS *rd_stats, int calculate_rd) {
kyslove3c05a82019-03-28 11:06:09 -0700766 // Note our transform coeffs are 8 times an orthogonal transform.
767 // Hence quantizer step is also 8 times. To get effective quantizer
768 // we need to divide by 8 before sending to modeling function.
kyslove3c05a82019-03-28 11:06:09 -0700769 const int ref = xd->mi[0]->ref_frame[0];
770
kyslove7ff3b62019-04-05 14:15:03 -0700771 assert(bsize < BLOCK_SIZES_ALL);
kyslove3c05a82019-03-28 11:06:09 -0700772
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700773 struct macroblock_plane *const p = &x->plane[0];
774 struct macroblockd_plane *const pd = &xd->plane[0];
775 unsigned int sse;
776 int rate;
777 int64_t dist;
kyslove3c05a82019-03-28 11:06:09 -0700778
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700779 unsigned int var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride,
780 pd->dst.buf, pd->dst.stride, &sse);
Cherma Rajan A3ed030f2019-08-16 18:56:00 +0530781 xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, x, var, sse);
kyslove3c05a82019-03-28 11:06:09 -0700782
Fyodor Kyslov26473052019-08-26 17:09:30 -0700783 if (calculate_rd) {
Fyodor Kyslov1dc01022019-07-02 16:47:44 -0700784 const int bwide = block_size_wide[bsize];
785 const int bhigh = block_size_high[bsize];
786 model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh, &rate,
787 &dist);
788 } else {
789 rate = INT_MAX; // this will be overwritten later with block_yrd
790 dist = INT_MAX;
791 }
Fyodor Kyslove8692412020-05-04 17:47:13 -0700792 rd_stats->sse = sse;
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700793 x->pred_sse[ref] = (unsigned int)AOMMIN(sse, UINT_MAX);
kyslove3c05a82019-03-28 11:06:09 -0700794
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700795 assert(rate >= 0);
kyslove3c05a82019-03-28 11:06:09 -0700796
Fyodor Kyslove8692412020-05-04 17:47:13 -0700797 rd_stats->skip_txfm = (rate == 0);
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -0700798 rate = AOMMIN(rate, INT_MAX);
Fyodor Kyslove8692412020-05-04 17:47:13 -0700799 rd_stats->rate = rate;
800 rd_stats->dist = dist;
kyslove3c05a82019-03-28 11:06:09 -0700801}
802
Fyodor Kyslov487b0132020-07-14 17:23:17 -0700803/*!\brief Calculates RD Cost using Hadamard transform.
804 *
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -0700805 * \ingroup nonrd_mode_search
Fyodor Kyslov487b0132020-07-14 17:23:17 -0700806 * \callgraph
807 * \callergraph
808 * Calculates RD Cost using Hadamard transform. For low bit depth this function
809 * uses low-precision set of functions (16-bit) and 32 bit for high bit depth
810 * \param[in] cpi Top-level encoder structure
811 * \param[in] x Pointer to structure holding all the data for
812 the current macroblock
813 * \param[in] mi_row Row index in 4x4 units
814 * \param[in] mi_col Column index in 4x4 units
815 * \param[in] this_rdc Pointer to calculated RD Cost
816 * \param[in] skippable Pointer to a flag indicating possible tx skip
817 * \param[in] bsize Current block size
818 * \param[in] tx_size Transform size
819 *
820 * \return Nothing is returned. Instead, calculated RD cost is placed to
821 * \c this_rdc. \c skippable flag is set if there is no non-zero quantized
822 * coefficients for Hadamard transform
823 */
kyslove3c05a82019-03-28 11:06:09 -0700824static void block_yrd(AV1_COMP *cpi, MACROBLOCK *x, int mi_row, int mi_col,
Fyodor Kyslove8692412020-05-04 17:47:13 -0700825 RD_STATS *this_rdc, int *skippable, BLOCK_SIZE bsize,
826 TX_SIZE tx_size) {
kyslove3c05a82019-03-28 11:06:09 -0700827 MACROBLOCKD *xd = &x->e_mbd;
828 const struct macroblockd_plane *pd = &xd->plane[0];
829 struct macroblock_plane *const p = &x->plane[0];
830 const int num_4x4_w = mi_size_wide[bsize];
831 const int num_4x4_h = mi_size_high[bsize];
832 const int step = 1 << (tx_size << 1);
833 const int block_step = (1 << tx_size);
Jerome Jiangc480d822019-05-01 18:30:37 -0700834 int block = 0;
kyslove3c05a82019-03-28 11:06:09 -0700835 const int max_blocks_wide =
836 num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >> 5);
837 const int max_blocks_high =
838 num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >> 5);
839 int eob_cost = 0;
840 const int bw = 4 * num_4x4_w;
841 const int bh = 4 * num_4x4_h;
842
kyslove3c05a82019-03-28 11:06:09 -0700843 (void)mi_row;
844 (void)mi_col;
kyslove3c05a82019-03-28 11:06:09 -0700845 (void)cpi;
846
Jerome Jiangd1f40c82020-02-18 14:42:46 -0800847#if CONFIG_AV1_HIGHBITDEPTH
848 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
849 aom_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
850 p->src.stride, pd->dst.buf, pd->dst.stride,
851 x->e_mbd.bd);
852 } else {
853 aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
854 pd->dst.buf, pd->dst.stride);
855 }
856#else
kyslove3c05a82019-03-28 11:06:09 -0700857 aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
858 pd->dst.buf, pd->dst.stride);
Jerome Jiangd1f40c82020-02-18 14:42:46 -0800859#endif
860
kyslove3c05a82019-03-28 11:06:09 -0700861 *skippable = 1;
862 // Keep track of the row and column of the blocks we use so that we know
863 // if we are in the unrestricted motion border.
Jerome Jiangc480d822019-05-01 18:30:37 -0700864 for (int r = 0; r < max_blocks_high; r += block_step) {
865 for (int c = 0; c < num_4x4_w; c += block_step) {
kyslove3c05a82019-03-28 11:06:09 -0700866 if (c < max_blocks_wide) {
Jingning Hanf62cbe22021-01-13 09:38:52 -0800867 const SCAN_ORDER *const scan_order = &av1_scan_orders[tx_size][DCT_DCT];
Yaowu Xu18a49962019-10-16 12:47:04 -0700868 const int block_offset = BLOCK_OFFSET(block);
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800869#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xu18a49962019-10-16 12:47:04 -0700870 tran_low_t *const coeff = p->coeff + block_offset;
871 tran_low_t *const qcoeff = p->qcoeff + block_offset;
Urvang Joshi9543ad72020-04-17 16:59:46 -0700872 tran_low_t *const dqcoeff = p->dqcoeff + block_offset;
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800873#else
874 int16_t *const low_coeff = (int16_t *)p->coeff + block_offset;
875 int16_t *const low_qcoeff = (int16_t *)p->qcoeff + block_offset;
Urvang Joshi9543ad72020-04-17 16:59:46 -0700876 int16_t *const low_dqcoeff = (int16_t *)p->dqcoeff + block_offset;
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800877#endif
kyslove3c05a82019-03-28 11:06:09 -0700878 uint16_t *const eob = &p->eobs[block];
879 const int diff_stride = bw;
880 const int16_t *src_diff;
881 src_diff = &p->src_diff[(r * diff_stride + c) << 2];
882
883 switch (tx_size) {
884 case TX_64X64:
885 assert(0); // Not implemented
886 break;
887 case TX_32X32:
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800888 assert(0); // Not used
kyslove3c05a82019-03-28 11:06:09 -0700889 break;
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800890#if CONFIG_AV1_HIGHBITDEPTH
kyslove3c05a82019-03-28 11:06:09 -0700891 case TX_16X16:
892 aom_hadamard_16x16(src_diff, diff_stride, coeff);
893 av1_quantize_fp(coeff, 16 * 16, p->zbin_QTX, p->round_fp_QTX,
894 p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
895 dqcoeff, p->dequant_QTX, eob, scan_order->scan,
896 scan_order->iscan);
897 break;
898 case TX_8X8:
899 aom_hadamard_8x8(src_diff, diff_stride, coeff);
900 av1_quantize_fp(coeff, 8 * 8, p->zbin_QTX, p->round_fp_QTX,
901 p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
902 dqcoeff, p->dequant_QTX, eob, scan_order->scan,
903 scan_order->iscan);
904 break;
Debargha Mukherjeec32fb592020-08-21 19:23:43 -0700905 default:
906 assert(tx_size == TX_4X4);
907 aom_fdct4x4(src_diff, coeff, diff_stride);
908 av1_quantize_fp(coeff, 4 * 4, p->zbin_QTX, p->round_fp_QTX,
909 p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
910 dqcoeff, p->dequant_QTX, eob, scan_order->scan,
911 scan_order->iscan);
912 break;
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800913#else
914 case TX_16X16:
915 aom_hadamard_lp_16x16(src_diff, diff_stride, low_coeff);
916 av1_quantize_lp(low_coeff, 16 * 16, p->round_fp_QTX,
917 p->quant_fp_QTX, low_qcoeff, low_dqcoeff,
918 p->dequant_QTX, eob, scan_order->scan);
919 break;
920 case TX_8X8:
921 aom_hadamard_lp_8x8(src_diff, diff_stride, low_coeff);
922 av1_quantize_lp(low_coeff, 8 * 8, p->round_fp_QTX, p->quant_fp_QTX,
923 low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
924 scan_order->scan);
925 break;
Jerome Jiangd4e351d2020-01-30 15:13:11 -0800926 default:
927 assert(tx_size == TX_4X4);
chiyotsai849bba62020-04-28 13:23:42 -0700928 aom_fdct4x4_lp(src_diff, low_coeff, diff_stride);
Jerome Jiangd4e351d2020-01-30 15:13:11 -0800929 av1_quantize_lp(low_coeff, 4 * 4, p->round_fp_QTX, p->quant_fp_QTX,
930 low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
931 scan_order->scan);
932 break;
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800933#endif
kyslove3c05a82019-03-28 11:06:09 -0700934 }
Debargha Mukherjeec32fb592020-08-21 19:23:43 -0700935 assert(*eob <= 1024);
kyslove3c05a82019-03-28 11:06:09 -0700936 *skippable &= (*eob == 0);
937 eob_cost += 1;
938 }
939 block += step;
940 }
941 }
chiyotsai8c004e12020-04-17 15:52:08 -0700942 this_rdc->skip_txfm = *skippable;
kyslove3c05a82019-03-28 11:06:09 -0700943 this_rdc->rate = 0;
Fyodor Kyslove8692412020-05-04 17:47:13 -0700944 if (this_rdc->sse < INT64_MAX) {
945 this_rdc->sse = (this_rdc->sse << 6) >> 2;
kyslove3c05a82019-03-28 11:06:09 -0700946 if (*skippable) {
Fyodor Kyslove8692412020-05-04 17:47:13 -0700947 this_rdc->dist = this_rdc->sse;
kyslove3c05a82019-03-28 11:06:09 -0700948 return;
949 }
950 }
951
952 block = 0;
953 this_rdc->dist = 0;
Jerome Jiangc480d822019-05-01 18:30:37 -0700954 for (int r = 0; r < max_blocks_high; r += block_step) {
955 for (int c = 0; c < num_4x4_w; c += block_step) {
kyslove3c05a82019-03-28 11:06:09 -0700956 if (c < max_blocks_wide) {
Yaowu Xu18a49962019-10-16 12:47:04 -0700957 const int block_offset = BLOCK_OFFSET(block);
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800958 uint16_t *const eob = &p->eobs[block];
959#if CONFIG_AV1_HIGHBITDEPTH
960 int64_t dummy;
Yaowu Xu18a49962019-10-16 12:47:04 -0700961 tran_low_t *const coeff = p->coeff + block_offset;
962 tran_low_t *const qcoeff = p->qcoeff + block_offset;
Urvang Joshi9543ad72020-04-17 16:59:46 -0700963 tran_low_t *const dqcoeff = p->dqcoeff + block_offset;
kyslove3c05a82019-03-28 11:06:09 -0700964
965 if (*eob == 1)
966 this_rdc->rate += (int)abs(qcoeff[0]);
967 else if (*eob > 1)
968 this_rdc->rate += aom_satd(qcoeff, step << 4);
969
970 this_rdc->dist +=
971 av1_block_error(coeff, dqcoeff, step << 4, &dummy) >> 2;
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800972#else
973 int16_t *const low_coeff = (int16_t *)p->coeff + block_offset;
974 int16_t *const low_qcoeff = (int16_t *)p->qcoeff + block_offset;
Urvang Joshi9543ad72020-04-17 16:59:46 -0700975 int16_t *const low_dqcoeff = (int16_t *)p->dqcoeff + block_offset;
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800976
977 if (*eob == 1)
978 this_rdc->rate += (int)abs(low_qcoeff[0]);
979 else if (*eob > 1)
Fyodor Kyslov093688b2020-01-29 16:43:15 -0800980 this_rdc->rate += aom_satd_lp(low_qcoeff, step << 4);
Fyodor Kyslov2ab25442020-01-28 16:41:26 -0800981
982 this_rdc->dist +=
983 av1_block_error_lp(low_coeff, low_dqcoeff, step << 4) >> 2;
984#endif
kyslove3c05a82019-03-28 11:06:09 -0700985 }
986 block += step;
987 }
988 }
989
990 // If skippable is set, rate gets clobbered later.
991 this_rdc->rate <<= (2 + AV1_PROB_COST_SHIFT);
992 this_rdc->rate += (eob_cost << AV1_PROB_COST_SHIFT);
993}
994
kyslov82449d12019-05-02 13:36:50 -0700995static INLINE void init_mbmi(MB_MODE_INFO *mbmi, PREDICTION_MODE pred_mode,
996 MV_REFERENCE_FRAME ref_frame0,
997 MV_REFERENCE_FRAME ref_frame1,
kyslove3c05a82019-03-28 11:06:09 -0700998 const AV1_COMMON *cm) {
999 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
kyslove3c05a82019-03-28 11:06:09 -07001000 mbmi->ref_mv_idx = 0;
kyslov82449d12019-05-02 13:36:50 -07001001 mbmi->mode = pred_mode;
kyslove3c05a82019-03-28 11:06:09 -07001002 mbmi->uv_mode = UV_DC_PRED;
kyslov82449d12019-05-02 13:36:50 -07001003 mbmi->ref_frame[0] = ref_frame0;
1004 mbmi->ref_frame[1] = ref_frame1;
kyslove3c05a82019-03-28 11:06:09 -07001005 pmi->palette_size[0] = 0;
1006 pmi->palette_size[1] = 0;
1007 mbmi->filter_intra_mode_info.use_filter_intra = 0;
1008 mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0;
1009 mbmi->motion_mode = SIMPLE_TRANSLATION;
1010 mbmi->num_proj_ref = 1;
1011 mbmi->interintra_mode = 0;
Urvang Joshi6237b882020-03-26 15:02:26 -07001012 set_default_interp_filters(mbmi, cm->features.interp_filter);
kyslove3c05a82019-03-28 11:06:09 -07001013}
1014
chiyotsai03c48a82019-10-18 16:48:59 -07001015#if CONFIG_INTERNAL_STATS
kyslove3c05a82019-03-28 11:06:09 -07001016static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx,
1017 int mode_index) {
chiyotsai03c48a82019-10-18 16:48:59 -07001018#else
1019static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1020#endif // CONFIG_INTERNAL_STATS
kyslove3c05a82019-03-28 11:06:09 -07001021 MACROBLOCKD *const xd = &x->e_mbd;
chiyotsai4c1e5c62020-04-30 17:54:14 -07001022 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
kyslove3c05a82019-03-28 11:06:09 -07001023
1024 // Take a snapshot of the coding context so it can be
1025 // restored if we decide to encode this way
chiyotsai4c1e5c62020-04-30 17:54:14 -07001026 ctx->rd_stats.skip_txfm = txfm_info->skip_txfm;
1027
Marco Paniconi553e0382020-03-20 19:12:34 -07001028 memset(ctx->blk_skip, 0, sizeof(ctx->blk_skip[0]) * ctx->num_4x4_blk);
1029 memset(ctx->tx_type_map, DCT_DCT,
1030 sizeof(ctx->tx_type_map[0]) * ctx->num_4x4_blk);
chiyotsai4c1e5c62020-04-30 17:54:14 -07001031 ctx->skippable = txfm_info->skip_txfm;
chiyotsai03c48a82019-10-18 16:48:59 -07001032#if CONFIG_INTERNAL_STATS
kyslove3c05a82019-03-28 11:06:09 -07001033 ctx->best_mode_index = mode_index;
chiyotsai03c48a82019-10-18 16:48:59 -07001034#endif // CONFIG_INTERNAL_STATS
kyslove3c05a82019-03-28 11:06:09 -07001035 ctx->mic = *xd->mi[0];
chiyotsai4c1e5c62020-04-30 17:54:14 -07001036 ctx->skippable = txfm_info->skip_txfm;
chiyotsai0b90c412020-09-29 14:48:16 -07001037 av1_copy_mbmi_ext_to_mbmi_ext_frame(&ctx->mbmi_ext_best, &x->mbmi_ext,
Ravi Chaudharye1a59ce2020-03-24 08:21:18 +05301038 av1_ref_frame_type(xd->mi[0]->ref_frame));
kyslove3c05a82019-03-28 11:06:09 -07001039 ctx->comp_pred_diff = 0;
1040 ctx->hybrid_pred_diff = 0;
1041 ctx->single_pred_diff = 0;
1042}
1043
1044static int get_pred_buffer(PRED_BUFFER *p, int len) {
Jerome Jiangc480d822019-05-01 18:30:37 -07001045 for (int i = 0; i < len; i++) {
kyslove3c05a82019-03-28 11:06:09 -07001046 if (!p[i].in_use) {
1047 p[i].in_use = 1;
1048 return i;
1049 }
1050 }
1051 return -1;
1052}
1053
1054static void free_pred_buffer(PRED_BUFFER *p) {
1055 if (p != NULL) p->in_use = 0;
1056}
1057
chiyotsai9a06d182020-05-01 17:12:12 -07001058static int cost_mv_ref(const ModeCosts *const mode_costs, PREDICTION_MODE mode,
kyslove3c05a82019-03-28 11:06:09 -07001059 int16_t mode_context) {
1060 if (is_inter_compound_mode(mode)) {
chiyotsai9a06d182020-05-01 17:12:12 -07001061 return mode_costs
kyslove3c05a82019-03-28 11:06:09 -07001062 ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)];
1063 }
1064
1065 int mode_cost = 0;
1066 int16_t mode_ctx = mode_context & NEWMV_CTX_MASK;
1067
1068 assert(is_inter_mode(mode));
1069
1070 if (mode == NEWMV) {
chiyotsai9a06d182020-05-01 17:12:12 -07001071 mode_cost = mode_costs->newmv_mode_cost[mode_ctx][0];
kyslove3c05a82019-03-28 11:06:09 -07001072 return mode_cost;
1073 } else {
chiyotsai9a06d182020-05-01 17:12:12 -07001074 mode_cost = mode_costs->newmv_mode_cost[mode_ctx][1];
kyslove3c05a82019-03-28 11:06:09 -07001075 mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
1076
1077 if (mode == GLOBALMV) {
chiyotsai9a06d182020-05-01 17:12:12 -07001078 mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][0];
kyslove3c05a82019-03-28 11:06:09 -07001079 return mode_cost;
1080 } else {
chiyotsai9a06d182020-05-01 17:12:12 -07001081 mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][1];
kyslove3c05a82019-03-28 11:06:09 -07001082 mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
chiyotsai9a06d182020-05-01 17:12:12 -07001083 mode_cost += mode_costs->refmv_mode_cost[mode_ctx][mode != NEARESTMV];
kyslove3c05a82019-03-28 11:06:09 -07001084 return mode_cost;
1085 }
1086 }
1087}
1088
1089static void newmv_diff_bias(MACROBLOCKD *xd, PREDICTION_MODE this_mode,
1090 RD_STATS *this_rdc, BLOCK_SIZE bsize, int mv_row,
Marco Paniconi221b8992020-10-01 11:14:41 -07001091 int mv_col, int speed, uint32_t spatial_variance,
Marco Paniconi988b34a2020-11-09 12:41:13 -08001092 CONTENT_STATE_SB content_state_sb) {
kyslove3c05a82019-03-28 11:06:09 -07001093 // Bias against MVs associated with NEWMV mode that are very different from
1094 // top/left neighbors.
1095 if (this_mode == NEWMV) {
1096 int al_mv_average_row;
1097 int al_mv_average_col;
1098 int left_row, left_col;
1099 int row_diff, col_diff;
1100 int above_mv_valid = 0;
1101 int left_mv_valid = 0;
1102 int above_row = 0;
1103 int above_col = 0;
Marco Paniconi988b34a2020-11-09 12:41:13 -08001104 if (bsize >= BLOCK_64X64 && content_state_sb.source_sad != kHighSad &&
Marco Paniconi221b8992020-10-01 11:14:41 -07001105 spatial_variance < 300 &&
1106 (mv_row > 16 || mv_row < -16 || mv_col > 16 || mv_col < -16)) {
1107 this_rdc->rdcost = this_rdc->rdcost << 2;
1108 return;
1109 }
kyslove3c05a82019-03-28 11:06:09 -07001110 if (xd->above_mbmi) {
1111 above_mv_valid = xd->above_mbmi->mv[0].as_int != INVALID_MV;
1112 above_row = xd->above_mbmi->mv[0].as_mv.row;
1113 above_col = xd->above_mbmi->mv[0].as_mv.col;
1114 }
1115 if (xd->left_mbmi) {
1116 left_mv_valid = xd->left_mbmi->mv[0].as_int != INVALID_MV;
1117 left_row = xd->left_mbmi->mv[0].as_mv.row;
1118 left_col = xd->left_mbmi->mv[0].as_mv.col;
1119 }
1120 if (above_mv_valid && left_mv_valid) {
1121 al_mv_average_row = (above_row + left_row + 1) >> 1;
1122 al_mv_average_col = (above_col + left_col + 1) >> 1;
1123 } else if (above_mv_valid) {
1124 al_mv_average_row = above_row;
1125 al_mv_average_col = above_col;
1126 } else if (left_mv_valid) {
1127 al_mv_average_row = left_row;
1128 al_mv_average_col = left_col;
1129 } else {
1130 al_mv_average_row = al_mv_average_col = 0;
1131 }
Jerome Jiangc480d822019-05-01 18:30:37 -07001132 row_diff = al_mv_average_row - mv_row;
1133 col_diff = al_mv_average_col - mv_col;
Fyodor Kyslov990da972019-11-15 11:15:38 -08001134 if (row_diff > 80 || row_diff < -80 || col_diff > 80 || col_diff < -80) {
1135 if (bsize >= BLOCK_32X32)
kyslove3c05a82019-03-28 11:06:09 -07001136 this_rdc->rdcost = this_rdc->rdcost << 1;
1137 else
1138 this_rdc->rdcost = 5 * this_rdc->rdcost >> 2;
1139 }
Marco Paniconi6a966f12020-02-13 12:14:28 -08001140 } else {
1141 // Bias for speed >= 8 for low spatial variance.
1142 if (speed >= 8 && spatial_variance < 150 &&
1143 (mv_row > 64 || mv_row < -64 || mv_col > 64 || mv_col < -64))
1144 this_rdc->rdcost = 5 * this_rdc->rdcost >> 2;
kyslove3c05a82019-03-28 11:06:09 -07001145 }
kyslove3c05a82019-03-28 11:06:09 -07001146}
1147
Fyodor Kyslovabef54f2019-08-14 14:43:12 -07001148static void model_rd_for_sb_uv(AV1_COMP *cpi, BLOCK_SIZE plane_bsize,
1149 MACROBLOCK *x, MACROBLOCKD *xd,
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07001150 RD_STATS *this_rdc, int64_t *sse_y,
1151 int start_plane, int stop_plane) {
Fyodor Kyslovabef54f2019-08-14 14:43:12 -07001152 // Note our transform coeffs are 8 times an orthogonal transform.
1153 // Hence quantizer step is also 8 times. To get effective quantizer
1154 // we need to divide by 8 before sending to modeling function.
1155 unsigned int sse;
1156 int rate;
1157 int64_t dist;
1158 int i;
Fyodor Kyslove8692412020-05-04 17:47:13 -07001159 int64_t tot_sse = *sse_y;
Fyodor Kyslovabef54f2019-08-14 14:43:12 -07001160
1161 this_rdc->rate = 0;
1162 this_rdc->dist = 0;
chiyotsai8c004e12020-04-17 15:52:08 -07001163 this_rdc->skip_txfm = 0;
Fyodor Kyslovabef54f2019-08-14 14:43:12 -07001164
1165 for (i = start_plane; i <= stop_plane; ++i) {
1166 struct macroblock_plane *const p = &x->plane[i];
1167 struct macroblockd_plane *const pd = &xd->plane[i];
1168 const uint32_t dc_quant = p->dequant_QTX[0];
1169 const uint32_t ac_quant = p->dequant_QTX[1];
1170 const BLOCK_SIZE bs = plane_bsize;
1171 unsigned int var;
1172 if (!x->color_sensitivity[i - 1]) continue;
1173
1174 var = cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf,
1175 pd->dst.stride, &sse);
1176 assert(sse >= var);
Fyodor Kyslovabef54f2019-08-14 14:43:12 -07001177 tot_sse += sse;
1178
1179 av1_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
1180 dc_quant >> 3, &rate, &dist);
1181
1182 this_rdc->rate += rate >> 1;
1183 this_rdc->dist += dist << 3;
1184
1185 av1_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs], ac_quant >> 3,
1186 &rate, &dist);
1187
1188 this_rdc->rate += rate;
1189 this_rdc->dist += dist << 4;
1190 }
1191
Fyodor Kyslov4c7776b2019-09-10 10:31:01 -07001192 if (this_rdc->rate == 0) {
chiyotsai8c004e12020-04-17 15:52:08 -07001193 this_rdc->skip_txfm = 1;
Fyodor Kyslov4c7776b2019-09-10 10:31:01 -07001194 }
1195
1196 if (RDCOST(x->rdmult, this_rdc->rate, this_rdc->dist) >=
Fyodor Kyslove8692412020-05-04 17:47:13 -07001197 RDCOST(x->rdmult, 0, tot_sse << 4)) {
Fyodor Kyslov4c7776b2019-09-10 10:31:01 -07001198 this_rdc->rate = 0;
1199 this_rdc->dist = tot_sse << 4;
chiyotsai8c004e12020-04-17 15:52:08 -07001200 this_rdc->skip_txfm = 1;
Fyodor Kyslov4c7776b2019-09-10 10:31:01 -07001201 }
1202
Fyodor Kyslovabef54f2019-08-14 14:43:12 -07001203 *sse_y = tot_sse;
1204}
1205
Fyodor Kyslov30824012020-07-14 13:48:08 -07001206/*!\cond */
kyslov82449d12019-05-02 13:36:50 -07001207struct estimate_block_intra_args {
1208 AV1_COMP *cpi;
1209 MACROBLOCK *x;
1210 PREDICTION_MODE mode;
1211 int skippable;
1212 RD_STATS *rdc;
1213};
Fyodor Kyslov30824012020-07-14 13:48:08 -07001214/*!\endcond */
kyslov82449d12019-05-02 13:36:50 -07001215
Fyodor Kyslov30824012020-07-14 13:48:08 -07001216/*!\brief Estimation of RD cost of an intra mode for Non-RD optimized case.
1217 *
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -07001218 * \ingroup nonrd_mode_search
Fyodor Kyslov30824012020-07-14 13:48:08 -07001219 * \callgraph
1220 * \callergraph
1221 * Calculates RD Cost for an intra mode for a single TX block using Hadamard
1222 * transform.
1223 * \param[in] plane Color plane
1224 * \param[in] block Index of a TX block in a prediction block
1225 * \param[in] row Row of a current TX block
1226 * \param[in] col Column of a current TX block
1227 * \param[in] plane_bsize Block size of a current prediction block
1228 * \param[in] tx_size Transform size
1229 * \param[in] arg Pointer to a structure that holds paramaters
1230 * for intra mode search
1231 *
1232 * \return Nothing is returned. Instead, best mode and RD Cost of the best mode
1233 * are set in \c args->rdc and \c args->mode
1234 */
kyslov82449d12019-05-02 13:36:50 -07001235static void estimate_block_intra(int plane, int block, int row, int col,
1236 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
1237 void *arg) {
1238 struct estimate_block_intra_args *const args = arg;
1239 AV1_COMP *const cpi = args->cpi;
1240 AV1_COMMON *const cm = &cpi->common;
1241 MACROBLOCK *const x = args->x;
1242 MACROBLOCKD *const xd = &x->e_mbd;
1243 struct macroblock_plane *const p = &x->plane[plane];
1244 struct macroblockd_plane *const pd = &xd->plane[plane];
Ruiling Song4b17f482019-06-26 09:33:19 +08001245 const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
kyslov82449d12019-05-02 13:36:50 -07001246 uint8_t *const src_buf_base = p->src.buf;
1247 uint8_t *const dst_buf_base = pd->dst.buf;
1248 const int64_t src_stride = p->src.stride;
1249 const int64_t dst_stride = pd->dst.stride;
1250 RD_STATS this_rdc;
1251
Fyodor Kyslov5695d732019-06-28 16:28:48 -07001252 (void)block;
Fyodor Kyslov5695d732019-06-28 16:28:48 -07001253
kyslov82449d12019-05-02 13:36:50 -07001254 p->src.buf = &src_buf_base[4 * (row * src_stride + col)];
1255 pd->dst.buf = &dst_buf_base[4 * (row * dst_stride + col)];
1256
Fyodor Kyslovabef54f2019-08-14 14:43:12 -07001257 av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
Fyodor Kyslove8692412020-05-04 17:47:13 -07001258 av1_invalid_rd_stats(&this_rdc);
kyslov82449d12019-05-02 13:36:50 -07001259
1260 if (plane == 0) {
Fyodor Kyslove8692412020-05-04 17:47:13 -07001261 block_yrd(cpi, x, 0, 0, &this_rdc, &args->skippable, bsize_tx,
Fyodor Kyslov1951ed52019-05-21 16:14:19 -07001262 AOMMIN(tx_size, TX_16X16));
1263 } else {
Fyodor Kyslove8692412020-05-04 17:47:13 -07001264 int64_t sse = 0;
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07001265 model_rd_for_sb_uv(cpi, plane_bsize, x, xd, &this_rdc, &sse, plane, plane);
kyslov82449d12019-05-02 13:36:50 -07001266 }
1267
1268 p->src.buf = src_buf_base;
1269 pd->dst.buf = dst_buf_base;
1270 args->rdc->rate += this_rdc.rate;
1271 args->rdc->dist += this_rdc.dist;
1272}
1273
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07001274static INLINE void update_thresh_freq_fact(AV1_COMP *cpi, MACROBLOCK *x,
1275 BLOCK_SIZE bsize,
1276 MV_REFERENCE_FRAME ref_frame,
1277 THR_MODES best_mode_idx,
1278 PREDICTION_MODE mode) {
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08001279 const THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
1280 const BLOCK_SIZE min_size = AOMMAX(bsize - 3, BLOCK_4X4);
1281 const BLOCK_SIZE max_size = AOMMIN(bsize + 6, BLOCK_128X128);
1282 for (BLOCK_SIZE bs = min_size; bs <= max_size; bs += 3) {
Fyodor Kyslov29430902020-11-18 13:44:30 -08001283 int *freq_fact = &x->thresh_freq_fact[bs][thr_mode_idx];
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08001284 if (thr_mode_idx == best_mode_idx) {
1285 *freq_fact -= (*freq_fact >> 4);
1286 } else {
1287 *freq_fact =
1288 AOMMIN(*freq_fact + RD_THRESH_INC,
1289 cpi->sf.inter_sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
1290 }
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07001291 }
1292}
1293
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08001294#if CONFIG_AV1_TEMPORAL_DENOISING
1295static void av1_pickmode_ctx_den_update(
1296 AV1_PICKMODE_CTX_DEN *ctx_den, int64_t zero_last_cost_orig,
1297 unsigned int ref_frame_cost[REF_FRAMES],
1298 int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], int reuse_inter_pred,
1299 BEST_PICKMODE *bp) {
1300 ctx_den->zero_last_cost_orig = zero_last_cost_orig;
1301 ctx_den->ref_frame_cost = ref_frame_cost;
1302 ctx_den->frame_mv = frame_mv;
1303 ctx_den->reuse_inter_pred = reuse_inter_pred;
1304 ctx_den->best_tx_size = bp->best_tx_size;
1305 ctx_den->best_mode = bp->best_mode;
1306 ctx_den->best_ref_frame = bp->best_ref_frame;
1307 ctx_den->best_pred_filter = bp->best_pred_filter;
1308 ctx_den->best_mode_skip_txfm = bp->best_mode_skip_txfm;
1309}
1310
1311static void recheck_zeromv_after_denoising(
1312 AV1_COMP *cpi, MB_MODE_INFO *const mi, MACROBLOCK *x, MACROBLOCKD *const xd,
1313 AV1_DENOISER_DECISION decision, AV1_PICKMODE_CTX_DEN *ctx_den,
1314 struct buf_2d yv12_mb[4][MAX_MB_PLANE], RD_STATS *best_rdc,
1315 BEST_PICKMODE *best_pickmode, BLOCK_SIZE bsize, int mi_row, int mi_col) {
1316 // If INTRA or GOLDEN reference was selected, re-evaluate ZEROMV on
1317 // denoised result. Only do this under noise conditions, and if rdcost of
1318 // ZEROMV onoriginal source is not significantly higher than rdcost of best
1319 // mode.
1320 if (cpi->noise_estimate.enabled && cpi->noise_estimate.level > kLow &&
1321 ctx_den->zero_last_cost_orig < (best_rdc->rdcost << 3) &&
1322 ((ctx_den->best_ref_frame == INTRA_FRAME && decision >= FILTER_BLOCK) ||
1323 (ctx_den->best_ref_frame == GOLDEN_FRAME &&
1324 cpi->svc.number_spatial_layers == 1 &&
1325 decision == FILTER_ZEROMV_BLOCK))) {
1326 // Check if we should pick ZEROMV on denoised signal.
1327 AV1_COMMON *const cm = &cpi->common;
1328 RD_STATS this_rdc;
1329 const ModeCosts *mode_costs = &x->mode_costs;
1330 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
1331 MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
1332
1333 mi->mode = GLOBALMV;
1334 mi->ref_frame[0] = LAST_FRAME;
1335 mi->ref_frame[1] = NONE_FRAME;
1336 set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME);
1337 mi->mv[0].as_int = 0;
1338 mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
1339 xd->plane[0].pre[0] = yv12_mb[LAST_FRAME][0];
1340 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1341 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, 1);
1342
1343 const int16_t mode_ctx =
1344 av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame);
1345 this_rdc.rate += cost_mv_ref(mode_costs, GLOBALMV, mode_ctx);
1346
1347 this_rdc.rate += ctx_den->ref_frame_cost[LAST_FRAME];
1348 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1349 txfm_info->skip_txfm = this_rdc.skip_txfm;
1350 // Don't switch to ZEROMV if the rdcost for ZEROMV on denoised source
1351 // is higher than best_ref mode (on original source).
1352 if (this_rdc.rdcost > best_rdc->rdcost) {
1353 this_rdc = *best_rdc;
1354 mi->mode = best_pickmode->best_mode;
1355 mi->ref_frame[0] = best_pickmode->best_ref_frame;
1356 set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME);
1357 mi->interp_filters = best_pickmode->best_pred_filter;
1358 if (best_pickmode->best_ref_frame == INTRA_FRAME) {
1359 mi->mv[0].as_int = INVALID_MV;
1360 } else {
1361 mi->mv[0].as_int = ctx_den
1362 ->frame_mv[best_pickmode->best_mode]
1363 [best_pickmode->best_ref_frame]
1364 .as_int;
1365 if (ctx_den->reuse_inter_pred) {
1366 xd->plane[0].pre[0] = yv12_mb[GOLDEN_FRAME][0];
1367 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1368 }
1369 }
1370 mi->tx_size = best_pickmode->best_tx_size;
1371 txfm_info->skip_txfm = best_pickmode->best_mode_skip_txfm;
1372 } else {
1373 ctx_den->best_ref_frame = LAST_FRAME;
1374 *best_rdc = this_rdc;
1375 }
1376 }
1377}
1378#endif // CONFIG_AV1_TEMPORAL_DENOISING
1379
Jerome Jiang0d58dae2020-03-04 14:32:41 -08001380static INLINE int get_force_skip_low_temp_var_small_sb(uint8_t *variance_low,
1381 int mi_row, int mi_col,
1382 BLOCK_SIZE bsize) {
1383 // Relative indices of MB inside the superblock.
1384 const int mi_x = mi_row & 0xF;
1385 const int mi_y = mi_col & 0xF;
1386 // Relative indices of 16x16 block inside the superblock.
1387 const int i = mi_x >> 2;
1388 const int j = mi_y >> 2;
1389 int force_skip_low_temp_var = 0;
1390 // Set force_skip_low_temp_var based on the block size and block offset.
1391 switch (bsize) {
1392 case BLOCK_64X64: force_skip_low_temp_var = variance_low[0]; break;
1393 case BLOCK_64X32:
1394 if (!mi_y && !mi_x) {
1395 force_skip_low_temp_var = variance_low[1];
1396 } else if (!mi_y && mi_x) {
1397 force_skip_low_temp_var = variance_low[2];
1398 }
1399 break;
1400 case BLOCK_32X64:
1401 if (!mi_y && !mi_x) {
1402 force_skip_low_temp_var = variance_low[3];
1403 } else if (mi_y && !mi_x) {
1404 force_skip_low_temp_var = variance_low[4];
1405 }
1406 break;
1407 case BLOCK_32X32:
1408 if (!mi_y && !mi_x) {
1409 force_skip_low_temp_var = variance_low[5];
1410 } else if (mi_y && !mi_x) {
1411 force_skip_low_temp_var = variance_low[6];
1412 } else if (!mi_y && mi_x) {
1413 force_skip_low_temp_var = variance_low[7];
1414 } else if (mi_y && mi_x) {
1415 force_skip_low_temp_var = variance_low[8];
1416 }
1417 break;
1418 case BLOCK_32X16:
1419 case BLOCK_16X32:
1420 case BLOCK_16X16:
1421 force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]];
1422 break;
1423 default: break;
1424 }
1425
1426 return force_skip_low_temp_var;
1427}
1428
Fyodor Kyslov59745d92019-07-30 18:16:54 -07001429static INLINE int get_force_skip_low_temp_var(uint8_t *variance_low, int mi_row,
1430 int mi_col, BLOCK_SIZE bsize) {
1431 int force_skip_low_temp_var = 0;
1432 int x, y;
Jerome Jianga996e772020-02-25 11:40:13 -08001433 x = (mi_col & 0x1F) >> 4;
1434 // y = (mi_row & 0x1F) >> 4;
1435 // const int idx64 = (y << 1) + x;
1436 y = (mi_row & 0x17) >> 3;
1437 const int idx64 = y + x;
1438
1439 x = (mi_col & 0xF) >> 3;
1440 // y = (mi_row & 0xF) >> 3;
1441 // const int idx32 = (y << 1) + x;
1442 y = (mi_row & 0xB) >> 2;
1443 const int idx32 = y + x;
1444
1445 x = (mi_col & 0x7) >> 2;
1446 // y = (mi_row & 0x7) >> 2;
1447 // const int idx16 = (y << 1) + x;
1448 y = (mi_row & 0x5) >> 1;
1449 const int idx16 = y + x;
Fyodor Kyslov59745d92019-07-30 18:16:54 -07001450 // Set force_skip_low_temp_var based on the block size and block offset.
1451 switch (bsize) {
1452 case BLOCK_128X128: force_skip_low_temp_var = variance_low[0]; break;
Jerome Jianga996e772020-02-25 11:40:13 -08001453 case BLOCK_128X64:
1454 assert((mi_col & 0x1F) == 0);
1455 force_skip_low_temp_var = variance_low[1 + ((mi_row & 0x1F) != 0)];
1456 break;
1457 case BLOCK_64X128:
1458 assert((mi_row & 0x1F) == 0);
1459 force_skip_low_temp_var = variance_low[3 + ((mi_col & 0x1F) != 0)];
1460 break;
Fyodor Kyslov59745d92019-07-30 18:16:54 -07001461 case BLOCK_64X64:
Jerome Jianga996e772020-02-25 11:40:13 -08001462 // Location of this 64x64 block inside the 128x128 superblock
1463 force_skip_low_temp_var = variance_low[5 + idx64];
1464 break;
1465 case BLOCK_64X32:
1466 x = (mi_col & 0x1F) >> 4;
1467 y = (mi_row & 0x1F) >> 3;
1468 /*
1469 .---------------.---------------.
1470 | x=0,y=0,idx=0 | x=0,y=0,idx=2 |
1471 :---------------+---------------:
1472 | x=0,y=1,idx=1 | x=1,y=1,idx=3 |
1473 :---------------+---------------:
1474 | x=0,y=2,idx=4 | x=1,y=2,idx=6 |
1475 :---------------+---------------:
1476 | x=0,y=3,idx=5 | x=1,y=3,idx=7 |
1477 '---------------'---------------'
1478 */
1479 const int idx64x32 = (x << 1) + (y % 2) + ((y >> 1) << 2);
1480 force_skip_low_temp_var = variance_low[9 + idx64x32];
1481 break;
1482 case BLOCK_32X64:
1483 x = (mi_col & 0x1F) >> 3;
1484 y = (mi_row & 0x1F) >> 4;
1485 const int idx32x64 = (y << 2) + x;
1486 force_skip_low_temp_var = variance_low[17 + idx32x64];
1487 break;
Fyodor Kyslov59745d92019-07-30 18:16:54 -07001488 case BLOCK_32X32:
Jerome Jianga996e772020-02-25 11:40:13 -08001489 force_skip_low_temp_var = variance_low[25 + (idx64 << 2) + idx32];
1490 break;
1491 case BLOCK_32X16:
1492 case BLOCK_16X32:
Fyodor Kyslov59745d92019-07-30 18:16:54 -07001493 case BLOCK_16X16:
Jerome Jianga996e772020-02-25 11:40:13 -08001494 force_skip_low_temp_var =
1495 variance_low[41 + (idx64 << 4) + (idx32 << 2) + idx16];
1496 break;
Fyodor Kyslov59745d92019-07-30 18:16:54 -07001497 default: break;
1498 }
1499 return force_skip_low_temp_var;
1500}
1501
Fyodor Kyslov94426692019-08-29 10:41:55 -07001502#define FILTER_SEARCH_SIZE 2
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001503/*!\brief Searches for the best intrpolation filter
1504 *
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -07001505 * \ingroup nonrd_mode_search
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001506 * \callgraph
1507 * \callergraph
1508 * Iterates through subset of possible interpolation filters (currently
1509 * only EIGHTTAP_REGULAR and EIGTHTAP_SMOOTH in both directions) and selects
1510 * the one that gives lowest RD cost. RD cost is calculated using curvfit model
1511 *
1512 * \param[in] cpi Top-level encoder structure
1513 * \param[in] x Pointer to structure holding all the
1514 * data for the current macroblock
1515 * \param[in] this_rdc Pointer to calculated RD Cost
1516 * \param[in] mi_row Row index in 4x4 units
1517 * \param[in] mi_col Column index in 4x4 units
1518 * \param[in] tmp Pointer to a temporary buffer for
1519 * prediction re-use
1520 * \param[in] bsize Current block size
1521 * \param[in] reuse_inter_pred Flag, indicating prediction re-use
1522 * \param[out] this_mode_pred Pointer to store prediction buffer
1523 * for prediction re-use
1524 * \param[out] this_early_term Flag, indicating that transform can be
1525 * skipped
1526 * \param[in] use_model_yrd_large Flag, indicating special logic to handle
1527 * large blocks
1528 *
1529 * \return Nothing is returned. Instead, calculated RD cost is placed to
1530 * \c this_rdc and best filter is placed to \c mi->interp_filters. In case
1531 * \c reuse_inter_pred flag is set, this function also ouputs
1532 * \c this_mode_pred. Also \c this_early_temp is set if transform can be
1533 * skipped
1534 */
Fyodor Kyslov26473052019-08-26 17:09:30 -07001535static void search_filter_ref(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc,
Marco Paniconi966ec042019-10-08 09:33:18 -07001536 int mi_row, int mi_col, PRED_BUFFER *tmp,
1537 BLOCK_SIZE bsize, int reuse_inter_pred,
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07001538 PRED_BUFFER **this_mode_pred,
Fyodor Kyslove8692412020-05-04 17:47:13 -07001539 int *this_early_term, int use_model_yrd_large) {
Fyodor Kyslov26473052019-08-26 17:09:30 -07001540 AV1_COMMON *const cm = &cpi->common;
1541 MACROBLOCKD *const xd = &x->e_mbd;
Marco Paniconi966ec042019-10-08 09:33:18 -07001542 struct macroblockd_plane *const pd = &xd->plane[0];
Fyodor Kyslov26473052019-08-26 17:09:30 -07001543 MB_MODE_INFO *const mi = xd->mi[0];
Marco Paniconi966ec042019-10-08 09:33:18 -07001544 const int bw = block_size_wide[bsize];
Fyodor Kyslove8692412020-05-04 17:47:13 -07001545 RD_STATS pf_rd_stats[FILTER_SEARCH_SIZE] = { 0 };
Fyodor Kyslov94426692019-08-29 10:41:55 -07001546 TX_SIZE pf_tx_size[FILTER_SEARCH_SIZE] = { 0 };
Marco Paniconi966ec042019-10-08 09:33:18 -07001547 PRED_BUFFER *current_pred = *this_mode_pred;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001548 int best_skip = 0;
1549 int best_early_term = 0;
1550 int64_t best_cost = INT64_MAX;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001551 int best_filter_index = -1;
Fyodor Kyslov94426692019-08-29 10:41:55 -07001552 InterpFilter filters[FILTER_SEARCH_SIZE] = { EIGHTTAP_REGULAR,
1553 EIGHTTAP_SMOOTH };
Hui Su22100852020-11-06 11:11:27 -08001554 for (int i = 0; i < FILTER_SEARCH_SIZE; ++i) {
Fyodor Kyslov26473052019-08-26 17:09:30 -07001555 int64_t cost;
1556 InterpFilter filter = filters[i];
1557 mi->interp_filters = av1_broadcast_interp_filter(filter);
Fyodor Kyslov4dfc85b2020-02-04 17:47:38 -08001558 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
Fyodor Kyslov26473052019-08-26 17:09:30 -07001559 if (use_model_yrd_large)
Fyodor Kyslove8692412020-05-04 17:47:13 -07001560 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07001561 &pf_rd_stats[i], this_early_term, 1);
Fyodor Kyslov26473052019-08-26 17:09:30 -07001562 else
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07001563 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[i], 1);
Hui Su22100852020-11-06 11:11:27 -08001564 pf_rd_stats[i].rate += av1_get_switchable_rate(
1565 x, xd, cm->features.interp_filter, cm->seq_params.enable_dual_filter);
Fyodor Kyslove8692412020-05-04 17:47:13 -07001566 cost = RDCOST(x->rdmult, pf_rd_stats[i].rate, pf_rd_stats[i].dist);
Fyodor Kyslov26473052019-08-26 17:09:30 -07001567 pf_tx_size[i] = mi->tx_size;
1568 if (cost < best_cost) {
1569 best_filter_index = i;
1570 best_cost = cost;
Fyodor Kyslove8692412020-05-04 17:47:13 -07001571 best_skip = pf_rd_stats[i].skip_txfm;
Fyodor Kyslov26473052019-08-26 17:09:30 -07001572 best_early_term = *this_early_term;
Marco Paniconi966ec042019-10-08 09:33:18 -07001573 if (reuse_inter_pred) {
1574 if (*this_mode_pred != current_pred) {
1575 free_pred_buffer(*this_mode_pred);
1576 *this_mode_pred = current_pred;
1577 }
1578 current_pred = &tmp[get_pred_buffer(tmp, 3)];
1579 pd->dst.buf = current_pred->data;
1580 pd->dst.stride = bw;
1581 }
Fyodor Kyslov26473052019-08-26 17:09:30 -07001582 }
1583 }
Fyodor Kyslov94426692019-08-29 10:41:55 -07001584 assert(best_filter_index >= 0 && best_filter_index < FILTER_SEARCH_SIZE);
Marco Paniconi966ec042019-10-08 09:33:18 -07001585 if (reuse_inter_pred && *this_mode_pred != current_pred)
1586 free_pred_buffer(current_pred);
Fyodor Kyslov26473052019-08-26 17:09:30 -07001587
1588 mi->interp_filters = av1_broadcast_interp_filter(filters[best_filter_index]);
1589 mi->tx_size = pf_tx_size[best_filter_index];
Fyodor Kyslove8692412020-05-04 17:47:13 -07001590 this_rdc->rate = pf_rd_stats[best_filter_index].rate;
1591 this_rdc->dist = pf_rd_stats[best_filter_index].dist;
Fyodor Kyslove8692412020-05-04 17:47:13 -07001592 this_rdc->sse = pf_rd_stats[best_filter_index].sse;
chiyotsai8c004e12020-04-17 15:52:08 -07001593 this_rdc->skip_txfm = (best_skip || best_early_term);
Fyodor Kyslov26473052019-08-26 17:09:30 -07001594 *this_early_term = best_early_term;
Marco Paniconi966ec042019-10-08 09:33:18 -07001595 if (reuse_inter_pred) {
1596 pd->dst.buf = (*this_mode_pred)->data;
1597 pd->dst.stride = (*this_mode_pred)->stride;
Marco Paniconib43603d2020-02-28 10:21:43 -08001598 } else if (best_filter_index < FILTER_SEARCH_SIZE - 1) {
Fyodor Kyslov4dfc85b2020-02-04 17:47:38 -08001599 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
Marco Paniconiecf1cc62019-10-07 19:17:37 -07001600 }
Fyodor Kyslov26473052019-08-26 17:09:30 -07001601}
1602
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07001603#define COLLECT_PICK_MODE_STAT 0
1604
1605#if COLLECT_PICK_MODE_STAT
1606typedef struct _mode_search_stat {
1607 int32_t num_blocks[BLOCK_SIZES];
1608 int64_t avg_block_times[BLOCK_SIZES];
1609 int32_t num_searches[BLOCK_SIZES][MB_MODE_COUNT];
1610 int32_t num_nonskipped_searches[BLOCK_SIZES][MB_MODE_COUNT];
1611 int64_t search_times[BLOCK_SIZES][MB_MODE_COUNT];
1612 int64_t nonskipped_search_times[BLOCK_SIZES][MB_MODE_COUNT];
1613 struct aom_usec_timer timer1;
1614 struct aom_usec_timer timer2;
1615} mode_search_stat;
1616#endif // COLLECT_PICK_MODE_STAT
1617
Marco Paniconi13460a22019-10-15 15:54:47 -07001618static void compute_intra_yprediction(const AV1_COMMON *cm,
1619 PREDICTION_MODE mode, BLOCK_SIZE bsize,
1620 MACROBLOCK *x, MACROBLOCKD *xd) {
1621 struct macroblockd_plane *const pd = &xd->plane[0];
1622 struct macroblock_plane *const p = &x->plane[0];
1623 uint8_t *const src_buf_base = p->src.buf;
1624 uint8_t *const dst_buf_base = pd->dst.buf;
1625 const int src_stride = p->src.stride;
1626 const int dst_stride = pd->dst.stride;
1627 int plane = 0;
1628 int row, col;
1629 // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
1630 // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
1631 // transform size varies per plane, look it up in a common way.
1632 const TX_SIZE tx_size = max_txsize_lookup[bsize];
1633 const BLOCK_SIZE plane_bsize =
1634 get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
1635 // If mb_to_right_edge is < 0 we are in a situation in which
1636 // the current block size extends into the UMV and we won't
1637 // visit the sub blocks that are wholly within the UMV.
1638 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1639 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
1640 // Keep track of the row and column of the blocks we use so that we know
1641 // if we are in the unrestricted motion border.
1642 for (row = 0; row < max_blocks_high; row += (1 << tx_size)) {
1643 // Skip visiting the sub blocks that are wholly within the UMV.
1644 for (col = 0; col < max_blocks_wide; col += (1 << tx_size)) {
1645 p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)];
1646 pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)];
1647 av1_predict_intra_block(cm, xd, block_size_wide[bsize],
1648 block_size_high[bsize], tx_size, mode, 0, 0,
1649 FILTER_INTRA_MODES, pd->dst.buf, dst_stride,
1650 pd->dst.buf, dst_stride, 0, 0, plane);
1651 }
1652 }
1653 p->src.buf = src_buf_base;
1654 pd->dst.buf = dst_buf_base;
1655}
1656
chiyotsai73ddf442020-06-01 15:42:23 -07001657void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
1658 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001659 AV1_COMMON *const cm = &cpi->common;
1660 MACROBLOCKD *const xd = &x->e_mbd;
1661 MB_MODE_INFO *const mi = xd->mi[0];
1662 RD_STATS this_rdc, best_rdc;
1663 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
chiyotsaia36d9002020-04-29 16:48:21 -07001664 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001665 const TX_SIZE intra_tx_size =
1666 AOMMIN(max_txsize_lookup[bsize],
chiyotsaia36d9002020-04-29 16:48:21 -07001667 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001668 int *bmode_costs;
Marco Paniconif38266b2021-03-07 22:43:23 -08001669 PREDICTION_MODE best_mode = DC_PRED;
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001670 const MB_MODE_INFO *above_mi = xd->above_mbmi;
1671 const MB_MODE_INFO *left_mi = xd->left_mbmi;
1672 const PREDICTION_MODE A = av1_above_block_mode(above_mi);
1673 const PREDICTION_MODE L = av1_left_block_mode(left_mi);
chiyotsai9a06d182020-05-01 17:12:12 -07001674 bmode_costs = x->mode_costs.y_mode_costs[A][L];
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001675
1676 av1_invalid_rd_stats(&best_rdc);
1677 av1_invalid_rd_stats(&this_rdc);
1678
Jerome Jiangcde6f4a2020-01-06 11:14:58 -08001679 init_mbmi(mi, DC_PRED, INTRA_FRAME, NONE_FRAME, cm);
1680 mi->mv[0].as_int = mi->mv[1].as_int = INVALID_MV;
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001681
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001682 // Change the limit of this loop to add other intra prediction
1683 // mode tests.
1684 for (int i = 0; i < 4; ++i) {
1685 PREDICTION_MODE this_mode = intra_mode_list[i];
1686 this_rdc.dist = this_rdc.rate = 0;
1687 args.mode = this_mode;
1688 args.skippable = 1;
1689 args.rdc = &this_rdc;
1690 mi->tx_size = intra_tx_size;
Marco Paniconif38266b2021-03-07 22:43:23 -08001691 mi->mode = this_mode;
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001692 av1_foreach_transformed_block_in_plane(xd, bsize, 0, estimate_block_intra,
1693 &args);
Deepa K Gc990ca22021-02-22 17:11:37 +05301694 const int skip_ctx = av1_get_skip_txfm_context(xd);
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001695 if (args.skippable) {
Deepa K Gc990ca22021-02-22 17:11:37 +05301696 this_rdc.rate = x->mode_costs.skip_txfm_cost[skip_ctx][1];
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001697 } else {
Deepa K Gc990ca22021-02-22 17:11:37 +05301698 this_rdc.rate += x->mode_costs.skip_txfm_cost[skip_ctx][0];
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001699 }
1700 this_rdc.rate += bmode_costs[this_mode];
1701 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1702
1703 if (this_rdc.rdcost < best_rdc.rdcost) {
1704 best_rdc = this_rdc;
Marco Paniconif38266b2021-03-07 22:43:23 -08001705 best_mode = this_mode;
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001706 }
1707 }
1708
Marco Paniconif38266b2021-03-07 22:43:23 -08001709 mi->mode = best_mode;
1710 // Keep DC for UV since mode test is based on Y channel only.
1711 mi->uv_mode = DC_PRED;
Jerome Jiangf7e6b902019-11-25 15:28:23 -08001712 *rd_cost = best_rdc;
1713
1714#if CONFIG_INTERNAL_STATS
1715 store_coding_context(x, ctx, mi->mode);
1716#else
1717 store_coding_context(x, ctx);
1718#endif // CONFIG_INTERNAL_STATS
1719}
1720
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001721static AOM_INLINE int is_same_gf_and_last_scale(AV1_COMMON *cm) {
1722 struct scale_factors *const sf_last = get_ref_scale_factors(cm, LAST_FRAME);
1723 struct scale_factors *const sf_golden =
1724 get_ref_scale_factors(cm, GOLDEN_FRAME);
1725 return ((sf_last->x_scale_fp == sf_golden->x_scale_fp) &&
1726 (sf_last->y_scale_fp == sf_golden->y_scale_fp));
1727}
1728
1729static AOM_INLINE void get_ref_frame_use_mask(AV1_COMP *cpi, MACROBLOCK *x,
1730 MB_MODE_INFO *mi, int mi_row,
1731 int mi_col, int bsize,
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07001732 int gf_temporal_ref,
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001733 int use_ref_frame[],
1734 int *force_skip_low_temp_var) {
1735 AV1_COMMON *const cm = &cpi->common;
1736 const struct segmentation *const seg = &cm->seg;
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001737 const int is_small_sb = (cm->seq_params.sb_size == BLOCK_64X64);
1738
Marco Paniconibc4a75d2020-09-07 22:03:45 -07001739 // For SVC the usage of alt_ref is determined by the ref_frame_flags.
1740 int use_alt_ref_frame = cpi->use_svc || cpi->sf.rt_sf.use_nonrd_altref_frame;
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001741 int use_golden_ref_frame = 1;
1742
1743 use_ref_frame[LAST_FRAME] = 1; // we never skip LAST
1744
1745 if (cpi->rc.frames_since_golden == 0 && gf_temporal_ref) {
1746 use_golden_ref_frame = 0;
1747 }
1748
1749 if (cpi->sf.rt_sf.short_circuit_low_temp_var &&
1750 x->nonrd_prune_ref_frame_search) {
1751 if (is_small_sb)
1752 *force_skip_low_temp_var = get_force_skip_low_temp_var_small_sb(
chiyotsai68eefbe2020-05-01 15:07:58 -07001753 &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001754 else
1755 *force_skip_low_temp_var = get_force_skip_low_temp_var(
chiyotsai68eefbe2020-05-01 15:07:58 -07001756 &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07001757 // If force_skip_low_temp_var is set, skip golden reference.
1758 if (*force_skip_low_temp_var) {
1759 use_golden_ref_frame = 0;
1760 use_alt_ref_frame = 0;
1761 }
1762 }
1763
1764 if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
1765 get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) {
1766 use_golden_ref_frame = 1;
1767 use_alt_ref_frame = 0;
1768 }
1769
1770 use_alt_ref_frame =
1771 cpi->ref_frame_flags & AOM_ALT_FLAG ? use_alt_ref_frame : 0;
1772 use_golden_ref_frame =
1773 cpi->ref_frame_flags & AOM_GOLD_FLAG ? use_golden_ref_frame : 0;
1774
1775 use_ref_frame[ALTREF_FRAME] = use_alt_ref_frame;
1776 use_ref_frame[GOLDEN_FRAME] = use_golden_ref_frame;
1777}
1778
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001779/*!\brief Estimates best intra mode for inter mode search
1780 *
Fyodor Kyslov2a3768e2020-07-20 14:38:05 -07001781 * \ingroup nonrd_mode_search
Fyodor Kyslov487b0132020-07-14 17:23:17 -07001782 * \callgraph
1783 * \callergraph
1784 *
1785 * Using heuristics based on best inter mode, block size, and other decides
1786 * whether to check intra modes. If so, estimates and selects best intra mode
1787 * from the reduced set of intra modes (max 4 intra modes checked)
1788 *
1789 * \param[in] cpi Top-level encoder structure
1790 * \param[in] x Pointer to structure holding all the
1791 * data for the current macroblock
1792 * \param[in] bsize Current block size
1793 * \param[in] use_modeled_non_rd_cost Flag, indicating usage of curvfit
1794 * model for RD cost
1795 * \param[in] best_early_term Flag, indicating that TX for the
1796 * best inter mode was skipped
1797 * \param[in] ref_cost_intra Cost of signalling intra mode
1798 * \param[in] reuse_prediction Flag, indicating prediction re-use
1799 * \param[in] orig_dst Original destination buffer
1800 * \param[in] tmp_buffers Pointer to a temporary buffers for
1801 * prediction re-use
1802 * \param[out] this_mode_pred Pointer to store prediction buffer
1803 * for prediction re-use
1804 * \param[in] best_rdc Pointer to RD cost for the best
1805 * selected intra mode
1806 * \param[in] best_pickmode Pointer to a structure containing
1807 * best mode picked so far
1808 *
1809 * \return Nothing is returned. Instead, calculated RD cost is placed to
1810 * \c best_rdc and best selected mode is placed to \c best_pickmode
1811 */
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001812static void estimate_intra_mode(
1813 AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int use_modeled_non_rd_cost,
1814 int best_early_term, unsigned int ref_cost_intra, int reuse_prediction,
1815 struct buf_2d *orig_dst, PRED_BUFFER *tmp_buffers,
1816 PRED_BUFFER **this_mode_pred, RD_STATS *best_rdc,
1817 BEST_PICKMODE *best_pickmode) {
1818 AV1_COMMON *const cm = &cpi->common;
1819 MACROBLOCKD *const xd = &x->e_mbd;
1820 MB_MODE_INFO *const mi = xd->mi[0];
chiyotsaia36d9002020-04-29 16:48:21 -07001821 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001822 const unsigned char segment_id = mi->segment_id;
1823 const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize];
1824 const int *const rd_thresh_freq_fact = x->thresh_freq_fact[bsize];
1825 const int mi_row = xd->mi_row;
1826 const int mi_col = xd->mi_col;
1827 struct macroblockd_plane *const pd = &xd->plane[0];
1828
1829 const CommonQuantParams *quant_params = &cm->quant_params;
1830
1831 RD_STATS this_rdc;
1832
1833 int intra_cost_penalty = av1_get_intra_cost_penalty(
1834 quant_params->base_qindex, quant_params->y_dc_delta_q,
1835 cm->seq_params.bit_depth);
1836 int64_t inter_mode_thresh = RDCOST(x->rdmult, intra_cost_penalty, 0);
1837 int perform_intra_pred = cpi->sf.rt_sf.check_intra_pred_nonrd;
Marco Paniconi84cb8b32021-01-19 14:02:19 -08001838 // For spatial enhancemanent layer: turn off intra prediction if the
1839 // previous spatial layer as golden ref is not chosen as best reference.
1840 // only do this for temporal enhancement layer and on non-key frames.
1841 if (cpi->svc.spatial_layer_id > 0 &&
1842 best_pickmode->best_ref_frame != GOLDEN_FRAME &&
1843 cpi->svc.temporal_layer_id > 0 &&
1844 !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame)
1845 perform_intra_pred = 0;
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001846
1847 int do_early_exit_rdthresh = 1;
1848
1849 uint32_t spatial_var_thresh = 50;
1850 int motion_thresh = 32;
1851 // Adjust thresholds to make intra mode likely tested if the other
Marco Paniconibc4a75d2020-09-07 22:03:45 -07001852 // references (golden, alt) are skipped/not checked. For now always
1853 // adjust for svc mode.
1854 if (cpi->use_svc || (cpi->sf.rt_sf.use_nonrd_altref_frame == 0 &&
1855 cpi->sf.rt_sf.nonrd_prune_ref_frame_search > 0)) {
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001856 spatial_var_thresh = 150;
1857 motion_thresh = 0;
1858 }
1859
1860 // Some adjustments to checking intra mode based on source variance.
1861 if (x->source_variance < spatial_var_thresh) {
1862 // If the best inter mode is large motion or non-LAST ref reduce intra cost
1863 // penalty, so intra mode is more likely tested.
1864 if (best_pickmode->best_ref_frame != LAST_FRAME ||
1865 abs(mi->mv[0].as_mv.row) >= motion_thresh ||
1866 abs(mi->mv[0].as_mv.col) >= motion_thresh) {
1867 intra_cost_penalty = intra_cost_penalty >> 2;
1868 inter_mode_thresh = RDCOST(x->rdmult, intra_cost_penalty, 0);
1869 do_early_exit_rdthresh = 0;
1870 }
1871 // For big blocks worth checking intra (since only DC will be checked),
1872 // even if best_early_term is set.
1873 if (bsize >= BLOCK_32X32) best_early_term = 0;
1874 } else if (cpi->sf.rt_sf.source_metrics_sb_nonrd &&
Marco Paniconi988b34a2020-11-09 12:41:13 -08001875 x->content_state_sb.source_sad == kLowSad) {
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001876 perform_intra_pred = 0;
1877 }
1878
Fyodor Kyslove0be17e2020-05-13 14:20:50 -07001879 if (cpi->sf.rt_sf.skip_intra_pred_if_tx_skip && best_rdc->skip_txfm &&
1880 best_pickmode->best_mode_initial_skip_flag) {
1881 perform_intra_pred = 0;
1882 }
1883
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001884 if (!(best_rdc->rdcost == INT64_MAX ||
1885 (perform_intra_pred && !best_early_term &&
1886 best_rdc->rdcost > inter_mode_thresh &&
1887 bsize <= cpi->sf.part_sf.max_intra_bsize))) {
1888 return;
1889 }
1890
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001891 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
chiyotsaia36d9002020-04-29 16:48:21 -07001892 TX_SIZE intra_tx_size = AOMMIN(
1893 AOMMIN(max_txsize_lookup[bsize],
1894 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]),
1895 TX_16X16);
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001896
1897 PRED_BUFFER *const best_pred = best_pickmode->best_pred;
1898 if (reuse_prediction && best_pred != NULL) {
1899 const int bh = block_size_high[bsize];
1900 const int bw = block_size_wide[bsize];
1901 if (best_pred->data == orig_dst->buf) {
1902 *this_mode_pred = &tmp_buffers[get_pred_buffer(tmp_buffers, 3)];
1903 aom_convolve_copy(best_pred->data, best_pred->stride,
1904 (*this_mode_pred)->data, (*this_mode_pred)->stride, bw,
1905 bh);
1906 best_pickmode->best_pred = *this_mode_pred;
1907 }
1908 }
1909 pd->dst = *orig_dst;
1910
1911 for (int i = 0; i < 4; ++i) {
1912 const PREDICTION_MODE this_mode = intra_mode_list[i];
1913 const THR_MODES mode_index = mode_idx[INTRA_FRAME][mode_offset(this_mode)];
Fyodor Kyslov29430902020-11-18 13:44:30 -08001914 const int64_t mode_rd_thresh = rd_threshes[mode_index];
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001915
Marco Paniconi06affb62020-10-07 16:11:29 -07001916 if (!((1 << this_mode) & cpi->sf.rt_sf.intra_y_mode_bsize_mask_nrd[bsize]))
Fyodor Kyslov2b9242a2020-06-19 11:12:59 -07001917 continue;
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001918
1919 if (rd_less_than_thresh(best_rdc->rdcost, mode_rd_thresh,
1920 rd_thresh_freq_fact[mode_index]) &&
1921 (do_early_exit_rdthresh || this_mode == SMOOTH_PRED)) {
1922 continue;
1923 }
1924 const BLOCK_SIZE uv_bsize = get_plane_block_size(
1925 bsize, xd->plane[1].subsampling_x, xd->plane[1].subsampling_y);
1926
1927 mi->mode = this_mode;
1928 mi->ref_frame[0] = INTRA_FRAME;
1929 mi->ref_frame[1] = NONE_FRAME;
1930
Fyodor Kyslove8692412020-05-04 17:47:13 -07001931 av1_invalid_rd_stats(&this_rdc);
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001932 args.mode = this_mode;
1933 args.skippable = 1;
1934 args.rdc = &this_rdc;
1935 mi->tx_size = intra_tx_size;
1936 compute_intra_yprediction(cm, this_mode, bsize, x, xd);
1937 // Look into selecting tx_size here, based on prediction residual.
1938 if (use_modeled_non_rd_cost)
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07001939 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, 1);
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001940 else
Fyodor Kyslove8692412020-05-04 17:47:13 -07001941 block_yrd(cpi, x, mi_row, mi_col, &this_rdc, &args.skippable, bsize,
1942 mi->tx_size);
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001943 // TODO(kyslov@) Need to account for skippable
1944 if (x->color_sensitivity[0]) {
1945 av1_foreach_transformed_block_in_plane(xd, uv_bsize, 1,
1946 estimate_block_intra, &args);
1947 }
1948 if (x->color_sensitivity[1]) {
1949 av1_foreach_transformed_block_in_plane(xd, uv_bsize, 2,
1950 estimate_block_intra, &args);
1951 }
1952
1953 int mode_cost = 0;
1954 if (av1_is_directional_mode(this_mode) && av1_use_angle_delta(bsize)) {
1955 mode_cost +=
chiyotsai9a06d182020-05-01 17:12:12 -07001956 x->mode_costs.angle_delta_cost[this_mode - V_PRED]
1957 [MAX_ANGLE_DELTA +
1958 mi->angle_delta[PLANE_TYPE_Y]];
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001959 }
1960 if (this_mode == DC_PRED && av1_filter_intra_allowed_bsize(cm, bsize)) {
chiyotsai9a06d182020-05-01 17:12:12 -07001961 mode_cost += x->mode_costs.filter_intra_cost[bsize][0];
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001962 }
1963 this_rdc.rate += ref_cost_intra;
1964 this_rdc.rate += intra_cost_penalty;
1965 this_rdc.rate += mode_cost;
1966 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1967
1968 if (this_rdc.rdcost < best_rdc->rdcost) {
1969 *best_rdc = this_rdc;
1970 best_pickmode->best_mode = this_mode;
Fyodor Kyslove0be17e2020-05-13 14:20:50 -07001971 best_pickmode->best_tx_size = mi->tx_size;
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001972 best_pickmode->best_ref_frame = INTRA_FRAME;
1973 mi->uv_mode = this_mode;
1974 mi->mv[0].as_int = INVALID_MV;
1975 mi->mv[1].as_int = INVALID_MV;
1976 }
1977 }
Fyodor Kyslove0be17e2020-05-13 14:20:50 -07001978 mi->tx_size = best_pickmode->best_tx_size;
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07001979}
1980
chiyotsai7f6e21a2020-06-16 14:52:30 -07001981static AOM_INLINE int is_filter_search_enabled(const AV1_COMP *cpi, int mi_row,
1982 int mi_col, BLOCK_SIZE bsize) {
1983 const AV1_COMMON *const cm = &cpi->common;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001984 int enable_filter_search = 0;
1985
1986 if (cpi->sf.rt_sf.use_nonrd_filter_search) {
1987 enable_filter_search = 1;
1988 if (cpi->sf.interp_sf.cb_pred_filter_search) {
1989 const int bsl = mi_size_wide_log2[bsize];
1990 enable_filter_search =
1991 (((mi_row + mi_col) >> bsl) +
1992 get_chessboard_index(cm->current_frame.frame_number)) &
1993 0x1;
1994 }
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07001995 }
1996 return enable_filter_search;
1997}
1998
1999static AOM_INLINE int skip_mode_by_threshold(
2000 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, int_mv mv,
2001 int frames_since_golden, const int *const rd_threshes,
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08002002 const int *const rd_thresh_freq_fact, int64_t best_cost, int best_skip,
2003 int extra_shift) {
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002004 int skip_this_mode = 0;
2005 const THR_MODES mode_index = mode_idx[ref_frame][INTER_OFFSET(mode)];
Fyodor Kyslov29430902020-11-18 13:44:30 -08002006 int64_t mode_rd_thresh =
2007 best_skip ? ((int64_t)rd_threshes[mode_index]) << (extra_shift + 1)
2008 : ((int64_t)rd_threshes[mode_index]) << extra_shift;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002009
2010 // Increase mode_rd_thresh value for non-LAST for improved encoding
2011 // speed
2012 if (ref_frame != LAST_FRAME) {
2013 mode_rd_thresh = mode_rd_thresh << 1;
2014 if (ref_frame == GOLDEN_FRAME && frames_since_golden > 4)
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08002015 mode_rd_thresh = mode_rd_thresh << (extra_shift + 1);
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002016 }
2017
2018 if (rd_less_than_thresh(best_cost, mode_rd_thresh,
2019 rd_thresh_freq_fact[mode_index]))
2020 if (mv.as_int != 0) skip_this_mode = 1;
2021
2022 return skip_this_mode;
2023}
2024
Marco Paniconi988b34a2020-11-09 12:41:13 -08002025static AOM_INLINE int skip_mode_by_low_temp(
2026 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
2027 CONTENT_STATE_SB content_state_sb, int_mv mv, int force_skip_low_temp_var) {
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002028 // Skip non-zeromv mode search for non-LAST frame if force_skip_low_temp_var
2029 // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped
2030 // later.
2031 if (force_skip_low_temp_var && ref_frame != LAST_FRAME && mv.as_int != 0) {
2032 return 1;
2033 }
2034
Marco Paniconi988b34a2020-11-09 12:41:13 -08002035 if (content_state_sb.source_sad != kHighSad && bsize >= BLOCK_64X64 &&
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002036 force_skip_low_temp_var && mode == NEWMV) {
2037 return 1;
2038 }
2039 return 0;
2040}
2041
2042static AOM_INLINE int skip_mode_by_bsize_and_ref_frame(
2043 PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
Fyodor Kyslov29430902020-11-18 13:44:30 -08002044 int extra_prune, unsigned int sse_zeromv_norm, int more_prune) {
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002045 const unsigned int thresh_skip_golden = 500;
2046
2047 if (ref_frame != LAST_FRAME && sse_zeromv_norm < thresh_skip_golden &&
2048 mode == NEWMV)
2049 return 1;
2050
2051 if (bsize == BLOCK_128X128 && mode == NEWMV) return 1;
2052
2053 // Skip testing non-LAST if this flag is set.
2054 if (extra_prune) {
2055 if (extra_prune > 1 && ref_frame != LAST_FRAME &&
2056 (bsize > BLOCK_64X64 || (bsize > BLOCK_16X16 && mode == NEWMV)))
2057 return 1;
2058
2059 if (ref_frame != LAST_FRAME && mode == NEARMV) return 1;
Fyodor Kyslov29430902020-11-18 13:44:30 -08002060
2061 if (more_prune && bsize >= BLOCK_32X32 && mode == NEARMV) return 1;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002062 }
2063 return 0;
2064}
2065
Fyodor Kyslov915b9b82019-11-11 11:53:03 -08002066void av1_nonrd_pick_inter_mode_sb(AV1_COMP *cpi, TileDataEnc *tile_data,
Hui Sub94cd5e2019-11-06 12:05:47 -08002067 MACROBLOCK *x, RD_STATS *rd_cost,
Marco Paniconi11067db2020-05-18 11:11:42 -07002068 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
kyslove3c05a82019-03-28 11:06:09 -07002069 AV1_COMMON *const cm = &cpi->common;
Marco Paniconi84cb8b32021-01-19 14:02:19 -08002070 SVC *const svc = &cpi->svc;
kyslove3c05a82019-03-28 11:06:09 -07002071 MACROBLOCKD *const xd = &x->e_mbd;
2072 MB_MODE_INFO *const mi = xd->mi[0];
2073 struct macroblockd_plane *const pd = &xd->plane[0];
2074
2075 BEST_PICKMODE best_pickmode;
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002076#if COLLECT_PICK_MODE_STAT
2077 static mode_search_stat ms_stat;
2078#endif
kyslove3c05a82019-03-28 11:06:09 -07002079 MV_REFERENCE_FRAME ref_frame;
kyslove3c05a82019-03-28 11:06:09 -07002080 int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES];
2081 uint8_t mode_checked[MB_MODE_COUNT][REF_FRAMES];
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07002082 struct buf_2d yv12_mb[REF_FRAMES][MAX_MB_PLANE];
kyslove3c05a82019-03-28 11:06:09 -07002083 RD_STATS this_rdc, best_rdc;
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07002084 const unsigned char segment_id = mi->segment_id;
2085 const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize];
Ravi Chaudharyb61cdea2019-07-05 15:01:20 +05302086 const int *const rd_thresh_freq_fact = x->thresh_freq_fact[bsize];
Fyodor Kyslov9fa800d2020-05-05 13:56:35 -07002087 const InterpFilter filter_ref = cm->features.interp_filter;
kyslove3c05a82019-03-28 11:06:09 -07002088 int best_early_term = 0;
Jerome Jiang037f5d22019-05-02 19:32:42 -07002089 unsigned int ref_costs_single[REF_FRAMES],
2090 ref_costs_comp[REF_FRAMES][REF_FRAMES];
kyslove3c05a82019-03-28 11:06:09 -07002091 int force_skip_low_temp_var = 0;
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07002092 int use_ref_frame_mask[REF_FRAMES] = { 0 };
Fyodor Kyslov84abaea2019-06-25 14:44:14 -07002093 unsigned int sse_zeromv_norm = UINT_MAX;
Marco Paniconi5a2e5492020-12-03 10:22:47 -08002094 // Use mode set that includes zeromv (via globalmv) for speed >= 9 for
2095 // content with low motion.
2096 int use_zeromv =
2097 ((cpi->oxcf.speed >= 9 && cpi->rc.avg_frame_low_motion > 70) ||
2098 cpi->sf.rt_sf.nonrd_agressive_skip);
2099 const int num_inter_modes =
2100 use_zeromv ? NUM_INTER_MODES_REDUCED : NUM_INTER_MODES_RT;
2101 const REF_MODE *const ref_mode_set =
2102 use_zeromv ? ref_mode_set_reduced : ref_mode_set_rt;
kyslove3c05a82019-03-28 11:06:09 -07002103 PRED_BUFFER tmp[4];
Marco Paniconi966ec042019-10-08 09:33:18 -07002104 DECLARE_ALIGNED(16, uint8_t, pred_buf[3 * 128 * 128]);
kyslove3c05a82019-03-28 11:06:09 -07002105 PRED_BUFFER *this_mode_pred = NULL;
Fyodor Kyslovfb9f22c2021-02-22 17:35:50 -08002106 const int reuse_inter_pred = cpi->sf.rt_sf.reuse_inter_pred_nonrd &&
2107 cm->seq_params.bit_depth == AOM_BITS_8;
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08002108
kyslove3c05a82019-03-28 11:06:09 -07002109 const int bh = block_size_high[bsize];
2110 const int bw = block_size_wide[bsize];
2111 const int pixels_in_block = bh * bw;
2112 struct buf_2d orig_dst = pd->dst;
Urvang Joshi17814622020-03-27 17:26:17 -07002113 const CommonQuantParams *quant_params = &cm->quant_params;
chiyotsaia36d9002020-04-29 16:48:21 -07002114 const TxfmSearchParams *txfm_params = &x->txfm_search_params;
chiyotsai4c1e5c62020-04-30 17:54:14 -07002115 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002116#if COLLECT_PICK_MODE_STAT
2117 aom_usec_timer_start(&ms_stat.timer2);
2118#endif
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002119 const InterpFilter default_interp_filter = EIGHTTAP_REGULAR;
Marco Paniconib3565a22020-03-05 15:29:08 -08002120 int64_t thresh_sad_pred = INT64_MAX;
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002121 const int mi_row = xd->mi_row;
2122 const int mi_col = xd->mi_col;
Marco Paniconi84cb8b32021-01-19 14:02:19 -08002123 int svc_mv_col = 0;
2124 int svc_mv_row = 0;
2125 int force_mv_inter_layer = 0;
Marco Paniconi0fac7ff2020-08-14 11:02:28 -07002126 int use_modeled_non_rd_cost = 0;
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08002127#if CONFIG_AV1_TEMPORAL_DENOISING
2128 const int denoise_recheck_zeromv = 1;
2129 AV1_PICKMODE_CTX_DEN ctx_den;
2130 int64_t zero_last_cost_orig = INT64_MAX;
2131 int denoise_svc_pickmode = 1;
2132 const int resize_pending =
2133 (cpi->resize_pending_params.width && cpi->resize_pending_params.height &&
2134 (cpi->common.width != cpi->resize_pending_params.width ||
2135 cpi->common.height != cpi->resize_pending_params.height));
2136
2137#endif
kyslov82449d12019-05-02 13:36:50 -07002138
kyslove3c05a82019-03-28 11:06:09 -07002139 init_best_pickmode(&best_pickmode);
2140
chiyotsai9a06d182020-05-01 17:12:12 -07002141 const ModeCosts *mode_costs = &x->mode_costs;
2142
2143 estimate_single_ref_frame_costs(cm, xd, mode_costs, segment_id,
2144 ref_costs_single);
chiyotsai56681b52019-12-16 11:15:25 -08002145 if (cpi->sf.rt_sf.use_comp_ref_nonrd)
chiyotsai9a06d182020-05-01 17:12:12 -07002146 estimate_comp_ref_frame_costs(cm, xd, mode_costs, segment_id,
2147 ref_costs_comp);
kyslove3c05a82019-03-28 11:06:09 -07002148
2149 memset(&mode_checked[0][0], 0, MB_MODE_COUNT * REF_FRAMES);
2150 if (reuse_inter_pred) {
Jerome Jiangc480d822019-05-01 18:30:37 -07002151 for (int i = 0; i < 3; i++) {
kyslove3c05a82019-03-28 11:06:09 -07002152 tmp[i].data = &pred_buf[pixels_in_block * i];
2153 tmp[i].stride = bw;
2154 tmp[i].in_use = 0;
2155 }
2156 tmp[3].data = pd->dst.buf;
2157 tmp[3].stride = pd->dst.stride;
2158 tmp[3].in_use = 0;
2159 }
2160
chiyotsai4c1e5c62020-04-30 17:54:14 -07002161 txfm_info->skip_txfm = 0;
kyslove3c05a82019-03-28 11:06:09 -07002162
kyslove3c05a82019-03-28 11:06:09 -07002163 // initialize mode decisions
2164 av1_invalid_rd_stats(&best_rdc);
kyslovad34be72019-05-03 14:04:02 -07002165 av1_invalid_rd_stats(&this_rdc);
kyslove3c05a82019-03-28 11:06:09 -07002166 av1_invalid_rd_stats(rd_cost);
chiyotsai0f5cd052020-08-27 14:37:44 -07002167 mi->bsize = bsize;
kyslove3c05a82019-03-28 11:06:09 -07002168 mi->ref_frame[0] = NONE_FRAME;
2169 mi->ref_frame[1] = NONE_FRAME;
2170
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08002171#if CONFIG_AV1_TEMPORAL_DENOISING
2172 if (cpi->oxcf.noise_sensitivity > 0) {
2173 // if (cpi->use_svc) denoise_svc_pickmode = av1_denoise_svc_non_key(cpi);
2174 if (cpi->denoiser.denoising_level > kDenLowLow && denoise_svc_pickmode)
2175 av1_denoiser_reset_frame_stats(ctx);
2176 }
2177#endif
2178
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07002179 const int gf_temporal_ref = is_same_gf_and_last_scale(cm);
2180
Marco Paniconi84cb8b32021-01-19 14:02:19 -08002181 // If the lower spatial layer uses an averaging filter for downsampling
2182 // (phase = 8), the target decimated pixel is shifted by (1/2, 1/2) relative
2183 // to source, so use subpel motion vector to compensate. The nonzero motion
2184 // is half pixel shifted to left and top, so (-4, -4). This has more effect
2185 // on higher resolutins, so condition it on that for now.
2186 if (cpi->use_svc && svc->spatial_layer_id > 0 &&
2187 svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 &&
2188 cm->width * cm->height > 640 * 480) {
2189 svc_mv_col = -4;
2190 svc_mv_row = -4;
2191 }
2192
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07002193 get_ref_frame_use_mask(cpi, x, mi, mi_row, mi_col, bsize, gf_temporal_ref,
2194 use_ref_frame_mask, &force_skip_low_temp_var);
kyslove3c05a82019-03-28 11:06:09 -07002195
Jerome Jiangc480d822019-05-01 18:30:37 -07002196 for (MV_REFERENCE_FRAME ref_frame_iter = LAST_FRAME;
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07002197 ref_frame_iter <= ALTREF_FRAME; ++ref_frame_iter) {
2198 if (use_ref_frame_mask[ref_frame_iter]) {
Fyodor Kyslov9fa800d2020-05-05 13:56:35 -07002199 find_predictors(cpi, x, ref_frame_iter, frame_mv, tile_data, yv12_mb,
2200 bsize, force_skip_low_temp_var);
kyslove3c05a82019-03-28 11:06:09 -07002201 }
2202 }
Marco Paniconib3565a22020-03-05 15:29:08 -08002203
2204 thresh_sad_pred = ((int64_t)x->pred_mv_sad[LAST_FRAME]) << 1;
2205 // Increase threshold for less agressive pruning.
2206 if (cpi->sf.rt_sf.nonrd_prune_ref_frame_search == 1)
2207 thresh_sad_pred += (x->pred_mv_sad[LAST_FRAME] >> 2);
2208
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -07002209 const int large_block = bsize >= BLOCK_32X32;
2210 const int use_model_yrd_large =
Vishesh39e74092020-06-16 17:13:48 +05302211 cpi->oxcf.rc_cfg.mode == AOM_CBR && large_block &&
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -07002212 !cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) &&
Fyodor Kyslovc7825d02020-03-31 15:04:01 -07002213 quant_params->base_qindex && cm->seq_params.bit_depth == 8;
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002214
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002215 const int enable_filter_search =
chiyotsai7f6e21a2020-06-16 14:52:30 -07002216 is_filter_search_enabled(cpi, mi_row, mi_col, bsize);
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002217
2218 // TODO(marpan): Look into reducing these conditions. For now constrain
2219 // it to avoid significant bdrate loss.
Marco Paniconi0fac7ff2020-08-14 11:02:28 -07002220 if (cpi->sf.rt_sf.use_modeled_non_rd_cost) {
2221 if (cpi->svc.non_reference_frame)
2222 use_modeled_non_rd_cost = 1;
2223 else if (cpi->svc.number_temporal_layers > 1 &&
2224 cpi->svc.temporal_layer_id == 0)
2225 use_modeled_non_rd_cost = 0;
2226 else
2227 use_modeled_non_rd_cost =
2228 (quant_params->base_qindex > 120 && x->source_variance > 100 &&
Marco Paniconi988b34a2020-11-09 12:41:13 -08002229 bsize <= BLOCK_16X16 && !x->content_state_sb.lighting_change &&
2230 x->content_state_sb.source_sad != kHighSad);
Marco Paniconi0fac7ff2020-08-14 11:02:28 -07002231 }
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002232
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002233#if COLLECT_PICK_MODE_STAT
2234 ms_stat.num_blocks[bsize]++;
2235#endif
Fyodor Kyslovad32ace2020-01-08 14:58:21 -08002236 init_mbmi(mi, DC_PRED, NONE_FRAME, NONE_FRAME, cm);
chiyotsaia36d9002020-04-29 16:48:21 -07002237 mi->tx_size = AOMMIN(
2238 AOMMIN(max_txsize_lookup[bsize],
2239 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]),
2240 TX_16X16);
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002241
Jerome Jiangc480d822019-05-01 18:30:37 -07002242 for (int idx = 0; idx < num_inter_modes; ++idx) {
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07002243 const struct segmentation *const seg = &cm->seg;
2244
kyslove3c05a82019-03-28 11:06:09 -07002245 int rate_mv = 0;
kyslove3c05a82019-03-28 11:06:09 -07002246 int is_skippable;
2247 int this_early_term = 0;
kyslove3c05a82019-03-28 11:06:09 -07002248 int skip_this_mv = 0;
kyslove3c05a82019-03-28 11:06:09 -07002249 PREDICTION_MODE this_mode;
chiyotsai0b90c412020-09-29 14:48:16 -07002250 MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
Fyodor Kyslove0be17e2020-05-13 14:20:50 -07002251 RD_STATS nonskip_rdc;
2252 av1_invalid_rd_stats(&nonskip_rdc);
kyslove3c05a82019-03-28 11:06:09 -07002253
2254 this_mode = ref_mode_set[idx].pred_mode;
2255 ref_frame = ref_mode_set[idx].ref_frame;
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002256
2257#if COLLECT_PICK_MODE_STAT
2258 aom_usec_timer_start(&ms_stat.timer1);
2259 ms_stat.num_searches[bsize][this_mode]++;
2260#endif
Fyodor Kyslovad32ace2020-01-08 14:58:21 -08002261 mi->mode = this_mode;
2262 mi->ref_frame[0] = ref_frame;
kyslove3c05a82019-03-28 11:06:09 -07002263
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07002264 if (!use_ref_frame_mask[ref_frame]) continue;
kyslove3c05a82019-03-28 11:06:09 -07002265
Marco Paniconi84cb8b32021-01-19 14:02:19 -08002266 force_mv_inter_layer = 0;
2267 if (cpi->use_svc && svc->spatial_layer_id > 0 &&
2268 ((ref_frame == LAST_FRAME && svc->skip_mvsearch_last) ||
2269 (ref_frame == GOLDEN_FRAME && svc->skip_mvsearch_gf))) {
2270 // Only test mode if NEARESTMV/NEARMV is (svc_mv_col, svc_mv_row),
2271 // otherwise set NEWMV to (svc_mv_col, svc_mv_row).
2272 // Skip newmv and filter search.
2273 force_mv_inter_layer = 1;
2274 if (this_mode == NEWMV) {
2275 frame_mv[this_mode][ref_frame].as_mv.col = svc_mv_col;
2276 frame_mv[this_mode][ref_frame].as_mv.row = svc_mv_row;
2277 } else if (frame_mv[this_mode][ref_frame].as_mv.col != svc_mv_col ||
2278 frame_mv[this_mode][ref_frame].as_mv.row != svc_mv_row) {
Marco Paniconiddd56662019-08-28 15:46:56 -07002279 continue;
Marco Paniconi84cb8b32021-01-19 14:02:19 -08002280 }
Marco Paniconiddd56662019-08-28 15:46:56 -07002281 }
2282
kyslove3c05a82019-03-28 11:06:09 -07002283 // If the segment reference frame feature is enabled then do nothing if the
2284 // current ref frame is not allowed.
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07002285 if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
2286 get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
kyslove3c05a82019-03-28 11:06:09 -07002287 continue;
2288
Fyodor Kyslov29430902020-11-18 13:44:30 -08002289 if (skip_mode_by_bsize_and_ref_frame(
2290 this_mode, ref_frame, bsize, x->nonrd_prune_ref_frame_search,
2291 sse_zeromv_norm, cpi->sf.rt_sf.nonrd_agressive_skip))
Fyodor Kyslov84abaea2019-06-25 14:44:14 -07002292 continue;
2293
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002294 if (skip_mode_by_low_temp(this_mode, ref_frame, bsize, x->content_state_sb,
2295 frame_mv[this_mode][ref_frame],
2296 force_skip_low_temp_var))
kyslove3c05a82019-03-28 11:06:09 -07002297 continue;
kyslove3c05a82019-03-28 11:06:09 -07002298
Marco Paniconib3565a22020-03-05 15:29:08 -08002299 // Disable this drop out case if the ref frame segment level feature is
2300 // enabled for this segment. This is to prevent the possibility that we
2301 // end up unable to pick any mode.
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07002302 if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
Marco Paniconib3565a22020-03-05 15:29:08 -08002303 // Check for skipping GOLDEN and ALTREF based pred_mv_sad.
2304 if (cpi->sf.rt_sf.nonrd_prune_ref_frame_search > 0 &&
Marco Paniconid1d46282020-04-02 09:16:14 -07002305 x->pred_mv_sad[ref_frame] != INT_MAX && ref_frame != LAST_FRAME) {
Fyodor Kyslov9fa800d2020-05-05 13:56:35 -07002306 if ((int64_t)(x->pred_mv_sad[ref_frame]) > thresh_sad_pred) continue;
Marco Paniconib3565a22020-03-05 15:29:08 -08002307 }
Marco Paniconib3565a22020-03-05 15:29:08 -08002308 }
2309
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002310 if (skip_mode_by_threshold(
2311 this_mode, ref_frame, frame_mv[this_mode][ref_frame],
2312 cpi->rc.frames_since_golden, rd_threshes, rd_thresh_freq_fact,
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08002313 best_rdc.rdcost, best_pickmode.best_mode_skip_txfm,
2314 (cpi->sf.rt_sf.nonrd_agressive_skip ? 1 : 0)))
Fyodor Kyslove182a9c2020-05-06 14:51:33 -07002315 continue;
2316
kyslove3c05a82019-03-28 11:06:09 -07002317 // Select prediction reference frames.
Jerome Jiangc480d822019-05-01 18:30:37 -07002318 for (int i = 0; i < MAX_MB_PLANE; i++) {
kyslove3c05a82019-03-28 11:06:09 -07002319 xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
2320 }
2321
2322 mi->ref_frame[0] = ref_frame;
Fyodor Kyslovfa2d76f2020-04-24 14:03:50 -07002323 mi->ref_frame[1] = NONE_FRAME;
2324 set_ref_ptrs(cm, xd, ref_frame, NONE_FRAME);
kyslove3c05a82019-03-28 11:06:09 -07002325
Marco Paniconi84cb8b32021-01-19 14:02:19 -08002326 if (this_mode == NEWMV && !force_mv_inter_layer) {
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07002327 if (search_new_mv(cpi, x, frame_mv, ref_frame, gf_temporal_ref, bsize,
2328 mi_row, mi_col, &rate_mv, &best_rdc))
kyslove3c05a82019-03-28 11:06:09 -07002329 continue;
2330 }
2331
Jerome Jiangc480d822019-05-01 18:30:37 -07002332 for (PREDICTION_MODE inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV;
2333 inter_mv_mode++) {
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07002334 if (inter_mv_mode == this_mode) continue;
kyslove3c05a82019-03-28 11:06:09 -07002335 if (mode_checked[inter_mv_mode][ref_frame] &&
2336 frame_mv[this_mode][ref_frame].as_int ==
Marco Paniconi6ba75b72020-03-16 14:13:53 -07002337 frame_mv[inter_mv_mode][ref_frame].as_int) {
kyslove3c05a82019-03-28 11:06:09 -07002338 skip_this_mv = 1;
2339 break;
2340 }
2341 }
2342
2343 if (skip_this_mv) continue;
2344
kyslove3c05a82019-03-28 11:06:09 -07002345 mi->mode = this_mode;
2346 mi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
2347 mi->mv[1].as_int = 0;
2348 if (reuse_inter_pred) {
2349 if (!this_mode_pred) {
2350 this_mode_pred = &tmp[3];
2351 } else {
2352 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
2353 pd->dst.buf = this_mode_pred->data;
2354 pd->dst.stride = bw;
2355 }
2356 }
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002357#if COLLECT_PICK_MODE_STAT
2358 ms_stat.num_nonskipped_searches[bsize][this_mode]++;
2359#endif
Marco Paniconi84cb8b32021-01-19 14:02:19 -08002360 if (enable_filter_search && !force_mv_inter_layer &&
Fyodor Kyslov0fc618e2019-10-02 16:25:14 -07002361 ((mi->mv[0].as_mv.row & 0x07) || (mi->mv[0].as_mv.col & 0x07)) &&
Fyodor Kyslov54ebdad2020-02-27 12:47:02 -08002362 (ref_frame == LAST_FRAME || !x->nonrd_prune_ref_frame_search)) {
Marco Paniconi966ec042019-10-08 09:33:18 -07002363 search_filter_ref(cpi, x, &this_rdc, mi_row, mi_col, tmp, bsize,
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07002364 reuse_inter_pred, &this_mode_pred, &this_early_term,
2365 use_model_yrd_large);
Fyodor Kyslov1dc01022019-07-02 16:47:44 -07002366 } else {
Marco Paniconib43603d2020-02-28 10:21:43 -08002367 mi->interp_filters =
2368 (filter_ref == SWITCHABLE)
2369 ? av1_broadcast_interp_filter(default_interp_filter)
2370 : av1_broadcast_interp_filter(filter_ref);
Marco Paniconi84cb8b32021-01-19 14:02:19 -08002371 if (force_mv_inter_layer)
2372 mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
2373
Fyodor Kyslov4dfc85b2020-02-04 17:47:38 -08002374 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
Marco Paniconi46f4cb82020-02-20 11:18:59 -08002375 if (use_model_yrd_large) {
Fyodor Kyslove8692412020-05-04 17:47:13 -07002376 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, &this_rdc,
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07002377 &this_early_term, use_modeled_non_rd_cost);
Fyodor Kyslov26473052019-08-26 17:09:30 -07002378 } else {
Fyodor Kyslov62fc03a2020-05-05 19:35:07 -07002379 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc,
Marco Paniconibcb2b722020-02-24 10:25:49 -08002380 use_modeled_non_rd_cost);
Fyodor Kyslov1dc01022019-07-02 16:47:44 -07002381 }
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -07002382 }
Fyodor Kyslov1dc01022019-07-02 16:47:44 -07002383
Fyodor Kyslov84abaea2019-06-25 14:44:14 -07002384 if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0) {
2385 sse_zeromv_norm =
Fyodor Kyslove8692412020-05-04 17:47:13 -07002386 (unsigned int)(this_rdc.sse >> (b_width_log2_lookup[bsize] +
2387 b_height_log2_lookup[bsize]));
Fyodor Kyslov84abaea2019-06-25 14:44:14 -07002388 }
kyslove3c05a82019-03-28 11:06:09 -07002389
chiyotsai8c004e12020-04-17 15:52:08 -07002390 const int skip_ctx = av1_get_skip_txfm_context(xd);
chiyotsai9a06d182020-05-01 17:12:12 -07002391 const int skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][1];
2392 const int no_skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][0];
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08002393 const int64_t sse_y = this_rdc.sse;
Fyodor Kyslova130ded2020-05-09 14:48:55 -07002394 if (this_early_term) {
2395 this_rdc.skip_txfm = 1;
2396 this_rdc.rate = skip_txfm_cost;
2397 this_rdc.dist = this_rdc.sse << 4;
2398 } else {
Marco Paniconibcb2b722020-02-24 10:25:49 -08002399 if (use_modeled_non_rd_cost) {
chiyotsai8c004e12020-04-17 15:52:08 -07002400 if (this_rdc.skip_txfm) {
2401 this_rdc.rate = skip_txfm_cost;
Fyodor Kyslov1dc01022019-07-02 16:47:44 -07002402 } else {
chiyotsai8c004e12020-04-17 15:52:08 -07002403 this_rdc.rate += no_skip_txfm_cost;
Fyodor Kyslov7cfbaa72019-05-31 13:37:29 -07002404 }
Fyodor Kyslov1dc01022019-07-02 16:47:44 -07002405 } else {
Fyodor Kyslove8692412020-05-04 17:47:13 -07002406 block_yrd(cpi, x, mi_row, mi_col, &this_rdc, &is_skippable, bsize,
2407 mi->tx_size);
2408 if (this_rdc.skip_txfm ||
2409 RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist) >=
2410 RDCOST(x->rdmult, 0, this_rdc.sse)) {
Fyodor Kyslova130ded2020-05-09 14:48:55 -07002411 if (!this_rdc.skip_txfm) {
2412 // Need to store "real" rdc for possible furure use if UV rdc
2413 // disallows tx skip
2414 nonskip_rdc = this_rdc;
2415 nonskip_rdc.rate += no_skip_txfm_cost;
2416 }
chiyotsai8c004e12020-04-17 15:52:08 -07002417 this_rdc.rate = skip_txfm_cost;
Fyodor Kyslove8692412020-05-04 17:47:13 -07002418 this_rdc.skip_txfm = 1;
2419 this_rdc.dist = this_rdc.sse;
Fyodor Kyslov1dc01022019-07-02 16:47:44 -07002420 } else {
Fyodor Kyslove8692412020-05-04 17:47:13 -07002421 this_rdc.rate += no_skip_txfm_cost;
Fyodor Kyslov1dc01022019-07-02 16:47:44 -07002422 }
kyslove3c05a82019-03-28 11:06:09 -07002423 }
Fyodor Kyslova130ded2020-05-09 14:48:55 -07002424 if ((x->color_sensitivity[0] || x->color_sensitivity[1])) {
2425 RD_STATS rdc_uv;
2426 const BLOCK_SIZE uv_bsize = get_plane_block_size(
2427 bsize, xd->plane[1].subsampling_x, xd->plane[1].subsampling_y);
2428 if (x->color_sensitivity[0]) {
2429 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2430 AOM_PLANE_U, AOM_PLANE_U);
2431 }
2432 if (x->color_sensitivity[1]) {
2433 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2434 AOM_PLANE_V, AOM_PLANE_V);
2435 }
2436 model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, &this_rdc.sse, 1, 2);
2437 // Restore Y rdc if UV rdc disallows txfm skip
2438 if (this_rdc.skip_txfm && !rdc_uv.skip_txfm &&
2439 nonskip_rdc.rate != INT_MAX)
2440 this_rdc = nonskip_rdc;
2441 this_rdc.rate += rdc_uv.rate;
2442 this_rdc.dist += rdc_uv.dist;
2443 this_rdc.skip_txfm = this_rdc.skip_txfm && rdc_uv.skip_txfm;
Fyodor Kyslovabef54f2019-08-14 14:43:12 -07002444 }
Fyodor Kyslovabef54f2019-08-14 14:43:12 -07002445 }
2446
kyslove3c05a82019-03-28 11:06:09 -07002447 // TODO(kyslov) account for UV prediction cost
kyslove3c05a82019-03-28 11:06:09 -07002448 this_rdc.rate += rate_mv;
2449 const int16_t mode_ctx =
2450 av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame);
chiyotsai9a06d182020-05-01 17:12:12 -07002451 this_rdc.rate += cost_mv_ref(mode_costs, this_mode, mode_ctx);
kyslove3c05a82019-03-28 11:06:09 -07002452
2453 this_rdc.rate += ref_costs_single[ref_frame];
2454
2455 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
Vishesh39e74092020-06-16 17:13:48 +05302456 if (cpi->oxcf.rc_cfg.mode == AOM_CBR) {
kyslove3c05a82019-03-28 11:06:09 -07002457 newmv_diff_bias(xd, this_mode, &this_rdc, bsize,
2458 frame_mv[this_mode][ref_frame].as_mv.row,
Marco Paniconi6a966f12020-02-13 12:14:28 -08002459 frame_mv[this_mode][ref_frame].as_mv.col, cpi->speed,
Marco Paniconi221b8992020-10-01 11:14:41 -07002460 x->source_variance, x->content_state_sb);
kyslove3c05a82019-03-28 11:06:09 -07002461 }
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08002462#if CONFIG_AV1_TEMPORAL_DENOISING
2463 if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc_pickmode &&
2464 cpi->denoiser.denoising_level > kDenLowLow) {
2465 av1_denoiser_update_frame_stats(mi, sse_y, this_mode, ctx);
2466 // Keep track of zero_last cost.
2467 if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0)
2468 zero_last_cost_orig = this_rdc.rdcost;
2469 }
2470#else
2471 (void)sse_y;
2472#endif
kyslove3c05a82019-03-28 11:06:09 -07002473
2474 mode_checked[this_mode][ref_frame] = 1;
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002475#if COLLECT_PICK_MODE_STAT
2476 aom_usec_timer_mark(&ms_stat.timer1);
2477 ms_stat.nonskipped_search_times[bsize][this_mode] +=
2478 aom_usec_timer_elapsed(&ms_stat.timer1);
2479#endif
kyslove3c05a82019-03-28 11:06:09 -07002480 if (this_rdc.rdcost < best_rdc.rdcost) {
2481 best_rdc = this_rdc;
2482 best_early_term = this_early_term;
2483 best_pickmode.best_mode = this_mode;
2484 best_pickmode.best_pred_filter = mi->interp_filters;
2485 best_pickmode.best_tx_size = mi->tx_size;
2486 best_pickmode.best_ref_frame = ref_frame;
chiyotsai8c004e12020-04-17 15:52:08 -07002487 best_pickmode.best_mode_skip_txfm = this_rdc.skip_txfm;
Fyodor Kyslove0be17e2020-05-13 14:20:50 -07002488 best_pickmode.best_mode_initial_skip_flag =
2489 (nonskip_rdc.rate == INT_MAX && this_rdc.skip_txfm);
2490
kyslove3c05a82019-03-28 11:06:09 -07002491 if (reuse_inter_pred) {
2492 free_pred_buffer(best_pickmode.best_pred);
2493 best_pickmode.best_pred = this_mode_pred;
2494 }
2495 } else {
2496 if (reuse_inter_pred) free_pred_buffer(this_mode_pred);
2497 }
Fyodor Kyslovb0f0fb12020-11-10 19:08:36 -08002498 if (best_early_term && (idx > 0 || cpi->sf.rt_sf.nonrd_agressive_skip)) {
chiyotsai4c1e5c62020-04-30 17:54:14 -07002499 txfm_info->skip_txfm = 1;
kyslove3c05a82019-03-28 11:06:09 -07002500 break;
2501 }
2502 }
2503
2504 mi->mode = best_pickmode.best_mode;
Fyodor Kyslov26473052019-08-26 17:09:30 -07002505 mi->interp_filters = best_pickmode.best_pred_filter;
kyslove3c05a82019-03-28 11:06:09 -07002506 mi->tx_size = best_pickmode.best_tx_size;
Fyodor Kyslov4dfdb5c2019-05-24 11:06:56 -07002507 memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size));
kyslove3c05a82019-03-28 11:06:09 -07002508 mi->ref_frame[0] = best_pickmode.best_ref_frame;
2509 mi->mv[0].as_int =
2510 frame_mv[best_pickmode.best_mode][best_pickmode.best_ref_frame].as_int;
kyslov82449d12019-05-02 13:36:50 -07002511
2512 // Perform intra prediction search, if the best SAD is above a certain
2513 // threshold.
2514 mi->angle_delta[PLANE_TYPE_Y] = 0;
2515 mi->angle_delta[PLANE_TYPE_UV] = 0;
2516 mi->filter_intra_mode_info.use_filter_intra = 0;
Marco Paniconi5b5b19f2019-11-19 11:31:48 -08002517
Fyodor Kyslov175b5b22020-04-28 19:08:51 -07002518 estimate_intra_mode(cpi, x, bsize, use_modeled_non_rd_cost, best_early_term,
2519 ref_costs_single[INTRA_FRAME], reuse_inter_pred,
2520 &orig_dst, tmp, &this_mode_pred, &best_rdc,
2521 &best_pickmode);
kyslov82449d12019-05-02 13:36:50 -07002522
kyslove3c05a82019-03-28 11:06:09 -07002523 pd->dst = orig_dst;
kyslov82449d12019-05-02 13:36:50 -07002524 mi->mode = best_pickmode.best_mode;
2525 mi->ref_frame[0] = best_pickmode.best_ref_frame;
Fyodor Kyslova130ded2020-05-09 14:48:55 -07002526 txfm_info->skip_txfm = best_rdc.skip_txfm;
kyslov82449d12019-05-02 13:36:50 -07002527
2528 if (!is_inter_block(mi)) {
2529 mi->interp_filters = av1_broadcast_interp_filter(SWITCHABLE_FILTERS);
2530 }
2531
kyslove3c05a82019-03-28 11:06:09 -07002532 if (reuse_inter_pred && best_pickmode.best_pred != NULL) {
2533 PRED_BUFFER *const best_pred = best_pickmode.best_pred;
2534 if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) {
2535 aom_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
Elliott Karpilovskyebe812f2020-04-13 18:48:50 -07002536 pd->dst.stride, bw, bh);
kyslove3c05a82019-03-28 11:06:09 -07002537 }
2538 }
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -08002539
2540#if CONFIG_AV1_TEMPORAL_DENOISING
2541 if (cpi->oxcf.noise_sensitivity > 0 && resize_pending == 0 &&
2542 denoise_svc_pickmode && cpi->denoiser.denoising_level > kDenLowLow &&
2543 cpi->denoiser.reset == 0) {
2544 AV1_DENOISER_DECISION decision = COPY_BLOCK;
2545 ctx->sb_skip_denoising = 0;
2546 av1_pickmode_ctx_den_update(&ctx_den, zero_last_cost_orig, ref_costs_single,
2547 frame_mv, reuse_inter_pred, &best_pickmode);
2548 av1_denoiser_denoise(cpi, x, mi_row, mi_col, bsize, ctx, &decision,
2549 gf_temporal_ref);
2550 if (denoise_recheck_zeromv)
2551 recheck_zeromv_after_denoising(cpi, mi, x, xd, decision, &ctx_den,
2552 yv12_mb, &best_rdc, &best_pickmode, bsize,
2553 mi_row, mi_col);
2554 best_pickmode.best_ref_frame = ctx_den.best_ref_frame;
2555 }
2556#endif
2557
chiyotsai868dc902019-12-12 15:32:52 -08002558 if (cpi->sf.inter_sf.adaptive_rd_thresh) {
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07002559 THR_MODES best_mode_idx =
2560 mode_idx[best_pickmode.best_ref_frame][mode_offset(mi->mode)];
2561 if (best_pickmode.best_ref_frame == INTRA_FRAME) {
2562 // Only consider the modes that are included in the intra_mode_list.
2563 int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE);
Jerome Jiangcb1f6192019-12-12 18:02:47 -08002564 for (int i = 0; i < intra_modes; i++) {
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07002565 update_thresh_freq_fact(cpi, x, bsize, INTRA_FRAME, best_mode_idx,
2566 intra_mode_list[i]);
2567 }
2568 } else {
Fyodor Kyslovf5c96902020-04-27 17:08:56 -07002569 PREDICTION_MODE this_mode;
2570 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
2571 update_thresh_freq_fact(cpi, x, bsize, best_pickmode.best_ref_frame,
2572 best_mode_idx, this_mode);
Fyodor Kyslov67691bd2019-07-25 14:28:34 -07002573 }
2574 }
2575 }
kyslove3c05a82019-03-28 11:06:09 -07002576
chiyotsai03c48a82019-10-18 16:48:59 -07002577#if CONFIG_INTERNAL_STATS
kyslove3c05a82019-03-28 11:06:09 -07002578 store_coding_context(x, ctx, mi->mode);
chiyotsai03c48a82019-10-18 16:48:59 -07002579#else
2580 store_coding_context(x, ctx);
2581#endif // CONFIG_INTERNAL_STATS
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002582#if COLLECT_PICK_MODE_STAT
2583 aom_usec_timer_mark(&ms_stat.timer2);
2584 ms_stat.avg_block_times[bsize] += aom_usec_timer_elapsed(&ms_stat.timer2);
2585 //
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002586 if ((mi_row + mi_size_high[bsize] >= (cpi->common.mi_params.mi_rows)) &&
2587 (mi_col + mi_size_wide[bsize] >= (cpi->common.mi_params.mi_cols))) {
Fyodor Kyslov45ec9562019-09-26 13:02:24 -07002588 int i, j;
2589 PREDICTION_MODE used_modes[3] = { NEARESTMV, NEARMV, NEWMV };
2590 BLOCK_SIZE bss[5] = { BLOCK_8X8, BLOCK_16X16, BLOCK_32X32, BLOCK_64X64,
2591 BLOCK_128X128 };
2592 int64_t total_time = 0l;
2593 int32_t total_blocks = 0;
2594
2595 printf("\n");
2596 for (i = 0; i < 5; i++) {
2597 printf("BS(%d) Num %d, Avg_time %f: ", bss[i], ms_stat.num_blocks[bss[i]],
2598 ms_stat.num_blocks[bss[i]] > 0
2599 ? (float)ms_stat.avg_block_times[bss[i]] /
2600 ms_stat.num_blocks[bss[i]]
2601 : 0);
2602 total_time += ms_stat.avg_block_times[bss[i]];
2603 total_blocks += ms_stat.num_blocks[bss[i]];
2604 for (j = 0; j < 3; j++) {
2605 printf("Mode %d, %d/%d tps %f ", used_modes[j],
2606 ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]],
2607 ms_stat.num_searches[bss[i]][used_modes[j]],
2608 ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]] > 0
2609 ? (float)ms_stat
2610 .nonskipped_search_times[bss[i]][used_modes[j]] /
2611 ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]]
2612 : 0l);
2613 }
2614 printf("\n");
2615 }
2616 printf("Total time = %ld. Total blocks = %d\n", total_time, total_blocks);
2617 }
2618 //
2619#endif // COLLECT_PICK_MODE_STAT
kyslove3c05a82019-03-28 11:06:09 -07002620 *rd_cost = best_rdc;
2621}