Coverage Report

Created: 2022-08-24 06:17

/src/aom/av1/encoder/nonrd_pickmode.c
Line
Count
Source (jump to first uncovered line)
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
18
#include "config/aom_dsp_rtcd.h"
19
#include "config/av1_rtcd.h"
20
21
#include "aom_dsp/aom_dsp_common.h"
22
#include "aom_dsp/blend.h"
23
#include "aom_mem/aom_mem.h"
24
#include "aom_ports/aom_timer.h"
25
#include "aom_ports/mem.h"
26
27
#include "av1/encoder/model_rd.h"
28
#include "av1/common/mvref_common.h"
29
#include "av1/common/pred_common.h"
30
#include "av1/common/reconinter.h"
31
#include "av1/common/reconintra.h"
32
33
#include "av1/encoder/encodemv.h"
34
#include "av1/encoder/rdopt.h"
35
#include "av1/encoder/reconinter_enc.h"
36
#include "av1/encoder/var_based_part.h"
37
38
extern int g_pick_inter_mode_cnt;
39
/*!\cond */
40
typedef struct {
41
  uint8_t *data;
42
  int stride;
43
  int in_use;
44
} PRED_BUFFER;
45
46
typedef struct {
47
  PRED_BUFFER *best_pred;
48
  PREDICTION_MODE best_mode;
49
  TX_SIZE best_tx_size;
50
  MV_REFERENCE_FRAME best_ref_frame;
51
  MV_REFERENCE_FRAME best_second_ref_frame;
52
  uint8_t best_mode_skip_txfm;
53
  uint8_t best_mode_initial_skip_flag;
54
  int_interpfilters best_pred_filter;
55
  MOTION_MODE best_motion_mode;
56
  WarpedMotionParams wm_params;
57
  int num_proj_ref;
58
  uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE / 4];
59
} BEST_PICKMODE;
60
61
typedef struct {
62
  MV_REFERENCE_FRAME ref_frame;
63
  PREDICTION_MODE pred_mode;
64
} REF_MODE;
65
66
typedef struct {
67
  InterpFilter filter_x;
68
  InterpFilter filter_y;
69
} INTER_FILTER;
70
/*!\endcond */
71
72
0
#define NUM_INTER_MODES_RT 9
73
0
#define NUM_INTER_MODES_REDUCED 8
74
75
static const REF_MODE ref_mode_set_rt[NUM_INTER_MODES_RT] = {
76
  { LAST_FRAME, NEARESTMV },   { LAST_FRAME, NEARMV },
77
  { LAST_FRAME, NEWMV },       { GOLDEN_FRAME, NEARESTMV },
78
  { GOLDEN_FRAME, NEARMV },    { GOLDEN_FRAME, NEWMV },
79
  { ALTREF_FRAME, NEARESTMV }, { ALTREF_FRAME, NEARMV },
80
  { ALTREF_FRAME, NEWMV }
81
};
82
83
// GLOBALMV in the set below is in fact ZEROMV as we don't do global ME in RT
84
// mode
85
static const REF_MODE ref_mode_set_reduced[NUM_INTER_MODES_REDUCED] = {
86
  { LAST_FRAME, GLOBALMV },   { LAST_FRAME, NEARESTMV },
87
  { GOLDEN_FRAME, GLOBALMV }, { LAST_FRAME, NEARMV },
88
  { LAST_FRAME, NEWMV },      { GOLDEN_FRAME, NEARESTMV },
89
  { GOLDEN_FRAME, NEARMV },   { GOLDEN_FRAME, NEWMV }
90
};
91
92
static const THR_MODES mode_idx[REF_FRAMES][4] = {
93
  { THR_DC, THR_V_PRED, THR_H_PRED, THR_SMOOTH },
94
  { THR_NEARESTMV, THR_NEARMV, THR_GLOBALMV, THR_NEWMV },
95
  { THR_NEARESTL2, THR_NEARL2, THR_GLOBALL2, THR_NEWL2 },
96
  { THR_NEARESTL3, THR_NEARL3, THR_GLOBALL3, THR_NEWL3 },
97
  { THR_NEARESTG, THR_NEARG, THR_GLOBALMV, THR_NEWG },
98
};
99
100
static const PREDICTION_MODE intra_mode_list[] = { DC_PRED, V_PRED, H_PRED,
101
                                                   SMOOTH_PRED };
102
103
static const INTER_FILTER filters_ref_set[9] = {
104
  { EIGHTTAP_REGULAR, EIGHTTAP_REGULAR }, { EIGHTTAP_SMOOTH, EIGHTTAP_SMOOTH },
105
  { EIGHTTAP_REGULAR, EIGHTTAP_SMOOTH },  { EIGHTTAP_SMOOTH, EIGHTTAP_REGULAR },
106
  { MULTITAP_SHARP, MULTITAP_SHARP },     { EIGHTTAP_REGULAR, MULTITAP_SHARP },
107
  { MULTITAP_SHARP, EIGHTTAP_REGULAR },   { EIGHTTAP_SMOOTH, MULTITAP_SHARP },
108
  { MULTITAP_SHARP, EIGHTTAP_SMOOTH }
109
};
110
111
0
static INLINE int mode_offset(const PREDICTION_MODE mode) {
112
0
  if (mode >= NEARESTMV) {
113
0
    return INTER_OFFSET(mode);
114
0
  } else {
115
0
    switch (mode) {
116
0
      case DC_PRED: return 0;
117
0
      case V_PRED: return 1;
118
0
      case H_PRED: return 2;
119
0
      case SMOOTH_PRED: return 3;
120
0
      default: assert(0); return -1;
121
0
    }
122
0
  }
123
0
}
124
125
enum {
126
  //  INTER_ALL = (1 << NEARESTMV) | (1 << NEARMV) | (1 << NEWMV),
127
  INTER_NEAREST = (1 << NEARESTMV),
128
  INTER_NEAREST_NEW = (1 << NEARESTMV) | (1 << NEWMV),
129
  INTER_NEAREST_NEAR = (1 << NEARESTMV) | (1 << NEARMV),
130
  INTER_NEAR_NEW = (1 << NEARMV) | (1 << NEWMV),
131
};
132
133
0
static INLINE void init_best_pickmode(BEST_PICKMODE *bp) {
134
0
  bp->best_mode = NEARESTMV;
135
0
  bp->best_ref_frame = LAST_FRAME;
136
0
  bp->best_second_ref_frame = NONE_FRAME;
137
0
  bp->best_tx_size = TX_8X8;
138
0
  bp->best_pred_filter = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
139
0
  bp->best_mode_skip_txfm = 0;
140
0
  bp->best_mode_initial_skip_flag = 0;
141
0
  bp->best_pred = NULL;
142
0
  bp->best_motion_mode = SIMPLE_TRANSLATION;
143
0
  bp->num_proj_ref = 0;
144
0
  memset(&bp->wm_params, 0, sizeof(bp->wm_params));
145
0
  memset(&bp->blk_skip, 0, sizeof(bp->blk_skip));
146
0
}
147
148
0
static INLINE int subpel_select(AV1_COMP *cpi, BLOCK_SIZE bsize, int_mv *mv) {
149
0
  int mv_thresh = 4;
150
0
  const int is_low_resoln =
151
0
      (cpi->common.width * cpi->common.height <= 320 * 240);
152
0
  mv_thresh = (bsize > BLOCK_32X32) ? 2 : (bsize > BLOCK_16X16) ? 4 : 6;
153
0
  if (cpi->rc.avg_frame_low_motion > 0 && cpi->rc.avg_frame_low_motion < 40)
154
0
    mv_thresh = 12;
155
0
  mv_thresh = (is_low_resoln) ? mv_thresh >> 1 : mv_thresh;
156
0
  if (abs(mv->as_fullmv.row) >= mv_thresh ||
157
0
      abs(mv->as_fullmv.col) >= mv_thresh)
158
0
    return HALF_PEL;
159
0
  else
160
0
    return cpi->sf.mv_sf.subpel_force_stop;
161
0
}
162
163
/*!\brief Runs Motion Estimation for a specific block and specific ref frame.
164
 *
165
 * \ingroup nonrd_mode_search
166
 * \callgraph
167
 * \callergraph
168
 * Finds the best Motion Vector by running Motion Estimation for a specific
169
 * block and a specific reference frame. Exits early if RDCost of Full Pel part
170
 * exceeds best RD Cost fund so far
171
 * \param[in]    cpi                      Top-level encoder structure
172
 * \param[in]    x                        Pointer to structure holding all the
173
 *                                        data for the current macroblock
174
 * \param[in]    bsize                    Current block size
175
 * \param[in]    mi_row                   Row index in 4x4 units
176
 * \param[in]    mi_col                   Column index in 4x4 units
177
 * \param[in]    tmp_mv                   Pointer to best found New MV
178
 * \param[in]    rate_mv                  Pointer to Rate of the best new MV
179
 * \param[in]    best_rd_sofar            RD Cost of the best mode found so far
180
 * \param[in]    use_base_mv              Flag, indicating that tmp_mv holds
181
 *                                        specific MV to start the search with
182
 *
183
 * \return Returns 0 if ME was terminated after Full Pel Search because too
184
 * high RD Cost. Otherwise returns 1. Best New MV is placed into \c tmp_mv.
185
 * Rate estimation for this vector is placed to \c rate_mv
186
 */
187
static int combined_motion_search(AV1_COMP *cpi, MACROBLOCK *x,
188
                                  BLOCK_SIZE bsize, int mi_row, int mi_col,
189
                                  int_mv *tmp_mv, int *rate_mv,
190
0
                                  int64_t best_rd_sofar, int use_base_mv) {
191
0
  MACROBLOCKD *xd = &x->e_mbd;
192
0
  const AV1_COMMON *cm = &cpi->common;
193
0
  const int num_planes = av1_num_planes(cm);
194
0
  MB_MODE_INFO *mi = xd->mi[0];
195
0
  struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } };
196
0
  int step_param = (cpi->sf.rt_sf.fullpel_search_step_param)
197
0
                       ? cpi->sf.rt_sf.fullpel_search_step_param
198
0
                       : cpi->mv_search_params.mv_step_param;
199
0
  FULLPEL_MV start_mv;
200
0
  const int ref = mi->ref_frame[0];
201
0
  const MV ref_mv = av1_get_ref_mv(x, mi->ref_mv_idx).as_mv;
202
0
  MV center_mv;
203
0
  int dis;
204
0
  int rv = 0;
205
0
  int cost_list[5];
206
0
  int search_subpel = 1;
207
0
  const YV12_BUFFER_CONFIG *scaled_ref_frame =
208
0
      av1_get_scaled_ref_frame(cpi, ref);
209
210
0
  if (scaled_ref_frame) {
211
0
    int i;
212
    // Swap out the reference frame for a version that's been scaled to
213
    // match the resolution of the current frame, allowing the existing
214
    // motion search code to be used without additional modifications.
215
0
    for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
216
0
    av1_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL,
217
0
                         num_planes);
218
0
  }
219
220
0
  start_mv = get_fullmv_from_mv(&ref_mv);
221
222
0
  if (!use_base_mv)
223
0
    center_mv = ref_mv;
224
0
  else
225
0
    center_mv = tmp_mv->as_mv;
226
0
  const search_site_config *src_search_sites =
227
0
      cpi->mv_search_params.search_site_cfg[SS_CFG_SRC];
228
0
  FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
229
0
  av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, &center_mv,
230
0
                                     src_search_sites,
231
0
                                     /*fine_search_interval=*/0);
232
233
0
  av1_full_pixel_search(start_mv, &full_ms_params, step_param,
234
0
                        cond_cost_list(cpi, cost_list), &tmp_mv->as_fullmv,
235
0
                        NULL);
236
237
  // calculate the bit cost on motion vector
238
0
  MV mvp_full = get_mv_from_fullmv(&tmp_mv->as_fullmv);
239
240
0
  *rate_mv = av1_mv_bit_cost(&mvp_full, &ref_mv, x->mv_costs->nmv_joint_cost,
241
0
                             x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
242
243
  // TODO(kyslov) Account for Rate Mode!
244
0
  rv = !(RDCOST(x->rdmult, (*rate_mv), 0) > best_rd_sofar);
245
246
0
  if (rv && search_subpel) {
247
0
    SUBPEL_MOTION_SEARCH_PARAMS ms_params;
248
0
    av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
249
0
                                      cost_list);
250
0
    if (cpi->sf.rt_sf.force_half_pel_block &&
251
0
        cpi->sf.mv_sf.subpel_force_stop < HALF_PEL)
252
0
      ms_params.forced_stop = subpel_select(cpi, bsize, tmp_mv);
253
0
    MV subpel_start_mv = get_mv_from_fullmv(&tmp_mv->as_fullmv);
254
0
    cpi->mv_search_params.find_fractional_mv_step(
255
0
        xd, cm, &ms_params, subpel_start_mv, &tmp_mv->as_mv, &dis,
256
0
        &x->pred_sse[ref], NULL);
257
258
0
    *rate_mv =
259
0
        av1_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->mv_costs->nmv_joint_cost,
260
0
                        x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
261
0
  }
262
263
0
  if (scaled_ref_frame) {
264
0
    int i;
265
0
    for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
266
0
  }
267
  // Final MV can not be equal to referance MV as this will trigger assert
268
  // later. This can happen if both NEAREST and NEAR modes were skipped
269
0
  rv = (tmp_mv->as_mv.col != ref_mv.col || tmp_mv->as_mv.row != ref_mv.row);
270
0
  return rv;
271
0
}
272
273
/*!\brief Searches for the best New Motion Vector.
274
 *
275
 * \ingroup nonrd_mode_search
276
 * \callgraph
277
 * \callergraph
278
 * Finds the best Motion Vector by doing Motion Estimation. Uses reduced
279
 * complexity ME for non-LAST frames or calls \c combined_motion_search
280
 * for LAST reference frame
281
 * \param[in]    cpi                      Top-level encoder structure
282
 * \param[in]    x                        Pointer to structure holding all the
283
 *                                        data for the current macroblock
284
 * \param[in]    frame_mv                 Array that holds MVs for all modes
285
 *                                        and ref frames
286
 * \param[in]    ref_frame                Reference freme for which to find
287
 *                                        the best New MVs
288
 * \param[in]    gf_temporal_ref          Flag, indicating temporal reference
289
 *                                        for GOLDEN frame
290
 * \param[in]    bsize                    Current block size
291
 * \param[in]    mi_row                   Row index in 4x4 units
292
 * \param[in]    mi_col                   Column index in 4x4 units
293
 * \param[in]    rate_mv                  Pointer to Rate of the best new MV
294
 * \param[in]    best_rdc                 Pointer to the RD Cost for the best
295
 *                                        mode found so far
296
 *
297
 * \return Returns -1 if the search was not done, otherwise returns 0.
298
 * Best New MV is placed into \c frame_mv array, Rate estimation for this
299
 * vector is placed to \c rate_mv
300
 */
301
static int search_new_mv(AV1_COMP *cpi, MACROBLOCK *x,
302
                         int_mv frame_mv[][REF_FRAMES],
303
                         MV_REFERENCE_FRAME ref_frame, int gf_temporal_ref,
304
                         BLOCK_SIZE bsize, int mi_row, int mi_col, int *rate_mv,
305
0
                         RD_STATS *best_rdc) {
306
0
  MACROBLOCKD *const xd = &x->e_mbd;
307
0
  MB_MODE_INFO *const mi = xd->mi[0];
308
0
  AV1_COMMON *cm = &cpi->common;
309
0
  if (ref_frame > LAST_FRAME && cpi->oxcf.rc_cfg.mode == AOM_CBR &&
310
0
      gf_temporal_ref) {
311
0
    int tmp_sad;
312
0
    int dis;
313
0
    int cost_list[5] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX };
314
315
0
    if (bsize < BLOCK_16X16) return -1;
316
317
0
    tmp_sad = av1_int_pro_motion_estimation(
318
0
        cpi, x, bsize, mi_row, mi_col,
319
0
        &x->mbmi_ext.ref_mv_stack[ref_frame][0].this_mv.as_mv);
320
321
0
    if (tmp_sad > x->pred_mv_sad[LAST_FRAME]) return -1;
322
323
0
    frame_mv[NEWMV][ref_frame].as_int = mi->mv[0].as_int;
324
0
    int_mv best_mv = mi->mv[0];
325
0
    best_mv.as_mv.row >>= 3;
326
0
    best_mv.as_mv.col >>= 3;
327
0
    MV ref_mv = av1_get_ref_mv(x, 0).as_mv;
328
329
0
    *rate_mv = av1_mv_bit_cost(&frame_mv[NEWMV][ref_frame].as_mv, &ref_mv,
330
0
                               x->mv_costs->nmv_joint_cost,
331
0
                               x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
332
0
    frame_mv[NEWMV][ref_frame].as_mv.row >>= 3;
333
0
    frame_mv[NEWMV][ref_frame].as_mv.col >>= 3;
334
335
0
    SUBPEL_MOTION_SEARCH_PARAMS ms_params;
336
0
    av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
337
0
                                      cost_list);
338
0
    if (cpi->sf.rt_sf.force_half_pel_block &&
339
0
        cpi->sf.mv_sf.subpel_force_stop < HALF_PEL)
340
0
      ms_params.forced_stop = subpel_select(cpi, bsize, &best_mv);
341
0
    MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
342
0
    cpi->mv_search_params.find_fractional_mv_step(
343
0
        xd, cm, &ms_params, start_mv, &best_mv.as_mv, &dis,
344
0
        &x->pred_sse[ref_frame], NULL);
345
0
    frame_mv[NEWMV][ref_frame].as_int = best_mv.as_int;
346
0
  } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
347
0
                                     &frame_mv[NEWMV][ref_frame], rate_mv,
348
0
                                     best_rdc->rdcost, 0)) {
349
0
    return -1;
350
0
  }
351
352
0
  return 0;
353
0
}
354
355
/*!\brief Finds predicted motion vectors for a block.
356
 *
357
 * \ingroup nonrd_mode_search
358
 * \callgraph
359
 * \callergraph
360
 * Finds predicted motion vectors for a block from a certain reference frame.
361
 * First, it fills reference MV stack, then picks the test from the stack and
362
 * predicts the final MV for a block for each mode.
363
 * \param[in]    cpi                      Top-level encoder structure
364
 * \param[in]    x                        Pointer to structure holding all the
365
 *                                        data for the current macroblock
366
 * \param[in]    ref_frame                Reference freme for which to find
367
 *                                        ref MVs
368
 * \param[in]    frame_mv                 Predicted MVs for a block
369
 * \param[in]    tile_data                Pointer to struct holding adaptive
370
 *                                        data/contexts/models for the tile
371
 *                                        during encoding
372
 * \param[in]    yv12_mb                  Buffer to hold predicted block
373
 * \param[in]    bsize                    Current block size
374
 * \param[in]    force_skip_low_temp_var  Flag indicating possible mode search
375
 *                                        prune for low temporal variance block
376
 * \param[in]    skip_pred_mv             Flag indicating to skip av1_mv_pred
377
 *
378
 * \return Nothing is returned. Instead, predicted MVs are placed into
379
 * \c frame_mv array
380
 */
381
static INLINE void find_predictors(
382
    AV1_COMP *cpi, MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame,
383
    int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], TileDataEnc *tile_data,
384
    struct buf_2d yv12_mb[8][MAX_MB_PLANE], BLOCK_SIZE bsize,
385
0
    int force_skip_low_temp_var, int skip_pred_mv) {
386
0
  AV1_COMMON *const cm = &cpi->common;
387
0
  MACROBLOCKD *const xd = &x->e_mbd;
388
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
389
0
  MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
390
0
  const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, ref_frame);
391
0
  const int num_planes = av1_num_planes(cm);
392
0
  (void)tile_data;
393
394
0
  x->pred_mv_sad[ref_frame] = INT_MAX;
395
0
  x->pred_mv0_sad[ref_frame] = INT_MAX;
396
0
  x->pred_mv1_sad[ref_frame] = INT_MAX;
397
0
  frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
398
  // TODO(kyslov) this needs various further optimizations. to be continued..
399
0
  assert(yv12 != NULL);
400
0
  if (yv12 != NULL) {
401
0
    const struct scale_factors *const sf =
402
0
        get_ref_scale_factors_const(cm, ref_frame);
403
0
    av1_setup_pred_block(xd, yv12_mb[ref_frame], yv12, sf, sf, num_planes);
404
0
    av1_find_mv_refs(cm, xd, mbmi, ref_frame, mbmi_ext->ref_mv_count,
405
0
                     xd->ref_mv_stack, xd->weight, NULL, mbmi_ext->global_mvs,
406
0
                     mbmi_ext->mode_context);
407
    // TODO(Ravi): Populate mbmi_ext->ref_mv_stack[ref_frame][4] and
408
    // mbmi_ext->weight[ref_frame][4] inside av1_find_mv_refs.
409
0
    av1_copy_usable_ref_mv_stack_and_weight(xd, mbmi_ext, ref_frame);
410
0
    av1_find_best_ref_mvs_from_stack(
411
0
        cm->features.allow_high_precision_mv, mbmi_ext, ref_frame,
412
0
        &frame_mv[NEARESTMV][ref_frame], &frame_mv[NEARMV][ref_frame], 0);
413
0
    frame_mv[GLOBALMV][ref_frame] = mbmi_ext->global_mvs[ref_frame];
414
    // Early exit for non-LAST frame if force_skip_low_temp_var is set.
415
0
    if (!av1_is_scaled(sf) && bsize >= BLOCK_8X8 && !skip_pred_mv &&
416
0
        !(force_skip_low_temp_var && ref_frame != LAST_FRAME)) {
417
0
      av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
418
0
                  bsize);
419
0
    }
420
0
  }
421
0
  av1_count_overlappable_neighbors(cm, xd);
422
0
  mbmi->num_proj_ref = 1;
423
0
}
424
425
static void estimate_single_ref_frame_costs(const AV1_COMMON *cm,
426
                                            const MACROBLOCKD *xd,
427
                                            const ModeCosts *mode_costs,
428
                                            int segment_id,
429
0
                                            unsigned int *ref_costs_single) {
430
0
  int seg_ref_active =
431
0
      segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME);
432
0
  if (seg_ref_active) {
433
0
    memset(ref_costs_single, 0, REF_FRAMES * sizeof(*ref_costs_single));
434
0
  } else {
435
0
    int intra_inter_ctx = av1_get_intra_inter_context(xd);
436
0
    ref_costs_single[INTRA_FRAME] =
437
0
        mode_costs->intra_inter_cost[intra_inter_ctx][0];
438
0
    unsigned int base_cost = mode_costs->intra_inter_cost[intra_inter_ctx][1];
439
0
    ref_costs_single[LAST_FRAME] = base_cost;
440
0
    ref_costs_single[GOLDEN_FRAME] = base_cost;
441
0
    ref_costs_single[ALTREF_FRAME] = base_cost;
442
    // add cost for last, golden, altref
443
0
    ref_costs_single[LAST_FRAME] += mode_costs->single_ref_cost[0][0][0];
444
0
    ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][0][1];
445
0
    ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][1][0];
446
0
    ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][0][1];
447
0
    ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][2][0];
448
0
  }
449
0
}
450
451
static TX_SIZE calculate_tx_size(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
452
                                 MACROBLOCK *const x, unsigned int var,
453
0
                                 unsigned int sse) {
454
0
  MACROBLOCKD *const xd = &x->e_mbd;
455
0
  TX_SIZE tx_size;
456
0
  const TxfmSearchParams *txfm_params = &x->txfm_search_params;
457
0
  if (txfm_params->tx_mode_search_type == TX_MODE_SELECT) {
458
0
    if (sse > (var << 1))
459
0
      tx_size =
460
0
          AOMMIN(max_txsize_lookup[bsize],
461
0
                 tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
462
0
    else
463
0
      tx_size = TX_8X8;
464
465
0
    if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
466
0
        cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id))
467
0
      tx_size = TX_8X8;
468
0
    else if (tx_size > TX_16X16)
469
0
      tx_size = TX_16X16;
470
0
  } else {
471
0
    tx_size =
472
0
        AOMMIN(max_txsize_lookup[bsize],
473
0
               tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
474
0
  }
475
476
0
  if (txfm_params->tx_mode_search_type != ONLY_4X4 && bsize > BLOCK_32X32)
477
0
    tx_size = TX_16X16;
478
479
0
  return AOMMIN(tx_size, TX_16X16);
480
0
}
481
482
static const uint8_t b_width_log2_lookup[BLOCK_SIZES] = { 0, 0, 1, 1, 1, 2,
483
                                                          2, 2, 3, 3, 3, 4,
484
                                                          4, 4, 5, 5 };
485
static const uint8_t b_height_log2_lookup[BLOCK_SIZES] = { 0, 1, 0, 1, 2, 1,
486
                                                           2, 3, 2, 3, 4, 3,
487
                                                           4, 5, 4, 5 };
488
489
static void block_variance(const uint8_t *src, int src_stride,
490
                           const uint8_t *ref, int ref_stride, int w, int h,
491
                           unsigned int *sse, int *sum, int block_size,
492
0
                           uint32_t *sse8x8, int *sum8x8, uint32_t *var8x8) {
493
0
  int i, j, k = 0;
494
495
0
  *sse = 0;
496
0
  *sum = 0;
497
498
0
  for (i = 0; i < h; i += block_size) {
499
0
    for (j = 0; j < w; j += block_size) {
500
0
      aom_get8x8var(src + src_stride * i + j, src_stride,
501
0
                    ref + ref_stride * i + j, ref_stride, &sse8x8[k],
502
0
                    &sum8x8[k]);
503
0
      *sse += sse8x8[k];
504
0
      *sum += sum8x8[k];
505
0
      var8x8[k] = sse8x8[k] - (uint32_t)(((int64_t)sum8x8[k] * sum8x8[k]) >> 6);
506
0
      k++;
507
0
    }
508
0
  }
509
0
}
510
511
static void calculate_variance(int bw, int bh, TX_SIZE tx_size,
512
                               unsigned int *sse_i, int *sum_i,
513
                               unsigned int *var_o, unsigned int *sse_o,
514
0
                               int *sum_o) {
515
0
  const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size];
516
0
  const int nw = 1 << (bw - b_width_log2_lookup[unit_size]);
517
0
  const int nh = 1 << (bh - b_height_log2_lookup[unit_size]);
518
0
  int i, j, k = 0;
519
520
0
  for (i = 0; i < nh; i += 2) {
521
0
    for (j = 0; j < nw; j += 2) {
522
0
      sse_o[k] = sse_i[i * nw + j] + sse_i[i * nw + j + 1] +
523
0
                 sse_i[(i + 1) * nw + j] + sse_i[(i + 1) * nw + j + 1];
524
0
      sum_o[k] = sum_i[i * nw + j] + sum_i[i * nw + j + 1] +
525
0
                 sum_i[(i + 1) * nw + j] + sum_i[(i + 1) * nw + j + 1];
526
0
      var_o[k] = sse_o[k] - (uint32_t)(((int64_t)sum_o[k] * sum_o[k]) >>
527
0
                                       (b_width_log2_lookup[unit_size] +
528
0
                                        b_height_log2_lookup[unit_size] + 6));
529
0
      k++;
530
0
    }
531
0
  }
532
0
}
533
534
// Adjust the ac_thr according to speed, width, height and normalized sum
535
static int ac_thr_factor(const int speed, const int width, const int height,
536
0
                         const int norm_sum) {
537
0
  if (speed >= 8 && norm_sum < 5) {
538
0
    if (width <= 640 && height <= 480)
539
0
      return 4;
540
0
    else
541
0
      return 2;
542
0
  }
543
0
  return 1;
544
0
}
545
546
static void model_skip_for_sb_y_large(AV1_COMP *cpi, BLOCK_SIZE bsize,
547
                                      int mi_row, int mi_col, MACROBLOCK *x,
548
                                      MACROBLOCKD *xd, RD_STATS *rd_stats,
549
0
                                      int *early_term, int calculate_rd) {
550
  // Note our transform coeffs are 8 times an orthogonal transform.
551
  // Hence quantizer step is also 8 times. To get effective quantizer
552
  // we need to divide by 8 before sending to modeling function.
553
0
  unsigned int sse;
554
0
  struct macroblock_plane *const p = &x->plane[0];
555
0
  struct macroblockd_plane *const pd = &xd->plane[0];
556
0
  const uint32_t dc_quant = p->dequant_QTX[0];
557
0
  const uint32_t ac_quant = p->dequant_QTX[1];
558
0
  const int64_t dc_thr = dc_quant * dc_quant >> 6;
559
0
  int64_t ac_thr = ac_quant * ac_quant >> 6;
560
0
  unsigned int var;
561
0
  int sum;
562
563
0
  const int bw = b_width_log2_lookup[bsize];
564
0
  const int bh = b_height_log2_lookup[bsize];
565
0
  const int num8x8 = 1 << (bw + bh - 2);
566
0
  unsigned int sse8x8[256] = { 0 };
567
0
  int sum8x8[256] = { 0 };
568
0
  unsigned int var8x8[256] = { 0 };
569
0
  TX_SIZE tx_size;
570
0
  int k;
571
  // Calculate variance for whole partition, and also save 8x8 blocks' variance
572
  // to be used in following transform skipping test.
573
0
  block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
574
0
                 4 << bw, 4 << bh, &sse, &sum, 8, sse8x8, sum8x8, var8x8);
575
0
  var = sse - (unsigned int)(((int64_t)sum * sum) >> (bw + bh + 4));
576
577
0
  rd_stats->sse = sse;
578
579
#if CONFIG_AV1_TEMPORAL_DENOISING
580
  if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
581
      cpi->oxcf.speed > 5)
582
    ac_thr = av1_scale_acskip_thresh(ac_thr, cpi->denoiser.denoising_level,
583
                                     (abs(sum) >> (bw + bh)),
584
                                     cpi->svc.temporal_layer_id);
585
  else
586
    ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width,
587
                            cpi->common.height, abs(sum) >> (bw + bh));
588
#else
589
0
  ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width,
590
0
                          cpi->common.height, abs(sum) >> (bw + bh));
591
592
0
#endif
593
0
  tx_size = calculate_tx_size(cpi, bsize, x, var, sse);
594
  // The code below for setting skip flag assumes tranform size of at least 8x8,
595
  // so force this lower limit on transform.
596
0
  if (tx_size < TX_8X8) tx_size = TX_8X8;
597
0
  xd->mi[0]->tx_size = tx_size;
598
599
  // Evaluate if the partition block is a skippable block in Y plane.
600
0
  {
601
0
    unsigned int sse16x16[64] = { 0 };
602
0
    int sum16x16[64] = { 0 };
603
0
    unsigned int var16x16[64] = { 0 };
604
0
    const int num16x16 = num8x8 >> 2;
605
606
0
    unsigned int sse32x32[16] = { 0 };
607
0
    int sum32x32[16] = { 0 };
608
0
    unsigned int var32x32[16] = { 0 };
609
0
    const int num32x32 = num8x8 >> 4;
610
611
0
    int ac_test = 1;
612
0
    int dc_test = 1;
613
0
    const int num = (tx_size == TX_8X8)
614
0
                        ? num8x8
615
0
                        : ((tx_size == TX_16X16) ? num16x16 : num32x32);
616
0
    const unsigned int *sse_tx =
617
0
        (tx_size == TX_8X8) ? sse8x8
618
0
                            : ((tx_size == TX_16X16) ? sse16x16 : sse32x32);
619
0
    const unsigned int *var_tx =
620
0
        (tx_size == TX_8X8) ? var8x8
621
0
                            : ((tx_size == TX_16X16) ? var16x16 : var32x32);
622
623
    // Calculate variance if tx_size > TX_8X8
624
0
    if (tx_size >= TX_16X16)
625
0
      calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16,
626
0
                         sum16x16);
627
0
    if (tx_size == TX_32X32)
628
0
      calculate_variance(bw, bh, TX_16X16, sse16x16, sum16x16, var32x32,
629
0
                         sse32x32, sum32x32);
630
631
    // Skipping test
632
0
    *early_term = 0;
633
0
    for (k = 0; k < num; k++)
634
      // Check if all ac coefficients can be quantized to zero.
635
0
      if (!(var_tx[k] < ac_thr || var == 0)) {
636
0
        ac_test = 0;
637
0
        break;
638
0
      }
639
640
0
    for (k = 0; k < num; k++)
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
647
0
    if (ac_test && dc_test) {
648
0
      int skip_uv[2] = { 0 };
649
0
      unsigned int var_uv[2];
650
0
      unsigned int sse_uv[2];
651
0
      AV1_COMMON *const cm = &cpi->common;
652
      // Transform skipping test in UV planes.
653
0
      for (int i = 1; i <= 2; i++) {
654
0
        int j = i - 1;
655
0
        skip_uv[j] = 1;
656
0
        if (x->color_sensitivity[j]) {
657
0
          skip_uv[j] = 0;
658
0
          struct macroblock_plane *const puv = &x->plane[i];
659
0
          struct macroblockd_plane *const puvd = &xd->plane[i];
660
0
          const BLOCK_SIZE uv_bsize = get_plane_block_size(
661
0
              bsize, puvd->subsampling_x, puvd->subsampling_y);
662
          // Adjust these thresholds for UV.
663
0
          const int64_t uv_dc_thr =
664
0
              (puv->dequant_QTX[0] * puv->dequant_QTX[0]) >> 3;
665
0
          const int64_t uv_ac_thr =
666
0
              (puv->dequant_QTX[1] * puv->dequant_QTX[1]) >> 3;
667
0
          av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, i,
668
0
                                        i);
669
0
          var_uv[j] = cpi->ppi->fn_ptr[uv_bsize].vf(
670
0
              puv->src.buf, puv->src.stride, puvd->dst.buf, puvd->dst.stride,
671
0
              &sse_uv[j]);
672
0
          if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) &&
673
0
              (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j]))
674
0
            skip_uv[j] = 1;
675
0
          else
676
0
            break;
677
0
        }
678
0
      }
679
0
      if (skip_uv[0] & skip_uv[1]) {
680
0
        *early_term = 1;
681
0
      }
682
0
    }
683
0
  }
684
0
  if (calculate_rd) {
685
0
    if (!*early_term) {
686
0
      const int bwide = block_size_wide[bsize];
687
0
      const int bhigh = block_size_high[bsize];
688
689
0
      model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh,
690
0
                            &rd_stats->rate, &rd_stats->dist);
691
0
    }
692
693
0
    if (*early_term) {
694
0
      rd_stats->rate = 0;
695
0
      rd_stats->dist = sse << 4;
696
0
    }
697
0
  }
698
0
}
699
700
static void model_rd_for_sb_y(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
701
                              MACROBLOCK *x, MACROBLOCKD *xd,
702
0
                              RD_STATS *rd_stats, int calculate_rd) {
703
  // Note our transform coeffs are 8 times an orthogonal transform.
704
  // Hence quantizer step is also 8 times. To get effective quantizer
705
  // we need to divide by 8 before sending to modeling function.
706
0
  const int ref = xd->mi[0]->ref_frame[0];
707
708
0
  assert(bsize < BLOCK_SIZES_ALL);
709
710
0
  struct macroblock_plane *const p = &x->plane[0];
711
0
  struct macroblockd_plane *const pd = &xd->plane[0];
712
0
  unsigned int sse;
713
0
  int rate;
714
0
  int64_t dist;
715
716
0
  unsigned int var = cpi->ppi->fn_ptr[bsize].vf(
717
0
      p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, &sse);
718
0
  xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, x, var, sse);
719
720
0
  if (calculate_rd) {
721
0
    const int bwide = block_size_wide[bsize];
722
0
    const int bhigh = block_size_high[bsize];
723
0
    model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh, &rate,
724
0
                          &dist);
725
0
  } else {
726
0
    rate = INT_MAX;  // this will be overwritten later with block_yrd
727
0
    dist = INT_MAX;
728
0
  }
729
0
  rd_stats->sse = sse;
730
0
  x->pred_sse[ref] = (unsigned int)AOMMIN(sse, UINT_MAX);
731
732
0
  assert(rate >= 0);
733
734
0
  rd_stats->skip_txfm = (rate == 0);
735
0
  rate = AOMMIN(rate, INT_MAX);
736
0
  rd_stats->rate = rate;
737
0
  rd_stats->dist = dist;
738
0
}
739
740
/*!\brief Calculates RD Cost using Hadamard transform.
741
 *
742
 * \ingroup nonrd_mode_search
743
 * \callgraph
744
 * \callergraph
745
 * Calculates RD Cost using Hadamard transform. For low bit depth this function
746
 * uses low-precision set of functions (16-bit) and 32 bit for high bit depth
747
 * \param[in]    cpi            Top-level encoder structure
748
 * \param[in]    x              Pointer to structure holding all the data for
749
                                the current macroblock
750
 * \param[in]    mi_row         Row index in 4x4 units
751
 * \param[in]    mi_col         Column index in 4x4 units
752
 * \param[in]    this_rdc       Pointer to calculated RD Cost
753
 * \param[in]    skippable      Pointer to a flag indicating possible tx skip
754
 * \param[in]    bsize          Current block size
755
 * \param[in]    tx_size        Transform size
756
 *
757
 * \return Nothing is returned. Instead, calculated RD cost is placed to
758
 * \c this_rdc. \c skippable flag is set if there is no non-zero quantized
759
 * coefficients for Hadamard transform
760
 */
761
static void block_yrd(AV1_COMP *cpi, MACROBLOCK *x, int mi_row, int mi_col,
762
                      RD_STATS *this_rdc, int *skippable, BLOCK_SIZE bsize,
763
0
                      TX_SIZE tx_size) {
764
0
  MACROBLOCKD *xd = &x->e_mbd;
765
0
  const struct macroblockd_plane *pd = &xd->plane[0];
766
0
  struct macroblock_plane *const p = &x->plane[0];
767
0
  const int num_4x4_w = mi_size_wide[bsize];
768
0
  const int num_8x8_w = num_4x4_w / 2;
769
0
  const int num_4x4_h = mi_size_high[bsize];
770
0
  const int step = 1 << (tx_size << 1);
771
0
  const int block_step = (1 << tx_size);
772
0
  int block = 0;
773
0
  const int max_blocks_wide =
774
0
      num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >> 5);
775
0
  const int max_blocks_high =
776
0
      num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >> 5);
777
0
  int eob_cost = 0;
778
0
  const int bw = 4 * num_4x4_w;
779
0
  const int bh = 4 * num_4x4_h;
780
0
  const int use_hbd = is_cur_buf_hbd(xd);
781
782
0
  (void)mi_row;
783
0
  (void)mi_col;
784
0
  (void)cpi;
785
786
0
#if CONFIG_AV1_HIGHBITDEPTH
787
0
  if (use_hbd) {
788
0
    aom_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
789
0
                              p->src.stride, pd->dst.buf, pd->dst.stride,
790
0
                              x->e_mbd.bd);
791
0
  } else {
792
0
    aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
793
0
                       pd->dst.buf, pd->dst.stride);
794
0
  }
795
#else
796
  aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
797
                     pd->dst.buf, pd->dst.stride);
798
#endif
799
800
0
  *skippable = 1;
801
  // Keep track of the row and column of the blocks we use so that we know
802
  // if we are in the unrestricted motion border.
803
0
  for (int r = 0; r < max_blocks_high; r += block_step) {
804
0
    for (int c = 0; c < num_4x4_w; c += block_step) {
805
0
      if (c < max_blocks_wide) {
806
0
        const SCAN_ORDER *const scan_order = &av1_scan_orders[tx_size][DCT_DCT];
807
0
        const int block_offset = BLOCK_OFFSET(block);
808
0
        int16_t *const low_coeff = (int16_t *)p->coeff + block_offset;
809
0
        int16_t *const low_qcoeff = (int16_t *)p->qcoeff + block_offset;
810
0
        int16_t *const low_dqcoeff = (int16_t *)p->dqcoeff + block_offset;
811
0
#if CONFIG_AV1_HIGHBITDEPTH
812
0
        tran_low_t *const coeff = p->coeff + block_offset;
813
0
        tran_low_t *const qcoeff = p->qcoeff + block_offset;
814
0
        tran_low_t *const dqcoeff = p->dqcoeff + block_offset;
815
#else
816
        (void)use_hbd;
817
#endif
818
0
        uint16_t *const eob = &p->eobs[block];
819
0
        const int diff_stride = bw;
820
0
        const int16_t *src_diff;
821
0
        src_diff = &p->src_diff[(r * diff_stride + c) << 2];
822
823
0
        switch (tx_size) {
824
0
          case TX_64X64:
825
0
            assert(0);  // Not implemented
826
0
            break;
827
0
          case TX_32X32:
828
0
            assert(0);  // Not used
829
0
            break;
830
831
0
#if CONFIG_AV1_HIGHBITDEPTH
832
0
          case TX_16X16:
833
0
            if (use_hbd) {
834
0
              aom_hadamard_16x16(src_diff, diff_stride, coeff);
835
0
              av1_quantize_fp(coeff, 16 * 16, p->zbin_QTX, p->round_fp_QTX,
836
0
                              p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
837
0
                              dqcoeff, p->dequant_QTX, eob, scan_order->scan,
838
0
                              scan_order->iscan);
839
0
            } else {
840
0
              aom_hadamard_lp_16x16(src_diff, diff_stride, low_coeff);
841
0
              av1_quantize_lp(low_coeff, 16 * 16, p->round_fp_QTX,
842
0
                              p->quant_fp_QTX, low_qcoeff, low_dqcoeff,
843
0
                              p->dequant_QTX, eob, scan_order->scan,
844
0
                              scan_order->iscan);
845
0
            }
846
0
            break;
847
0
          case TX_8X8:
848
0
            if (use_hbd) {
849
0
              aom_hadamard_8x8(src_diff, diff_stride, coeff);
850
0
              av1_quantize_fp(coeff, 8 * 8, p->zbin_QTX, p->round_fp_QTX,
851
0
                              p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
852
0
                              dqcoeff, p->dequant_QTX, eob, scan_order->scan,
853
0
                              scan_order->iscan);
854
0
            } else {
855
0
              aom_hadamard_lp_8x8(src_diff, diff_stride, low_coeff);
856
0
              av1_quantize_lp(low_coeff, 8 * 8, p->round_fp_QTX,
857
0
                              p->quant_fp_QTX, low_qcoeff, low_dqcoeff,
858
0
                              p->dequant_QTX, eob, scan_order->scan,
859
0
                              scan_order->iscan);
860
0
            }
861
0
            break;
862
0
          default:
863
0
            assert(tx_size == TX_4X4);
864
0
            if (use_hbd) {
865
0
              aom_fdct4x4(src_diff, coeff, diff_stride);
866
0
              av1_quantize_fp(coeff, 4 * 4, p->zbin_QTX, p->round_fp_QTX,
867
0
                              p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
868
0
                              dqcoeff, p->dequant_QTX, eob, scan_order->scan,
869
0
                              scan_order->iscan);
870
0
            } else {
871
0
              aom_fdct4x4_lp(src_diff, low_coeff, diff_stride);
872
0
              av1_quantize_lp(low_coeff, 4 * 4, p->round_fp_QTX,
873
0
                              p->quant_fp_QTX, low_qcoeff, low_dqcoeff,
874
0
                              p->dequant_QTX, eob, scan_order->scan,
875
0
                              scan_order->iscan);
876
0
            }
877
0
            break;
878
#else
879
          case TX_16X16:
880
            aom_hadamard_lp_16x16(src_diff, diff_stride, low_coeff);
881
            av1_quantize_lp(low_coeff, 16 * 16, p->round_fp_QTX,
882
                            p->quant_fp_QTX, low_qcoeff, low_dqcoeff,
883
                            p->dequant_QTX, eob, scan_order->scan,
884
                            scan_order->iscan);
885
            break;
886
          case TX_8X8:
887
            aom_hadamard_lp_8x8(src_diff, diff_stride, low_coeff);
888
            av1_quantize_lp(low_coeff, 8 * 8, p->round_fp_QTX, p->quant_fp_QTX,
889
                            low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
890
                            scan_order->scan, scan_order->iscan);
891
            break;
892
          default:
893
            assert(tx_size == TX_4X4);
894
            aom_fdct4x4_lp(src_diff, low_coeff, diff_stride);
895
            av1_quantize_lp(low_coeff, 4 * 4, p->round_fp_QTX, p->quant_fp_QTX,
896
                            low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
897
                            scan_order->scan, scan_order->iscan);
898
            break;
899
#endif
900
0
        }
901
0
        assert(*eob <= 1024);
902
0
        *skippable &= (*eob == 0);
903
0
        x->txfm_search_info.blk_skip[(r * num_8x8_w + c) / 2] =
904
0
            (*eob == 0) ? 1 : 0;
905
0
        eob_cost += 1;
906
0
      }
907
0
      block += step;
908
0
    }
909
0
  }
910
0
  this_rdc->skip_txfm = *skippable;
911
0
  this_rdc->rate = 0;
912
0
  if (this_rdc->sse < INT64_MAX) {
913
0
    this_rdc->sse = (this_rdc->sse << 6) >> 2;
914
0
    if (*skippable) {
915
0
      this_rdc->dist = this_rdc->sse;
916
0
      return;
917
0
    }
918
0
  }
919
920
0
  block = 0;
921
0
  this_rdc->dist = 0;
922
0
  for (int r = 0; r < max_blocks_high; r += block_step) {
923
0
    for (int c = 0; c < num_4x4_w; c += block_step) {
924
0
      if (c < max_blocks_wide) {
925
0
        const int block_offset = BLOCK_OFFSET(block);
926
0
        uint16_t *const eob = &p->eobs[block];
927
0
#if CONFIG_AV1_HIGHBITDEPTH
928
0
        if (use_hbd) {
929
0
          int64_t dummy;
930
0
          tran_low_t *const coeff = p->coeff + block_offset;
931
0
          tran_low_t *const qcoeff = p->qcoeff + block_offset;
932
0
          tran_low_t *const dqcoeff = p->dqcoeff + block_offset;
933
934
0
          if (*eob == 1)
935
0
            this_rdc->rate += (int)abs(qcoeff[0]);
936
0
          else if (*eob > 1)
937
0
            this_rdc->rate += aom_satd(qcoeff, step << 4);
938
939
0
          this_rdc->dist +=
940
0
              av1_block_error(coeff, dqcoeff, step << 4, &dummy) >> 2;
941
0
        } else {
942
0
          int16_t *const low_coeff = (int16_t *)p->coeff + block_offset;
943
0
          int16_t *const low_qcoeff = (int16_t *)p->qcoeff + block_offset;
944
0
          int16_t *const low_dqcoeff = (int16_t *)p->dqcoeff + block_offset;
945
946
0
          if (*eob == 1)
947
0
            this_rdc->rate += (int)abs(low_qcoeff[0]);
948
0
          else if (*eob > 1)
949
0
            this_rdc->rate += aom_satd_lp(low_qcoeff, step << 4);
950
951
0
          this_rdc->dist +=
952
0
              av1_block_error_lp(low_coeff, low_dqcoeff, step << 4) >> 2;
953
0
        }
954
#else
955
        int16_t *const low_coeff = (int16_t *)p->coeff + block_offset;
956
        int16_t *const low_qcoeff = (int16_t *)p->qcoeff + block_offset;
957
        int16_t *const low_dqcoeff = (int16_t *)p->dqcoeff + block_offset;
958
959
        if (*eob == 1)
960
          this_rdc->rate += (int)abs(low_qcoeff[0]);
961
        else if (*eob > 1)
962
          this_rdc->rate += aom_satd_lp(low_qcoeff, step << 4);
963
964
        this_rdc->dist +=
965
            av1_block_error_lp(low_coeff, low_dqcoeff, step << 4) >> 2;
966
#endif
967
0
      }
968
0
      block += step;
969
0
    }
970
0
  }
971
972
  // If skippable is set, rate gets clobbered later.
973
0
  this_rdc->rate <<= (2 + AV1_PROB_COST_SHIFT);
974
0
  this_rdc->rate += (eob_cost << AV1_PROB_COST_SHIFT);
975
0
}
976
977
static INLINE void init_mbmi(MB_MODE_INFO *mbmi, PREDICTION_MODE pred_mode,
978
                             MV_REFERENCE_FRAME ref_frame0,
979
                             MV_REFERENCE_FRAME ref_frame1,
980
0
                             const AV1_COMMON *cm) {
981
0
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
982
0
  mbmi->ref_mv_idx = 0;
983
0
  mbmi->mode = pred_mode;
984
0
  mbmi->uv_mode = UV_DC_PRED;
985
0
  mbmi->ref_frame[0] = ref_frame0;
986
0
  mbmi->ref_frame[1] = ref_frame1;
987
0
  pmi->palette_size[0] = 0;
988
0
  pmi->palette_size[1] = 0;
989
0
  mbmi->filter_intra_mode_info.use_filter_intra = 0;
990
0
  mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0;
991
0
  mbmi->motion_mode = SIMPLE_TRANSLATION;
992
0
  mbmi->num_proj_ref = 1;
993
0
  mbmi->interintra_mode = 0;
994
0
  set_default_interp_filters(mbmi, cm->features.interp_filter);
995
0
}
996
997
#if CONFIG_INTERNAL_STATS
998
static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx,
999
                                 int mode_index) {
1000
#else
1001
0
static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1002
0
#endif  // CONFIG_INTERNAL_STATS
1003
0
  MACROBLOCKD *const xd = &x->e_mbd;
1004
0
  TxfmSearchInfo *txfm_info = &x->txfm_search_info;
1005
1006
  // Take a snapshot of the coding context so it can be
1007
  // restored if we decide to encode this way
1008
0
  ctx->rd_stats.skip_txfm = txfm_info->skip_txfm;
1009
1010
0
  ctx->skippable = txfm_info->skip_txfm;
1011
#if CONFIG_INTERNAL_STATS
1012
  ctx->best_mode_index = mode_index;
1013
#endif  // CONFIG_INTERNAL_STATS
1014
0
  ctx->mic = *xd->mi[0];
1015
0
  ctx->skippable = txfm_info->skip_txfm;
1016
0
  av1_copy_mbmi_ext_to_mbmi_ext_frame(&ctx->mbmi_ext_best, &x->mbmi_ext,
1017
0
                                      av1_ref_frame_type(xd->mi[0]->ref_frame));
1018
0
}
1019
1020
0
static int get_pred_buffer(PRED_BUFFER *p, int len) {
1021
0
  for (int i = 0; i < len; i++) {
1022
0
    if (!p[i].in_use) {
1023
0
      p[i].in_use = 1;
1024
0
      return i;
1025
0
    }
1026
0
  }
1027
0
  return -1;
1028
0
}
1029
1030
0
static void free_pred_buffer(PRED_BUFFER *p) {
1031
0
  if (p != NULL) p->in_use = 0;
1032
0
}
1033
1034
static int cost_mv_ref(const ModeCosts *const mode_costs, PREDICTION_MODE mode,
1035
0
                       int16_t mode_context) {
1036
0
  if (is_inter_compound_mode(mode)) {
1037
0
    return mode_costs
1038
0
        ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)];
1039
0
  }
1040
1041
0
  int mode_cost = 0;
1042
0
  int16_t mode_ctx = mode_context & NEWMV_CTX_MASK;
1043
1044
0
  assert(is_inter_mode(mode));
1045
1046
0
  if (mode == NEWMV) {
1047
0
    mode_cost = mode_costs->newmv_mode_cost[mode_ctx][0];
1048
0
    return mode_cost;
1049
0
  } else {
1050
0
    mode_cost = mode_costs->newmv_mode_cost[mode_ctx][1];
1051
0
    mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
1052
1053
0
    if (mode == GLOBALMV) {
1054
0
      mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][0];
1055
0
      return mode_cost;
1056
0
    } else {
1057
0
      mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][1];
1058
0
      mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
1059
0
      mode_cost += mode_costs->refmv_mode_cost[mode_ctx][mode != NEARESTMV];
1060
0
      return mode_cost;
1061
0
    }
1062
0
  }
1063
0
}
1064
1065
static void newmv_diff_bias(MACROBLOCKD *xd, PREDICTION_MODE this_mode,
1066
                            RD_STATS *this_rdc, BLOCK_SIZE bsize, int mv_row,
1067
                            int mv_col, int speed, uint32_t spatial_variance,
1068
0
                            CONTENT_STATE_SB content_state_sb) {
1069
  // Bias against MVs associated with NEWMV mode that are very different from
1070
  // top/left neighbors.
1071
0
  if (this_mode == NEWMV) {
1072
0
    int al_mv_average_row;
1073
0
    int al_mv_average_col;
1074
0
    int row_diff, col_diff;
1075
0
    int above_mv_valid = 0;
1076
0
    int left_mv_valid = 0;
1077
0
    int above_row = INVALID_MV_ROW_COL, above_col = INVALID_MV_ROW_COL;
1078
0
    int left_row = INVALID_MV_ROW_COL, left_col = INVALID_MV_ROW_COL;
1079
0
    if (bsize >= BLOCK_64X64 && content_state_sb.source_sad != kHighSad &&
1080
0
        spatial_variance < 300 &&
1081
0
        (mv_row > 16 || mv_row < -16 || mv_col > 16 || mv_col < -16)) {
1082
0
      this_rdc->rdcost = this_rdc->rdcost << 2;
1083
0
      return;
1084
0
    }
1085
0
    if (xd->above_mbmi) {
1086
0
      above_mv_valid = xd->above_mbmi->mv[0].as_int != INVALID_MV;
1087
0
      above_row = xd->above_mbmi->mv[0].as_mv.row;
1088
0
      above_col = xd->above_mbmi->mv[0].as_mv.col;
1089
0
    }
1090
0
    if (xd->left_mbmi) {
1091
0
      left_mv_valid = xd->left_mbmi->mv[0].as_int != INVALID_MV;
1092
0
      left_row = xd->left_mbmi->mv[0].as_mv.row;
1093
0
      left_col = xd->left_mbmi->mv[0].as_mv.col;
1094
0
    }
1095
0
    if (above_mv_valid && left_mv_valid) {
1096
0
      al_mv_average_row = (above_row + left_row + 1) >> 1;
1097
0
      al_mv_average_col = (above_col + left_col + 1) >> 1;
1098
0
    } else if (above_mv_valid) {
1099
0
      al_mv_average_row = above_row;
1100
0
      al_mv_average_col = above_col;
1101
0
    } else if (left_mv_valid) {
1102
0
      al_mv_average_row = left_row;
1103
0
      al_mv_average_col = left_col;
1104
0
    } else {
1105
0
      al_mv_average_row = al_mv_average_col = 0;
1106
0
    }
1107
0
    row_diff = al_mv_average_row - mv_row;
1108
0
    col_diff = al_mv_average_col - mv_col;
1109
0
    if (row_diff > 80 || row_diff < -80 || col_diff > 80 || col_diff < -80) {
1110
0
      if (bsize >= BLOCK_32X32)
1111
0
        this_rdc->rdcost = this_rdc->rdcost << 1;
1112
0
      else
1113
0
        this_rdc->rdcost = 5 * this_rdc->rdcost >> 2;
1114
0
    }
1115
0
  } else {
1116
    // Bias for speed >= 8 for low spatial variance.
1117
0
    if (speed >= 8 && spatial_variance < 150 &&
1118
0
        (mv_row > 64 || mv_row < -64 || mv_col > 64 || mv_col < -64))
1119
0
      this_rdc->rdcost = 5 * this_rdc->rdcost >> 2;
1120
0
  }
1121
0
}
1122
1123
static void model_rd_for_sb_uv(AV1_COMP *cpi, BLOCK_SIZE plane_bsize,
1124
                               MACROBLOCK *x, MACROBLOCKD *xd,
1125
                               RD_STATS *this_rdc, int64_t *sse_y,
1126
0
                               int start_plane, int stop_plane) {
1127
  // Note our transform coeffs are 8 times an orthogonal transform.
1128
  // Hence quantizer step is also 8 times. To get effective quantizer
1129
  // we need to divide by 8 before sending to modeling function.
1130
0
  unsigned int sse;
1131
0
  int rate;
1132
0
  int64_t dist;
1133
0
  int i;
1134
0
  int64_t tot_sse = *sse_y;
1135
1136
0
  this_rdc->rate = 0;
1137
0
  this_rdc->dist = 0;
1138
0
  this_rdc->skip_txfm = 0;
1139
1140
0
  for (i = start_plane; i <= stop_plane; ++i) {
1141
0
    struct macroblock_plane *const p = &x->plane[i];
1142
0
    struct macroblockd_plane *const pd = &xd->plane[i];
1143
0
    const uint32_t dc_quant = p->dequant_QTX[0];
1144
0
    const uint32_t ac_quant = p->dequant_QTX[1];
1145
0
    const BLOCK_SIZE bs = plane_bsize;
1146
0
    unsigned int var;
1147
0
    if (!x->color_sensitivity[i - 1]) continue;
1148
1149
0
    var = cpi->ppi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf,
1150
0
                                  pd->dst.stride, &sse);
1151
0
    assert(sse >= var);
1152
0
    tot_sse += sse;
1153
1154
0
    av1_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
1155
0
                                 dc_quant >> 3, &rate, &dist);
1156
1157
0
    this_rdc->rate += rate >> 1;
1158
0
    this_rdc->dist += dist << 3;
1159
1160
0
    av1_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs], ac_quant >> 3,
1161
0
                                 &rate, &dist);
1162
1163
0
    this_rdc->rate += rate;
1164
0
    this_rdc->dist += dist << 4;
1165
0
  }
1166
1167
0
  if (this_rdc->rate == 0) {
1168
0
    this_rdc->skip_txfm = 1;
1169
0
  }
1170
1171
0
  if (RDCOST(x->rdmult, this_rdc->rate, this_rdc->dist) >=
1172
0
      RDCOST(x->rdmult, 0, tot_sse << 4)) {
1173
0
    this_rdc->rate = 0;
1174
0
    this_rdc->dist = tot_sse << 4;
1175
0
    this_rdc->skip_txfm = 1;
1176
0
  }
1177
1178
0
  *sse_y = tot_sse;
1179
0
}
1180
1181
/*!\cond */
1182
struct estimate_block_intra_args {
1183
  AV1_COMP *cpi;
1184
  MACROBLOCK *x;
1185
  PREDICTION_MODE mode;
1186
  int skippable;
1187
  RD_STATS *rdc;
1188
};
1189
/*!\endcond */
1190
1191
/*!\brief Estimation of RD cost of an intra mode for Non-RD optimized case.
1192
 *
1193
 * \ingroup nonrd_mode_search
1194
 * \callgraph
1195
 * \callergraph
1196
 * Calculates RD Cost for an intra mode for a single TX block using Hadamard
1197
 * transform.
1198
 * \param[in]    plane          Color plane
1199
 * \param[in]    block          Index of a TX block in a prediction block
1200
 * \param[in]    row            Row of a current TX block
1201
 * \param[in]    col            Column of a current TX block
1202
 * \param[in]    plane_bsize    Block size of a current prediction block
1203
 * \param[in]    tx_size        Transform size
1204
 * \param[in]    arg            Pointer to a structure that holds paramaters
1205
 *                              for intra mode search
1206
 *
1207
 * \return Nothing is returned. Instead, best mode and RD Cost of the best mode
1208
 * are set in \c args->rdc and \c args->mode
1209
 */
1210
static void estimate_block_intra(int plane, int block, int row, int col,
1211
                                 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
1212
0
                                 void *arg) {
1213
0
  struct estimate_block_intra_args *const args = arg;
1214
0
  AV1_COMP *const cpi = args->cpi;
1215
0
  AV1_COMMON *const cm = &cpi->common;
1216
0
  MACROBLOCK *const x = args->x;
1217
0
  MACROBLOCKD *const xd = &x->e_mbd;
1218
0
  struct macroblock_plane *const p = &x->plane[plane];
1219
0
  struct macroblockd_plane *const pd = &xd->plane[plane];
1220
0
  const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
1221
0
  uint8_t *const src_buf_base = p->src.buf;
1222
0
  uint8_t *const dst_buf_base = pd->dst.buf;
1223
0
  const int64_t src_stride = p->src.stride;
1224
0
  const int64_t dst_stride = pd->dst.stride;
1225
0
  RD_STATS this_rdc;
1226
1227
0
  (void)block;
1228
1229
0
  av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
1230
0
  av1_invalid_rd_stats(&this_rdc);
1231
1232
0
  p->src.buf = &src_buf_base[4 * (row * src_stride + col)];
1233
0
  pd->dst.buf = &dst_buf_base[4 * (row * dst_stride + col)];
1234
1235
0
  if (plane == 0) {
1236
0
    block_yrd(cpi, x, 0, 0, &this_rdc, &args->skippable, bsize_tx,
1237
0
              AOMMIN(tx_size, TX_16X16));
1238
0
  } else {
1239
0
    int64_t sse = 0;
1240
0
    model_rd_for_sb_uv(cpi, plane_bsize, x, xd, &this_rdc, &sse, plane, plane);
1241
0
  }
1242
1243
0
  p->src.buf = src_buf_base;
1244
0
  pd->dst.buf = dst_buf_base;
1245
0
  args->rdc->rate += this_rdc.rate;
1246
0
  args->rdc->dist += this_rdc.dist;
1247
0
}
1248
1249
static INLINE void update_thresh_freq_fact(AV1_COMP *cpi, MACROBLOCK *x,
1250
                                           BLOCK_SIZE bsize,
1251
                                           MV_REFERENCE_FRAME ref_frame,
1252
                                           THR_MODES best_mode_idx,
1253
0
                                           PREDICTION_MODE mode) {
1254
0
  const THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
1255
0
  const BLOCK_SIZE min_size = AOMMAX(bsize - 3, BLOCK_4X4);
1256
0
  const BLOCK_SIZE max_size = AOMMIN(bsize + 6, BLOCK_128X128);
1257
0
  for (BLOCK_SIZE bs = min_size; bs <= max_size; bs += 3) {
1258
0
    int *freq_fact = &x->thresh_freq_fact[bs][thr_mode_idx];
1259
0
    if (thr_mode_idx == best_mode_idx) {
1260
0
      *freq_fact -= (*freq_fact >> 4);
1261
0
    } else {
1262
0
      *freq_fact =
1263
0
          AOMMIN(*freq_fact + RD_THRESH_INC,
1264
0
                 cpi->sf.inter_sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
1265
0
    }
1266
0
  }
1267
0
}
1268
1269
#if CONFIG_AV1_TEMPORAL_DENOISING
1270
static void av1_pickmode_ctx_den_update(
1271
    AV1_PICKMODE_CTX_DEN *ctx_den, int64_t zero_last_cost_orig,
1272
    unsigned int ref_frame_cost[REF_FRAMES],
1273
    int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], int reuse_inter_pred,
1274
    BEST_PICKMODE *bp) {
1275
  ctx_den->zero_last_cost_orig = zero_last_cost_orig;
1276
  ctx_den->ref_frame_cost = ref_frame_cost;
1277
  ctx_den->frame_mv = frame_mv;
1278
  ctx_den->reuse_inter_pred = reuse_inter_pred;
1279
  ctx_den->best_tx_size = bp->best_tx_size;
1280
  ctx_den->best_mode = bp->best_mode;
1281
  ctx_den->best_ref_frame = bp->best_ref_frame;
1282
  ctx_den->best_pred_filter = bp->best_pred_filter;
1283
  ctx_den->best_mode_skip_txfm = bp->best_mode_skip_txfm;
1284
}
1285
1286
static void recheck_zeromv_after_denoising(
1287
    AV1_COMP *cpi, MB_MODE_INFO *const mi, MACROBLOCK *x, MACROBLOCKD *const xd,
1288
    AV1_DENOISER_DECISION decision, AV1_PICKMODE_CTX_DEN *ctx_den,
1289
    struct buf_2d yv12_mb[4][MAX_MB_PLANE], RD_STATS *best_rdc,
1290
    BEST_PICKMODE *best_pickmode, BLOCK_SIZE bsize, int mi_row, int mi_col) {
1291
  // If INTRA or GOLDEN reference was selected, re-evaluate ZEROMV on
1292
  // denoised result. Only do this under noise conditions, and if rdcost of
1293
  // ZEROMV onoriginal source is not significantly higher than rdcost of best
1294
  // mode.
1295
  if (cpi->noise_estimate.enabled && cpi->noise_estimate.level > kLow &&
1296
      ctx_den->zero_last_cost_orig < (best_rdc->rdcost << 3) &&
1297
      ((ctx_den->best_ref_frame == INTRA_FRAME && decision >= FILTER_BLOCK) ||
1298
       (ctx_den->best_ref_frame == GOLDEN_FRAME &&
1299
        cpi->svc.number_spatial_layers == 1 &&
1300
        decision == FILTER_ZEROMV_BLOCK))) {
1301
    // Check if we should pick ZEROMV on denoised signal.
1302
    AV1_COMMON *const cm = &cpi->common;
1303
    RD_STATS this_rdc;
1304
    const ModeCosts *mode_costs = &x->mode_costs;
1305
    TxfmSearchInfo *txfm_info = &x->txfm_search_info;
1306
    MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
1307
1308
    mi->mode = GLOBALMV;
1309
    mi->ref_frame[0] = LAST_FRAME;
1310
    mi->ref_frame[1] = NONE_FRAME;
1311
    set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME);
1312
    mi->mv[0].as_int = 0;
1313
    mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
1314
    xd->plane[0].pre[0] = yv12_mb[LAST_FRAME][0];
1315
    av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1316
    model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, 1);
1317
1318
    const int16_t mode_ctx =
1319
        av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame);
1320
    this_rdc.rate += cost_mv_ref(mode_costs, GLOBALMV, mode_ctx);
1321
1322
    this_rdc.rate += ctx_den->ref_frame_cost[LAST_FRAME];
1323
    this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1324
    txfm_info->skip_txfm = this_rdc.skip_txfm;
1325
    // Don't switch to ZEROMV if the rdcost for ZEROMV on denoised source
1326
    // is higher than best_ref mode (on original source).
1327
    if (this_rdc.rdcost > best_rdc->rdcost) {
1328
      this_rdc = *best_rdc;
1329
      mi->mode = best_pickmode->best_mode;
1330
      mi->ref_frame[0] = best_pickmode->best_ref_frame;
1331
      set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME);
1332
      mi->interp_filters = best_pickmode->best_pred_filter;
1333
      if (best_pickmode->best_ref_frame == INTRA_FRAME) {
1334
        mi->mv[0].as_int = INVALID_MV;
1335
      } else {
1336
        mi->mv[0].as_int = ctx_den
1337
                               ->frame_mv[best_pickmode->best_mode]
1338
                                         [best_pickmode->best_ref_frame]
1339
                               .as_int;
1340
        if (ctx_den->reuse_inter_pred) {
1341
          xd->plane[0].pre[0] = yv12_mb[GOLDEN_FRAME][0];
1342
          av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1343
        }
1344
      }
1345
      mi->tx_size = best_pickmode->best_tx_size;
1346
      txfm_info->skip_txfm = best_pickmode->best_mode_skip_txfm;
1347
    } else {
1348
      ctx_den->best_ref_frame = LAST_FRAME;
1349
      *best_rdc = this_rdc;
1350
    }
1351
  }
1352
}
1353
#endif  // CONFIG_AV1_TEMPORAL_DENOISING
1354
1355
0
#define FILTER_SEARCH_SIZE 2
1356
1357
/*!\brief Searches for the best intrpolation filter
1358
 *
1359
 * \ingroup nonrd_mode_search
1360
 * \callgraph
1361
 * \callergraph
1362
 * Iterates through subset of possible interpolation filters (EIGHTTAP_REGULAR,
1363
 * EIGTHTAP_SMOOTH, MULTITAP_SHARP, depending on FILTER_SEARCH_SIZE) and selects
1364
 * the one that gives lowest RD cost. RD cost is calculated using curvfit model.
1365
 * Support for dual filters (different filters in the x & y directions) is
1366
 * allowed if sf.interp_sf.disable_dual_filter = 0.
1367
 *
1368
 * \param[in]    cpi                  Top-level encoder structure
1369
 * \param[in]    x                    Pointer to structure holding all the
1370
 *                                    data for the current macroblock
1371
 * \param[in]    this_rdc             Pointer to calculated RD Cost
1372
 * \param[in]    mi_row               Row index in 4x4 units
1373
 * \param[in]    mi_col               Column index in 4x4 units
1374
 * \param[in]    tmp                  Pointer to a temporary buffer for
1375
 *                                    prediction re-use
1376
 * \param[in]    bsize                Current block size
1377
 * \param[in]    reuse_inter_pred     Flag, indicating prediction re-use
1378
 * \param[out]   this_mode_pred       Pointer to store prediction buffer
1379
 *                                    for prediction re-use
1380
 * \param[out]   this_early_term      Flag, indicating that transform can be
1381
 *                                    skipped
1382
 * \param[in]    use_model_yrd_large  Flag, indicating special logic to handle
1383
 *                                    large blocks
1384
 *
1385
 * \return Nothing is returned. Instead, calculated RD cost is placed to
1386
 * \c this_rdc and best filter is placed to \c mi->interp_filters. In case
1387
 * \c reuse_inter_pred flag is set, this function also ouputs
1388
 * \c this_mode_pred. Also \c this_early_temp is set if transform can be
1389
 * skipped
1390
 */
1391
static void search_filter_ref(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc,
1392
                              int mi_row, int mi_col, PRED_BUFFER *tmp,
1393
                              BLOCK_SIZE bsize, int reuse_inter_pred,
1394
                              PRED_BUFFER **this_mode_pred,
1395
0
                              int *this_early_term, int use_model_yrd_large) {
1396
0
  AV1_COMMON *const cm = &cpi->common;
1397
0
  MACROBLOCKD *const xd = &x->e_mbd;
1398
0
  struct macroblockd_plane *const pd = &xd->plane[0];
1399
0
  MB_MODE_INFO *const mi = xd->mi[0];
1400
0
  const int bw = block_size_wide[bsize];
1401
0
  int dim_factor =
1402
0
      (cpi->sf.interp_sf.disable_dual_filter == 0) ? FILTER_SEARCH_SIZE : 1;
1403
0
  RD_STATS pf_rd_stats[FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE] = { 0 };
1404
0
  TX_SIZE pf_tx_size[FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE] = { 0 };
1405
0
  PRED_BUFFER *current_pred = *this_mode_pred;
1406
0
  int best_skip = 0;
1407
0
  int best_early_term = 0;
1408
0
  int64_t best_cost = INT64_MAX;
1409
0
  int best_filter_index = -1;
1410
0
  for (int i = 0; i < FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE; ++i) {
1411
0
    int64_t cost;
1412
0
    if (cpi->sf.interp_sf.disable_dual_filter &&
1413
0
        filters_ref_set[i].filter_x != filters_ref_set[i].filter_y)
1414
0
      continue;
1415
0
    mi->interp_filters.as_filters.x_filter = filters_ref_set[i].filter_x;
1416
0
    mi->interp_filters.as_filters.y_filter = filters_ref_set[i].filter_y;
1417
0
    av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1418
0
    if (use_model_yrd_large)
1419
0
      model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
1420
0
                                &pf_rd_stats[i], this_early_term, 1);
1421
0
    else
1422
0
      model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[i], 1);
1423
0
    pf_rd_stats[i].rate += av1_get_switchable_rate(
1424
0
        x, xd, cm->features.interp_filter, cm->seq_params->enable_dual_filter);
1425
0
    cost = RDCOST(x->rdmult, pf_rd_stats[i].rate, pf_rd_stats[i].dist);
1426
0
    pf_tx_size[i] = mi->tx_size;
1427
0
    if (cost < best_cost) {
1428
0
      best_filter_index = i;
1429
0
      best_cost = cost;
1430
0
      best_skip = pf_rd_stats[i].skip_txfm;
1431
0
      best_early_term = *this_early_term;
1432
0
      if (reuse_inter_pred) {
1433
0
        if (*this_mode_pred != current_pred) {
1434
0
          free_pred_buffer(*this_mode_pred);
1435
0
          *this_mode_pred = current_pred;
1436
0
        }
1437
0
        current_pred = &tmp[get_pred_buffer(tmp, 3)];
1438
0
        pd->dst.buf = current_pred->data;
1439
0
        pd->dst.stride = bw;
1440
0
      }
1441
0
    }
1442
0
  }
1443
0
  assert(best_filter_index >= 0 &&
1444
0
         best_filter_index < dim_factor * FILTER_SEARCH_SIZE);
1445
0
  if (reuse_inter_pred && *this_mode_pred != current_pred)
1446
0
    free_pred_buffer(current_pred);
1447
1448
0
  mi->interp_filters.as_filters.x_filter =
1449
0
      filters_ref_set[best_filter_index].filter_x;
1450
0
  mi->interp_filters.as_filters.y_filter =
1451
0
      filters_ref_set[best_filter_index].filter_y;
1452
0
  mi->tx_size = pf_tx_size[best_filter_index];
1453
0
  this_rdc->rate = pf_rd_stats[best_filter_index].rate;
1454
0
  this_rdc->dist = pf_rd_stats[best_filter_index].dist;
1455
0
  this_rdc->sse = pf_rd_stats[best_filter_index].sse;
1456
0
  this_rdc->skip_txfm = (best_skip || best_early_term);
1457
0
  *this_early_term = best_early_term;
1458
0
  if (reuse_inter_pred) {
1459
0
    pd->dst.buf = (*this_mode_pred)->data;
1460
0
    pd->dst.stride = (*this_mode_pred)->stride;
1461
0
  } else if (best_filter_index < dim_factor * FILTER_SEARCH_SIZE - 1) {
1462
0
    av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1463
0
  }
1464
0
}
1465
#if !CONFIG_REALTIME_ONLY
1466
#define MOTION_MODE_SEARCH_SIZE 2
1467
1468
static AOM_INLINE int is_warped_mode_allowed(const AV1_COMMON *cm,
1469
                                             MACROBLOCK *const x,
1470
0
                                             const MB_MODE_INFO *mbmi) {
1471
0
  const FeatureFlags *const features = &cm->features;
1472
0
  const MACROBLOCKD *xd = &x->e_mbd;
1473
1474
0
  if (has_second_ref(mbmi)) return 0;
1475
0
  MOTION_MODE last_motion_mode_allowed = SIMPLE_TRANSLATION;
1476
1477
0
  if (features->switchable_motion_mode) {
1478
    // Determine which motion modes to search if more than SIMPLE_TRANSLATION
1479
    // is allowed.
1480
0
    last_motion_mode_allowed = motion_mode_allowed(
1481
0
        xd->global_motion, xd, mbmi, features->allow_warped_motion);
1482
0
  }
1483
1484
0
  if (last_motion_mode_allowed == WARPED_CAUSAL) {
1485
0
    return 1;
1486
0
  }
1487
1488
0
  return 0;
1489
0
}
1490
1491
0
static void calc_num_proj_ref(AV1_COMP *cpi, MACROBLOCK *x, MB_MODE_INFO *mi) {
1492
0
  AV1_COMMON *const cm = &cpi->common;
1493
0
  MACROBLOCKD *const xd = &x->e_mbd;
1494
0
  const FeatureFlags *const features = &cm->features;
1495
1496
0
  mi->num_proj_ref = 1;
1497
0
  WARP_SAMPLE_INFO *const warp_sample_info =
1498
0
      &x->warp_sample_info[mi->ref_frame[0]];
1499
0
  int *pts0 = warp_sample_info->pts;
1500
0
  int *pts_inref0 = warp_sample_info->pts_inref;
1501
0
  MOTION_MODE last_motion_mode_allowed = SIMPLE_TRANSLATION;
1502
1503
0
  if (features->switchable_motion_mode) {
1504
    // Determine which motion modes to search if more than SIMPLE_TRANSLATION
1505
    // is allowed.
1506
0
    last_motion_mode_allowed = motion_mode_allowed(
1507
0
        xd->global_motion, xd, mi, features->allow_warped_motion);
1508
0
  }
1509
1510
0
  if (last_motion_mode_allowed == WARPED_CAUSAL) {
1511
0
    if (warp_sample_info->num < 0) {
1512
0
      warp_sample_info->num = av1_findSamples(cm, xd, pts0, pts_inref0);
1513
0
    }
1514
0
    mi->num_proj_ref = warp_sample_info->num;
1515
0
  }
1516
0
}
1517
1518
static void search_motion_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc,
1519
                               int mi_row, int mi_col, BLOCK_SIZE bsize,
1520
                               int *this_early_term, int use_model_yrd_large,
1521
0
                               int *rate_mv) {
1522
0
  AV1_COMMON *const cm = &cpi->common;
1523
0
  MACROBLOCKD *const xd = &x->e_mbd;
1524
0
  const FeatureFlags *const features = &cm->features;
1525
0
  MB_MODE_INFO *const mi = xd->mi[0];
1526
0
  RD_STATS pf_rd_stats[MOTION_MODE_SEARCH_SIZE] = { 0 };
1527
0
  int best_skip = 0;
1528
0
  int best_early_term = 0;
1529
0
  int64_t best_cost = INT64_MAX;
1530
0
  int best_mode_index = -1;
1531
0
  const int interp_filter = features->interp_filter;
1532
1533
0
  const MOTION_MODE motion_modes[MOTION_MODE_SEARCH_SIZE] = {
1534
0
    SIMPLE_TRANSLATION, WARPED_CAUSAL
1535
0
  };
1536
0
  int mode_search_size = is_warped_mode_allowed(cm, x, mi) ? 2 : 1;
1537
1538
0
  WARP_SAMPLE_INFO *const warp_sample_info =
1539
0
      &x->warp_sample_info[mi->ref_frame[0]];
1540
0
  int *pts0 = warp_sample_info->pts;
1541
0
  int *pts_inref0 = warp_sample_info->pts_inref;
1542
1543
0
  const int total_samples = mi->num_proj_ref;
1544
0
  if (total_samples == 0) {
1545
    // Do not search WARPED_CAUSAL if there are no samples to use to determine
1546
    // warped parameters.
1547
0
    mode_search_size = 1;
1548
0
  }
1549
1550
0
  const MB_MODE_INFO base_mbmi = *mi;
1551
0
  MB_MODE_INFO best_mbmi;
1552
1553
0
  for (int i = 0; i < mode_search_size; ++i) {
1554
0
    int64_t cost = INT64_MAX;
1555
0
    MOTION_MODE motion_mode = motion_modes[i];
1556
0
    *mi = base_mbmi;
1557
0
    mi->motion_mode = motion_mode;
1558
0
    if (motion_mode == SIMPLE_TRANSLATION) {
1559
0
      mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
1560
1561
0
      av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, 0, 0);
1562
0
      if (use_model_yrd_large)
1563
0
        model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
1564
0
                                  &pf_rd_stats[i], this_early_term, 1);
1565
0
      else
1566
0
        model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[i], 1);
1567
0
      pf_rd_stats[i].rate +=
1568
0
          av1_get_switchable_rate(x, xd, cm->features.interp_filter,
1569
0
                                  cm->seq_params->enable_dual_filter);
1570
0
      cost = RDCOST(x->rdmult, pf_rd_stats[i].rate, pf_rd_stats[i].dist);
1571
0
    } else if (motion_mode == WARPED_CAUSAL) {
1572
0
      int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
1573
0
      const ModeCosts *mode_costs = &x->mode_costs;
1574
0
      mi->wm_params.wmtype = DEFAULT_WMTYPE;
1575
0
      mi->interp_filters =
1576
0
          av1_broadcast_interp_filter(av1_unswitchable_filter(interp_filter));
1577
1578
0
      memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0));
1579
0
      memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0));
1580
      // Select the samples according to motion vector difference
1581
0
      if (mi->num_proj_ref > 1) {
1582
0
        mi->num_proj_ref = av1_selectSamples(&mi->mv[0].as_mv, pts, pts_inref,
1583
0
                                             mi->num_proj_ref, bsize);
1584
0
      }
1585
1586
      // Compute the warped motion parameters with a least squares fit
1587
      //  using the collected samples
1588
0
      if (!av1_find_projection(mi->num_proj_ref, pts, pts_inref, bsize,
1589
0
                               mi->mv[0].as_mv.row, mi->mv[0].as_mv.col,
1590
0
                               &mi->wm_params, mi_row, mi_col)) {
1591
0
        if (mi->mode == NEWMV) {
1592
0
          const int_mv mv0 = mi->mv[0];
1593
0
          const WarpedMotionParams wm_params0 = mi->wm_params;
1594
0
          const int num_proj_ref0 = mi->num_proj_ref;
1595
1596
0
          const int_mv ref_mv = av1_get_ref_mv(x, 0);
1597
0
          SUBPEL_MOTION_SEARCH_PARAMS ms_params;
1598
0
          av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize,
1599
0
                                            &ref_mv.as_mv, NULL);
1600
1601
          // Refine MV in a small range.
1602
0
          av1_refine_warped_mv(xd, cm, &ms_params, bsize, pts0, pts_inref0,
1603
0
                               total_samples);
1604
0
          if (mi->mv[0].as_int == ref_mv.as_int) {
1605
0
            continue;
1606
0
          }
1607
1608
0
          if (mv0.as_int != mi->mv[0].as_int) {
1609
            // Keep the refined MV and WM parameters.
1610
0
            int tmp_rate_mv = av1_mv_bit_cost(
1611
0
                &mi->mv[0].as_mv, &ref_mv.as_mv, x->mv_costs->nmv_joint_cost,
1612
0
                x->mv_costs->mv_cost_stack, MV_COST_WEIGHT);
1613
0
            *rate_mv = tmp_rate_mv;
1614
0
          } else {
1615
            // Restore the old MV and WM parameters.
1616
0
            mi->mv[0] = mv0;
1617
0
            mi->wm_params = wm_params0;
1618
0
            mi->num_proj_ref = num_proj_ref0;
1619
0
          }
1620
0
        }
1621
        // Build the warped predictor
1622
0
        av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, 0,
1623
0
                                      av1_num_planes(cm) - 1);
1624
0
        if (use_model_yrd_large)
1625
0
          model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd,
1626
0
                                    &pf_rd_stats[i], this_early_term, 1);
1627
0
        else
1628
0
          model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[i], 1);
1629
1630
0
        pf_rd_stats[i].rate +=
1631
0
            mode_costs->motion_mode_cost[bsize][mi->motion_mode];
1632
0
        cost = RDCOST(x->rdmult, pf_rd_stats[i].rate, pf_rd_stats[i].dist);
1633
0
      } else {
1634
0
        cost = INT64_MAX;
1635
0
      }
1636
0
    }
1637
0
    if (cost < best_cost) {
1638
0
      best_mode_index = i;
1639
0
      best_cost = cost;
1640
0
      best_skip = pf_rd_stats[i].skip_txfm;
1641
0
      best_early_term = *this_early_term;
1642
0
      best_mbmi = *mi;
1643
0
    }
1644
0
  }
1645
0
  assert(best_mode_index >= 0 && best_mode_index < FILTER_SEARCH_SIZE);
1646
1647
0
  *mi = best_mbmi;
1648
0
  this_rdc->rate = pf_rd_stats[best_mode_index].rate;
1649
0
  this_rdc->dist = pf_rd_stats[best_mode_index].dist;
1650
0
  this_rdc->sse = pf_rd_stats[best_mode_index].sse;
1651
0
  this_rdc->skip_txfm = (best_skip || best_early_term);
1652
0
  *this_early_term = best_early_term;
1653
0
  if (best_mode_index < FILTER_SEARCH_SIZE - 1) {
1654
0
    av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, 0, 0);
1655
0
  }
1656
0
}
1657
#endif  // !CONFIG_REALTIME_ONLY
1658
1659
#define COLLECT_PICK_MODE_STAT 0
1660
1661
#if COLLECT_PICK_MODE_STAT
1662
typedef struct _mode_search_stat {
1663
  int32_t num_blocks[BLOCK_SIZES];
1664
  int64_t avg_block_times[BLOCK_SIZES];
1665
  int32_t num_searches[BLOCK_SIZES][MB_MODE_COUNT];
1666
  int32_t num_nonskipped_searches[BLOCK_SIZES][MB_MODE_COUNT];
1667
  int64_t search_times[BLOCK_SIZES][MB_MODE_COUNT];
1668
  int64_t nonskipped_search_times[BLOCK_SIZES][MB_MODE_COUNT];
1669
  struct aom_usec_timer timer1;
1670
  struct aom_usec_timer timer2;
1671
} mode_search_stat;
1672
#endif  // COLLECT_PICK_MODE_STAT
1673
1674
static void compute_intra_yprediction(const AV1_COMMON *cm,
1675
                                      PREDICTION_MODE mode, BLOCK_SIZE bsize,
1676
0
                                      MACROBLOCK *x, MACROBLOCKD *xd) {
1677
0
  const SequenceHeader *seq_params = cm->seq_params;
1678
0
  struct macroblockd_plane *const pd = &xd->plane[0];
1679
0
  struct macroblock_plane *const p = &x->plane[0];
1680
0
  uint8_t *const src_buf_base = p->src.buf;
1681
0
  uint8_t *const dst_buf_base = pd->dst.buf;
1682
0
  const int src_stride = p->src.stride;
1683
0
  const int dst_stride = pd->dst.stride;
1684
0
  int plane = 0;
1685
0
  int row, col;
1686
  // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
1687
  // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
1688
  // transform size varies per plane, look it up in a common way.
1689
0
  const TX_SIZE tx_size = max_txsize_lookup[bsize];
1690
0
  const BLOCK_SIZE plane_bsize =
1691
0
      get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
1692
  // If mb_to_right_edge is < 0 we are in a situation in which
1693
  // the current block size extends into the UMV and we won't
1694
  // visit the sub blocks that are wholly within the UMV.
1695
0
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1696
0
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
1697
  // Keep track of the row and column of the blocks we use so that we know
1698
  // if we are in the unrestricted motion border.
1699
0
  for (row = 0; row < max_blocks_high; row += (1 << tx_size)) {
1700
    // Skip visiting the sub blocks that are wholly within the UMV.
1701
0
    for (col = 0; col < max_blocks_wide; col += (1 << tx_size)) {
1702
0
      p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)];
1703
0
      pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)];
1704
0
      av1_predict_intra_block(
1705
0
          xd, seq_params->sb_size, seq_params->enable_intra_edge_filter,
1706
0
          block_size_wide[bsize], block_size_high[bsize], tx_size, mode, 0, 0,
1707
0
          FILTER_INTRA_MODES, pd->dst.buf, dst_stride, pd->dst.buf, dst_stride,
1708
0
          0, 0, plane);
1709
0
    }
1710
0
  }
1711
0
  p->src.buf = src_buf_base;
1712
0
  pd->dst.buf = dst_buf_base;
1713
0
}
1714
1715
void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
1716
0
                               BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1717
0
  AV1_COMMON *const cm = &cpi->common;
1718
0
  MACROBLOCKD *const xd = &x->e_mbd;
1719
0
  MB_MODE_INFO *const mi = xd->mi[0];
1720
0
  RD_STATS this_rdc, best_rdc;
1721
0
  struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
1722
0
  const TxfmSearchParams *txfm_params = &x->txfm_search_params;
1723
0
  const TX_SIZE intra_tx_size =
1724
0
      AOMMIN(max_txsize_lookup[bsize],
1725
0
             tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]);
1726
0
  int *bmode_costs;
1727
0
  PREDICTION_MODE best_mode = DC_PRED;
1728
0
  const MB_MODE_INFO *above_mi = xd->above_mbmi;
1729
0
  const MB_MODE_INFO *left_mi = xd->left_mbmi;
1730
0
  const PREDICTION_MODE A = av1_above_block_mode(above_mi);
1731
0
  const PREDICTION_MODE L = av1_left_block_mode(left_mi);
1732
0
  const int above_ctx = intra_mode_context[A];
1733
0
  const int left_ctx = intra_mode_context[L];
1734
0
  bmode_costs = x->mode_costs.y_mode_costs[above_ctx][left_ctx];
1735
1736
0
  av1_invalid_rd_stats(&best_rdc);
1737
0
  av1_invalid_rd_stats(&this_rdc);
1738
1739
0
  init_mbmi(mi, DC_PRED, INTRA_FRAME, NONE_FRAME, cm);
1740
0
  mi->mv[0].as_int = mi->mv[1].as_int = INVALID_MV;
1741
1742
  // Change the limit of this loop to add other intra prediction
1743
  // mode tests.
1744
0
  for (int i = 0; i < 4; ++i) {
1745
0
    PREDICTION_MODE this_mode = intra_mode_list[i];
1746
0
    this_rdc.dist = this_rdc.rate = 0;
1747
0
    args.mode = this_mode;
1748
0
    args.skippable = 1;
1749
0
    args.rdc = &this_rdc;
1750
0
    mi->tx_size = intra_tx_size;
1751
0
    mi->mode = this_mode;
1752
0
    av1_foreach_transformed_block_in_plane(xd, bsize, 0, estimate_block_intra,
1753
0
                                           &args);
1754
0
    const int skip_ctx = av1_get_skip_txfm_context(xd);
1755
0
    if (args.skippable) {
1756
0
      this_rdc.rate = x->mode_costs.skip_txfm_cost[skip_ctx][1];
1757
0
    } else {
1758
0
      this_rdc.rate += x->mode_costs.skip_txfm_cost[skip_ctx][0];
1759
0
    }
1760
0
    this_rdc.rate += bmode_costs[this_mode];
1761
0
    this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1762
1763
0
    if (this_rdc.rdcost < best_rdc.rdcost) {
1764
0
      best_rdc = this_rdc;
1765
0
      best_mode = this_mode;
1766
0
      if (!this_rdc.skip_txfm) {
1767
0
        memset(ctx->blk_skip, 0,
1768
0
               sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
1769
0
      }
1770
0
    }
1771
0
  }
1772
1773
0
  mi->mode = best_mode;
1774
  // Keep DC for UV since mode test is based on Y channel only.
1775
0
  mi->uv_mode = DC_PRED;
1776
0
  *rd_cost = best_rdc;
1777
1778
#if CONFIG_INTERNAL_STATS
1779
  store_coding_context(x, ctx, mi->mode);
1780
#else
1781
0
  store_coding_context(x, ctx);
1782
0
#endif  // CONFIG_INTERNAL_STATS
1783
0
}
1784
1785
0
static AOM_INLINE int is_same_gf_and_last_scale(AV1_COMMON *cm) {
1786
0
  struct scale_factors *const sf_last = get_ref_scale_factors(cm, LAST_FRAME);
1787
0
  struct scale_factors *const sf_golden =
1788
0
      get_ref_scale_factors(cm, GOLDEN_FRAME);
1789
0
  return ((sf_last->x_scale_fp == sf_golden->x_scale_fp) &&
1790
0
          (sf_last->y_scale_fp == sf_golden->y_scale_fp));
1791
0
}
1792
1793
static AOM_INLINE void get_ref_frame_use_mask(AV1_COMP *cpi, MACROBLOCK *x,
1794
                                              MB_MODE_INFO *mi, int mi_row,
1795
                                              int mi_col, int bsize,
1796
                                              int gf_temporal_ref,
1797
                                              int use_ref_frame[],
1798
0
                                              int *force_skip_low_temp_var) {
1799
0
  AV1_COMMON *const cm = &cpi->common;
1800
0
  const struct segmentation *const seg = &cm->seg;
1801
0
  const int is_small_sb = (cm->seq_params->sb_size == BLOCK_64X64);
1802
1803
  // For SVC the usage of alt_ref is determined by the ref_frame_flags.
1804
0
  int use_alt_ref_frame =
1805
0
      cpi->ppi->use_svc || cpi->sf.rt_sf.use_nonrd_altref_frame;
1806
0
  int use_golden_ref_frame = 1;
1807
0
  int use_last_ref_frame = 1;
1808
1809
0
  if (cpi->ppi->use_svc)
1810
0
    use_last_ref_frame =
1811
0
        cpi->ref_frame_flags & AOM_LAST_FLAG ? use_last_ref_frame : 0;
1812
1813
  // Only remove golden and altref reference below if last is a reference,
1814
  // which may not be the case for svc.
1815
0
  if (use_last_ref_frame && cpi->rc.frames_since_golden == 0 &&
1816
0
      gf_temporal_ref) {
1817
0
    use_golden_ref_frame = 0;
1818
0
  }
1819
0
  if (use_last_ref_frame && cpi->sf.rt_sf.short_circuit_low_temp_var &&
1820
0
      x->nonrd_prune_ref_frame_search) {
1821
0
    if (is_small_sb)
1822
0
      *force_skip_low_temp_var = av1_get_force_skip_low_temp_var_small_sb(
1823
0
          &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
1824
0
    else
1825
0
      *force_skip_low_temp_var = av1_get_force_skip_low_temp_var(
1826
0
          &x->part_search_info.variance_low[0], mi_row, mi_col, bsize);
1827
    // If force_skip_low_temp_var is set, skip golden reference.
1828
0
    if (*force_skip_low_temp_var) {
1829
0
      use_golden_ref_frame = 0;
1830
0
      use_alt_ref_frame = 0;
1831
0
    }
1832
0
  }
1833
1834
0
  if (use_last_ref_frame &&
1835
0
      (x->nonrd_prune_ref_frame_search > 2 ||
1836
0
       (x->nonrd_prune_ref_frame_search > 1 && bsize > BLOCK_64X64))) {
1837
0
    use_golden_ref_frame = 0;
1838
0
    use_alt_ref_frame = 0;
1839
0
  }
1840
1841
0
  if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
1842
0
      get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) {
1843
0
    use_golden_ref_frame = 1;
1844
0
    use_alt_ref_frame = 0;
1845
0
  }
1846
1847
0
  use_alt_ref_frame =
1848
0
      cpi->ref_frame_flags & AOM_ALT_FLAG ? use_alt_ref_frame : 0;
1849
0
  use_golden_ref_frame =
1850
0
      cpi->ref_frame_flags & AOM_GOLD_FLAG ? use_golden_ref_frame : 0;
1851
1852
0
  use_ref_frame[ALTREF_FRAME] = use_alt_ref_frame;
1853
0
  use_ref_frame[GOLDEN_FRAME] = use_golden_ref_frame;
1854
0
  use_ref_frame[LAST_FRAME] = use_last_ref_frame;
1855
  // For now keep this assert on, but we should remove it for svc mode,
1856
  // as the user may want to generate an intra-only frame (no inter-modes).
1857
  // Remove this assert in subsequent CL when nonrd_pickmode is tested for the
1858
  // case of intra-only frame (no references enabled).
1859
0
  assert(use_last_ref_frame || use_golden_ref_frame || use_alt_ref_frame);
1860
0
}
1861
1862
/*!\brief Estimates best intra mode for inter mode search
1863
 *
1864
 * \ingroup nonrd_mode_search
1865
 * \callgraph
1866
 * \callergraph
1867
 *
1868
 * Using heuristics based on best inter mode, block size, and other decides
1869
 * whether to check intra modes. If so, estimates and selects best intra mode
1870
 * from the reduced set of intra modes (max 4 intra modes checked)
1871
 *
1872
 * \param[in]    cpi                      Top-level encoder structure
1873
 * \param[in]    x                        Pointer to structure holding all the
1874
 *                                        data for the current macroblock
1875
 * \param[in]    bsize                    Current block size
1876
 * \param[in]    use_modeled_non_rd_cost  Flag, indicating usage of curvfit
1877
 *                                        model for RD cost
1878
 * \param[in]    best_early_term          Flag, indicating that TX for the
1879
 *                                        best inter mode was skipped
1880
 * \param[in]    ref_cost_intra           Cost of signalling intra mode
1881
 * \param[in]    reuse_prediction         Flag, indicating prediction re-use
1882
 * \param[in]    orig_dst                 Original destination buffer
1883
 * \param[in]    tmp_buffers              Pointer to a temporary buffers for
1884
 *                                        prediction re-use
1885
 * \param[out]   this_mode_pred           Pointer to store prediction buffer
1886
 *                                        for prediction re-use
1887
 * \param[in]    best_rdc                 Pointer to RD cost for the best
1888
 *                                        selected intra mode
1889
 * \param[in]    best_pickmode            Pointer to a structure containing
1890
 *                                        best mode picked so far
1891
 *
1892
 * \return Nothing is returned. Instead, calculated RD cost is placed to
1893
 * \c best_rdc and best selected mode is placed to \c best_pickmode
1894
 */
1895
static void estimate_intra_mode(
1896
    AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int use_modeled_non_rd_cost,
1897
    int best_early_term, unsigned int ref_cost_intra, int reuse_prediction,
1898
    struct buf_2d *orig_dst, PRED_BUFFER *tmp_buffers,
1899
    PRED_BUFFER **this_mode_pred, RD_STATS *best_rdc,
1900
0
    BEST_PICKMODE *best_pickmode) {
1901
0
  AV1_COMMON *const cm = &cpi->common;
1902
0
  MACROBLOCKD *const xd = &x->e_mbd;
1903
0
  MB_MODE_INFO *const mi = xd->mi[0];
1904
0
  const TxfmSearchParams *txfm_params = &x->txfm_search_params;
1905
0
  const unsigned char segment_id = mi->segment_id;
1906
0
  const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize];
1907
0
  const int *const rd_thresh_freq_fact = x->thresh_freq_fact[bsize];
1908
0
  const int mi_row = xd->mi_row;
1909
0
  const int mi_col = xd->mi_col;
1910
0
  const int num_8x8_blocks = mi_size_wide[bsize] * mi_size_high[bsize] / 4;
1911
0
  struct macroblockd_plane *const pd = &xd->plane[0];
1912
1913
0
  const CommonQuantParams *quant_params = &cm->quant_params;
1914
1915
0
  RD_STATS this_rdc;
1916
1917
0
  int intra_cost_penalty = av1_get_intra_cost_penalty(
1918
0
      quant_params->base_qindex, quant_params->y_dc_delta_q,
1919
0
      cm->seq_params->bit_depth);
1920
0
  int64_t inter_mode_thresh = RDCOST(x->rdmult, intra_cost_penalty, 0);
1921
0
  int perform_intra_pred = cpi->sf.rt_sf.check_intra_pred_nonrd;
1922
  // For spatial enhancemanent layer: turn off intra prediction if the
1923
  // previous spatial layer as golden ref is not chosen as best reference.
1924
  // only do this for temporal enhancement layer and on non-key frames.
1925
0
  if (cpi->svc.spatial_layer_id > 0 &&
1926
0
      best_pickmode->best_ref_frame != GOLDEN_FRAME &&
1927
0
      cpi->svc.temporal_layer_id > 0 &&
1928
0
      !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame)
1929
0
    perform_intra_pred = 0;
1930
1931
0
  int do_early_exit_rdthresh = 1;
1932
1933
0
  uint32_t spatial_var_thresh = 50;
1934
0
  int motion_thresh = 32;
1935
  // Adjust thresholds to make intra mode likely tested if the other
1936
  // references (golden, alt) are skipped/not checked. For now always
1937
  // adjust for svc mode.
1938
0
  if (cpi->ppi->use_svc || (cpi->sf.rt_sf.use_nonrd_altref_frame == 0 &&
1939
0
                            cpi->sf.rt_sf.nonrd_prune_ref_frame_search > 0)) {
1940
0
    spatial_var_thresh = 150;
1941
0
    motion_thresh = 0;
1942
0
  }
1943
1944
  // Some adjustments to checking intra mode based on source variance.
1945
0
  if (x->source_variance < spatial_var_thresh) {
1946
    // If the best inter mode is large motion or non-LAST ref reduce intra cost
1947
    // penalty, so intra mode is more likely tested.
1948
0
    if (best_rdc->rdcost != INT64_MAX &&
1949
0
        (best_pickmode->best_ref_frame != LAST_FRAME ||
1950
0
         abs(mi->mv[0].as_mv.row) >= motion_thresh ||
1951
0
         abs(mi->mv[0].as_mv.col) >= motion_thresh)) {
1952
0
      intra_cost_penalty = intra_cost_penalty >> 2;
1953
0
      inter_mode_thresh = RDCOST(x->rdmult, intra_cost_penalty, 0);
1954
0
      do_early_exit_rdthresh = 0;
1955
0
    }
1956
    // For big blocks worth checking intra (since only DC will be checked),
1957
    // even if best_early_term is set.
1958
0
    if (bsize >= BLOCK_32X32) best_early_term = 0;
1959
0
  } else if (cpi->sf.rt_sf.source_metrics_sb_nonrd &&
1960
0
             x->content_state_sb.source_sad == kLowSad) {
1961
0
    perform_intra_pred = 0;
1962
0
  }
1963
1964
0
  if (cpi->sf.rt_sf.skip_intra_pred_if_tx_skip && best_rdc->skip_txfm &&
1965
0
      best_pickmode->best_mode_initial_skip_flag) {
1966
0
    perform_intra_pred = 0;
1967
0
  }
1968
1969
0
  if (!(best_rdc->rdcost == INT64_MAX ||
1970
0
        (perform_intra_pred && !best_early_term &&
1971
0
         best_rdc->rdcost > inter_mode_thresh &&
1972
0
         bsize <= cpi->sf.part_sf.max_intra_bsize))) {
1973
0
    return;
1974
0
  }
1975
1976
0
  struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
1977
0
  TX_SIZE intra_tx_size = AOMMIN(
1978
0
      AOMMIN(max_txsize_lookup[bsize],
1979
0
             tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]),
1980
0
      TX_16X16);
1981
1982
0
  PRED_BUFFER *const best_pred = best_pickmode->best_pred;
1983
0
  if (reuse_prediction && best_pred != NULL) {
1984
0
    const int bh = block_size_high[bsize];
1985
0
    const int bw = block_size_wide[bsize];
1986
0
    if (best_pred->data == orig_dst->buf) {
1987
0
      *this_mode_pred = &tmp_buffers[get_pred_buffer(tmp_buffers, 3)];
1988
0
      aom_convolve_copy(best_pred->data, best_pred->stride,
1989
0
                        (*this_mode_pred)->data, (*this_mode_pred)->stride, bw,
1990
0
                        bh);
1991
0
      best_pickmode->best_pred = *this_mode_pred;
1992
0
    }
1993
0
  }
1994
0
  pd->dst = *orig_dst;
1995
1996
0
  for (int i = 0; i < 4; ++i) {
1997
0
    const PREDICTION_MODE this_mode = intra_mode_list[i];
1998
0
    const THR_MODES mode_index = mode_idx[INTRA_FRAME][mode_offset(this_mode)];
1999
0
    const int64_t mode_rd_thresh = rd_threshes[mode_index];
2000
2001
0
    if (!((1 << this_mode) & cpi->sf.rt_sf.intra_y_mode_bsize_mask_nrd[bsize]))
2002
0
      continue;
2003
2004
0
    if (rd_less_than_thresh(best_rdc->rdcost, mode_rd_thresh,
2005
0
                            rd_thresh_freq_fact[mode_index]) &&
2006
0
        (do_early_exit_rdthresh || this_mode == SMOOTH_PRED)) {
2007
0
      continue;
2008
0
    }
2009
0
    const BLOCK_SIZE uv_bsize = get_plane_block_size(
2010
0
        bsize, xd->plane[1].subsampling_x, xd->plane[1].subsampling_y);
2011
2012
0
    mi->mode = this_mode;
2013
0
    mi->ref_frame[0] = INTRA_FRAME;
2014
0
    mi->ref_frame[1] = NONE_FRAME;
2015
2016
0
    av1_invalid_rd_stats(&this_rdc);
2017
0
    args.mode = this_mode;
2018
0
    args.skippable = 1;
2019
0
    args.rdc = &this_rdc;
2020
0
    mi->tx_size = intra_tx_size;
2021
0
    compute_intra_yprediction(cm, this_mode, bsize, x, xd);
2022
    // Look into selecting tx_size here, based on prediction residual.
2023
0
    if (use_modeled_non_rd_cost)
2024
0
      model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, 1);
2025
0
    else
2026
0
      block_yrd(cpi, x, mi_row, mi_col, &this_rdc, &args.skippable, bsize,
2027
0
                mi->tx_size);
2028
    // TODO(kyslov@) Need to account for skippable
2029
0
    if (x->color_sensitivity[0]) {
2030
0
      av1_foreach_transformed_block_in_plane(xd, uv_bsize, 1,
2031
0
                                             estimate_block_intra, &args);
2032
0
    }
2033
0
    if (x->color_sensitivity[1]) {
2034
0
      av1_foreach_transformed_block_in_plane(xd, uv_bsize, 2,
2035
0
                                             estimate_block_intra, &args);
2036
0
    }
2037
2038
0
    int mode_cost = 0;
2039
0
    if (av1_is_directional_mode(this_mode) && av1_use_angle_delta(bsize)) {
2040
0
      mode_cost +=
2041
0
          x->mode_costs.angle_delta_cost[this_mode - V_PRED]
2042
0
                                        [MAX_ANGLE_DELTA +
2043
0
                                         mi->angle_delta[PLANE_TYPE_Y]];
2044
0
    }
2045
0
    if (this_mode == DC_PRED && av1_filter_intra_allowed_bsize(cm, bsize)) {
2046
0
      mode_cost += x->mode_costs.filter_intra_cost[bsize][0];
2047
0
    }
2048
0
    this_rdc.rate += ref_cost_intra;
2049
0
    this_rdc.rate += intra_cost_penalty;
2050
0
    this_rdc.rate += mode_cost;
2051
0
    this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
2052
2053
0
    if (this_rdc.rdcost < best_rdc->rdcost) {
2054
0
      *best_rdc = this_rdc;
2055
0
      best_pickmode->best_mode = this_mode;
2056
0
      best_pickmode->best_tx_size = mi->tx_size;
2057
0
      best_pickmode->best_ref_frame = INTRA_FRAME;
2058
0
      best_pickmode->best_second_ref_frame = NONE;
2059
0
      if (!this_rdc.skip_txfm) {
2060
0
        memcpy(best_pickmode->blk_skip, x->txfm_search_info.blk_skip,
2061
0
               sizeof(x->txfm_search_info.blk_skip[0]) * num_8x8_blocks);
2062
0
      }
2063
0
      mi->uv_mode = this_mode;
2064
0
      mi->mv[0].as_int = INVALID_MV;
2065
0
      mi->mv[1].as_int = INVALID_MV;
2066
0
    }
2067
0
  }
2068
0
  mi->tx_size = best_pickmode->best_tx_size;
2069
0
}
2070
2071
static AOM_INLINE int is_filter_search_enabled(const AV1_COMP *cpi, int mi_row,
2072
                                               int mi_col, BLOCK_SIZE bsize,
2073
0
                                               int segment_id) {
2074
0
  const AV1_COMMON *const cm = &cpi->common;
2075
0
  int enable_filter_search = 0;
2076
2077
0
  if (cpi->sf.rt_sf.use_nonrd_filter_search) {
2078
0
    enable_filter_search = 1;
2079
0
    if (cpi->sf.interp_sf.cb_pred_filter_search) {
2080
0
      const int bsl = mi_size_wide_log2[bsize];
2081
0
      enable_filter_search =
2082
0
          (((mi_row + mi_col) >> bsl) +
2083
0
           get_chessboard_index(cm->current_frame.frame_number)) &
2084
0
          0x1;
2085
0
      if (cyclic_refresh_segment_id_boosted(segment_id))
2086
0
        enable_filter_search = 1;
2087
0
    }
2088
0
  }
2089
0
  return enable_filter_search;
2090
0
}
2091
2092
static AOM_INLINE int skip_mode_by_threshold(
2093
    PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, int_mv mv,
2094
    int frames_since_golden, const int *const rd_threshes,
2095
    const int *const rd_thresh_freq_fact, int64_t best_cost, int best_skip,
2096
0
    int extra_shift) {
2097
0
  int skip_this_mode = 0;
2098
0
  const THR_MODES mode_index = mode_idx[ref_frame][INTER_OFFSET(mode)];
2099
0
  int64_t mode_rd_thresh =
2100
0
      best_skip ? ((int64_t)rd_threshes[mode_index]) << (extra_shift + 1)
2101
0
                : ((int64_t)rd_threshes[mode_index]) << extra_shift;
2102
2103
  // Increase mode_rd_thresh value for non-LAST for improved encoding
2104
  // speed
2105
0
  if (ref_frame != LAST_FRAME) {
2106
0
    mode_rd_thresh = mode_rd_thresh << 1;
2107
0
    if (ref_frame == GOLDEN_FRAME && frames_since_golden > 4)
2108
0
      mode_rd_thresh = mode_rd_thresh << (extra_shift + 1);
2109
0
  }
2110
2111
0
  if (rd_less_than_thresh(best_cost, mode_rd_thresh,
2112
0
                          rd_thresh_freq_fact[mode_index]))
2113
0
    if (mv.as_int != 0) skip_this_mode = 1;
2114
2115
0
  return skip_this_mode;
2116
0
}
2117
2118
static AOM_INLINE int skip_mode_by_low_temp(
2119
    PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
2120
0
    CONTENT_STATE_SB content_state_sb, int_mv mv, int force_skip_low_temp_var) {
2121
  // Skip non-zeromv mode search for non-LAST frame if force_skip_low_temp_var
2122
  // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped
2123
  // later.
2124
0
  if (force_skip_low_temp_var && ref_frame != LAST_FRAME && mv.as_int != 0) {
2125
0
    return 1;
2126
0
  }
2127
2128
0
  if (content_state_sb.source_sad != kHighSad && bsize >= BLOCK_64X64 &&
2129
0
      force_skip_low_temp_var && mode == NEWMV) {
2130
0
    return 1;
2131
0
  }
2132
0
  return 0;
2133
0
}
2134
2135
static AOM_INLINE int skip_mode_by_bsize_and_ref_frame(
2136
    PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize,
2137
0
    int extra_prune, unsigned int sse_zeromv_norm, int more_prune) {
2138
0
  const unsigned int thresh_skip_golden = 500;
2139
2140
0
  if (ref_frame != LAST_FRAME && sse_zeromv_norm < thresh_skip_golden &&
2141
0
      mode == NEWMV)
2142
0
    return 1;
2143
2144
0
  if (bsize == BLOCK_128X128 && mode == NEWMV) return 1;
2145
2146
  // Skip testing non-LAST if this flag is set.
2147
0
  if (extra_prune) {
2148
0
    if (extra_prune > 1 && ref_frame != LAST_FRAME &&
2149
0
        (bsize > BLOCK_16X16 && mode == NEWMV))
2150
0
      return 1;
2151
2152
0
    if (ref_frame != LAST_FRAME && mode == NEARMV) return 1;
2153
2154
0
    if (more_prune && bsize >= BLOCK_32X32 && mode == NEARMV) return 1;
2155
0
  }
2156
0
  return 0;
2157
0
}
2158
2159
void set_color_sensitivity(AV1_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
2160
                           BLOCK_SIZE bsize, int y_sad,
2161
0
                           unsigned int source_variance) {
2162
0
  const int factor = (bsize >= BLOCK_32X32) ? 2 : 3;
2163
0
  NOISE_LEVEL noise_level = kLow;
2164
0
  int norm_sad =
2165
0
      y_sad >> (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
2166
  // If the spatial source variance is high and the normalized y_sad
2167
  // is low, then y-channel is likely good for mode estimation, so keep
2168
  // color_sensitivity off. For low noise content for now, since there is
2169
  // some bdrate regression for noisy color clip.
2170
0
  if (cpi->noise_estimate.enabled)
2171
0
    noise_level = av1_noise_estimate_extract_level(&cpi->noise_estimate);
2172
0
  if (noise_level == kLow && source_variance > 1000 && norm_sad < 50) {
2173
0
    x->color_sensitivity[0] = 0;
2174
0
    x->color_sensitivity[1] = 0;
2175
0
    return;
2176
0
  }
2177
0
  for (int i = 1; i <= 2; ++i) {
2178
0
    if (x->color_sensitivity[i - 1] == 2) {
2179
0
      struct macroblock_plane *const p = &x->plane[i];
2180
0
      struct macroblockd_plane *const pd = &xd->plane[i];
2181
0
      const BLOCK_SIZE bs =
2182
0
          get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
2183
0
      const int uv_sad = cpi->ppi->fn_ptr[bs].sdf(p->src.buf, p->src.stride,
2184
0
                                                  pd->dst.buf, pd->dst.stride);
2185
0
      const int norm_uv_sad =
2186
0
          uv_sad >> (b_width_log2_lookup[bs] + b_height_log2_lookup[bs]);
2187
0
      x->color_sensitivity[i - 1] =
2188
0
          uv_sad > (factor * (y_sad >> 3)) && norm_uv_sad > 40;
2189
0
    }
2190
0
  }
2191
0
}
2192
2193
void setup_compound_prediction(AV1_COMP *cpi, MACROBLOCK *x,
2194
                               struct buf_2d yv12_mb[8][MAX_MB_PLANE],
2195
                               int *use_ref_frame_mask, int flag_comp,
2196
0
                               int *ref_mv_idx) {
2197
0
  AV1_COMMON *const cm = &cpi->common;
2198
0
  MACROBLOCKD *const xd = &x->e_mbd;
2199
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
2200
0
  MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
2201
0
  MV_REFERENCE_FRAME rf[2] = { LAST_FRAME, GOLDEN_FRAME };
2202
0
  MV_REFERENCE_FRAME ref_frame_comp;
2203
0
  if (flag_comp == 1) {
2204
0
    rf[1] = LAST2_FRAME;
2205
0
  } else if (flag_comp == 2) {
2206
0
    rf[1] = ALTREF_FRAME;
2207
0
  }
2208
0
  if (!use_ref_frame_mask[rf[1]]) {
2209
    // Need to setup pred_block, if it hasn't been done in find_predictors.
2210
0
    const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, rf[1]);
2211
0
    const int num_planes = av1_num_planes(cm);
2212
0
    if (yv12 != NULL) {
2213
0
      const struct scale_factors *const sf =
2214
0
          get_ref_scale_factors_const(cm, rf[1]);
2215
0
      av1_setup_pred_block(xd, yv12_mb[rf[1]], yv12, sf, sf, num_planes);
2216
0
    }
2217
0
  }
2218
0
  ref_frame_comp = av1_ref_frame_type(rf);
2219
0
  mbmi_ext->mode_context[ref_frame_comp] = 0;
2220
0
  mbmi_ext->ref_mv_count[ref_frame_comp] = UINT8_MAX;
2221
0
  av1_find_mv_refs(cm, xd, mbmi, ref_frame_comp, mbmi_ext->ref_mv_count,
2222
0
                   xd->ref_mv_stack, xd->weight, NULL, mbmi_ext->global_mvs,
2223
0
                   mbmi_ext->mode_context);
2224
0
  av1_copy_usable_ref_mv_stack_and_weight(xd, mbmi_ext, ref_frame_comp);
2225
0
  *ref_mv_idx = mbmi->ref_mv_idx + 1;
2226
0
}
2227
2228
static void set_compound_mode(MACROBLOCK *x, int comp_index, int ref_frame,
2229
                              int ref_frame2, int ref_mv_idx,
2230
                              int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],
2231
0
                              PREDICTION_MODE *this_mode) {
2232
0
  MACROBLOCKD *const xd = &x->e_mbd;
2233
0
  MB_MODE_INFO *const mi = xd->mi[0];
2234
0
  *this_mode = GLOBAL_GLOBALMV;
2235
0
  mi->ref_frame[0] = ref_frame;
2236
0
  mi->ref_frame[1] = ref_frame2;
2237
0
  mi->compound_idx = 1;
2238
0
  mi->comp_group_idx = 0;
2239
0
  mi->interinter_comp.type = COMPOUND_AVERAGE;
2240
0
  MV_REFERENCE_FRAME ref_frame_comp = av1_ref_frame_type(mi->ref_frame);
2241
0
  if (comp_index % 3 == 0) {
2242
0
    frame_mv[*this_mode][ref_frame].as_int = 0;
2243
0
    frame_mv[*this_mode][ref_frame2].as_int = 0;
2244
0
  } else if (comp_index % 3 == 1) {
2245
0
    *this_mode = NEAREST_NEARESTMV;
2246
0
    frame_mv[*this_mode][ref_frame].as_int =
2247
0
        xd->ref_mv_stack[ref_frame_comp][0].this_mv.as_int;
2248
0
    frame_mv[*this_mode][ref_frame2].as_int =
2249
0
        xd->ref_mv_stack[ref_frame_comp][0].comp_mv.as_int;
2250
0
  } else if (comp_index % 3 == 2) {
2251
0
    *this_mode = NEAR_NEARMV;
2252
0
    frame_mv[*this_mode][ref_frame].as_int =
2253
0
        xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].this_mv.as_int;
2254
0
    frame_mv[*this_mode][ref_frame2].as_int =
2255
0
        xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].comp_mv.as_int;
2256
0
  }
2257
0
}
2258
2259
void av1_nonrd_pick_inter_mode_sb(AV1_COMP *cpi, TileDataEnc *tile_data,
2260
                                  MACROBLOCK *x, RD_STATS *rd_cost,
2261
0
                                  BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
2262
0
  AV1_COMMON *const cm = &cpi->common;
2263
0
  SVC *const svc = &cpi->svc;
2264
0
  MACROBLOCKD *const xd = &x->e_mbd;
2265
0
  MB_MODE_INFO *const mi = xd->mi[0];
2266
0
  struct macroblockd_plane *const pd = &xd->plane[0];
2267
0
  const InterpFilter filter_ref = cm->features.interp_filter;
2268
0
  const InterpFilter default_interp_filter = EIGHTTAP_REGULAR;
2269
0
  BEST_PICKMODE best_pickmode;
2270
#if COLLECT_PICK_MODE_STAT
2271
  static mode_search_stat ms_stat;
2272
#endif
2273
0
  MV_REFERENCE_FRAME ref_frame, ref_frame2;
2274
0
  int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES];
2275
0
  int_mv frame_mv_best[MB_MODE_COUNT][REF_FRAMES];
2276
0
  uint8_t mode_checked[MB_MODE_COUNT][REF_FRAMES];
2277
0
  struct buf_2d yv12_mb[REF_FRAMES][MAX_MB_PLANE];
2278
0
  RD_STATS this_rdc, best_rdc;
2279
0
  const unsigned char segment_id = mi->segment_id;
2280
0
  const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize];
2281
0
  const int *const rd_thresh_freq_fact = x->thresh_freq_fact[bsize];
2282
0
  int best_early_term = 0;
2283
0
  unsigned int ref_costs_single[REF_FRAMES];
2284
0
  int force_skip_low_temp_var = 0;
2285
0
  int use_ref_frame_mask[REF_FRAMES] = { 0 };
2286
0
  unsigned int sse_zeromv_norm = UINT_MAX;
2287
  // Use mode set that includes zeromv (via globalmv) for speed >= 9 for
2288
  // content with low motion.
2289
0
  int use_zeromv =
2290
0
      cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN ||
2291
0
      ((cpi->oxcf.speed >= 9 && cpi->rc.avg_frame_low_motion > 70) ||
2292
0
       cpi->sf.rt_sf.nonrd_agressive_skip);
2293
0
  int skip_pred_mv = 0;
2294
0
  const int num_inter_modes =
2295
0
      use_zeromv ? NUM_INTER_MODES_REDUCED : NUM_INTER_MODES_RT;
2296
0
  const REF_MODE *const ref_mode_set =
2297
0
      use_zeromv ? ref_mode_set_reduced : ref_mode_set_rt;
2298
0
  PRED_BUFFER tmp[4];
2299
0
  DECLARE_ALIGNED(16, uint8_t, pred_buf[3 * 128 * 128]);
2300
0
  PRED_BUFFER *this_mode_pred = NULL;
2301
0
  const int reuse_inter_pred = cpi->sf.rt_sf.reuse_inter_pred_nonrd &&
2302
0
                               cm->seq_params->bit_depth == AOM_BITS_8;
2303
2304
0
  const int bh = block_size_high[bsize];
2305
0
  const int bw = block_size_wide[bsize];
2306
0
  const int pixels_in_block = bh * bw;
2307
0
  const int num_8x8_blocks = ctx->num_4x4_blk / 4;
2308
0
  struct buf_2d orig_dst = pd->dst;
2309
0
  const CommonQuantParams *quant_params = &cm->quant_params;
2310
0
  const TxfmSearchParams *txfm_params = &x->txfm_search_params;
2311
0
  TxfmSearchInfo *txfm_info = &x->txfm_search_info;
2312
#if COLLECT_PICK_MODE_STAT
2313
  aom_usec_timer_start(&ms_stat.timer2);
2314
#endif
2315
0
  int64_t thresh_sad_pred = INT64_MAX;
2316
0
  const int mi_row = xd->mi_row;
2317
0
  const int mi_col = xd->mi_col;
2318
0
  int svc_mv_col = 0;
2319
0
  int svc_mv_row = 0;
2320
0
  int force_mv_inter_layer = 0;
2321
0
  int use_modeled_non_rd_cost = 0;
2322
0
  int comp_pred = 0;
2323
0
  int num_comp_modes_ref = 0;
2324
0
  int tot_num_comp_modes = 9;
2325
0
  int ref_mv_idx = 0;
2326
#if CONFIG_AV1_TEMPORAL_DENOISING
2327
  const int denoise_recheck_zeromv = 1;
2328
  AV1_PICKMODE_CTX_DEN ctx_den;
2329
  int64_t zero_last_cost_orig = INT64_MAX;
2330
  int denoise_svc_pickmode = 1;
2331
  const int resize_pending = is_frame_resize_pending(cpi);
2332
#endif
2333
0
  x->color_sensitivity[0] = x->color_sensitivity_sb[0];
2334
0
  x->color_sensitivity[1] = x->color_sensitivity_sb[1];
2335
0
  init_best_pickmode(&best_pickmode);
2336
2337
0
  const ModeCosts *mode_costs = &x->mode_costs;
2338
2339
0
  estimate_single_ref_frame_costs(cm, xd, mode_costs, segment_id,
2340
0
                                  ref_costs_single);
2341
2342
0
  memset(&mode_checked[0][0], 0, MB_MODE_COUNT * REF_FRAMES);
2343
0
  if (reuse_inter_pred) {
2344
0
    for (int i = 0; i < 3; i++) {
2345
0
      tmp[i].data = &pred_buf[pixels_in_block * i];
2346
0
      tmp[i].stride = bw;
2347
0
      tmp[i].in_use = 0;
2348
0
    }
2349
0
    tmp[3].data = pd->dst.buf;
2350
0
    tmp[3].stride = pd->dst.stride;
2351
0
    tmp[3].in_use = 0;
2352
0
  }
2353
2354
0
  txfm_info->skip_txfm = 0;
2355
2356
  // initialize mode decisions
2357
0
  av1_invalid_rd_stats(&best_rdc);
2358
0
  av1_invalid_rd_stats(&this_rdc);
2359
0
  av1_invalid_rd_stats(rd_cost);
2360
0
  for (int i = 0; i < REF_FRAMES; ++i) {
2361
0
    x->warp_sample_info[i].num = -1;
2362
0
  }
2363
2364
0
  mi->bsize = bsize;
2365
0
  mi->ref_frame[0] = NONE_FRAME;
2366
0
  mi->ref_frame[1] = NONE_FRAME;
2367
2368
#if CONFIG_AV1_TEMPORAL_DENOISING
2369
  if (cpi->oxcf.noise_sensitivity > 0) {
2370
    // if (cpi->ppi->use_svc) denoise_svc_pickmode =
2371
    // av1_denoise_svc_non_key(cpi);
2372
    if (cpi->denoiser.denoising_level > kDenLowLow && denoise_svc_pickmode)
2373
      av1_denoiser_reset_frame_stats(ctx);
2374
  }
2375
#endif
2376
2377
0
  const int gf_temporal_ref = is_same_gf_and_last_scale(cm);
2378
2379
  // If the lower spatial layer uses an averaging filter for downsampling
2380
  // (phase = 8), the target decimated pixel is shifted by (1/2, 1/2) relative
2381
  // to source, so use subpel motion vector to compensate. The nonzero motion
2382
  // is half pixel shifted to left and top, so (-4, -4). This has more effect
2383
  // on higher resolutins, so condition it on that for now.
2384
0
  if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
2385
0
      svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 &&
2386
0
      cm->width * cm->height > 640 * 480) {
2387
0
    svc_mv_col = -4;
2388
0
    svc_mv_row = -4;
2389
0
  }
2390
2391
0
  get_ref_frame_use_mask(cpi, x, mi, mi_row, mi_col, bsize, gf_temporal_ref,
2392
0
                         use_ref_frame_mask, &force_skip_low_temp_var);
2393
2394
0
  skip_pred_mv = (x->nonrd_prune_ref_frame_search > 2 &&
2395
0
                  x->color_sensitivity[0] != 2 && x->color_sensitivity[1] != 2);
2396
2397
  // Compound modes per reference pair (GOLDEN_LAST/LAST2_LAST/ALTREF_LAST):
2398
  // (0_0)/(NEAREST_NEAREST)/(NEAR_NEAR).
2399
  // For now to reduce slowdowm, use only (0,0) for blocks above 16x16
2400
  // for non-svc case or on enhancement layers for svc.
2401
0
  if (cpi->sf.rt_sf.use_comp_ref_nonrd && is_comp_ref_allowed(bsize)) {
2402
0
    if (cpi->ppi->use_svc && cpi->svc.temporal_layer_id == 0)
2403
0
      num_comp_modes_ref = 2;
2404
0
    else if (bsize > BLOCK_16X16)
2405
0
      num_comp_modes_ref = 1;
2406
0
    else
2407
0
      tot_num_comp_modes = 0;
2408
0
  } else {
2409
0
    tot_num_comp_modes = 0;
2410
0
  }
2411
2412
0
  for (MV_REFERENCE_FRAME ref_frame_iter = LAST_FRAME;
2413
0
       ref_frame_iter <= ALTREF_FRAME; ++ref_frame_iter) {
2414
0
    if (use_ref_frame_mask[ref_frame_iter]) {
2415
0
      find_predictors(cpi, x, ref_frame_iter, frame_mv, tile_data, yv12_mb,
2416
0
                      bsize, force_skip_low_temp_var, skip_pred_mv);
2417
0
    }
2418
0
  }
2419
2420
0
  thresh_sad_pred = ((int64_t)x->pred_mv_sad[LAST_FRAME]) << 1;
2421
  // Increase threshold for less agressive pruning.
2422
0
  if (cpi->sf.rt_sf.nonrd_prune_ref_frame_search == 1)
2423
0
    thresh_sad_pred += (x->pred_mv_sad[LAST_FRAME] >> 2);
2424
2425
0
  const int large_block = bsize >= BLOCK_32X32;
2426
0
  const int use_model_yrd_large =
2427
0
      cpi->oxcf.rc_cfg.mode == AOM_CBR && large_block &&
2428
0
      !cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) &&
2429
0
      quant_params->base_qindex && cm->seq_params->bit_depth == 8;
2430
2431
0
  const int enable_filter_search =
2432
0
      is_filter_search_enabled(cpi, mi_row, mi_col, bsize, segment_id);
2433
2434
  // TODO(marpan): Look into reducing these conditions. For now constrain
2435
  // it to avoid significant bdrate loss.
2436
0
  if (cpi->sf.rt_sf.use_modeled_non_rd_cost) {
2437
0
    if (cpi->svc.non_reference_frame)
2438
0
      use_modeled_non_rd_cost = 1;
2439
0
    else if (cpi->svc.number_temporal_layers > 1 &&
2440
0
             cpi->svc.temporal_layer_id == 0)
2441
0
      use_modeled_non_rd_cost = 0;
2442
0
    else
2443
0
      use_modeled_non_rd_cost =
2444
0
          (quant_params->base_qindex > 120 && x->source_variance > 100 &&
2445
0
           bsize <= BLOCK_16X16 && !x->content_state_sb.lighting_change &&
2446
0
           x->content_state_sb.source_sad != kHighSad);
2447
0
  }
2448
2449
#if COLLECT_PICK_MODE_STAT
2450
  ms_stat.num_blocks[bsize]++;
2451
#endif
2452
0
  init_mbmi(mi, DC_PRED, NONE_FRAME, NONE_FRAME, cm);
2453
0
  mi->tx_size = AOMMIN(
2454
0
      AOMMIN(max_txsize_lookup[bsize],
2455
0
             tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]),
2456
0
      TX_16X16);
2457
0
  for (int idx = 0; idx < num_inter_modes + tot_num_comp_modes; ++idx) {
2458
0
    const struct segmentation *const seg = &cm->seg;
2459
2460
0
    int rate_mv = 0;
2461
0
    int is_skippable;
2462
0
    int this_early_term = 0;
2463
0
    int skip_this_mv = 0;
2464
0
    comp_pred = 0;
2465
0
    PREDICTION_MODE this_mode;
2466
0
    MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext;
2467
0
    RD_STATS nonskip_rdc;
2468
0
    av1_invalid_rd_stats(&nonskip_rdc);
2469
0
    memset(txfm_info->blk_skip, 0,
2470
0
           sizeof(txfm_info->blk_skip[0]) * num_8x8_blocks);
2471
2472
0
    if (idx >= num_inter_modes) {
2473
0
      int comp_index = idx - num_inter_modes;
2474
0
      if (comp_index % 3 == 0) {
2475
0
        int i = 0;
2476
0
        ref_mv_idx = 0;
2477
        // Only needs to be done once per reference pair.
2478
0
        if (comp_index == 3) i = 1;
2479
0
        if (comp_index == 6) i = 2;
2480
0
        if (cpi->sf.rt_sf.ref_frame_comp_nonrd[i])
2481
0
          setup_compound_prediction(cpi, x, yv12_mb, use_ref_frame_mask, i,
2482
0
                                    &ref_mv_idx);
2483
0
      }
2484
      // num_comp_modes_ref == 1 only do (0,0)
2485
0
      if (num_comp_modes_ref == 1 && comp_index % 3 != 0) continue;
2486
      // num_comp_modes_ref == 2 only do (0,0) and (NEAREST_NEAREST)
2487
0
      if (num_comp_modes_ref == 2 && comp_index % 3 == 2) continue;
2488
0
      ref_frame = LAST_FRAME;
2489
0
      ref_frame2 = GOLDEN_FRAME;
2490
0
      if (comp_index >= 0 && comp_index < 3) {
2491
        // comp_index = 0,1,2 for (0/NEAREST/NEAR) for GOLDEN_LAST.
2492
0
        if (cpi->sf.rt_sf.ref_frame_comp_nonrd[0] == 0 ||
2493
0
            !(cpi->ref_frame_flags & AOM_GOLD_FLAG))
2494
0
          continue;
2495
0
      } else if (comp_index >= 3 && comp_index < 6) {
2496
        // comp_index = 3,4,5 for (0/NEAREST/NEAR) for LAST2_LAST.
2497
0
        ref_frame2 = LAST2_FRAME;
2498
0
        if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1] == 0 ||
2499
0
            !(cpi->ref_frame_flags & AOM_LAST2_FLAG))
2500
0
          continue;
2501
0
      } else if (comp_index >= 6 && comp_index < 9) {
2502
        // comp_index = 6,7,8 for (0/NEAREST/NEAR) for ALTREF_LAST.
2503
0
        ref_frame2 = ALTREF_FRAME;
2504
0
        if (cpi->sf.rt_sf.ref_frame_comp_nonrd[2] == 0 ||
2505
0
            !(cpi->ref_frame_flags & AOM_ALT_FLAG))
2506
0
          continue;
2507
0
      }
2508
0
      set_compound_mode(x, comp_index, ref_frame, ref_frame2, ref_mv_idx,
2509
0
                        frame_mv, &this_mode);
2510
0
      if (this_mode != GLOBAL_GLOBALMV &&
2511
0
          frame_mv[this_mode][ref_frame].as_int == 0 &&
2512
0
          frame_mv[this_mode][ref_frame2].as_int == 0)
2513
0
        continue;
2514
0
      comp_pred = 1;
2515
0
    } else {
2516
0
      this_mode = ref_mode_set[idx].pred_mode;
2517
0
      ref_frame = ref_mode_set[idx].ref_frame;
2518
0
      ref_frame2 = NONE_FRAME;
2519
0
    }
2520
2521
#if COLLECT_PICK_MODE_STAT
2522
    aom_usec_timer_start(&ms_stat.timer1);
2523
    ms_stat.num_searches[bsize][this_mode]++;
2524
#endif
2525
0
    mi->mode = this_mode;
2526
0
    mi->ref_frame[0] = ref_frame;
2527
0
    mi->ref_frame[1] = ref_frame2;
2528
2529
0
    if (!use_ref_frame_mask[ref_frame]) continue;
2530
2531
0
    force_mv_inter_layer = 0;
2532
0
    if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
2533
0
        ((ref_frame == LAST_FRAME && svc->skip_mvsearch_last) ||
2534
0
         (ref_frame == GOLDEN_FRAME && svc->skip_mvsearch_gf))) {
2535
      // Only test mode if NEARESTMV/NEARMV is (svc_mv_col, svc_mv_row),
2536
      // otherwise set NEWMV to (svc_mv_col, svc_mv_row).
2537
      // Skip newmv and filter search.
2538
0
      force_mv_inter_layer = 1;
2539
0
      if (this_mode == NEWMV) {
2540
0
        frame_mv[this_mode][ref_frame].as_mv.col = svc_mv_col;
2541
0
        frame_mv[this_mode][ref_frame].as_mv.row = svc_mv_row;
2542
0
      } else if (frame_mv[this_mode][ref_frame].as_mv.col != svc_mv_col ||
2543
0
                 frame_mv[this_mode][ref_frame].as_mv.row != svc_mv_row) {
2544
0
        continue;
2545
0
      }
2546
0
    }
2547
2548
    // If the segment reference frame feature is enabled then do nothing if the
2549
    // current ref frame is not allowed.
2550
0
    if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
2551
0
        get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
2552
0
      continue;
2553
2554
    // For screen content. If source_sad is computed: skip non-zero motion
2555
    // check for stationary (super)blocks. Otherwise skip non-zero motion
2556
    // check for spatially flat blocks.
2557
0
    if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
2558
0
      if (cpi->sf.rt_sf.source_metrics_sb_nonrd) {
2559
0
        if (frame_mv[this_mode][ref_frame].as_int != 0 &&
2560
0
            x->content_state_sb.source_sad == kZeroSad)
2561
0
          continue;
2562
0
      }
2563
0
    }
2564
2565
0
    if (skip_mode_by_bsize_and_ref_frame(
2566
0
            this_mode, ref_frame, bsize, x->nonrd_prune_ref_frame_search,
2567
0
            sse_zeromv_norm, cpi->sf.rt_sf.nonrd_agressive_skip))
2568
0
      continue;
2569
2570
0
    if (skip_mode_by_low_temp(this_mode, ref_frame, bsize, x->content_state_sb,
2571
0
                              frame_mv[this_mode][ref_frame],
2572
0
                              force_skip_low_temp_var))
2573
0
      continue;
2574
2575
    // Disable this drop out case if the ref frame segment level feature is
2576
    // enabled for this segment. This is to prevent the possibility that we
2577
    // end up unable to pick any mode.
2578
0
    if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
2579
      // Check for skipping GOLDEN and ALTREF based pred_mv_sad.
2580
0
      if (cpi->sf.rt_sf.nonrd_prune_ref_frame_search > 0 &&
2581
0
          x->pred_mv_sad[ref_frame] != INT_MAX && ref_frame != LAST_FRAME) {
2582
0
        if ((int64_t)(x->pred_mv_sad[ref_frame]) > thresh_sad_pred) continue;
2583
0
      }
2584
0
    }
2585
    // Check for skipping NEARMV based on pred_mv_sad.
2586
0
    if (this_mode == NEARMV && x->pred_mv1_sad[ref_frame] != INT_MAX &&
2587
0
        x->pred_mv1_sad[ref_frame] > (x->pred_mv0_sad[ref_frame] << 1))
2588
0
      continue;
2589
2590
0
    if (!comp_pred) {
2591
0
      if (skip_mode_by_threshold(
2592
0
              this_mode, ref_frame, frame_mv[this_mode][ref_frame],
2593
0
              cpi->rc.frames_since_golden, rd_threshes, rd_thresh_freq_fact,
2594
0
              best_rdc.rdcost, best_pickmode.best_mode_skip_txfm,
2595
0
              (cpi->sf.rt_sf.nonrd_agressive_skip ? 1 : 0)))
2596
0
        continue;
2597
0
    }
2598
2599
    // Select prediction reference frames.
2600
0
    for (int i = 0; i < MAX_MB_PLANE; i++) {
2601
0
      xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
2602
0
      if (comp_pred) xd->plane[i].pre[1] = yv12_mb[ref_frame2][i];
2603
0
    }
2604
2605
0
    mi->ref_frame[0] = ref_frame;
2606
0
    mi->ref_frame[1] = ref_frame2;
2607
0
    set_ref_ptrs(cm, xd, ref_frame, ref_frame2);
2608
2609
0
    if (this_mode == NEWMV && !force_mv_inter_layer) {
2610
0
      if (search_new_mv(cpi, x, frame_mv, ref_frame, gf_temporal_ref, bsize,
2611
0
                        mi_row, mi_col, &rate_mv, &best_rdc))
2612
0
        continue;
2613
0
    }
2614
2615
0
    for (PREDICTION_MODE inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV;
2616
0
         inter_mv_mode++) {
2617
0
      if (inter_mv_mode == this_mode) continue;
2618
0
      if (mode_checked[inter_mv_mode][ref_frame] &&
2619
0
          frame_mv[this_mode][ref_frame].as_int ==
2620
0
              frame_mv[inter_mv_mode][ref_frame].as_int) {
2621
0
        skip_this_mv = 1;
2622
0
        break;
2623
0
      }
2624
0
    }
2625
2626
0
    if (skip_this_mv && !comp_pred) continue;
2627
2628
0
    mi->mode = this_mode;
2629
0
    mi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
2630
0
    mi->mv[1].as_int = 0;
2631
0
    if (comp_pred) mi->mv[1].as_int = frame_mv[this_mode][ref_frame2].as_int;
2632
2633
0
    if (reuse_inter_pred) {
2634
0
      if (!this_mode_pred) {
2635
0
        this_mode_pred = &tmp[3];
2636
0
      } else {
2637
0
        this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
2638
0
        pd->dst.buf = this_mode_pred->data;
2639
0
        pd->dst.stride = bw;
2640
0
      }
2641
0
    }
2642
#if COLLECT_PICK_MODE_STAT
2643
    ms_stat.num_nonskipped_searches[bsize][this_mode]++;
2644
#endif
2645
2646
0
    if (idx == 0 && !skip_pred_mv) {
2647
      // Set color sensitivity on first tested mode only.
2648
      // Use y-sad already computed in find_predictors: take the sad with motion
2649
      // vector closest to 0; the uv-sad computed below in set_color_sensitivity
2650
      // is for zeromv.
2651
0
      int y_sad = x->pred_mv0_sad[LAST_FRAME];
2652
0
      if (x->pred_mv1_sad[LAST_FRAME] != INT_MAX &&
2653
0
          (abs(frame_mv[NEARMV][LAST_FRAME].as_mv.col) +
2654
0
           abs(frame_mv[NEARMV][LAST_FRAME].as_mv.row)) <
2655
0
              (abs(frame_mv[NEARESTMV][LAST_FRAME].as_mv.col) +
2656
0
               abs(frame_mv[NEARESTMV][LAST_FRAME].as_mv.row)))
2657
0
        y_sad = x->pred_mv1_sad[LAST_FRAME];
2658
0
      set_color_sensitivity(cpi, x, xd, bsize, y_sad, x->source_variance);
2659
0
    }
2660
0
    mi->motion_mode = SIMPLE_TRANSLATION;
2661
0
#if !CONFIG_REALTIME_ONLY
2662
0
    if (cpi->oxcf.motion_mode_cfg.allow_warped_motion) {
2663
0
      calc_num_proj_ref(cpi, x, mi);
2664
0
    }
2665
0
#endif
2666
2667
0
    if (enable_filter_search && !force_mv_inter_layer && !comp_pred &&
2668
0
        ((mi->mv[0].as_mv.row & 0x07) || (mi->mv[0].as_mv.col & 0x07)) &&
2669
0
        (ref_frame == LAST_FRAME || !x->nonrd_prune_ref_frame_search)) {
2670
0
      search_filter_ref(cpi, x, &this_rdc, mi_row, mi_col, tmp, bsize,
2671
0
                        reuse_inter_pred, &this_mode_pred, &this_early_term,
2672
0
                        use_model_yrd_large);
2673
0
#if !CONFIG_REALTIME_ONLY
2674
0
    } else if (cpi->oxcf.motion_mode_cfg.allow_warped_motion &&
2675
0
               this_mode == NEWMV) {
2676
0
      search_motion_mode(cpi, x, &this_rdc, mi_row, mi_col, bsize,
2677
0
                         &this_early_term, use_model_yrd_large, &rate_mv);
2678
0
      if (this_mode == NEWMV) {
2679
0
        frame_mv[this_mode][ref_frame] = mi->mv[0];
2680
0
      }
2681
0
#endif
2682
0
    } else {
2683
0
      mi->interp_filters =
2684
0
          (filter_ref == SWITCHABLE)
2685
0
              ? av1_broadcast_interp_filter(default_interp_filter)
2686
0
              : av1_broadcast_interp_filter(filter_ref);
2687
0
      if (force_mv_inter_layer)
2688
0
        mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
2689
2690
      // If it is sub-pel motion and best filter was not selected in
2691
      // search_filter_ref() for all blocks, then check top and left values and
2692
      // force smooth if both were selected to be smooth.
2693
0
      if (cpi->sf.interp_sf.cb_pred_filter_search &&
2694
0
          (mi->mv[0].as_mv.row & 0x07 || mi->mv[0].as_mv.col & 0x07)) {
2695
0
        if (xd->left_mbmi && xd->above_mbmi) {
2696
0
          if ((xd->left_mbmi->interp_filters.as_filters.x_filter ==
2697
0
                   EIGHTTAP_SMOOTH &&
2698
0
               xd->above_mbmi->interp_filters.as_filters.x_filter ==
2699
0
                   EIGHTTAP_SMOOTH))
2700
0
            mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_SMOOTH);
2701
0
        }
2702
0
      }
2703
0
      if (!comp_pred)
2704
0
        av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
2705
0
      else
2706
0
        av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, 0,
2707
0
                                      0);
2708
2709
0
      if (use_model_yrd_large) {
2710
0
        model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, &this_rdc,
2711
0
                                  &this_early_term, use_modeled_non_rd_cost);
2712
0
      } else {
2713
0
        model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc,
2714
0
                          use_modeled_non_rd_cost);
2715
0
      }
2716
0
    }
2717
2718
0
    if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0) {
2719
0
      sse_zeromv_norm =
2720
0
          (unsigned int)(this_rdc.sse >> (b_width_log2_lookup[bsize] +
2721
0
                                          b_height_log2_lookup[bsize]));
2722
0
    }
2723
2724
0
    const int skip_ctx = av1_get_skip_txfm_context(xd);
2725
0
    const int skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][1];
2726
0
    const int no_skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][0];
2727
0
    const int64_t sse_y = this_rdc.sse;
2728
0
    if (this_early_term) {
2729
0
      this_rdc.skip_txfm = 1;
2730
0
      this_rdc.rate = skip_txfm_cost;
2731
0
      this_rdc.dist = this_rdc.sse << 4;
2732
0
    } else {
2733
0
      if (use_modeled_non_rd_cost) {
2734
0
        if (this_rdc.skip_txfm) {
2735
0
          this_rdc.rate = skip_txfm_cost;
2736
0
        } else {
2737
0
          this_rdc.rate += no_skip_txfm_cost;
2738
0
        }
2739
0
      } else {
2740
0
        block_yrd(cpi, x, mi_row, mi_col, &this_rdc, &is_skippable, bsize,
2741
0
                  mi->tx_size);
2742
0
        if (this_rdc.skip_txfm ||
2743
0
            RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist) >=
2744
0
                RDCOST(x->rdmult, 0, this_rdc.sse)) {
2745
0
          if (!this_rdc.skip_txfm) {
2746
            // Need to store "real" rdc for possible furure use if UV rdc
2747
            // disallows tx skip
2748
0
            nonskip_rdc = this_rdc;
2749
0
            nonskip_rdc.rate += no_skip_txfm_cost;
2750
0
          }
2751
0
          this_rdc.rate = skip_txfm_cost;
2752
0
          this_rdc.skip_txfm = 1;
2753
0
          this_rdc.dist = this_rdc.sse;
2754
0
        } else {
2755
0
          this_rdc.rate += no_skip_txfm_cost;
2756
0
        }
2757
0
      }
2758
0
      if ((x->color_sensitivity[0] || x->color_sensitivity[1])) {
2759
0
        RD_STATS rdc_uv;
2760
0
        const BLOCK_SIZE uv_bsize = get_plane_block_size(
2761
0
            bsize, xd->plane[1].subsampling_x, xd->plane[1].subsampling_y);
2762
0
        if (x->color_sensitivity[0]) {
2763
0
          av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2764
0
                                        AOM_PLANE_U, AOM_PLANE_U);
2765
0
        }
2766
0
        if (x->color_sensitivity[1]) {
2767
0
          av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2768
0
                                        AOM_PLANE_V, AOM_PLANE_V);
2769
0
        }
2770
0
        model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, &this_rdc.sse, 1, 2);
2771
        // Restore Y rdc if UV rdc disallows txfm skip
2772
0
        if (this_rdc.skip_txfm && !rdc_uv.skip_txfm &&
2773
0
            nonskip_rdc.rate != INT_MAX)
2774
0
          this_rdc = nonskip_rdc;
2775
0
        this_rdc.rate += rdc_uv.rate;
2776
0
        this_rdc.dist += rdc_uv.dist;
2777
0
        this_rdc.skip_txfm = this_rdc.skip_txfm && rdc_uv.skip_txfm;
2778
0
      }
2779
0
    }
2780
2781
    // TODO(kyslov) account for UV prediction cost
2782
0
    this_rdc.rate += rate_mv;
2783
0
    const int16_t mode_ctx =
2784
0
        av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame);
2785
0
    this_rdc.rate += cost_mv_ref(mode_costs, this_mode, mode_ctx);
2786
2787
0
    this_rdc.rate += ref_costs_single[ref_frame];
2788
2789
0
    this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
2790
0
    if (cpi->oxcf.rc_cfg.mode == AOM_CBR && !comp_pred) {
2791
0
      newmv_diff_bias(xd, this_mode, &this_rdc, bsize,
2792
0
                      frame_mv[this_mode][ref_frame].as_mv.row,
2793
0
                      frame_mv[this_mode][ref_frame].as_mv.col, cpi->speed,
2794
0
                      x->source_variance, x->content_state_sb);
2795
0
    }
2796
#if CONFIG_AV1_TEMPORAL_DENOISING
2797
    if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc_pickmode &&
2798
        cpi->denoiser.denoising_level > kDenLowLow) {
2799
      av1_denoiser_update_frame_stats(mi, sse_y, this_mode, ctx);
2800
      // Keep track of zero_last cost.
2801
      if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0)
2802
        zero_last_cost_orig = this_rdc.rdcost;
2803
    }
2804
#else
2805
0
    (void)sse_y;
2806
0
#endif
2807
2808
0
    mode_checked[this_mode][ref_frame] = 1;
2809
#if COLLECT_PICK_MODE_STAT
2810
    aom_usec_timer_mark(&ms_stat.timer1);
2811
    ms_stat.nonskipped_search_times[bsize][this_mode] +=
2812
        aom_usec_timer_elapsed(&ms_stat.timer1);
2813
#endif
2814
0
    if (this_rdc.rdcost < best_rdc.rdcost) {
2815
0
      best_rdc = this_rdc;
2816
0
      best_early_term = this_early_term;
2817
0
      best_pickmode.best_mode = this_mode;
2818
0
      best_pickmode.best_motion_mode = mi->motion_mode;
2819
0
      best_pickmode.wm_params = mi->wm_params;
2820
0
      best_pickmode.num_proj_ref = mi->num_proj_ref;
2821
0
      best_pickmode.best_pred_filter = mi->interp_filters;
2822
0
      best_pickmode.best_tx_size = mi->tx_size;
2823
0
      best_pickmode.best_ref_frame = ref_frame;
2824
0
      best_pickmode.best_second_ref_frame = ref_frame2;
2825
0
      best_pickmode.best_mode_skip_txfm = this_rdc.skip_txfm;
2826
0
      best_pickmode.best_mode_initial_skip_flag =
2827
0
          (nonskip_rdc.rate == INT_MAX && this_rdc.skip_txfm);
2828
0
      if (!best_pickmode.best_mode_skip_txfm && !use_modeled_non_rd_cost) {
2829
0
        memcpy(best_pickmode.blk_skip, txfm_info->blk_skip,
2830
0
               sizeof(txfm_info->blk_skip[0]) * num_8x8_blocks);
2831
0
      }
2832
2833
      // This is needed for the compound modes.
2834
0
      frame_mv_best[this_mode][ref_frame].as_int =
2835
0
          frame_mv[this_mode][ref_frame].as_int;
2836
0
      if (ref_frame2 > NONE_FRAME)
2837
0
        frame_mv_best[this_mode][ref_frame2].as_int =
2838
0
            frame_mv[this_mode][ref_frame2].as_int;
2839
2840
0
      if (reuse_inter_pred) {
2841
0
        free_pred_buffer(best_pickmode.best_pred);
2842
0
        best_pickmode.best_pred = this_mode_pred;
2843
0
      }
2844
0
    } else {
2845
0
      if (reuse_inter_pred) free_pred_buffer(this_mode_pred);
2846
0
    }
2847
0
    if (best_early_term && (idx > 0 || cpi->sf.rt_sf.nonrd_agressive_skip)) {
2848
0
      txfm_info->skip_txfm = 1;
2849
0
      break;
2850
0
    }
2851
0
  }
2852
2853
0
  mi->mode = best_pickmode.best_mode;
2854
0
  mi->motion_mode = best_pickmode.best_motion_mode;
2855
0
  mi->wm_params = best_pickmode.wm_params;
2856
0
  mi->num_proj_ref = best_pickmode.num_proj_ref;
2857
0
  mi->interp_filters = best_pickmode.best_pred_filter;
2858
0
  mi->tx_size = best_pickmode.best_tx_size;
2859
0
  memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size));
2860
0
  mi->ref_frame[0] = best_pickmode.best_ref_frame;
2861
0
  mi->mv[0].as_int =
2862
0
      frame_mv_best[best_pickmode.best_mode][best_pickmode.best_ref_frame]
2863
0
          .as_int;
2864
0
  mi->mv[1].as_int = 0;
2865
0
  if (best_pickmode.best_second_ref_frame > INTRA_FRAME) {
2866
0
    mi->ref_frame[1] = best_pickmode.best_second_ref_frame;
2867
0
    mi->mv[1].as_int = frame_mv_best[best_pickmode.best_mode]
2868
0
                                    [best_pickmode.best_second_ref_frame]
2869
0
                                        .as_int;
2870
0
  }
2871
  // Perform intra prediction search, if the best SAD is above a certain
2872
  // threshold.
2873
0
  mi->angle_delta[PLANE_TYPE_Y] = 0;
2874
0
  mi->angle_delta[PLANE_TYPE_UV] = 0;
2875
0
  mi->filter_intra_mode_info.use_filter_intra = 0;
2876
2877
0
  estimate_intra_mode(cpi, x, bsize, use_modeled_non_rd_cost, best_early_term,
2878
0
                      ref_costs_single[INTRA_FRAME], reuse_inter_pred,
2879
0
                      &orig_dst, tmp, &this_mode_pred, &best_rdc,
2880
0
                      &best_pickmode);
2881
2882
0
  pd->dst = orig_dst;
2883
0
  mi->mode = best_pickmode.best_mode;
2884
0
  mi->ref_frame[0] = best_pickmode.best_ref_frame;
2885
0
  mi->ref_frame[1] = best_pickmode.best_second_ref_frame;
2886
0
  txfm_info->skip_txfm = best_rdc.skip_txfm;
2887
0
  if (!txfm_info->skip_txfm) {
2888
0
    if (best_pickmode.best_mode >= INTRA_MODE_END)
2889
0
      memcpy(ctx->blk_skip, best_pickmode.blk_skip,
2890
0
             sizeof(best_pickmode.blk_skip[0]) * num_8x8_blocks);
2891
0
    else
2892
0
      memset(ctx->blk_skip, 0,
2893
0
             sizeof(best_pickmode.blk_skip[0]) * ctx->num_4x4_blk);
2894
0
  }
2895
0
  if (has_second_ref(mi)) {
2896
0
    mi->comp_group_idx = 0;
2897
0
    mi->compound_idx = 1;
2898
0
    mi->interinter_comp.type = COMPOUND_AVERAGE;
2899
0
  }
2900
2901
0
  if (!is_inter_block(mi)) {
2902
0
    mi->interp_filters = av1_broadcast_interp_filter(SWITCHABLE_FILTERS);
2903
0
  }
2904
2905
0
  if (reuse_inter_pred && best_pickmode.best_pred != NULL) {
2906
0
    PRED_BUFFER *const best_pred = best_pickmode.best_pred;
2907
0
    if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) {
2908
0
      aom_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
2909
0
                        pd->dst.stride, bw, bh);
2910
0
    }
2911
0
  }
2912
2913
#if CONFIG_AV1_TEMPORAL_DENOISING
2914
  if (cpi->oxcf.noise_sensitivity > 0 && resize_pending == 0 &&
2915
      denoise_svc_pickmode && cpi->denoiser.denoising_level > kDenLowLow &&
2916
      cpi->denoiser.reset == 0) {
2917
    AV1_DENOISER_DECISION decision = COPY_BLOCK;
2918
    ctx->sb_skip_denoising = 0;
2919
    av1_pickmode_ctx_den_update(&ctx_den, zero_last_cost_orig, ref_costs_single,
2920
                                frame_mv, reuse_inter_pred, &best_pickmode);
2921
    av1_denoiser_denoise(cpi, x, mi_row, mi_col, bsize, ctx, &decision,
2922
                         gf_temporal_ref);
2923
    if (denoise_recheck_zeromv)
2924
      recheck_zeromv_after_denoising(cpi, mi, x, xd, decision, &ctx_den,
2925
                                     yv12_mb, &best_rdc, &best_pickmode, bsize,
2926
                                     mi_row, mi_col);
2927
    best_pickmode.best_ref_frame = ctx_den.best_ref_frame;
2928
  }
2929
#endif
2930
2931
0
  if (cpi->sf.inter_sf.adaptive_rd_thresh && !has_second_ref(mi)) {
2932
0
    THR_MODES best_mode_idx =
2933
0
        mode_idx[best_pickmode.best_ref_frame][mode_offset(mi->mode)];
2934
0
    if (best_pickmode.best_ref_frame == INTRA_FRAME) {
2935
      // Only consider the modes that are included in the intra_mode_list.
2936
0
      int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE);
2937
0
      for (int i = 0; i < intra_modes; i++) {
2938
0
        update_thresh_freq_fact(cpi, x, bsize, INTRA_FRAME, best_mode_idx,
2939
0
                                intra_mode_list[i]);
2940
0
      }
2941
0
    } else {
2942
0
      PREDICTION_MODE this_mode;
2943
0
      for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
2944
0
        update_thresh_freq_fact(cpi, x, bsize, best_pickmode.best_ref_frame,
2945
0
                                best_mode_idx, this_mode);
2946
0
      }
2947
0
    }
2948
0
  }
2949
2950
#if CONFIG_INTERNAL_STATS
2951
  store_coding_context(x, ctx, mi->mode);
2952
#else
2953
0
  store_coding_context(x, ctx);
2954
0
#endif  // CONFIG_INTERNAL_STATS
2955
#if COLLECT_PICK_MODE_STAT
2956
  aom_usec_timer_mark(&ms_stat.timer2);
2957
  ms_stat.avg_block_times[bsize] += aom_usec_timer_elapsed(&ms_stat.timer2);
2958
  //
2959
  if ((mi_row + mi_size_high[bsize] >= (cpi->common.mi_params.mi_rows)) &&
2960
      (mi_col + mi_size_wide[bsize] >= (cpi->common.mi_params.mi_cols))) {
2961
    int i, j;
2962
    PREDICTION_MODE used_modes[3] = { NEARESTMV, NEARMV, NEWMV };
2963
    BLOCK_SIZE bss[5] = { BLOCK_8X8, BLOCK_16X16, BLOCK_32X32, BLOCK_64X64,
2964
                          BLOCK_128X128 };
2965
    int64_t total_time = 0l;
2966
    int32_t total_blocks = 0;
2967
2968
    printf("\n");
2969
    for (i = 0; i < 5; i++) {
2970
      printf("BS(%d) Num %d, Avg_time %f: ", bss[i], ms_stat.num_blocks[bss[i]],
2971
             ms_stat.num_blocks[bss[i]] > 0
2972
                 ? (float)ms_stat.avg_block_times[bss[i]] /
2973
                       ms_stat.num_blocks[bss[i]]
2974
                 : 0);
2975
      total_time += ms_stat.avg_block_times[bss[i]];
2976
      total_blocks += ms_stat.num_blocks[bss[i]];
2977
      for (j = 0; j < 3; j++) {
2978
        printf("Mode %d, %d/%d tps %f ", used_modes[j],
2979
               ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]],
2980
               ms_stat.num_searches[bss[i]][used_modes[j]],
2981
               ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]] > 0
2982
                   ? (float)ms_stat
2983
                             .nonskipped_search_times[bss[i]][used_modes[j]] /
2984
                         ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]]
2985
                   : 0l);
2986
      }
2987
      printf("\n");
2988
    }
2989
    printf("Total time = %ld. Total blocks = %d\n", total_time, total_blocks);
2990
  }
2991
  //
2992
#endif  // COLLECT_PICK_MODE_STAT
2993
0
  *rd_cost = best_rdc;
2994
0
}