Coverage Report

Created: 2026-11-12 08:20

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