blob: 06adc08d1fdce43f9084bf2775213823c9f1a1c0 [file] [log] [blame]
Jayasanker Je9ad4752020-06-30 19:30:03 +05301/*
Krishna Rapaka7319db52021-09-28 20:35:29 -07002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Jayasanker Je9ad4752020-06-30 19:30:03 +05303 *
Vibhoothi41c6dd72021-10-12 18:48:26 +00004 * This source code is subject to the terms of the BSD 3-Clause Clear License
5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6 * License was not distributed with this source code in the LICENSE file, you
7 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the
8 * Alliance for Open Media Patent License 1.0 was not distributed with this
9 * source code in the PATENTS file, you can obtain it at
10 * aomedia.org/license/patent-license/.
Jayasanker Je9ad4752020-06-30 19:30:03 +053011 */
12
13#include "aom_ports/system_state.h"
14
15#include "av1/common/reconintra.h"
16
17#include "av1/encoder/encoder.h"
18#include "av1/encoder/encodeframe_utils.h"
19#include "av1/encoder/partition_strategy.h"
20#include "av1/encoder/rdopt.h"
21
22static AOM_INLINE int set_deltaq_rdmult(const AV1_COMP *const cpi,
23 const MACROBLOCK *const x) {
24 const AV1_COMMON *const cm = &cpi->common;
25 const CommonQuantParams *quant_params = &cm->quant_params;
26 return av1_compute_rd_mult(cpi, quant_params->base_qindex + x->delta_qindex +
27 quant_params->y_dc_delta_q);
28}
29
30void av1_set_ssim_rdmult(const AV1_COMP *const cpi, MvCosts *const mv_costs,
31 const BLOCK_SIZE bsize, const int mi_row,
32 const int mi_col, int *const rdmult) {
33 const AV1_COMMON *const cm = &cpi->common;
34
35 const int bsize_base = BLOCK_16X16;
36 const int num_mi_w = mi_size_wide[bsize_base];
37 const int num_mi_h = mi_size_high[bsize_base];
38 const int num_cols = (cm->mi_params.mi_cols + num_mi_w - 1) / num_mi_w;
39 const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
40 const int num_bcols = (mi_size_wide[bsize] + num_mi_w - 1) / num_mi_w;
41 const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h;
42 int row, col;
43 double num_of_mi = 0.0;
44 double geom_mean_of_scale = 0.0;
45
Vishesh94a65292020-07-01 15:28:53 +053046 assert(cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIM);
Jayasanker Je9ad4752020-06-30 19:30:03 +053047
48 aom_clear_system_state();
49 for (row = mi_row / num_mi_w;
50 row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
51 for (col = mi_col / num_mi_h;
52 col < num_cols && col < mi_col / num_mi_h + num_bcols; ++col) {
53 const int index = row * num_cols + col;
54 geom_mean_of_scale += log(cpi->ssim_rdmult_scaling_factors[index]);
55 num_of_mi += 1.0;
56 }
57 }
58 geom_mean_of_scale = exp(geom_mean_of_scale / num_of_mi);
59
60 *rdmult = (int)((double)(*rdmult) * geom_mean_of_scale + 0.5);
61 *rdmult = AOMMAX(*rdmult, 0);
62 av1_set_error_per_bit(mv_costs, *rdmult);
63 aom_clear_system_state();
64}
65
Urvang Joshie198bf12020-10-08 15:37:55 -070066// Return the end column for the current superblock, in unit of TPL blocks.
67static int get_superblock_tpl_column_end(const AV1_COMMON *const cm, int mi_col,
68 int num_mi_w) {
69 // Find the start column of this superblock.
70 const int sb_mi_col_start = (mi_col >> cm->seq_params.mib_size_log2)
71 << cm->seq_params.mib_size_log2;
72 // Same but in superres upscaled dimension.
73 const int sb_mi_col_start_sr =
74 coded_to_superres_mi(sb_mi_col_start, cm->superres_scale_denominator);
75 // Width of this superblock in mi units.
76 const int sb_mi_width = mi_size_wide[cm->seq_params.sb_size];
77 // Same but in superres upscaled dimension.
78 const int sb_mi_width_sr =
79 coded_to_superres_mi(sb_mi_width, cm->superres_scale_denominator);
80 // Superblock end in mi units.
81 const int sb_mi_end = sb_mi_col_start_sr + sb_mi_width_sr;
82 // Superblock end in TPL units.
83 return (sb_mi_end + num_mi_w - 1) / num_mi_w;
84}
85
Jayasanker Je9ad4752020-06-30 19:30:03 +053086int av1_get_hier_tpl_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x,
87 const BLOCK_SIZE bsize, const int mi_row,
88 const int mi_col, int orig_rdmult) {
89 const AV1_COMMON *const cm = &cpi->common;
90 const GF_GROUP *const gf_group = &cpi->gf_group;
91 assert(IMPLIES(cpi->gf_group.size > 0,
92 cpi->gf_group.index < cpi->gf_group.size));
93 const int tpl_idx = cpi->gf_group.index;
94 const TplDepFrame *tpl_frame = &cpi->tpl_data.tpl_frame[tpl_idx];
95 const int deltaq_rdmult = set_deltaq_rdmult(cpi, x);
96 if (tpl_frame->is_valid == 0) return deltaq_rdmult;
Deepa K G21e5e8e2020-03-28 13:26:09 +053097 if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return deltaq_rdmult;
Jayasanker Je9ad4752020-06-30 19:30:03 +053098 if (tpl_idx >= MAX_TPL_FRAME_IDX) return deltaq_rdmult;
Jayasanker Je9ad4752020-06-30 19:30:03 +053099 if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return deltaq_rdmult;
100
Urvang Joshie198bf12020-10-08 15:37:55 -0700101 const int mi_col_sr =
102 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
103 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
104 const int block_mi_width_sr =
105 coded_to_superres_mi(mi_size_wide[bsize], cm->superres_scale_denominator);
106
Jayasanker Je9ad4752020-06-30 19:30:03 +0530107 const int bsize_base = BLOCK_16X16;
108 const int num_mi_w = mi_size_wide[bsize_base];
109 const int num_mi_h = mi_size_high[bsize_base];
Urvang Joshie198bf12020-10-08 15:37:55 -0700110 const int num_cols = (mi_cols_sr + num_mi_w - 1) / num_mi_w;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530111 const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
Urvang Joshie198bf12020-10-08 15:37:55 -0700112 const int num_bcols = (block_mi_width_sr + num_mi_w - 1) / num_mi_w;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530113 const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h;
Urvang Joshie198bf12020-10-08 15:37:55 -0700114 // This is required because the end col of superblock may be off by 1 in case
115 // of superres.
116 const int sb_bcol_end = get_superblock_tpl_column_end(cm, mi_col, num_mi_w);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530117 int row, col;
118 double base_block_count = 0.0;
119 double geom_mean_of_scale = 0.0;
120 aom_clear_system_state();
121 for (row = mi_row / num_mi_w;
122 row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
Urvang Joshie198bf12020-10-08 15:37:55 -0700123 for (col = mi_col_sr / num_mi_h;
124 col < num_cols && col < mi_col_sr / num_mi_h + num_bcols &&
125 col < sb_bcol_end;
126 ++col) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530127 const int index = row * num_cols + col;
128 geom_mean_of_scale += log(cpi->tpl_sb_rdmult_scaling_factors[index]);
129 base_block_count += 1.0;
130 }
131 }
132 geom_mean_of_scale = exp(geom_mean_of_scale / base_block_count);
133 int rdmult = (int)((double)orig_rdmult * geom_mean_of_scale + 0.5);
134 rdmult = AOMMAX(rdmult, 0);
135 av1_set_error_per_bit(&x->mv_costs, rdmult);
136 aom_clear_system_state();
137 if (bsize == cm->seq_params.sb_size) {
138 const int rdmult_sb = set_deltaq_rdmult(cpi, x);
139 assert(rdmult_sb == rdmult);
140 (void)rdmult_sb;
141 }
142 return rdmult;
143}
144
145static AOM_INLINE void update_filter_type_count(FRAME_COUNTS *counts,
146 const MACROBLOCKD *xd,
147 const MB_MODE_INFO *mbmi) {
Hui Su93c395b2020-10-05 12:00:20 -0700148#if CONFIG_REMOVE_DUAL_FILTER
149 const int ctx = av1_get_pred_context_switchable_interp(xd, 0);
150 ++counts->switchable_interp[ctx][mbmi->interp_fltr];
151#else
152 for (int dir = 0; dir < 2; ++dir) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530153 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
154 InterpFilter filter = av1_extract_interp_filter(mbmi->interp_filters, dir);
155 ++counts->switchable_interp[ctx][filter];
156 }
Hui Su93c395b2020-10-05 12:00:20 -0700157#endif // CONFIG_REMOVE_DUAL_FILTER
Jayasanker Je9ad4752020-06-30 19:30:03 +0530158}
159
160static void reset_tx_size(MACROBLOCK *x, MB_MODE_INFO *mbmi,
161 const TX_MODE tx_mode) {
162 MACROBLOCKD *const xd = &x->e_mbd;
163 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
liang zhaoc6f775a2020-12-17 11:54:58 -0800164#if CONFIG_SDP
165 int plane_index = xd->tree_type == CHROMA_PART;
166#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530167 if (xd->lossless[mbmi->segment_id]) {
168 mbmi->tx_size = TX_4X4;
169 } else if (tx_mode != TX_MODE_SELECT) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800170#if CONFIG_SDP
171 mbmi->tx_size = tx_size_from_tx_mode(mbmi->sb_type[plane_index], tx_mode);
172#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530173 mbmi->tx_size = tx_size_from_tx_mode(mbmi->sb_type, tx_mode);
liang zhaoc6f775a2020-12-17 11:54:58 -0800174#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530175 } else {
liang zhaoc6f775a2020-12-17 11:54:58 -0800176#if CONFIG_SDP
177 BLOCK_SIZE bsize = mbmi->sb_type[plane_index];
178#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530179 BLOCK_SIZE bsize = mbmi->sb_type;
liang zhaoc6f775a2020-12-17 11:54:58 -0800180#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530181 TX_SIZE min_tx_size = depth_to_tx_size(MAX_TX_DEPTH, bsize);
182 mbmi->tx_size = (TX_SIZE)TXSIZEMAX(mbmi->tx_size, min_tx_size);
183 }
leolzhao3ab59842021-05-11 10:07:48 -0700184#if CONFIG_SDP
185 if (is_inter_block(mbmi, xd->tree_type)) {
186#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530187 if (is_inter_block(mbmi)) {
leolzhao3ab59842021-05-11 10:07:48 -0700188#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530189 memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size));
190 }
191 const int stride = xd->tx_type_map_stride;
liang zhaoc6f775a2020-12-17 11:54:58 -0800192#if CONFIG_SDP
193 const int bw = mi_size_wide[mbmi->sb_type[plane_index]];
194 for (int row = 0; row < mi_size_high[mbmi->sb_type[plane_index]]; ++row) {
195#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530196 const int bw = mi_size_wide[mbmi->sb_type];
197 for (int row = 0; row < mi_size_high[mbmi->sb_type]; ++row) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800198#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530199 memset(xd->tx_type_map + row * stride, DCT_DCT,
200 bw * sizeof(xd->tx_type_map[0]));
201 }
202 av1_zero(txfm_info->blk_skip);
203 txfm_info->skip_txfm = 0;
204}
205
206// This function will copy the best reference mode information from
207// MB_MODE_INFO_EXT_FRAME to MB_MODE_INFO_EXT.
208static INLINE void copy_mbmi_ext_frame_to_mbmi_ext(
209 MB_MODE_INFO_EXT *mbmi_ext,
210 const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_best, uint8_t ref_frame_type) {
211 memcpy(mbmi_ext->ref_mv_stack[ref_frame_type], mbmi_ext_best->ref_mv_stack,
212 sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE]));
213 memcpy(mbmi_ext->weight[ref_frame_type], mbmi_ext_best->weight,
214 sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE]));
215 mbmi_ext->mode_context[ref_frame_type] = mbmi_ext_best->mode_context;
216 mbmi_ext->ref_mv_count[ref_frame_type] = mbmi_ext_best->ref_mv_count;
217 memcpy(mbmi_ext->global_mvs, mbmi_ext_best->global_mvs,
218 sizeof(mbmi_ext->global_mvs));
219}
220
221void av1_update_state(const AV1_COMP *const cpi, ThreadData *td,
222 const PICK_MODE_CONTEXT *const ctx, int mi_row,
223 int mi_col, BLOCK_SIZE bsize, RUN_TYPE dry_run) {
224 int i, x_idx, y;
225 const AV1_COMMON *const cm = &cpi->common;
226 const CommonModeInfoParams *const mi_params = &cm->mi_params;
227 const int num_planes = av1_num_planes(cm);
228 RD_COUNTS *const rdc = &td->rd_counts;
229 MACROBLOCK *const x = &td->mb;
230 MACROBLOCKD *const xd = &x->e_mbd;
231 struct macroblock_plane *const p = x->plane;
232 struct macroblockd_plane *const pd = xd->plane;
233 const MB_MODE_INFO *const mi = &ctx->mic;
234 MB_MODE_INFO *const mi_addr = xd->mi[0];
235 const struct segmentation *const seg = &cm->seg;
venkat sanampudi24055022020-07-03 06:52:28 +0530236 assert(bsize < BLOCK_SIZES_ALL);
liang zhaoc6f775a2020-12-17 11:54:58 -0800237#if CONFIG_SDP
238 const int bw = mi_size_wide[mi->sb_type[xd->tree_type == CHROMA_PART]];
239 const int bh = mi_size_high[mi->sb_type[xd->tree_type == CHROMA_PART]];
240#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530241 const int bw = mi_size_wide[mi->sb_type];
242 const int bh = mi_size_high[mi->sb_type];
liang zhaoc6f775a2020-12-17 11:54:58 -0800243#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530244 const int mis = mi_params->mi_stride;
245 const int mi_width = mi_size_wide[bsize];
246 const int mi_height = mi_size_high[bsize];
247 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
liang zhaoc6f775a2020-12-17 11:54:58 -0800248#if CONFIG_SDP
249 assert(mi->sb_type[xd->tree_type == CHROMA_PART] == bsize);
250#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530251 assert(mi->sb_type == bsize);
liang zhaoc6f775a2020-12-17 11:54:58 -0800252#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530253
254 *mi_addr = *mi;
liang zhaoc6f775a2020-12-17 11:54:58 -0800255#if CONFIG_SDP
256 if (xd->tree_type != CHROMA_PART)
257#endif
258 copy_mbmi_ext_frame_to_mbmi_ext(x->mbmi_ext, &ctx->mbmi_ext_best,
259 av1_ref_frame_type(ctx->mic.ref_frame));
Jayasanker Je9ad4752020-06-30 19:30:03 +0530260
261 memcpy(txfm_info->blk_skip, ctx->blk_skip,
262 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
263
264 txfm_info->skip_txfm = ctx->rd_stats.skip_txfm;
liang zhaoc6f775a2020-12-17 11:54:58 -0800265#if CONFIG_SDP
266 if (xd->tree_type != CHROMA_PART) {
267#endif
268 xd->tx_type_map = ctx->tx_type_map;
269 xd->tx_type_map_stride = mi_size_wide[bsize];
270 // If not dry_run, copy the transform type data into the frame level buffer.
271 // Encoder will fetch tx types when writing bitstream.
272 if (!dry_run) {
273 const int grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col);
274 uint8_t *const tx_type_map = mi_params->tx_type_map + grid_idx;
275 const int mi_stride = mi_params->mi_stride;
276 for (int blk_row = 0; blk_row < bh; ++blk_row) {
277 av1_copy_array(tx_type_map + blk_row * mi_stride,
278 xd->tx_type_map + blk_row * xd->tx_type_map_stride, bw);
279 }
280 xd->tx_type_map = tx_type_map;
281 xd->tx_type_map_stride = mi_stride;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530282 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800283#if CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530284 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800285#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530286
287 // If segmentation in use
288 if (seg->enabled) {
289 // For in frame complexity AQ copy the segment id from the segment map.
290 if (cpi->oxcf.q_cfg.aq_mode == COMPLEXITY_AQ) {
291 const uint8_t *const map =
292 seg->update_map ? cpi->enc_seg.map : cm->last_frame_seg_map;
293 mi_addr->segment_id =
294 map ? get_segment_id(mi_params, map, bsize, mi_row, mi_col) : 0;
295 reset_tx_size(x, mi_addr, x->txfm_search_params.tx_mode_search_type);
296 }
297 // Else for cyclic refresh mode update the segment map, set the segment id
298 // and then update the quantizer.
leolzhaoaa4d7692021-01-28 11:00:33 -0800299#if CONFIG_SDP
300 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
301 xd->tree_type == SHARED_PART) {
302#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530303 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) {
leolzhaoaa4d7692021-01-28 11:00:33 -0800304#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530305 av1_cyclic_refresh_update_segment(cpi, mi_addr, mi_row, mi_col, bsize,
306 ctx->rd_stats.rate, ctx->rd_stats.dist,
307 txfm_info->skip_txfm);
308 }
309 if (mi_addr->uv_mode == UV_CFL_PRED && !is_cfl_allowed(xd))
310 mi_addr->uv_mode = UV_DC_PRED;
311 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800312#if CONFIG_SDP
313 for (i = (xd->tree_type == CHROMA_PART); i < num_planes; ++i) {
314#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530315 for (i = 0; i < num_planes; ++i) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800316#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530317 p[i].coeff = ctx->coeff[i];
318 p[i].qcoeff = ctx->qcoeff[i];
319 p[i].dqcoeff = ctx->dqcoeff[i];
320 p[i].eobs = ctx->eobs[i];
321 p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
322 }
323 for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i];
324 // Restore the coding context of the MB to that that was in place
325 // when the mode was picked for it
326 for (y = 0; y < mi_height; y++) {
327 for (x_idx = 0; x_idx < mi_width; x_idx++) {
328 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx &&
329 (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800330#if CONFIG_SDP
331 const int mi_idx =
332 get_alloc_mi_idx(mi_params, mi_row + y, mi_col + x_idx);
333 xd->mi[x_idx + y * mis] = &mi_params->mi_alloc[mi_idx];
334 if (xd->tree_type == LUMA_PART) {
335 *(xd->mi[x_idx + y * mis]) = *mi_addr;
336 } else if (xd->tree_type == CHROMA_PART) {
337 xd->mi[x_idx + y * mis]->sb_type[PLANE_TYPE_UV] =
338 mi_addr->sb_type[PLANE_TYPE_UV];
339 xd->mi[x_idx + y * mis]->uv_mode = mi_addr->uv_mode;
340 xd->mi[x_idx + y * mis]->angle_delta[PLANE_TYPE_UV] =
341 mi_addr->angle_delta[PLANE_TYPE_UV];
342 xd->mi[x_idx + y * mis]->cfl_alpha_signs = mi_addr->cfl_alpha_signs;
343 xd->mi[x_idx + y * mis]->cfl_alpha_idx = mi_addr->cfl_alpha_idx;
344 xd->mi[x_idx + y * mis]->partition = mi_addr->partition;
345 xd->mi[x_idx + y * mis]
346 ->palette_mode_info.palette_size[PLANE_TYPE_UV] =
347 mi_addr->palette_mode_info.palette_size[PLANE_TYPE_UV];
348 for (i = PALETTE_MAX_SIZE; i < 3 * PALETTE_MAX_SIZE; i++)
349 xd->mi[x_idx + y * mis]->palette_mode_info.palette_colors[i] =
350 mi_addr->palette_mode_info.palette_colors[i];
351 } else {
352 xd->mi[x_idx + y * mis] = mi_addr;
353 }
354#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530355 xd->mi[x_idx + y * mis] = mi_addr;
liang zhaoc6f775a2020-12-17 11:54:58 -0800356#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530357 }
358 }
359 }
360
361 if (cpi->oxcf.q_cfg.aq_mode)
362 av1_init_plane_quantizers(cpi, x, mi_addr->segment_id);
363
364 if (dry_run) return;
365
366#if CONFIG_INTERNAL_STATS
367 {
368 unsigned int *const mode_chosen_counts =
369 (unsigned int *)cpi->mode_chosen_counts; // Cast const away.
370 if (frame_is_intra_only(cm)) {
371 static const int kf_mode_index[] = {
372 THR_DC /*DC_PRED*/,
373 THR_V_PRED /*V_PRED*/,
374 THR_H_PRED /*H_PRED*/,
375 THR_D45_PRED /*D45_PRED*/,
376 THR_D135_PRED /*D135_PRED*/,
377 THR_D113_PRED /*D113_PRED*/,
378 THR_D157_PRED /*D157_PRED*/,
379 THR_D203_PRED /*D203_PRED*/,
380 THR_D67_PRED /*D67_PRED*/,
381 THR_SMOOTH, /*SMOOTH_PRED*/
382 THR_SMOOTH_V, /*SMOOTH_V_PRED*/
383 THR_SMOOTH_H, /*SMOOTH_H_PRED*/
384 THR_PAETH /*PAETH_PRED*/,
385 };
386 ++mode_chosen_counts[kf_mode_index[mi_addr->mode]];
387 } else {
388 // Note how often each mode chosen as best
389 ++mode_chosen_counts[ctx->best_mode_index];
390 }
391 }
392#endif
393 if (!frame_is_intra_only(cm)) {
leolzhao3ab59842021-05-11 10:07:48 -0700394#if CONFIG_SDP
395 if (is_inter_block(mi_addr, xd->tree_type)) {
396#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530397 if (is_inter_block(mi_addr)) {
leolzhao3ab59842021-05-11 10:07:48 -0700398#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530399 // TODO(sarahparker): global motion stats need to be handled per-tile
400 // to be compatible with tile-based threading.
401 update_global_motion_used(mi_addr->mode, bsize, mi_addr, rdc);
402 }
403
404 if (cm->features.interp_filter == SWITCHABLE &&
405 mi_addr->motion_mode != WARPED_CAUSAL &&
406 !is_nontrans_global_motion(xd, xd->mi[0])) {
407 update_filter_type_count(td->counts, xd, mi_addr);
408 }
409
410 rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
411 rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
412 rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
413 }
414
415 const int x_mis = AOMMIN(bw, mi_params->mi_cols - mi_col);
416 const int y_mis = AOMMIN(bh, mi_params->mi_rows - mi_row);
417 if (cm->seq_params.order_hint_info.enable_ref_frame_mvs)
418 av1_copy_frame_mvs(cm, mi, mi_row, mi_col, x_mis, y_mis);
419}
420
421void av1_update_inter_mode_stats(FRAME_CONTEXT *fc, FRAME_COUNTS *counts,
422 PREDICTION_MODE mode, int16_t mode_context) {
423 (void)counts;
Debargha Mukherjee0a45de82021-03-18 02:00:47 -0700424#if CONFIG_NEW_INTER_MODES
425 const int16_t ismode_ctx = inter_single_mode_ctx(mode_context);
426#if CONFIG_ENTROPY_STATS
427 ++counts->inter_single_mode[ismode_ctx][mode - SINGLE_INTER_MODE_START];
428#endif
429 update_cdf(fc->inter_single_mode_cdf[ismode_ctx],
430 mode - SINGLE_INTER_MODE_START, INTER_SINGLE_MODES);
431#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530432 int16_t mode_ctx = mode_context & NEWMV_CTX_MASK;
433 if (mode == NEWMV) {
434#if CONFIG_ENTROPY_STATS
435 ++counts->newmv_mode[mode_ctx][0];
436#endif
437 update_cdf(fc->newmv_cdf[mode_ctx], 0, 2);
438 return;
439 }
440
441#if CONFIG_ENTROPY_STATS
442 ++counts->newmv_mode[mode_ctx][1];
443#endif
444 update_cdf(fc->newmv_cdf[mode_ctx], 1, 2);
445
446 mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
447 if (mode == GLOBALMV) {
448#if CONFIG_ENTROPY_STATS
449 ++counts->zeromv_mode[mode_ctx][0];
450#endif
451 update_cdf(fc->zeromv_cdf[mode_ctx], 0, 2);
452 return;
453 }
454
455#if CONFIG_ENTROPY_STATS
456 ++counts->zeromv_mode[mode_ctx][1];
457#endif
458 update_cdf(fc->zeromv_cdf[mode_ctx], 1, 2);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530459 mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
460#if CONFIG_ENTROPY_STATS
461 ++counts->refmv_mode[mode_ctx][mode != NEARESTMV];
Elliott Karpilovsky1ad8f1d2021-02-01 22:27:50 -0800462#endif // CONFIG_ENTROPY_STATS
Jayasanker Je9ad4752020-06-30 19:30:03 +0530463 update_cdf(fc->refmv_cdf[mode_ctx], mode != NEARESTMV, 2);
Debargha Mukherjee0a45de82021-03-18 02:00:47 -0700464#endif // CONFIG_NEW_INTER_MODES
Jayasanker Je9ad4752020-06-30 19:30:03 +0530465}
466
467static void update_palette_cdf(MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
468 FRAME_COUNTS *counts) {
469 FRAME_CONTEXT *fc = xd->tile_ctx;
liang zhaoc6f775a2020-12-17 11:54:58 -0800470#if CONFIG_SDP
471 const BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART];
472#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530473 const BLOCK_SIZE bsize = mbmi->sb_type;
liang zhaoc6f775a2020-12-17 11:54:58 -0800474#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530475 const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
476 const int palette_bsize_ctx = av1_get_palette_bsize_ctx(bsize);
477
478 (void)counts;
liang zhaoc6f775a2020-12-17 11:54:58 -0800479#if CONFIG_SDP
480 if (mbmi->mode == DC_PRED && xd->tree_type != CHROMA_PART) {
481#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530482 if (mbmi->mode == DC_PRED) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800483#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530484 const int n = pmi->palette_size[0];
485 const int palette_mode_ctx = av1_get_palette_mode_ctx(xd);
486
487#if CONFIG_ENTROPY_STATS
488 ++counts->palette_y_mode[palette_bsize_ctx][palette_mode_ctx][n > 0];
489#endif
490 update_cdf(fc->palette_y_mode_cdf[palette_bsize_ctx][palette_mode_ctx],
491 n > 0, 2);
492 if (n > 0) {
493#if CONFIG_ENTROPY_STATS
494 ++counts->palette_y_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE];
495#endif
496 update_cdf(fc->palette_y_size_cdf[palette_bsize_ctx],
497 n - PALETTE_MIN_SIZE, PALETTE_SIZES);
498 }
499 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800500#if CONFIG_SDP
501 if (mbmi->uv_mode == UV_DC_PRED && xd->tree_type != LUMA_PART) {
502#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530503 if (mbmi->uv_mode == UV_DC_PRED) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800504#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530505 const int n = pmi->palette_size[1];
506 const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
507
508#if CONFIG_ENTROPY_STATS
509 ++counts->palette_uv_mode[palette_uv_mode_ctx][n > 0];
510#endif
511 update_cdf(fc->palette_uv_mode_cdf[palette_uv_mode_ctx], n > 0, 2);
512
513 if (n > 0) {
514#if CONFIG_ENTROPY_STATS
515 ++counts->palette_uv_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE];
516#endif
517 update_cdf(fc->palette_uv_size_cdf[palette_bsize_ctx],
518 n - PALETTE_MIN_SIZE, PALETTE_SIZES);
519 }
520 }
521}
522
523void av1_sum_intra_stats(const AV1_COMMON *const cm, FRAME_COUNTS *counts,
524 MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
525 const MB_MODE_INFO *above_mi,
526 const MB_MODE_INFO *left_mi, const int intraonly) {
527 FRAME_CONTEXT *fc = xd->tile_ctx;
528 const PREDICTION_MODE y_mode = mbmi->mode;
529 (void)counts;
liang zhaoc6f775a2020-12-17 11:54:58 -0800530#if CONFIG_SDP
531 const BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART];
532#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530533 const BLOCK_SIZE bsize = mbmi->sb_type;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530534#endif
liang zhaoc6f775a2020-12-17 11:54:58 -0800535#if CONFIG_SDP
536 if (xd->tree_type != CHROMA_PART) {
537#endif
538 if (intraonly) {
539#if CONFIG_ENTROPY_STATS
540 const PREDICTION_MODE above = av1_above_block_mode(above_mi);
541 const PREDICTION_MODE left = av1_left_block_mode(left_mi);
542 const int above_ctx = intra_mode_context[above];
543 const int left_ctx = intra_mode_context[left];
544 ++counts->kf_y_mode[above_ctx][left_ctx][y_mode];
545#endif // CONFIG_ENTROPY_STATS
546 update_cdf(get_y_mode_cdf(fc, above_mi, left_mi), y_mode, INTRA_MODES);
547 } else {
548#if CONFIG_ENTROPY_STATS
549 ++counts->y_mode[size_group_lookup[bsize]][y_mode];
550#endif // CONFIG_ENTROPY_STATS
551 update_cdf(fc->y_mode_cdf[size_group_lookup[bsize]], y_mode, INTRA_MODES);
552 }
leolzhao943a08d2021-03-30 15:32:52 -0700553#if CONFIG_MRLS
554 if (cm->seq_params.enable_mrls && av1_is_directional_mode(mbmi->mode)) {
555 update_cdf(fc->mrl_index_cdf, mbmi->mrl_index, MRL_LINE_NUMBER);
556 }
557#endif
liang zhaoc6f775a2020-12-17 11:54:58 -0800558 if (av1_filter_intra_allowed(cm, mbmi)) {
559 const int use_filter_intra_mode =
560 mbmi->filter_intra_mode_info.use_filter_intra;
561#if CONFIG_ENTROPY_STATS
562#if CONFIG_SDP
563 ++counts->filter_intra[mbmi->sb_type[xd->tree_type == CHROMA_PART]]
564 [use_filter_intra_mode];
565#else
566 ++counts->filter_intra[mbmi->sb_type][use_filter_intra_mode];
567#endif
568 if (use_filter_intra_mode) {
569 ++counts->filter_intra_mode[mbmi->filter_intra_mode_info
570 .filter_intra_mode];
571 }
572#endif // CONFIG_ENTROPY_STATS
573#if CONFIG_SDP
574 update_cdf(
575 fc->filter_intra_cdfs[mbmi->sb_type[xd->tree_type == CHROMA_PART]],
576 use_filter_intra_mode, 2);
577#else
578 update_cdf(fc->filter_intra_cdfs[mbmi->sb_type], use_filter_intra_mode, 2);
579#endif
580 if (use_filter_intra_mode) {
581 update_cdf(fc->filter_intra_mode_cdf,
582 mbmi->filter_intra_mode_info.filter_intra_mode,
583 FILTER_INTRA_MODES);
584 }
585 }
586 if (av1_is_directional_mode(mbmi->mode) && av1_use_angle_delta(bsize)) {
587#if CONFIG_ENTROPY_STATS
588 ++counts->angle_delta[mbmi->mode - V_PRED]
589 [mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA];
590#endif
SARWER, Mohammed Golam4241ffe2021-06-08 11:06:06 -0700591
592#if CONFIG_ORIP
593 int signal_intra_filter =
leolzhaob817d632021-06-15 18:22:05 -0700594 av1_signal_orip_for_horver_modes(cm, mbmi, PLANE_TYPE_Y, bsize);
SARWER, Mohammed Golam4241ffe2021-06-08 11:06:06 -0700595 if (signal_intra_filter) {
596#if CONFIG_SDP
597 update_cdf(fc->angle_delta_cdf_hv[PLANE_TYPE_Y][mbmi->mode - V_PRED],
598#else
599 update_cdf(fc->angle_delta_cdf_hv[mbmi->mode - V_PRED],
600#endif
601 get_angle_delta_to_idx(mbmi->angle_delta[PLANE_TYPE_Y]),
602 2 * MAX_ANGLE_DELTA + 1 + ADDITIONAL_ANGLE_DELTA);
603 } else {
604#endif
605#if CONFIG_SDP
606 update_cdf(fc->angle_delta_cdf[PLANE_TYPE_Y][mbmi->mode - V_PRED],
liang zhaoc6f775a2020-12-17 11:54:58 -0800607#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530608 update_cdf(fc->angle_delta_cdf[mbmi->mode - V_PRED],
SARWER, Mohammed Golam4241ffe2021-06-08 11:06:06 -0700609#endif
610 mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA,
611 2 * MAX_ANGLE_DELTA + 1);
612
613#if CONFIG_ORIP
614 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800615#endif
616 }
617#if CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530618 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800619#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530620
621 if (!xd->is_chroma_ref) return;
liang zhaoc6f775a2020-12-17 11:54:58 -0800622#if CONFIG_SDP
623 if (xd->tree_type != LUMA_PART) {
624#endif
625 const UV_PREDICTION_MODE uv_mode = mbmi->uv_mode;
626 const CFL_ALLOWED_TYPE cfl_allowed = is_cfl_allowed(xd);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530627#if CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800628 ++counts->uv_mode[cfl_allowed][y_mode][uv_mode];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530629#endif // CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800630 update_cdf(fc->uv_mode_cdf[cfl_allowed][y_mode], uv_mode,
631 UV_INTRA_MODES - !cfl_allowed);
632 if (uv_mode == UV_CFL_PRED) {
633 const int8_t joint_sign = mbmi->cfl_alpha_signs;
634 const uint8_t idx = mbmi->cfl_alpha_idx;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530635
636#if CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800637 ++counts->cfl_sign[joint_sign];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530638#endif
liang zhaoc6f775a2020-12-17 11:54:58 -0800639 update_cdf(fc->cfl_sign_cdf, joint_sign, CFL_JOINT_SIGNS);
640 if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
641 aom_cdf_prob *cdf_u = fc->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530642
643#if CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800644 ++counts->cfl_alpha[CFL_CONTEXT_U(joint_sign)][CFL_IDX_U(idx)];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530645#endif
liang zhaoc6f775a2020-12-17 11:54:58 -0800646 update_cdf(cdf_u, CFL_IDX_U(idx), CFL_ALPHABET_SIZE);
647 }
648 if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
649 aom_cdf_prob *cdf_v = fc->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
650
651#if CONFIG_ENTROPY_STATS
652 ++counts->cfl_alpha[CFL_CONTEXT_V(joint_sign)][CFL_IDX_V(idx)];
653#endif
654 update_cdf(cdf_v, CFL_IDX_V(idx), CFL_ALPHABET_SIZE);
655 }
Jayasanker Je9ad4752020-06-30 19:30:03 +0530656 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800657 if (av1_is_directional_mode(get_uv_mode(uv_mode)) &&
658 av1_use_angle_delta(bsize)) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530659#if CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800660 ++counts->angle_delta[uv_mode - UV_V_PRED]
661 [mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530662#endif
liang zhaoc6f775a2020-12-17 11:54:58 -0800663#if CONFIG_SDP
leolzhao02141602021-02-16 15:06:35 -0800664 if (cm->seq_params.enable_sdp)
665 update_cdf(fc->angle_delta_cdf[PLANE_TYPE_UV][uv_mode - UV_V_PRED],
666 mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA,
667 2 * MAX_ANGLE_DELTA + 1);
668 else
669 update_cdf(fc->angle_delta_cdf[PLANE_TYPE_Y][uv_mode - UV_V_PRED],
670 mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA,
671 2 * MAX_ANGLE_DELTA + 1);
liang zhaoc6f775a2020-12-17 11:54:58 -0800672#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530673 update_cdf(fc->angle_delta_cdf[uv_mode - UV_V_PRED],
674 mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA,
675 2 * MAX_ANGLE_DELTA + 1);
liang zhaoc6f775a2020-12-17 11:54:58 -0800676#endif
677 }
678#if CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530679 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800680#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530681 if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize)) {
682 update_palette_cdf(xd, mbmi, counts);
683 }
684}
685
686void av1_restore_context(MACROBLOCK *x, const RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
687 int mi_row, int mi_col, BLOCK_SIZE bsize,
688 const int num_planes) {
689 MACROBLOCKD *xd = &x->e_mbd;
690 int p;
691 const int num_4x4_blocks_wide = mi_size_wide[bsize];
692 const int num_4x4_blocks_high = mi_size_high[bsize];
693 int mi_width = mi_size_wide[bsize];
694 int mi_height = mi_size_high[bsize];
liang zhaoc6f775a2020-12-17 11:54:58 -0800695#if CONFIG_SDP
696 for (p = (xd->tree_type == CHROMA_PART); p < num_planes; p++) {
697#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530698 for (p = 0; p < num_planes; p++) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800699#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530700 int tx_col = mi_col;
701 int tx_row = mi_row & MAX_MIB_MASK;
702 memcpy(
703 xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x),
704 ctx->a + num_4x4_blocks_wide * p,
705 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
706 xd->plane[p].subsampling_x);
707 memcpy(xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y),
708 ctx->l + num_4x4_blocks_high * p,
709 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
710 xd->plane[p].subsampling_y);
liang zhaoc6f775a2020-12-17 11:54:58 -0800711#if CONFIG_SDP
712 memcpy(xd->above_partition_context[p] + mi_col, ctx->sa + mi_width * p,
713 sizeof(*xd->above_partition_context[p]) * mi_width);
714 memcpy(xd->left_partition_context[p] + (mi_row & MAX_MIB_MASK),
715 ctx->sl + mi_height * p,
716 sizeof(xd->left_partition_context[p][0]) * mi_height);
717#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530718 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800719#if !CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530720 memcpy(xd->above_partition_context + mi_col, ctx->sa,
721 sizeof(*xd->above_partition_context) * mi_width);
722 memcpy(xd->left_partition_context + (mi_row & MAX_MIB_MASK), ctx->sl,
723 sizeof(xd->left_partition_context[0]) * mi_height);
liang zhaoc6f775a2020-12-17 11:54:58 -0800724#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530725 xd->above_txfm_context = ctx->p_ta;
726 xd->left_txfm_context = ctx->p_tl;
727 memcpy(xd->above_txfm_context, ctx->ta,
728 sizeof(*xd->above_txfm_context) * mi_width);
729 memcpy(xd->left_txfm_context, ctx->tl,
730 sizeof(*xd->left_txfm_context) * mi_height);
731}
732
733void av1_save_context(const MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
734 int mi_row, int mi_col, BLOCK_SIZE bsize,
735 const int num_planes) {
736 const MACROBLOCKD *xd = &x->e_mbd;
737 int p;
738 int mi_width = mi_size_wide[bsize];
739 int mi_height = mi_size_high[bsize];
740
741 // buffer the above/left context information of the block in search.
liang zhaoc6f775a2020-12-17 11:54:58 -0800742#if CONFIG_SDP
743 for (p = (xd->tree_type == CHROMA_PART); p < num_planes; ++p) {
744#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530745 for (p = 0; p < num_planes; ++p) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800746#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530747 int tx_col = mi_col;
748 int tx_row = mi_row & MAX_MIB_MASK;
749 memcpy(
750 ctx->a + mi_width * p,
751 xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x),
752 (sizeof(ENTROPY_CONTEXT) * mi_width) >> xd->plane[p].subsampling_x);
753 memcpy(ctx->l + mi_height * p,
754 xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y),
755 (sizeof(ENTROPY_CONTEXT) * mi_height) >> xd->plane[p].subsampling_y);
liang zhaoc6f775a2020-12-17 11:54:58 -0800756#if CONFIG_SDP
757 memcpy(ctx->sa + mi_width * p, xd->above_partition_context[p] + mi_col,
758 sizeof(*xd->above_partition_context[p]) * mi_width);
759 memcpy(ctx->sl + mi_height * p,
760 xd->left_partition_context[p] + (mi_row & MAX_MIB_MASK),
761 sizeof(xd->left_partition_context[p][0]) * mi_height);
762#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530763 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800764#if !CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530765 memcpy(ctx->sa, xd->above_partition_context + mi_col,
766 sizeof(*xd->above_partition_context) * mi_width);
767 memcpy(ctx->sl, xd->left_partition_context + (mi_row & MAX_MIB_MASK),
768 sizeof(xd->left_partition_context[0]) * mi_height);
liang zhaoc6f775a2020-12-17 11:54:58 -0800769#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530770 memcpy(ctx->ta, xd->above_txfm_context,
771 sizeof(*xd->above_txfm_context) * mi_width);
772 memcpy(ctx->tl, xd->left_txfm_context,
773 sizeof(*xd->left_txfm_context) * mi_height);
774 ctx->p_ta = xd->above_txfm_context;
775 ctx->p_tl = xd->left_txfm_context;
776}
777
778static void set_partial_sb_partition(const AV1_COMMON *const cm,
779 MB_MODE_INFO *mi, int bh_in, int bw_in,
780 int mi_rows_remaining,
781 int mi_cols_remaining, BLOCK_SIZE bsize,
782 MB_MODE_INFO **mib) {
783 int bh = bh_in;
784 int r, c;
785 for (r = 0; r < cm->seq_params.mib_size; r += bh) {
786 int bw = bw_in;
787 for (c = 0; c < cm->seq_params.mib_size; c += bw) {
788 const int grid_index = get_mi_grid_idx(&cm->mi_params, r, c);
789 const int mi_index = get_alloc_mi_idx(&cm->mi_params, r, c);
790 mib[grid_index] = mi + mi_index;
liang zhaoc6f775a2020-12-17 11:54:58 -0800791#if CONFIG_SDP
leolzhaoaa4d7692021-01-28 11:00:33 -0800792 mib[grid_index]->sb_type[PLANE_TYPE_Y] =
793 mib[grid_index]->sb_type[PLANE_TYPE_UV] = find_partition_size(
794 bsize, mi_rows_remaining - r, mi_cols_remaining - c, &bh, &bw);
liang zhaoc6f775a2020-12-17 11:54:58 -0800795#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530796 mib[grid_index]->sb_type = find_partition_size(
797 bsize, mi_rows_remaining - r, mi_cols_remaining - c, &bh, &bw);
liang zhaoc6f775a2020-12-17 11:54:58 -0800798#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530799 }
800 }
801}
802
803// This function attempts to set all mode info entries in a given superblock
804// to the same block partition size.
805// However, at the bottom and right borders of the image the requested size
806// may not be allowed in which case this code attempts to choose the largest
807// allowable partition.
808void av1_set_fixed_partitioning(AV1_COMP *cpi, const TileInfo *const tile,
809 MB_MODE_INFO **mib, int mi_row, int mi_col,
810 BLOCK_SIZE bsize) {
811 AV1_COMMON *const cm = &cpi->common;
812 const CommonModeInfoParams *const mi_params = &cm->mi_params;
813 const int mi_rows_remaining = tile->mi_row_end - mi_row;
814 const int mi_cols_remaining = tile->mi_col_end - mi_col;
815 MB_MODE_INFO *const mi_upper_left =
816 mi_params->mi_alloc + get_alloc_mi_idx(mi_params, mi_row, mi_col);
817 int bh = mi_size_high[bsize];
818 int bw = mi_size_wide[bsize];
819
820 assert(bsize >= mi_params->mi_alloc_bsize &&
821 "Attempted to use bsize < mi_params->mi_alloc_bsize");
822 assert((mi_rows_remaining > 0) && (mi_cols_remaining > 0));
823
824 // Apply the requested partition size to the SB if it is all "in image"
825 if ((mi_cols_remaining >= cm->seq_params.mib_size) &&
826 (mi_rows_remaining >= cm->seq_params.mib_size)) {
827 for (int block_row = 0; block_row < cm->seq_params.mib_size;
828 block_row += bh) {
829 for (int block_col = 0; block_col < cm->seq_params.mib_size;
830 block_col += bw) {
831 const int grid_index = get_mi_grid_idx(mi_params, block_row, block_col);
832 const int mi_index = get_alloc_mi_idx(mi_params, block_row, block_col);
833 mib[grid_index] = mi_upper_left + mi_index;
liang zhaoc6f775a2020-12-17 11:54:58 -0800834#if CONFIG_SDP
leolzhaoaa4d7692021-01-28 11:00:33 -0800835 mib[grid_index]->sb_type[PLANE_TYPE_Y] = bsize;
836 mib[grid_index]->sb_type[PLANE_TYPE_UV] = bsize;
liang zhaoc6f775a2020-12-17 11:54:58 -0800837#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530838 mib[grid_index]->sb_type = bsize;
liang zhaoc6f775a2020-12-17 11:54:58 -0800839#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530840 }
841 }
842 } else {
843 // Else this is a partial SB.
844 set_partial_sb_partition(cm, mi_upper_left, bh, bw, mi_rows_remaining,
845 mi_cols_remaining, bsize, mib);
846 }
847}
leolzhao3db7cca2021-01-26 16:53:07 -0800848#if CONFIG_SDP
849int av1_is_leaf_split_partition(AV1_COMMON *cm, MACROBLOCKD *const xd,
850 int mi_row, int mi_col,
851#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530852int av1_is_leaf_split_partition(AV1_COMMON *cm, int mi_row, int mi_col,
leolzhao3db7cca2021-01-26 16:53:07 -0800853#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530854 BLOCK_SIZE bsize) {
855 const int bs = mi_size_wide[bsize];
856 const int hbs = bs / 2;
857 assert(bsize >= BLOCK_8X8);
858 const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
859
860 for (int i = 0; i < 4; i++) {
861 int x_idx = (i & 1) * hbs;
862 int y_idx = (i >> 1) * hbs;
863 if ((mi_row + y_idx >= cm->mi_params.mi_rows) ||
864 (mi_col + x_idx >= cm->mi_params.mi_cols))
865 return 0;
leolzhao3db7cca2021-01-26 16:53:07 -0800866#if CONFIG_SDP
867 if (get_partition(cm, xd->tree_type == CHROMA_PART, mi_row + y_idx,
868 mi_col + x_idx, subsize) !=
869#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530870 if (get_partition(cm, mi_row + y_idx, mi_col + x_idx, subsize) !=
leolzhao3db7cca2021-01-26 16:53:07 -0800871#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530872 PARTITION_NONE &&
873 subsize != BLOCK_8X8)
874 return 0;
875 }
876 return 1;
877}
878
Jayasanker J37596eb2020-08-20 16:39:40 +0530879int av1_get_rdmult_delta(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
880 int mi_col, int orig_rdmult) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530881 AV1_COMMON *const cm = &cpi->common;
882 const GF_GROUP *const gf_group = &cpi->gf_group;
883 assert(IMPLIES(cpi->gf_group.size > 0,
884 cpi->gf_group.index < cpi->gf_group.size));
885 const int tpl_idx = cpi->gf_group.index;
886 TplParams *const tpl_data = &cpi->tpl_data;
887 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
888 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
889 const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
890 int tpl_stride = tpl_frame->stride;
891 int64_t intra_cost = 0;
892 int64_t mc_dep_cost = 0;
893 const int mi_wide = mi_size_wide[bsize];
894 const int mi_high = mi_size_high[bsize];
895
896 if (tpl_frame->is_valid == 0) return orig_rdmult;
897
Deepa K G21e5e8e2020-03-28 13:26:09 +0530898 if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return orig_rdmult;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530899
900 if (cpi->gf_group.index >= MAX_TPL_FRAME_IDX) return orig_rdmult;
901
Jayasanker Je9ad4752020-06-30 19:30:03 +0530902 int mi_count = 0;
903 const int mi_col_sr =
904 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
905 const int mi_col_end_sr =
906 coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
907 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
908 const int step = 1 << block_mis_log2;
Urvang Joshie198bf12020-10-08 15:37:55 -0700909 const int row_step = step;
910 const int col_step_sr =
911 coded_to_superres_mi(step, cm->superres_scale_denominator);
912 for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
913 for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530914 if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
915 TplDepStats *this_stats =
916 &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
917 int64_t mc_dep_delta =
918 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
919 this_stats->mc_dep_dist);
920 intra_cost += this_stats->recrf_dist << RDDIV_BITS;
921 mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530922 mi_count++;
923 }
924 }
Urvang Joshie198bf12020-10-08 15:37:55 -0700925 assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530926
927 aom_clear_system_state();
928
929 double beta = 1.0;
Jayasanker J37596eb2020-08-20 16:39:40 +0530930 if (mc_dep_cost > 0 && intra_cost > 0) {
931 const double r0 = cpi->rd.r0;
932 const double rk = (double)intra_cost / mc_dep_cost;
933 beta = (r0 / rk);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530934 }
935
936 int rdmult = av1_get_adaptive_rdmult(cpi, beta);
937
938 aom_clear_system_state();
939
940 rdmult = AOMMIN(rdmult, orig_rdmult * 3 / 2);
941 rdmult = AOMMAX(rdmult, orig_rdmult * 1 / 2);
942
943 rdmult = AOMMAX(1, rdmult);
944
945 return rdmult;
946}
947
948// Checks to see if a super block is on a horizontal image edge.
949// In most cases this is the "real" edge unless there are formatting
950// bars embedded in the stream.
951int av1_active_h_edge(const AV1_COMP *cpi, int mi_row, int mi_step) {
952 int top_edge = 0;
953 int bottom_edge = cpi->common.mi_params.mi_rows;
954 int is_active_h_edge = 0;
955
Jayasanker Je9ad4752020-06-30 19:30:03 +0530956 if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) ||
957 ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) {
958 is_active_h_edge = 1;
959 }
960 return is_active_h_edge;
961}
962
963// Checks to see if a super block is on a vertical image edge.
964// In most cases this is the "real" edge unless there are formatting
965// bars embedded in the stream.
966int av1_active_v_edge(const AV1_COMP *cpi, int mi_col, int mi_step) {
967 int left_edge = 0;
968 int right_edge = cpi->common.mi_params.mi_cols;
969 int is_active_v_edge = 0;
970
Jayasanker Je9ad4752020-06-30 19:30:03 +0530971 if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) ||
972 ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) {
973 is_active_v_edge = 1;
974 }
975 return is_active_v_edge;
976}
977
978void av1_get_tpl_stats_sb(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
979 int mi_col, SuperBlockEnc *sb_enc) {
980 sb_enc->tpl_data_count = 0;
981
982 if (!cpi->oxcf.algo_cfg.enable_tpl_model) return;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530983 if (cpi->common.current_frame.frame_type == KEY_FRAME) return;
984 const FRAME_UPDATE_TYPE update_type = get_frame_update_type(&cpi->gf_group);
Debargha Mukherjee5f64acd2020-08-18 14:32:28 -0700985 if (update_type == INTNL_OVERLAY_UPDATE || update_type == OVERLAY_UPDATE ||
986 update_type == KFFLT_OVERLAY_UPDATE)
Jayasanker Je9ad4752020-06-30 19:30:03 +0530987 return;
988 assert(IMPLIES(cpi->gf_group.size > 0,
989 cpi->gf_group.index < cpi->gf_group.size));
990
991 AV1_COMMON *const cm = &cpi->common;
992 const int gf_group_index = cpi->gf_group.index;
993 TplParams *const tpl_data = &cpi->tpl_data;
994 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[gf_group_index];
995 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
996 int tpl_stride = tpl_frame->stride;
997 const int mi_wide = mi_size_wide[bsize];
998 const int mi_high = mi_size_high[bsize];
999
1000 if (tpl_frame->is_valid == 0) return;
1001 if (gf_group_index >= MAX_TPL_FRAME_IDX) return;
1002
1003 int mi_count = 0;
1004 int count = 0;
1005 const int mi_col_sr =
1006 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
1007 const int mi_col_end_sr =
1008 coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
1009 // mi_cols_sr is mi_cols at superres case.
1010 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
1011
1012 // TPL store unit size is not the same as the motion estimation unit size.
1013 // Here always use motion estimation size to avoid getting repetitive inter/
1014 // intra cost.
Yunqing Wangf0a8cf42020-08-14 14:50:33 -07001015 const BLOCK_SIZE tpl_bsize = convert_length_to_bsize(tpl_data->tpl_bsize_1d);
Jayasanker Je9ad4752020-06-30 19:30:03 +05301016 assert(mi_size_wide[tpl_bsize] == mi_size_high[tpl_bsize]);
Urvang Joshie198bf12020-10-08 15:37:55 -07001017 const int row_step = mi_size_high[tpl_bsize];
1018 const int col_step_sr = coded_to_superres_mi(mi_size_wide[tpl_bsize],
1019 cm->superres_scale_denominator);
Jayasanker Je9ad4752020-06-30 19:30:03 +05301020
1021 // Stride is only based on SB size, and we fill in values for every 16x16
1022 // block in a SB.
Urvang Joshie198bf12020-10-08 15:37:55 -07001023 sb_enc->tpl_stride = (mi_col_end_sr - mi_col_sr) / col_step_sr;
Jayasanker Je9ad4752020-06-30 19:30:03 +05301024
Urvang Joshie198bf12020-10-08 15:37:55 -07001025 for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
1026 for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
1027 assert(count < MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
Jayasanker Je9ad4752020-06-30 19:30:03 +05301028 // Handle partial SB, so that no invalid values are used later.
1029 if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) {
1030 sb_enc->tpl_inter_cost[count] = INT64_MAX;
1031 sb_enc->tpl_intra_cost[count] = INT64_MAX;
1032 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1033 sb_enc->tpl_mv[count][i].as_int = INVALID_MV;
1034 }
1035 count++;
1036 continue;
1037 }
1038
1039 TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
1040 row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
1041 sb_enc->tpl_inter_cost[count] = this_stats->inter_cost;
1042 sb_enc->tpl_intra_cost[count] = this_stats->intra_cost;
1043 memcpy(sb_enc->tpl_mv[count], this_stats->mv, sizeof(this_stats->mv));
1044 mi_count++;
1045 count++;
1046 }
1047 }
1048
Urvang Joshie198bf12020-10-08 15:37:55 -07001049 assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
Jayasanker Je9ad4752020-06-30 19:30:03 +05301050 sb_enc->tpl_data_count = mi_count;
1051}
1052
1053// analysis_type 0: Use mc_dep_cost and intra_cost
1054// analysis_type 1: Use count of best inter predictor chosen
1055// analysis_type 2: Use cost reduction from intra to inter for best inter
1056// predictor chosen
1057int av1_get_q_for_deltaq_objective(AV1_COMP *const cpi, BLOCK_SIZE bsize,
1058 int mi_row, int mi_col) {
1059 AV1_COMMON *const cm = &cpi->common;
1060 const GF_GROUP *const gf_group = &cpi->gf_group;
1061 assert(IMPLIES(cpi->gf_group.size > 0,
1062 cpi->gf_group.index < cpi->gf_group.size));
1063 const int tpl_idx = cpi->gf_group.index;
1064 TplParams *const tpl_data = &cpi->tpl_data;
1065 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
1066 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
1067 const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
1068 int tpl_stride = tpl_frame->stride;
1069 int64_t intra_cost = 0;
1070 int64_t mc_dep_cost = 0;
1071 const int mi_wide = mi_size_wide[bsize];
1072 const int mi_high = mi_size_high[bsize];
1073 const int base_qindex = cm->quant_params.base_qindex;
1074
1075 if (tpl_frame->is_valid == 0) return base_qindex;
1076
Deepa K G21e5e8e2020-03-28 13:26:09 +05301077 if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return base_qindex;
Jayasanker Je9ad4752020-06-30 19:30:03 +05301078
1079 if (cpi->gf_group.index >= MAX_TPL_FRAME_IDX) return base_qindex;
1080
Jayasanker Je9ad4752020-06-30 19:30:03 +05301081 int mi_count = 0;
1082 const int mi_col_sr =
1083 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
1084 const int mi_col_end_sr =
1085 coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
1086 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
1087 const int step = 1 << block_mis_log2;
Urvang Joshie198bf12020-10-08 15:37:55 -07001088 const int row_step = step;
1089 const int col_step_sr =
1090 coded_to_superres_mi(step, cm->superres_scale_denominator);
1091 for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
1092 for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
Jayasanker Je9ad4752020-06-30 19:30:03 +05301093 if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
1094 TplDepStats *this_stats =
1095 &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
1096 int64_t mc_dep_delta =
1097 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
1098 this_stats->mc_dep_dist);
1099 intra_cost += this_stats->recrf_dist << RDDIV_BITS;
1100 mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Jayasanker Je9ad4752020-06-30 19:30:03 +05301101 mi_count++;
1102 }
1103 }
Urvang Joshie198bf12020-10-08 15:37:55 -07001104 assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
Jayasanker Je9ad4752020-06-30 19:30:03 +05301105
1106 aom_clear_system_state();
1107
1108 int offset = 0;
1109 double beta = 1.0;
1110 if (mc_dep_cost > 0 && intra_cost > 0) {
1111 const double r0 = cpi->rd.r0;
1112 const double rk = (double)intra_cost / mc_dep_cost;
1113 beta = (r0 / rk);
1114 assert(beta > 0.0);
1115 }
1116 offset = av1_get_deltaq_offset(cpi, base_qindex, beta);
1117 aom_clear_system_state();
1118
1119 const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
1120 offset = AOMMIN(offset, delta_q_info->delta_q_res * 9 - 1);
1121 offset = AOMMAX(offset, -delta_q_info->delta_q_res * 9 + 1);
1122 int qindex = cm->quant_params.base_qindex + offset;
Ryan Leiccc6ea72021-01-06 11:43:56 -08001123#if CONFIG_EXTQUANT
Vibhoothi41c6dd72021-10-12 18:48:26 +00001124 qindex =
1125 AOMMIN(qindex, cm->seq_params.bit_depth == AOM_BITS_8 ? MAXQ_8_BITS
1126 : cm->seq_params.bit_depth == AOM_BITS_10 ? MAXQ_10_BITS
1127 : MAXQ);
Ryan Leiccc6ea72021-01-06 11:43:56 -08001128#else
Jayasanker Je9ad4752020-06-30 19:30:03 +05301129 qindex = AOMMIN(qindex, MAXQ);
Ryan Leiccc6ea72021-01-06 11:43:56 -08001130#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +05301131 qindex = AOMMAX(qindex, MINQ);
1132
1133 return qindex;
1134}
Jayasanker Je9ad4752020-06-30 19:30:03 +05301135
1136void av1_reset_simple_motion_tree_partition(SIMPLE_MOTION_DATA_TREE *sms_tree,
1137 BLOCK_SIZE bsize) {
1138 sms_tree->partitioning = PARTITION_NONE;
1139
1140 if (bsize >= BLOCK_8X8) {
1141 BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
1142 for (int idx = 0; idx < 4; ++idx)
1143 av1_reset_simple_motion_tree_partition(sms_tree->split[idx], subsize);
1144 }
1145}
1146
1147// Record the ref frames that have been selected by square partition blocks.
1148void av1_update_picked_ref_frames_mask(MACROBLOCK *const x, int ref_type,
1149 BLOCK_SIZE bsize, int mib_size,
1150 int mi_row, int mi_col) {
1151 assert(mi_size_wide[bsize] == mi_size_high[bsize]);
1152 const int sb_size_mask = mib_size - 1;
1153 const int mi_row_in_sb = mi_row & sb_size_mask;
1154 const int mi_col_in_sb = mi_col & sb_size_mask;
1155 const int mi_size = mi_size_wide[bsize];
1156 for (int i = mi_row_in_sb; i < mi_row_in_sb + mi_size; ++i) {
1157 for (int j = mi_col_in_sb; j < mi_col_in_sb + mi_size; ++j) {
1158 x->picked_ref_frames_mask[i * 32 + j] |= 1 << ref_type;
1159 }
1160 }
1161}
1162
1163static void avg_cdf_symbol(aom_cdf_prob *cdf_ptr_left, aom_cdf_prob *cdf_ptr_tr,
1164 int num_cdfs, int cdf_stride, int nsymbs,
1165 int wt_left, int wt_tr) {
1166 for (int i = 0; i < num_cdfs; i++) {
1167 for (int j = 0; j <= nsymbs; j++) {
1168 cdf_ptr_left[i * cdf_stride + j] =
1169 (aom_cdf_prob)(((int)cdf_ptr_left[i * cdf_stride + j] * wt_left +
1170 (int)cdf_ptr_tr[i * cdf_stride + j] * wt_tr +
1171 ((wt_left + wt_tr) / 2)) /
1172 (wt_left + wt_tr));
1173 assert(cdf_ptr_left[i * cdf_stride + j] >= 0 &&
1174 cdf_ptr_left[i * cdf_stride + j] < CDF_PROB_TOP);
1175 }
1176 }
1177}
1178
1179#define AVERAGE_CDF(cname_left, cname_tr, nsymbs) \
1180 AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, CDF_SIZE(nsymbs))
1181
1182#define AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, cdf_stride) \
1183 do { \
1184 aom_cdf_prob *cdf_ptr_left = (aom_cdf_prob *)cname_left; \
1185 aom_cdf_prob *cdf_ptr_tr = (aom_cdf_prob *)cname_tr; \
1186 int array_size = (int)sizeof(cname_left) / sizeof(aom_cdf_prob); \
1187 int num_cdfs = array_size / cdf_stride; \
1188 avg_cdf_symbol(cdf_ptr_left, cdf_ptr_tr, num_cdfs, cdf_stride, nsymbs, \
1189 wt_left, wt_tr); \
1190 } while (0)
1191
1192static void avg_nmv(nmv_context *nmv_left, nmv_context *nmv_tr, int wt_left,
1193 int wt_tr) {
1194 AVERAGE_CDF(nmv_left->joints_cdf, nmv_tr->joints_cdf, 4);
1195 for (int i = 0; i < 2; i++) {
1196 AVERAGE_CDF(nmv_left->comps[i].classes_cdf, nmv_tr->comps[i].classes_cdf,
1197 MV_CLASSES);
1198 AVERAGE_CDF(nmv_left->comps[i].class0_fp_cdf,
1199 nmv_tr->comps[i].class0_fp_cdf, MV_FP_SIZE);
1200 AVERAGE_CDF(nmv_left->comps[i].fp_cdf, nmv_tr->comps[i].fp_cdf, MV_FP_SIZE);
1201 AVERAGE_CDF(nmv_left->comps[i].sign_cdf, nmv_tr->comps[i].sign_cdf, 2);
1202 AVERAGE_CDF(nmv_left->comps[i].class0_hp_cdf,
1203 nmv_tr->comps[i].class0_hp_cdf, 2);
1204 AVERAGE_CDF(nmv_left->comps[i].hp_cdf, nmv_tr->comps[i].hp_cdf, 2);
1205 AVERAGE_CDF(nmv_left->comps[i].class0_cdf, nmv_tr->comps[i].class0_cdf,
1206 CLASS0_SIZE);
1207 AVERAGE_CDF(nmv_left->comps[i].bits_cdf, nmv_tr->comps[i].bits_cdf, 2);
1208 }
1209}
1210
1211// In case of row-based multi-threading of encoder, since we always
1212// keep a top - right sync, we can average the top - right SB's CDFs and
1213// the left SB's CDFs and use the same for current SB's encoding to
1214// improve the performance. This function facilitates the averaging
1215// of CDF and used only when row-mt is enabled in encoder.
1216void av1_avg_cdf_symbols(FRAME_CONTEXT *ctx_left, FRAME_CONTEXT *ctx_tr,
1217 int wt_left, int wt_tr) {
1218 AVERAGE_CDF(ctx_left->txb_skip_cdf, ctx_tr->txb_skip_cdf, 2);
1219 AVERAGE_CDF(ctx_left->eob_extra_cdf, ctx_tr->eob_extra_cdf, 2);
1220 AVERAGE_CDF(ctx_left->dc_sign_cdf, ctx_tr->dc_sign_cdf, 2);
1221 AVERAGE_CDF(ctx_left->eob_flag_cdf16, ctx_tr->eob_flag_cdf16, 5);
1222 AVERAGE_CDF(ctx_left->eob_flag_cdf32, ctx_tr->eob_flag_cdf32, 6);
1223 AVERAGE_CDF(ctx_left->eob_flag_cdf64, ctx_tr->eob_flag_cdf64, 7);
1224 AVERAGE_CDF(ctx_left->eob_flag_cdf128, ctx_tr->eob_flag_cdf128, 8);
1225 AVERAGE_CDF(ctx_left->eob_flag_cdf256, ctx_tr->eob_flag_cdf256, 9);
1226 AVERAGE_CDF(ctx_left->eob_flag_cdf512, ctx_tr->eob_flag_cdf512, 10);
1227 AVERAGE_CDF(ctx_left->eob_flag_cdf1024, ctx_tr->eob_flag_cdf1024, 11);
1228 AVERAGE_CDF(ctx_left->coeff_base_eob_cdf, ctx_tr->coeff_base_eob_cdf, 3);
1229 AVERAGE_CDF(ctx_left->coeff_base_cdf, ctx_tr->coeff_base_cdf, 4);
1230 AVERAGE_CDF(ctx_left->coeff_br_cdf, ctx_tr->coeff_br_cdf, BR_CDF_SIZE);
Elliott Karpilovsky1ad8f1d2021-02-01 22:27:50 -08001231#if CONFIG_NEW_INTER_MODES
Debargha Mukherjee0a45de82021-03-18 02:00:47 -07001232 AVERAGE_CDF(ctx_left->inter_single_mode_cdf, ctx_tr->inter_single_mode_cdf,
1233 INTER_SINGLE_MODES);
Elliott Karpilovsky1ad8f1d2021-02-01 22:27:50 -08001234 AVERAGE_CDF(ctx_left->drl_cdf[0], ctx_tr->drl_cdf[0], 2);
1235 AVERAGE_CDF(ctx_left->drl_cdf[1], ctx_tr->drl_cdf[1], 2);
1236 AVERAGE_CDF(ctx_left->drl_cdf[2], ctx_tr->drl_cdf[2], 2);
1237#else
Debargha Mukherjee0a45de82021-03-18 02:00:47 -07001238 AVERAGE_CDF(ctx_left->newmv_cdf, ctx_tr->newmv_cdf, 2);
1239 AVERAGE_CDF(ctx_left->zeromv_cdf, ctx_tr->zeromv_cdf, 2);
Jayasanker Je9ad4752020-06-30 19:30:03 +05301240 AVERAGE_CDF(ctx_left->refmv_cdf, ctx_tr->refmv_cdf, 2);
1241 AVERAGE_CDF(ctx_left->drl_cdf, ctx_tr->drl_cdf, 2);
Elliott Karpilovsky1ad8f1d2021-02-01 22:27:50 -08001242#endif // CONFIG_NEW_INTER_MODES
Lester Lu5fb73042021-10-01 00:08:55 +00001243#if CONFIG_OPTFLOW_REFINEMENT
1244 AVERAGE_CDF(ctx_left->inter_compound_mode_cdf,
1245 ctx_tr->inter_compound_mode_cdf, INTER_COMPOUND_REF_TYPES);
1246#else
Jayasanker Je9ad4752020-06-30 19:30:03 +05301247 AVERAGE_CDF(ctx_left->inter_compound_mode_cdf,
1248 ctx_tr->inter_compound_mode_cdf, INTER_COMPOUND_MODES);
Lester Lu5fb73042021-10-01 00:08:55 +00001249#endif // CONFIG_OPTFLOW_REFINEMENT
Jayasanker Je9ad4752020-06-30 19:30:03 +05301250 AVERAGE_CDF(ctx_left->compound_type_cdf, ctx_tr->compound_type_cdf,
1251 MASKED_COMPOUND_TYPES);
1252 AVERAGE_CDF(ctx_left->wedge_idx_cdf, ctx_tr->wedge_idx_cdf, 16);
1253 AVERAGE_CDF(ctx_left->interintra_cdf, ctx_tr->interintra_cdf, 2);
1254 AVERAGE_CDF(ctx_left->wedge_interintra_cdf, ctx_tr->wedge_interintra_cdf, 2);
1255 AVERAGE_CDF(ctx_left->interintra_mode_cdf, ctx_tr->interintra_mode_cdf,
1256 INTERINTRA_MODES);
1257 AVERAGE_CDF(ctx_left->motion_mode_cdf, ctx_tr->motion_mode_cdf, MOTION_MODES);
1258 AVERAGE_CDF(ctx_left->obmc_cdf, ctx_tr->obmc_cdf, 2);
1259 AVERAGE_CDF(ctx_left->palette_y_size_cdf, ctx_tr->palette_y_size_cdf,
1260 PALETTE_SIZES);
1261 AVERAGE_CDF(ctx_left->palette_uv_size_cdf, ctx_tr->palette_uv_size_cdf,
1262 PALETTE_SIZES);
1263 for (int j = 0; j < PALETTE_SIZES; j++) {
1264 int nsymbs = j + PALETTE_MIN_SIZE;
1265 AVG_CDF_STRIDE(ctx_left->palette_y_color_index_cdf[j],
1266 ctx_tr->palette_y_color_index_cdf[j], nsymbs,
1267 CDF_SIZE(PALETTE_COLORS));
1268 AVG_CDF_STRIDE(ctx_left->palette_uv_color_index_cdf[j],
1269 ctx_tr->palette_uv_color_index_cdf[j], nsymbs,
1270 CDF_SIZE(PALETTE_COLORS));
1271 }
1272 AVERAGE_CDF(ctx_left->palette_y_mode_cdf, ctx_tr->palette_y_mode_cdf, 2);
1273 AVERAGE_CDF(ctx_left->palette_uv_mode_cdf, ctx_tr->palette_uv_mode_cdf, 2);
1274 AVERAGE_CDF(ctx_left->comp_inter_cdf, ctx_tr->comp_inter_cdf, 2);
1275 AVERAGE_CDF(ctx_left->single_ref_cdf, ctx_tr->single_ref_cdf, 2);
1276 AVERAGE_CDF(ctx_left->comp_ref_type_cdf, ctx_tr->comp_ref_type_cdf, 2);
1277 AVERAGE_CDF(ctx_left->uni_comp_ref_cdf, ctx_tr->uni_comp_ref_cdf, 2);
1278 AVERAGE_CDF(ctx_left->comp_ref_cdf, ctx_tr->comp_ref_cdf, 2);
1279 AVERAGE_CDF(ctx_left->comp_bwdref_cdf, ctx_tr->comp_bwdref_cdf, 2);
Sarah Parker4247f722021-06-16 08:31:09 -07001280#if CONFIG_NEW_TX_PARTITION
1281 // Square blocks
1282 AVERAGE_CDF(ctx_left->inter_4way_txfm_partition_cdf[0],
1283 ctx_tr->inter_4way_txfm_partition_cdf[0], 4);
1284 // Rectangular blocks
1285 AVERAGE_CDF(ctx_left->inter_4way_txfm_partition_cdf[1],
1286 ctx_tr->inter_4way_txfm_partition_cdf[1], 4);
1287 AVERAGE_CDF(ctx_left->inter_2way_txfm_partition_cdf,
1288 ctx_tr->inter_2way_txfm_partition_cdf, 2);
1289 AVERAGE_CDF(ctx_left->inter_2way_rect_txfm_partition_cdf,
1290 ctx_tr->inter_2way_rect_txfm_partition_cdf, 2);
1291#else // CONFIG_NEW_TX_PARTITION
Jayasanker Je9ad4752020-06-30 19:30:03 +05301292 AVERAGE_CDF(ctx_left->txfm_partition_cdf, ctx_tr->txfm_partition_cdf, 2);
Sarah Parker4247f722021-06-16 08:31:09 -07001293#endif // CONFIG_NEW_TX_PARTITION
Debargha Mukherjee5bd41f92020-10-04 11:06:11 -07001294#if !CONFIG_REMOVE_DIST_WTD_COMP
Jayasanker Je9ad4752020-06-30 19:30:03 +05301295 AVERAGE_CDF(ctx_left->compound_index_cdf, ctx_tr->compound_index_cdf, 2);
Debargha Mukherjee5bd41f92020-10-04 11:06:11 -07001296#endif // !CONFIG_REMOVE_DIST_WTD_COMP
Jayasanker Je9ad4752020-06-30 19:30:03 +05301297 AVERAGE_CDF(ctx_left->comp_group_idx_cdf, ctx_tr->comp_group_idx_cdf, 2);
1298 AVERAGE_CDF(ctx_left->skip_mode_cdfs, ctx_tr->skip_mode_cdfs, 2);
1299 AVERAGE_CDF(ctx_left->skip_txfm_cdfs, ctx_tr->skip_txfm_cdfs, 2);
1300 AVERAGE_CDF(ctx_left->intra_inter_cdf, ctx_tr->intra_inter_cdf, 2);
1301 avg_nmv(&ctx_left->nmvc, &ctx_tr->nmvc, wt_left, wt_tr);
1302 avg_nmv(&ctx_left->ndvc, &ctx_tr->ndvc, wt_left, wt_tr);
1303 AVERAGE_CDF(ctx_left->intrabc_cdf, ctx_tr->intrabc_cdf, 2);
1304 AVERAGE_CDF(ctx_left->seg.tree_cdf, ctx_tr->seg.tree_cdf, MAX_SEGMENTS);
1305 AVERAGE_CDF(ctx_left->seg.pred_cdf, ctx_tr->seg.pred_cdf, 2);
1306 AVERAGE_CDF(ctx_left->seg.spatial_pred_seg_cdf,
1307 ctx_tr->seg.spatial_pred_seg_cdf, MAX_SEGMENTS);
1308 AVERAGE_CDF(ctx_left->filter_intra_cdfs, ctx_tr->filter_intra_cdfs, 2);
1309 AVERAGE_CDF(ctx_left->filter_intra_mode_cdf, ctx_tr->filter_intra_mode_cdf,
1310 FILTER_INTRA_MODES);
1311 AVERAGE_CDF(ctx_left->switchable_restore_cdf, ctx_tr->switchable_restore_cdf,
1312 RESTORE_SWITCHABLE_TYPES);
1313 AVERAGE_CDF(ctx_left->wiener_restore_cdf, ctx_tr->wiener_restore_cdf, 2);
1314 AVERAGE_CDF(ctx_left->sgrproj_restore_cdf, ctx_tr->sgrproj_restore_cdf, 2);
leolzhao943a08d2021-03-30 15:32:52 -07001315#if CONFIG_MRLS
1316 AVERAGE_CDF(ctx_left->mrl_index_cdf, ctx_tr->mrl_index_cdf, MRL_LINE_NUMBER);
1317#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +05301318 AVERAGE_CDF(ctx_left->y_mode_cdf, ctx_tr->y_mode_cdf, INTRA_MODES);
1319 AVG_CDF_STRIDE(ctx_left->uv_mode_cdf[0], ctx_tr->uv_mode_cdf[0],
1320 UV_INTRA_MODES - 1, CDF_SIZE(UV_INTRA_MODES));
1321 AVERAGE_CDF(ctx_left->uv_mode_cdf[1], ctx_tr->uv_mode_cdf[1], UV_INTRA_MODES);
liang zhaoc6f775a2020-12-17 11:54:58 -08001322#if CONFIG_SDP
1323 for (int plane_index = 0; plane_index < PARTITION_STRUCTURE_NUM;
1324 plane_index++) {
1325 for (int i = 0; i < PARTITION_CONTEXTS; i++) {
1326 if (i < 4) {
1327 AVG_CDF_STRIDE(ctx_left->partition_cdf[plane_index][i],
1328 ctx_tr->partition_cdf[plane_index][i], 4, CDF_SIZE(10));
1329 } else if (i < 16) {
1330 AVERAGE_CDF(ctx_left->partition_cdf[plane_index][i],
1331 ctx_tr->partition_cdf[plane_index][i], 10);
1332 } else {
1333 AVG_CDF_STRIDE(ctx_left->partition_cdf[plane_index][i],
1334 ctx_tr->partition_cdf[plane_index][i], 8, CDF_SIZE(10));
1335 }
1336 }
1337 }
1338#else
Jayasanker Je9ad4752020-06-30 19:30:03 +05301339 for (int i = 0; i < PARTITION_CONTEXTS; i++) {
1340 if (i < 4) {
1341 AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 4,
1342 CDF_SIZE(10));
1343 } else if (i < 16) {
1344 AVERAGE_CDF(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 10);
1345 } else {
1346 AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 8,
1347 CDF_SIZE(10));
1348 }
1349 }
liang zhaoc6f775a2020-12-17 11:54:58 -08001350#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +05301351 AVERAGE_CDF(ctx_left->switchable_interp_cdf, ctx_tr->switchable_interp_cdf,
1352 SWITCHABLE_FILTERS);
1353 AVERAGE_CDF(ctx_left->kf_y_cdf, ctx_tr->kf_y_cdf, INTRA_MODES);
1354 AVERAGE_CDF(ctx_left->angle_delta_cdf, ctx_tr->angle_delta_cdf,
1355 2 * MAX_ANGLE_DELTA + 1);
SARWER, Mohammed Golam4241ffe2021-06-08 11:06:06 -07001356#if CONFIG_ORIP
1357 AVERAGE_CDF(ctx_left->angle_delta_cdf_hv, ctx_tr->angle_delta_cdf_hv,
1358 2 * MAX_ANGLE_DELTA + 1 + ADDITIONAL_ANGLE_DELTA);
1359#endif
Sarah Parker4247f722021-06-16 08:31:09 -07001360#if CONFIG_NEW_TX_PARTITION
1361 // Square blocks
1362 AVERAGE_CDF(ctx_left->intra_4way_txfm_partition_cdf[0],
1363 ctx_tr->intra_4way_txfm_partition_cdf[0], 4);
1364 // Rectangular blocks
1365 AVERAGE_CDF(ctx_left->intra_4way_txfm_partition_cdf[1],
1366 ctx_tr->intra_4way_txfm_partition_cdf[1], 4);
1367 AVERAGE_CDF(ctx_left->intra_2way_txfm_partition_cdf,
1368 ctx_tr->intra_2way_txfm_partition_cdf, 2);
1369 AVERAGE_CDF(ctx_left->intra_2way_rect_txfm_partition_cdf,
1370 ctx_tr->intra_2way_rect_txfm_partition_cdf, 2);
1371#else
Jayasanker Je9ad4752020-06-30 19:30:03 +05301372 AVG_CDF_STRIDE(ctx_left->tx_size_cdf[0], ctx_tr->tx_size_cdf[0], MAX_TX_DEPTH,
1373 CDF_SIZE(MAX_TX_DEPTH + 1));
1374 AVERAGE_CDF(ctx_left->tx_size_cdf[1], ctx_tr->tx_size_cdf[1],
1375 MAX_TX_DEPTH + 1);
1376 AVERAGE_CDF(ctx_left->tx_size_cdf[2], ctx_tr->tx_size_cdf[2],
1377 MAX_TX_DEPTH + 1);
1378 AVERAGE_CDF(ctx_left->tx_size_cdf[3], ctx_tr->tx_size_cdf[3],
1379 MAX_TX_DEPTH + 1);
Sarah Parker4247f722021-06-16 08:31:09 -07001380#endif // CONFIG_NEW_TX_PARTITION
Jayasanker Je9ad4752020-06-30 19:30:03 +05301381 AVERAGE_CDF(ctx_left->delta_q_cdf, ctx_tr->delta_q_cdf, DELTA_Q_PROBS + 1);
1382 AVERAGE_CDF(ctx_left->delta_lf_cdf, ctx_tr->delta_lf_cdf, DELTA_LF_PROBS + 1);
1383 for (int i = 0; i < FRAME_LF_COUNT; i++) {
1384 AVERAGE_CDF(ctx_left->delta_lf_multi_cdf[i], ctx_tr->delta_lf_multi_cdf[i],
1385 DELTA_LF_PROBS + 1);
1386 }
1387 AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[1], ctx_tr->intra_ext_tx_cdf[1], 7,
1388 CDF_SIZE(TX_TYPES));
1389 AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[2], ctx_tr->intra_ext_tx_cdf[2], 5,
1390 CDF_SIZE(TX_TYPES));
1391 AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[1], ctx_tr->inter_ext_tx_cdf[1], 16,
1392 CDF_SIZE(TX_TYPES));
1393 AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[2], ctx_tr->inter_ext_tx_cdf[2], 12,
1394 CDF_SIZE(TX_TYPES));
1395 AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[3], ctx_tr->inter_ext_tx_cdf[3], 2,
1396 CDF_SIZE(TX_TYPES));
1397 AVERAGE_CDF(ctx_left->cfl_sign_cdf, ctx_tr->cfl_sign_cdf, CFL_JOINT_SIGNS);
1398 AVERAGE_CDF(ctx_left->cfl_alpha_cdf, ctx_tr->cfl_alpha_cdf,
1399 CFL_ALPHABET_SIZE);
Madhu Peringassery Krishnanee0d2f02021-06-14 12:08:32 -07001400#if CONFIG_IST
1401 AVG_CDF_STRIDE(ctx_left->stx_cdf, ctx_tr->stx_cdf, STX_TYPES,
1402 CDF_SIZE(STX_TYPES));
1403#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +05301404}
1405
Jayasanker Je9ad4752020-06-30 19:30:03 +05301406// Memset the mbmis at the current superblock to 0
1407void av1_reset_mbmi(CommonModeInfoParams *const mi_params, BLOCK_SIZE sb_size,
1408 int mi_row, int mi_col) {
1409 // size of sb in unit of mi (BLOCK_4X4)
1410 const int sb_size_mi = mi_size_wide[sb_size];
1411 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
1412 // size of sb in unit of allocated mi size
1413 const int sb_size_alloc_mi = mi_size_wide[sb_size] / mi_alloc_size_1d;
1414 assert(mi_params->mi_alloc_stride % sb_size_alloc_mi == 0 &&
1415 "mi is not allocated as a multiple of sb!");
1416 assert(mi_params->mi_stride % sb_size_mi == 0 &&
1417 "mi_grid_base is not allocated as a multiple of sb!");
1418
1419 const int mi_rows = mi_size_high[sb_size];
1420 for (int cur_mi_row = 0; cur_mi_row < mi_rows; cur_mi_row++) {
1421 assert(get_mi_grid_idx(mi_params, 0, mi_col + mi_alloc_size_1d) <
1422 mi_params->mi_stride);
1423 const int mi_grid_idx =
1424 get_mi_grid_idx(mi_params, mi_row + cur_mi_row, mi_col);
1425 const int alloc_mi_idx =
1426 get_alloc_mi_idx(mi_params, mi_row + cur_mi_row, mi_col);
1427 memset(&mi_params->mi_grid_base[mi_grid_idx], 0,
1428 sb_size_mi * sizeof(*mi_params->mi_grid_base));
1429 memset(&mi_params->tx_type_map[mi_grid_idx], 0,
1430 sb_size_mi * sizeof(*mi_params->tx_type_map));
1431 if (cur_mi_row % mi_alloc_size_1d == 0) {
1432 memset(&mi_params->mi_alloc[alloc_mi_idx], 0,
1433 sb_size_alloc_mi * sizeof(*mi_params->mi_alloc));
1434 }
1435 }
1436}
1437
1438void av1_backup_sb_state(SB_FIRST_PASS_STATS *sb_fp_stats, const AV1_COMP *cpi,
1439 ThreadData *td, const TileDataEnc *tile_data,
1440 int mi_row, int mi_col) {
1441 MACROBLOCK *x = &td->mb;
1442 MACROBLOCKD *xd = &x->e_mbd;
1443 const TileInfo *tile_info = &tile_data->tile_info;
1444
1445 const AV1_COMMON *cm = &cpi->common;
1446 const int num_planes = av1_num_planes(cm);
1447 const BLOCK_SIZE sb_size = cm->seq_params.sb_size;
1448
1449 xd->above_txfm_context =
1450 cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
1451 xd->left_txfm_context =
1452 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
1453 av1_save_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size, num_planes);
1454
1455 sb_fp_stats->rd_count = cpi->td.rd_counts;
1456 sb_fp_stats->split_count = x->txfm_search_info.txb_split_count;
1457
1458 sb_fp_stats->fc = *td->counts;
1459
1460 memcpy(sb_fp_stats->inter_mode_rd_models, tile_data->inter_mode_rd_models,
1461 sizeof(sb_fp_stats->inter_mode_rd_models));
1462
1463 memcpy(sb_fp_stats->thresh_freq_fact, x->thresh_freq_fact,
1464 sizeof(sb_fp_stats->thresh_freq_fact));
1465
1466 const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
1467 sb_fp_stats->current_qindex =
1468 cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex;
1469
1470#if CONFIG_INTERNAL_STATS
1471 memcpy(sb_fp_stats->mode_chosen_counts, cpi->mode_chosen_counts,
1472 sizeof(sb_fp_stats->mode_chosen_counts));
1473#endif // CONFIG_INTERNAL_STATS
1474}
1475
1476void av1_restore_sb_state(const SB_FIRST_PASS_STATS *sb_fp_stats, AV1_COMP *cpi,
1477 ThreadData *td, TileDataEnc *tile_data, int mi_row,
1478 int mi_col) {
1479 MACROBLOCK *x = &td->mb;
1480
1481 const AV1_COMMON *cm = &cpi->common;
1482 const int num_planes = av1_num_planes(cm);
1483 const BLOCK_SIZE sb_size = cm->seq_params.sb_size;
1484
1485 av1_restore_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size,
1486 num_planes);
1487
1488 cpi->td.rd_counts = sb_fp_stats->rd_count;
1489 x->txfm_search_info.txb_split_count = sb_fp_stats->split_count;
1490
1491 *td->counts = sb_fp_stats->fc;
1492
1493 memcpy(tile_data->inter_mode_rd_models, sb_fp_stats->inter_mode_rd_models,
1494 sizeof(sb_fp_stats->inter_mode_rd_models));
1495 memcpy(x->thresh_freq_fact, sb_fp_stats->thresh_freq_fact,
1496 sizeof(sb_fp_stats->thresh_freq_fact));
1497
1498 const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
1499 cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex =
1500 sb_fp_stats->current_qindex;
1501
1502#if CONFIG_INTERNAL_STATS
1503 memcpy(cpi->mode_chosen_counts, sb_fp_stats->mode_chosen_counts,
1504 sizeof(sb_fp_stats->mode_chosen_counts));
1505#endif // CONFIG_INTERNAL_STATS
1506}
1507
1508// Update the rate costs of some symbols according to the frequency directed
1509// by speed features
1510void av1_set_cost_upd_freq(AV1_COMP *cpi, ThreadData *td,
1511 const TileInfo *const tile_info, const int mi_row,
1512 const int mi_col) {
1513 AV1_COMMON *const cm = &cpi->common;
1514 const int num_planes = av1_num_planes(cm);
1515 MACROBLOCK *const x = &td->mb;
1516 MACROBLOCKD *const xd = &x->e_mbd;
1517
1518 switch (cpi->oxcf.cost_upd_freq.coeff) {
1519 case COST_UPD_TILE: // Tile level
1520 if (mi_row != tile_info->mi_row_start) break;
1521 AOM_FALLTHROUGH_INTENDED;
1522 case COST_UPD_SBROW: // SB row level in tile
1523 if (mi_col != tile_info->mi_col_start) break;
1524 AOM_FALLTHROUGH_INTENDED;
1525 case COST_UPD_SB: // SB level
1526 if (cpi->sf.inter_sf.disable_sb_level_coeff_cost_upd &&
1527 mi_col != tile_info->mi_col_start)
1528 break;
1529 av1_fill_coeff_costs(&x->coeff_costs, xd->tile_ctx, num_planes);
1530 break;
1531 default: assert(0);
1532 }
1533
1534 switch (cpi->oxcf.cost_upd_freq.mode) {
1535 case COST_UPD_TILE: // Tile level
1536 if (mi_row != tile_info->mi_row_start) break;
1537 AOM_FALLTHROUGH_INTENDED;
1538 case COST_UPD_SBROW: // SB row level in tile
1539 if (mi_col != tile_info->mi_col_start) break;
1540 AOM_FALLTHROUGH_INTENDED;
1541 case COST_UPD_SB: // SB level
leolzhao3db7cca2021-01-26 16:53:07 -08001542#if CONFIG_SDP
1543 av1_fill_mode_rates(cm, xd, &x->mode_costs, xd->tile_ctx);
1544#else
Jayasanker Je9ad4752020-06-30 19:30:03 +05301545 av1_fill_mode_rates(cm, &x->mode_costs, xd->tile_ctx);
leolzhao3db7cca2021-01-26 16:53:07 -08001546#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +05301547 break;
1548 default: assert(0);
1549 }
1550 switch (cpi->oxcf.cost_upd_freq.mv) {
1551 case COST_UPD_OFF: break;
1552 case COST_UPD_TILE: // Tile level
1553 if (mi_row != tile_info->mi_row_start) break;
1554 AOM_FALLTHROUGH_INTENDED;
1555 case COST_UPD_SBROW: // SB row level in tile
1556 if (mi_col != tile_info->mi_col_start) break;
1557 AOM_FALLTHROUGH_INTENDED;
1558 case COST_UPD_SB: // SB level
1559 if (cpi->sf.inter_sf.disable_sb_level_mv_cost_upd &&
1560 mi_col != tile_info->mi_col_start)
1561 break;
1562 av1_fill_mv_costs(xd->tile_ctx, cm->features.cur_frame_force_integer_mv,
1563 cm->features.allow_high_precision_mv, &x->mv_costs);
1564 break;
1565 default: assert(0);
1566 }
1567}