Coverage Report

Created: 2026-07-25 07:06

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 unsigned 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 && full_var_rd * 40 < 62267 * 7) ||
257
0
        (bsize == BLOCK_32X32 && full_var_rd * 8 < 42380) ||
258
0
        (bsize == BLOCK_16X16 && 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 > ((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 *lookahead_search_sites =
1637
0
      cpi->mv_search_params.search_site_cfg[SS_CFG_LOOKAHEAD];
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,
1641
0
                                     lookahead_search_sites, 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
  int_mv best_mv;
1651
0
  int bestsme = INT_MAX;
1652
0
  if (!cpi->sf.mv_sf.hash_max_8x8_intrabc_blocks || bsize <= BLOCK_8X8) {
1653
0
    bestsme = av1_intrabc_hash_search(
1654
0
        cpi, xd, &fullms_params, &x->intrabc_hash_info, &best_mv.as_fullmv);
1655
0
  }
1656
0
  if (bestsme == INT_MAX) {
1657
0
    const int miss_mode = cpi->sf.rt_sf.rt_intrabc_miss_mode;
1658
0
    if (miss_mode == 0) {
1659
0
      FULLPEL_MV_STATS best_mv_stats;
1660
0
      bestsme = av1_full_pixel_search(start_mv, &fullms_params,
1661
0
                                      cpi->mv_search_params.mv_step_param, NULL,
1662
0
                                      &best_mv.as_fullmv, &best_mv_stats, NULL);
1663
0
    } else if (miss_mode == 1 || miss_mode == 2) {
1664
      // Evaluate start_mv (BVP candidate)
1665
0
      FULLPEL_MV cand_mv = start_mv;
1666
0
      if (cand_mv.col >= fullms_params.mv_limits.col_min &&
1667
0
          cand_mv.col <= fullms_params.mv_limits.col_max &&
1668
0
          cand_mv.row >= fullms_params.mv_limits.row_min &&
1669
0
          cand_mv.row <= fullms_params.mv_limits.row_max) {
1670
0
        MV dv = get_mv_from_fullmv(&cand_mv);
1671
0
        if (av1_is_dv_valid(dv, cm, xd, mi_row, mi_col, bsize,
1672
0
                            cm->seq_params->mib_size_log2)) {
1673
0
          const struct buf_2d *src = fullms_params.ms_buffers.src;
1674
0
          const struct buf_2d *ref = fullms_params.ms_buffers.ref;
1675
0
          unsigned int sad = fullms_params.vfp->sdf(
1676
0
              src->buf, src->stride, get_buf_from_fullmv(ref, &cand_mv),
1677
0
              ref->stride);
1678
0
          unsigned int rate =
1679
0
              av1_mv_bit_cost(&dv, &dv_ref.as_mv, x->dv_costs->joint_mv,
1680
0
                              x->dv_costs->dv_costs, MV_COST_WEIGHT);
1681
0
          best_mv.as_fullmv = cand_mv;
1682
0
          bestsme = (int)(sad + rate);
1683
0
        }
1684
0
      }
1685
1686
      // If miss_mode == 2, do a one-shot 12-point offset probe around start_mv
1687
0
      if (miss_mode == 2) {
1688
0
        const int row_offsets[12] = { -1, 1, 0, 0, -2, 2, 0, 0, -1, -1, 1, 1 };
1689
0
        const int col_offsets[12] = { 0, 0, -1, 1, 0, 0, -2, 2, -1, 1, -1, 1 };
1690
0
        for (int k = 0; k < 12; ++k) {
1691
0
          FULLPEL_MV probe_mv;
1692
0
          probe_mv.row = start_mv.row + row_offsets[k];
1693
0
          probe_mv.col = start_mv.col + col_offsets[k];
1694
0
          if (probe_mv.col >= fullms_params.mv_limits.col_min &&
1695
0
              probe_mv.col <= fullms_params.mv_limits.col_max &&
1696
0
              probe_mv.row >= fullms_params.mv_limits.row_min &&
1697
0
              probe_mv.row <= fullms_params.mv_limits.row_max) {
1698
0
            MV dv = get_mv_from_fullmv(&probe_mv);
1699
0
            if (av1_is_dv_valid(dv, cm, xd, mi_row, mi_col, bsize,
1700
0
                                cm->seq_params->mib_size_log2)) {
1701
0
              const struct buf_2d *src = fullms_params.ms_buffers.src;
1702
0
              const struct buf_2d *ref = fullms_params.ms_buffers.ref;
1703
0
              unsigned int sad = fullms_params.vfp->sdf(
1704
0
                  src->buf, src->stride, get_buf_from_fullmv(ref, &probe_mv),
1705
0
                  ref->stride);
1706
0
              unsigned int rate =
1707
0
                  av1_mv_bit_cost(&dv, &dv_ref.as_mv, x->dv_costs->joint_mv,
1708
0
                                  x->dv_costs->dv_costs, MV_COST_WEIGHT);
1709
0
              int cost = (int)(sad + rate);
1710
0
              if (cost < bestsme) {
1711
0
                bestsme = cost;
1712
0
                best_mv.as_fullmv = probe_mv;
1713
0
              }
1714
0
            }
1715
0
          }
1716
0
        }
1717
0
      }
1718
0
    } else {
1719
      // miss_mode == 3: skip entirely (hash-only)
1720
0
      bestsme = INT_MAX;
1721
0
    }
1722
0
  }
1723
0
  if (bestsme != INT_MAX) {
1724
0
    MV dv = get_mv_from_fullmv(&best_mv.as_fullmv);
1725
0
    if (av1_is_dv_valid(dv, cm, xd, mi_row, mi_col, bsize,
1726
0
                        cm->seq_params->mib_size_log2)) {
1727
0
      const int8_t ori_mi_use_intrabc = mi->use_intrabc;
1728
0
      const int_mv ori_mi_mv0 = mi->mv[0];
1729
0
      const PREDICTION_MODE ori_mi_mode = mi->mode;
1730
0
      const MV_REFERENCE_FRAME ori_ref0 = mi->ref_frame[0];
1731
1732
0
      mi->use_intrabc = 1;
1733
0
      mi->mv[0].as_mv = dv;
1734
0
      mi->mode = DC_PRED;
1735
0
      mi->ref_frame[0] = INTRA_FRAME;
1736
1737
0
      av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, 0, 0);
1738
0
      if (num_planes > 1) {
1739
0
        av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, 1,
1740
0
                                      1);
1741
0
        av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, 2,
1742
0
                                      2);
1743
0
      }
1744
1745
0
      this_rdc->rate =
1746
0
          av1_mv_bit_cost(&dv, &dv_ref.as_mv, x->dv_costs->joint_mv,
1747
0
                          x->dv_costs->dv_costs, MV_COST_WEIGHT);
1748
0
      this_rdc->rate += x->mode_costs.intrabc_cost[1];
1749
0
      this_rdc->dist = 0;
1750
0
      int skippable;
1751
0
      av1_block_yrd(x, this_rdc, &skippable, bsize,
1752
0
                    AOMMIN(mi->tx_size, TX_16X16));
1753
0
      if (num_planes > 1) {
1754
0
        RD_STATS rdc_uv;
1755
0
        av1_invalid_rd_stats(&rdc_uv);
1756
0
        av1_model_rd_for_sb_uv(cpi, bsize, x, xd, &rdc_uv, 1, 2);
1757
0
        this_rdc->rate += rdc_uv.rate;
1758
0
        this_rdc->dist += rdc_uv.dist;
1759
0
      }
1760
0
      this_rdc->rdcost = RDCOST(x->rdmult, this_rdc->rate, this_rdc->dist);
1761
0
      *best_dv = mi->mv[0];
1762
1763
      // Restore mi state after trial
1764
0
      mi->use_intrabc = ori_mi_use_intrabc;
1765
0
      mi->mv[0] = ori_mi_mv0;
1766
0
      mi->mode = ori_mi_mode;
1767
0
      mi->ref_frame[0] = ori_ref0;
1768
0
    }
1769
0
  }
1770
1771
  // Restore pre[0] state
1772
0
  for (int i = 0; i < num_planes; ++i) xd->plane[i].pre[0] = ori_pre[i];
1773
0
}
1774
1775
void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
1776
0
                               BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1777
0
  AV1_COMMON *const cm = &cpi->common;
1778
0
  MACROBLOCKD *const xd = &x->e_mbd;
1779
0
  MB_MODE_INFO *const mi = xd->mi[0];
1780
0
  RD_STATS this_rdc, best_rdc;
1781
0
  struct estimate_block_intra_args args;
1782
0
  init_estimate_block_intra_args(&args, cpi, x);
1783
0
  const TxfmSearchParams *txfm_params = &x->txfm_search_params;
1784
0
  const int mi_row = xd->mi_row;
1785
0
  const int mi_col = xd->mi_col;
1786
0
  mi->tx_size =
1787
0
      AOMMIN(max_txsize_lookup[bsize],
1788
0
             tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
1789
  // For flat blocks at high qp, along the top/left boundary, cap the
1790
  // transform size to 16x16. This is avoid chessboard artifact that
1791
  // can occur for flat input at high QP.
1792
0
  if (cm->quant_params.base_qindex > 150 && x->source_variance == 0 &&
1793
0
      (mi_row == 0 || mi_col == 0) && mi->tx_size > TX_16X16) {
1794
0
    mi->tx_size = TX_16X16;
1795
0
  }
1796
0
  assert(IMPLIES(xd->lossless[mi->segment_id], mi->tx_size == TX_4X4));
1797
0
  const BLOCK_SIZE tx_bsize = txsize_to_bsize[mi->tx_size];
1798
1799
  // If the current block size is the same as the transform block size, enable
1800
  // mode pruning based on the best SAD so far.
1801
0
  if (cpi->sf.rt_sf.prune_intra_mode_using_best_sad_so_far && bsize == tx_bsize)
1802
0
    args.prune_mode_based_on_sad = true;
1803
1804
0
  int *bmode_costs;
1805
0
  PREDICTION_MODE best_mode = DC_PRED;
1806
0
  const MB_MODE_INFO *above_mi = xd->above_mbmi;
1807
0
  const MB_MODE_INFO *left_mi = xd->left_mbmi;
1808
0
  const PREDICTION_MODE A = av1_above_block_mode(above_mi);
1809
0
  const PREDICTION_MODE L = av1_left_block_mode(left_mi);
1810
0
  const int above_ctx = intra_mode_context[A];
1811
0
  const int left_ctx = intra_mode_context[L];
1812
0
  const unsigned int source_variance = x->source_variance;
1813
0
  bmode_costs = x->mode_costs.y_mode_costs[above_ctx][left_ctx];
1814
  // Use this flag to signal large flat blocks that may need special
1815
  // treatment: in the current case H/V/SMOOTH may not be skipped if
1816
  // DC has nonzero distortion and skippable is set. This is to remove
1817
  // visual artifacts observed for screen in realtime mode.
1818
0
  const bool flat_blocks_screen =
1819
0
      cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
1820
0
      cpi->oxcf.mode == REALTIME && x->source_variance == 0 &&
1821
0
      bsize >= BLOCK_32X32;
1822
0
  av1_invalid_rd_stats(&best_rdc);
1823
0
  av1_invalid_rd_stats(&this_rdc);
1824
1825
0
  init_mbmi_nonrd(mi, DC_PRED, INTRA_FRAME, NONE_FRAME, cm);
1826
0
  mi->mv[0].as_int = mi->mv[1].as_int = INVALID_MV;
1827
1828
0
  bool allow_skip_nondc = true;
1829
0
  bool palette_selected = false;
1830
  // Change the limit of this loop to add other intra prediction
1831
  // mode tests.
1832
0
  for (int mode_index = 0; mode_index < RTC_INTRA_MODES; ++mode_index) {
1833
0
    PREDICTION_MODE this_mode = intra_mode_list[mode_index];
1834
1835
    // Force DC for spatially flat block for large bsize, on top-left corner.
1836
    // This removed potential artifact observed in gray scale image for high Q.
1837
0
    if (x->source_variance == 0 && mi_col == 0 && mi_row == 0 &&
1838
0
        bsize >= BLOCK_32X32 && this_mode > 0)
1839
0
      continue;
1840
1841
    // As per the statistics generated for intra mode evaluation in the nonrd
1842
    // path, it is found that the probability of H_PRED mode being the winner is
1843
    // very low when the best mode so far is V_PRED (out of DC_PRED and V_PRED).
1844
    // If V_PRED is the winner mode out of DC_PRED and V_PRED, it could imply
1845
    // the presence of a vertically dominant pattern. Hence, H_PRED mode is not
1846
    // evaluated.
1847
0
    if (cpi->sf.rt_sf.prune_h_pred_using_best_mode_so_far &&
1848
0
        this_mode == H_PRED && best_mode == V_PRED && allow_skip_nondc)
1849
0
      continue;
1850
1851
0
    if (should_prune_intra_modes_using_neighbors(
1852
0
            xd, cpi->sf.rt_sf.enable_intra_mode_pruning_using_neighbors,
1853
0
            this_mode, A, L)) {
1854
      // Prune V_PRED and H_PRED if source variance of the block is less than
1855
      // or equal to 50. The source variance threshold is obtained empirically.
1856
0
      if ((this_mode == V_PRED || this_mode == H_PRED) &&
1857
0
          source_variance <= 50 && allow_skip_nondc)
1858
0
        continue;
1859
1860
      // As per the statistics, probability of SMOOTH_PRED being the winner is
1861
      // low when best mode so far is DC_PRED (out of DC_PRED, V_PRED and
1862
      // H_PRED). Hence, SMOOTH_PRED mode is not evaluated.
1863
0
      if (best_mode == DC_PRED && this_mode == SMOOTH_PRED && allow_skip_nondc)
1864
0
        continue;
1865
0
    }
1866
1867
0
    this_rdc.dist = this_rdc.rate = 0;
1868
0
    args.mode = this_mode;
1869
0
    args.skippable = 1;
1870
0
    args.rdc = &this_rdc;
1871
0
    mi->mode = this_mode;
1872
0
    av1_foreach_transformed_block_in_plane(xd, bsize, AOM_PLANE_Y,
1873
0
                                           av1_estimate_block_intra, &args);
1874
1875
0
    if (this_rdc.rate == INT_MAX) continue;
1876
1877
0
    const int skip_ctx = av1_get_skip_txfm_context(xd);
1878
0
    if (args.skippable) {
1879
0
      this_rdc.rate = x->mode_costs.skip_txfm_cost[skip_ctx][1];
1880
0
    } else {
1881
0
      this_rdc.rate += x->mode_costs.skip_txfm_cost[skip_ctx][0];
1882
0
    }
1883
0
    this_rdc.rate += bmode_costs[this_mode];
1884
0
    this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1885
0
    if (this_rdc.rdcost < best_rdc.rdcost) {
1886
0
      best_rdc = this_rdc;
1887
0
      best_mode = this_mode;
1888
0
    }
1889
0
    if (this_mode == DC_PRED) {
1890
0
      if (flat_blocks_screen && args.skippable && this_rdc.dist > 0)
1891
0
        allow_skip_nondc = false;
1892
0
    }
1893
0
  }
1894
1895
0
  const unsigned int thresh_sad =
1896
0
      cpi->sf.rt_sf.prune_palette_search_nonrd > 1 ? 100 : 20;
1897
0
  const unsigned int best_sad_norm =
1898
0
      args.best_sad >>
1899
0
      (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
1900
1901
  // Try palette if it's enabled.
1902
0
  bool try_palette =
1903
0
      cpi->oxcf.tool_cfg.enable_palette &&
1904
0
      av1_allow_palette(cpi->common.features.allow_screen_content_tools,
1905
0
                        mi->bsize);
1906
0
  if (cpi->sf.rt_sf.prune_palette_search_nonrd > 0) {
1907
0
    bool prune =
1908
0
        (!args.prune_mode_based_on_sad || best_sad_norm > thresh_sad) &&
1909
0
        bsize <= BLOCK_16X16 && x->source_variance > 200;
1910
0
    try_palette &= prune;
1911
0
  }
1912
0
  if (try_palette) {
1913
0
    const unsigned int intra_ref_frame_cost = 0;
1914
0
    x->color_palette_thresh = (best_sad_norm < 500) ? 32 : 64;
1915
1916
    // Search palette mode for Luma plane in intra frame.
1917
0
    av1_search_palette_mode_luma(cpi, x, bsize, intra_ref_frame_cost, ctx,
1918
0
                                 &this_rdc, best_rdc.rdcost);
1919
    // Update best mode data.
1920
0
    if (this_rdc.rdcost < best_rdc.rdcost) {
1921
0
      palette_selected = true;
1922
0
      best_mode = DC_PRED;
1923
0
      mi->mv[0].as_int = INVALID_MV;
1924
0
      mi->mv[1].as_int = INVALID_MV;
1925
0
      best_rdc.rate = this_rdc.rate;
1926
0
      best_rdc.dist = this_rdc.dist;
1927
0
      best_rdc.rdcost = this_rdc.rdcost;
1928
0
      if (xd->tx_type_map[0] != DCT_DCT)
1929
0
        av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
1930
0
    } else {
1931
0
      av1_zero(mi->palette_mode_info);
1932
0
    }
1933
0
  }
1934
1935
0
  bool try_intrabc =
1936
0
      cpi->sf.rt_sf.rt_use_intrabc && av1_allow_intrabc(cm) &&
1937
0
      bsize <= BLOCK_16X16 &&
1938
0
      (!cpi->sf.rt_sf.rt_prune_intrabc_nonrd || palette_selected);
1939
1940
0
  if (try_intrabc) {
1941
0
    int_mv best_dv;
1942
0
    av1_search_intrabc_nonrd(cpi, x, bsize, &this_rdc, &best_dv);
1943
0
    if (this_rdc.rdcost < best_rdc.rdcost) {
1944
0
      best_rdc = this_rdc;
1945
0
      best_mode = DC_PRED;
1946
0
      mi->use_intrabc = 1;
1947
0
      mi->mv[0] = best_dv;
1948
0
      mi->uv_mode = UV_DC_PRED;
1949
0
      mi->motion_mode = SIMPLE_TRANSLATION;
1950
0
      mi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
1951
0
      memset(&mi->palette_mode_info, 0, sizeof(mi->palette_mode_info));
1952
0
      memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size));
1953
0
    } else {
1954
0
      mi->use_intrabc = 0;
1955
0
    }
1956
0
  }
1957
1958
0
  mi->mode = best_mode;
1959
  // Keep DC for UV since mode test is based on Y channel only.
1960
0
  mi->uv_mode = UV_DC_PRED;
1961
0
  *rd_cost = best_rdc;
1962
1963
  // For lossless: always force the skip flags off.
1964
  // Even though the blk_skip is set to 0 above in the rdcost comparison,
1965
  // do it here again in case the above logic changes.
1966
0
  if (is_lossless_requested(&cpi->oxcf.rc_cfg)) {
1967
0
    x->txfm_search_info.skip_txfm = 0;
1968
0
  }
1969
1970
#if CONFIG_INTERNAL_STATS
1971
  store_coding_context_nonrd(x, ctx, mi->mode);
1972
#else
1973
0
  store_coding_context_nonrd(x, ctx);
1974
0
#endif  // CONFIG_INTERNAL_STATS
1975
0
}
1976
1977
0
static inline int is_same_gf_and_last_scale(AV1_COMMON *cm) {
1978
0
  struct scale_factors *const sf_last = get_ref_scale_factors(cm, LAST_FRAME);
1979
0
  struct scale_factors *const sf_golden =
1980
0
      get_ref_scale_factors(cm, GOLDEN_FRAME);
1981
0
  return ((sf_last->x_scale_fp == sf_golden->x_scale_fp) &&
1982
0
          (sf_last->y_scale_fp == sf_golden->y_scale_fp));
1983
0
}
1984
1985
static inline void get_ref_frame_use_mask(AV1_COMP *cpi, MACROBLOCK *x,
1986
                                          MB_MODE_INFO *mi, int mi_row,
1987
                                          int mi_col, BLOCK_SIZE bsize,
1988
                                          int gf_temporal_ref,
1989
                                          int use_ref_frame[],
1990
0
                                          int *force_skip_low_temp_var) {
1991
0
  AV1_COMMON *const cm = &cpi->common;
1992
0
  const struct segmentation *const seg = &cm->seg;
1993
0
  const int is_small_sb = (cm->seq_params->sb_size == BLOCK_64X64);
1994
1995
  // When the ref_frame_config is used to set the reference frame structure
1996
  // then the usage of alt_ref is determined by the ref_frame_flags
1997
  // (and not the speed feature use_nonrd_altref_frame).
1998
0
  int use_alt_ref_frame = cpi->ppi->rtc_ref.set_ref_frame_config ||
1999
0
                          cpi->sf.rt_sf.use_nonrd_altref_frame;
2000
2001
0
  int use_golden_ref_frame = 1;
2002
0
  int use_last_ref_frame = 1;
2003
2004
  // When the ref_frame_config is used to set the reference frame structure:
2005
  // check if LAST is used as a reference. And only remove golden and altref
2006
  // references below if last is used as a reference.
2007
0
  if (cpi->ppi->rtc_ref.set_ref_frame_config)
2008
0
    use_last_ref_frame =
2009
0
        cpi->ref_frame_flags & AOM_LAST_FLAG ? use_last_ref_frame : 0;
2010
2011
  // frame_since_golden is not used when user sets the referene structure.
2012
0
  if (!cpi->ppi->rtc_ref.set_ref_frame_config && use_last_ref_frame &&
2013
0
      cpi->rc.frames_since_golden == 0 && gf_temporal_ref) {
2014
0
    use_golden_ref_frame = 0;
2015
0
  }
2016
2017
0
  if (use_last_ref_frame && cpi->sf.rt_sf.short_circuit_low_temp_var &&
2018
0
      x->nonrd_prune_ref_frame_search) {
2019
0
    if (is_small_sb)
2020
0
      *force_skip_low_temp_var = av1_get_force_skip_low_temp_var_small_sb(
2021
0
          &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
2022
0
    else
2023
0
      *force_skip_low_temp_var = av1_get_force_skip_low_temp_var(
2024
0
          &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
2025
    // If force_skip_low_temp_var is set, skip golden reference.
2026
0
    if (*force_skip_low_temp_var) {
2027
0
      use_golden_ref_frame = 0;
2028
0
      use_alt_ref_frame = 0;
2029
0
    }
2030
0
  }
2031
2032
0
  if (use_last_ref_frame &&
2033
0
      (x->nonrd_prune_ref_frame_search > 2 || x->force_zeromv_skip_for_blk ||
2034
0
       (x->nonrd_prune_ref_frame_search > 1 && bsize > BLOCK_64X64))) {
2035
0
    use_golden_ref_frame = 0;
2036
0
    use_alt_ref_frame = 0;
2037
0
  }
2038
2039
0
  if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
2040
0
      get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) {
2041
0
    use_ref_frame[GOLDEN_FRAME] = 1;
2042
0
    use_ref_frame[ALTREF_FRAME] = 0;
2043
0
    return;
2044
0
  } else if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
2045
0
             get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) ==
2046
0
                 ALTREF_FRAME) {
2047
0
    use_ref_frame[GOLDEN_FRAME] = 0;
2048
0
    use_ref_frame[ALTREF_FRAME] = 1;
2049
0
    return;
2050
0
  }
2051
2052
  // Skip golden/altref reference if color is set, on flat blocks with motion.
2053
  // For screen: always skip golden/alt (if color_sensitivity_sb_g/alt is set)
2054
  // except when x->nonrd_prune_ref_frame_search = 0. This latter flag
2055
  // may be set in the variance partition when golden is a much better
2056
  // reference than last, in which case it may not be worth skipping
2057
  // golden/altref completely.
2058
  // Condition on use_last_ref to make sure there remains at least one
2059
  // reference.
2060
0
  if (use_last_ref_frame &&
2061
0
      ((cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
2062
0
        x->nonrd_prune_ref_frame_search != 0) ||
2063
0
       (x->source_variance < 200 &&
2064
0
        x->content_state_sb.source_sad_nonrd >= kLowSad))) {
2065
0
    if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2066
0
        x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
2067
0
      use_golden_ref_frame = 0;
2068
0
    if (x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2069
0
        x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
2070
0
      use_alt_ref_frame = 0;
2071
0
  }
2072
2073
  // For non-screen: if golden and altref are not being selected as references
2074
  // (use_golden_ref_frame/use_alt_ref_frame = 0) check to allow golden back
2075
  // based on the sad of nearest/nearmv of LAST ref. If this block sad is large,
2076
  // keep golden as reference. Only do this for the agrressive pruning mode and
2077
  // avoid it when color is set for golden reference.
2078
0
  if (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN &&
2079
0
      (cpi->ref_frame_flags & AOM_LAST_FLAG) && !use_golden_ref_frame &&
2080
0
      !use_alt_ref_frame && x->pred_mv_sad[LAST_FRAME] != INT_MAX &&
2081
0
      x->nonrd_prune_ref_frame_search > 2 &&
2082
0
      x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
2083
0
      x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) {
2084
0
    int thr = (cm->width * cm->height > RESOLUTION_288P) ? 100 : 150;
2085
0
    int pred = x->pred_mv_sad[LAST_FRAME] >>
2086
0
               (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
2087
0
    if (pred > thr) use_golden_ref_frame = 1;
2088
0
  }
2089
2090
0
  use_alt_ref_frame =
2091
0
      cpi->ref_frame_flags & AOM_ALT_FLAG ? use_alt_ref_frame : 0;
2092
0
  use_golden_ref_frame =
2093
0
      cpi->ref_frame_flags & AOM_GOLD_FLAG ? use_golden_ref_frame : 0;
2094
2095
  // For spatial layers: enable golden ref if it is set by user and
2096
  // corresponds to the lower spatial layer.
2097
0
  if (cpi->svc.spatial_layer_id > 0 && (cpi->ref_frame_flags & AOM_GOLD_FLAG) &&
2098
0
      x->content_state_sb.source_sad_nonrd < kHighSad) {
2099
0
    const int buffslot_golden =
2100
0
        cpi->ppi->rtc_ref.ref_idx[GOLDEN_FRAME - LAST_FRAME];
2101
0
    if (cpi->ppi->rtc_ref.buffer_time_index[buffslot_golden] ==
2102
0
        cpi->svc.current_superframe)
2103
0
      use_golden_ref_frame = 1;
2104
0
  }
2105
2106
0
  use_ref_frame[ALTREF_FRAME] = use_alt_ref_frame;
2107
0
  use_ref_frame[GOLDEN_FRAME] = use_golden_ref_frame;
2108
0
  use_ref_frame[LAST_FRAME] = use_last_ref_frame;
2109
  // Keep this assert on, as only 3 references are used in nonrd_pickmode
2110
  // (LAST, GOLDEN, ALTREF), and if all 3 are not set by user then this
2111
  // frame must be an intra-only frame and hence should never enter the
2112
  // pickmode here for inter frames.
2113
0
  assert(use_last_ref_frame || use_golden_ref_frame || use_alt_ref_frame);
2114
0
}
2115
2116
static inline int is_filter_search_enabled_blk(AV1_COMP *cpi, MACROBLOCK *x,
2117
                                               int mi_row, int mi_col,
2118
                                               BLOCK_SIZE bsize, int segment_id,
2119
                                               int cb_pred_filter_search,
2120
0
                                               InterpFilter *filt_select) {
2121
0
  const AV1_COMMON *const cm = &cpi->common;
2122
  // filt search disabled
2123
0
  if (!cpi->sf.rt_sf.use_nonrd_filter_search) return 0;
2124
  // filt search purely based on mode properties
2125
0
  if (!cb_pred_filter_search) return 1;
2126
0
  MACROBLOCKD *const xd = &x->e_mbd;
2127
0
  int enable_interp_search = 0;
2128
0
  if (!(xd->left_mbmi && xd->above_mbmi)) {
2129
    // neighbors info unavailable
2130
0
    enable_interp_search = 2;
2131
0
  } else if (!(is_inter_block(xd->left_mbmi) &&
2132
0
               is_inter_block(xd->above_mbmi))) {
2133
    // neighbor is INTRA
2134
0
    enable_interp_search = 2;
2135
0
  } else if (xd->left_mbmi->interp_filters.as_int !=
2136
0
             xd->above_mbmi->interp_filters.as_int) {
2137
    // filters are different
2138
0
    enable_interp_search = 2;
2139
0
  } else if ((cb_pred_filter_search == 1) &&
2140
0
             (xd->left_mbmi->interp_filters.as_filters.x_filter !=
2141
0
              EIGHTTAP_REGULAR)) {
2142
    // not regular
2143
0
    enable_interp_search = 2;
2144
0
  } else {
2145
    // enable prediction based on chessboard pattern
2146
0
    if (xd->left_mbmi->interp_filters.as_filters.x_filter == EIGHTTAP_SMOOTH)
2147
0
      *filt_select = EIGHTTAP_SMOOTH;
2148
0
    const int bsl = mi_size_wide_log2[bsize];
2149
0
    enable_interp_search =
2150
0
        (bool)((((mi_row + mi_col) >> bsl) +
2151
0
                get_chessboard_index(cm->current_frame.frame_number)) &
2152
0
               0x1);
2153
0
    if (cyclic_refresh_segment_id_boosted(segment_id)) enable_interp_search = 1;
2154
0
  }
2155
0
  return enable_interp_search;
2156
0
}
2157
2158
static inline int skip_mode_by_threshold(PREDICTION_MODE mode,
2159
                                         MV_REFERENCE_FRAME ref_frame,
2160
                                         int_mv mv, int frames_since_golden,
2161
                                         const int *const rd_threshes,
2162
                                         const int *const rd_thresh_freq_fact,
2163
                                         int64_t best_cost, int best_skip,
2164
0
                                         int extra_shift) {
2165
0
  int skip_this_mode = 0;
2166
0
  const THR_MODES mode_index = mode_idx[ref_frame][INTER_OFFSET(mode)];
2167
0
  int64_t mode_rd_thresh =
2168
0
      best_skip ? ((int64_t)rd_threshes[mode_index]) << (extra_shift + 1)
2169
0
                : ((int64_t)rd_threshes[mode_index]) << extra_shift;
2170
2171
  // Increase mode_rd_thresh value for non-LAST for improved encoding
2172
  // speed
2173
0
  if (ref_frame != LAST_FRAME) {
2174
0
    mode_rd_thresh = mode_rd_thresh << 1;
2175
0
    if (ref_frame == GOLDEN_FRAME && frames_since_golden > 4)
2176
0
      mode_rd_thresh = mode_rd_thresh << (extra_shift + 1);
2177
0
  }
2178
2179
0
  if (rd_less_than_thresh(best_cost, mode_rd_thresh,
2180
0
                          rd_thresh_freq_fact[mode_index]))
2181
0
    if (mv.as_int != 0) skip_this_mode = 1;
2182
2183
0
  return skip_this_mode;
2184
0
}
2185
2186
static inline int skip_mode_by_low_temp(
2187
    PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
2188
0
    CONTENT_STATE_SB content_state_sb, int_mv mv, int force_skip_low_temp_var) {
2189
  // Skip non-zeromv mode search for non-LAST frame if force_skip_low_temp_var
2190
  // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped
2191
  // later.
2192
0
  if (force_skip_low_temp_var && ref_frame != LAST_FRAME && mv.as_int != 0) {
2193
0
    return 1;
2194
0
  }
2195
2196
0
  if (content_state_sb.source_sad_nonrd != kHighSad && bsize >= BLOCK_64X64 &&
2197
0
      force_skip_low_temp_var && mode == NEWMV) {
2198
0
    return 1;
2199
0
  }
2200
0
  return 0;
2201
0
}
2202
2203
static inline int skip_mode_by_bsize_and_ref_frame(
2204
    PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
2205
    int extra_prune, unsigned int sse_zeromv_norm, int more_prune,
2206
0
    int skip_nearmv) {
2207
0
  const unsigned int thresh_skip_golden = 500;
2208
2209
0
  if (ref_frame != LAST_FRAME && sse_zeromv_norm < thresh_skip_golden &&
2210
0
      mode == NEWMV)
2211
0
    return 1;
2212
2213
0
  if ((bsize == BLOCK_128X128 && mode == NEWMV) ||
2214
0
      (skip_nearmv && mode == NEARMV))
2215
0
    return 1;
2216
2217
  // Skip testing non-LAST if this flag is set.
2218
0
  if (extra_prune) {
2219
0
    if (extra_prune > 1 && ref_frame != LAST_FRAME &&
2220
0
        (bsize > BLOCK_16X16 && mode == NEWMV))
2221
0
      return 1;
2222
2223
0
    if (ref_frame != LAST_FRAME && mode == NEARMV) return 1;
2224
2225
0
    if (more_prune && bsize >= BLOCK_32X32 && mode == NEARMV) return 1;
2226
0
  }
2227
0
  return 0;
2228
0
}
2229
2230
static void set_block_source_sad(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
2231
0
                                 struct buf_2d *yv12_mb) {
2232
0
  struct macroblock_plane *const p = &x->plane[0];
2233
0
  const int y_sad = cpi->ppi->fn_ptr[bsize].sdf(p->src.buf, p->src.stride,
2234
0
                                                yv12_mb->buf, yv12_mb->stride);
2235
0
  if (y_sad == 0) x->block_is_zero_sad = 1;
2236
0
}
2237
2238
static void set_color_sensitivity(AV1_COMP *cpi, MACROBLOCK *x,
2239
                                  BLOCK_SIZE bsize, int y_sad,
2240
                                  unsigned int source_variance,
2241
0
                                  struct buf_2d yv12_mb[MAX_MB_PLANE]) {
2242
0
  const int subsampling_x = cpi->common.seq_params->subsampling_x;
2243
0
  const int subsampling_y = cpi->common.seq_params->subsampling_y;
2244
0
  const int source_sad_nonrd = x->content_state_sb.source_sad_nonrd;
2245
0
  const int high_res = cpi->common.width * cpi->common.height >= 640 * 360;
2246
0
  if (bsize == cpi->common.seq_params->sb_size &&
2247
0
      !x->force_color_check_block_level) {
2248
    // At superblock level color_sensitivity is already set to 0, 1, or 2.
2249
    // 2 is middle/uncertain level. To avoid additional sad
2250
    // computations when bsize = sb_size force level 2 to 1 (certain color)
2251
    // for motion areas. Avoid this shortcut if x->force_color_check_block_level
2252
    // is set.
2253
0
    if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 2) {
2254
0
      x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] =
2255
0
          source_sad_nonrd >= kMedSad ? 1 : 0;
2256
0
    }
2257
0
    if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 2) {
2258
0
      x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] =
2259
0
          source_sad_nonrd >= kMedSad ? 1 : 0;
2260
0
    }
2261
0
    return;
2262
0
  }
2263
  // Divide factor for comparing uv_sad to y_sad.
2264
0
  int shift = 3;
2265
  // Threshold for the block spatial source variance.
2266
0
  unsigned int source_var_thr = 50;
2267
  // Thresholds for normalized uv_sad, the first one is used for
2268
  // low source_varaince.
2269
0
  int norm_uv_sad_thresh = 100;
2270
0
  int norm_uv_sad_thresh2 = 40;
2271
0
  if (source_sad_nonrd >= kMedSad && x->source_variance > 0 && high_res)
2272
0
    shift = 4;
2273
0
  if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
2274
0
    if (cpi->rc.high_source_sad) shift = 6;
2275
0
    if (source_sad_nonrd > kMedSad) {
2276
0
      source_var_thr = 1200;
2277
0
      norm_uv_sad_thresh = 10;
2278
0
    }
2279
0
    if (cpi->rc.percent_blocks_with_motion > 90 &&
2280
0
        cpi->rc.frame_source_sad > 10000 && source_sad_nonrd > kLowSad) {
2281
      // Aggressive setting for color_sensitivity for this content.
2282
0
      shift = 10;
2283
0
      norm_uv_sad_thresh = 0;
2284
0
      norm_uv_sad_thresh2 = 0;
2285
0
    }
2286
0
  }
2287
0
  NOISE_LEVEL noise_level = kLow;
2288
0
  int norm_sad =
2289
0
      y_sad >> (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
2290
0
  unsigned int thresh_spatial = (cpi->common.width > 1920) ? 5000 : 1000;
2291
  // If the spatial source variance is high and the normalized y_sad
2292
  // is low, then y-channel is likely good for mode estimation, so keep
2293
  // color_sensitivity off. For low noise content for now, since there is
2294
  // some bdrate regression for noisy color clip.
2295
0
  if (cpi->noise_estimate.enabled)
2296
0
    noise_level = av1_noise_estimate_extract_level(&cpi->noise_estimate);
2297
0
  if (noise_level == kLow && source_variance > thresh_spatial &&
2298
0
      cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN && norm_sad < 50) {
2299
0
    x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] = 0;
2300
0
    x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] = 0;
2301
0
    return;
2302
0
  }
2303
0
  const int num_planes = av1_num_planes(&cpi->common);
2304
2305
0
  for (int plane = AOM_PLANE_U; plane < num_planes; ++plane) {
2306
    // Always check if level = 2. If level = 0 check again for
2307
    // motion areas for higher resolns, where color artifacts
2308
    // are more noticeable. Always check if
2309
    // x->force_color_check_block_level is set.
2310
0
    if (x->color_sensitivity[COLOR_SENS_IDX(plane)] == 2 ||
2311
0
        x->force_color_check_block_level ||
2312
0
        (x->color_sensitivity[COLOR_SENS_IDX(plane)] == 0 &&
2313
0
         source_sad_nonrd >= kMedSad && high_res)) {
2314
0
      struct macroblock_plane *const p = &x->plane[plane];
2315
0
      const BLOCK_SIZE bs =
2316
0
          get_plane_block_size(bsize, subsampling_x, subsampling_y);
2317
2318
0
      const int uv_sad = cpi->ppi->fn_ptr[bs].sdf(
2319
0
          p->src.buf, p->src.stride, yv12_mb[plane].buf, yv12_mb[plane].stride);
2320
2321
0
      const int norm_uv_sad =
2322
0
          uv_sad >> (b_width_log2_lookup[bs] + b_height_log2_lookup[bs]);
2323
0
      x->color_sensitivity[COLOR_SENS_IDX(plane)] =
2324
0
          uv_sad > (y_sad >> shift) && norm_uv_sad > norm_uv_sad_thresh2;
2325
0
      if (source_variance < source_var_thr && norm_uv_sad > norm_uv_sad_thresh)
2326
0
        x->color_sensitivity[COLOR_SENS_IDX(plane)] = 1;
2327
0
    }
2328
0
  }
2329
0
}
2330
2331
static void setup_compound_prediction(const AV1_COMMON *cm, MACROBLOCK *x,
2332
                                      struct buf_2d yv12_mb[8][MAX_MB_PLANE],
2333
                                      const int *use_ref_frame_mask,
2334
                                      const MV_REFERENCE_FRAME *rf,
2335
0
                                      int *ref_mv_idx) {
2336
0
  MACROBLOCKD *const xd = &x->e_mbd;
2337
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
2338
0
  MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
2339
0
  MV_REFERENCE_FRAME ref_frame_comp;
2340
0
  if (!use_ref_frame_mask[rf[1]]) {
2341
    // Need to setup pred_block, if it hasn't been done in find_predictors.
2342
0
    const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, rf[1]);
2343
0
    const int num_planes = av1_num_planes(cm);
2344
0
    if (yv12 != NULL) {
2345
0
      const struct scale_factors *const sf =
2346
0
          get_ref_scale_factors_const(cm, rf[1]);
2347
0
      av1_setup_pred_block(xd, yv12_mb[rf[1]], yv12, sf, sf, num_planes);
2348
0
    }
2349
0
  }
2350
0
  ref_frame_comp = av1_ref_frame_type(rf);
2351
0
  mbmi_ext->mode_context[ref_frame_comp] = 0;
2352
0
  mbmi_ext->ref_mv_count[ref_frame_comp] = UINT8_MAX;
2353
0
  av1_find_mv_refs(cm, xd, mbmi, ref_frame_comp, mbmi_ext->ref_mv_count,
2354
0
                   xd->ref_mv_stack, xd->weight, NULL, mbmi_ext->global_mvs,
2355
0
                   mbmi_ext->mode_context);
2356
0
  av1_copy_usable_ref_mv_stack_and_weight(xd, mbmi_ext, ref_frame_comp);
2357
0
  *ref_mv_idx = mbmi->ref_mv_idx + 1;
2358
0
}
2359
2360
static void set_compound_mode(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame,
2361
                              MV_REFERENCE_FRAME ref_frame2, int ref_mv_idx,
2362
                              int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],
2363
0
                              PREDICTION_MODE this_mode) {
2364
0
  MACROBLOCKD *const xd = &x->e_mbd;
2365
0
  MB_MODE_INFO *const mi = xd->mi[0];
2366
0
  mi->ref_frame[0] = ref_frame;
2367
0
  mi->ref_frame[1] = ref_frame2;
2368
0
  mi->compound_idx = 1;
2369
0
  mi->comp_group_idx = 0;
2370
0
  mi->interinter_comp.type = COMPOUND_AVERAGE;
2371
0
  MV_REFERENCE_FRAME ref_frame_comp = av1_ref_frame_type(mi->ref_frame);
2372
0
  if (this_mode == GLOBAL_GLOBALMV) {
2373
0
    frame_mv[this_mode][ref_frame].as_int = 0;
2374
0
    frame_mv[this_mode][ref_frame2].as_int = 0;
2375
0
  } else if (this_mode == NEAREST_NEARESTMV) {
2376
0
    frame_mv[this_mode][ref_frame].as_int =
2377
0
        xd->ref_mv_stack[ref_frame_comp][0].this_mv.as_int;
2378
0
    frame_mv[this_mode][ref_frame2].as_int =
2379
0
        xd->ref_mv_stack[ref_frame_comp][0].comp_mv.as_int;
2380
0
  } else if (this_mode == NEAR_NEARMV) {
2381
0
    frame_mv[this_mode][ref_frame].as_int =
2382
0
        xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].this_mv.as_int;
2383
0
    frame_mv[this_mode][ref_frame2].as_int =
2384
0
        xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].comp_mv.as_int;
2385
0
  }
2386
0
}
2387
2388
// Prune compound mode if the single mode variance is lower than a fixed
2389
// percentage of the median value.
2390
static bool skip_comp_based_on_var(
2391
0
    const unsigned int (*single_vars)[REF_FRAMES], BLOCK_SIZE bsize) {
2392
0
  unsigned int best_var = UINT_MAX;
2393
0
  for (int cur_mode_idx = 0; cur_mode_idx < RTC_INTER_MODES; cur_mode_idx++) {
2394
0
    for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
2395
0
      best_var = AOMMIN(best_var, single_vars[cur_mode_idx][ref_idx]);
2396
0
    }
2397
0
  }
2398
0
  const unsigned int thresh_64 = (unsigned int)(0.57356805f * 8659);
2399
0
  const unsigned int thresh_32 = (unsigned int)(0.23964763f * 4281);
2400
2401
  // Currently, the thresh for 128 and 16 are not well-tuned. We are using the
2402
  // results from 64 and 32 as an heuristic.
2403
0
  switch (bsize) {
2404
0
    case BLOCK_128X128: return best_var < 4 * thresh_64;
2405
0
    case BLOCK_64X64: return best_var < thresh_64;
2406
0
    case BLOCK_32X32: return best_var < thresh_32;
2407
0
    case BLOCK_16X16: return best_var < thresh_32 / 4;
2408
0
    default: return false;
2409
0
  }
2410
0
}
2411
2412
static AOM_FORCE_INLINE void fill_single_inter_mode_costs(
2413
    int (*single_inter_mode_costs)[REF_FRAMES], int num_inter_modes,
2414
    const REF_MODE *reference_mode_set, const ModeCosts *mode_costs,
2415
0
    const int16_t *mode_context) {
2416
0
  bool ref_frame_used[REF_FRAMES] = { false };
2417
0
  for (int idx = 0; idx < num_inter_modes; idx++) {
2418
0
    ref_frame_used[reference_mode_set[idx].ref_frame] = true;
2419
0
  }
2420
2421
0
  for (int this_ref_frame = LAST_FRAME; this_ref_frame < REF_FRAMES;
2422
0
       this_ref_frame++) {
2423
0
    if (!ref_frame_used[this_ref_frame]) {
2424
0
      continue;
2425
0
    }
2426
2427
0
    const MV_REFERENCE_FRAME rf[2] = { this_ref_frame, NONE_FRAME };
2428
0
    const int16_t mode_ctx = av1_mode_context_analyzer(mode_context, rf);
2429
0
    for (PREDICTION_MODE this_mode = NEARESTMV; this_mode <= NEWMV;
2430
0
         this_mode++) {
2431
0
      single_inter_mode_costs[INTER_OFFSET(this_mode)][this_ref_frame] =
2432
0
          cost_mv_ref(mode_costs, this_mode, mode_ctx);
2433
0
    }
2434
0
  }
2435
0
}
2436
2437
static inline bool is_globalmv_better(
2438
    PREDICTION_MODE this_mode, MV_REFERENCE_FRAME ref_frame, int rate_mv,
2439
    const ModeCosts *mode_costs,
2440
    const int (*single_inter_mode_costs)[REF_FRAMES],
2441
0
    const MB_MODE_INFO_EXT *mbmi_ext) {
2442
0
  const int globalmv_mode_cost =
2443
0
      single_inter_mode_costs[INTER_OFFSET(GLOBALMV)][ref_frame];
2444
0
  int this_mode_cost =
2445
0
      rate_mv + single_inter_mode_costs[INTER_OFFSET(this_mode)][ref_frame];
2446
0
  if (this_mode == NEWMV || this_mode == NEARMV) {
2447
0
    const MV_REFERENCE_FRAME rf[2] = { ref_frame, NONE_FRAME };
2448
0
    this_mode_cost += get_drl_cost(
2449
0
        NEWMV, 0, mbmi_ext, mode_costs->drl_mode_cost0, av1_ref_frame_type(rf));
2450
0
  }
2451
0
  return this_mode_cost > globalmv_mode_cost;
2452
0
}
2453
2454
// Set up the mv/ref_frames etc based on the comp_index. Returns 1 if it
2455
// succeeds, 0 if it fails.
2456
static inline int setup_compound_params_from_comp_idx(
2457
    const AV1_COMP *cpi, MACROBLOCK *x, struct buf_2d yv12_mb[8][MAX_MB_PLANE],
2458
    PREDICTION_MODE *this_mode, MV_REFERENCE_FRAME *ref_frame,
2459
    MV_REFERENCE_FRAME *ref_frame2, int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],
2460
    const int *use_ref_frame_mask, int comp_index,
2461
    bool comp_use_zero_zeromv_only, MV_REFERENCE_FRAME *last_comp_ref_frame,
2462
0
    BLOCK_SIZE bsize) {
2463
0
  const MV_REFERENCE_FRAME *rf =
2464
0
      is_one_pass_rt_lag_params(cpi)
2465
0
          ? comp_ref_mode_set_full[comp_index].ref_frame
2466
0
          : comp_ref_mode_set[comp_index].ref_frame;
2467
0
  int skip_gf = 0;
2468
0
  int skip_alt = 0;
2469
0
  if (x->source_variance < 50 && bsize > BLOCK_16X16) {
2470
0
    if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2471
0
        x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
2472
0
      skip_gf = 1;
2473
0
    if (x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2474
0
        x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
2475
0
      skip_alt = 1;
2476
0
  }
2477
0
  if (comp_use_zero_zeromv_only && *this_mode != GLOBAL_GLOBALMV) {
2478
0
    return 0;
2479
0
  }
2480
0
  if (*ref_frame2 == GOLDEN_FRAME &&
2481
0
      (cpi->sf.rt_sf.ref_frame_comp_nonrd[0] == 0 || skip_gf ||
2482
0
       !(cpi->ref_frame_flags & AOM_GOLD_FLAG))) {
2483
0
    return 0;
2484
0
  } else if (*ref_frame2 == LAST2_FRAME &&
2485
0
             (cpi->sf.rt_sf.ref_frame_comp_nonrd[1] == 0 ||
2486
0
              !(cpi->ref_frame_flags & AOM_LAST2_FLAG))) {
2487
0
    return 0;
2488
0
  } else if (*ref_frame2 == ALTREF_FRAME &&
2489
0
             (cpi->sf.rt_sf.ref_frame_comp_nonrd[2] == 0 || skip_alt ||
2490
0
              !(cpi->ref_frame_flags & AOM_ALT_FLAG))) {
2491
0
    return 0;
2492
0
  }
2493
0
  int ref_mv_idx = 0;
2494
0
  if (*last_comp_ref_frame != rf[1]) {
2495
    // Only needs to be done once per reference pair.
2496
0
    setup_compound_prediction(&cpi->common, x, yv12_mb, use_ref_frame_mask, rf,
2497
0
                              &ref_mv_idx);
2498
0
    *last_comp_ref_frame = rf[1];
2499
0
  }
2500
0
  set_compound_mode(x, *ref_frame, *ref_frame2, ref_mv_idx, frame_mv,
2501
0
                    *this_mode);
2502
0
  if (*this_mode != GLOBAL_GLOBALMV &&
2503
0
      frame_mv[*this_mode][*ref_frame].as_int == 0 &&
2504
0
      frame_mv[*this_mode][*ref_frame2].as_int == 0) {
2505
0
    return 0;
2506
0
  }
2507
2508
0
  return 1;
2509
0
}
2510
2511
static inline bool previous_mode_performed_poorly(
2512
    PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame,
2513
    const unsigned int (*vars)[REF_FRAMES],
2514
0
    const int64_t (*uv_dist)[REF_FRAMES]) {
2515
0
  unsigned int best_var = UINT_MAX;
2516
0
  int64_t best_uv_dist = INT64_MAX;
2517
0
  for (int midx = 0; midx < RTC_INTER_MODES; midx++) {
2518
0
    best_var = AOMMIN(best_var, vars[midx][ref_frame]);
2519
0
    best_uv_dist = AOMMIN(best_uv_dist, uv_dist[midx][ref_frame]);
2520
0
  }
2521
0
  assert(best_var != UINT_MAX && "Invalid variance data.");
2522
0
  const float mult = 1.125f;
2523
0
  bool var_bad = mult * best_var < vars[INTER_OFFSET(mode)][ref_frame];
2524
0
  if (uv_dist[INTER_OFFSET(mode)][ref_frame] < INT64_MAX &&
2525
0
      best_uv_dist != uv_dist[INTER_OFFSET(mode)][ref_frame]) {
2526
    // If we have chroma info, then take it into account
2527
0
    var_bad &= mult * best_uv_dist < uv_dist[INTER_OFFSET(mode)][ref_frame];
2528
0
  }
2529
0
  return var_bad;
2530
0
}
2531
2532
static inline bool prune_compoundmode_with_singlemode_var(
2533
    PREDICTION_MODE compound_mode, MV_REFERENCE_FRAME ref_frame,
2534
    MV_REFERENCE_FRAME ref_frame2, const int_mv (*frame_mv)[REF_FRAMES],
2535
    const uint8_t (*mode_checked)[REF_FRAMES],
2536
    const unsigned int (*vars)[REF_FRAMES],
2537
0
    const int64_t (*uv_dist)[REF_FRAMES]) {
2538
0
  const PREDICTION_MODE single_mode0 = compound_ref0_mode(compound_mode);
2539
0
  const PREDICTION_MODE single_mode1 = compound_ref1_mode(compound_mode);
2540
2541
0
  bool first_ref_valid = false, second_ref_valid = false;
2542
0
  bool first_ref_bad = false, second_ref_bad = false;
2543
0
  if (mode_checked[single_mode0][ref_frame] &&
2544
0
      frame_mv[single_mode0][ref_frame].as_int ==
2545
0
          frame_mv[compound_mode][ref_frame].as_int &&
2546
0
      vars[INTER_OFFSET(single_mode0)][ref_frame] < UINT_MAX) {
2547
0
    first_ref_valid = true;
2548
0
    first_ref_bad =
2549
0
        previous_mode_performed_poorly(single_mode0, ref_frame, vars, uv_dist);
2550
0
  }
2551
0
  if (mode_checked[single_mode1][ref_frame2] &&
2552
0
      frame_mv[single_mode1][ref_frame2].as_int ==
2553
0
          frame_mv[compound_mode][ref_frame2].as_int &&
2554
0
      vars[INTER_OFFSET(single_mode1)][ref_frame2] < UINT_MAX) {
2555
0
    second_ref_valid = true;
2556
0
    second_ref_bad =
2557
0
        previous_mode_performed_poorly(single_mode1, ref_frame2, vars, uv_dist);
2558
0
  }
2559
0
  if (first_ref_valid && second_ref_valid) {
2560
0
    return first_ref_bad && second_ref_bad;
2561
0
  } else if (first_ref_valid || second_ref_valid) {
2562
0
    return first_ref_bad || second_ref_bad;
2563
0
  }
2564
0
  return false;
2565
0
}
2566
2567
// Function to setup parameters used for inter mode evaluation in non-rd.
2568
static AOM_FORCE_INLINE void set_params_nonrd_pick_inter_mode(
2569
    AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
2570
    RD_STATS *rd_cost, int *force_skip_low_temp_var, int mi_row, int mi_col,
2571
    int gf_temporal_ref, unsigned char segment_id, BLOCK_SIZE bsize
2572
#if CONFIG_AV1_TEMPORAL_DENOISING
2573
    ,
2574
    PICK_MODE_CONTEXT *ctx, int denoise_svc_pickmode
2575
#endif
2576
0
) {
2577
0
  AV1_COMMON *const cm = &cpi->common;
2578
0
  MACROBLOCKD *const xd = &x->e_mbd;
2579
0
  TxfmSearchInfo *txfm_info = &x->txfm_search_info;
2580
0
  MB_MODE_INFO *const mi = xd->mi[0];
2581
0
  const ModeCosts *mode_costs = &x->mode_costs;
2582
0
  int skip_pred_mv = 0;
2583
2584
  // Initialize variance and distortion (chroma) for all modes and reference
2585
  // frames
2586
0
  for (int idx = 0; idx < RTC_INTER_MODES; idx++) {
2587
0
    for (int ref = 0; ref < REF_FRAMES; ref++) {
2588
0
      search_state->vars[idx][ref] = UINT_MAX;
2589
0
      search_state->uv_dist[idx][ref] = INT64_MAX;
2590
0
    }
2591
0
  }
2592
2593
  // Initialize values of color sensitivity with sb level color sensitivity
2594
0
  av1_copy(x->color_sensitivity, x->color_sensitivity_sb);
2595
2596
0
  init_best_pickmode(&search_state->best_pickmode);
2597
2598
  // Estimate cost for single reference frames
2599
0
  estimate_single_ref_frame_costs(cm, xd, mode_costs, segment_id, bsize,
2600
0
                                  search_state->ref_costs_single,
2601
0
                                  is_one_pass_rt_lag_params(cpi));
2602
2603
  // Reset flag to indicate modes evaluated
2604
0
  av1_zero(search_state->mode_checked);
2605
2606
0
  txfm_info->skip_txfm = 0;
2607
2608
  // Initialize mode decisions
2609
0
  av1_invalid_rd_stats(&search_state->best_rdc);
2610
0
  av1_invalid_rd_stats(&search_state->this_rdc);
2611
0
  av1_invalid_rd_stats(rd_cost);
2612
0
  for (int ref_idx = 0; ref_idx < REF_FRAMES; ++ref_idx) {
2613
0
    x->warp_sample_info[ref_idx].num = -1;
2614
0
  }
2615
2616
0
  mi->bsize = bsize;
2617
0
  mi->ref_frame[0] = NONE_FRAME;
2618
0
  mi->ref_frame[1] = NONE_FRAME;
2619
2620
#if CONFIG_AV1_TEMPORAL_DENOISING
2621
  if (cpi->oxcf.noise_sensitivity > 0) {
2622
    // if (cpi->ppi->use_svc) denoise_svc_pickmode =
2623
    // av1_denoise_svc_non_key(cpi);
2624
    if (cpi->denoiser.denoising_level > kDenLowLow && denoise_svc_pickmode)
2625
      av1_denoiser_reset_frame_stats(ctx);
2626
  }
2627
#endif
2628
2629
  // Populate predicated motion vectors for LAST_FRAME
2630
0
  if (cpi->ref_frame_flags & AOM_LAST_FLAG) {
2631
0
    find_predictors(cpi, x, LAST_FRAME, search_state->frame_mv,
2632
0
                    search_state->yv12_mb, bsize, *force_skip_low_temp_var,
2633
0
                    x->force_zeromv_skip_for_blk,
2634
0
                    &search_state->use_scaled_ref_frame[LAST_FRAME]);
2635
0
  }
2636
  // Update mask to use all reference frame
2637
0
  if (!is_one_pass_rt_lag_params(cpi)) {
2638
0
    get_ref_frame_use_mask(cpi, x, mi, mi_row, mi_col, bsize, gf_temporal_ref,
2639
0
                           search_state->use_ref_frame_mask,
2640
0
                           force_skip_low_temp_var);
2641
0
  } else {
2642
0
    for (MV_REFERENCE_FRAME ref_frame_iter = LAST_FRAME;
2643
0
         ref_frame_iter <= ALTREF_FRAME; ++ref_frame_iter) {
2644
0
      search_state->use_ref_frame_mask[ref_frame_iter] =
2645
0
          cpi->ref_frame_flags & (1 << (ref_frame_iter - 1));
2646
0
    }
2647
0
  }
2648
2649
0
  skip_pred_mv = x->force_zeromv_skip_for_blk ||
2650
0
                 (x->nonrd_prune_ref_frame_search > 2 &&
2651
0
                  x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] != 2 &&
2652
0
                  x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] != 2);
2653
2654
  // Populate predicated motion vectors for other single reference frame
2655
  // Start at LAST_FRAME + 1.
2656
0
  for (MV_REFERENCE_FRAME ref_frame_iter = LAST_FRAME + 1;
2657
0
       ref_frame_iter <= ALTREF_FRAME; ++ref_frame_iter) {
2658
0
    if (search_state->use_ref_frame_mask[ref_frame_iter]) {
2659
0
      find_predictors(cpi, x, ref_frame_iter, search_state->frame_mv,
2660
0
                      search_state->yv12_mb, bsize, *force_skip_low_temp_var,
2661
0
                      skip_pred_mv,
2662
0
                      &search_state->use_scaled_ref_frame[ref_frame_iter]);
2663
0
    }
2664
0
  }
2665
0
}
2666
2667
// Function to check the inter mode can be skipped based on mode statistics and
2668
// speed features settings.
2669
static AOM_FORCE_INLINE bool skip_inter_mode_nonrd(
2670
    AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
2671
    int64_t *thresh_sad_pred, int *force_mv_inter_layer, int *is_single_pred,
2672
    PREDICTION_MODE *this_mode, MV_REFERENCE_FRAME *last_comp_ref_frame,
2673
    MV_REFERENCE_FRAME *ref_frame, MV_REFERENCE_FRAME *ref_frame2, int idx,
2674
    int_mv svc_mv, int force_skip_low_temp_var, unsigned int sse_zeromv_norm,
2675
    int num_inter_modes, unsigned char segment_id, BLOCK_SIZE bsize,
2676
0
    bool comp_use_zero_zeromv_only, bool check_globalmv) {
2677
0
  AV1_COMMON *const cm = &cpi->common;
2678
0
  const struct segmentation *const seg = &cm->seg;
2679
0
  const SVC *const svc = &cpi->svc;
2680
0
  MACROBLOCKD *const xd = &x->e_mbd;
2681
0
  MB_MODE_INFO *const mi = xd->mi[0];
2682
0
  const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
2683
2684
  // Skip compound mode based on reference frame mask and type of the mode and
2685
  // for allowed compound modes, setup ref mv stack and reference frame.
2686
0
  if (idx >= num_inter_modes) {
2687
0
    const int comp_index = idx - num_inter_modes;
2688
0
    const COMP_REF_MODE *const comp_modes = is_one_pass_rt_lag_params(cpi)
2689
0
                                                ? comp_ref_mode_set_full
2690
0
                                                : comp_ref_mode_set;
2691
0
    const MV_REFERENCE_FRAME *rf = comp_modes[comp_index].ref_frame;
2692
0
    *this_mode = comp_modes[comp_index].pred_mode;
2693
0
    *ref_frame = rf[0];
2694
0
    *ref_frame2 = rf[1];
2695
0
    assert(*ref_frame == LAST_FRAME);
2696
0
    assert(*this_mode == GLOBAL_GLOBALMV || *this_mode == NEAREST_NEARESTMV);
2697
0
    if (!setup_compound_params_from_comp_idx(
2698
0
            cpi, x, search_state->yv12_mb, this_mode, ref_frame, ref_frame2,
2699
0
            search_state->frame_mv, search_state->use_ref_frame_mask,
2700
0
            comp_index, comp_use_zero_zeromv_only, last_comp_ref_frame,
2701
0
            bsize)) {
2702
0
      return true;
2703
0
    }
2704
0
    *is_single_pred = 0;
2705
0
  } else {
2706
0
    *ref_frame2 = NONE_FRAME;
2707
0
    const REF_MODE *const single_modes =
2708
0
        is_one_pass_rt_lag_params(cpi) ? ref_mode_set_full : ref_mode_set;
2709
0
    *this_mode = single_modes[idx].pred_mode;
2710
0
    *ref_frame = single_modes[idx].ref_frame;
2711
0
  }
2712
2713
0
  if (is_one_pass_rt_lag_params(cpi) && cpi->rc.is_src_frame_alt_ref) {
2714
0
    if (*this_mode != GLOBALMV || *ref_frame != ALTREF_FRAME) return true;
2715
0
  }
2716
2717
0
  if (cpi->sf.rt_sf.skip_newmv_mode_sad_screen && cpi->rc.high_source_sad &&
2718
0
      x->content_state_sb.source_sad_nonrd >= kMedSad && bsize <= BLOCK_16X16 &&
2719
0
      !x->sb_me_block && (*ref_frame != LAST_FRAME || *this_mode == NEWMV))
2720
0
    return true;
2721
2722
0
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) &&
2723
0
      (*this_mode != GLOBALMV || *ref_frame != LAST_FRAME))
2724
0
    return true;
2725
2726
  // Skip the mode if use reference frame mask flag is not set.
2727
0
  if (!search_state->use_ref_frame_mask[*ref_frame]) return true;
2728
2729
0
  *force_mv_inter_layer = 0;
2730
0
  if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
2731
0
      ((*ref_frame == LAST_FRAME && svc->skip_mvsearch_last) ||
2732
0
       (*ref_frame == GOLDEN_FRAME && svc->skip_mvsearch_gf) ||
2733
0
       (*ref_frame == ALTREF_FRAME && svc->skip_mvsearch_altref))) {
2734
    // Only test mode if NEARESTMV/NEARMV is (svc_mv.mv.col, svc_mv.mv.row),
2735
    // otherwise set NEWMV to (svc_mv.mv.col, svc_mv.mv.row).
2736
    // Skip newmv and filter search.
2737
0
    *force_mv_inter_layer = 1;
2738
0
    if (*this_mode == NEWMV) {
2739
0
      search_state->frame_mv[*this_mode][*ref_frame] = svc_mv;
2740
0
    } else if (search_state->frame_mv[*this_mode][*ref_frame].as_int !=
2741
0
               svc_mv.as_int) {
2742
0
      return true;
2743
0
    }
2744
0
  }
2745
2746
  // If the segment reference frame feature is enabled then do nothing if the
2747
  // current ref frame is not allowed.
2748
0
  if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
2749
0
    if (get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)(*ref_frame))
2750
0
      return true;
2751
0
    return false;
2752
0
  }
2753
2754
  // Don't skip non_last references if LAST is not used a reference.
2755
0
  if (!(cpi->ref_frame_flags & AOM_LAST_FLAG) &&
2756
0
      (*ref_frame == GOLDEN_FRAME || *ref_frame == ALTREF_FRAME))
2757
0
    return false;
2758
2759
  // Skip mode for some modes and reference frames when
2760
  // force_zeromv_skip_for_blk flag is true.
2761
0
  if (x->force_zeromv_skip_for_blk &&
2762
0
      ((!(*this_mode == NEARESTMV &&
2763
0
          search_state->frame_mv[*this_mode][*ref_frame].as_int == 0) &&
2764
0
        *this_mode != GLOBALMV) ||
2765
0
       *ref_frame != LAST_FRAME))
2766
0
    return true;
2767
2768
0
  if (x->sb_me_block && *ref_frame == LAST_FRAME) {
2769
    // We want to make sure to test the superblock MV:
2770
    // so don't skip (return false) for NEAREST_LAST or NEAR_LAST if they
2771
    // have this sb MV. And don't skip NEWMV_LAST: this will be set to
2772
    // sb MV in handle_inter_mode_nonrd(), in case NEAREST or NEAR don't
2773
    // have it.
2774
0
    if (*this_mode == NEARESTMV &&
2775
0
        search_state->frame_mv[NEARESTMV][LAST_FRAME].as_int ==
2776
0
            x->sb_me_mv.as_int) {
2777
0
      return false;
2778
0
    }
2779
0
    if (*this_mode == NEARMV &&
2780
0
        search_state->frame_mv[NEARMV][LAST_FRAME].as_int ==
2781
0
            x->sb_me_mv.as_int) {
2782
0
      return false;
2783
0
    }
2784
0
    if (*this_mode == NEWMV) {
2785
0
      return false;
2786
0
    }
2787
0
  }
2788
2789
  // Skip the single reference mode for which mode check flag is set.
2790
0
  if (*is_single_pred && search_state->mode_checked[*this_mode][*ref_frame]) {
2791
0
    return true;
2792
0
  }
2793
2794
  // Skip GLOBALMV mode if check_globalmv flag is not enabled.
2795
0
  if (!check_globalmv && *this_mode == GLOBALMV) {
2796
0
    return true;
2797
0
  }
2798
2799
#if COLLECT_NONRD_PICK_MODE_STAT
2800
  aom_usec_timer_start(&x->ms_stat_nonrd.timer1);
2801
  x->ms_stat_nonrd.num_searches[bsize][*this_mode]++;
2802
#endif
2803
0
  mi->mode = *this_mode;
2804
0
  mi->ref_frame[0] = *ref_frame;
2805
0
  mi->ref_frame[1] = *ref_frame2;
2806
2807
  // Skip compound mode based on variance of previously evaluated single
2808
  // reference modes.
2809
0
  if (rt_sf->prune_compoundmode_with_singlemode_var && !*is_single_pred &&
2810
0
      !is_one_pass_rt_lag_params(cpi) &&
2811
0
      prune_compoundmode_with_singlemode_var(
2812
0
          *this_mode, *ref_frame, *ref_frame2, search_state->frame_mv,
2813
0
          search_state->mode_checked, search_state->vars,
2814
0
          search_state->uv_dist)) {
2815
0
    return true;
2816
0
  }
2817
2818
  // For screen content: skip mode testing based on source_sad.
2819
0
  if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
2820
0
      !x->force_zeromv_skip_for_blk) {
2821
    // If source_sad is computed: skip non-zero motion
2822
    // check for stationary (super)blocks. Otherwise if superblock
2823
    // has motion skip the modes with zero motion on last reference
2824
    // for flat blocks, and color is not set.
2825
    // For the latter condition: the same condition should apply
2826
    // to newmv if (0, 0), so this latter condition is repeated
2827
    // below after search_new_mv.
2828
0
    if (rt_sf->source_metrics_sb_nonrd) {
2829
0
      if ((search_state->frame_mv[*this_mode][*ref_frame].as_int != 0 &&
2830
0
           x->content_state_sb.source_sad_nonrd == kZeroSad) ||
2831
0
          (search_state->frame_mv[*this_mode][*ref_frame].as_int == 0 &&
2832
0
           x->block_is_zero_sad == 0 && *ref_frame == LAST_FRAME &&
2833
0
           ((x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
2834
0
             x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) ||
2835
0
            cpi->rc.high_source_sad) &&
2836
0
           x->source_variance == 0))
2837
0
        return true;
2838
0
    }
2839
    // Skip NEWMV search for flat blocks.
2840
0
    if (rt_sf->skip_newmv_flat_blocks_screen && *this_mode == NEWMV &&
2841
0
        x->source_variance < 100)
2842
0
      return true;
2843
    // Skip non-LAST for color on flat blocks.
2844
0
    if (*ref_frame > LAST_FRAME && x->source_variance == 0 &&
2845
0
        (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 ||
2846
0
         x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 1))
2847
0
      return true;
2848
0
  }
2849
2850
0
  if (is_one_pass_rt_lag_params(cpi)) return false;
2851
2852
  // Skip mode based on block size, reference frame mode and other block
2853
  // properties.
2854
0
  if (skip_mode_by_bsize_and_ref_frame(
2855
0
          *this_mode, *ref_frame, bsize, x->nonrd_prune_ref_frame_search,
2856
0
          sse_zeromv_norm, rt_sf->nonrd_aggressive_skip,
2857
0
          rt_sf->increase_source_sad_thresh))
2858
0
    return true;
2859
2860
  // Skip mode based on low temporal variance and souce sad.
2861
0
  if (skip_mode_by_low_temp(*this_mode, *ref_frame, bsize, x->content_state_sb,
2862
0
                            search_state->frame_mv[*this_mode][*ref_frame],
2863
0
                            force_skip_low_temp_var))
2864
0
    return true;
2865
2866
  // Disable this drop out case if the ref frame segment level feature is
2867
  // enabled for this segment. This is to prevent the possibility that we
2868
  // end up unable to pick any mode.
2869
0
  if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
2870
    // Check for skipping GOLDEN and ALTREF based pred_mv_sad.
2871
0
    if (rt_sf->nonrd_prune_ref_frame_search > 0 &&
2872
0
        x->pred_mv_sad[*ref_frame] != INT_MAX && *ref_frame != LAST_FRAME) {
2873
0
      if ((int64_t)(x->pred_mv_sad[*ref_frame]) > *thresh_sad_pred) return true;
2874
0
    }
2875
0
  }
2876
2877
  // Check for skipping NEARMV based on pred_mv_sad.
2878
0
  if (*this_mode == NEARMV && x->pred_mv1_sad[*ref_frame] != INT_MAX &&
2879
0
      x->pred_mv1_sad[*ref_frame] > (x->pred_mv0_sad[*ref_frame] << 1))
2880
0
    return true;
2881
2882
  // Skip single reference mode based on rd threshold.
2883
0
  if (*is_single_pred) {
2884
0
    if (skip_mode_by_threshold(
2885
0
            *this_mode, *ref_frame,
2886
0
            search_state->frame_mv[*this_mode][*ref_frame],
2887
0
            cpi->rc.frames_since_golden, cpi->rd.threshes[segment_id][bsize],
2888
0
            x->thresh_freq_fact[bsize], search_state->best_rdc.rdcost,
2889
0
            search_state->best_pickmode.best_mode_skip_txfm,
2890
0
            (rt_sf->nonrd_aggressive_skip ? 1 : 0)))
2891
0
      return true;
2892
0
  }
2893
0
  return false;
2894
0
}
2895
2896
// Function to perform inter mode evaluation for non-rd
2897
static AOM_FORCE_INLINE bool handle_inter_mode_nonrd(
2898
    AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
2899
    PICK_MODE_CONTEXT *ctx, PRED_BUFFER **this_mode_pred,
2900
    PRED_BUFFER *tmp_buffer, InterPredParams inter_pred_params_sr,
2901
    int *best_early_term, unsigned int *sse_zeromv_norm, bool *check_globalmv,
2902
#if CONFIG_AV1_TEMPORAL_DENOISING
2903
    int64_t *zero_last_cost_orig, int denoise_svc_pickmode,
2904
#endif
2905
    int idx, int force_mv_inter_layer, int is_single_pred, int gf_temporal_ref,
2906
    int use_model_yrd_large, int filter_search_enabled_blk, BLOCK_SIZE bsize,
2907
    PREDICTION_MODE this_mode, InterpFilter filt_select,
2908
    int cb_pred_filter_search, int reuse_inter_pred,
2909
0
    int *sb_me_has_been_tested) {
2910
0
  AV1_COMMON *const cm = &cpi->common;
2911
0
  MACROBLOCKD *const xd = &x->e_mbd;
2912
0
  MB_MODE_INFO *const mi = xd->mi[0];
2913
0
  const MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
2914
0
  const int mi_row = xd->mi_row;
2915
0
  const int mi_col = xd->mi_col;
2916
0
  struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
2917
0
  const int bw = block_size_wide[bsize];
2918
0
  const InterpFilter filter_ref = cm->features.interp_filter;
2919
0
  const InterpFilter default_interp_filter = EIGHTTAP_REGULAR;
2920
0
  TxfmSearchInfo *txfm_info = &x->txfm_search_info;
2921
0
  const ModeCosts *mode_costs = &x->mode_costs;
2922
0
  const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
2923
0
  BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode;
2924
2925
0
  MV_REFERENCE_FRAME ref_frame = mi->ref_frame[0];
2926
0
  MV_REFERENCE_FRAME ref_frame2 = mi->ref_frame[1];
2927
0
  int_mv *const this_mv = &search_state->frame_mv[this_mode][ref_frame];
2928
0
  unsigned int var = UINT_MAX;
2929
0
  int this_early_term = 0;
2930
0
  int rate_mv = 0;
2931
0
  int is_skippable;
2932
0
  int skip_this_mv = 0;
2933
0
  unsigned int var_threshold = UINT_MAX;
2934
0
  PREDICTION_MODE this_best_mode;
2935
0
  RD_STATS nonskip_rdc;
2936
0
  av1_invalid_rd_stats(&nonskip_rdc);
2937
2938
0
  (void)ctx;
2939
2940
0
  if (x->sb_me_block && this_mode == NEWMV && ref_frame == LAST_FRAME) {
2941
    // Set the NEWMV_LAST to the sb MV.
2942
0
    search_state->frame_mv[NEWMV][LAST_FRAME].as_int = x->sb_me_mv.as_int;
2943
0
  } else if (this_mode == NEWMV && !force_mv_inter_layer) {
2944
#if COLLECT_NONRD_PICK_MODE_STAT
2945
    aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
2946
#endif
2947
    // Find the best motion vector for single/compound mode.
2948
0
    const bool skip_newmv = search_new_mv(
2949
0
        cpi, x, search_state->frame_mv, ref_frame, gf_temporal_ref, bsize,
2950
0
        mi_row, mi_col, &rate_mv, &search_state->best_rdc);
2951
#if COLLECT_NONRD_PICK_MODE_STAT
2952
    aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
2953
    x->ms_stat_nonrd.ms_time[bsize][this_mode] +=
2954
        aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
2955
#endif
2956
    // Skip NEWMV mode,
2957
    //   (i). For bsize smaller than 16X16
2958
    //  (ii). Based on sad of the predicted mv w.r.t LAST_FRAME
2959
    // (iii). When motion vector is same as that of reference mv
2960
0
    if (skip_newmv) {
2961
0
      return true;
2962
0
    }
2963
0
  }
2964
2965
  // Check the current motion vector is same as that of previously evaluated
2966
  // motion vectors.
2967
0
  for (PREDICTION_MODE inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV;
2968
0
       inter_mv_mode++) {
2969
0
    if (inter_mv_mode == this_mode) continue;
2970
0
    if (is_single_pred &&
2971
0
        search_state->mode_checked[inter_mv_mode][ref_frame] &&
2972
0
        this_mv->as_int ==
2973
0
            search_state->frame_mv[inter_mv_mode][ref_frame].as_int) {
2974
0
      skip_this_mv = 1;
2975
0
      break;
2976
0
    }
2977
0
  }
2978
2979
  // Skip single mode if current motion vector is same that of previously
2980
  // evaluated motion vectors.
2981
0
  if (skip_this_mv && is_single_pred) return true;
2982
2983
  // For screen: for spatially flat blocks with non-zero motion,
2984
  // skip newmv if the motion vector is (0, 0)-LAST, and color is not set.
2985
0
  if (this_mode == NEWMV && cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
2986
0
      cpi->svc.spatial_layer_id == 0 && rt_sf->source_metrics_sb_nonrd) {
2987
0
    if (this_mv->as_int == 0 && ref_frame == LAST_FRAME &&
2988
0
        x->block_is_zero_sad == 0 &&
2989
0
        ((x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 &&
2990
0
          x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) ||
2991
0
         cpi->rc.high_source_sad) &&
2992
0
        x->source_variance == 0)
2993
0
      return true;
2994
0
  }
2995
2996
0
  mi->mode = this_mode;
2997
0
  mi->mv[0].as_int = this_mv->as_int;
2998
0
  mi->mv[1].as_int = 0;
2999
0
  if (!is_single_pred)
3000
0
    mi->mv[1].as_int = search_state->frame_mv[this_mode][ref_frame2].as_int;
3001
3002
  // Set buffers to store predicted samples for reuse
3003
0
  if (reuse_inter_pred) {
3004
0
    if (!*this_mode_pred) {
3005
0
      *this_mode_pred = &tmp_buffer[3];
3006
0
    } else {
3007
0
      *this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
3008
0
      pd->dst.buf = (*this_mode_pred)->data;
3009
0
      pd->dst.stride = bw;
3010
0
    }
3011
0
  }
3012
3013
0
  mi->motion_mode = SIMPLE_TRANSLATION;
3014
0
#if !CONFIG_REALTIME_ONLY
3015
0
  if (cpi->oxcf.motion_mode_cfg.allow_warped_motion) {
3016
0
    calc_num_proj_ref(cpi, x, mi);
3017
0
  }
3018
0
#endif
3019
  // set variance threshold for compound mode pruning
3020
0
  if (rt_sf->prune_compoundmode_with_singlecompound_var && !is_single_pred &&
3021
0
      use_model_yrd_large) {
3022
0
    const PREDICTION_MODE single_mode0 = compound_ref0_mode(this_mode);
3023
0
    const PREDICTION_MODE single_mode1 = compound_ref1_mode(this_mode);
3024
0
    var_threshold =
3025
0
        AOMMIN(var_threshold,
3026
0
               search_state->vars[INTER_OFFSET(single_mode0)][ref_frame]);
3027
0
    var_threshold =
3028
0
        AOMMIN(var_threshold,
3029
0
               search_state->vars[INTER_OFFSET(single_mode1)][ref_frame2]);
3030
0
  }
3031
3032
  // decide interpolation filter, build prediction signal, get sse
3033
0
  const bool is_mv_subpel =
3034
0
      (mi->mv[0].as_mv.row & 0x07) || (mi->mv[0].as_mv.col & 0x07);
3035
0
  const bool enable_filt_search_this_mode =
3036
0
      (filter_search_enabled_blk == 2)
3037
0
          ? true
3038
0
          : (filter_search_enabled_blk && !force_mv_inter_layer &&
3039
0
             is_single_pred &&
3040
0
             (ref_frame == LAST_FRAME || !x->nonrd_prune_ref_frame_search));
3041
0
  if (is_mv_subpel && enable_filt_search_this_mode) {
3042
#if COLLECT_NONRD_PICK_MODE_STAT
3043
    aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
3044
#endif
3045
0
    search_filter_ref(
3046
0
        cpi, x, &search_state->this_rdc, &inter_pred_params_sr, mi_row, mi_col,
3047
0
        tmp_buffer, bsize, reuse_inter_pred, this_mode_pred, &this_early_term,
3048
0
        &var, use_model_yrd_large, best_pickmode->best_sse, is_single_pred);
3049
#if COLLECT_NONRD_PICK_MODE_STAT
3050
    aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
3051
    x->ms_stat_nonrd.ifs_time[bsize][this_mode] +=
3052
        aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
3053
#endif
3054
0
#if !CONFIG_REALTIME_ONLY
3055
0
  } else if (cpi->oxcf.motion_mode_cfg.allow_warped_motion &&
3056
0
             this_mode == NEWMV) {
3057
    // Find the best motion mode when current mode is NEWMV
3058
0
    search_motion_mode(cpi, x, &search_state->this_rdc, mi_row, mi_col, bsize,
3059
0
                       &this_early_term, use_model_yrd_large, &rate_mv,
3060
0
                       best_pickmode->best_sse);
3061
0
    if (this_mode == NEWMV) {
3062
0
      this_mv[0] = mi->mv[0];
3063
0
    }
3064
0
#endif
3065
0
  } else {
3066
0
    mi->interp_filters =
3067
0
        (filter_ref == SWITCHABLE)
3068
0
            ? av1_broadcast_interp_filter(default_interp_filter)
3069
0
            : av1_broadcast_interp_filter(filter_ref);
3070
0
    if (force_mv_inter_layer)
3071
0
      mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
3072
3073
    // If it is sub-pel motion and cb_pred_filter_search is enabled, select
3074
    // the pre-decided filter
3075
0
    if (is_mv_subpel && cb_pred_filter_search)
3076
0
      mi->interp_filters = av1_broadcast_interp_filter(filt_select);
3077
3078
#if COLLECT_NONRD_PICK_MODE_STAT
3079
    aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
3080
#endif
3081
0
    if (is_single_pred) {
3082
0
      SubpelParams subpel_params;
3083
      // Initialize inter mode level params for single reference mode.
3084
0
      init_inter_mode_params(&mi->mv[0].as_mv, &inter_pred_params_sr,
3085
0
                             &subpel_params, xd->block_ref_scale_factors[0],
3086
0
                             pd->pre->width, pd->pre->height);
3087
0
      av1_enc_build_inter_predictor_y_nonrd(xd, &inter_pred_params_sr,
3088
0
                                            &subpel_params);
3089
0
    } else {
3090
0
      av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3091
0
                                    AOM_PLANE_Y, AOM_PLANE_Y);
3092
0
    }
3093
3094
0
    if (use_model_yrd_large) {
3095
0
      model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
3096
0
                                &search_state->this_rdc, &this_early_term, 0,
3097
0
                                best_pickmode->best_sse, &var, var_threshold);
3098
0
    } else {
3099
0
      model_rd_for_sb_y(cpi, bsize, x, xd, &search_state->this_rdc, &var, 0,
3100
0
                        &this_early_term);
3101
0
    }
3102
#if COLLECT_NONRD_PICK_MODE_STAT
3103
    aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
3104
    x->ms_stat_nonrd.model_rd_time[bsize][this_mode] +=
3105
        aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
3106
#endif
3107
0
  }
3108
3109
  // update variance for single mode
3110
0
  if (is_single_pred) {
3111
0
    search_state->vars[INTER_OFFSET(this_mode)][ref_frame] = var;
3112
0
    if (this_mv->as_int == 0) {
3113
0
      search_state->vars[INTER_OFFSET(GLOBALMV)][ref_frame] = var;
3114
0
    }
3115
0
  }
3116
  // prune compound mode based on single mode var threshold
3117
0
  if (!is_single_pred && var > var_threshold) {
3118
0
    if (reuse_inter_pred) free_pred_buffer(*this_mode_pred);
3119
0
    return true;
3120
0
  }
3121
3122
0
  if (ref_frame == LAST_FRAME && this_mv->as_int == 0) {
3123
0
    *sse_zeromv_norm = (unsigned int)(search_state->this_rdc.sse >>
3124
0
                                      (b_width_log2_lookup[bsize] +
3125
0
                                       b_height_log2_lookup[bsize]));
3126
0
  }
3127
3128
  // Perform early termination based on sse.
3129
0
  if (rt_sf->sse_early_term_inter_search &&
3130
0
      early_term_inter_search_with_sse(rt_sf->sse_early_term_inter_search,
3131
0
                                       bsize, search_state->this_rdc.sse,
3132
0
                                       best_pickmode->best_sse, this_mode)) {
3133
0
    if (reuse_inter_pred) free_pred_buffer(*this_mode_pred);
3134
0
    return true;
3135
0
  }
3136
3137
#if COLLECT_NONRD_PICK_MODE_STAT
3138
  x->ms_stat_nonrd.num_nonskipped_searches[bsize][this_mode]++;
3139
#endif
3140
3141
0
  const int skip_ctx = av1_get_skip_txfm_context(xd);
3142
0
  const int skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][1];
3143
0
  const int no_skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][0];
3144
0
  const int64_t sse_y = search_state->this_rdc.sse;
3145
3146
0
  if (this_early_term) {
3147
0
    search_state->this_rdc.skip_txfm = 1;
3148
0
    search_state->this_rdc.rate = skip_txfm_cost;
3149
0
    search_state->this_rdc.dist = search_state->this_rdc.sse << 4;
3150
0
  } else {
3151
#if COLLECT_NONRD_PICK_MODE_STAT
3152
    aom_usec_timer_start(&x->ms_stat_nonrd.timer2);
3153
#endif
3154
    // Calculates RD Cost using Hadamard transform.
3155
0
    av1_block_yrd(x, &search_state->this_rdc, &is_skippable, bsize,
3156
0
                  mi->tx_size);
3157
0
    if (search_state->this_rdc.skip_txfm ||
3158
0
        RDCOST(x->rdmult, search_state->this_rdc.rate,
3159
0
               search_state->this_rdc.dist) >=
3160
0
            RDCOST(x->rdmult, 0, search_state->this_rdc.sse)) {
3161
0
      if (!search_state->this_rdc.skip_txfm) {
3162
        // Need to store "real" rdc for possible future use if UV rdc
3163
        // disallows tx skip
3164
0
        nonskip_rdc = search_state->this_rdc;
3165
0
        nonskip_rdc.rate += no_skip_txfm_cost;
3166
0
      }
3167
0
      search_state->this_rdc.rate = skip_txfm_cost;
3168
0
      search_state->this_rdc.skip_txfm = 1;
3169
0
      search_state->this_rdc.dist = search_state->this_rdc.sse;
3170
0
    } else {
3171
0
      search_state->this_rdc.rate += no_skip_txfm_cost;
3172
0
    }
3173
3174
    // Populate predicted sample for chroma planes based on color sensitivity.
3175
0
    if ((x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3176
0
         x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])) {
3177
0
      RD_STATS rdc_uv;
3178
0
      const BLOCK_SIZE uv_bsize =
3179
0
          get_plane_block_size(bsize, xd->plane[AOM_PLANE_U].subsampling_x,
3180
0
                               xd->plane[AOM_PLANE_U].subsampling_y);
3181
0
      if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)]) {
3182
0
        av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3183
0
                                      AOM_PLANE_U, AOM_PLANE_U);
3184
0
      }
3185
0
      if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) {
3186
0
        av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3187
0
                                      AOM_PLANE_V, AOM_PLANE_V);
3188
0
      }
3189
      // Compute sse for chroma planes.
3190
0
      const int64_t sse_uv = av1_model_rd_for_sb_uv(
3191
0
          cpi, uv_bsize, x, xd, &rdc_uv, AOM_PLANE_U, AOM_PLANE_V);
3192
0
      if (rdc_uv.dist < x->min_dist_inter_uv)
3193
0
        x->min_dist_inter_uv = rdc_uv.dist;
3194
0
      search_state->this_rdc.sse += sse_uv;
3195
      // Restore Y rdc if UV rdc disallows txfm skip
3196
0
      if (search_state->this_rdc.skip_txfm && !rdc_uv.skip_txfm &&
3197
0
          nonskip_rdc.rate != INT_MAX)
3198
0
        search_state->this_rdc = nonskip_rdc;
3199
0
      if (is_single_pred) {
3200
0
        search_state->uv_dist[INTER_OFFSET(this_mode)][ref_frame] = rdc_uv.dist;
3201
0
      }
3202
0
      search_state->this_rdc.rate += rdc_uv.rate;
3203
0
      search_state->this_rdc.dist += rdc_uv.dist;
3204
0
      search_state->this_rdc.skip_txfm =
3205
0
          search_state->this_rdc.skip_txfm && rdc_uv.skip_txfm;
3206
0
    }
3207
#if COLLECT_NONRD_PICK_MODE_STAT
3208
    aom_usec_timer_mark(&x->ms_stat_nonrd.timer2);
3209
    x->ms_stat_nonrd.txfm_time[bsize][this_mode] +=
3210
        aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2);
3211
#endif
3212
0
  }
3213
3214
0
  this_best_mode = this_mode;
3215
  // TODO(kyslov) account for UV prediction cost
3216
0
  search_state->this_rdc.rate += rate_mv;
3217
0
  if (!is_single_pred) {
3218
0
    const int16_t mode_ctx =
3219
0
        av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame);
3220
0
    search_state->this_rdc.rate += cost_mv_ref(mode_costs, this_mode, mode_ctx);
3221
0
  } else {
3222
    // If the current mode has zeromv but is not GLOBALMV, compare the rate
3223
    // cost. If GLOBALMV is cheaper, use GLOBALMV instead.
3224
0
    if (this_mode != GLOBALMV &&
3225
0
        this_mv->as_int == search_state->frame_mv[GLOBALMV][ref_frame].as_int) {
3226
0
      if (is_globalmv_better(this_mode, ref_frame, rate_mv, mode_costs,
3227
0
                             search_state->single_inter_mode_costs, mbmi_ext)) {
3228
0
        this_best_mode = GLOBALMV;
3229
0
      }
3230
0
    }
3231
3232
0
    search_state->this_rdc.rate +=
3233
0
        search_state
3234
0
            ->single_inter_mode_costs[INTER_OFFSET(this_best_mode)][ref_frame];
3235
0
  }
3236
3237
0
  if (is_single_pred && this_mv->as_int == 0 && var < UINT_MAX) {
3238
0
    search_state->vars[INTER_OFFSET(GLOBALMV)][ref_frame] = var;
3239
0
  }
3240
3241
0
  search_state->this_rdc.rate += search_state->ref_costs_single[ref_frame];
3242
3243
0
  search_state->this_rdc.rdcost = RDCOST(x->rdmult, search_state->this_rdc.rate,
3244
0
                                         search_state->this_rdc.dist);
3245
0
  if (cpi->oxcf.rc_cfg.mode == AOM_CBR && is_single_pred) {
3246
0
    newmv_diff_bias(xd, this_best_mode, &search_state->this_rdc, bsize,
3247
0
                    search_state->frame_mv[this_best_mode][ref_frame].as_mv.row,
3248
0
                    search_state->frame_mv[this_best_mode][ref_frame].as_mv.col,
3249
0
                    cpi->speed, x->source_variance, x->content_state_sb);
3250
0
  }
3251
3252
#if CONFIG_AV1_TEMPORAL_DENOISING
3253
  if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc_pickmode &&
3254
      cpi->denoiser.denoising_level > kDenLowLow) {
3255
    av1_denoiser_update_frame_stats(mi, sse_y, this_mode, ctx);
3256
    // Keep track of zero_last cost.
3257
    if (ref_frame == LAST_FRAME && this_mv->as_int == 0)
3258
      *zero_last_cost_orig = search_state->this_rdc.rdcost;
3259
  }
3260
#else
3261
0
  (void)(sse_y);
3262
0
#endif
3263
3264
0
  search_state->mode_checked[this_mode][ref_frame] = 1;
3265
0
  search_state->mode_checked[this_best_mode][ref_frame] = 1;
3266
3267
0
  if (*check_globalmv) {
3268
0
    int32_t abs_mv =
3269
0
        abs(search_state->frame_mv[this_best_mode][ref_frame].as_mv.row) +
3270
0
        abs(search_state->frame_mv[this_best_mode][ref_frame].as_mv.col);
3271
    // Early exit check: if the magnitude of this_best_mode's mv is small
3272
    // enough, we skip GLOBALMV check in the next loop iteration.
3273
0
    if (abs_mv < 2) {
3274
0
      *check_globalmv = false;
3275
0
    }
3276
0
  }
3277
#if COLLECT_NONRD_PICK_MODE_STAT
3278
  aom_usec_timer_mark(&x->ms_stat_nonrd.timer1);
3279
  x->ms_stat_nonrd.nonskipped_search_times[bsize][this_mode] +=
3280
      aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer1);
3281
#endif
3282
3283
0
  if (x->sb_me_block && ref_frame == LAST_FRAME &&
3284
0
      search_state->frame_mv[this_best_mode][ref_frame].as_int ==
3285
0
          x->sb_me_mv.as_int)
3286
0
    *sb_me_has_been_tested = 1;
3287
3288
  // Copy best mode params to search state
3289
0
  if (search_state->this_rdc.rdcost < search_state->best_rdc.rdcost) {
3290
0
    search_state->best_rdc = search_state->this_rdc;
3291
0
    *best_early_term = this_early_term;
3292
0
    update_search_state_nonrd(search_state, mi, txfm_info, &nonskip_rdc,
3293
0
                              this_best_mode, sse_y);
3294
3295
    // This is needed for the compound modes.
3296
0
    search_state->frame_mv_best[this_best_mode][ref_frame].as_int =
3297
0
        search_state->frame_mv[this_best_mode][ref_frame].as_int;
3298
0
    if (ref_frame2 > NONE_FRAME) {
3299
0
      search_state->frame_mv_best[this_best_mode][ref_frame2].as_int =
3300
0
          search_state->frame_mv[this_best_mode][ref_frame2].as_int;
3301
0
    }
3302
3303
0
    if (reuse_inter_pred) {
3304
0
      free_pred_buffer(best_pickmode->best_pred);
3305
0
      best_pickmode->best_pred = *this_mode_pred;
3306
0
    }
3307
0
  } else {
3308
0
    if (reuse_inter_pred) free_pred_buffer(*this_mode_pred);
3309
0
  }
3310
3311
0
  if (*best_early_term && (idx > 0 || rt_sf->nonrd_aggressive_skip)) {
3312
0
    txfm_info->skip_txfm = 1;
3313
0
    if (!x->sb_me_block || *sb_me_has_been_tested) return false;
3314
0
  }
3315
0
  return true;
3316
0
}
3317
3318
// Function to perform screen content mode evaluation for non-rd
3319
static AOM_FORCE_INLINE void handle_screen_content_mode_nonrd(
3320
    AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state,
3321
    PRED_BUFFER *this_mode_pred, PICK_MODE_CONTEXT *ctx,
3322
    PRED_BUFFER *tmp_buffer, struct buf_2d *orig_dst, int skip_idtx_palette,
3323
    int try_palette, BLOCK_SIZE bsize, int reuse_inter_pred, int mi_col,
3324
0
    int mi_row) {
3325
0
  AV1_COMMON *const cm = &cpi->common;
3326
0
  const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
3327
0
  MACROBLOCKD *const xd = &x->e_mbd;
3328
0
  MB_MODE_INFO *const mi = xd->mi[0];
3329
0
  struct macroblockd_plane *const pd = &xd->plane[0];
3330
0
  const int bw = block_size_wide[bsize];
3331
0
  const int bh = block_size_high[bsize];
3332
0
  BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode;
3333
3334
  // TODO(marpan): Only allow for 8 bit-depth for now, re-enable for 10/12 bit
3335
  // when issue 3359 is fixed.
3336
0
  if (cm->seq_params->bit_depth == 8 && rt_sf->use_idtx_nonrd &&
3337
0
      !skip_idtx_palette && !cpi->oxcf.txfm_cfg.use_inter_dct_only &&
3338
0
      !x->force_zeromv_skip_for_blk &&
3339
0
      is_inter_mode(best_pickmode->best_mode) &&
3340
0
      best_pickmode->best_pred != NULL &&
3341
0
      (!rt_sf->prune_idtx_nonrd ||
3342
0
       (rt_sf->prune_idtx_nonrd && bsize <= BLOCK_32X32 &&
3343
0
        best_pickmode->best_mode_skip_txfm != 1 && x->source_variance > 200))) {
3344
0
    RD_STATS idtx_rdc;
3345
0
    av1_init_rd_stats(&idtx_rdc);
3346
0
    int is_skippable;
3347
0
    this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
3348
0
    pd->dst.buf = this_mode_pred->data;
3349
0
    pd->dst.stride = bw;
3350
0
    const PRED_BUFFER *const best_pred = best_pickmode->best_pred;
3351
0
    av1_block_yrd_idtx(x, best_pred->data, best_pred->stride, &idtx_rdc,
3352
0
                       &is_skippable, bsize, mi->tx_size);
3353
0
    int64_t idx_rdcost_y = RDCOST(x->rdmult, idtx_rdc.rate, idtx_rdc.dist);
3354
0
    int allow_idtx = 1;
3355
    // Incorporate color into rd cost.
3356
0
    if ((x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3357
0
         x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])) {
3358
0
      RD_STATS rdc_uv;
3359
0
      const BLOCK_SIZE uv_bsize =
3360
0
          get_plane_block_size(bsize, xd->plane[AOM_PLANE_U].subsampling_x,
3361
0
                               xd->plane[AOM_PLANE_U].subsampling_y);
3362
0
      if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)]) {
3363
0
        av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3364
0
                                      AOM_PLANE_U, AOM_PLANE_U);
3365
0
      }
3366
0
      if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) {
3367
0
        av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3368
0
                                      AOM_PLANE_V, AOM_PLANE_V);
3369
0
      }
3370
0
      av1_model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, AOM_PLANE_U,
3371
0
                             AOM_PLANE_V);
3372
0
      if (rdc_uv.dist < x->min_dist_inter_uv)
3373
0
        x->min_dist_inter_uv = rdc_uv.dist;
3374
0
      idtx_rdc.rate += rdc_uv.rate;
3375
0
      idtx_rdc.dist += rdc_uv.dist;
3376
0
      idtx_rdc.skip_txfm = idtx_rdc.skip_txfm && rdc_uv.skip_txfm;
3377
0
      if (idx_rdcost_y == 0 && rdc_uv.dist > 0 && x->source_variance < 3000 &&
3378
0
          x->content_state_sb.source_sad_nonrd > kMedSad)
3379
0
        allow_idtx = 0;
3380
0
    }
3381
0
    int64_t idx_rdcost = RDCOST(x->rdmult, idtx_rdc.rate, idtx_rdc.dist);
3382
0
    if (allow_idtx && idx_rdcost < search_state->best_rdc.rdcost) {
3383
0
      best_pickmode->tx_type = IDTX;
3384
0
      search_state->best_rdc.rdcost = idx_rdcost;
3385
0
      best_pickmode->best_mode_skip_txfm = idtx_rdc.skip_txfm;
3386
0
      xd->tx_type_map[0] = best_pickmode->tx_type;
3387
0
      memset(ctx->tx_type_map, best_pickmode->tx_type, ctx->num_4x4_blk);
3388
0
      memset(xd->tx_type_map, best_pickmode->tx_type, ctx->num_4x4_blk);
3389
0
    }
3390
0
    pd->dst = *orig_dst;
3391
0
  }
3392
3393
0
  if (!try_palette) return;
3394
0
  const unsigned int intra_ref_frame_cost =
3395
0
      search_state->ref_costs_single[INTRA_FRAME];
3396
3397
0
  if (!is_mode_intra(best_pickmode->best_mode)) {
3398
0
    PRED_BUFFER *const best_pred = best_pickmode->best_pred;
3399
0
    if (reuse_inter_pred && best_pred != NULL) {
3400
0
      if (best_pred->data == orig_dst->buf) {
3401
0
        this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)];
3402
0
        aom_convolve_copy(best_pred->data, best_pred->stride,
3403
0
                          this_mode_pred->data, this_mode_pred->stride, bw, bh);
3404
0
        best_pickmode->best_pred = this_mode_pred;
3405
0
      }
3406
0
    }
3407
0
    pd->dst = *orig_dst;
3408
0
  }
3409
  // Search palette mode for Luma plane in inter frame.
3410
0
  av1_search_palette_mode_luma(cpi, x, bsize, intra_ref_frame_cost, ctx,
3411
0
                               &search_state->this_rdc,
3412
0
                               search_state->best_rdc.rdcost);
3413
  // Update best mode data in search_state
3414
0
  if (search_state->this_rdc.rdcost < search_state->best_rdc.rdcost) {
3415
0
    best_pickmode->pmi = mi->palette_mode_info;
3416
0
    best_pickmode->best_mode = DC_PRED;
3417
0
    mi->mv[0].as_int = INVALID_MV;
3418
0
    mi->mv[1].as_int = INVALID_MV;
3419
0
    best_pickmode->best_ref_frame = INTRA_FRAME;
3420
0
    best_pickmode->best_second_ref_frame = NONE;
3421
0
    search_state->best_rdc.rate = search_state->this_rdc.rate;
3422
0
    search_state->best_rdc.dist = search_state->this_rdc.dist;
3423
0
    search_state->best_rdc.rdcost = search_state->this_rdc.rdcost;
3424
0
    best_pickmode->best_mode_skip_txfm = search_state->this_rdc.skip_txfm;
3425
    // Keep the skip_txfm off if the color_sensitivity is set.
3426
0
    if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3427
0
        x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])
3428
0
      search_state->this_rdc.skip_txfm = 0;
3429
3430
0
    if (xd->tx_type_map[0] != DCT_DCT)
3431
0
      av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
3432
0
  }
3433
0
}
3434
3435
static inline bool enable_palette(AV1_COMP *cpi, bool is_mode_intra,
3436
                                  BLOCK_SIZE bsize,
3437
                                  unsigned int source_variance,
3438
                                  int force_zeromv_skip, int skip_idtx_palette,
3439
                                  int force_palette_test,
3440
0
                                  unsigned int best_intra_sad_norm) {
3441
0
  const unsigned int sad_thresh =
3442
0
      cpi->sf.rt_sf.prune_palette_search_nonrd > 2
3443
0
          ? (cpi->oxcf.frm_dim_cfg.width * cpi->oxcf.frm_dim_cfg.height <=
3444
0
             1280 * 720)
3445
0
                ? 6
3446
0
                : 12
3447
0
          : 10;
3448
0
  if (!cpi->oxcf.tool_cfg.enable_palette) return false;
3449
0
  if (!av1_allow_palette(cpi->common.features.allow_screen_content_tools,
3450
0
                         bsize)) {
3451
0
    return false;
3452
0
  }
3453
0
  if (skip_idtx_palette) return false;
3454
3455
0
  if (cpi->sf.rt_sf.prune_palette_search_nonrd > 1 &&
3456
0
      ((cpi->rc.high_source_sad && cpi->ppi->rtc_ref.non_reference_frame) ||
3457
0
       bsize > BLOCK_16X16)) {
3458
0
    return false;
3459
0
  }
3460
3461
0
  if (prune_palette_testing_inter(cpi, source_variance) &&
3462
0
      best_intra_sad_norm < sad_thresh)
3463
0
    return false;
3464
3465
0
  if ((is_mode_intra || force_palette_test) && source_variance > 0 &&
3466
0
      !force_zeromv_skip &&
3467
0
      (cpi->rc.high_source_sad || source_variance > 300)) {
3468
0
    return true;
3469
0
  } else {
3470
0
    return false;
3471
0
  }
3472
0
}
3473
3474
/*!\brief AV1 inter mode selection based on Non-RD optimized model.
3475
 *
3476
 * \ingroup nonrd_mode_search
3477
 * \callgraph
3478
 * Top level function for Non-RD optimized inter mode selection.
3479
 * This finction will loop over subset of inter modes and select the best one
3480
 * based on calculated modelled RD cost. While making decisions which modes to
3481
 * check, this function applies heuristics based on previously checked modes,
3482
 * block residual variance, block size, and other factors to prune certain
3483
 * modes and reference frames. Currently only single reference frame modes
3484
 * are checked. Additional heuristics are applied to decide if intra modes
3485
 *  need to be checked.
3486
 *  *
3487
 * \param[in]    cpi            Top-level encoder structure
3488
 * \param[in]    tile_data      Pointer to struct holding adaptive
3489
                                data/contexts/models for the tile during
3490
                                encoding
3491
 * \param[in]    x              Pointer to structure holding all the data for
3492
                                the current macroblock
3493
 * \param[in]    rd_cost        Struct to keep track of the RD information
3494
 * \param[in]    bsize          Current block size
3495
 * \param[in]    ctx            Structure to hold snapshot of coding context
3496
                                during the mode picking process
3497
 *
3498
 * \remark Nothing is returned. Instead, the MB_MODE_INFO struct inside x
3499
 * is modified to store information about the best mode computed
3500
 * in this function. The rd_cost struct is also updated with the RD stats
3501
 * corresponding to the best mode found.
3502
 */
3503
void av1_nonrd_pick_inter_mode_sb(AV1_COMP *cpi, TileDataEnc *tile_data,
3504
                                  MACROBLOCK *x, RD_STATS *rd_cost,
3505
0
                                  BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
3506
0
  AV1_COMMON *const cm = &cpi->common;
3507
0
  SVC *const svc = &cpi->svc;
3508
0
  MACROBLOCKD *const xd = &x->e_mbd;
3509
0
  MB_MODE_INFO *const mi = xd->mi[0];
3510
0
  const struct segmentation *const seg = &cm->seg;
3511
0
  struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
3512
0
  const MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
3513
0
  MV_REFERENCE_FRAME ref_frame, ref_frame2;
3514
0
  const unsigned char segment_id = mi->segment_id;
3515
0
  int best_early_term = 0;
3516
0
  int force_skip_low_temp_var = 0;
3517
0
  unsigned int sse_zeromv_norm = UINT_MAX;
3518
0
  const int num_inter_modes =
3519
0
      is_one_pass_rt_lag_params(cpi) ? NUM_INTER_MODES_FULL : NUM_INTER_MODES;
3520
0
  const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf;
3521
0
  bool check_globalmv = rt_sf->check_globalmv_on_single_ref;
3522
0
  PRED_BUFFER tmp_buffer[4];
3523
0
  DECLARE_ALIGNED(16, uint8_t, pred_buf[MAX_MB_PLANE * MAX_SB_SQUARE]);
3524
0
  PRED_BUFFER *this_mode_pred = NULL;
3525
0
  const int reuse_inter_pred =
3526
0
      rt_sf->reuse_inter_pred_nonrd && cm->seq_params->bit_depth == AOM_BITS_8;
3527
0
  InterModeSearchStateNonrd search_state;
3528
0
  av1_zero(search_state.use_ref_frame_mask);
3529
0
  av1_zero(search_state.use_scaled_ref_frame);
3530
0
  BEST_PICKMODE *const best_pickmode = &search_state.best_pickmode;
3531
0
  (void)tile_data;
3532
3533
0
  const int bh = block_size_high[bsize];
3534
0
  const int bw = block_size_wide[bsize];
3535
0
  const int pixels_in_block = bh * bw;
3536
0
  struct buf_2d orig_dst = pd->dst;
3537
0
  const TxfmSearchParams *txfm_params = &x->txfm_search_params;
3538
0
  TxfmSearchInfo *txfm_info = &x->txfm_search_info;
3539
#if COLLECT_NONRD_PICK_MODE_STAT
3540
  // Mode statistics can be collected only when num_workers is 1
3541
  assert(cpi->mt_info.num_workers <= 1);
3542
  aom_usec_timer_start(&x->ms_stat_nonrd.bsize_timer);
3543
#endif
3544
0
  int64_t thresh_sad_pred = INT64_MAX;
3545
0
  const int mi_row = xd->mi_row;
3546
0
  const int mi_col = xd->mi_col;
3547
0
  int_mv svc_mv = { .as_int = 0 };
3548
0
  int force_mv_inter_layer = 0;
3549
0
  bool comp_use_zero_zeromv_only = 0;
3550
0
  int tot_num_comp_modes = is_one_pass_rt_lag_params(cpi)
3551
0
                               ? NUM_COMP_INTER_MODES_RT_FULL
3552
0
                               : NUM_COMP_INTER_MODES_RT;
3553
#if CONFIG_AV1_TEMPORAL_DENOISING
3554
  const int denoise_recheck_zeromv = 1;
3555
  AV1_PICKMODE_CTX_DEN ctx_den;
3556
  int64_t zero_last_cost_orig = INT64_MAX;
3557
  int denoise_svc_pickmode = 1;
3558
  const int resize_pending = is_frame_resize_pending(cpi);
3559
#endif
3560
0
  const ModeCosts *mode_costs = &x->mode_costs;
3561
0
  struct scale_factors sf_no_scale;
3562
0
  av1_setup_scale_factors_for_frame(&sf_no_scale, cm->width, cm->height,
3563
0
                                    cm->width, cm->height);
3564
0
  if (reuse_inter_pred) {
3565
0
    for (int buf_idx = 0; buf_idx < 3; buf_idx++) {
3566
0
      tmp_buffer[buf_idx].data = &pred_buf[pixels_in_block * buf_idx];
3567
0
      tmp_buffer[buf_idx].stride = bw;
3568
0
      tmp_buffer[buf_idx].in_use = 0;
3569
0
    }
3570
0
    tmp_buffer[3].data = pd->dst.buf;
3571
0
    tmp_buffer[3].stride = pd->dst.stride;
3572
0
    tmp_buffer[3].in_use = 0;
3573
0
  }
3574
3575
0
  const int gf_temporal_ref = is_same_gf_and_last_scale(cm);
3576
3577
  // If the lower spatial layer uses an averaging filter for downsampling
3578
  // (phase = 8), the target decimated pixel is shifted by (1/2, 1/2) relative
3579
  // to source, so use subpel motion vector to compensate. The nonzero motion
3580
  // is half pixel shifted to left and top, so (-4, -4). This has more effect
3581
  // on higher resolutions, so condition it on that for now.
3582
  // Exclude quality layers, which have the same resolution and hence no shift.
3583
0
  if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
3584
0
      !svc->has_lower_quality_layer &&
3585
0
      svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 &&
3586
0
      cm->width * cm->height > 640 * 480) {
3587
0
    svc_mv.as_mv.row = -4;
3588
0
    svc_mv.as_mv.col = -4;
3589
0
  }
3590
3591
  // Setup parameters used for inter mode evaluation.
3592
0
  set_params_nonrd_pick_inter_mode(cpi, x, &search_state, rd_cost,
3593
0
                                   &force_skip_low_temp_var, mi_row, mi_col,
3594
0
                                   gf_temporal_ref, segment_id, bsize
3595
#if CONFIG_AV1_TEMPORAL_DENOISING
3596
                                   ,
3597
                                   ctx, denoise_svc_pickmode
3598
#endif
3599
0
  );
3600
3601
0
  if (rt_sf->use_comp_ref_nonrd && is_comp_ref_allowed(bsize)) {
3602
    // Only search compound if bsize \gt BLOCK_16X16.
3603
0
    if (bsize > BLOCK_16X16) {
3604
0
      comp_use_zero_zeromv_only = rt_sf->check_only_zero_zeromv_on_large_blocks;
3605
0
    } else {
3606
0
      tot_num_comp_modes = 0;
3607
0
    }
3608
0
  } else {
3609
0
    tot_num_comp_modes = 0;
3610
0
  }
3611
3612
  // No compound if SEG_LVL_REF_FRAME is set.
3613
0
  if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME))
3614
0
    tot_num_comp_modes = 0;
3615
3616
0
  if (x->pred_mv_sad[LAST_FRAME] != INT_MAX) {
3617
0
    thresh_sad_pred = ((int64_t)x->pred_mv_sad[LAST_FRAME]) << 1;
3618
    // Increase threshold for less aggressive pruning.
3619
0
    if (rt_sf->nonrd_prune_ref_frame_search == 1)
3620
0
      thresh_sad_pred += (x->pred_mv_sad[LAST_FRAME] >> 2);
3621
0
  }
3622
3623
0
  const int use_model_yrd_large = get_model_rd_flag(cpi, xd, bsize);
3624
3625
  // decide block-level interp filter search flags:
3626
  // filter_search_enabled_blk:
3627
  // 0: disabled
3628
  // 1: filter search depends on mode properties
3629
  // 2: filter search forced since prediction is unreliable
3630
  // cb_pred_filter_search 0: disabled cb prediction
3631
0
  InterpFilter filt_select = EIGHTTAP_REGULAR;
3632
0
  const int cb_pred_filter_search =
3633
0
      x->content_state_sb.source_sad_nonrd > kVeryLowSad
3634
0
          ? cpi->sf.interp_sf.cb_pred_filter_search
3635
0
          : 0;
3636
0
  const int filter_search_enabled_blk =
3637
0
      is_filter_search_enabled_blk(cpi, x, mi_row, mi_col, bsize, segment_id,
3638
0
                                   cb_pred_filter_search, &filt_select);
3639
3640
#if COLLECT_NONRD_PICK_MODE_STAT
3641
  x->ms_stat_nonrd.num_blocks[bsize]++;
3642
#endif
3643
0
  init_mbmi_nonrd(mi, DC_PRED, NONE_FRAME, NONE_FRAME, cm);
3644
0
  mi->tx_size = AOMMIN(
3645
0
      AOMMIN(max_txsize_lookup[bsize],
3646
0
             tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]),
3647
0
      TX_16X16);
3648
3649
0
  if (!is_one_pass_rt_lag_params(cpi))
3650
0
    fill_single_inter_mode_costs(search_state.single_inter_mode_costs,
3651
0
                                 num_inter_modes, ref_mode_set, mode_costs,
3652
0
                                 mbmi_ext->mode_context);
3653
0
  else
3654
0
    fill_single_inter_mode_costs(search_state.single_inter_mode_costs,
3655
0
                                 num_inter_modes, ref_mode_set_full, mode_costs,
3656
0
                                 mbmi_ext->mode_context);
3657
3658
0
  MV_REFERENCE_FRAME last_comp_ref_frame = NONE_FRAME;
3659
3660
  // Initialize inter prediction params at block level for single reference
3661
  // mode.
3662
0
  InterPredParams inter_pred_params_sr;
3663
0
  init_inter_block_params(&inter_pred_params_sr, pd->width, pd->height,
3664
0
                          mi_row * MI_SIZE, mi_col * MI_SIZE, pd->subsampling_x,
3665
0
                          pd->subsampling_y, xd->bd, is_cur_buf_hbd(xd),
3666
0
                          /*is_intrabc=*/0);
3667
0
  inter_pred_params_sr.conv_params =
3668
0
      get_conv_params(/*do_average=*/0, AOM_PLANE_Y, xd->bd);
3669
3670
0
  x->block_is_zero_sad = x->content_state_sb.source_sad_nonrd == kZeroSad ||
3671
0
                         segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
3672
0
  if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
3673
0
      !x->force_zeromv_skip_for_blk &&
3674
0
      x->content_state_sb.source_sad_nonrd != kZeroSad &&
3675
0
      x->source_variance == 0 && bsize < cm->seq_params->sb_size &&
3676
0
      search_state.use_ref_frame_mask[LAST_FRAME] &&
3677
0
      search_state.yv12_mb[LAST_FRAME][0].width == cm->width &&
3678
0
      search_state.yv12_mb[LAST_FRAME][0].height == cm->height) {
3679
0
    set_block_source_sad(cpi, x, bsize, &search_state.yv12_mb[LAST_FRAME][0]);
3680
0
  }
3681
3682
0
  int sb_me_has_been_tested = 0;
3683
0
  x->sb_me_block = x->sb_me_partition;
3684
  // Only use this feature (force testing of superblock motion) if coding
3685
  // block size is large.
3686
0
  if (x->sb_me_block) {
3687
0
    if (cm->seq_params->sb_size == BLOCK_128X128 && bsize < BLOCK_64X64)
3688
0
      x->sb_me_block = 0;
3689
0
    else if (cm->seq_params->sb_size == BLOCK_64X64 && bsize < BLOCK_32X32)
3690
0
      x->sb_me_block = 0;
3691
0
  }
3692
3693
0
  x->min_dist_inter_uv = INT64_MAX;
3694
0
  for (int idx = 0; idx < num_inter_modes + tot_num_comp_modes; ++idx) {
3695
    // If we are at the first compound mode, and the single modes already
3696
    // perform well, then end the search.
3697
0
    if (rt_sf->skip_compound_based_on_var && idx == num_inter_modes &&
3698
0
        skip_comp_based_on_var(search_state.vars, bsize)) {
3699
0
      break;
3700
0
    }
3701
3702
0
    int is_single_pred = 1;
3703
0
    PREDICTION_MODE this_mode;
3704
3705
0
    if (idx == 0 && !x->force_zeromv_skip_for_blk) {
3706
      // Set color sensitivity on first tested mode only.
3707
      // Use y-sad already computed in find_predictors: take the sad with motion
3708
      // vector closest to 0; the uv-sad computed below in set_color_sensitivity
3709
      // is for zeromv.
3710
      // For screen: first check if golden reference is being used, if so,
3711
      // force color_sensitivity on (=1) if the color sensitivity for sb_g is 1.
3712
      // The check in set_color_sensitivity() will then follow and check for
3713
      // setting the flag if the level is still 2 or 0.
3714
0
      if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
3715
0
          search_state.use_ref_frame_mask[GOLDEN_FRAME]) {
3716
0
        if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1)
3717
0
          x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] = 1;
3718
0
        if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)
3719
0
          x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] = 1;
3720
0
      }
3721
0
      if (search_state.use_ref_frame_mask[LAST_FRAME] &&
3722
0
          x->pred_mv0_sad[LAST_FRAME] != INT_MAX) {
3723
0
        int y_sad = x->pred_mv0_sad[LAST_FRAME];
3724
0
        if (x->pred_mv1_sad[LAST_FRAME] != INT_MAX &&
3725
0
            (abs(search_state.frame_mv[NEARMV][LAST_FRAME].as_mv.col) +
3726
0
             abs(search_state.frame_mv[NEARMV][LAST_FRAME].as_mv.row)) <
3727
0
                (abs(search_state.frame_mv[NEARESTMV][LAST_FRAME].as_mv.col) +
3728
0
                 abs(search_state.frame_mv[NEARESTMV][LAST_FRAME].as_mv.row)))
3729
0
          y_sad = x->pred_mv1_sad[LAST_FRAME];
3730
0
        set_color_sensitivity(cpi, x, bsize, y_sad, x->source_variance,
3731
0
                              search_state.yv12_mb[LAST_FRAME]);
3732
0
      }
3733
0
    }
3734
3735
    // Check the inter mode can be skipped based on mode statistics and speed
3736
    // features settings.
3737
0
    if (skip_inter_mode_nonrd(cpi, x, &search_state, &thresh_sad_pred,
3738
0
                              &force_mv_inter_layer, &is_single_pred,
3739
0
                              &this_mode, &last_comp_ref_frame, &ref_frame,
3740
0
                              &ref_frame2, idx, svc_mv, force_skip_low_temp_var,
3741
0
                              sse_zeromv_norm, num_inter_modes, segment_id,
3742
0
                              bsize, comp_use_zero_zeromv_only, check_globalmv))
3743
0
      continue;
3744
3745
    // Select prediction reference frames.
3746
0
    for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
3747
0
      xd->plane[plane].pre[0] = search_state.yv12_mb[ref_frame][plane];
3748
0
      if (!is_single_pred)
3749
0
        xd->plane[plane].pre[1] = search_state.yv12_mb[ref_frame2][plane];
3750
0
    }
3751
3752
0
    mi->ref_frame[0] = ref_frame;
3753
0
    mi->ref_frame[1] = ref_frame2;
3754
0
    set_ref_ptrs(cm, xd, ref_frame, ref_frame2);
3755
3756
    // Check if the scaled reference frame should be used. This is set in the
3757
    // find_predictors() for each usable reference. If so, set the
3758
    // block_ref_scale_factors[] to no reference scaling.
3759
0
    if (search_state.use_scaled_ref_frame[ref_frame]) {
3760
0
      xd->block_ref_scale_factors[0] = &sf_no_scale;
3761
0
    }
3762
0
    if (!is_single_pred && search_state.use_scaled_ref_frame[ref_frame2]) {
3763
0
      xd->block_ref_scale_factors[1] = &sf_no_scale;
3764
0
    }
3765
3766
    // Perform inter mode evaluation for non-rd
3767
0
    if (!handle_inter_mode_nonrd(
3768
0
            cpi, x, &search_state, ctx, &this_mode_pred, tmp_buffer,
3769
0
            inter_pred_params_sr, &best_early_term, &sse_zeromv_norm,
3770
0
            &check_globalmv,
3771
#if CONFIG_AV1_TEMPORAL_DENOISING
3772
            &zero_last_cost_orig, denoise_svc_pickmode,
3773
#endif
3774
0
            idx, force_mv_inter_layer, is_single_pred, gf_temporal_ref,
3775
0
            use_model_yrd_large, filter_search_enabled_blk, bsize, this_mode,
3776
0
            filt_select, cb_pred_filter_search, reuse_inter_pred,
3777
0
            &sb_me_has_been_tested)) {
3778
0
      break;
3779
0
    }
3780
0
  }
3781
3782
  // Restore mode data of best inter mode
3783
0
  mi->mode = best_pickmode->best_mode;
3784
0
  mi->motion_mode = best_pickmode->best_motion_mode;
3785
0
  mi->wm_params = best_pickmode->wm_params;
3786
0
  mi->num_proj_ref = best_pickmode->num_proj_ref;
3787
0
  mi->interp_filters = best_pickmode->best_pred_filter;
3788
0
  mi->tx_size = best_pickmode->best_tx_size;
3789
0
  memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size));
3790
0
  mi->ref_frame[0] = best_pickmode->best_ref_frame;
3791
0
  mi->mv[0].as_int = search_state
3792
0
                         .frame_mv_best[best_pickmode->best_mode]
3793
0
                                       [best_pickmode->best_ref_frame]
3794
0
                         .as_int;
3795
0
  mi->mv[1].as_int = 0;
3796
0
  if (best_pickmode->best_second_ref_frame > INTRA_FRAME) {
3797
0
    mi->ref_frame[1] = best_pickmode->best_second_ref_frame;
3798
0
    mi->mv[1].as_int = search_state
3799
0
                           .frame_mv_best[best_pickmode->best_mode]
3800
0
                                         [best_pickmode->best_second_ref_frame]
3801
0
                           .as_int;
3802
0
  }
3803
  // Perform intra prediction search, if the best SAD is above a certain
3804
  // threshold.
3805
0
  mi->angle_delta[PLANE_TYPE_Y] = 0;
3806
0
  mi->angle_delta[PLANE_TYPE_UV] = 0;
3807
0
  mi->filter_intra_mode_info.use_filter_intra = 0;
3808
3809
#if COLLECT_NONRD_PICK_MODE_STAT
3810
  aom_usec_timer_start(&x->ms_stat_nonrd.timer1);
3811
  x->ms_stat_nonrd.num_searches[bsize][DC_PRED]++;
3812
  x->ms_stat_nonrd.num_nonskipped_searches[bsize][DC_PRED]++;
3813
#endif
3814
3815
0
  int force_palette_test = 0;
3816
0
  if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
3817
0
      x->content_state_sb.source_sad_nonrd != kZeroSad &&
3818
0
      bsize <= BLOCK_16X16) {
3819
0
    unsigned int thresh_sse = cpi->rc.high_source_sad ? 15000 : 100000;
3820
0
    unsigned int thresh_source_var = cpi->rc.high_source_sad ? 50 : 200;
3821
0
    unsigned int best_sse_inter_motion =
3822
0
        (unsigned int)(search_state.best_rdc.sse >>
3823
0
                       (b_width_log2_lookup[bsize] +
3824
0
                        b_height_log2_lookup[bsize]));
3825
0
    if (best_sse_inter_motion > thresh_sse &&
3826
0
        x->source_variance > thresh_source_var)
3827
0
      force_palette_test = 1;
3828
0
  }
3829
3830
  // For the SEG_LVL_REF_FRAME inter_mode must be selected if reference set is
3831
  // not INTRA_FRAME, so skip all intra mode (and palette below).
3832
0
  const int inter_forced_on_segment =
3833
0
      segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
3834
0
      get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
3835
3836
  // Evaluate Intra modes in inter frame
3837
0
  unsigned int best_intra_sad_norm = UINT_MAX;
3838
0
  if (!x->force_zeromv_skip_for_blk && !inter_forced_on_segment)
3839
0
    av1_estimate_intra_mode(cpi, x, bsize, best_early_term,
3840
0
                            search_state.ref_costs_single[INTRA_FRAME],
3841
0
                            reuse_inter_pred, &orig_dst, tmp_buffer,
3842
0
                            &this_mode_pred, &search_state.best_rdc,
3843
0
                            best_pickmode, &best_intra_sad_norm);
3844
3845
0
  int skip_idtx_palette = (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
3846
0
                           x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) &&
3847
0
                          x->content_state_sb.source_sad_nonrd != kZeroSad &&
3848
0
                          !cpi->rc.high_source_sad &&
3849
0
                          (cpi->rc.high_motion_content_screen_rtc ||
3850
0
                           cpi->rc.frame_source_sad < 1000);
3851
3852
0
  bool try_palette = enable_palette(
3853
0
      cpi, is_mode_intra(best_pickmode->best_mode), bsize, x->source_variance,
3854
0
      x->force_zeromv_skip_for_blk, skip_idtx_palette, force_palette_test,
3855
0
      best_intra_sad_norm);
3856
3857
0
  if (try_palette && prune_palette_testing_inter(cpi, x->source_variance))
3858
0
    x->color_palette_thresh =
3859
0
        cpi->sf.rt_sf.prune_palette_search_nonrd > 2 ? 20 : 32;
3860
3861
0
  if (!inter_forced_on_segment) {
3862
    // Perform screen content mode evaluation for non-rd
3863
0
    handle_screen_content_mode_nonrd(cpi, x, &search_state, this_mode_pred, ctx,
3864
0
                                     tmp_buffer, &orig_dst, skip_idtx_palette,
3865
0
                                     try_palette, bsize, reuse_inter_pred,
3866
0
                                     mi_col, mi_row);
3867
0
  }
3868
3869
#if COLLECT_NONRD_PICK_MODE_STAT
3870
  aom_usec_timer_mark(&x->ms_stat_nonrd.timer1);
3871
  x->ms_stat_nonrd.nonskipped_search_times[bsize][DC_PRED] +=
3872
      aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer1);
3873
#endif
3874
3875
0
  pd->dst = orig_dst;
3876
  // Best mode is finalized. Restore the mode data to mbmi
3877
0
  if (try_palette) mi->palette_mode_info = best_pickmode->pmi;
3878
0
  mi->mode = best_pickmode->best_mode;
3879
0
  mi->ref_frame[0] = best_pickmode->best_ref_frame;
3880
0
  mi->ref_frame[1] = best_pickmode->best_second_ref_frame;
3881
  // For lossless: always force the skip flags off.
3882
0
  if (is_lossless_requested(&cpi->oxcf.rc_cfg)) {
3883
0
    txfm_info->skip_txfm = 0;
3884
0
  } else {
3885
0
    txfm_info->skip_txfm = best_pickmode->best_mode_skip_txfm;
3886
0
  }
3887
0
  if (has_second_ref(mi)) {
3888
0
    mi->comp_group_idx = 0;
3889
0
    mi->compound_idx = 1;
3890
0
    mi->interinter_comp.type = COMPOUND_AVERAGE;
3891
0
  }
3892
3893
0
  if (!is_inter_block(mi)) {
3894
0
    mi->interp_filters = av1_broadcast_interp_filter(SWITCHABLE_FILTERS);
3895
0
  } else {
3896
    // If inter mode is selected and ref_frame was one that uses the
3897
    // scaled reference frame, then we can't use reuse_inter_pred.
3898
0
    if (search_state.use_scaled_ref_frame[best_pickmode->best_ref_frame] ||
3899
0
        (has_second_ref(mi) &&
3900
0
         search_state
3901
0
             .use_scaled_ref_frame[best_pickmode->best_second_ref_frame]))
3902
0
      x->reuse_inter_pred = 0;
3903
0
  }
3904
3905
  // Restore the predicted samples of best mode to final buffer
3906
0
  if (reuse_inter_pred && best_pickmode->best_pred != NULL) {
3907
0
    PRED_BUFFER *const best_pred = best_pickmode->best_pred;
3908
0
    if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) {
3909
0
      aom_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
3910
0
                        pd->dst.stride, bw, bh);
3911
0
    }
3912
0
  }
3913
3914
#if CONFIG_AV1_TEMPORAL_DENOISING
3915
  if (cpi->oxcf.noise_sensitivity > 0 && resize_pending == 0 &&
3916
      denoise_svc_pickmode && cpi->denoiser.denoising_level > kDenLowLow &&
3917
      cpi->denoiser.reset == 0) {
3918
    AV1_DENOISER_DECISION decision = COPY_BLOCK;
3919
    ctx->sb_skip_denoising = 0;
3920
    av1_pickmode_ctx_den_update(
3921
        &ctx_den, zero_last_cost_orig, search_state.ref_costs_single,
3922
        search_state.frame_mv, reuse_inter_pred, best_pickmode);
3923
    av1_denoiser_denoise(cpi, x, mi_row, mi_col, bsize, ctx, &decision,
3924
                         gf_temporal_ref);
3925
    if (denoise_recheck_zeromv)
3926
      recheck_zeromv_after_denoising(
3927
          cpi, mi, x, xd, decision, &ctx_den, search_state.yv12_mb,
3928
          &search_state.best_rdc, best_pickmode, bsize, mi_row, mi_col);
3929
    best_pickmode->best_ref_frame = ctx_den.best_ref_frame;
3930
  }
3931
#endif
3932
3933
  // Update the factors used for RD thresholding for all modes.
3934
0
  if (cpi->sf.inter_sf.adaptive_rd_thresh && !has_second_ref(mi)) {
3935
0
    THR_MODES best_mode_idx =
3936
0
        mode_idx[best_pickmode->best_ref_frame][mode_offset(mi->mode)];
3937
0
    if (best_pickmode->best_ref_frame == INTRA_FRAME) {
3938
      // Only consider the modes that are included in the intra_mode_list.
3939
0
      int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE);
3940
0
      for (int mode_index = 0; mode_index < intra_modes; mode_index++) {
3941
0
        update_thresh_freq_fact(cpi, x, bsize, INTRA_FRAME, best_mode_idx,
3942
0
                                intra_mode_list[mode_index]);
3943
0
      }
3944
0
    } else {
3945
0
      PREDICTION_MODE this_mode;
3946
0
      for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
3947
0
        update_thresh_freq_fact(cpi, x, bsize, best_pickmode->best_ref_frame,
3948
0
                                best_mode_idx, this_mode);
3949
0
      }
3950
0
    }
3951
0
  }
3952
3953
#if CONFIG_INTERNAL_STATS
3954
  store_coding_context_nonrd(x, ctx, mi->mode);
3955
#else
3956
0
  store_coding_context_nonrd(x, ctx);
3957
0
#endif  // CONFIG_INTERNAL_STATS
3958
3959
#if COLLECT_NONRD_PICK_MODE_STAT
3960
  aom_usec_timer_mark(&x->ms_stat_nonrd.bsize_timer);
3961
  x->ms_stat_nonrd.total_block_times[bsize] +=
3962
      aom_usec_timer_elapsed(&x->ms_stat_nonrd.bsize_timer);
3963
  print_time(&x->ms_stat_nonrd, bsize, cm->mi_params.mi_rows,
3964
             cm->mi_params.mi_cols, mi_row, mi_col);
3965
#endif  // COLLECT_NONRD_PICK_MODE_STAT
3966
3967
0
  *rd_cost = search_state.best_rdc;
3968
3969
0
  mi->num_proj_ref = 0;
3970
0
  if (warped_motion_update_num_proj_ref(cpi, mi)) {
3971
0
    int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
3972
0
    mi->num_proj_ref = av1_findSamples(cm, xd, pts, pts_inref);
3973
0
  }
3974
3975
  // Reset the xd->block_ref_scale_factors[i], as they may have
3976
  // been set to pointer &sf_no_scale, which becomes invalid afer
3977
  // this function.
3978
0
  set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
3979
0
}