Coverage Report

Created: 2026-08-13 06:43

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