Coverage Report

Created: 2022-08-24 06:15

/src/aom/av1/encoder/encodeframe_utils.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020, 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
#include "av1/common/common_data.h"
13
#include "av1/common/quant_common.h"
14
#include "av1/common/reconintra.h"
15
16
#include "av1/encoder/encoder.h"
17
#include "av1/encoder/encodeframe_utils.h"
18
#include "av1/encoder/partition_strategy.h"
19
#include "av1/encoder/rdopt.h"
20
#include "av1/encoder/aq_variance.h"
21
22
void av1_set_ssim_rdmult(const AV1_COMP *const cpi, int *errorperbit,
23
                         const BLOCK_SIZE bsize, const int mi_row,
24
255k
                         const int mi_col, int *const rdmult) {
25
255k
  const AV1_COMMON *const cm = &cpi->common;
26
27
255k
  const int bsize_base = BLOCK_16X16;
28
255k
  const int num_mi_w = mi_size_wide[bsize_base];
29
255k
  const int num_mi_h = mi_size_high[bsize_base];
30
255k
  const int num_cols = (cm->mi_params.mi_cols + num_mi_w - 1) / num_mi_w;
31
255k
  const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
32
255k
  const int num_bcols = (mi_size_wide[bsize] + num_mi_w - 1) / num_mi_w;
33
255k
  const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h;
34
255k
  int row, col;
35
255k
  double num_of_mi = 0.0;
36
255k
  double geom_mean_of_scale = 0.0;
37
38
255k
  assert(cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIM);
39
40
255k
  for (row = mi_row / num_mi_w;
41
624k
       row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
42
369k
    for (col = mi_col / num_mi_h;
43
1.06M
         col < num_cols && col < mi_col / num_mi_h + num_bcols; ++col) {
44
694k
      const int index = row * num_cols + col;
45
694k
      geom_mean_of_scale += log(cpi->ssim_rdmult_scaling_factors[index]);
46
694k
      num_of_mi += 1.0;
47
694k
    }
48
369k
  }
49
255k
  geom_mean_of_scale = exp(geom_mean_of_scale / num_of_mi);
50
51
255k
  *rdmult = (int)((double)(*rdmult) * geom_mean_of_scale + 0.5);
52
255k
  *rdmult = AOMMAX(*rdmult, 0);
53
255k
  av1_set_error_per_bit(errorperbit, *rdmult);
54
255k
}
55
56
// TODO(angiebird): Move these function to tpl_model.c
57
#if !CONFIG_REALTIME_ONLY
58
static AOM_INLINE int set_deltaq_rdmult(const AV1_COMP *const cpi,
59
0
                                        const MACROBLOCK *const x) {
60
0
  const AV1_COMMON *const cm = &cpi->common;
61
0
  const CommonQuantParams *quant_params = &cm->quant_params;
62
0
  return av1_compute_rd_mult(cpi, quant_params->base_qindex + x->delta_qindex +
63
0
                                      quant_params->y_dc_delta_q);
64
0
}
65
66
// Return the end column for the current superblock, in unit of TPL blocks.
67
static int get_superblock_tpl_column_end(const AV1_COMMON *const cm, int mi_col,
68
0
                                         int num_mi_w) {
69
  // Find the start column of this superblock.
70
0
  const int sb_mi_col_start = (mi_col >> cm->seq_params->mib_size_log2)
71
0
                              << cm->seq_params->mib_size_log2;
72
  // Same but in superres upscaled dimension.
73
0
  const int sb_mi_col_start_sr =
74
0
      coded_to_superres_mi(sb_mi_col_start, cm->superres_scale_denominator);
75
  // Width of this superblock in mi units.
76
0
  const int sb_mi_width = mi_size_wide[cm->seq_params->sb_size];
77
  // Same but in superres upscaled dimension.
78
0
  const int sb_mi_width_sr =
79
0
      coded_to_superres_mi(sb_mi_width, cm->superres_scale_denominator);
80
  // Superblock end in mi units.
81
0
  const int sb_mi_end = sb_mi_col_start_sr + sb_mi_width_sr;
82
  // Superblock end in TPL units.
83
0
  return (sb_mi_end + num_mi_w - 1) / num_mi_w;
84
0
}
85
86
int av1_get_cb_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x,
87
                      const BLOCK_SIZE bsize, const int mi_row,
88
0
                      const int mi_col) {
89
0
  const AV1_COMMON *const cm = &cpi->common;
90
0
  assert(IMPLIES(cpi->ppi->gf_group.size > 0,
91
0
                 cpi->gf_frame_index < cpi->ppi->gf_group.size));
92
0
  const int tpl_idx = cpi->gf_frame_index;
93
0
  int deltaq_rdmult = set_deltaq_rdmult(cpi, x);
94
0
  if (!av1_tpl_stats_ready(&cpi->ppi->tpl_data, tpl_idx)) return deltaq_rdmult;
95
0
  if (cm->superres_scale_denominator != SCALE_NUMERATOR) return deltaq_rdmult;
96
0
  if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return deltaq_rdmult;
97
0
  if (x->rb == 0) return deltaq_rdmult;
98
99
0
  TplParams *const tpl_data = &cpi->ppi->tpl_data;
100
0
  TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
101
0
  TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
102
103
0
  const int mi_wide = mi_size_wide[bsize];
104
0
  const int mi_high = mi_size_high[bsize];
105
106
0
  int tpl_stride = tpl_frame->stride;
107
0
  double intra_cost_base = 0;
108
0
  double mc_dep_cost_base = 0;
109
0
  double cbcmp_base = 0;
110
0
  const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
111
112
0
  for (int row = mi_row; row < mi_row + mi_high; row += step) {
113
0
    for (int col = mi_col; col < mi_col + mi_wide; col += step) {
114
0
      if (row >= cm->mi_params.mi_rows || col >= cm->mi_params.mi_cols)
115
0
        continue;
116
117
0
      TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
118
0
          row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
119
120
0
      double cbcmp = (double)this_stats->srcrf_dist;
121
0
      int64_t mc_dep_delta =
122
0
          RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
123
0
                 this_stats->mc_dep_dist);
124
0
      double dist_scaled = (double)(this_stats->recrf_dist << RDDIV_BITS);
125
0
      intra_cost_base += log(dist_scaled) * cbcmp;
126
0
      mc_dep_cost_base += log(3 * dist_scaled + mc_dep_delta) * cbcmp;
127
0
      cbcmp_base += cbcmp;
128
0
    }
129
0
  }
130
131
0
  if (cbcmp_base == 0) return deltaq_rdmult;
132
133
0
  double rk = exp((intra_cost_base - mc_dep_cost_base) / cbcmp_base);
134
0
  deltaq_rdmult = (int)(deltaq_rdmult * (rk / x->rb));
135
136
0
  return AOMMAX(deltaq_rdmult, 1);
137
0
}
138
139
int av1_get_hier_tpl_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x,
140
                            const BLOCK_SIZE bsize, const int mi_row,
141
0
                            const int mi_col, int orig_rdmult) {
142
0
  const AV1_COMMON *const cm = &cpi->common;
143
0
  const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
144
0
  assert(IMPLIES(cpi->ppi->gf_group.size > 0,
145
0
                 cpi->gf_frame_index < cpi->ppi->gf_group.size));
146
0
  const int tpl_idx = cpi->gf_frame_index;
147
0
  const int deltaq_rdmult = set_deltaq_rdmult(cpi, x);
148
0
  if (!av1_tpl_stats_ready(&cpi->ppi->tpl_data, tpl_idx)) return deltaq_rdmult;
149
0
  if (!is_frame_tpl_eligible(gf_group, cpi->gf_frame_index))
150
0
    return deltaq_rdmult;
151
0
  if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return deltaq_rdmult;
152
153
0
  const int mi_col_sr =
154
0
      coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
155
0
  const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
156
0
  const int block_mi_width_sr =
157
0
      coded_to_superres_mi(mi_size_wide[bsize], cm->superres_scale_denominator);
158
159
0
  const int bsize_base = BLOCK_16X16;
160
0
  const int num_mi_w = mi_size_wide[bsize_base];
161
0
  const int num_mi_h = mi_size_high[bsize_base];
162
0
  const int num_cols = (mi_cols_sr + num_mi_w - 1) / num_mi_w;
163
0
  const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
164
0
  const int num_bcols = (block_mi_width_sr + num_mi_w - 1) / num_mi_w;
165
0
  const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h;
166
  // This is required because the end col of superblock may be off by 1 in case
167
  // of superres.
168
0
  const int sb_bcol_end = get_superblock_tpl_column_end(cm, mi_col, num_mi_w);
169
0
  int row, col;
170
0
  double base_block_count = 0.0;
171
0
  double geom_mean_of_scale = 0.0;
172
0
  for (row = mi_row / num_mi_w;
173
0
       row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
174
0
    for (col = mi_col_sr / num_mi_h;
175
0
         col < num_cols && col < mi_col_sr / num_mi_h + num_bcols &&
176
0
         col < sb_bcol_end;
177
0
         ++col) {
178
0
      const int index = row * num_cols + col;
179
0
      geom_mean_of_scale += log(cpi->ppi->tpl_sb_rdmult_scaling_factors[index]);
180
0
      base_block_count += 1.0;
181
0
    }
182
0
  }
183
0
  geom_mean_of_scale = exp(geom_mean_of_scale / base_block_count);
184
0
  int rdmult = (int)((double)orig_rdmult * geom_mean_of_scale + 0.5);
185
0
  rdmult = AOMMAX(rdmult, 0);
186
0
  av1_set_error_per_bit(&x->errorperbit, rdmult);
187
0
#if !CONFIG_RD_COMMAND
188
0
  if (bsize == cm->seq_params->sb_size) {
189
0
    const int rdmult_sb = set_deltaq_rdmult(cpi, x);
190
0
    assert(rdmult_sb == rdmult);
191
0
    (void)rdmult_sb;
192
0
  }
193
0
#endif  // !CONFIG_RD_COMMAND
194
0
  return rdmult;
195
0
}
196
#endif  // !CONFIG_REALTIME_ONLY
197
198
static AOM_INLINE void update_filter_type_count(FRAME_COUNTS *counts,
199
                                                const MACROBLOCKD *xd,
200
0
                                                const MB_MODE_INFO *mbmi) {
201
0
  int dir;
202
0
  for (dir = 0; dir < 2; ++dir) {
203
0
    const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
204
0
    InterpFilter filter = av1_extract_interp_filter(mbmi->interp_filters, dir);
205
0
    ++counts->switchable_interp[ctx][filter];
206
0
  }
207
0
}
208
209
static void reset_tx_size(MACROBLOCK *x, MB_MODE_INFO *mbmi,
210
0
                          const TX_MODE tx_mode) {
211
0
  MACROBLOCKD *const xd = &x->e_mbd;
212
0
  TxfmSearchInfo *txfm_info = &x->txfm_search_info;
213
0
  if (xd->lossless[mbmi->segment_id]) {
214
0
    mbmi->tx_size = TX_4X4;
215
0
  } else if (tx_mode != TX_MODE_SELECT) {
216
0
    mbmi->tx_size = tx_size_from_tx_mode(mbmi->bsize, tx_mode);
217
0
  } else {
218
0
    const BLOCK_SIZE bsize = mbmi->bsize;
219
0
    const TX_SIZE min_tx_size = depth_to_tx_size(MAX_TX_DEPTH, bsize);
220
0
    if (tx_size_wide[min_tx_size] > tx_size_wide[mbmi->tx_size] ||
221
0
        tx_size_high[min_tx_size] > tx_size_high[mbmi->tx_size])
222
0
      mbmi->tx_size = min_tx_size;
223
224
0
    const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, bsize, 0);
225
0
    if (tx_size_wide[max_tx_size] < tx_size_wide[mbmi->tx_size] ||
226
0
        tx_size_high[max_tx_size] < tx_size_high[mbmi->tx_size])
227
0
      mbmi->tx_size = max_tx_size;
228
0
  }
229
0
  if (is_inter_block(mbmi)) {
230
0
    memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size));
231
0
  }
232
0
  const int stride = xd->tx_type_map_stride;
233
0
  const int bw = mi_size_wide[mbmi->bsize];
234
0
  for (int row = 0; row < mi_size_high[mbmi->bsize]; ++row) {
235
0
    memset(xd->tx_type_map + row * stride, DCT_DCT,
236
0
           bw * sizeof(xd->tx_type_map[0]));
237
0
  }
238
0
  av1_zero(txfm_info->blk_skip);
239
0
  txfm_info->skip_txfm = 0;
240
0
}
241
242
// This function will copy the best reference mode information from
243
// MB_MODE_INFO_EXT_FRAME to MB_MODE_INFO_EXT.
244
static INLINE void copy_mbmi_ext_frame_to_mbmi_ext(
245
    MB_MODE_INFO_EXT *mbmi_ext,
246
61.6k
    const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_best, uint8_t ref_frame_type) {
247
61.6k
  memcpy(mbmi_ext->ref_mv_stack[ref_frame_type], mbmi_ext_best->ref_mv_stack,
248
61.6k
         sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE]));
249
61.6k
  memcpy(mbmi_ext->weight[ref_frame_type], mbmi_ext_best->weight,
250
61.6k
         sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE]));
251
61.6k
  mbmi_ext->mode_context[ref_frame_type] = mbmi_ext_best->mode_context;
252
61.6k
  mbmi_ext->ref_mv_count[ref_frame_type] = mbmi_ext_best->ref_mv_count;
253
61.6k
  memcpy(mbmi_ext->global_mvs, mbmi_ext_best->global_mvs,
254
61.6k
         sizeof(mbmi_ext->global_mvs));
255
61.6k
}
256
257
void av1_update_state(const AV1_COMP *const cpi, ThreadData *td,
258
                      const PICK_MODE_CONTEXT *const ctx, int mi_row,
259
61.6k
                      int mi_col, BLOCK_SIZE bsize, RUN_TYPE dry_run) {
260
61.6k
  int i, x_idx, y;
261
61.6k
  const AV1_COMMON *const cm = &cpi->common;
262
61.6k
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
263
61.6k
  const int num_planes = av1_num_planes(cm);
264
61.6k
  MACROBLOCK *const x = &td->mb;
265
61.6k
  MACROBLOCKD *const xd = &x->e_mbd;
266
61.6k
  struct macroblock_plane *const p = x->plane;
267
61.6k
  struct macroblockd_plane *const pd = xd->plane;
268
61.6k
  const MB_MODE_INFO *const mi = &ctx->mic;
269
61.6k
  MB_MODE_INFO *const mi_addr = xd->mi[0];
270
61.6k
  const struct segmentation *const seg = &cm->seg;
271
61.6k
  assert(bsize < BLOCK_SIZES_ALL);
272
61.6k
  const int bw = mi_size_wide[mi->bsize];
273
61.6k
  const int bh = mi_size_high[mi->bsize];
274
61.6k
  const int mis = mi_params->mi_stride;
275
61.6k
  const int mi_width = mi_size_wide[bsize];
276
61.6k
  const int mi_height = mi_size_high[bsize];
277
61.6k
  TxfmSearchInfo *txfm_info = &x->txfm_search_info;
278
279
61.6k
  assert(mi->bsize == bsize);
280
281
61.6k
  *mi_addr = *mi;
282
61.6k
  copy_mbmi_ext_frame_to_mbmi_ext(&x->mbmi_ext, &ctx->mbmi_ext_best,
283
61.6k
                                  av1_ref_frame_type(ctx->mic.ref_frame));
284
285
61.6k
  memcpy(txfm_info->blk_skip, ctx->blk_skip,
286
61.6k
         sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
287
288
61.6k
  txfm_info->skip_txfm = ctx->rd_stats.skip_txfm;
289
290
61.6k
  xd->tx_type_map = ctx->tx_type_map;
291
61.6k
  xd->tx_type_map_stride = mi_size_wide[bsize];
292
  // If not dry_run, copy the transform type data into the frame level buffer.
293
  // Encoder will fetch tx types when writing bitstream.
294
61.6k
  if (!dry_run) {
295
11.8k
    const int grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col);
296
11.8k
    uint8_t *const tx_type_map = mi_params->tx_type_map + grid_idx;
297
11.8k
    const int mi_stride = mi_params->mi_stride;
298
176k
    for (int blk_row = 0; blk_row < bh; ++blk_row) {
299
164k
      av1_copy_array(tx_type_map + blk_row * mi_stride,
300
164k
                     xd->tx_type_map + blk_row * xd->tx_type_map_stride, bw);
301
164k
    }
302
11.8k
    xd->tx_type_map = tx_type_map;
303
11.8k
    xd->tx_type_map_stride = mi_stride;
304
11.8k
  }
305
306
  // If segmentation in use
307
61.6k
  if (seg->enabled) {
308
    // For in frame complexity AQ copy the segment id from the segment map.
309
0
    if (cpi->oxcf.q_cfg.aq_mode == COMPLEXITY_AQ) {
310
0
      const uint8_t *const map =
311
0
          seg->update_map ? cpi->enc_seg.map : cm->last_frame_seg_map;
312
0
      mi_addr->segment_id =
313
0
          map ? get_segment_id(mi_params, map, bsize, mi_row, mi_col) : 0;
314
0
      reset_tx_size(x, mi_addr, x->txfm_search_params.tx_mode_search_type);
315
0
    }
316
    // Else for cyclic refresh mode update the segment map, set the segment id
317
    // and then update the quantizer.
318
0
    if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
319
0
        !cpi->rc.rtc_external_ratectrl) {
320
0
      av1_cyclic_refresh_update_segment(cpi, x, mi_row, mi_col, bsize,
321
0
                                        ctx->rd_stats.rate, ctx->rd_stats.dist,
322
0
                                        txfm_info->skip_txfm, dry_run);
323
0
    }
324
0
    if (mi_addr->uv_mode == UV_CFL_PRED && !is_cfl_allowed(xd))
325
0
      mi_addr->uv_mode = UV_DC_PRED;
326
0
  }
327
328
  // Count zero motion vector.
329
61.6k
  if (!dry_run && cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
330
61.6k
      !frame_is_intra_only(cm)) {
331
0
    const MV mv = mi->mv[0].as_mv;
332
0
    if (is_inter_block(mi) && mi->ref_frame[0] == LAST_FRAME &&
333
0
        abs(mv.row) < 8 && abs(mv.col) < 8) {
334
0
      const int ymis = AOMMIN(cm->mi_params.mi_rows - mi_row, bh);
335
      // Accumulate low_content_frame.
336
0
      for (int mi_y = 0; mi_y < ymis; mi_y += 2) x->cnt_zeromv += bw << 1;
337
0
    }
338
0
  }
339
340
246k
  for (i = 0; i < num_planes; ++i) {
341
184k
    p[i].coeff = ctx->coeff[i];
342
184k
    p[i].qcoeff = ctx->qcoeff[i];
343
184k
    p[i].dqcoeff = ctx->dqcoeff[i];
344
184k
    p[i].eobs = ctx->eobs[i];
345
184k
    p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
346
184k
  }
347
184k
  for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i];
348
  // Restore the coding context of the MB to that that was in place
349
  // when the mode was picked for it
350
396k
  for (y = 0; y < mi_height; y++) {
351
3.37M
    for (x_idx = 0; x_idx < mi_width; x_idx++) {
352
3.04M
      if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx &&
353
3.04M
          (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
354
2.62M
        xd->mi[x_idx + y * mis] = mi_addr;
355
2.62M
      }
356
3.04M
    }
357
334k
  }
358
359
61.6k
  if (cpi->oxcf.q_cfg.aq_mode)
360
0
    av1_init_plane_quantizers(cpi, x, mi_addr->segment_id);
361
362
61.6k
  if (dry_run) return;
363
364
#if CONFIG_INTERNAL_STATS
365
  {
366
    unsigned int *const mode_chosen_counts =
367
        (unsigned int *)cpi->mode_chosen_counts;  // Cast const away.
368
    if (frame_is_intra_only(cm)) {
369
      static const int kf_mode_index[] = {
370
        THR_DC /*DC_PRED*/,
371
        THR_V_PRED /*V_PRED*/,
372
        THR_H_PRED /*H_PRED*/,
373
        THR_D45_PRED /*D45_PRED*/,
374
        THR_D135_PRED /*D135_PRED*/,
375
        THR_D113_PRED /*D113_PRED*/,
376
        THR_D157_PRED /*D157_PRED*/,
377
        THR_D203_PRED /*D203_PRED*/,
378
        THR_D67_PRED /*D67_PRED*/,
379
        THR_SMOOTH,   /*SMOOTH_PRED*/
380
        THR_SMOOTH_V, /*SMOOTH_V_PRED*/
381
        THR_SMOOTH_H, /*SMOOTH_H_PRED*/
382
        THR_PAETH /*PAETH_PRED*/,
383
      };
384
      ++mode_chosen_counts[kf_mode_index[mi_addr->mode]];
385
    } else {
386
      // Note how often each mode chosen as best
387
      ++mode_chosen_counts[ctx->best_mode_index];
388
    }
389
  }
390
#endif
391
11.8k
  if (!frame_is_intra_only(cm)) {
392
0
    if (cm->features.interp_filter == SWITCHABLE &&
393
0
        mi_addr->motion_mode != WARPED_CAUSAL &&
394
0
        !is_nontrans_global_motion(xd, xd->mi[0])) {
395
0
      update_filter_type_count(td->counts, xd, mi_addr);
396
0
    }
397
0
  }
398
399
11.8k
  const int x_mis = AOMMIN(bw, mi_params->mi_cols - mi_col);
400
11.8k
  const int y_mis = AOMMIN(bh, mi_params->mi_rows - mi_row);
401
11.8k
  if (cm->seq_params->order_hint_info.enable_ref_frame_mvs)
402
0
    av1_copy_frame_mvs(cm, mi, mi_row, mi_col, x_mis, y_mis);
403
11.8k
}
404
405
void av1_update_inter_mode_stats(FRAME_CONTEXT *fc, FRAME_COUNTS *counts,
406
0
                                 PREDICTION_MODE mode, int16_t mode_context) {
407
0
  (void)counts;
408
409
0
  int16_t mode_ctx = mode_context & NEWMV_CTX_MASK;
410
0
  if (mode == NEWMV) {
411
#if CONFIG_ENTROPY_STATS
412
    ++counts->newmv_mode[mode_ctx][0];
413
#endif
414
0
    update_cdf(fc->newmv_cdf[mode_ctx], 0, 2);
415
0
    return;
416
0
  }
417
418
#if CONFIG_ENTROPY_STATS
419
  ++counts->newmv_mode[mode_ctx][1];
420
#endif
421
0
  update_cdf(fc->newmv_cdf[mode_ctx], 1, 2);
422
423
0
  mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
424
0
  if (mode == GLOBALMV) {
425
#if CONFIG_ENTROPY_STATS
426
    ++counts->zeromv_mode[mode_ctx][0];
427
#endif
428
0
    update_cdf(fc->zeromv_cdf[mode_ctx], 0, 2);
429
0
    return;
430
0
  }
431
432
#if CONFIG_ENTROPY_STATS
433
  ++counts->zeromv_mode[mode_ctx][1];
434
#endif
435
0
  update_cdf(fc->zeromv_cdf[mode_ctx], 1, 2);
436
437
0
  mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
438
#if CONFIG_ENTROPY_STATS
439
  ++counts->refmv_mode[mode_ctx][mode != NEARESTMV];
440
#endif
441
0
  update_cdf(fc->refmv_cdf[mode_ctx], mode != NEARESTMV, 2);
442
0
}
443
444
static void update_palette_cdf(MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
445
0
                               FRAME_COUNTS *counts) {
446
0
  FRAME_CONTEXT *fc = xd->tile_ctx;
447
0
  const BLOCK_SIZE bsize = mbmi->bsize;
448
0
  const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
449
0
  const int palette_bsize_ctx = av1_get_palette_bsize_ctx(bsize);
450
451
0
  (void)counts;
452
453
0
  if (mbmi->mode == DC_PRED) {
454
0
    const int n = pmi->palette_size[0];
455
0
    const int palette_mode_ctx = av1_get_palette_mode_ctx(xd);
456
457
#if CONFIG_ENTROPY_STATS
458
    ++counts->palette_y_mode[palette_bsize_ctx][palette_mode_ctx][n > 0];
459
#endif
460
0
    update_cdf(fc->palette_y_mode_cdf[palette_bsize_ctx][palette_mode_ctx],
461
0
               n > 0, 2);
462
0
    if (n > 0) {
463
#if CONFIG_ENTROPY_STATS
464
      ++counts->palette_y_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE];
465
#endif
466
0
      update_cdf(fc->palette_y_size_cdf[palette_bsize_ctx],
467
0
                 n - PALETTE_MIN_SIZE, PALETTE_SIZES);
468
0
    }
469
0
  }
470
471
0
  if (mbmi->uv_mode == UV_DC_PRED) {
472
0
    const int n = pmi->palette_size[1];
473
0
    const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
474
475
#if CONFIG_ENTROPY_STATS
476
    ++counts->palette_uv_mode[palette_uv_mode_ctx][n > 0];
477
#endif
478
0
    update_cdf(fc->palette_uv_mode_cdf[palette_uv_mode_ctx], n > 0, 2);
479
480
0
    if (n > 0) {
481
#if CONFIG_ENTROPY_STATS
482
      ++counts->palette_uv_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE];
483
#endif
484
0
      update_cdf(fc->palette_uv_size_cdf[palette_bsize_ctx],
485
0
                 n - PALETTE_MIN_SIZE, PALETTE_SIZES);
486
0
    }
487
0
  }
488
0
}
489
490
void av1_sum_intra_stats(const AV1_COMMON *const cm, FRAME_COUNTS *counts,
491
                         MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
492
                         const MB_MODE_INFO *above_mi,
493
11.8k
                         const MB_MODE_INFO *left_mi, const int intraonly) {
494
11.8k
  FRAME_CONTEXT *fc = xd->tile_ctx;
495
11.8k
  const PREDICTION_MODE y_mode = mbmi->mode;
496
11.8k
  (void)counts;
497
11.8k
  const BLOCK_SIZE bsize = mbmi->bsize;
498
499
11.8k
  if (intraonly) {
500
#if CONFIG_ENTROPY_STATS
501
    const PREDICTION_MODE above = av1_above_block_mode(above_mi);
502
    const PREDICTION_MODE left = av1_left_block_mode(left_mi);
503
    const int above_ctx = intra_mode_context[above];
504
    const int left_ctx = intra_mode_context[left];
505
    ++counts->kf_y_mode[above_ctx][left_ctx][y_mode];
506
#endif  // CONFIG_ENTROPY_STATS
507
11.8k
    update_cdf(get_y_mode_cdf(fc, above_mi, left_mi), y_mode, INTRA_MODES);
508
11.8k
  } else {
509
#if CONFIG_ENTROPY_STATS
510
    ++counts->y_mode[size_group_lookup[bsize]][y_mode];
511
#endif  // CONFIG_ENTROPY_STATS
512
0
    update_cdf(fc->y_mode_cdf[size_group_lookup[bsize]], y_mode, INTRA_MODES);
513
0
  }
514
515
11.8k
  if (av1_filter_intra_allowed(cm, mbmi)) {
516
704
    const int use_filter_intra_mode =
517
704
        mbmi->filter_intra_mode_info.use_filter_intra;
518
#if CONFIG_ENTROPY_STATS
519
    ++counts->filter_intra[mbmi->bsize][use_filter_intra_mode];
520
    if (use_filter_intra_mode) {
521
      ++counts
522
            ->filter_intra_mode[mbmi->filter_intra_mode_info.filter_intra_mode];
523
    }
524
#endif  // CONFIG_ENTROPY_STATS
525
704
    update_cdf(fc->filter_intra_cdfs[mbmi->bsize], use_filter_intra_mode, 2);
526
704
    if (use_filter_intra_mode) {
527
2
      update_cdf(fc->filter_intra_mode_cdf,
528
2
                 mbmi->filter_intra_mode_info.filter_intra_mode,
529
2
                 FILTER_INTRA_MODES);
530
2
    }
531
704
  }
532
11.8k
  if (av1_is_directional_mode(mbmi->mode) && av1_use_angle_delta(bsize)) {
533
#if CONFIG_ENTROPY_STATS
534
    ++counts->angle_delta[mbmi->mode - V_PRED]
535
                         [mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA];
536
#endif
537
148
    update_cdf(fc->angle_delta_cdf[mbmi->mode - V_PRED],
538
148
               mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA,
539
148
               2 * MAX_ANGLE_DELTA + 1);
540
148
  }
541
542
11.8k
  if (!xd->is_chroma_ref) return;
543
544
11.8k
  const UV_PREDICTION_MODE uv_mode = mbmi->uv_mode;
545
11.8k
  const CFL_ALLOWED_TYPE cfl_allowed = is_cfl_allowed(xd);
546
#if CONFIG_ENTROPY_STATS
547
  ++counts->uv_mode[cfl_allowed][y_mode][uv_mode];
548
#endif  // CONFIG_ENTROPY_STATS
549
11.8k
  update_cdf(fc->uv_mode_cdf[cfl_allowed][y_mode], uv_mode,
550
11.8k
             UV_INTRA_MODES - !cfl_allowed);
551
11.8k
  if (uv_mode == UV_CFL_PRED) {
552
0
    const int8_t joint_sign = mbmi->cfl_alpha_signs;
553
0
    const uint8_t idx = mbmi->cfl_alpha_idx;
554
555
#if CONFIG_ENTROPY_STATS
556
    ++counts->cfl_sign[joint_sign];
557
#endif
558
0
    update_cdf(fc->cfl_sign_cdf, joint_sign, CFL_JOINT_SIGNS);
559
0
    if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
560
0
      aom_cdf_prob *cdf_u = fc->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
561
562
#if CONFIG_ENTROPY_STATS
563
      ++counts->cfl_alpha[CFL_CONTEXT_U(joint_sign)][CFL_IDX_U(idx)];
564
#endif
565
0
      update_cdf(cdf_u, CFL_IDX_U(idx), CFL_ALPHABET_SIZE);
566
0
    }
567
0
    if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
568
0
      aom_cdf_prob *cdf_v = fc->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
569
570
#if CONFIG_ENTROPY_STATS
571
      ++counts->cfl_alpha[CFL_CONTEXT_V(joint_sign)][CFL_IDX_V(idx)];
572
#endif
573
0
      update_cdf(cdf_v, CFL_IDX_V(idx), CFL_ALPHABET_SIZE);
574
0
    }
575
0
  }
576
11.8k
  if (av1_is_directional_mode(get_uv_mode(uv_mode)) &&
577
11.8k
      av1_use_angle_delta(bsize)) {
578
#if CONFIG_ENTROPY_STATS
579
    ++counts->angle_delta[uv_mode - UV_V_PRED]
580
                         [mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA];
581
#endif
582
148
    update_cdf(fc->angle_delta_cdf[uv_mode - UV_V_PRED],
583
148
               mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA,
584
148
               2 * MAX_ANGLE_DELTA + 1);
585
148
  }
586
11.8k
  if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize)) {
587
0
    update_palette_cdf(xd, mbmi, counts);
588
0
  }
589
11.8k
}
590
591
void av1_restore_context(MACROBLOCK *x, const RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
592
                         int mi_row, int mi_col, BLOCK_SIZE bsize,
593
177k
                         const int num_planes) {
594
177k
  MACROBLOCKD *xd = &x->e_mbd;
595
177k
  int p;
596
177k
  const int num_4x4_blocks_wide = mi_size_wide[bsize];
597
177k
  const int num_4x4_blocks_high = mi_size_high[bsize];
598
177k
  int mi_width = mi_size_wide[bsize];
599
177k
  int mi_height = mi_size_high[bsize];
600
710k
  for (p = 0; p < num_planes; p++) {
601
533k
    int tx_col = mi_col;
602
533k
    int tx_row = mi_row & MAX_MIB_MASK;
603
533k
    memcpy(
604
533k
        xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x),
605
533k
        ctx->a + num_4x4_blocks_wide * p,
606
533k
        (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
607
533k
            xd->plane[p].subsampling_x);
608
533k
    memcpy(xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y),
609
533k
           ctx->l + num_4x4_blocks_high * p,
610
533k
           (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
611
533k
               xd->plane[p].subsampling_y);
612
533k
  }
613
177k
  memcpy(xd->above_partition_context + mi_col, ctx->sa,
614
177k
         sizeof(*xd->above_partition_context) * mi_width);
615
177k
  memcpy(xd->left_partition_context + (mi_row & MAX_MIB_MASK), ctx->sl,
616
177k
         sizeof(xd->left_partition_context[0]) * mi_height);
617
177k
  xd->above_txfm_context = ctx->p_ta;
618
177k
  xd->left_txfm_context = ctx->p_tl;
619
177k
  memcpy(xd->above_txfm_context, ctx->ta,
620
177k
         sizeof(*xd->above_txfm_context) * mi_width);
621
177k
  memcpy(xd->left_txfm_context, ctx->tl,
622
177k
         sizeof(*xd->left_txfm_context) * mi_height);
623
177k
}
624
625
void av1_save_context(const MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
626
                      int mi_row, int mi_col, BLOCK_SIZE bsize,
627
88.0k
                      const int num_planes) {
628
88.0k
  const MACROBLOCKD *xd = &x->e_mbd;
629
88.0k
  int p;
630
88.0k
  int mi_width = mi_size_wide[bsize];
631
88.0k
  int mi_height = mi_size_high[bsize];
632
633
  // buffer the above/left context information of the block in search.
634
352k
  for (p = 0; p < num_planes; ++p) {
635
264k
    int tx_col = mi_col;
636
264k
    int tx_row = mi_row & MAX_MIB_MASK;
637
264k
    memcpy(
638
264k
        ctx->a + mi_width * p,
639
264k
        xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x),
640
264k
        (sizeof(ENTROPY_CONTEXT) * mi_width) >> xd->plane[p].subsampling_x);
641
264k
    memcpy(ctx->l + mi_height * p,
642
264k
           xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y),
643
264k
           (sizeof(ENTROPY_CONTEXT) * mi_height) >> xd->plane[p].subsampling_y);
644
264k
  }
645
88.0k
  memcpy(ctx->sa, xd->above_partition_context + mi_col,
646
88.0k
         sizeof(*xd->above_partition_context) * mi_width);
647
88.0k
  memcpy(ctx->sl, xd->left_partition_context + (mi_row & MAX_MIB_MASK),
648
88.0k
         sizeof(xd->left_partition_context[0]) * mi_height);
649
88.0k
  memcpy(ctx->ta, xd->above_txfm_context,
650
88.0k
         sizeof(*xd->above_txfm_context) * mi_width);
651
88.0k
  memcpy(ctx->tl, xd->left_txfm_context,
652
88.0k
         sizeof(*xd->left_txfm_context) * mi_height);
653
88.0k
  ctx->p_ta = xd->above_txfm_context;
654
88.0k
  ctx->p_tl = xd->left_txfm_context;
655
88.0k
}
656
657
static void set_partial_sb_partition(const AV1_COMMON *const cm,
658
                                     MB_MODE_INFO *mi, int bh_in, int bw_in,
659
                                     int mi_rows_remaining,
660
                                     int mi_cols_remaining, BLOCK_SIZE bsize,
661
0
                                     MB_MODE_INFO **mib) {
662
0
  int bh = bh_in;
663
0
  int r, c;
664
0
  for (r = 0; r < cm->seq_params->mib_size; r += bh) {
665
0
    int bw = bw_in;
666
0
    for (c = 0; c < cm->seq_params->mib_size; c += bw) {
667
0
      const int grid_index = get_mi_grid_idx(&cm->mi_params, r, c);
668
0
      const int mi_index = get_alloc_mi_idx(&cm->mi_params, r, c);
669
0
      mib[grid_index] = mi + mi_index;
670
0
      mib[grid_index]->bsize = find_partition_size(
671
0
          bsize, mi_rows_remaining - r, mi_cols_remaining - c, &bh, &bw);
672
0
    }
673
0
  }
674
0
}
675
676
// This function attempts to set all mode info entries in a given superblock
677
// to the same block partition size.
678
// However, at the bottom and right borders of the image the requested size
679
// may not be allowed in which case this code attempts to choose the largest
680
// allowable partition.
681
void av1_set_fixed_partitioning(AV1_COMP *cpi, const TileInfo *const tile,
682
                                MB_MODE_INFO **mib, int mi_row, int mi_col,
683
0
                                BLOCK_SIZE bsize) {
684
0
  AV1_COMMON *const cm = &cpi->common;
685
0
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
686
0
  const int mi_rows_remaining = tile->mi_row_end - mi_row;
687
0
  const int mi_cols_remaining = tile->mi_col_end - mi_col;
688
0
  MB_MODE_INFO *const mi_upper_left =
689
0
      mi_params->mi_alloc + get_alloc_mi_idx(mi_params, mi_row, mi_col);
690
0
  int bh = mi_size_high[bsize];
691
0
  int bw = mi_size_wide[bsize];
692
693
0
  assert(bsize >= mi_params->mi_alloc_bsize &&
694
0
         "Attempted to use bsize < mi_params->mi_alloc_bsize");
695
0
  assert((mi_rows_remaining > 0) && (mi_cols_remaining > 0));
696
697
  // Apply the requested partition size to the SB if it is all "in image"
698
0
  if ((mi_cols_remaining >= cm->seq_params->mib_size) &&
699
0
      (mi_rows_remaining >= cm->seq_params->mib_size)) {
700
0
    for (int block_row = 0; block_row < cm->seq_params->mib_size;
701
0
         block_row += bh) {
702
0
      for (int block_col = 0; block_col < cm->seq_params->mib_size;
703
0
           block_col += bw) {
704
0
        const int grid_index = get_mi_grid_idx(mi_params, block_row, block_col);
705
0
        const int mi_index = get_alloc_mi_idx(mi_params, block_row, block_col);
706
0
        mib[grid_index] = mi_upper_left + mi_index;
707
0
        mib[grid_index]->bsize = bsize;
708
0
      }
709
0
    }
710
0
  } else {
711
    // Else this is a partial SB.
712
0
    set_partial_sb_partition(cm, mi_upper_left, bh, bw, mi_rows_remaining,
713
0
                             mi_cols_remaining, bsize, mib);
714
0
  }
715
0
}
716
717
int av1_is_leaf_split_partition(AV1_COMMON *cm, int mi_row, int mi_col,
718
0
                                BLOCK_SIZE bsize) {
719
0
  const int bs = mi_size_wide[bsize];
720
0
  const int hbs = bs / 2;
721
0
  assert(bsize >= BLOCK_8X8);
722
0
  const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
723
724
0
  for (int i = 0; i < 4; i++) {
725
0
    int x_idx = (i & 1) * hbs;
726
0
    int y_idx = (i >> 1) * hbs;
727
0
    if ((mi_row + y_idx >= cm->mi_params.mi_rows) ||
728
0
        (mi_col + x_idx >= cm->mi_params.mi_cols))
729
0
      return 0;
730
0
    if (get_partition(cm, mi_row + y_idx, mi_col + x_idx, subsize) !=
731
0
            PARTITION_NONE &&
732
0
        subsize != BLOCK_8X8)
733
0
      return 0;
734
0
  }
735
0
  return 1;
736
0
}
737
738
#if !CONFIG_REALTIME_ONLY
739
int av1_get_rdmult_delta(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
740
0
                         int mi_col, int orig_rdmult) {
741
0
  AV1_COMMON *const cm = &cpi->common;
742
0
  const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
743
0
  assert(IMPLIES(cpi->ppi->gf_group.size > 0,
744
0
                 cpi->gf_frame_index < cpi->ppi->gf_group.size));
745
0
  const int tpl_idx = cpi->gf_frame_index;
746
0
  TplParams *const tpl_data = &cpi->ppi->tpl_data;
747
0
  const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
748
0
  int64_t intra_cost = 0;
749
0
  int64_t mc_dep_cost = 0;
750
0
  const int mi_wide = mi_size_wide[bsize];
751
0
  const int mi_high = mi_size_high[bsize];
752
753
0
  TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
754
0
  TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
755
0
  int tpl_stride = tpl_frame->stride;
756
757
0
  if (!av1_tpl_stats_ready(&cpi->ppi->tpl_data, cpi->gf_frame_index)) {
758
0
    return orig_rdmult;
759
0
  }
760
0
  if (!is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) {
761
0
    return orig_rdmult;
762
0
  }
763
764
0
  int mi_count = 0;
765
0
  const int mi_col_sr =
766
0
      coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
767
0
  const int mi_col_end_sr =
768
0
      coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
769
0
  const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
770
0
  const int step = 1 << block_mis_log2;
771
0
  const int row_step = step;
772
0
  const int col_step_sr =
773
0
      coded_to_superres_mi(step, cm->superres_scale_denominator);
774
0
  for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
775
0
    for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
776
0
      if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
777
0
      TplDepStats *this_stats =
778
0
          &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
779
0
      int64_t mc_dep_delta =
780
0
          RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
781
0
                 this_stats->mc_dep_dist);
782
0
      intra_cost += this_stats->recrf_dist << RDDIV_BITS;
783
0
      mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
784
0
      mi_count++;
785
0
    }
786
0
  }
787
0
  assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
788
789
0
  double beta = 1.0;
790
0
  if (mc_dep_cost > 0 && intra_cost > 0) {
791
0
    const double r0 = cpi->rd.r0;
792
0
    const double rk = (double)intra_cost / mc_dep_cost;
793
0
    beta = (r0 / rk);
794
0
  }
795
796
0
  int rdmult = av1_get_adaptive_rdmult(cpi, beta);
797
798
0
  rdmult = AOMMIN(rdmult, orig_rdmult * 3 / 2);
799
0
  rdmult = AOMMAX(rdmult, orig_rdmult * 1 / 2);
800
801
0
  rdmult = AOMMAX(1, rdmult);
802
803
0
  return rdmult;
804
0
}
805
806
// Checks to see if a super block is on a horizontal image edge.
807
// In most cases this is the "real" edge unless there are formatting
808
// bars embedded in the stream.
809
25.2k
int av1_active_h_edge(const AV1_COMP *cpi, int mi_row, int mi_step) {
810
25.2k
  int top_edge = 0;
811
25.2k
  int bottom_edge = cpi->common.mi_params.mi_rows;
812
25.2k
  int is_active_h_edge = 0;
813
814
  // For two pass account for any formatting bars detected.
815
25.2k
  if (is_stat_consumption_stage_twopass(cpi)) {
816
0
    const AV1_COMMON *const cm = &cpi->common;
817
0
    const FIRSTPASS_STATS *const this_frame_stats = read_one_frame_stats(
818
0
        &cpi->ppi->twopass, cm->current_frame.display_order_hint);
819
0
    if (this_frame_stats == NULL) return AOM_CODEC_ERROR;
820
821
    // The inactive region is specified in MBs not mi units.
822
    // The image edge is in the following MB row.
823
0
    top_edge += (int)(this_frame_stats->inactive_zone_rows * 4);
824
825
0
    bottom_edge -= (int)(this_frame_stats->inactive_zone_rows * 4);
826
0
    bottom_edge = AOMMAX(top_edge, bottom_edge);
827
0
  }
828
829
25.2k
  if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) ||
830
25.2k
      ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) {
831
3.96k
    is_active_h_edge = 1;
832
3.96k
  }
833
25.2k
  return is_active_h_edge;
834
25.2k
}
835
836
// Checks to see if a super block is on a vertical image edge.
837
// In most cases this is the "real" edge unless there are formatting
838
// bars embedded in the stream.
839
25.2k
int av1_active_v_edge(const AV1_COMP *cpi, int mi_col, int mi_step) {
840
25.2k
  int left_edge = 0;
841
25.2k
  int right_edge = cpi->common.mi_params.mi_cols;
842
25.2k
  int is_active_v_edge = 0;
843
844
  // For two pass account for any formatting bars detected.
845
25.2k
  if (is_stat_consumption_stage_twopass(cpi)) {
846
0
    const AV1_COMMON *const cm = &cpi->common;
847
0
    const FIRSTPASS_STATS *const this_frame_stats = read_one_frame_stats(
848
0
        &cpi->ppi->twopass, cm->current_frame.display_order_hint);
849
0
    if (this_frame_stats == NULL) return AOM_CODEC_ERROR;
850
851
    // The inactive region is specified in MBs not mi units.
852
    // The image edge is in the following MB row.
853
0
    left_edge += (int)(this_frame_stats->inactive_zone_cols * 4);
854
855
0
    right_edge -= (int)(this_frame_stats->inactive_zone_cols * 4);
856
0
    right_edge = AOMMAX(left_edge, right_edge);
857
0
  }
858
859
25.2k
  if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) ||
860
25.2k
      ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) {
861
3.37k
    is_active_v_edge = 1;
862
3.37k
  }
863
25.2k
  return is_active_v_edge;
864
25.2k
}
865
866
void av1_get_tpl_stats_sb(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
867
11.7k
                          int mi_col, SuperBlockEnc *sb_enc) {
868
11.7k
  sb_enc->tpl_data_count = 0;
869
870
11.7k
  if (!cpi->oxcf.algo_cfg.enable_tpl_model) return;
871
11.7k
  if (cpi->common.current_frame.frame_type == KEY_FRAME) return;
872
0
  const FRAME_UPDATE_TYPE update_type =
873
0
      get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
874
0
  if (update_type == INTNL_OVERLAY_UPDATE || update_type == OVERLAY_UPDATE)
875
0
    return;
876
0
  assert(IMPLIES(cpi->ppi->gf_group.size > 0,
877
0
                 cpi->gf_frame_index < cpi->ppi->gf_group.size));
878
879
0
  AV1_COMMON *const cm = &cpi->common;
880
0
  const int gf_group_index = cpi->gf_frame_index;
881
0
  TplParams *const tpl_data = &cpi->ppi->tpl_data;
882
0
  if (!av1_tpl_stats_ready(tpl_data, gf_group_index)) return;
883
0
  const int mi_wide = mi_size_wide[bsize];
884
0
  const int mi_high = mi_size_high[bsize];
885
886
0
  TplDepFrame *tpl_frame = &tpl_data->tpl_frame[gf_group_index];
887
0
  TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
888
0
  int tpl_stride = tpl_frame->stride;
889
890
0
  int mi_count = 0;
891
0
  int count = 0;
892
0
  const int mi_col_sr =
893
0
      coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
894
0
  const int mi_col_end_sr =
895
0
      coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
896
  // mi_cols_sr is mi_cols at superres case.
897
0
  const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
898
899
  // TPL store unit size is not the same as the motion estimation unit size.
900
  // Here always use motion estimation size to avoid getting repetitive inter/
901
  // intra cost.
902
0
  const BLOCK_SIZE tpl_bsize = convert_length_to_bsize(tpl_data->tpl_bsize_1d);
903
0
  assert(mi_size_wide[tpl_bsize] == mi_size_high[tpl_bsize]);
904
0
  const int row_step = mi_size_high[tpl_bsize];
905
0
  const int col_step_sr = coded_to_superres_mi(mi_size_wide[tpl_bsize],
906
0
                                               cm->superres_scale_denominator);
907
908
  // Stride is only based on SB size, and we fill in values for every 16x16
909
  // block in a SB.
910
0
  sb_enc->tpl_stride = (mi_col_end_sr - mi_col_sr) / col_step_sr;
911
912
0
  for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
913
0
    for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
914
0
      assert(count < MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
915
      // Handle partial SB, so that no invalid values are used later.
916
0
      if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) {
917
0
        sb_enc->tpl_inter_cost[count] = INT64_MAX;
918
0
        sb_enc->tpl_intra_cost[count] = INT64_MAX;
919
0
        for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
920
0
          sb_enc->tpl_mv[count][i].as_int = INVALID_MV;
921
0
        }
922
0
        count++;
923
0
        continue;
924
0
      }
925
926
0
      TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
927
0
          row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
928
0
      sb_enc->tpl_inter_cost[count] = this_stats->inter_cost;
929
0
      sb_enc->tpl_intra_cost[count] = this_stats->intra_cost;
930
0
      memcpy(sb_enc->tpl_mv[count], this_stats->mv, sizeof(this_stats->mv));
931
0
      mi_count++;
932
0
      count++;
933
0
    }
934
0
  }
935
936
0
  assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
937
0
  sb_enc->tpl_data_count = mi_count;
938
0
}
939
940
// analysis_type 0: Use mc_dep_cost and intra_cost
941
// analysis_type 1: Use count of best inter predictor chosen
942
// analysis_type 2: Use cost reduction from intra to inter for best inter
943
//                  predictor chosen
944
int av1_get_q_for_deltaq_objective(AV1_COMP *const cpi, ThreadData *td,
945
                                   int64_t *delta_dist, BLOCK_SIZE bsize,
946
11.7k
                                   int mi_row, int mi_col) {
947
11.7k
  AV1_COMMON *const cm = &cpi->common;
948
11.7k
  assert(IMPLIES(cpi->ppi->gf_group.size > 0,
949
11.7k
                 cpi->gf_frame_index < cpi->ppi->gf_group.size));
950
11.7k
  const int tpl_idx = cpi->gf_frame_index;
951
11.7k
  TplParams *const tpl_data = &cpi->ppi->tpl_data;
952
11.7k
  const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
953
11.7k
  double intra_cost = 0;
954
11.7k
  double mc_dep_reg = 0;
955
11.7k
  double mc_dep_cost = 0;
956
11.7k
  double cbcmp_base = 1;
957
11.7k
  double srcrf_dist = 0;
958
11.7k
  double srcrf_sse = 0;
959
11.7k
  double srcrf_rate = 0;
960
11.7k
  const int mi_wide = mi_size_wide[bsize];
961
11.7k
  const int mi_high = mi_size_high[bsize];
962
11.7k
  const int base_qindex = cm->quant_params.base_qindex;
963
964
11.7k
  if (tpl_idx >= MAX_TPL_FRAME_IDX) return base_qindex;
965
966
11.7k
  TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
967
11.7k
  TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
968
11.7k
  int tpl_stride = tpl_frame->stride;
969
11.7k
  if (!tpl_frame->is_valid) return base_qindex;
970
971
0
  int mi_count = 0;
972
0
  const int mi_col_sr =
973
0
      coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
974
0
  const int mi_col_end_sr =
975
0
      coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
976
0
  const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
977
0
  const int step = 1 << block_mis_log2;
978
0
  const int row_step = step;
979
0
  const int col_step_sr =
980
0
      coded_to_superres_mi(step, cm->superres_scale_denominator);
981
0
  for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
982
0
    for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
983
0
      if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
984
0
      TplDepStats *this_stats =
985
0
          &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
986
0
      double cbcmp = (double)this_stats->srcrf_dist;
987
0
      int64_t mc_dep_delta =
988
0
          RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
989
0
                 this_stats->mc_dep_dist);
990
0
      double dist_scaled = (double)(this_stats->recrf_dist << RDDIV_BITS);
991
0
      intra_cost += log(dist_scaled) * cbcmp;
992
0
      mc_dep_cost += log(dist_scaled + mc_dep_delta) * cbcmp;
993
0
      mc_dep_reg += log(3 * dist_scaled + mc_dep_delta) * cbcmp;
994
0
      srcrf_dist += (double)(this_stats->srcrf_dist << RDDIV_BITS);
995
0
      srcrf_sse += (double)(this_stats->srcrf_sse << RDDIV_BITS);
996
0
      srcrf_rate += (double)this_stats->srcrf_rate;
997
0
      mi_count++;
998
0
      cbcmp_base += cbcmp;
999
0
    }
1000
0
  }
1001
0
  assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
1002
1003
0
  int offset = 0;
1004
0
  double beta = 1.0;
1005
0
  double rk;
1006
0
  if (mc_dep_cost > 0 && intra_cost > 0) {
1007
0
    const double r0 = cpi->rd.r0;
1008
0
    rk = exp((intra_cost - mc_dep_cost) / cbcmp_base);
1009
0
    td->mb.rb = exp((intra_cost - mc_dep_reg) / cbcmp_base);
1010
0
    beta = (r0 / rk);
1011
0
    assert(beta > 0.0);
1012
0
  } else {
1013
0
    return base_qindex;
1014
0
  }
1015
0
  offset = av1_get_deltaq_offset(cm->seq_params->bit_depth, base_qindex, beta);
1016
1017
0
  const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
1018
0
  offset = AOMMIN(offset, delta_q_info->delta_q_res * 9 - 1);
1019
0
  offset = AOMMAX(offset, -delta_q_info->delta_q_res * 9 + 1);
1020
0
  int qindex = cm->quant_params.base_qindex + offset;
1021
0
  qindex = AOMMIN(qindex, MAXQ);
1022
0
  qindex = AOMMAX(qindex, MINQ);
1023
1024
0
  int frm_qstep = av1_dc_quant_QTX(base_qindex, 0, cm->seq_params->bit_depth);
1025
0
  int sbs_qstep =
1026
0
      av1_dc_quant_QTX(base_qindex, offset, cm->seq_params->bit_depth);
1027
1028
0
  if (delta_dist) {
1029
0
    double sbs_dist = srcrf_dist * pow((double)sbs_qstep / frm_qstep, 2.0);
1030
0
    double sbs_rate = srcrf_rate * ((double)frm_qstep / sbs_qstep);
1031
0
    sbs_dist = AOMMIN(sbs_dist, srcrf_sse);
1032
0
    *delta_dist = (int64_t)((sbs_dist - srcrf_dist) / rk);
1033
0
    *delta_dist += RDCOST(tpl_frame->base_rdmult, 4 * 256, 0);
1034
0
    *delta_dist += RDCOST(tpl_frame->base_rdmult, sbs_rate - srcrf_rate, 0);
1035
0
  }
1036
0
  return qindex;
1037
0
}
1038
1039
#if !DISABLE_HDR_LUMA_DELTAQ
1040
// offset table defined in Table3 of T-REC-H.Sup15 document.
1041
static const int hdr_thres[HDR_QP_LEVELS + 1] = { 0,   301, 367, 434, 501, 567,
1042
                                                  634, 701, 767, 834, 1024 };
1043
1044
static const int hdr10_qp_offset[HDR_QP_LEVELS] = { 3,  2,  1,  0,  -1,
1045
                                                    -2, -3, -4, -5, -6 };
1046
#endif
1047
1048
int av1_get_q_for_hdr(AV1_COMP *const cpi, MACROBLOCK *const x,
1049
0
                      BLOCK_SIZE bsize, int mi_row, int mi_col) {
1050
0
  AV1_COMMON *const cm = &cpi->common;
1051
0
  assert(cm->seq_params->bit_depth == AOM_BITS_10);
1052
1053
0
#if DISABLE_HDR_LUMA_DELTAQ
1054
0
  (void)x;
1055
0
  (void)bsize;
1056
0
  (void)mi_row;
1057
0
  (void)mi_col;
1058
0
  return cm->quant_params.base_qindex;
1059
#else
1060
  // calculate pixel average
1061
  const int block_luma_avg = av1_log_block_avg(cpi, x, bsize, mi_row, mi_col);
1062
  // adjust offset based on average of the pixel block
1063
  int offset = 0;
1064
  for (int i = 0; i < HDR_QP_LEVELS; i++) {
1065
    if (block_luma_avg >= hdr_thres[i] && block_luma_avg < hdr_thres[i + 1]) {
1066
      offset = (int)(hdr10_qp_offset[i] * QP_SCALE_FACTOR);
1067
      break;
1068
    }
1069
  }
1070
1071
  const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
1072
  offset = AOMMIN(offset, delta_q_info->delta_q_res * 9 - 1);
1073
  offset = AOMMAX(offset, -delta_q_info->delta_q_res * 9 + 1);
1074
  int qindex = cm->quant_params.base_qindex + offset;
1075
  qindex = AOMMIN(qindex, MAXQ);
1076
  qindex = AOMMAX(qindex, MINQ);
1077
1078
  return qindex;
1079
#endif
1080
0
}
1081
#endif  // !CONFIG_REALTIME_ONLY
1082
1083
void av1_reset_simple_motion_tree_partition(SIMPLE_MOTION_DATA_TREE *sms_tree,
1084
3.99M
                                            BLOCK_SIZE bsize) {
1085
3.99M
  sms_tree->partitioning = PARTITION_NONE;
1086
1087
3.99M
  if (bsize >= BLOCK_8X8) {
1088
995k
    BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
1089
4.97M
    for (int idx = 0; idx < 4; ++idx)
1090
3.98M
      av1_reset_simple_motion_tree_partition(sms_tree->split[idx], subsize);
1091
995k
  }
1092
3.99M
}
1093
1094
// Record the ref frames that have been selected by square partition blocks.
1095
void av1_update_picked_ref_frames_mask(MACROBLOCK *const x, int ref_type,
1096
                                       BLOCK_SIZE bsize, int mib_size,
1097
0
                                       int mi_row, int mi_col) {
1098
0
  assert(mi_size_wide[bsize] == mi_size_high[bsize]);
1099
0
  const int sb_size_mask = mib_size - 1;
1100
0
  const int mi_row_in_sb = mi_row & sb_size_mask;
1101
0
  const int mi_col_in_sb = mi_col & sb_size_mask;
1102
0
  const int mi_size = mi_size_wide[bsize];
1103
0
  for (int i = mi_row_in_sb; i < mi_row_in_sb + mi_size; ++i) {
1104
0
    for (int j = mi_col_in_sb; j < mi_col_in_sb + mi_size; ++j) {
1105
0
      x->picked_ref_frames_mask[i * 32 + j] |= 1 << ref_type;
1106
0
    }
1107
0
  }
1108
0
}
1109
1110
static void avg_cdf_symbol(aom_cdf_prob *cdf_ptr_left, aom_cdf_prob *cdf_ptr_tr,
1111
                           int num_cdfs, int cdf_stride, int nsymbs,
1112
764k
                           int wt_left, int wt_tr) {
1113
8.96M
  for (int i = 0; i < num_cdfs; i++) {
1114
51.7M
    for (int j = 0; j <= nsymbs; j++) {
1115
43.5M
      cdf_ptr_left[i * cdf_stride + j] =
1116
43.5M
          (aom_cdf_prob)(((int)cdf_ptr_left[i * cdf_stride + j] * wt_left +
1117
43.5M
                          (int)cdf_ptr_tr[i * cdf_stride + j] * wt_tr +
1118
43.5M
                          ((wt_left + wt_tr) / 2)) /
1119
43.5M
                         (wt_left + wt_tr));
1120
43.5M
      assert(cdf_ptr_left[i * cdf_stride + j] >= 0 &&
1121
43.5M
             cdf_ptr_left[i * cdf_stride + j] < CDF_PROB_TOP);
1122
43.5M
    }
1123
8.19M
  }
1124
764k
}
1125
1126
#define AVERAGE_CDF(cname_left, cname_tr, nsymbs) \
1127
610k
  AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, CDF_SIZE(nsymbs))
1128
1129
#define AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, cdf_stride)           \
1130
768k
  do {                                                                     \
1131
768k
    aom_cdf_prob *cdf_ptr_left = (aom_cdf_prob *)cname_left;               \
1132
768k
    aom_cdf_prob *cdf_ptr_tr = (aom_cdf_prob *)cname_tr;                   \
1133
768k
    int array_size = (int)sizeof(cname_left) / sizeof(aom_cdf_prob);       \
1134
768k
    int num_cdfs = array_size / cdf_stride;                                \
1135
768k
    avg_cdf_symbol(cdf_ptr_left, cdf_ptr_tr, num_cdfs, cdf_stride, nsymbs, \
1136
768k
                   wt_left, wt_tr);                                        \
1137
768k
  } while (0)
1138
1139
static void avg_nmv(nmv_context *nmv_left, nmv_context *nmv_tr, int wt_left,
1140
10.9k
                    int wt_tr) {
1141
10.9k
  AVERAGE_CDF(nmv_left->joints_cdf, nmv_tr->joints_cdf, 4);
1142
32.7k
  for (int i = 0; i < 2; i++) {
1143
21.8k
    AVERAGE_CDF(nmv_left->comps[i].classes_cdf, nmv_tr->comps[i].classes_cdf,
1144
21.8k
                MV_CLASSES);
1145
21.8k
    AVERAGE_CDF(nmv_left->comps[i].class0_fp_cdf,
1146
21.8k
                nmv_tr->comps[i].class0_fp_cdf, MV_FP_SIZE);
1147
21.8k
    AVERAGE_CDF(nmv_left->comps[i].fp_cdf, nmv_tr->comps[i].fp_cdf, MV_FP_SIZE);
1148
21.8k
    AVERAGE_CDF(nmv_left->comps[i].sign_cdf, nmv_tr->comps[i].sign_cdf, 2);
1149
21.8k
    AVERAGE_CDF(nmv_left->comps[i].class0_hp_cdf,
1150
21.8k
                nmv_tr->comps[i].class0_hp_cdf, 2);
1151
21.8k
    AVERAGE_CDF(nmv_left->comps[i].hp_cdf, nmv_tr->comps[i].hp_cdf, 2);
1152
21.8k
    AVERAGE_CDF(nmv_left->comps[i].class0_cdf, nmv_tr->comps[i].class0_cdf,
1153
21.8k
                CLASS0_SIZE);
1154
21.8k
    AVERAGE_CDF(nmv_left->comps[i].bits_cdf, nmv_tr->comps[i].bits_cdf, 2);
1155
21.8k
  }
1156
10.9k
}
1157
1158
// In case of row-based multi-threading of encoder, since we always
1159
// keep a top - right sync, we can average the top - right SB's CDFs and
1160
// the left SB's CDFs and use the same for current SB's encoding to
1161
// improve the performance. This function facilitates the averaging
1162
// of CDF and used only when row-mt is enabled in encoder.
1163
void av1_avg_cdf_symbols(FRAME_CONTEXT *ctx_left, FRAME_CONTEXT *ctx_tr,
1164
5.45k
                         int wt_left, int wt_tr) {
1165
5.45k
  AVERAGE_CDF(ctx_left->txb_skip_cdf, ctx_tr->txb_skip_cdf, 2);
1166
5.45k
  AVERAGE_CDF(ctx_left->eob_extra_cdf, ctx_tr->eob_extra_cdf, 2);
1167
5.45k
  AVERAGE_CDF(ctx_left->dc_sign_cdf, ctx_tr->dc_sign_cdf, 2);
1168
5.45k
  AVERAGE_CDF(ctx_left->eob_flag_cdf16, ctx_tr->eob_flag_cdf16, 5);
1169
5.45k
  AVERAGE_CDF(ctx_left->eob_flag_cdf32, ctx_tr->eob_flag_cdf32, 6);
1170
5.45k
  AVERAGE_CDF(ctx_left->eob_flag_cdf64, ctx_tr->eob_flag_cdf64, 7);
1171
5.45k
  AVERAGE_CDF(ctx_left->eob_flag_cdf128, ctx_tr->eob_flag_cdf128, 8);
1172
5.45k
  AVERAGE_CDF(ctx_left->eob_flag_cdf256, ctx_tr->eob_flag_cdf256, 9);
1173
5.45k
  AVERAGE_CDF(ctx_left->eob_flag_cdf512, ctx_tr->eob_flag_cdf512, 10);
1174
5.45k
  AVERAGE_CDF(ctx_left->eob_flag_cdf1024, ctx_tr->eob_flag_cdf1024, 11);
1175
5.45k
  AVERAGE_CDF(ctx_left->coeff_base_eob_cdf, ctx_tr->coeff_base_eob_cdf, 3);
1176
5.45k
  AVERAGE_CDF(ctx_left->coeff_base_cdf, ctx_tr->coeff_base_cdf, 4);
1177
5.45k
  AVERAGE_CDF(ctx_left->coeff_br_cdf, ctx_tr->coeff_br_cdf, BR_CDF_SIZE);
1178
5.45k
  AVERAGE_CDF(ctx_left->newmv_cdf, ctx_tr->newmv_cdf, 2);
1179
5.45k
  AVERAGE_CDF(ctx_left->zeromv_cdf, ctx_tr->zeromv_cdf, 2);
1180
5.45k
  AVERAGE_CDF(ctx_left->refmv_cdf, ctx_tr->refmv_cdf, 2);
1181
5.45k
  AVERAGE_CDF(ctx_left->drl_cdf, ctx_tr->drl_cdf, 2);
1182
5.45k
  AVERAGE_CDF(ctx_left->inter_compound_mode_cdf,
1183
5.45k
              ctx_tr->inter_compound_mode_cdf, INTER_COMPOUND_MODES);
1184
5.45k
  AVERAGE_CDF(ctx_left->compound_type_cdf, ctx_tr->compound_type_cdf,
1185
5.45k
              MASKED_COMPOUND_TYPES);
1186
5.45k
  AVERAGE_CDF(ctx_left->wedge_idx_cdf, ctx_tr->wedge_idx_cdf, 16);
1187
5.45k
  AVERAGE_CDF(ctx_left->interintra_cdf, ctx_tr->interintra_cdf, 2);
1188
5.45k
  AVERAGE_CDF(ctx_left->wedge_interintra_cdf, ctx_tr->wedge_interintra_cdf, 2);
1189
5.45k
  AVERAGE_CDF(ctx_left->interintra_mode_cdf, ctx_tr->interintra_mode_cdf,
1190
5.45k
              INTERINTRA_MODES);
1191
5.45k
  AVERAGE_CDF(ctx_left->motion_mode_cdf, ctx_tr->motion_mode_cdf, MOTION_MODES);
1192
5.45k
  AVERAGE_CDF(ctx_left->obmc_cdf, ctx_tr->obmc_cdf, 2);
1193
5.45k
  AVERAGE_CDF(ctx_left->palette_y_size_cdf, ctx_tr->palette_y_size_cdf,
1194
5.45k
              PALETTE_SIZES);
1195
5.45k
  AVERAGE_CDF(ctx_left->palette_uv_size_cdf, ctx_tr->palette_uv_size_cdf,
1196
5.45k
              PALETTE_SIZES);
1197
43.6k
  for (int j = 0; j < PALETTE_SIZES; j++) {
1198
38.1k
    int nsymbs = j + PALETTE_MIN_SIZE;
1199
38.1k
    AVG_CDF_STRIDE(ctx_left->palette_y_color_index_cdf[j],
1200
38.1k
                   ctx_tr->palette_y_color_index_cdf[j], nsymbs,
1201
38.1k
                   CDF_SIZE(PALETTE_COLORS));
1202
38.1k
    AVG_CDF_STRIDE(ctx_left->palette_uv_color_index_cdf[j],
1203
38.1k
                   ctx_tr->palette_uv_color_index_cdf[j], nsymbs,
1204
38.1k
                   CDF_SIZE(PALETTE_COLORS));
1205
38.1k
  }
1206
5.45k
  AVERAGE_CDF(ctx_left->palette_y_mode_cdf, ctx_tr->palette_y_mode_cdf, 2);
1207
5.45k
  AVERAGE_CDF(ctx_left->palette_uv_mode_cdf, ctx_tr->palette_uv_mode_cdf, 2);
1208
5.45k
  AVERAGE_CDF(ctx_left->comp_inter_cdf, ctx_tr->comp_inter_cdf, 2);
1209
5.45k
  AVERAGE_CDF(ctx_left->single_ref_cdf, ctx_tr->single_ref_cdf, 2);
1210
5.45k
  AVERAGE_CDF(ctx_left->comp_ref_type_cdf, ctx_tr->comp_ref_type_cdf, 2);
1211
5.45k
  AVERAGE_CDF(ctx_left->uni_comp_ref_cdf, ctx_tr->uni_comp_ref_cdf, 2);
1212
5.45k
  AVERAGE_CDF(ctx_left->comp_ref_cdf, ctx_tr->comp_ref_cdf, 2);
1213
5.45k
  AVERAGE_CDF(ctx_left->comp_bwdref_cdf, ctx_tr->comp_bwdref_cdf, 2);
1214
5.45k
  AVERAGE_CDF(ctx_left->txfm_partition_cdf, ctx_tr->txfm_partition_cdf, 2);
1215
5.45k
  AVERAGE_CDF(ctx_left->compound_index_cdf, ctx_tr->compound_index_cdf, 2);
1216
5.45k
  AVERAGE_CDF(ctx_left->comp_group_idx_cdf, ctx_tr->comp_group_idx_cdf, 2);
1217
5.45k
  AVERAGE_CDF(ctx_left->skip_mode_cdfs, ctx_tr->skip_mode_cdfs, 2);
1218
5.45k
  AVERAGE_CDF(ctx_left->skip_txfm_cdfs, ctx_tr->skip_txfm_cdfs, 2);
1219
5.45k
  AVERAGE_CDF(ctx_left->intra_inter_cdf, ctx_tr->intra_inter_cdf, 2);
1220
5.45k
  avg_nmv(&ctx_left->nmvc, &ctx_tr->nmvc, wt_left, wt_tr);
1221
5.45k
  avg_nmv(&ctx_left->ndvc, &ctx_tr->ndvc, wt_left, wt_tr);
1222
5.45k
  AVERAGE_CDF(ctx_left->intrabc_cdf, ctx_tr->intrabc_cdf, 2);
1223
5.45k
  AVERAGE_CDF(ctx_left->seg.tree_cdf, ctx_tr->seg.tree_cdf, MAX_SEGMENTS);
1224
5.45k
  AVERAGE_CDF(ctx_left->seg.pred_cdf, ctx_tr->seg.pred_cdf, 2);
1225
5.45k
  AVERAGE_CDF(ctx_left->seg.spatial_pred_seg_cdf,
1226
5.45k
              ctx_tr->seg.spatial_pred_seg_cdf, MAX_SEGMENTS);
1227
5.45k
  AVERAGE_CDF(ctx_left->filter_intra_cdfs, ctx_tr->filter_intra_cdfs, 2);
1228
5.45k
  AVERAGE_CDF(ctx_left->filter_intra_mode_cdf, ctx_tr->filter_intra_mode_cdf,
1229
5.45k
              FILTER_INTRA_MODES);
1230
5.45k
  AVERAGE_CDF(ctx_left->switchable_restore_cdf, ctx_tr->switchable_restore_cdf,
1231
5.45k
              RESTORE_SWITCHABLE_TYPES);
1232
5.45k
  AVERAGE_CDF(ctx_left->wiener_restore_cdf, ctx_tr->wiener_restore_cdf, 2);
1233
5.45k
  AVERAGE_CDF(ctx_left->sgrproj_restore_cdf, ctx_tr->sgrproj_restore_cdf, 2);
1234
5.45k
  AVERAGE_CDF(ctx_left->y_mode_cdf, ctx_tr->y_mode_cdf, INTRA_MODES);
1235
5.45k
  AVG_CDF_STRIDE(ctx_left->uv_mode_cdf[0], ctx_tr->uv_mode_cdf[0],
1236
5.45k
                 UV_INTRA_MODES - 1, CDF_SIZE(UV_INTRA_MODES));
1237
5.45k
  AVERAGE_CDF(ctx_left->uv_mode_cdf[1], ctx_tr->uv_mode_cdf[1], UV_INTRA_MODES);
1238
114k
  for (int i = 0; i < PARTITION_CONTEXTS; i++) {
1239
109k
    if (i < 4) {
1240
21.8k
      AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 4,
1241
21.8k
                     CDF_SIZE(10));
1242
87.2k
    } else if (i < 16) {
1243
65.4k
      AVERAGE_CDF(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 10);
1244
65.4k
    } else {
1245
21.7k
      AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 8,
1246
21.7k
                     CDF_SIZE(10));
1247
21.7k
    }
1248
109k
  }
1249
5.45k
  AVERAGE_CDF(ctx_left->switchable_interp_cdf, ctx_tr->switchable_interp_cdf,
1250
5.45k
              SWITCHABLE_FILTERS);
1251
5.45k
  AVERAGE_CDF(ctx_left->kf_y_cdf, ctx_tr->kf_y_cdf, INTRA_MODES);
1252
5.45k
  AVERAGE_CDF(ctx_left->angle_delta_cdf, ctx_tr->angle_delta_cdf,
1253
5.45k
              2 * MAX_ANGLE_DELTA + 1);
1254
5.45k
  AVG_CDF_STRIDE(ctx_left->tx_size_cdf[0], ctx_tr->tx_size_cdf[0], MAX_TX_DEPTH,
1255
5.45k
                 CDF_SIZE(MAX_TX_DEPTH + 1));
1256
5.45k
  AVERAGE_CDF(ctx_left->tx_size_cdf[1], ctx_tr->tx_size_cdf[1],
1257
5.45k
              MAX_TX_DEPTH + 1);
1258
5.45k
  AVERAGE_CDF(ctx_left->tx_size_cdf[2], ctx_tr->tx_size_cdf[2],
1259
5.45k
              MAX_TX_DEPTH + 1);
1260
5.45k
  AVERAGE_CDF(ctx_left->tx_size_cdf[3], ctx_tr->tx_size_cdf[3],
1261
5.45k
              MAX_TX_DEPTH + 1);
1262
5.45k
  AVERAGE_CDF(ctx_left->delta_q_cdf, ctx_tr->delta_q_cdf, DELTA_Q_PROBS + 1);
1263
5.45k
  AVERAGE_CDF(ctx_left->delta_lf_cdf, ctx_tr->delta_lf_cdf, DELTA_LF_PROBS + 1);
1264
27.2k
  for (int i = 0; i < FRAME_LF_COUNT; i++) {
1265
21.8k
    AVERAGE_CDF(ctx_left->delta_lf_multi_cdf[i], ctx_tr->delta_lf_multi_cdf[i],
1266
21.8k
                DELTA_LF_PROBS + 1);
1267
21.8k
  }
1268
5.45k
  AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[1], ctx_tr->intra_ext_tx_cdf[1], 7,
1269
5.45k
                 CDF_SIZE(TX_TYPES));
1270
5.45k
  AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[2], ctx_tr->intra_ext_tx_cdf[2], 5,
1271
5.45k
                 CDF_SIZE(TX_TYPES));
1272
5.45k
  AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[1], ctx_tr->inter_ext_tx_cdf[1], 16,
1273
5.45k
                 CDF_SIZE(TX_TYPES));
1274
5.45k
  AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[2], ctx_tr->inter_ext_tx_cdf[2], 12,
1275
5.45k
                 CDF_SIZE(TX_TYPES));
1276
5.45k
  AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[3], ctx_tr->inter_ext_tx_cdf[3], 2,
1277
5.45k
                 CDF_SIZE(TX_TYPES));
1278
5.45k
  AVERAGE_CDF(ctx_left->cfl_sign_cdf, ctx_tr->cfl_sign_cdf, CFL_JOINT_SIGNS);
1279
5.45k
  AVERAGE_CDF(ctx_left->cfl_alpha_cdf, ctx_tr->cfl_alpha_cdf,
1280
5.45k
              CFL_ALPHABET_SIZE);
1281
5.45k
}
1282
1283
// Grade the temporal variation of the source by comparing the current sb and
1284
// its collocated block in the last frame.
1285
0
void av1_source_content_sb(AV1_COMP *cpi, MACROBLOCK *x, int offset) {
1286
0
  unsigned int tmp_sse;
1287
0
  unsigned int tmp_variance;
1288
0
  const BLOCK_SIZE bsize = cpi->common.seq_params->sb_size;
1289
0
  uint8_t *src_y = cpi->source->y_buffer;
1290
0
  int src_ystride = cpi->source->y_stride;
1291
0
  uint8_t *last_src_y = cpi->last_source->y_buffer;
1292
0
  int last_src_ystride = cpi->last_source->y_stride;
1293
0
  uint64_t avg_source_sse_threshold = 100000;        // ~5*5*(64*64)
1294
0
  uint64_t avg_source_sse_threshold_high = 1000000;  // ~15*15*(64*64)
1295
0
  uint64_t sum_sq_thresh = 10000;  // sum = sqrt(thresh / 64*64)) ~1.5
1296
0
#if CONFIG_AV1_HIGHBITDEPTH
1297
0
  MACROBLOCKD *xd = &x->e_mbd;
1298
0
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) return;
1299
0
#endif
1300
0
  src_y += offset;
1301
0
  last_src_y += offset;
1302
0
  tmp_variance = cpi->ppi->fn_ptr[bsize].vf(src_y, src_ystride, last_src_y,
1303
0
                                            last_src_ystride, &tmp_sse);
1304
1305
0
  if (tmp_sse == 0)
1306
0
    x->content_state_sb.source_sad = kZeroSad;
1307
0
  else if (tmp_sse < avg_source_sse_threshold)
1308
0
    x->content_state_sb.source_sad = kLowSad;
1309
0
  else if (tmp_sse > avg_source_sse_threshold_high)
1310
0
    x->content_state_sb.source_sad = kHighSad;
1311
  // Detect large lighting change.
1312
  // Note: tmp_sse - tmp_variance = ((sum * sum) >> 12)
1313
0
  if (tmp_sse > 0) {
1314
0
    if (tmp_variance < (tmp_sse >> 1) &&
1315
0
        (tmp_sse - tmp_variance) > sum_sq_thresh)
1316
0
      x->content_state_sb.lighting_change = 1;
1317
0
    if ((tmp_sse - tmp_variance) < (sum_sq_thresh >> 1))
1318
0
      x->content_state_sb.low_sumdiff = 1;
1319
0
  }
1320
0
}
1321
1322
// Memset the mbmis at the current superblock to 0
1323
void av1_reset_mbmi(CommonModeInfoParams *const mi_params, BLOCK_SIZE sb_size,
1324
0
                    int mi_row, int mi_col) {
1325
  // size of sb in unit of mi (BLOCK_4X4)
1326
0
  const int sb_size_mi = mi_size_wide[sb_size];
1327
0
  const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
1328
  // size of sb in unit of allocated mi size
1329
0
  const int sb_size_alloc_mi = mi_size_wide[sb_size] / mi_alloc_size_1d;
1330
0
  assert(mi_params->mi_alloc_stride % sb_size_alloc_mi == 0 &&
1331
0
         "mi is not allocated as a multiple of sb!");
1332
0
  assert(mi_params->mi_stride % sb_size_mi == 0 &&
1333
0
         "mi_grid_base is not allocated as a multiple of sb!");
1334
1335
0
  const int mi_rows = mi_size_high[sb_size];
1336
0
  for (int cur_mi_row = 0; cur_mi_row < mi_rows; cur_mi_row++) {
1337
0
    assert(get_mi_grid_idx(mi_params, 0, mi_col + mi_alloc_size_1d) <
1338
0
           mi_params->mi_stride);
1339
0
    const int mi_grid_idx =
1340
0
        get_mi_grid_idx(mi_params, mi_row + cur_mi_row, mi_col);
1341
0
    const int alloc_mi_idx =
1342
0
        get_alloc_mi_idx(mi_params, mi_row + cur_mi_row, mi_col);
1343
0
    memset(&mi_params->mi_grid_base[mi_grid_idx], 0,
1344
0
           sb_size_mi * sizeof(*mi_params->mi_grid_base));
1345
0
    memset(&mi_params->tx_type_map[mi_grid_idx], 0,
1346
0
           sb_size_mi * sizeof(*mi_params->tx_type_map));
1347
0
    if (cur_mi_row % mi_alloc_size_1d == 0) {
1348
0
      memset(&mi_params->mi_alloc[alloc_mi_idx], 0,
1349
0
             sb_size_alloc_mi * sizeof(*mi_params->mi_alloc));
1350
0
    }
1351
0
  }
1352
0
}
1353
1354
void av1_backup_sb_state(SB_FIRST_PASS_STATS *sb_fp_stats, const AV1_COMP *cpi,
1355
                         ThreadData *td, const TileDataEnc *tile_data,
1356
0
                         int mi_row, int mi_col) {
1357
0
  MACROBLOCK *x = &td->mb;
1358
0
  MACROBLOCKD *xd = &x->e_mbd;
1359
0
  const TileInfo *tile_info = &tile_data->tile_info;
1360
1361
0
  const AV1_COMMON *cm = &cpi->common;
1362
0
  const int num_planes = av1_num_planes(cm);
1363
0
  const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
1364
1365
0
  xd->above_txfm_context =
1366
0
      cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
1367
0
  xd->left_txfm_context =
1368
0
      xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
1369
0
  av1_save_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size, num_planes);
1370
1371
0
  sb_fp_stats->rd_count = cpi->td.rd_counts;
1372
0
  sb_fp_stats->split_count = x->txfm_search_info.txb_split_count;
1373
1374
0
  sb_fp_stats->fc = *td->counts;
1375
1376
0
  memcpy(sb_fp_stats->inter_mode_rd_models, tile_data->inter_mode_rd_models,
1377
0
         sizeof(sb_fp_stats->inter_mode_rd_models));
1378
1379
0
  memcpy(sb_fp_stats->thresh_freq_fact, x->thresh_freq_fact,
1380
0
         sizeof(sb_fp_stats->thresh_freq_fact));
1381
1382
0
  const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
1383
0
  sb_fp_stats->current_qindex =
1384
0
      cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex;
1385
1386
#if CONFIG_INTERNAL_STATS
1387
  memcpy(sb_fp_stats->mode_chosen_counts, cpi->mode_chosen_counts,
1388
         sizeof(sb_fp_stats->mode_chosen_counts));
1389
#endif  // CONFIG_INTERNAL_STATS
1390
0
}
1391
1392
void av1_restore_sb_state(const SB_FIRST_PASS_STATS *sb_fp_stats, AV1_COMP *cpi,
1393
                          ThreadData *td, TileDataEnc *tile_data, int mi_row,
1394
0
                          int mi_col) {
1395
0
  MACROBLOCK *x = &td->mb;
1396
1397
0
  const AV1_COMMON *cm = &cpi->common;
1398
0
  const int num_planes = av1_num_planes(cm);
1399
0
  const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
1400
1401
0
  av1_restore_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size,
1402
0
                      num_planes);
1403
1404
0
  cpi->td.rd_counts = sb_fp_stats->rd_count;
1405
0
  x->txfm_search_info.txb_split_count = sb_fp_stats->split_count;
1406
1407
0
  *td->counts = sb_fp_stats->fc;
1408
1409
0
  memcpy(tile_data->inter_mode_rd_models, sb_fp_stats->inter_mode_rd_models,
1410
0
         sizeof(sb_fp_stats->inter_mode_rd_models));
1411
0
  memcpy(x->thresh_freq_fact, sb_fp_stats->thresh_freq_fact,
1412
0
         sizeof(sb_fp_stats->thresh_freq_fact));
1413
1414
0
  const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
1415
0
  cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex =
1416
0
      sb_fp_stats->current_qindex;
1417
1418
#if CONFIG_INTERNAL_STATS
1419
  memcpy(cpi->mode_chosen_counts, sb_fp_stats->mode_chosen_counts,
1420
         sizeof(sb_fp_stats->mode_chosen_counts));
1421
#endif  // CONFIG_INTERNAL_STATS
1422
0
}
1423
1424
/*! Checks whether to skip updating the entropy cost based on tile info.
1425
 *
1426
 * This function contains the common code used to skip the cost update of coeff,
1427
 * mode, mv and dv symbols.
1428
 */
1429
static int skip_cost_update(const SequenceHeader *seq_params,
1430
                            const TileInfo *const tile_info, const int mi_row,
1431
                            const int mi_col,
1432
23.5k
                            INTERNAL_COST_UPDATE_TYPE upd_level) {
1433
23.5k
  if (upd_level == INTERNAL_COST_UPD_SB) return 0;
1434
0
  if (upd_level == INTERNAL_COST_UPD_OFF) return 1;
1435
1436
  // upd_level is at most as frequent as each sb_row in a tile.
1437
0
  if (mi_col != tile_info->mi_col_start) return 1;
1438
1439
0
  if (upd_level == INTERNAL_COST_UPD_SBROW_SET) {
1440
0
    const int mib_size_log2 = seq_params->mib_size_log2;
1441
0
    const int sb_row = (mi_row - tile_info->mi_row_start) >> mib_size_log2;
1442
0
    const int sb_size = seq_params->mib_size * MI_SIZE;
1443
0
    const int tile_height =
1444
0
        (tile_info->mi_row_end - tile_info->mi_row_start) * MI_SIZE;
1445
    // When upd_level = INTERNAL_COST_UPD_SBROW_SET, the cost update happens
1446
    // once for 2, 4 sb rows for sb size 128, sb size 64 respectively. However,
1447
    // as the update will not be equally spaced in smaller resolutions making
1448
    // it equally spaced by calculating (mv_num_rows_cost_update) the number of
1449
    // rows after which the cost update should happen.
1450
0
    const int sb_size_update_freq_map[2] = { 2, 4 };
1451
0
    const int update_freq_sb_rows =
1452
0
        sb_size_update_freq_map[sb_size != MAX_SB_SIZE];
1453
0
    const int update_freq_num_rows = sb_size * update_freq_sb_rows;
1454
    // Round-up the division result to next integer.
1455
0
    const int num_updates_per_tile =
1456
0
        (tile_height + update_freq_num_rows - 1) / update_freq_num_rows;
1457
0
    const int num_rows_update_per_tile = num_updates_per_tile * sb_size;
1458
    // Round-up the division result to next integer.
1459
0
    const int num_sb_rows_per_update =
1460
0
        (tile_height + num_rows_update_per_tile - 1) / num_rows_update_per_tile;
1461
0
    if ((sb_row % num_sb_rows_per_update) != 0) return 1;
1462
0
  }
1463
0
  return 0;
1464
0
}
1465
1466
// Checks for skip status of mv cost update.
1467
static int skip_mv_cost_update(AV1_COMP *cpi, const TileInfo *const tile_info,
1468
0
                               const int mi_row, const int mi_col) {
1469
0
  const AV1_COMMON *cm = &cpi->common;
1470
  // For intra frames, mv cdfs are not updated during the encode. Hence, the mv
1471
  // cost calculation is skipped in this case.
1472
0
  if (frame_is_intra_only(cm)) return 1;
1473
1474
0
  return skip_cost_update(cm->seq_params, tile_info, mi_row, mi_col,
1475
0
                          cpi->sf.inter_sf.mv_cost_upd_level);
1476
0
}
1477
1478
// Checks for skip status of dv cost update.
1479
static int skip_dv_cost_update(AV1_COMP *cpi, const TileInfo *const tile_info,
1480
0
                               const int mi_row, const int mi_col) {
1481
0
  const AV1_COMMON *cm = &cpi->common;
1482
  // Intrabc is only applicable to intra frames. So skip if intrabc is not
1483
  // allowed.
1484
0
  if (!av1_allow_intrabc(cm) || is_stat_generation_stage(cpi)) {
1485
0
    return 1;
1486
0
  }
1487
1488
0
  return skip_cost_update(cm->seq_params, tile_info, mi_row, mi_col,
1489
0
                          cpi->sf.intra_sf.dv_cost_upd_level);
1490
0
}
1491
1492
// Update the rate costs of some symbols according to the frequency directed
1493
// by speed features
1494
void av1_set_cost_upd_freq(AV1_COMP *cpi, ThreadData *td,
1495
                           const TileInfo *const tile_info, const int mi_row,
1496
11.7k
                           const int mi_col) {
1497
11.7k
  AV1_COMMON *const cm = &cpi->common;
1498
11.7k
  const int num_planes = av1_num_planes(cm);
1499
11.7k
  MACROBLOCK *const x = &td->mb;
1500
11.7k
  MACROBLOCKD *const xd = &x->e_mbd;
1501
1502
11.7k
  switch (cpi->sf.inter_sf.coeff_cost_upd_level) {
1503
0
    case INTERNAL_COST_UPD_OFF:
1504
0
    case INTERNAL_COST_UPD_TILE:  // Tile level
1505
0
      break;
1506
0
    case INTERNAL_COST_UPD_SBROW_SET:  // SB row set level in tile
1507
0
    case INTERNAL_COST_UPD_SBROW:      // SB row level in tile
1508
11.7k
    case INTERNAL_COST_UPD_SB:         // SB level
1509
11.7k
      if (skip_cost_update(cm->seq_params, tile_info, mi_row, mi_col,
1510
11.7k
                           cpi->sf.inter_sf.coeff_cost_upd_level))
1511
0
        break;
1512
11.7k
      av1_fill_coeff_costs(&x->coeff_costs, xd->tile_ctx, num_planes);
1513
11.7k
      break;
1514
0
    default: assert(0);
1515
11.7k
  }
1516
1517
11.7k
  switch (cpi->sf.inter_sf.mode_cost_upd_level) {
1518
0
    case INTERNAL_COST_UPD_OFF:
1519
0
    case INTERNAL_COST_UPD_TILE:  // Tile level
1520
0
      break;
1521
0
    case INTERNAL_COST_UPD_SBROW_SET:  // SB row set level in tile
1522
0
    case INTERNAL_COST_UPD_SBROW:      // SB row level in tile
1523
11.7k
    case INTERNAL_COST_UPD_SB:         // SB level
1524
11.7k
      if (skip_cost_update(cm->seq_params, tile_info, mi_row, mi_col,
1525
11.7k
                           cpi->sf.inter_sf.mode_cost_upd_level))
1526
0
        break;
1527
11.7k
      av1_fill_mode_rates(cm, &x->mode_costs, xd->tile_ctx);
1528
11.7k
      break;
1529
0
    default: assert(0);
1530
11.7k
  }
1531
1532
11.7k
  switch (cpi->sf.inter_sf.mv_cost_upd_level) {
1533
11.7k
    case INTERNAL_COST_UPD_OFF:
1534
11.7k
    case INTERNAL_COST_UPD_TILE:  // Tile level
1535
11.7k
      break;
1536
0
    case INTERNAL_COST_UPD_SBROW_SET:  // SB row set level in tile
1537
0
    case INTERNAL_COST_UPD_SBROW:      // SB row level in tile
1538
0
    case INTERNAL_COST_UPD_SB:         // SB level
1539
      // Checks for skip status of mv cost update.
1540
0
      if (skip_mv_cost_update(cpi, tile_info, mi_row, mi_col)) break;
1541
0
      av1_fill_mv_costs(&xd->tile_ctx->nmvc,
1542
0
                        cm->features.cur_frame_force_integer_mv,
1543
0
                        cm->features.allow_high_precision_mv, x->mv_costs);
1544
0
      break;
1545
0
    default: assert(0);
1546
11.7k
  }
1547
1548
11.7k
  switch (cpi->sf.intra_sf.dv_cost_upd_level) {
1549
11.7k
    case INTERNAL_COST_UPD_OFF:
1550
11.7k
    case INTERNAL_COST_UPD_TILE:  // Tile level
1551
11.7k
      break;
1552
0
    case INTERNAL_COST_UPD_SBROW_SET:  // SB row set level in tile
1553
0
    case INTERNAL_COST_UPD_SBROW:      // SB row level in tile
1554
0
    case INTERNAL_COST_UPD_SB:         // SB level
1555
      // Checks for skip status of dv cost update.
1556
0
      if (skip_dv_cost_update(cpi, tile_info, mi_row, mi_col)) break;
1557
0
      av1_fill_dv_costs(&xd->tile_ctx->ndvc, x->dv_costs);
1558
0
      break;
1559
0
    default: assert(0);
1560
11.7k
  }
1561
11.7k
}