blob: 372b7c36e8d6ea85f88c87fe3b456540256ba436 [file] [log] [blame]
Jayasanker Je9ad4752020-06-30 19:30:03 +05301/*
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_ports/system_state.h"
13
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
21static AOM_INLINE int set_deltaq_rdmult(const AV1_COMP *const cpi,
22 const MACROBLOCK *const x) {
23 const AV1_COMMON *const cm = &cpi->common;
24 const CommonQuantParams *quant_params = &cm->quant_params;
25 return av1_compute_rd_mult(cpi, quant_params->base_qindex + x->delta_qindex +
26 quant_params->y_dc_delta_q);
27}
28
29void av1_set_ssim_rdmult(const AV1_COMP *const cpi, MvCosts *const mv_costs,
30 const BLOCK_SIZE bsize, const int mi_row,
31 const int mi_col, int *const rdmult) {
32 const AV1_COMMON *const cm = &cpi->common;
33
34 const int bsize_base = BLOCK_16X16;
35 const int num_mi_w = mi_size_wide[bsize_base];
36 const int num_mi_h = mi_size_high[bsize_base];
37 const int num_cols = (cm->mi_params.mi_cols + num_mi_w - 1) / num_mi_w;
38 const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
39 const int num_bcols = (mi_size_wide[bsize] + num_mi_w - 1) / num_mi_w;
40 const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h;
41 int row, col;
42 double num_of_mi = 0.0;
43 double geom_mean_of_scale = 0.0;
44
Vishesh94a65292020-07-01 15:28:53 +053045 assert(cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIM);
Jayasanker Je9ad4752020-06-30 19:30:03 +053046
47 aom_clear_system_state();
48 for (row = mi_row / num_mi_w;
49 row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
50 for (col = mi_col / num_mi_h;
51 col < num_cols && col < mi_col / num_mi_h + num_bcols; ++col) {
52 const int index = row * num_cols + col;
53 geom_mean_of_scale += log(cpi->ssim_rdmult_scaling_factors[index]);
54 num_of_mi += 1.0;
55 }
56 }
57 geom_mean_of_scale = exp(geom_mean_of_scale / num_of_mi);
58
59 *rdmult = (int)((double)(*rdmult) * geom_mean_of_scale + 0.5);
60 *rdmult = AOMMAX(*rdmult, 0);
61 av1_set_error_per_bit(mv_costs, *rdmult);
62 aom_clear_system_state();
63}
64
65int av1_get_hier_tpl_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x,
66 const BLOCK_SIZE bsize, const int mi_row,
67 const int mi_col, int orig_rdmult) {
68 const AV1_COMMON *const cm = &cpi->common;
69 const GF_GROUP *const gf_group = &cpi->gf_group;
70 assert(IMPLIES(cpi->gf_group.size > 0,
71 cpi->gf_group.index < cpi->gf_group.size));
72 const int tpl_idx = cpi->gf_group.index;
73 const TplDepFrame *tpl_frame = &cpi->tpl_data.tpl_frame[tpl_idx];
74 const int deltaq_rdmult = set_deltaq_rdmult(cpi, x);
75 if (tpl_frame->is_valid == 0) return deltaq_rdmult;
Deepa K G21e5e8e2020-03-28 13:26:09 +053076 if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return deltaq_rdmult;
Jayasanker Je9ad4752020-06-30 19:30:03 +053077 if (tpl_idx >= MAX_TPL_FRAME_IDX) return deltaq_rdmult;
78 if (cpi->superres_mode != AOM_SUPERRES_NONE) return deltaq_rdmult;
79 if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return deltaq_rdmult;
80
81 const int bsize_base = BLOCK_16X16;
82 const int num_mi_w = mi_size_wide[bsize_base];
83 const int num_mi_h = mi_size_high[bsize_base];
84 const int num_cols = (cm->mi_params.mi_cols + num_mi_w - 1) / num_mi_w;
85 const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
86 const int num_bcols = (mi_size_wide[bsize] + num_mi_w - 1) / num_mi_w;
87 const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h;
88 int row, col;
89 double base_block_count = 0.0;
90 double geom_mean_of_scale = 0.0;
91 aom_clear_system_state();
92 for (row = mi_row / num_mi_w;
93 row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
94 for (col = mi_col / num_mi_h;
95 col < num_cols && col < mi_col / num_mi_h + num_bcols; ++col) {
96 const int index = row * num_cols + col;
97 geom_mean_of_scale += log(cpi->tpl_sb_rdmult_scaling_factors[index]);
98 base_block_count += 1.0;
99 }
100 }
101 geom_mean_of_scale = exp(geom_mean_of_scale / base_block_count);
102 int rdmult = (int)((double)orig_rdmult * geom_mean_of_scale + 0.5);
103 rdmult = AOMMAX(rdmult, 0);
104 av1_set_error_per_bit(&x->mv_costs, rdmult);
105 aom_clear_system_state();
106 if (bsize == cm->seq_params.sb_size) {
107 const int rdmult_sb = set_deltaq_rdmult(cpi, x);
108 assert(rdmult_sb == rdmult);
109 (void)rdmult_sb;
110 }
111 return rdmult;
112}
113
114static AOM_INLINE void update_filter_type_count(FRAME_COUNTS *counts,
115 const MACROBLOCKD *xd,
116 const MB_MODE_INFO *mbmi) {
117 int dir;
118 for (dir = 0; dir < 2; ++dir) {
119 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
120 InterpFilter filter = av1_extract_interp_filter(mbmi->interp_filters, dir);
121 ++counts->switchable_interp[ctx][filter];
122 }
123}
124
125static void reset_tx_size(MACROBLOCK *x, MB_MODE_INFO *mbmi,
126 const TX_MODE tx_mode) {
127 MACROBLOCKD *const xd = &x->e_mbd;
128 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
129 if (xd->lossless[mbmi->segment_id]) {
130 mbmi->tx_size = TX_4X4;
131 } else if (tx_mode != TX_MODE_SELECT) {
132 mbmi->tx_size = tx_size_from_tx_mode(mbmi->sb_type, tx_mode);
133 } else {
134 BLOCK_SIZE bsize = mbmi->sb_type;
135 TX_SIZE min_tx_size = depth_to_tx_size(MAX_TX_DEPTH, bsize);
136 mbmi->tx_size = (TX_SIZE)TXSIZEMAX(mbmi->tx_size, min_tx_size);
137 }
138 if (is_inter_block(mbmi)) {
139 memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size));
140 }
141 const int stride = xd->tx_type_map_stride;
142 const int bw = mi_size_wide[mbmi->sb_type];
143 for (int row = 0; row < mi_size_high[mbmi->sb_type]; ++row) {
144 memset(xd->tx_type_map + row * stride, DCT_DCT,
145 bw * sizeof(xd->tx_type_map[0]));
146 }
147 av1_zero(txfm_info->blk_skip);
148 txfm_info->skip_txfm = 0;
149}
150
151// This function will copy the best reference mode information from
152// MB_MODE_INFO_EXT_FRAME to MB_MODE_INFO_EXT.
153static INLINE void copy_mbmi_ext_frame_to_mbmi_ext(
154 MB_MODE_INFO_EXT *mbmi_ext,
155 const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_best, uint8_t ref_frame_type) {
156 memcpy(mbmi_ext->ref_mv_stack[ref_frame_type], mbmi_ext_best->ref_mv_stack,
157 sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE]));
158 memcpy(mbmi_ext->weight[ref_frame_type], mbmi_ext_best->weight,
159 sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE]));
160 mbmi_ext->mode_context[ref_frame_type] = mbmi_ext_best->mode_context;
161 mbmi_ext->ref_mv_count[ref_frame_type] = mbmi_ext_best->ref_mv_count;
162 memcpy(mbmi_ext->global_mvs, mbmi_ext_best->global_mvs,
163 sizeof(mbmi_ext->global_mvs));
164}
165
166void av1_update_state(const AV1_COMP *const cpi, ThreadData *td,
167 const PICK_MODE_CONTEXT *const ctx, int mi_row,
168 int mi_col, BLOCK_SIZE bsize, RUN_TYPE dry_run) {
169 int i, x_idx, y;
170 const AV1_COMMON *const cm = &cpi->common;
171 const CommonModeInfoParams *const mi_params = &cm->mi_params;
172 const int num_planes = av1_num_planes(cm);
173 RD_COUNTS *const rdc = &td->rd_counts;
174 MACROBLOCK *const x = &td->mb;
175 MACROBLOCKD *const xd = &x->e_mbd;
176 struct macroblock_plane *const p = x->plane;
177 struct macroblockd_plane *const pd = xd->plane;
178 const MB_MODE_INFO *const mi = &ctx->mic;
179 MB_MODE_INFO *const mi_addr = xd->mi[0];
180 const struct segmentation *const seg = &cm->seg;
venkat sanampudi24055022020-07-03 06:52:28 +0530181 assert(bsize < BLOCK_SIZES_ALL);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530182 const int bw = mi_size_wide[mi->sb_type];
183 const int bh = mi_size_high[mi->sb_type];
184 const int mis = mi_params->mi_stride;
185 const int mi_width = mi_size_wide[bsize];
186 const int mi_height = mi_size_high[bsize];
187 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
188
189 assert(mi->sb_type == bsize);
190
191 *mi_addr = *mi;
192 copy_mbmi_ext_frame_to_mbmi_ext(x->mbmi_ext, &ctx->mbmi_ext_best,
193 av1_ref_frame_type(ctx->mic.ref_frame));
194
195 memcpy(txfm_info->blk_skip, ctx->blk_skip,
196 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
197
198 txfm_info->skip_txfm = ctx->rd_stats.skip_txfm;
199
200 xd->tx_type_map = ctx->tx_type_map;
201 xd->tx_type_map_stride = mi_size_wide[bsize];
202 // If not dry_run, copy the transform type data into the frame level buffer.
203 // Encoder will fetch tx types when writing bitstream.
204 if (!dry_run) {
205 const int grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col);
206 uint8_t *const tx_type_map = mi_params->tx_type_map + grid_idx;
207 const int mi_stride = mi_params->mi_stride;
208 for (int blk_row = 0; blk_row < bh; ++blk_row) {
209 av1_copy_array(tx_type_map + blk_row * mi_stride,
210 xd->tx_type_map + blk_row * xd->tx_type_map_stride, bw);
211 }
212 xd->tx_type_map = tx_type_map;
213 xd->tx_type_map_stride = mi_stride;
214 }
215
216 // If segmentation in use
217 if (seg->enabled) {
218 // For in frame complexity AQ copy the segment id from the segment map.
219 if (cpi->oxcf.q_cfg.aq_mode == COMPLEXITY_AQ) {
220 const uint8_t *const map =
221 seg->update_map ? cpi->enc_seg.map : cm->last_frame_seg_map;
222 mi_addr->segment_id =
223 map ? get_segment_id(mi_params, map, bsize, mi_row, mi_col) : 0;
224 reset_tx_size(x, mi_addr, x->txfm_search_params.tx_mode_search_type);
225 }
226 // Else for cyclic refresh mode update the segment map, set the segment id
227 // and then update the quantizer.
228 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) {
229 av1_cyclic_refresh_update_segment(cpi, mi_addr, mi_row, mi_col, bsize,
230 ctx->rd_stats.rate, ctx->rd_stats.dist,
231 txfm_info->skip_txfm);
232 }
233 if (mi_addr->uv_mode == UV_CFL_PRED && !is_cfl_allowed(xd))
234 mi_addr->uv_mode = UV_DC_PRED;
235 }
236
237 for (i = 0; i < num_planes; ++i) {
238 p[i].coeff = ctx->coeff[i];
239 p[i].qcoeff = ctx->qcoeff[i];
240 p[i].dqcoeff = ctx->dqcoeff[i];
241 p[i].eobs = ctx->eobs[i];
242 p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
243 }
244 for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i];
245 // Restore the coding context of the MB to that that was in place
246 // when the mode was picked for it
247 for (y = 0; y < mi_height; y++) {
248 for (x_idx = 0; x_idx < mi_width; x_idx++) {
249 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx &&
250 (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
251 xd->mi[x_idx + y * mis] = mi_addr;
252 }
253 }
254 }
255
256 if (cpi->oxcf.q_cfg.aq_mode)
257 av1_init_plane_quantizers(cpi, x, mi_addr->segment_id);
258
259 if (dry_run) return;
260
261#if CONFIG_INTERNAL_STATS
262 {
263 unsigned int *const mode_chosen_counts =
264 (unsigned int *)cpi->mode_chosen_counts; // Cast const away.
265 if (frame_is_intra_only(cm)) {
266 static const int kf_mode_index[] = {
267 THR_DC /*DC_PRED*/,
268 THR_V_PRED /*V_PRED*/,
269 THR_H_PRED /*H_PRED*/,
270 THR_D45_PRED /*D45_PRED*/,
271 THR_D135_PRED /*D135_PRED*/,
272 THR_D113_PRED /*D113_PRED*/,
273 THR_D157_PRED /*D157_PRED*/,
274 THR_D203_PRED /*D203_PRED*/,
275 THR_D67_PRED /*D67_PRED*/,
276 THR_SMOOTH, /*SMOOTH_PRED*/
277 THR_SMOOTH_V, /*SMOOTH_V_PRED*/
278 THR_SMOOTH_H, /*SMOOTH_H_PRED*/
279 THR_PAETH /*PAETH_PRED*/,
280 };
281 ++mode_chosen_counts[kf_mode_index[mi_addr->mode]];
282 } else {
283 // Note how often each mode chosen as best
284 ++mode_chosen_counts[ctx->best_mode_index];
285 }
286 }
287#endif
288 if (!frame_is_intra_only(cm)) {
289 if (is_inter_block(mi_addr)) {
290 // TODO(sarahparker): global motion stats need to be handled per-tile
291 // to be compatible with tile-based threading.
292 update_global_motion_used(mi_addr->mode, bsize, mi_addr, rdc);
293 }
294
295 if (cm->features.interp_filter == SWITCHABLE &&
296 mi_addr->motion_mode != WARPED_CAUSAL &&
297 !is_nontrans_global_motion(xd, xd->mi[0])) {
298 update_filter_type_count(td->counts, xd, mi_addr);
299 }
300
301 rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
302 rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
303 rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
304 }
305
306 const int x_mis = AOMMIN(bw, mi_params->mi_cols - mi_col);
307 const int y_mis = AOMMIN(bh, mi_params->mi_rows - mi_row);
308 if (cm->seq_params.order_hint_info.enable_ref_frame_mvs)
309 av1_copy_frame_mvs(cm, mi, mi_row, mi_col, x_mis, y_mis);
310}
311
312void av1_update_inter_mode_stats(FRAME_CONTEXT *fc, FRAME_COUNTS *counts,
313 PREDICTION_MODE mode, int16_t mode_context) {
314 (void)counts;
315
316 int16_t mode_ctx = mode_context & NEWMV_CTX_MASK;
317 if (mode == NEWMV) {
318#if CONFIG_ENTROPY_STATS
319 ++counts->newmv_mode[mode_ctx][0];
320#endif
321 update_cdf(fc->newmv_cdf[mode_ctx], 0, 2);
322 return;
323 }
324
325#if CONFIG_ENTROPY_STATS
326 ++counts->newmv_mode[mode_ctx][1];
327#endif
328 update_cdf(fc->newmv_cdf[mode_ctx], 1, 2);
329
330 mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
331 if (mode == GLOBALMV) {
332#if CONFIG_ENTROPY_STATS
333 ++counts->zeromv_mode[mode_ctx][0];
334#endif
335 update_cdf(fc->zeromv_cdf[mode_ctx], 0, 2);
336 return;
337 }
338
339#if CONFIG_ENTROPY_STATS
340 ++counts->zeromv_mode[mode_ctx][1];
341#endif
342 update_cdf(fc->zeromv_cdf[mode_ctx], 1, 2);
343
344 mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
345#if CONFIG_ENTROPY_STATS
346 ++counts->refmv_mode[mode_ctx][mode != NEARESTMV];
347#endif
348 update_cdf(fc->refmv_cdf[mode_ctx], mode != NEARESTMV, 2);
349}
350
351static void update_palette_cdf(MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
352 FRAME_COUNTS *counts) {
353 FRAME_CONTEXT *fc = xd->tile_ctx;
354 const BLOCK_SIZE bsize = mbmi->sb_type;
355 const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
356 const int palette_bsize_ctx = av1_get_palette_bsize_ctx(bsize);
357
358 (void)counts;
359
360 if (mbmi->mode == DC_PRED) {
361 const int n = pmi->palette_size[0];
362 const int palette_mode_ctx = av1_get_palette_mode_ctx(xd);
363
364#if CONFIG_ENTROPY_STATS
365 ++counts->palette_y_mode[palette_bsize_ctx][palette_mode_ctx][n > 0];
366#endif
367 update_cdf(fc->palette_y_mode_cdf[palette_bsize_ctx][palette_mode_ctx],
368 n > 0, 2);
369 if (n > 0) {
370#if CONFIG_ENTROPY_STATS
371 ++counts->palette_y_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE];
372#endif
373 update_cdf(fc->palette_y_size_cdf[palette_bsize_ctx],
374 n - PALETTE_MIN_SIZE, PALETTE_SIZES);
375 }
376 }
377
378 if (mbmi->uv_mode == UV_DC_PRED) {
379 const int n = pmi->palette_size[1];
380 const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
381
382#if CONFIG_ENTROPY_STATS
383 ++counts->palette_uv_mode[palette_uv_mode_ctx][n > 0];
384#endif
385 update_cdf(fc->palette_uv_mode_cdf[palette_uv_mode_ctx], n > 0, 2);
386
387 if (n > 0) {
388#if CONFIG_ENTROPY_STATS
389 ++counts->palette_uv_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE];
390#endif
391 update_cdf(fc->palette_uv_size_cdf[palette_bsize_ctx],
392 n - PALETTE_MIN_SIZE, PALETTE_SIZES);
393 }
394 }
395}
396
397void av1_sum_intra_stats(const AV1_COMMON *const cm, FRAME_COUNTS *counts,
398 MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
399 const MB_MODE_INFO *above_mi,
400 const MB_MODE_INFO *left_mi, const int intraonly) {
401 FRAME_CONTEXT *fc = xd->tile_ctx;
402 const PREDICTION_MODE y_mode = mbmi->mode;
403 (void)counts;
404 const BLOCK_SIZE bsize = mbmi->sb_type;
405
406 if (intraonly) {
407#if CONFIG_ENTROPY_STATS
408 const PREDICTION_MODE above = av1_above_block_mode(above_mi);
409 const PREDICTION_MODE left = av1_left_block_mode(left_mi);
410 const int above_ctx = intra_mode_context[above];
411 const int left_ctx = intra_mode_context[left];
412 ++counts->kf_y_mode[above_ctx][left_ctx][y_mode];
413#endif // CONFIG_ENTROPY_STATS
414 update_cdf(get_y_mode_cdf(fc, above_mi, left_mi), y_mode, INTRA_MODES);
415 } else {
416#if CONFIG_ENTROPY_STATS
417 ++counts->y_mode[size_group_lookup[bsize]][y_mode];
418#endif // CONFIG_ENTROPY_STATS
419 update_cdf(fc->y_mode_cdf[size_group_lookup[bsize]], y_mode, INTRA_MODES);
420 }
421
422 if (av1_filter_intra_allowed(cm, mbmi)) {
423 const int use_filter_intra_mode =
424 mbmi->filter_intra_mode_info.use_filter_intra;
425#if CONFIG_ENTROPY_STATS
426 ++counts->filter_intra[mbmi->sb_type][use_filter_intra_mode];
427 if (use_filter_intra_mode) {
428 ++counts
429 ->filter_intra_mode[mbmi->filter_intra_mode_info.filter_intra_mode];
430 }
431#endif // CONFIG_ENTROPY_STATS
432 update_cdf(fc->filter_intra_cdfs[mbmi->sb_type], use_filter_intra_mode, 2);
433 if (use_filter_intra_mode) {
434 update_cdf(fc->filter_intra_mode_cdf,
435 mbmi->filter_intra_mode_info.filter_intra_mode,
436 FILTER_INTRA_MODES);
437 }
438 }
439 if (av1_is_directional_mode(mbmi->mode) && av1_use_angle_delta(bsize)) {
440#if CONFIG_ENTROPY_STATS
441 ++counts->angle_delta[mbmi->mode - V_PRED]
442 [mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA];
443#endif
444 update_cdf(fc->angle_delta_cdf[mbmi->mode - V_PRED],
445 mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA,
446 2 * MAX_ANGLE_DELTA + 1);
447 }
448
449 if (!xd->is_chroma_ref) return;
450
451 const UV_PREDICTION_MODE uv_mode = mbmi->uv_mode;
452 const CFL_ALLOWED_TYPE cfl_allowed = is_cfl_allowed(xd);
453#if CONFIG_ENTROPY_STATS
454 ++counts->uv_mode[cfl_allowed][y_mode][uv_mode];
455#endif // CONFIG_ENTROPY_STATS
456 update_cdf(fc->uv_mode_cdf[cfl_allowed][y_mode], uv_mode,
457 UV_INTRA_MODES - !cfl_allowed);
458 if (uv_mode == UV_CFL_PRED) {
459 const int8_t joint_sign = mbmi->cfl_alpha_signs;
460 const uint8_t idx = mbmi->cfl_alpha_idx;
461
462#if CONFIG_ENTROPY_STATS
463 ++counts->cfl_sign[joint_sign];
464#endif
465 update_cdf(fc->cfl_sign_cdf, joint_sign, CFL_JOINT_SIGNS);
466 if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
467 aom_cdf_prob *cdf_u = fc->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
468
469#if CONFIG_ENTROPY_STATS
470 ++counts->cfl_alpha[CFL_CONTEXT_U(joint_sign)][CFL_IDX_U(idx)];
471#endif
472 update_cdf(cdf_u, CFL_IDX_U(idx), CFL_ALPHABET_SIZE);
473 }
474 if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
475 aom_cdf_prob *cdf_v = fc->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
476
477#if CONFIG_ENTROPY_STATS
478 ++counts->cfl_alpha[CFL_CONTEXT_V(joint_sign)][CFL_IDX_V(idx)];
479#endif
480 update_cdf(cdf_v, CFL_IDX_V(idx), CFL_ALPHABET_SIZE);
481 }
482 }
483 if (av1_is_directional_mode(get_uv_mode(uv_mode)) &&
484 av1_use_angle_delta(bsize)) {
485#if CONFIG_ENTROPY_STATS
486 ++counts->angle_delta[uv_mode - UV_V_PRED]
487 [mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA];
488#endif
489 update_cdf(fc->angle_delta_cdf[uv_mode - UV_V_PRED],
490 mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA,
491 2 * MAX_ANGLE_DELTA + 1);
492 }
493 if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize)) {
494 update_palette_cdf(xd, mbmi, counts);
495 }
496}
497
498void av1_restore_context(MACROBLOCK *x, const RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
499 int mi_row, int mi_col, BLOCK_SIZE bsize,
500 const int num_planes) {
501 MACROBLOCKD *xd = &x->e_mbd;
502 int p;
503 const int num_4x4_blocks_wide = mi_size_wide[bsize];
504 const int num_4x4_blocks_high = mi_size_high[bsize];
505 int mi_width = mi_size_wide[bsize];
506 int mi_height = mi_size_high[bsize];
507 for (p = 0; p < num_planes; p++) {
508 int tx_col = mi_col;
509 int tx_row = mi_row & MAX_MIB_MASK;
510 memcpy(
511 xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x),
512 ctx->a + num_4x4_blocks_wide * p,
513 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
514 xd->plane[p].subsampling_x);
515 memcpy(xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y),
516 ctx->l + num_4x4_blocks_high * p,
517 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
518 xd->plane[p].subsampling_y);
519 }
520 memcpy(xd->above_partition_context + mi_col, ctx->sa,
521 sizeof(*xd->above_partition_context) * mi_width);
522 memcpy(xd->left_partition_context + (mi_row & MAX_MIB_MASK), ctx->sl,
523 sizeof(xd->left_partition_context[0]) * mi_height);
524 xd->above_txfm_context = ctx->p_ta;
525 xd->left_txfm_context = ctx->p_tl;
526 memcpy(xd->above_txfm_context, ctx->ta,
527 sizeof(*xd->above_txfm_context) * mi_width);
528 memcpy(xd->left_txfm_context, ctx->tl,
529 sizeof(*xd->left_txfm_context) * mi_height);
530}
531
532void av1_save_context(const MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
533 int mi_row, int mi_col, BLOCK_SIZE bsize,
534 const int num_planes) {
535 const MACROBLOCKD *xd = &x->e_mbd;
536 int p;
537 int mi_width = mi_size_wide[bsize];
538 int mi_height = mi_size_high[bsize];
539
540 // buffer the above/left context information of the block in search.
541 for (p = 0; p < num_planes; ++p) {
542 int tx_col = mi_col;
543 int tx_row = mi_row & MAX_MIB_MASK;
544 memcpy(
545 ctx->a + mi_width * p,
546 xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x),
547 (sizeof(ENTROPY_CONTEXT) * mi_width) >> xd->plane[p].subsampling_x);
548 memcpy(ctx->l + mi_height * p,
549 xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y),
550 (sizeof(ENTROPY_CONTEXT) * mi_height) >> xd->plane[p].subsampling_y);
551 }
552 memcpy(ctx->sa, xd->above_partition_context + mi_col,
553 sizeof(*xd->above_partition_context) * mi_width);
554 memcpy(ctx->sl, xd->left_partition_context + (mi_row & MAX_MIB_MASK),
555 sizeof(xd->left_partition_context[0]) * mi_height);
556 memcpy(ctx->ta, xd->above_txfm_context,
557 sizeof(*xd->above_txfm_context) * mi_width);
558 memcpy(ctx->tl, xd->left_txfm_context,
559 sizeof(*xd->left_txfm_context) * mi_height);
560 ctx->p_ta = xd->above_txfm_context;
561 ctx->p_tl = xd->left_txfm_context;
562}
563
564static void set_partial_sb_partition(const AV1_COMMON *const cm,
565 MB_MODE_INFO *mi, int bh_in, int bw_in,
566 int mi_rows_remaining,
567 int mi_cols_remaining, BLOCK_SIZE bsize,
568 MB_MODE_INFO **mib) {
569 int bh = bh_in;
570 int r, c;
571 for (r = 0; r < cm->seq_params.mib_size; r += bh) {
572 int bw = bw_in;
573 for (c = 0; c < cm->seq_params.mib_size; c += bw) {
574 const int grid_index = get_mi_grid_idx(&cm->mi_params, r, c);
575 const int mi_index = get_alloc_mi_idx(&cm->mi_params, r, c);
576 mib[grid_index] = mi + mi_index;
577 mib[grid_index]->sb_type = find_partition_size(
578 bsize, mi_rows_remaining - r, mi_cols_remaining - c, &bh, &bw);
579 }
580 }
581}
582
583// This function attempts to set all mode info entries in a given superblock
584// to the same block partition size.
585// However, at the bottom and right borders of the image the requested size
586// may not be allowed in which case this code attempts to choose the largest
587// allowable partition.
588void av1_set_fixed_partitioning(AV1_COMP *cpi, const TileInfo *const tile,
589 MB_MODE_INFO **mib, int mi_row, int mi_col,
590 BLOCK_SIZE bsize) {
591 AV1_COMMON *const cm = &cpi->common;
592 const CommonModeInfoParams *const mi_params = &cm->mi_params;
593 const int mi_rows_remaining = tile->mi_row_end - mi_row;
594 const int mi_cols_remaining = tile->mi_col_end - mi_col;
595 MB_MODE_INFO *const mi_upper_left =
596 mi_params->mi_alloc + get_alloc_mi_idx(mi_params, mi_row, mi_col);
597 int bh = mi_size_high[bsize];
598 int bw = mi_size_wide[bsize];
599
600 assert(bsize >= mi_params->mi_alloc_bsize &&
601 "Attempted to use bsize < mi_params->mi_alloc_bsize");
602 assert((mi_rows_remaining > 0) && (mi_cols_remaining > 0));
603
604 // Apply the requested partition size to the SB if it is all "in image"
605 if ((mi_cols_remaining >= cm->seq_params.mib_size) &&
606 (mi_rows_remaining >= cm->seq_params.mib_size)) {
607 for (int block_row = 0; block_row < cm->seq_params.mib_size;
608 block_row += bh) {
609 for (int block_col = 0; block_col < cm->seq_params.mib_size;
610 block_col += bw) {
611 const int grid_index = get_mi_grid_idx(mi_params, block_row, block_col);
612 const int mi_index = get_alloc_mi_idx(mi_params, block_row, block_col);
613 mib[grid_index] = mi_upper_left + mi_index;
614 mib[grid_index]->sb_type = bsize;
615 }
616 }
617 } else {
618 // Else this is a partial SB.
619 set_partial_sb_partition(cm, mi_upper_left, bh, bw, mi_rows_remaining,
620 mi_cols_remaining, bsize, mib);
621 }
622}
623
624int av1_is_leaf_split_partition(AV1_COMMON *cm, int mi_row, int mi_col,
625 BLOCK_SIZE bsize) {
626 const int bs = mi_size_wide[bsize];
627 const int hbs = bs / 2;
628 assert(bsize >= BLOCK_8X8);
629 const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
630
631 for (int i = 0; i < 4; i++) {
632 int x_idx = (i & 1) * hbs;
633 int y_idx = (i >> 1) * hbs;
634 if ((mi_row + y_idx >= cm->mi_params.mi_rows) ||
635 (mi_col + x_idx >= cm->mi_params.mi_cols))
636 return 0;
637 if (get_partition(cm, mi_row + y_idx, mi_col + x_idx, subsize) !=
638 PARTITION_NONE &&
639 subsize != BLOCK_8X8)
640 return 0;
641 }
642 return 1;
643}
644
645#if !CONFIG_REALTIME_ONLY
646int av1_get_rdmult_delta(AV1_COMP *cpi, BLOCK_SIZE bsize, int analysis_type,
647 int mi_row, int mi_col, int orig_rdmult) {
648 AV1_COMMON *const cm = &cpi->common;
649 const GF_GROUP *const gf_group = &cpi->gf_group;
650 assert(IMPLIES(cpi->gf_group.size > 0,
651 cpi->gf_group.index < cpi->gf_group.size));
652 const int tpl_idx = cpi->gf_group.index;
653 TplParams *const tpl_data = &cpi->tpl_data;
654 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
655 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
656 const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
657 int tpl_stride = tpl_frame->stride;
658 int64_t intra_cost = 0;
659 int64_t mc_dep_cost = 0;
660 const int mi_wide = mi_size_wide[bsize];
661 const int mi_high = mi_size_high[bsize];
662
663 if (tpl_frame->is_valid == 0) return orig_rdmult;
664
Deepa K G21e5e8e2020-03-28 13:26:09 +0530665 if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return orig_rdmult;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530666
667 if (cpi->gf_group.index >= MAX_TPL_FRAME_IDX) return orig_rdmult;
668
669 int64_t mc_count = 0, mc_saved = 0;
670 int mi_count = 0;
671 const int mi_col_sr =
672 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
673 const int mi_col_end_sr =
674 coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
675 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
676 const int step = 1 << block_mis_log2;
677 for (int row = mi_row; row < mi_row + mi_high; row += step) {
678 for (int col = mi_col_sr; col < mi_col_end_sr; col += step) {
679 if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
680 TplDepStats *this_stats =
681 &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
682 int64_t mc_dep_delta =
683 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
684 this_stats->mc_dep_dist);
685 intra_cost += this_stats->recrf_dist << RDDIV_BITS;
686 mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
687 mc_count += this_stats->mc_count;
688 mc_saved += this_stats->mc_saved;
689 mi_count++;
690 }
691 }
692
693 aom_clear_system_state();
694
695 double beta = 1.0;
696 if (analysis_type == 0) {
697 if (mc_dep_cost > 0 && intra_cost > 0) {
698 const double r0 = cpi->rd.r0;
699 const double rk = (double)intra_cost / mc_dep_cost;
700 beta = (r0 / rk);
701 }
702 } else if (analysis_type == 1) {
703 const double mc_count_base = (mi_count * cpi->rd.mc_count_base);
704 beta = (mc_count + 1.0) / (mc_count_base + 1.0);
705 beta = pow(beta, 0.5);
706 } else if (analysis_type == 2) {
707 const double mc_saved_base = (mi_count * cpi->rd.mc_saved_base);
708 beta = (mc_saved + 1.0) / (mc_saved_base + 1.0);
709 beta = pow(beta, 0.5);
710 }
711
712 int rdmult = av1_get_adaptive_rdmult(cpi, beta);
713
714 aom_clear_system_state();
715
716 rdmult = AOMMIN(rdmult, orig_rdmult * 3 / 2);
717 rdmult = AOMMAX(rdmult, orig_rdmult * 1 / 2);
718
719 rdmult = AOMMAX(1, rdmult);
720
721 return rdmult;
722}
723
724// Checks to see if a super block is on a horizontal image edge.
725// In most cases this is the "real" edge unless there are formatting
726// bars embedded in the stream.
727int av1_active_h_edge(const AV1_COMP *cpi, int mi_row, int mi_step) {
728 int top_edge = 0;
729 int bottom_edge = cpi->common.mi_params.mi_rows;
730 int is_active_h_edge = 0;
731
Jayasanker Je9ad4752020-06-30 19:30:03 +0530732 if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) ||
733 ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) {
734 is_active_h_edge = 1;
735 }
736 return is_active_h_edge;
737}
738
739// Checks to see if a super block is on a vertical image edge.
740// In most cases this is the "real" edge unless there are formatting
741// bars embedded in the stream.
742int av1_active_v_edge(const AV1_COMP *cpi, int mi_col, int mi_step) {
743 int left_edge = 0;
744 int right_edge = cpi->common.mi_params.mi_cols;
745 int is_active_v_edge = 0;
746
Jayasanker Je9ad4752020-06-30 19:30:03 +0530747 if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) ||
748 ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) {
749 is_active_v_edge = 1;
750 }
751 return is_active_v_edge;
752}
753
754void av1_get_tpl_stats_sb(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
755 int mi_col, SuperBlockEnc *sb_enc) {
756 sb_enc->tpl_data_count = 0;
757
758 if (!cpi->oxcf.algo_cfg.enable_tpl_model) return;
759 if (cpi->superres_mode != AOM_SUPERRES_NONE) return;
760 if (cpi->common.current_frame.frame_type == KEY_FRAME) return;
761 const FRAME_UPDATE_TYPE update_type = get_frame_update_type(&cpi->gf_group);
Debargha Mukherjee5f64acd2020-08-18 14:32:28 -0700762 if (update_type == INTNL_OVERLAY_UPDATE || update_type == OVERLAY_UPDATE ||
763 update_type == KFFLT_OVERLAY_UPDATE)
Jayasanker Je9ad4752020-06-30 19:30:03 +0530764 return;
765 assert(IMPLIES(cpi->gf_group.size > 0,
766 cpi->gf_group.index < cpi->gf_group.size));
767
768 AV1_COMMON *const cm = &cpi->common;
769 const int gf_group_index = cpi->gf_group.index;
770 TplParams *const tpl_data = &cpi->tpl_data;
771 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[gf_group_index];
772 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
773 int tpl_stride = tpl_frame->stride;
774 const int mi_wide = mi_size_wide[bsize];
775 const int mi_high = mi_size_high[bsize];
776
777 if (tpl_frame->is_valid == 0) return;
778 if (gf_group_index >= MAX_TPL_FRAME_IDX) return;
779
780 int mi_count = 0;
781 int count = 0;
782 const int mi_col_sr =
783 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
784 const int mi_col_end_sr =
785 coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
786 // mi_cols_sr is mi_cols at superres case.
787 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
788
789 // TPL store unit size is not the same as the motion estimation unit size.
790 // Here always use motion estimation size to avoid getting repetitive inter/
791 // intra cost.
792 const BLOCK_SIZE tpl_bsize = convert_length_to_bsize(MC_FLOW_BSIZE_1D);
793 const int step = mi_size_wide[tpl_bsize];
794 assert(mi_size_wide[tpl_bsize] == mi_size_high[tpl_bsize]);
795
796 // Stride is only based on SB size, and we fill in values for every 16x16
797 // block in a SB.
798 sb_enc->tpl_stride = (mi_col_end_sr - mi_col_sr) / step;
799
800 for (int row = mi_row; row < mi_row + mi_high; row += step) {
801 for (int col = mi_col_sr; col < mi_col_end_sr; col += step) {
802 // Handle partial SB, so that no invalid values are used later.
803 if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) {
804 sb_enc->tpl_inter_cost[count] = INT64_MAX;
805 sb_enc->tpl_intra_cost[count] = INT64_MAX;
806 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
807 sb_enc->tpl_mv[count][i].as_int = INVALID_MV;
808 }
809 count++;
810 continue;
811 }
812
813 TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
814 row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
815 sb_enc->tpl_inter_cost[count] = this_stats->inter_cost;
816 sb_enc->tpl_intra_cost[count] = this_stats->intra_cost;
817 memcpy(sb_enc->tpl_mv[count], this_stats->mv, sizeof(this_stats->mv));
818 mi_count++;
819 count++;
820 }
821 }
822
823 sb_enc->tpl_data_count = mi_count;
824}
825
826// analysis_type 0: Use mc_dep_cost and intra_cost
827// analysis_type 1: Use count of best inter predictor chosen
828// analysis_type 2: Use cost reduction from intra to inter for best inter
829// predictor chosen
830int av1_get_q_for_deltaq_objective(AV1_COMP *const cpi, BLOCK_SIZE bsize,
831 int mi_row, int mi_col) {
832 AV1_COMMON *const cm = &cpi->common;
833 const GF_GROUP *const gf_group = &cpi->gf_group;
834 assert(IMPLIES(cpi->gf_group.size > 0,
835 cpi->gf_group.index < cpi->gf_group.size));
836 const int tpl_idx = cpi->gf_group.index;
837 TplParams *const tpl_data = &cpi->tpl_data;
838 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
839 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
840 const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
841 int tpl_stride = tpl_frame->stride;
842 int64_t intra_cost = 0;
843 int64_t mc_dep_cost = 0;
844 const int mi_wide = mi_size_wide[bsize];
845 const int mi_high = mi_size_high[bsize];
846 const int base_qindex = cm->quant_params.base_qindex;
847
848 if (tpl_frame->is_valid == 0) return base_qindex;
849
Deepa K G21e5e8e2020-03-28 13:26:09 +0530850 if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return base_qindex;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530851
852 if (cpi->gf_group.index >= MAX_TPL_FRAME_IDX) return base_qindex;
853
854 int64_t mc_count = 0, mc_saved = 0;
855 int mi_count = 0;
856 const int mi_col_sr =
857 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
858 const int mi_col_end_sr =
859 coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
860 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
861 const int step = 1 << block_mis_log2;
862 for (int row = mi_row; row < mi_row + mi_high; row += step) {
863 for (int col = mi_col_sr; col < mi_col_end_sr; col += step) {
864 if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
865 TplDepStats *this_stats =
866 &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
867 int64_t mc_dep_delta =
868 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
869 this_stats->mc_dep_dist);
870 intra_cost += this_stats->recrf_dist << RDDIV_BITS;
871 mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
872 mc_count += this_stats->mc_count;
873 mc_saved += this_stats->mc_saved;
874 mi_count++;
875 }
876 }
877
878 aom_clear_system_state();
879
880 int offset = 0;
881 double beta = 1.0;
882 if (mc_dep_cost > 0 && intra_cost > 0) {
883 const double r0 = cpi->rd.r0;
884 const double rk = (double)intra_cost / mc_dep_cost;
885 beta = (r0 / rk);
886 assert(beta > 0.0);
887 }
888 offset = av1_get_deltaq_offset(cpi, base_qindex, beta);
889 aom_clear_system_state();
890
891 const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
892 offset = AOMMIN(offset, delta_q_info->delta_q_res * 9 - 1);
893 offset = AOMMAX(offset, -delta_q_info->delta_q_res * 9 + 1);
894 int qindex = cm->quant_params.base_qindex + offset;
895 qindex = AOMMIN(qindex, MAXQ);
896 qindex = AOMMAX(qindex, MINQ);
897
898 return qindex;
899}
900#endif // !CONFIG_REALTIME_ONLY
901
902void av1_reset_simple_motion_tree_partition(SIMPLE_MOTION_DATA_TREE *sms_tree,
903 BLOCK_SIZE bsize) {
904 sms_tree->partitioning = PARTITION_NONE;
905
906 if (bsize >= BLOCK_8X8) {
907 BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
908 for (int idx = 0; idx < 4; ++idx)
909 av1_reset_simple_motion_tree_partition(sms_tree->split[idx], subsize);
910 }
911}
912
913// Record the ref frames that have been selected by square partition blocks.
914void av1_update_picked_ref_frames_mask(MACROBLOCK *const x, int ref_type,
915 BLOCK_SIZE bsize, int mib_size,
916 int mi_row, int mi_col) {
917 assert(mi_size_wide[bsize] == mi_size_high[bsize]);
918 const int sb_size_mask = mib_size - 1;
919 const int mi_row_in_sb = mi_row & sb_size_mask;
920 const int mi_col_in_sb = mi_col & sb_size_mask;
921 const int mi_size = mi_size_wide[bsize];
922 for (int i = mi_row_in_sb; i < mi_row_in_sb + mi_size; ++i) {
923 for (int j = mi_col_in_sb; j < mi_col_in_sb + mi_size; ++j) {
924 x->picked_ref_frames_mask[i * 32 + j] |= 1 << ref_type;
925 }
926 }
927}
928
929static void avg_cdf_symbol(aom_cdf_prob *cdf_ptr_left, aom_cdf_prob *cdf_ptr_tr,
930 int num_cdfs, int cdf_stride, int nsymbs,
931 int wt_left, int wt_tr) {
932 for (int i = 0; i < num_cdfs; i++) {
933 for (int j = 0; j <= nsymbs; j++) {
934 cdf_ptr_left[i * cdf_stride + j] =
935 (aom_cdf_prob)(((int)cdf_ptr_left[i * cdf_stride + j] * wt_left +
936 (int)cdf_ptr_tr[i * cdf_stride + j] * wt_tr +
937 ((wt_left + wt_tr) / 2)) /
938 (wt_left + wt_tr));
939 assert(cdf_ptr_left[i * cdf_stride + j] >= 0 &&
940 cdf_ptr_left[i * cdf_stride + j] < CDF_PROB_TOP);
941 }
942 }
943}
944
945#define AVERAGE_CDF(cname_left, cname_tr, nsymbs) \
946 AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, CDF_SIZE(nsymbs))
947
948#define AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, cdf_stride) \
949 do { \
950 aom_cdf_prob *cdf_ptr_left = (aom_cdf_prob *)cname_left; \
951 aom_cdf_prob *cdf_ptr_tr = (aom_cdf_prob *)cname_tr; \
952 int array_size = (int)sizeof(cname_left) / sizeof(aom_cdf_prob); \
953 int num_cdfs = array_size / cdf_stride; \
954 avg_cdf_symbol(cdf_ptr_left, cdf_ptr_tr, num_cdfs, cdf_stride, nsymbs, \
955 wt_left, wt_tr); \
956 } while (0)
957
958static void avg_nmv(nmv_context *nmv_left, nmv_context *nmv_tr, int wt_left,
959 int wt_tr) {
960 AVERAGE_CDF(nmv_left->joints_cdf, nmv_tr->joints_cdf, 4);
961 for (int i = 0; i < 2; i++) {
962 AVERAGE_CDF(nmv_left->comps[i].classes_cdf, nmv_tr->comps[i].classes_cdf,
963 MV_CLASSES);
964 AVERAGE_CDF(nmv_left->comps[i].class0_fp_cdf,
965 nmv_tr->comps[i].class0_fp_cdf, MV_FP_SIZE);
966 AVERAGE_CDF(nmv_left->comps[i].fp_cdf, nmv_tr->comps[i].fp_cdf, MV_FP_SIZE);
967 AVERAGE_CDF(nmv_left->comps[i].sign_cdf, nmv_tr->comps[i].sign_cdf, 2);
968 AVERAGE_CDF(nmv_left->comps[i].class0_hp_cdf,
969 nmv_tr->comps[i].class0_hp_cdf, 2);
970 AVERAGE_CDF(nmv_left->comps[i].hp_cdf, nmv_tr->comps[i].hp_cdf, 2);
971 AVERAGE_CDF(nmv_left->comps[i].class0_cdf, nmv_tr->comps[i].class0_cdf,
972 CLASS0_SIZE);
973 AVERAGE_CDF(nmv_left->comps[i].bits_cdf, nmv_tr->comps[i].bits_cdf, 2);
974 }
975}
976
977// In case of row-based multi-threading of encoder, since we always
978// keep a top - right sync, we can average the top - right SB's CDFs and
979// the left SB's CDFs and use the same for current SB's encoding to
980// improve the performance. This function facilitates the averaging
981// of CDF and used only when row-mt is enabled in encoder.
982void av1_avg_cdf_symbols(FRAME_CONTEXT *ctx_left, FRAME_CONTEXT *ctx_tr,
983 int wt_left, int wt_tr) {
984 AVERAGE_CDF(ctx_left->txb_skip_cdf, ctx_tr->txb_skip_cdf, 2);
985 AVERAGE_CDF(ctx_left->eob_extra_cdf, ctx_tr->eob_extra_cdf, 2);
986 AVERAGE_CDF(ctx_left->dc_sign_cdf, ctx_tr->dc_sign_cdf, 2);
987 AVERAGE_CDF(ctx_left->eob_flag_cdf16, ctx_tr->eob_flag_cdf16, 5);
988 AVERAGE_CDF(ctx_left->eob_flag_cdf32, ctx_tr->eob_flag_cdf32, 6);
989 AVERAGE_CDF(ctx_left->eob_flag_cdf64, ctx_tr->eob_flag_cdf64, 7);
990 AVERAGE_CDF(ctx_left->eob_flag_cdf128, ctx_tr->eob_flag_cdf128, 8);
991 AVERAGE_CDF(ctx_left->eob_flag_cdf256, ctx_tr->eob_flag_cdf256, 9);
992 AVERAGE_CDF(ctx_left->eob_flag_cdf512, ctx_tr->eob_flag_cdf512, 10);
993 AVERAGE_CDF(ctx_left->eob_flag_cdf1024, ctx_tr->eob_flag_cdf1024, 11);
994 AVERAGE_CDF(ctx_left->coeff_base_eob_cdf, ctx_tr->coeff_base_eob_cdf, 3);
995 AVERAGE_CDF(ctx_left->coeff_base_cdf, ctx_tr->coeff_base_cdf, 4);
996 AVERAGE_CDF(ctx_left->coeff_br_cdf, ctx_tr->coeff_br_cdf, BR_CDF_SIZE);
997 AVERAGE_CDF(ctx_left->newmv_cdf, ctx_tr->newmv_cdf, 2);
998 AVERAGE_CDF(ctx_left->zeromv_cdf, ctx_tr->zeromv_cdf, 2);
999 AVERAGE_CDF(ctx_left->refmv_cdf, ctx_tr->refmv_cdf, 2);
1000 AVERAGE_CDF(ctx_left->drl_cdf, ctx_tr->drl_cdf, 2);
1001 AVERAGE_CDF(ctx_left->inter_compound_mode_cdf,
1002 ctx_tr->inter_compound_mode_cdf, INTER_COMPOUND_MODES);
1003 AVERAGE_CDF(ctx_left->compound_type_cdf, ctx_tr->compound_type_cdf,
1004 MASKED_COMPOUND_TYPES);
1005 AVERAGE_CDF(ctx_left->wedge_idx_cdf, ctx_tr->wedge_idx_cdf, 16);
1006 AVERAGE_CDF(ctx_left->interintra_cdf, ctx_tr->interintra_cdf, 2);
1007 AVERAGE_CDF(ctx_left->wedge_interintra_cdf, ctx_tr->wedge_interintra_cdf, 2);
1008 AVERAGE_CDF(ctx_left->interintra_mode_cdf, ctx_tr->interintra_mode_cdf,
1009 INTERINTRA_MODES);
1010 AVERAGE_CDF(ctx_left->motion_mode_cdf, ctx_tr->motion_mode_cdf, MOTION_MODES);
1011 AVERAGE_CDF(ctx_left->obmc_cdf, ctx_tr->obmc_cdf, 2);
1012 AVERAGE_CDF(ctx_left->palette_y_size_cdf, ctx_tr->palette_y_size_cdf,
1013 PALETTE_SIZES);
1014 AVERAGE_CDF(ctx_left->palette_uv_size_cdf, ctx_tr->palette_uv_size_cdf,
1015 PALETTE_SIZES);
1016 for (int j = 0; j < PALETTE_SIZES; j++) {
1017 int nsymbs = j + PALETTE_MIN_SIZE;
1018 AVG_CDF_STRIDE(ctx_left->palette_y_color_index_cdf[j],
1019 ctx_tr->palette_y_color_index_cdf[j], nsymbs,
1020 CDF_SIZE(PALETTE_COLORS));
1021 AVG_CDF_STRIDE(ctx_left->palette_uv_color_index_cdf[j],
1022 ctx_tr->palette_uv_color_index_cdf[j], nsymbs,
1023 CDF_SIZE(PALETTE_COLORS));
1024 }
1025 AVERAGE_CDF(ctx_left->palette_y_mode_cdf, ctx_tr->palette_y_mode_cdf, 2);
1026 AVERAGE_CDF(ctx_left->palette_uv_mode_cdf, ctx_tr->palette_uv_mode_cdf, 2);
1027 AVERAGE_CDF(ctx_left->comp_inter_cdf, ctx_tr->comp_inter_cdf, 2);
1028 AVERAGE_CDF(ctx_left->single_ref_cdf, ctx_tr->single_ref_cdf, 2);
1029 AVERAGE_CDF(ctx_left->comp_ref_type_cdf, ctx_tr->comp_ref_type_cdf, 2);
1030 AVERAGE_CDF(ctx_left->uni_comp_ref_cdf, ctx_tr->uni_comp_ref_cdf, 2);
1031 AVERAGE_CDF(ctx_left->comp_ref_cdf, ctx_tr->comp_ref_cdf, 2);
1032 AVERAGE_CDF(ctx_left->comp_bwdref_cdf, ctx_tr->comp_bwdref_cdf, 2);
1033 AVERAGE_CDF(ctx_left->txfm_partition_cdf, ctx_tr->txfm_partition_cdf, 2);
Debargha Mukherjee5bd41f92020-10-04 11:06:11 -07001034#if !CONFIG_REMOVE_DIST_WTD_COMP
Jayasanker Je9ad4752020-06-30 19:30:03 +05301035 AVERAGE_CDF(ctx_left->compound_index_cdf, ctx_tr->compound_index_cdf, 2);
Debargha Mukherjee5bd41f92020-10-04 11:06:11 -07001036#endif // !CONFIG_REMOVE_DIST_WTD_COMP
Jayasanker Je9ad4752020-06-30 19:30:03 +05301037 AVERAGE_CDF(ctx_left->comp_group_idx_cdf, ctx_tr->comp_group_idx_cdf, 2);
1038 AVERAGE_CDF(ctx_left->skip_mode_cdfs, ctx_tr->skip_mode_cdfs, 2);
1039 AVERAGE_CDF(ctx_left->skip_txfm_cdfs, ctx_tr->skip_txfm_cdfs, 2);
1040 AVERAGE_CDF(ctx_left->intra_inter_cdf, ctx_tr->intra_inter_cdf, 2);
1041 avg_nmv(&ctx_left->nmvc, &ctx_tr->nmvc, wt_left, wt_tr);
1042 avg_nmv(&ctx_left->ndvc, &ctx_tr->ndvc, wt_left, wt_tr);
1043 AVERAGE_CDF(ctx_left->intrabc_cdf, ctx_tr->intrabc_cdf, 2);
1044 AVERAGE_CDF(ctx_left->seg.tree_cdf, ctx_tr->seg.tree_cdf, MAX_SEGMENTS);
1045 AVERAGE_CDF(ctx_left->seg.pred_cdf, ctx_tr->seg.pred_cdf, 2);
1046 AVERAGE_CDF(ctx_left->seg.spatial_pred_seg_cdf,
1047 ctx_tr->seg.spatial_pred_seg_cdf, MAX_SEGMENTS);
1048 AVERAGE_CDF(ctx_left->filter_intra_cdfs, ctx_tr->filter_intra_cdfs, 2);
1049 AVERAGE_CDF(ctx_left->filter_intra_mode_cdf, ctx_tr->filter_intra_mode_cdf,
1050 FILTER_INTRA_MODES);
1051 AVERAGE_CDF(ctx_left->switchable_restore_cdf, ctx_tr->switchable_restore_cdf,
1052 RESTORE_SWITCHABLE_TYPES);
1053 AVERAGE_CDF(ctx_left->wiener_restore_cdf, ctx_tr->wiener_restore_cdf, 2);
1054 AVERAGE_CDF(ctx_left->sgrproj_restore_cdf, ctx_tr->sgrproj_restore_cdf, 2);
1055 AVERAGE_CDF(ctx_left->y_mode_cdf, ctx_tr->y_mode_cdf, INTRA_MODES);
1056 AVG_CDF_STRIDE(ctx_left->uv_mode_cdf[0], ctx_tr->uv_mode_cdf[0],
1057 UV_INTRA_MODES - 1, CDF_SIZE(UV_INTRA_MODES));
1058 AVERAGE_CDF(ctx_left->uv_mode_cdf[1], ctx_tr->uv_mode_cdf[1], UV_INTRA_MODES);
1059 for (int i = 0; i < PARTITION_CONTEXTS; i++) {
1060 if (i < 4) {
1061 AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 4,
1062 CDF_SIZE(10));
1063 } else if (i < 16) {
1064 AVERAGE_CDF(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 10);
1065 } else {
1066 AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 8,
1067 CDF_SIZE(10));
1068 }
1069 }
1070 AVERAGE_CDF(ctx_left->switchable_interp_cdf, ctx_tr->switchable_interp_cdf,
1071 SWITCHABLE_FILTERS);
1072 AVERAGE_CDF(ctx_left->kf_y_cdf, ctx_tr->kf_y_cdf, INTRA_MODES);
1073 AVERAGE_CDF(ctx_left->angle_delta_cdf, ctx_tr->angle_delta_cdf,
1074 2 * MAX_ANGLE_DELTA + 1);
1075 AVG_CDF_STRIDE(ctx_left->tx_size_cdf[0], ctx_tr->tx_size_cdf[0], MAX_TX_DEPTH,
1076 CDF_SIZE(MAX_TX_DEPTH + 1));
1077 AVERAGE_CDF(ctx_left->tx_size_cdf[1], ctx_tr->tx_size_cdf[1],
1078 MAX_TX_DEPTH + 1);
1079 AVERAGE_CDF(ctx_left->tx_size_cdf[2], ctx_tr->tx_size_cdf[2],
1080 MAX_TX_DEPTH + 1);
1081 AVERAGE_CDF(ctx_left->tx_size_cdf[3], ctx_tr->tx_size_cdf[3],
1082 MAX_TX_DEPTH + 1);
1083 AVERAGE_CDF(ctx_left->delta_q_cdf, ctx_tr->delta_q_cdf, DELTA_Q_PROBS + 1);
1084 AVERAGE_CDF(ctx_left->delta_lf_cdf, ctx_tr->delta_lf_cdf, DELTA_LF_PROBS + 1);
1085 for (int i = 0; i < FRAME_LF_COUNT; i++) {
1086 AVERAGE_CDF(ctx_left->delta_lf_multi_cdf[i], ctx_tr->delta_lf_multi_cdf[i],
1087 DELTA_LF_PROBS + 1);
1088 }
1089 AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[1], ctx_tr->intra_ext_tx_cdf[1], 7,
1090 CDF_SIZE(TX_TYPES));
1091 AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[2], ctx_tr->intra_ext_tx_cdf[2], 5,
1092 CDF_SIZE(TX_TYPES));
1093 AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[1], ctx_tr->inter_ext_tx_cdf[1], 16,
1094 CDF_SIZE(TX_TYPES));
1095 AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[2], ctx_tr->inter_ext_tx_cdf[2], 12,
1096 CDF_SIZE(TX_TYPES));
1097 AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[3], ctx_tr->inter_ext_tx_cdf[3], 2,
1098 CDF_SIZE(TX_TYPES));
1099 AVERAGE_CDF(ctx_left->cfl_sign_cdf, ctx_tr->cfl_sign_cdf, CFL_JOINT_SIGNS);
1100 AVERAGE_CDF(ctx_left->cfl_alpha_cdf, ctx_tr->cfl_alpha_cdf,
1101 CFL_ALPHABET_SIZE);
1102}
1103
1104// Grade the temporal variation of the source by comparing the current sb and
1105// its collocated block in the last frame.
1106void av1_source_content_sb(AV1_COMP *cpi, MACROBLOCK *x, int offset) {
1107 unsigned int tmp_sse;
1108 unsigned int tmp_variance;
1109 const BLOCK_SIZE bsize = cpi->common.seq_params.sb_size;
1110 uint8_t *src_y = cpi->source->y_buffer;
1111 int src_ystride = cpi->source->y_stride;
1112 uint8_t *last_src_y = cpi->last_source->y_buffer;
1113 int last_src_ystride = cpi->last_source->y_stride;
1114 uint64_t avg_source_sse_threshold = 100000; // ~5*5*(64*64)
1115 uint64_t avg_source_sse_threshold_high = 1000000; // ~15*15*(64*64)
1116 uint64_t sum_sq_thresh = 10000; // sum = sqrt(thresh / 64*64)) ~1.5
1117#if CONFIG_AV1_HIGHBITDEPTH
1118 MACROBLOCKD *xd = &x->e_mbd;
1119 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) return;
1120#endif
1121 src_y += offset;
1122 last_src_y += offset;
1123 tmp_variance = cpi->fn_ptr[bsize].vf(src_y, src_ystride, last_src_y,
1124 last_src_ystride, &tmp_sse);
1125 // Note: tmp_sse - tmp_variance = ((sum * sum) >> 12)
1126 // Detect large lighting change.
1127 if (tmp_variance < (tmp_sse >> 1) && (tmp_sse - tmp_variance) > sum_sq_thresh)
1128 x->content_state_sb = kLowVarHighSumdiff;
1129 else if (tmp_sse < avg_source_sse_threshold)
1130 x->content_state_sb = kLowSad;
1131 else if (tmp_sse > avg_source_sse_threshold_high)
1132 x->content_state_sb = kHighSad;
1133}
1134
1135// Memset the mbmis at the current superblock to 0
1136void av1_reset_mbmi(CommonModeInfoParams *const mi_params, BLOCK_SIZE sb_size,
1137 int mi_row, int mi_col) {
1138 // size of sb in unit of mi (BLOCK_4X4)
1139 const int sb_size_mi = mi_size_wide[sb_size];
1140 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
1141 // size of sb in unit of allocated mi size
1142 const int sb_size_alloc_mi = mi_size_wide[sb_size] / mi_alloc_size_1d;
1143 assert(mi_params->mi_alloc_stride % sb_size_alloc_mi == 0 &&
1144 "mi is not allocated as a multiple of sb!");
1145 assert(mi_params->mi_stride % sb_size_mi == 0 &&
1146 "mi_grid_base is not allocated as a multiple of sb!");
1147
1148 const int mi_rows = mi_size_high[sb_size];
1149 for (int cur_mi_row = 0; cur_mi_row < mi_rows; cur_mi_row++) {
1150 assert(get_mi_grid_idx(mi_params, 0, mi_col + mi_alloc_size_1d) <
1151 mi_params->mi_stride);
1152 const int mi_grid_idx =
1153 get_mi_grid_idx(mi_params, mi_row + cur_mi_row, mi_col);
1154 const int alloc_mi_idx =
1155 get_alloc_mi_idx(mi_params, mi_row + cur_mi_row, mi_col);
1156 memset(&mi_params->mi_grid_base[mi_grid_idx], 0,
1157 sb_size_mi * sizeof(*mi_params->mi_grid_base));
1158 memset(&mi_params->tx_type_map[mi_grid_idx], 0,
1159 sb_size_mi * sizeof(*mi_params->tx_type_map));
1160 if (cur_mi_row % mi_alloc_size_1d == 0) {
1161 memset(&mi_params->mi_alloc[alloc_mi_idx], 0,
1162 sb_size_alloc_mi * sizeof(*mi_params->mi_alloc));
1163 }
1164 }
1165}
1166
1167void av1_backup_sb_state(SB_FIRST_PASS_STATS *sb_fp_stats, const AV1_COMP *cpi,
1168 ThreadData *td, const TileDataEnc *tile_data,
1169 int mi_row, int mi_col) {
1170 MACROBLOCK *x = &td->mb;
1171 MACROBLOCKD *xd = &x->e_mbd;
1172 const TileInfo *tile_info = &tile_data->tile_info;
1173
1174 const AV1_COMMON *cm = &cpi->common;
1175 const int num_planes = av1_num_planes(cm);
1176 const BLOCK_SIZE sb_size = cm->seq_params.sb_size;
1177
1178 xd->above_txfm_context =
1179 cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
1180 xd->left_txfm_context =
1181 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
1182 av1_save_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size, num_planes);
1183
1184 sb_fp_stats->rd_count = cpi->td.rd_counts;
1185 sb_fp_stats->split_count = x->txfm_search_info.txb_split_count;
1186
1187 sb_fp_stats->fc = *td->counts;
1188
1189 memcpy(sb_fp_stats->inter_mode_rd_models, tile_data->inter_mode_rd_models,
1190 sizeof(sb_fp_stats->inter_mode_rd_models));
1191
1192 memcpy(sb_fp_stats->thresh_freq_fact, x->thresh_freq_fact,
1193 sizeof(sb_fp_stats->thresh_freq_fact));
1194
1195 const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
1196 sb_fp_stats->current_qindex =
1197 cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex;
1198
1199#if CONFIG_INTERNAL_STATS
1200 memcpy(sb_fp_stats->mode_chosen_counts, cpi->mode_chosen_counts,
1201 sizeof(sb_fp_stats->mode_chosen_counts));
1202#endif // CONFIG_INTERNAL_STATS
1203}
1204
1205void av1_restore_sb_state(const SB_FIRST_PASS_STATS *sb_fp_stats, AV1_COMP *cpi,
1206 ThreadData *td, TileDataEnc *tile_data, int mi_row,
1207 int mi_col) {
1208 MACROBLOCK *x = &td->mb;
1209
1210 const AV1_COMMON *cm = &cpi->common;
1211 const int num_planes = av1_num_planes(cm);
1212 const BLOCK_SIZE sb_size = cm->seq_params.sb_size;
1213
1214 av1_restore_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size,
1215 num_planes);
1216
1217 cpi->td.rd_counts = sb_fp_stats->rd_count;
1218 x->txfm_search_info.txb_split_count = sb_fp_stats->split_count;
1219
1220 *td->counts = sb_fp_stats->fc;
1221
1222 memcpy(tile_data->inter_mode_rd_models, sb_fp_stats->inter_mode_rd_models,
1223 sizeof(sb_fp_stats->inter_mode_rd_models));
1224 memcpy(x->thresh_freq_fact, sb_fp_stats->thresh_freq_fact,
1225 sizeof(sb_fp_stats->thresh_freq_fact));
1226
1227 const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
1228 cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex =
1229 sb_fp_stats->current_qindex;
1230
1231#if CONFIG_INTERNAL_STATS
1232 memcpy(cpi->mode_chosen_counts, sb_fp_stats->mode_chosen_counts,
1233 sizeof(sb_fp_stats->mode_chosen_counts));
1234#endif // CONFIG_INTERNAL_STATS
1235}
1236
1237// Update the rate costs of some symbols according to the frequency directed
1238// by speed features
1239void av1_set_cost_upd_freq(AV1_COMP *cpi, ThreadData *td,
1240 const TileInfo *const tile_info, const int mi_row,
1241 const int mi_col) {
1242 AV1_COMMON *const cm = &cpi->common;
1243 const int num_planes = av1_num_planes(cm);
1244 MACROBLOCK *const x = &td->mb;
1245 MACROBLOCKD *const xd = &x->e_mbd;
1246
1247 switch (cpi->oxcf.cost_upd_freq.coeff) {
1248 case COST_UPD_TILE: // Tile level
1249 if (mi_row != tile_info->mi_row_start) break;
1250 AOM_FALLTHROUGH_INTENDED;
1251 case COST_UPD_SBROW: // SB row level in tile
1252 if (mi_col != tile_info->mi_col_start) break;
1253 AOM_FALLTHROUGH_INTENDED;
1254 case COST_UPD_SB: // SB level
1255 if (cpi->sf.inter_sf.disable_sb_level_coeff_cost_upd &&
1256 mi_col != tile_info->mi_col_start)
1257 break;
1258 av1_fill_coeff_costs(&x->coeff_costs, xd->tile_ctx, num_planes);
1259 break;
1260 default: assert(0);
1261 }
1262
1263 switch (cpi->oxcf.cost_upd_freq.mode) {
1264 case COST_UPD_TILE: // Tile level
1265 if (mi_row != tile_info->mi_row_start) break;
1266 AOM_FALLTHROUGH_INTENDED;
1267 case COST_UPD_SBROW: // SB row level in tile
1268 if (mi_col != tile_info->mi_col_start) break;
1269 AOM_FALLTHROUGH_INTENDED;
1270 case COST_UPD_SB: // SB level
1271 av1_fill_mode_rates(cm, &x->mode_costs, xd->tile_ctx);
1272 break;
1273 default: assert(0);
1274 }
1275 switch (cpi->oxcf.cost_upd_freq.mv) {
1276 case COST_UPD_OFF: break;
1277 case COST_UPD_TILE: // Tile level
1278 if (mi_row != tile_info->mi_row_start) break;
1279 AOM_FALLTHROUGH_INTENDED;
1280 case COST_UPD_SBROW: // SB row level in tile
1281 if (mi_col != tile_info->mi_col_start) break;
1282 AOM_FALLTHROUGH_INTENDED;
1283 case COST_UPD_SB: // SB level
1284 if (cpi->sf.inter_sf.disable_sb_level_mv_cost_upd &&
1285 mi_col != tile_info->mi_col_start)
1286 break;
1287 av1_fill_mv_costs(xd->tile_ctx, cm->features.cur_frame_force_integer_mv,
1288 cm->features.allow_high_precision_mv, &x->mv_costs);
1289 break;
1290 default: assert(0);
1291 }
1292}