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