blob: c0fdc016f4ef647fdf26eeaf341c847abc20e05e [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
Urvang Joshie198bf12020-10-08 15:37:55 -070065// Return the end column for the current superblock, in unit of TPL blocks.
66static int get_superblock_tpl_column_end(const AV1_COMMON *const cm, int mi_col,
67 int num_mi_w) {
68 // Find the start column of this superblock.
69 const int sb_mi_col_start = (mi_col >> cm->seq_params.mib_size_log2)
70 << cm->seq_params.mib_size_log2;
71 // Same but in superres upscaled dimension.
72 const int sb_mi_col_start_sr =
73 coded_to_superres_mi(sb_mi_col_start, cm->superres_scale_denominator);
74 // Width of this superblock in mi units.
75 const int sb_mi_width = mi_size_wide[cm->seq_params.sb_size];
76 // Same but in superres upscaled dimension.
77 const int sb_mi_width_sr =
78 coded_to_superres_mi(sb_mi_width, cm->superres_scale_denominator);
79 // Superblock end in mi units.
80 const int sb_mi_end = sb_mi_col_start_sr + sb_mi_width_sr;
81 // Superblock end in TPL units.
82 return (sb_mi_end + num_mi_w - 1) / num_mi_w;
83}
84
Jayasanker Je9ad4752020-06-30 19:30:03 +053085int av1_get_hier_tpl_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x,
86 const BLOCK_SIZE bsize, const int mi_row,
87 const int mi_col, int orig_rdmult) {
88 const AV1_COMMON *const cm = &cpi->common;
89 const GF_GROUP *const gf_group = &cpi->gf_group;
90 assert(IMPLIES(cpi->gf_group.size > 0,
91 cpi->gf_group.index < cpi->gf_group.size));
92 const int tpl_idx = cpi->gf_group.index;
93 const TplDepFrame *tpl_frame = &cpi->tpl_data.tpl_frame[tpl_idx];
94 const int deltaq_rdmult = set_deltaq_rdmult(cpi, x);
95 if (tpl_frame->is_valid == 0) return deltaq_rdmult;
Deepa K G21e5e8e2020-03-28 13:26:09 +053096 if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return deltaq_rdmult;
Jayasanker Je9ad4752020-06-30 19:30:03 +053097 if (tpl_idx >= MAX_TPL_FRAME_IDX) return deltaq_rdmult;
Jayasanker Je9ad4752020-06-30 19:30:03 +053098 if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return deltaq_rdmult;
99
Urvang Joshie198bf12020-10-08 15:37:55 -0700100 const int mi_col_sr =
101 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
102 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
103 const int block_mi_width_sr =
104 coded_to_superres_mi(mi_size_wide[bsize], cm->superres_scale_denominator);
105
Jayasanker Je9ad4752020-06-30 19:30:03 +0530106 const int bsize_base = BLOCK_16X16;
107 const int num_mi_w = mi_size_wide[bsize_base];
108 const int num_mi_h = mi_size_high[bsize_base];
Urvang Joshie198bf12020-10-08 15:37:55 -0700109 const int num_cols = (mi_cols_sr + num_mi_w - 1) / num_mi_w;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530110 const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
Urvang Joshie198bf12020-10-08 15:37:55 -0700111 const int num_bcols = (block_mi_width_sr + num_mi_w - 1) / num_mi_w;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530112 const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h;
Urvang Joshie198bf12020-10-08 15:37:55 -0700113 // This is required because the end col of superblock may be off by 1 in case
114 // of superres.
115 const int sb_bcol_end = get_superblock_tpl_column_end(cm, mi_col, num_mi_w);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530116 int row, col;
117 double base_block_count = 0.0;
118 double geom_mean_of_scale = 0.0;
119 aom_clear_system_state();
120 for (row = mi_row / num_mi_w;
121 row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
Urvang Joshie198bf12020-10-08 15:37:55 -0700122 for (col = mi_col_sr / num_mi_h;
123 col < num_cols && col < mi_col_sr / num_mi_h + num_bcols &&
124 col < sb_bcol_end;
125 ++col) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530126 const int index = row * num_cols + col;
127 geom_mean_of_scale += log(cpi->tpl_sb_rdmult_scaling_factors[index]);
128 base_block_count += 1.0;
129 }
130 }
131 geom_mean_of_scale = exp(geom_mean_of_scale / base_block_count);
132 int rdmult = (int)((double)orig_rdmult * geom_mean_of_scale + 0.5);
133 rdmult = AOMMAX(rdmult, 0);
134 av1_set_error_per_bit(&x->mv_costs, rdmult);
135 aom_clear_system_state();
136 if (bsize == cm->seq_params.sb_size) {
137 const int rdmult_sb = set_deltaq_rdmult(cpi, x);
138 assert(rdmult_sb == rdmult);
139 (void)rdmult_sb;
140 }
141 return rdmult;
142}
143
144static AOM_INLINE void update_filter_type_count(FRAME_COUNTS *counts,
145 const MACROBLOCKD *xd,
146 const MB_MODE_INFO *mbmi) {
Hui Su93c395b2020-10-05 12:00:20 -0700147#if CONFIG_REMOVE_DUAL_FILTER
148 const int ctx = av1_get_pred_context_switchable_interp(xd, 0);
149 ++counts->switchable_interp[ctx][mbmi->interp_fltr];
150#else
151 for (int dir = 0; dir < 2; ++dir) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530152 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
153 InterpFilter filter = av1_extract_interp_filter(mbmi->interp_filters, dir);
154 ++counts->switchable_interp[ctx][filter];
155 }
Hui Su93c395b2020-10-05 12:00:20 -0700156#endif // CONFIG_REMOVE_DUAL_FILTER
Jayasanker Je9ad4752020-06-30 19:30:03 +0530157}
158
159static void reset_tx_size(MACROBLOCK *x, MB_MODE_INFO *mbmi,
160 const TX_MODE tx_mode) {
161 MACROBLOCKD *const xd = &x->e_mbd;
162 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
liang zhaoc6f775a2020-12-17 11:54:58 -0800163#if CONFIG_SDP
164 int plane_index = xd->tree_type == CHROMA_PART;
165#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530166 if (xd->lossless[mbmi->segment_id]) {
167 mbmi->tx_size = TX_4X4;
168 } else if (tx_mode != TX_MODE_SELECT) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800169#if CONFIG_SDP
170 mbmi->tx_size = tx_size_from_tx_mode(mbmi->sb_type[plane_index], tx_mode);
171#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530172 mbmi->tx_size = tx_size_from_tx_mode(mbmi->sb_type, tx_mode);
liang zhaoc6f775a2020-12-17 11:54:58 -0800173#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530174 } else {
liang zhaoc6f775a2020-12-17 11:54:58 -0800175#if CONFIG_SDP
176 BLOCK_SIZE bsize = mbmi->sb_type[plane_index];
177#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530178 BLOCK_SIZE bsize = mbmi->sb_type;
liang zhaoc6f775a2020-12-17 11:54:58 -0800179#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530180 TX_SIZE min_tx_size = depth_to_tx_size(MAX_TX_DEPTH, bsize);
181 mbmi->tx_size = (TX_SIZE)TXSIZEMAX(mbmi->tx_size, min_tx_size);
182 }
183 if (is_inter_block(mbmi)) {
184 memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size));
185 }
186 const int stride = xd->tx_type_map_stride;
liang zhaoc6f775a2020-12-17 11:54:58 -0800187#if CONFIG_SDP
188 const int bw = mi_size_wide[mbmi->sb_type[plane_index]];
189 for (int row = 0; row < mi_size_high[mbmi->sb_type[plane_index]]; ++row) {
190#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530191 const int bw = mi_size_wide[mbmi->sb_type];
192 for (int row = 0; row < mi_size_high[mbmi->sb_type]; ++row) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800193#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530194 memset(xd->tx_type_map + row * stride, DCT_DCT,
195 bw * sizeof(xd->tx_type_map[0]));
196 }
197 av1_zero(txfm_info->blk_skip);
198 txfm_info->skip_txfm = 0;
199}
200
201// This function will copy the best reference mode information from
202// MB_MODE_INFO_EXT_FRAME to MB_MODE_INFO_EXT.
203static INLINE void copy_mbmi_ext_frame_to_mbmi_ext(
204 MB_MODE_INFO_EXT *mbmi_ext,
205 const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_best, uint8_t ref_frame_type) {
206 memcpy(mbmi_ext->ref_mv_stack[ref_frame_type], mbmi_ext_best->ref_mv_stack,
207 sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE]));
208 memcpy(mbmi_ext->weight[ref_frame_type], mbmi_ext_best->weight,
209 sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE]));
210 mbmi_ext->mode_context[ref_frame_type] = mbmi_ext_best->mode_context;
211 mbmi_ext->ref_mv_count[ref_frame_type] = mbmi_ext_best->ref_mv_count;
212 memcpy(mbmi_ext->global_mvs, mbmi_ext_best->global_mvs,
213 sizeof(mbmi_ext->global_mvs));
214}
215
216void av1_update_state(const AV1_COMP *const cpi, ThreadData *td,
217 const PICK_MODE_CONTEXT *const ctx, int mi_row,
218 int mi_col, BLOCK_SIZE bsize, RUN_TYPE dry_run) {
219 int i, x_idx, y;
220 const AV1_COMMON *const cm = &cpi->common;
221 const CommonModeInfoParams *const mi_params = &cm->mi_params;
222 const int num_planes = av1_num_planes(cm);
223 RD_COUNTS *const rdc = &td->rd_counts;
224 MACROBLOCK *const x = &td->mb;
225 MACROBLOCKD *const xd = &x->e_mbd;
226 struct macroblock_plane *const p = x->plane;
227 struct macroblockd_plane *const pd = xd->plane;
228 const MB_MODE_INFO *const mi = &ctx->mic;
229 MB_MODE_INFO *const mi_addr = xd->mi[0];
230 const struct segmentation *const seg = &cm->seg;
venkat sanampudi24055022020-07-03 06:52:28 +0530231 assert(bsize < BLOCK_SIZES_ALL);
liang zhaoc6f775a2020-12-17 11:54:58 -0800232#if CONFIG_SDP
233 const int bw = mi_size_wide[mi->sb_type[xd->tree_type == CHROMA_PART]];
234 const int bh = mi_size_high[mi->sb_type[xd->tree_type == CHROMA_PART]];
235#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530236 const int bw = mi_size_wide[mi->sb_type];
237 const int bh = mi_size_high[mi->sb_type];
liang zhaoc6f775a2020-12-17 11:54:58 -0800238#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530239 const int mis = mi_params->mi_stride;
240 const int mi_width = mi_size_wide[bsize];
241 const int mi_height = mi_size_high[bsize];
242 TxfmSearchInfo *txfm_info = &x->txfm_search_info;
liang zhaoc6f775a2020-12-17 11:54:58 -0800243#if CONFIG_SDP
244 assert(mi->sb_type[xd->tree_type == CHROMA_PART] == bsize);
245#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530246 assert(mi->sb_type == bsize);
liang zhaoc6f775a2020-12-17 11:54:58 -0800247#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530248
249 *mi_addr = *mi;
liang zhaoc6f775a2020-12-17 11:54:58 -0800250#if CONFIG_SDP
251 if (xd->tree_type != CHROMA_PART)
252#endif
253 copy_mbmi_ext_frame_to_mbmi_ext(x->mbmi_ext, &ctx->mbmi_ext_best,
254 av1_ref_frame_type(ctx->mic.ref_frame));
Jayasanker Je9ad4752020-06-30 19:30:03 +0530255
256 memcpy(txfm_info->blk_skip, ctx->blk_skip,
257 sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk);
258
259 txfm_info->skip_txfm = ctx->rd_stats.skip_txfm;
liang zhaoc6f775a2020-12-17 11:54:58 -0800260#if CONFIG_SDP
261 if (xd->tree_type != CHROMA_PART) {
262#endif
263 xd->tx_type_map = ctx->tx_type_map;
264 xd->tx_type_map_stride = mi_size_wide[bsize];
265 // If not dry_run, copy the transform type data into the frame level buffer.
266 // Encoder will fetch tx types when writing bitstream.
267 if (!dry_run) {
268 const int grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col);
269 uint8_t *const tx_type_map = mi_params->tx_type_map + grid_idx;
270 const int mi_stride = mi_params->mi_stride;
271 for (int blk_row = 0; blk_row < bh; ++blk_row) {
272 av1_copy_array(tx_type_map + blk_row * mi_stride,
273 xd->tx_type_map + blk_row * xd->tx_type_map_stride, bw);
274 }
275 xd->tx_type_map = tx_type_map;
276 xd->tx_type_map_stride = mi_stride;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530277 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800278#if CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530279 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800280#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530281
282 // If segmentation in use
283 if (seg->enabled) {
284 // For in frame complexity AQ copy the segment id from the segment map.
285 if (cpi->oxcf.q_cfg.aq_mode == COMPLEXITY_AQ) {
286 const uint8_t *const map =
287 seg->update_map ? cpi->enc_seg.map : cm->last_frame_seg_map;
288 mi_addr->segment_id =
289 map ? get_segment_id(mi_params, map, bsize, mi_row, mi_col) : 0;
290 reset_tx_size(x, mi_addr, x->txfm_search_params.tx_mode_search_type);
291 }
292 // Else for cyclic refresh mode update the segment map, set the segment id
293 // and then update the quantizer.
leolzhaoaa4d7692021-01-28 11:00:33 -0800294#if CONFIG_SDP
295 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
296 xd->tree_type == SHARED_PART) {
297#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530298 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) {
leolzhaoaa4d7692021-01-28 11:00:33 -0800299#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530300 av1_cyclic_refresh_update_segment(cpi, mi_addr, mi_row, mi_col, bsize,
301 ctx->rd_stats.rate, ctx->rd_stats.dist,
302 txfm_info->skip_txfm);
303 }
304 if (mi_addr->uv_mode == UV_CFL_PRED && !is_cfl_allowed(xd))
305 mi_addr->uv_mode = UV_DC_PRED;
306 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800307#if CONFIG_SDP
308 for (i = (xd->tree_type == CHROMA_PART); i < num_planes; ++i) {
309#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530310 for (i = 0; i < num_planes; ++i) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800311#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530312 p[i].coeff = ctx->coeff[i];
313 p[i].qcoeff = ctx->qcoeff[i];
314 p[i].dqcoeff = ctx->dqcoeff[i];
315 p[i].eobs = ctx->eobs[i];
316 p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
317 }
318 for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i];
319 // Restore the coding context of the MB to that that was in place
320 // when the mode was picked for it
321 for (y = 0; y < mi_height; y++) {
322 for (x_idx = 0; x_idx < mi_width; x_idx++) {
323 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx &&
324 (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800325#if CONFIG_SDP
326 const int mi_idx =
327 get_alloc_mi_idx(mi_params, mi_row + y, mi_col + x_idx);
328 xd->mi[x_idx + y * mis] = &mi_params->mi_alloc[mi_idx];
329 if (xd->tree_type == LUMA_PART) {
330 *(xd->mi[x_idx + y * mis]) = *mi_addr;
331 } else if (xd->tree_type == CHROMA_PART) {
332 xd->mi[x_idx + y * mis]->sb_type[PLANE_TYPE_UV] =
333 mi_addr->sb_type[PLANE_TYPE_UV];
334 xd->mi[x_idx + y * mis]->uv_mode = mi_addr->uv_mode;
335 xd->mi[x_idx + y * mis]->angle_delta[PLANE_TYPE_UV] =
336 mi_addr->angle_delta[PLANE_TYPE_UV];
337 xd->mi[x_idx + y * mis]->cfl_alpha_signs = mi_addr->cfl_alpha_signs;
338 xd->mi[x_idx + y * mis]->cfl_alpha_idx = mi_addr->cfl_alpha_idx;
339 xd->mi[x_idx + y * mis]->partition = mi_addr->partition;
340 xd->mi[x_idx + y * mis]
341 ->palette_mode_info.palette_size[PLANE_TYPE_UV] =
342 mi_addr->palette_mode_info.palette_size[PLANE_TYPE_UV];
343 for (i = PALETTE_MAX_SIZE; i < 3 * PALETTE_MAX_SIZE; i++)
344 xd->mi[x_idx + y * mis]->palette_mode_info.palette_colors[i] =
345 mi_addr->palette_mode_info.palette_colors[i];
346 } else {
347 xd->mi[x_idx + y * mis] = mi_addr;
348 }
349#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530350 xd->mi[x_idx + y * mis] = mi_addr;
liang zhaoc6f775a2020-12-17 11:54:58 -0800351#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530352 }
353 }
354 }
355
356 if (cpi->oxcf.q_cfg.aq_mode)
357 av1_init_plane_quantizers(cpi, x, mi_addr->segment_id);
358
359 if (dry_run) return;
360
361#if CONFIG_INTERNAL_STATS
362 {
363 unsigned int *const mode_chosen_counts =
364 (unsigned int *)cpi->mode_chosen_counts; // Cast const away.
365 if (frame_is_intra_only(cm)) {
366 static const int kf_mode_index[] = {
367 THR_DC /*DC_PRED*/,
368 THR_V_PRED /*V_PRED*/,
369 THR_H_PRED /*H_PRED*/,
370 THR_D45_PRED /*D45_PRED*/,
371 THR_D135_PRED /*D135_PRED*/,
372 THR_D113_PRED /*D113_PRED*/,
373 THR_D157_PRED /*D157_PRED*/,
374 THR_D203_PRED /*D203_PRED*/,
375 THR_D67_PRED /*D67_PRED*/,
376 THR_SMOOTH, /*SMOOTH_PRED*/
377 THR_SMOOTH_V, /*SMOOTH_V_PRED*/
378 THR_SMOOTH_H, /*SMOOTH_H_PRED*/
379 THR_PAETH /*PAETH_PRED*/,
380 };
381 ++mode_chosen_counts[kf_mode_index[mi_addr->mode]];
382 } else {
383 // Note how often each mode chosen as best
384 ++mode_chosen_counts[ctx->best_mode_index];
385 }
386 }
387#endif
388 if (!frame_is_intra_only(cm)) {
389 if (is_inter_block(mi_addr)) {
390 // TODO(sarahparker): global motion stats need to be handled per-tile
391 // to be compatible with tile-based threading.
392 update_global_motion_used(mi_addr->mode, bsize, mi_addr, rdc);
393 }
394
395 if (cm->features.interp_filter == SWITCHABLE &&
396 mi_addr->motion_mode != WARPED_CAUSAL &&
397 !is_nontrans_global_motion(xd, xd->mi[0])) {
398 update_filter_type_count(td->counts, xd, mi_addr);
399 }
400
401 rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
402 rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
403 rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
404 }
405
406 const int x_mis = AOMMIN(bw, mi_params->mi_cols - mi_col);
407 const int y_mis = AOMMIN(bh, mi_params->mi_rows - mi_row);
408 if (cm->seq_params.order_hint_info.enable_ref_frame_mvs)
409 av1_copy_frame_mvs(cm, mi, mi_row, mi_col, x_mis, y_mis);
410}
411
412void av1_update_inter_mode_stats(FRAME_CONTEXT *fc, FRAME_COUNTS *counts,
413 PREDICTION_MODE mode, int16_t mode_context) {
414 (void)counts;
415
416 int16_t mode_ctx = mode_context & NEWMV_CTX_MASK;
417 if (mode == NEWMV) {
418#if CONFIG_ENTROPY_STATS
419 ++counts->newmv_mode[mode_ctx][0];
420#endif
421 update_cdf(fc->newmv_cdf[mode_ctx], 0, 2);
422 return;
423 }
424
425#if CONFIG_ENTROPY_STATS
426 ++counts->newmv_mode[mode_ctx][1];
427#endif
428 update_cdf(fc->newmv_cdf[mode_ctx], 1, 2);
429
430 mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
431 if (mode == GLOBALMV) {
432#if CONFIG_ENTROPY_STATS
433 ++counts->zeromv_mode[mode_ctx][0];
434#endif
435 update_cdf(fc->zeromv_cdf[mode_ctx], 0, 2);
436 return;
437 }
438
439#if CONFIG_ENTROPY_STATS
440 ++counts->zeromv_mode[mode_ctx][1];
441#endif
442 update_cdf(fc->zeromv_cdf[mode_ctx], 1, 2);
443
444 mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
445#if CONFIG_ENTROPY_STATS
446 ++counts->refmv_mode[mode_ctx][mode != NEARESTMV];
447#endif
448 update_cdf(fc->refmv_cdf[mode_ctx], mode != NEARESTMV, 2);
449}
450
451static void update_palette_cdf(MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
452 FRAME_COUNTS *counts) {
453 FRAME_CONTEXT *fc = xd->tile_ctx;
liang zhaoc6f775a2020-12-17 11:54:58 -0800454#if CONFIG_SDP
455 const BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART];
456#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530457 const BLOCK_SIZE bsize = mbmi->sb_type;
liang zhaoc6f775a2020-12-17 11:54:58 -0800458#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530459 const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
460 const int palette_bsize_ctx = av1_get_palette_bsize_ctx(bsize);
461
462 (void)counts;
liang zhaoc6f775a2020-12-17 11:54:58 -0800463#if CONFIG_SDP
464 if (mbmi->mode == DC_PRED && xd->tree_type != CHROMA_PART) {
465#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530466 if (mbmi->mode == DC_PRED) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800467#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530468 const int n = pmi->palette_size[0];
469 const int palette_mode_ctx = av1_get_palette_mode_ctx(xd);
470
471#if CONFIG_ENTROPY_STATS
472 ++counts->palette_y_mode[palette_bsize_ctx][palette_mode_ctx][n > 0];
473#endif
474 update_cdf(fc->palette_y_mode_cdf[palette_bsize_ctx][palette_mode_ctx],
475 n > 0, 2);
476 if (n > 0) {
477#if CONFIG_ENTROPY_STATS
478 ++counts->palette_y_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE];
479#endif
480 update_cdf(fc->palette_y_size_cdf[palette_bsize_ctx],
481 n - PALETTE_MIN_SIZE, PALETTE_SIZES);
482 }
483 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800484#if CONFIG_SDP
485 if (mbmi->uv_mode == UV_DC_PRED && xd->tree_type != LUMA_PART) {
486#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530487 if (mbmi->uv_mode == UV_DC_PRED) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800488#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530489 const int n = pmi->palette_size[1];
490 const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
491
492#if CONFIG_ENTROPY_STATS
493 ++counts->palette_uv_mode[palette_uv_mode_ctx][n > 0];
494#endif
495 update_cdf(fc->palette_uv_mode_cdf[palette_uv_mode_ctx], n > 0, 2);
496
497 if (n > 0) {
498#if CONFIG_ENTROPY_STATS
499 ++counts->palette_uv_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE];
500#endif
501 update_cdf(fc->palette_uv_size_cdf[palette_bsize_ctx],
502 n - PALETTE_MIN_SIZE, PALETTE_SIZES);
503 }
504 }
505}
506
507void av1_sum_intra_stats(const AV1_COMMON *const cm, FRAME_COUNTS *counts,
508 MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
509 const MB_MODE_INFO *above_mi,
510 const MB_MODE_INFO *left_mi, const int intraonly) {
511 FRAME_CONTEXT *fc = xd->tile_ctx;
512 const PREDICTION_MODE y_mode = mbmi->mode;
513 (void)counts;
liang zhaoc6f775a2020-12-17 11:54:58 -0800514#if CONFIG_SDP
515 const BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART];
516#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530517 const BLOCK_SIZE bsize = mbmi->sb_type;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530518#endif
liang zhaoc6f775a2020-12-17 11:54:58 -0800519#if CONFIG_SDP
520 if (xd->tree_type != CHROMA_PART) {
521#endif
522 if (intraonly) {
523#if CONFIG_ENTROPY_STATS
524 const PREDICTION_MODE above = av1_above_block_mode(above_mi);
525 const PREDICTION_MODE left = av1_left_block_mode(left_mi);
526 const int above_ctx = intra_mode_context[above];
527 const int left_ctx = intra_mode_context[left];
528 ++counts->kf_y_mode[above_ctx][left_ctx][y_mode];
529#endif // CONFIG_ENTROPY_STATS
530 update_cdf(get_y_mode_cdf(fc, above_mi, left_mi), y_mode, INTRA_MODES);
531 } else {
532#if CONFIG_ENTROPY_STATS
533 ++counts->y_mode[size_group_lookup[bsize]][y_mode];
534#endif // CONFIG_ENTROPY_STATS
535 update_cdf(fc->y_mode_cdf[size_group_lookup[bsize]], y_mode, INTRA_MODES);
536 }
537
538 if (av1_filter_intra_allowed(cm, mbmi)) {
539 const int use_filter_intra_mode =
540 mbmi->filter_intra_mode_info.use_filter_intra;
541#if CONFIG_ENTROPY_STATS
542#if CONFIG_SDP
543 ++counts->filter_intra[mbmi->sb_type[xd->tree_type == CHROMA_PART]]
544 [use_filter_intra_mode];
545#else
546 ++counts->filter_intra[mbmi->sb_type][use_filter_intra_mode];
547#endif
548 if (use_filter_intra_mode) {
549 ++counts->filter_intra_mode[mbmi->filter_intra_mode_info
550 .filter_intra_mode];
551 }
552#endif // CONFIG_ENTROPY_STATS
553#if CONFIG_SDP
554 update_cdf(
555 fc->filter_intra_cdfs[mbmi->sb_type[xd->tree_type == CHROMA_PART]],
556 use_filter_intra_mode, 2);
557#else
558 update_cdf(fc->filter_intra_cdfs[mbmi->sb_type], use_filter_intra_mode, 2);
559#endif
560 if (use_filter_intra_mode) {
561 update_cdf(fc->filter_intra_mode_cdf,
562 mbmi->filter_intra_mode_info.filter_intra_mode,
563 FILTER_INTRA_MODES);
564 }
565 }
566 if (av1_is_directional_mode(mbmi->mode) && av1_use_angle_delta(bsize)) {
567#if CONFIG_ENTROPY_STATS
568 ++counts->angle_delta[mbmi->mode - V_PRED]
569 [mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA];
570#endif
571#if CONFIG_SDP
572 update_cdf(fc->angle_delta_cdf[PLANE_TYPE_Y][mbmi->mode - V_PRED],
573 mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA,
574 2 * MAX_ANGLE_DELTA + 1);
575#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530576 update_cdf(fc->angle_delta_cdf[mbmi->mode - V_PRED],
577 mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA,
578 2 * MAX_ANGLE_DELTA + 1);
liang zhaoc6f775a2020-12-17 11:54:58 -0800579#endif
580 }
581#if CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530582 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800583#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530584
585 if (!xd->is_chroma_ref) return;
liang zhaoc6f775a2020-12-17 11:54:58 -0800586#if CONFIG_SDP
587 if (xd->tree_type != LUMA_PART) {
588#endif
589 const UV_PREDICTION_MODE uv_mode = mbmi->uv_mode;
590 const CFL_ALLOWED_TYPE cfl_allowed = is_cfl_allowed(xd);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530591#if CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800592 ++counts->uv_mode[cfl_allowed][y_mode][uv_mode];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530593#endif // CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800594 update_cdf(fc->uv_mode_cdf[cfl_allowed][y_mode], uv_mode,
595 UV_INTRA_MODES - !cfl_allowed);
596 if (uv_mode == UV_CFL_PRED) {
597 const int8_t joint_sign = mbmi->cfl_alpha_signs;
598 const uint8_t idx = mbmi->cfl_alpha_idx;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530599
600#if CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800601 ++counts->cfl_sign[joint_sign];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530602#endif
liang zhaoc6f775a2020-12-17 11:54:58 -0800603 update_cdf(fc->cfl_sign_cdf, joint_sign, CFL_JOINT_SIGNS);
604 if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
605 aom_cdf_prob *cdf_u = fc->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530606
607#if CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800608 ++counts->cfl_alpha[CFL_CONTEXT_U(joint_sign)][CFL_IDX_U(idx)];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530609#endif
liang zhaoc6f775a2020-12-17 11:54:58 -0800610 update_cdf(cdf_u, CFL_IDX_U(idx), CFL_ALPHABET_SIZE);
611 }
612 if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
613 aom_cdf_prob *cdf_v = fc->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
614
615#if CONFIG_ENTROPY_STATS
616 ++counts->cfl_alpha[CFL_CONTEXT_V(joint_sign)][CFL_IDX_V(idx)];
617#endif
618 update_cdf(cdf_v, CFL_IDX_V(idx), CFL_ALPHABET_SIZE);
619 }
Jayasanker Je9ad4752020-06-30 19:30:03 +0530620 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800621 if (av1_is_directional_mode(get_uv_mode(uv_mode)) &&
622 av1_use_angle_delta(bsize)) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530623#if CONFIG_ENTROPY_STATS
liang zhaoc6f775a2020-12-17 11:54:58 -0800624 ++counts->angle_delta[uv_mode - UV_V_PRED]
625 [mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA];
Jayasanker Je9ad4752020-06-30 19:30:03 +0530626#endif
liang zhaoc6f775a2020-12-17 11:54:58 -0800627#if CONFIG_SDP
628 update_cdf(fc->angle_delta_cdf[PLANE_TYPE_UV][uv_mode - UV_V_PRED],
629 mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA,
630 2 * MAX_ANGLE_DELTA + 1);
631#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530632 update_cdf(fc->angle_delta_cdf[uv_mode - UV_V_PRED],
633 mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA,
634 2 * MAX_ANGLE_DELTA + 1);
liang zhaoc6f775a2020-12-17 11:54:58 -0800635#endif
636 }
637#if CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530638 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800639#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530640 if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize)) {
641 update_palette_cdf(xd, mbmi, counts);
642 }
643}
644
645void av1_restore_context(MACROBLOCK *x, const RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
646 int mi_row, int mi_col, BLOCK_SIZE bsize,
647 const int num_planes) {
648 MACROBLOCKD *xd = &x->e_mbd;
649 int p;
650 const int num_4x4_blocks_wide = mi_size_wide[bsize];
651 const int num_4x4_blocks_high = mi_size_high[bsize];
652 int mi_width = mi_size_wide[bsize];
653 int mi_height = mi_size_high[bsize];
liang zhaoc6f775a2020-12-17 11:54:58 -0800654#if CONFIG_SDP
655 for (p = (xd->tree_type == CHROMA_PART); p < num_planes; p++) {
656#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530657 for (p = 0; p < num_planes; p++) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800658#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530659 int tx_col = mi_col;
660 int tx_row = mi_row & MAX_MIB_MASK;
661 memcpy(
662 xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x),
663 ctx->a + num_4x4_blocks_wide * p,
664 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
665 xd->plane[p].subsampling_x);
666 memcpy(xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y),
667 ctx->l + num_4x4_blocks_high * p,
668 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
669 xd->plane[p].subsampling_y);
liang zhaoc6f775a2020-12-17 11:54:58 -0800670#if CONFIG_SDP
671 memcpy(xd->above_partition_context[p] + mi_col, ctx->sa + mi_width * p,
672 sizeof(*xd->above_partition_context[p]) * mi_width);
673 memcpy(xd->left_partition_context[p] + (mi_row & MAX_MIB_MASK),
674 ctx->sl + mi_height * p,
675 sizeof(xd->left_partition_context[p][0]) * mi_height);
676#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530677 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800678#if !CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530679 memcpy(xd->above_partition_context + mi_col, ctx->sa,
680 sizeof(*xd->above_partition_context) * mi_width);
681 memcpy(xd->left_partition_context + (mi_row & MAX_MIB_MASK), ctx->sl,
682 sizeof(xd->left_partition_context[0]) * mi_height);
liang zhaoc6f775a2020-12-17 11:54:58 -0800683#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530684 xd->above_txfm_context = ctx->p_ta;
685 xd->left_txfm_context = ctx->p_tl;
686 memcpy(xd->above_txfm_context, ctx->ta,
687 sizeof(*xd->above_txfm_context) * mi_width);
688 memcpy(xd->left_txfm_context, ctx->tl,
689 sizeof(*xd->left_txfm_context) * mi_height);
690}
691
692void av1_save_context(const MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
693 int mi_row, int mi_col, BLOCK_SIZE bsize,
694 const int num_planes) {
695 const MACROBLOCKD *xd = &x->e_mbd;
696 int p;
697 int mi_width = mi_size_wide[bsize];
698 int mi_height = mi_size_high[bsize];
699
700 // buffer the above/left context information of the block in search.
liang zhaoc6f775a2020-12-17 11:54:58 -0800701#if CONFIG_SDP
702 for (p = (xd->tree_type == CHROMA_PART); p < num_planes; ++p) {
703#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530704 for (p = 0; p < num_planes; ++p) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800705#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530706 int tx_col = mi_col;
707 int tx_row = mi_row & MAX_MIB_MASK;
708 memcpy(
709 ctx->a + mi_width * p,
710 xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x),
711 (sizeof(ENTROPY_CONTEXT) * mi_width) >> xd->plane[p].subsampling_x);
712 memcpy(ctx->l + mi_height * p,
713 xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y),
714 (sizeof(ENTROPY_CONTEXT) * mi_height) >> xd->plane[p].subsampling_y);
liang zhaoc6f775a2020-12-17 11:54:58 -0800715#if CONFIG_SDP
716 memcpy(ctx->sa + mi_width * p, xd->above_partition_context[p] + mi_col,
717 sizeof(*xd->above_partition_context[p]) * mi_width);
718 memcpy(ctx->sl + mi_height * p,
719 xd->left_partition_context[p] + (mi_row & MAX_MIB_MASK),
720 sizeof(xd->left_partition_context[p][0]) * mi_height);
721#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530722 }
liang zhaoc6f775a2020-12-17 11:54:58 -0800723#if !CONFIG_SDP
Jayasanker Je9ad4752020-06-30 19:30:03 +0530724 memcpy(ctx->sa, xd->above_partition_context + mi_col,
725 sizeof(*xd->above_partition_context) * mi_width);
726 memcpy(ctx->sl, xd->left_partition_context + (mi_row & MAX_MIB_MASK),
727 sizeof(xd->left_partition_context[0]) * mi_height);
liang zhaoc6f775a2020-12-17 11:54:58 -0800728#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530729 memcpy(ctx->ta, xd->above_txfm_context,
730 sizeof(*xd->above_txfm_context) * mi_width);
731 memcpy(ctx->tl, xd->left_txfm_context,
732 sizeof(*xd->left_txfm_context) * mi_height);
733 ctx->p_ta = xd->above_txfm_context;
734 ctx->p_tl = xd->left_txfm_context;
735}
736
737static void set_partial_sb_partition(const AV1_COMMON *const cm,
738 MB_MODE_INFO *mi, int bh_in, int bw_in,
739 int mi_rows_remaining,
740 int mi_cols_remaining, BLOCK_SIZE bsize,
741 MB_MODE_INFO **mib) {
742 int bh = bh_in;
743 int r, c;
744 for (r = 0; r < cm->seq_params.mib_size; r += bh) {
745 int bw = bw_in;
746 for (c = 0; c < cm->seq_params.mib_size; c += bw) {
747 const int grid_index = get_mi_grid_idx(&cm->mi_params, r, c);
748 const int mi_index = get_alloc_mi_idx(&cm->mi_params, r, c);
749 mib[grid_index] = mi + mi_index;
liang zhaoc6f775a2020-12-17 11:54:58 -0800750#if CONFIG_SDP
leolzhaoaa4d7692021-01-28 11:00:33 -0800751 mib[grid_index]->sb_type[PLANE_TYPE_Y] =
752 mib[grid_index]->sb_type[PLANE_TYPE_UV] = find_partition_size(
753 bsize, mi_rows_remaining - r, mi_cols_remaining - c, &bh, &bw);
liang zhaoc6f775a2020-12-17 11:54:58 -0800754#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530755 mib[grid_index]->sb_type = find_partition_size(
756 bsize, mi_rows_remaining - r, mi_cols_remaining - c, &bh, &bw);
liang zhaoc6f775a2020-12-17 11:54:58 -0800757#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530758 }
759 }
760}
761
762// This function attempts to set all mode info entries in a given superblock
763// to the same block partition size.
764// However, at the bottom and right borders of the image the requested size
765// may not be allowed in which case this code attempts to choose the largest
766// allowable partition.
767void av1_set_fixed_partitioning(AV1_COMP *cpi, const TileInfo *const tile,
768 MB_MODE_INFO **mib, int mi_row, int mi_col,
769 BLOCK_SIZE bsize) {
770 AV1_COMMON *const cm = &cpi->common;
771 const CommonModeInfoParams *const mi_params = &cm->mi_params;
772 const int mi_rows_remaining = tile->mi_row_end - mi_row;
773 const int mi_cols_remaining = tile->mi_col_end - mi_col;
774 MB_MODE_INFO *const mi_upper_left =
775 mi_params->mi_alloc + get_alloc_mi_idx(mi_params, mi_row, mi_col);
776 int bh = mi_size_high[bsize];
777 int bw = mi_size_wide[bsize];
778
779 assert(bsize >= mi_params->mi_alloc_bsize &&
780 "Attempted to use bsize < mi_params->mi_alloc_bsize");
781 assert((mi_rows_remaining > 0) && (mi_cols_remaining > 0));
782
783 // Apply the requested partition size to the SB if it is all "in image"
784 if ((mi_cols_remaining >= cm->seq_params.mib_size) &&
785 (mi_rows_remaining >= cm->seq_params.mib_size)) {
786 for (int block_row = 0; block_row < cm->seq_params.mib_size;
787 block_row += bh) {
788 for (int block_col = 0; block_col < cm->seq_params.mib_size;
789 block_col += bw) {
790 const int grid_index = get_mi_grid_idx(mi_params, block_row, block_col);
791 const int mi_index = get_alloc_mi_idx(mi_params, block_row, block_col);
792 mib[grid_index] = mi_upper_left + mi_index;
liang zhaoc6f775a2020-12-17 11:54:58 -0800793#if CONFIG_SDP
leolzhaoaa4d7692021-01-28 11:00:33 -0800794 mib[grid_index]->sb_type[PLANE_TYPE_Y] = bsize;
795 mib[grid_index]->sb_type[PLANE_TYPE_UV] = bsize;
liang zhaoc6f775a2020-12-17 11:54:58 -0800796#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530797 mib[grid_index]->sb_type = bsize;
liang zhaoc6f775a2020-12-17 11:54:58 -0800798#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530799 }
800 }
801 } else {
802 // Else this is a partial SB.
803 set_partial_sb_partition(cm, mi_upper_left, bh, bw, mi_rows_remaining,
804 mi_cols_remaining, bsize, mib);
805 }
806}
leolzhao3db7cca2021-01-26 16:53:07 -0800807#if CONFIG_SDP
808int av1_is_leaf_split_partition(AV1_COMMON *cm, MACROBLOCKD *const xd,
809 int mi_row, int mi_col,
810#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530811int av1_is_leaf_split_partition(AV1_COMMON *cm, int mi_row, int mi_col,
leolzhao3db7cca2021-01-26 16:53:07 -0800812#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530813 BLOCK_SIZE bsize) {
814 const int bs = mi_size_wide[bsize];
815 const int hbs = bs / 2;
816 assert(bsize >= BLOCK_8X8);
817 const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
818
819 for (int i = 0; i < 4; i++) {
820 int x_idx = (i & 1) * hbs;
821 int y_idx = (i >> 1) * hbs;
822 if ((mi_row + y_idx >= cm->mi_params.mi_rows) ||
823 (mi_col + x_idx >= cm->mi_params.mi_cols))
824 return 0;
leolzhao3db7cca2021-01-26 16:53:07 -0800825#if CONFIG_SDP
826 if (get_partition(cm, xd->tree_type == CHROMA_PART, mi_row + y_idx,
827 mi_col + x_idx, subsize) !=
828#else
Jayasanker Je9ad4752020-06-30 19:30:03 +0530829 if (get_partition(cm, mi_row + y_idx, mi_col + x_idx, subsize) !=
leolzhao3db7cca2021-01-26 16:53:07 -0800830#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +0530831 PARTITION_NONE &&
832 subsize != BLOCK_8X8)
833 return 0;
834 }
835 return 1;
836}
837
838#if !CONFIG_REALTIME_ONLY
Jayasanker J37596eb2020-08-20 16:39:40 +0530839int av1_get_rdmult_delta(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
840 int mi_col, int orig_rdmult) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530841 AV1_COMMON *const cm = &cpi->common;
842 const GF_GROUP *const gf_group = &cpi->gf_group;
843 assert(IMPLIES(cpi->gf_group.size > 0,
844 cpi->gf_group.index < cpi->gf_group.size));
845 const int tpl_idx = cpi->gf_group.index;
846 TplParams *const tpl_data = &cpi->tpl_data;
847 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
848 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
849 const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
850 int tpl_stride = tpl_frame->stride;
851 int64_t intra_cost = 0;
852 int64_t mc_dep_cost = 0;
853 const int mi_wide = mi_size_wide[bsize];
854 const int mi_high = mi_size_high[bsize];
855
856 if (tpl_frame->is_valid == 0) return orig_rdmult;
857
Deepa K G21e5e8e2020-03-28 13:26:09 +0530858 if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return orig_rdmult;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530859
860 if (cpi->gf_group.index >= MAX_TPL_FRAME_IDX) return orig_rdmult;
861
Jayasanker Je9ad4752020-06-30 19:30:03 +0530862 int mi_count = 0;
863 const int mi_col_sr =
864 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
865 const int mi_col_end_sr =
866 coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
867 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
868 const int step = 1 << block_mis_log2;
Urvang Joshie198bf12020-10-08 15:37:55 -0700869 const int row_step = step;
870 const int col_step_sr =
871 coded_to_superres_mi(step, cm->superres_scale_denominator);
872 for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
873 for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
Jayasanker Je9ad4752020-06-30 19:30:03 +0530874 if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
875 TplDepStats *this_stats =
876 &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
877 int64_t mc_dep_delta =
878 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
879 this_stats->mc_dep_dist);
880 intra_cost += this_stats->recrf_dist << RDDIV_BITS;
881 mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530882 mi_count++;
883 }
884 }
Urvang Joshie198bf12020-10-08 15:37:55 -0700885 assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530886
887 aom_clear_system_state();
888
889 double beta = 1.0;
Jayasanker J37596eb2020-08-20 16:39:40 +0530890 if (mc_dep_cost > 0 && intra_cost > 0) {
891 const double r0 = cpi->rd.r0;
892 const double rk = (double)intra_cost / mc_dep_cost;
893 beta = (r0 / rk);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530894 }
895
896 int rdmult = av1_get_adaptive_rdmult(cpi, beta);
897
898 aom_clear_system_state();
899
900 rdmult = AOMMIN(rdmult, orig_rdmult * 3 / 2);
901 rdmult = AOMMAX(rdmult, orig_rdmult * 1 / 2);
902
903 rdmult = AOMMAX(1, rdmult);
904
905 return rdmult;
906}
907
908// Checks to see if a super block is on a horizontal image edge.
909// In most cases this is the "real" edge unless there are formatting
910// bars embedded in the stream.
911int av1_active_h_edge(const AV1_COMP *cpi, int mi_row, int mi_step) {
912 int top_edge = 0;
913 int bottom_edge = cpi->common.mi_params.mi_rows;
914 int is_active_h_edge = 0;
915
Jayasanker Je9ad4752020-06-30 19:30:03 +0530916 if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) ||
917 ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) {
918 is_active_h_edge = 1;
919 }
920 return is_active_h_edge;
921}
922
923// Checks to see if a super block is on a vertical image edge.
924// In most cases this is the "real" edge unless there are formatting
925// bars embedded in the stream.
926int av1_active_v_edge(const AV1_COMP *cpi, int mi_col, int mi_step) {
927 int left_edge = 0;
928 int right_edge = cpi->common.mi_params.mi_cols;
929 int is_active_v_edge = 0;
930
Jayasanker Je9ad4752020-06-30 19:30:03 +0530931 if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) ||
932 ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) {
933 is_active_v_edge = 1;
934 }
935 return is_active_v_edge;
936}
937
938void av1_get_tpl_stats_sb(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
939 int mi_col, SuperBlockEnc *sb_enc) {
940 sb_enc->tpl_data_count = 0;
941
942 if (!cpi->oxcf.algo_cfg.enable_tpl_model) return;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530943 if (cpi->common.current_frame.frame_type == KEY_FRAME) return;
944 const FRAME_UPDATE_TYPE update_type = get_frame_update_type(&cpi->gf_group);
Debargha Mukherjee5f64acd2020-08-18 14:32:28 -0700945 if (update_type == INTNL_OVERLAY_UPDATE || update_type == OVERLAY_UPDATE ||
946 update_type == KFFLT_OVERLAY_UPDATE)
Jayasanker Je9ad4752020-06-30 19:30:03 +0530947 return;
948 assert(IMPLIES(cpi->gf_group.size > 0,
949 cpi->gf_group.index < cpi->gf_group.size));
950
951 AV1_COMMON *const cm = &cpi->common;
952 const int gf_group_index = cpi->gf_group.index;
953 TplParams *const tpl_data = &cpi->tpl_data;
954 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[gf_group_index];
955 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
956 int tpl_stride = tpl_frame->stride;
957 const int mi_wide = mi_size_wide[bsize];
958 const int mi_high = mi_size_high[bsize];
959
960 if (tpl_frame->is_valid == 0) return;
961 if (gf_group_index >= MAX_TPL_FRAME_IDX) return;
962
963 int mi_count = 0;
964 int count = 0;
965 const int mi_col_sr =
966 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
967 const int mi_col_end_sr =
968 coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
969 // mi_cols_sr is mi_cols at superres case.
970 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
971
972 // TPL store unit size is not the same as the motion estimation unit size.
973 // Here always use motion estimation size to avoid getting repetitive inter/
974 // intra cost.
Yunqing Wangf0a8cf42020-08-14 14:50:33 -0700975 const BLOCK_SIZE tpl_bsize = convert_length_to_bsize(tpl_data->tpl_bsize_1d);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530976 assert(mi_size_wide[tpl_bsize] == mi_size_high[tpl_bsize]);
Urvang Joshie198bf12020-10-08 15:37:55 -0700977 const int row_step = mi_size_high[tpl_bsize];
978 const int col_step_sr = coded_to_superres_mi(mi_size_wide[tpl_bsize],
979 cm->superres_scale_denominator);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530980
981 // Stride is only based on SB size, and we fill in values for every 16x16
982 // block in a SB.
Urvang Joshie198bf12020-10-08 15:37:55 -0700983 sb_enc->tpl_stride = (mi_col_end_sr - mi_col_sr) / col_step_sr;
Jayasanker Je9ad4752020-06-30 19:30:03 +0530984
Urvang Joshie198bf12020-10-08 15:37:55 -0700985 for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
986 for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
987 assert(count < MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
Jayasanker Je9ad4752020-06-30 19:30:03 +0530988 // Handle partial SB, so that no invalid values are used later.
989 if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) {
990 sb_enc->tpl_inter_cost[count] = INT64_MAX;
991 sb_enc->tpl_intra_cost[count] = INT64_MAX;
992 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
993 sb_enc->tpl_mv[count][i].as_int = INVALID_MV;
994 }
995 count++;
996 continue;
997 }
998
999 TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
1000 row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
1001 sb_enc->tpl_inter_cost[count] = this_stats->inter_cost;
1002 sb_enc->tpl_intra_cost[count] = this_stats->intra_cost;
1003 memcpy(sb_enc->tpl_mv[count], this_stats->mv, sizeof(this_stats->mv));
1004 mi_count++;
1005 count++;
1006 }
1007 }
1008
Urvang Joshie198bf12020-10-08 15:37:55 -07001009 assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
Jayasanker Je9ad4752020-06-30 19:30:03 +05301010 sb_enc->tpl_data_count = mi_count;
1011}
1012
1013// analysis_type 0: Use mc_dep_cost and intra_cost
1014// analysis_type 1: Use count of best inter predictor chosen
1015// analysis_type 2: Use cost reduction from intra to inter for best inter
1016// predictor chosen
1017int av1_get_q_for_deltaq_objective(AV1_COMP *const cpi, BLOCK_SIZE bsize,
1018 int mi_row, int mi_col) {
1019 AV1_COMMON *const cm = &cpi->common;
1020 const GF_GROUP *const gf_group = &cpi->gf_group;
1021 assert(IMPLIES(cpi->gf_group.size > 0,
1022 cpi->gf_group.index < cpi->gf_group.size));
1023 const int tpl_idx = cpi->gf_group.index;
1024 TplParams *const tpl_data = &cpi->tpl_data;
1025 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
1026 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
1027 const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
1028 int tpl_stride = tpl_frame->stride;
1029 int64_t intra_cost = 0;
1030 int64_t mc_dep_cost = 0;
1031 const int mi_wide = mi_size_wide[bsize];
1032 const int mi_high = mi_size_high[bsize];
1033 const int base_qindex = cm->quant_params.base_qindex;
1034
1035 if (tpl_frame->is_valid == 0) return base_qindex;
1036
Deepa K G21e5e8e2020-03-28 13:26:09 +05301037 if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return base_qindex;
Jayasanker Je9ad4752020-06-30 19:30:03 +05301038
1039 if (cpi->gf_group.index >= MAX_TPL_FRAME_IDX) return base_qindex;
1040
Jayasanker Je9ad4752020-06-30 19:30:03 +05301041 int mi_count = 0;
1042 const int mi_col_sr =
1043 coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
1044 const int mi_col_end_sr =
1045 coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
1046 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
1047 const int step = 1 << block_mis_log2;
Urvang Joshie198bf12020-10-08 15:37:55 -07001048 const int row_step = step;
1049 const int col_step_sr =
1050 coded_to_superres_mi(step, cm->superres_scale_denominator);
1051 for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
1052 for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
Jayasanker Je9ad4752020-06-30 19:30:03 +05301053 if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
1054 TplDepStats *this_stats =
1055 &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
1056 int64_t mc_dep_delta =
1057 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
1058 this_stats->mc_dep_dist);
1059 intra_cost += this_stats->recrf_dist << RDDIV_BITS;
1060 mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Jayasanker Je9ad4752020-06-30 19:30:03 +05301061 mi_count++;
1062 }
1063 }
Urvang Joshie198bf12020-10-08 15:37:55 -07001064 assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
Jayasanker Je9ad4752020-06-30 19:30:03 +05301065
1066 aom_clear_system_state();
1067
1068 int offset = 0;
1069 double beta = 1.0;
1070 if (mc_dep_cost > 0 && intra_cost > 0) {
1071 const double r0 = cpi->rd.r0;
1072 const double rk = (double)intra_cost / mc_dep_cost;
1073 beta = (r0 / rk);
1074 assert(beta > 0.0);
1075 }
1076 offset = av1_get_deltaq_offset(cpi, base_qindex, beta);
1077 aom_clear_system_state();
1078
1079 const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
1080 offset = AOMMIN(offset, delta_q_info->delta_q_res * 9 - 1);
1081 offset = AOMMAX(offset, -delta_q_info->delta_q_res * 9 + 1);
1082 int qindex = cm->quant_params.base_qindex + offset;
1083 qindex = AOMMIN(qindex, MAXQ);
1084 qindex = AOMMAX(qindex, MINQ);
1085
1086 return qindex;
1087}
1088#endif // !CONFIG_REALTIME_ONLY
1089
1090void av1_reset_simple_motion_tree_partition(SIMPLE_MOTION_DATA_TREE *sms_tree,
1091 BLOCK_SIZE bsize) {
1092 sms_tree->partitioning = PARTITION_NONE;
1093
1094 if (bsize >= BLOCK_8X8) {
1095 BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
1096 for (int idx = 0; idx < 4; ++idx)
1097 av1_reset_simple_motion_tree_partition(sms_tree->split[idx], subsize);
1098 }
1099}
1100
1101// Record the ref frames that have been selected by square partition blocks.
1102void av1_update_picked_ref_frames_mask(MACROBLOCK *const x, int ref_type,
1103 BLOCK_SIZE bsize, int mib_size,
1104 int mi_row, int mi_col) {
1105 assert(mi_size_wide[bsize] == mi_size_high[bsize]);
1106 const int sb_size_mask = mib_size - 1;
1107 const int mi_row_in_sb = mi_row & sb_size_mask;
1108 const int mi_col_in_sb = mi_col & sb_size_mask;
1109 const int mi_size = mi_size_wide[bsize];
1110 for (int i = mi_row_in_sb; i < mi_row_in_sb + mi_size; ++i) {
1111 for (int j = mi_col_in_sb; j < mi_col_in_sb + mi_size; ++j) {
1112 x->picked_ref_frames_mask[i * 32 + j] |= 1 << ref_type;
1113 }
1114 }
1115}
1116
1117static void avg_cdf_symbol(aom_cdf_prob *cdf_ptr_left, aom_cdf_prob *cdf_ptr_tr,
1118 int num_cdfs, int cdf_stride, int nsymbs,
1119 int wt_left, int wt_tr) {
1120 for (int i = 0; i < num_cdfs; i++) {
1121 for (int j = 0; j <= nsymbs; j++) {
1122 cdf_ptr_left[i * cdf_stride + j] =
1123 (aom_cdf_prob)(((int)cdf_ptr_left[i * cdf_stride + j] * wt_left +
1124 (int)cdf_ptr_tr[i * cdf_stride + j] * wt_tr +
1125 ((wt_left + wt_tr) / 2)) /
1126 (wt_left + wt_tr));
1127 assert(cdf_ptr_left[i * cdf_stride + j] >= 0 &&
1128 cdf_ptr_left[i * cdf_stride + j] < CDF_PROB_TOP);
1129 }
1130 }
1131}
1132
1133#define AVERAGE_CDF(cname_left, cname_tr, nsymbs) \
1134 AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, CDF_SIZE(nsymbs))
1135
1136#define AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, cdf_stride) \
1137 do { \
1138 aom_cdf_prob *cdf_ptr_left = (aom_cdf_prob *)cname_left; \
1139 aom_cdf_prob *cdf_ptr_tr = (aom_cdf_prob *)cname_tr; \
1140 int array_size = (int)sizeof(cname_left) / sizeof(aom_cdf_prob); \
1141 int num_cdfs = array_size / cdf_stride; \
1142 avg_cdf_symbol(cdf_ptr_left, cdf_ptr_tr, num_cdfs, cdf_stride, nsymbs, \
1143 wt_left, wt_tr); \
1144 } while (0)
1145
1146static void avg_nmv(nmv_context *nmv_left, nmv_context *nmv_tr, int wt_left,
1147 int wt_tr) {
1148 AVERAGE_CDF(nmv_left->joints_cdf, nmv_tr->joints_cdf, 4);
1149 for (int i = 0; i < 2; i++) {
1150 AVERAGE_CDF(nmv_left->comps[i].classes_cdf, nmv_tr->comps[i].classes_cdf,
1151 MV_CLASSES);
1152 AVERAGE_CDF(nmv_left->comps[i].class0_fp_cdf,
1153 nmv_tr->comps[i].class0_fp_cdf, MV_FP_SIZE);
1154 AVERAGE_CDF(nmv_left->comps[i].fp_cdf, nmv_tr->comps[i].fp_cdf, MV_FP_SIZE);
1155 AVERAGE_CDF(nmv_left->comps[i].sign_cdf, nmv_tr->comps[i].sign_cdf, 2);
1156 AVERAGE_CDF(nmv_left->comps[i].class0_hp_cdf,
1157 nmv_tr->comps[i].class0_hp_cdf, 2);
1158 AVERAGE_CDF(nmv_left->comps[i].hp_cdf, nmv_tr->comps[i].hp_cdf, 2);
1159 AVERAGE_CDF(nmv_left->comps[i].class0_cdf, nmv_tr->comps[i].class0_cdf,
1160 CLASS0_SIZE);
1161 AVERAGE_CDF(nmv_left->comps[i].bits_cdf, nmv_tr->comps[i].bits_cdf, 2);
1162 }
1163}
1164
1165// In case of row-based multi-threading of encoder, since we always
1166// keep a top - right sync, we can average the top - right SB's CDFs and
1167// the left SB's CDFs and use the same for current SB's encoding to
1168// improve the performance. This function facilitates the averaging
1169// of CDF and used only when row-mt is enabled in encoder.
1170void av1_avg_cdf_symbols(FRAME_CONTEXT *ctx_left, FRAME_CONTEXT *ctx_tr,
1171 int wt_left, int wt_tr) {
1172 AVERAGE_CDF(ctx_left->txb_skip_cdf, ctx_tr->txb_skip_cdf, 2);
1173 AVERAGE_CDF(ctx_left->eob_extra_cdf, ctx_tr->eob_extra_cdf, 2);
1174 AVERAGE_CDF(ctx_left->dc_sign_cdf, ctx_tr->dc_sign_cdf, 2);
1175 AVERAGE_CDF(ctx_left->eob_flag_cdf16, ctx_tr->eob_flag_cdf16, 5);
1176 AVERAGE_CDF(ctx_left->eob_flag_cdf32, ctx_tr->eob_flag_cdf32, 6);
1177 AVERAGE_CDF(ctx_left->eob_flag_cdf64, ctx_tr->eob_flag_cdf64, 7);
1178 AVERAGE_CDF(ctx_left->eob_flag_cdf128, ctx_tr->eob_flag_cdf128, 8);
1179 AVERAGE_CDF(ctx_left->eob_flag_cdf256, ctx_tr->eob_flag_cdf256, 9);
1180 AVERAGE_CDF(ctx_left->eob_flag_cdf512, ctx_tr->eob_flag_cdf512, 10);
1181 AVERAGE_CDF(ctx_left->eob_flag_cdf1024, ctx_tr->eob_flag_cdf1024, 11);
1182 AVERAGE_CDF(ctx_left->coeff_base_eob_cdf, ctx_tr->coeff_base_eob_cdf, 3);
1183 AVERAGE_CDF(ctx_left->coeff_base_cdf, ctx_tr->coeff_base_cdf, 4);
1184 AVERAGE_CDF(ctx_left->coeff_br_cdf, ctx_tr->coeff_br_cdf, BR_CDF_SIZE);
1185 AVERAGE_CDF(ctx_left->newmv_cdf, ctx_tr->newmv_cdf, 2);
1186 AVERAGE_CDF(ctx_left->zeromv_cdf, ctx_tr->zeromv_cdf, 2);
1187 AVERAGE_CDF(ctx_left->refmv_cdf, ctx_tr->refmv_cdf, 2);
1188 AVERAGE_CDF(ctx_left->drl_cdf, ctx_tr->drl_cdf, 2);
1189 AVERAGE_CDF(ctx_left->inter_compound_mode_cdf,
1190 ctx_tr->inter_compound_mode_cdf, INTER_COMPOUND_MODES);
1191 AVERAGE_CDF(ctx_left->compound_type_cdf, ctx_tr->compound_type_cdf,
1192 MASKED_COMPOUND_TYPES);
1193 AVERAGE_CDF(ctx_left->wedge_idx_cdf, ctx_tr->wedge_idx_cdf, 16);
1194 AVERAGE_CDF(ctx_left->interintra_cdf, ctx_tr->interintra_cdf, 2);
1195 AVERAGE_CDF(ctx_left->wedge_interintra_cdf, ctx_tr->wedge_interintra_cdf, 2);
1196 AVERAGE_CDF(ctx_left->interintra_mode_cdf, ctx_tr->interintra_mode_cdf,
1197 INTERINTRA_MODES);
1198 AVERAGE_CDF(ctx_left->motion_mode_cdf, ctx_tr->motion_mode_cdf, MOTION_MODES);
1199 AVERAGE_CDF(ctx_left->obmc_cdf, ctx_tr->obmc_cdf, 2);
1200 AVERAGE_CDF(ctx_left->palette_y_size_cdf, ctx_tr->palette_y_size_cdf,
1201 PALETTE_SIZES);
1202 AVERAGE_CDF(ctx_left->palette_uv_size_cdf, ctx_tr->palette_uv_size_cdf,
1203 PALETTE_SIZES);
1204 for (int j = 0; j < PALETTE_SIZES; j++) {
1205 int nsymbs = j + PALETTE_MIN_SIZE;
1206 AVG_CDF_STRIDE(ctx_left->palette_y_color_index_cdf[j],
1207 ctx_tr->palette_y_color_index_cdf[j], nsymbs,
1208 CDF_SIZE(PALETTE_COLORS));
1209 AVG_CDF_STRIDE(ctx_left->palette_uv_color_index_cdf[j],
1210 ctx_tr->palette_uv_color_index_cdf[j], nsymbs,
1211 CDF_SIZE(PALETTE_COLORS));
1212 }
1213 AVERAGE_CDF(ctx_left->palette_y_mode_cdf, ctx_tr->palette_y_mode_cdf, 2);
1214 AVERAGE_CDF(ctx_left->palette_uv_mode_cdf, ctx_tr->palette_uv_mode_cdf, 2);
1215 AVERAGE_CDF(ctx_left->comp_inter_cdf, ctx_tr->comp_inter_cdf, 2);
1216 AVERAGE_CDF(ctx_left->single_ref_cdf, ctx_tr->single_ref_cdf, 2);
1217 AVERAGE_CDF(ctx_left->comp_ref_type_cdf, ctx_tr->comp_ref_type_cdf, 2);
1218 AVERAGE_CDF(ctx_left->uni_comp_ref_cdf, ctx_tr->uni_comp_ref_cdf, 2);
1219 AVERAGE_CDF(ctx_left->comp_ref_cdf, ctx_tr->comp_ref_cdf, 2);
1220 AVERAGE_CDF(ctx_left->comp_bwdref_cdf, ctx_tr->comp_bwdref_cdf, 2);
1221 AVERAGE_CDF(ctx_left->txfm_partition_cdf, ctx_tr->txfm_partition_cdf, 2);
Debargha Mukherjee5bd41f92020-10-04 11:06:11 -07001222#if !CONFIG_REMOVE_DIST_WTD_COMP
Jayasanker Je9ad4752020-06-30 19:30:03 +05301223 AVERAGE_CDF(ctx_left->compound_index_cdf, ctx_tr->compound_index_cdf, 2);
Debargha Mukherjee5bd41f92020-10-04 11:06:11 -07001224#endif // !CONFIG_REMOVE_DIST_WTD_COMP
Jayasanker Je9ad4752020-06-30 19:30:03 +05301225 AVERAGE_CDF(ctx_left->comp_group_idx_cdf, ctx_tr->comp_group_idx_cdf, 2);
1226 AVERAGE_CDF(ctx_left->skip_mode_cdfs, ctx_tr->skip_mode_cdfs, 2);
1227 AVERAGE_CDF(ctx_left->skip_txfm_cdfs, ctx_tr->skip_txfm_cdfs, 2);
1228 AVERAGE_CDF(ctx_left->intra_inter_cdf, ctx_tr->intra_inter_cdf, 2);
1229 avg_nmv(&ctx_left->nmvc, &ctx_tr->nmvc, wt_left, wt_tr);
1230 avg_nmv(&ctx_left->ndvc, &ctx_tr->ndvc, wt_left, wt_tr);
1231 AVERAGE_CDF(ctx_left->intrabc_cdf, ctx_tr->intrabc_cdf, 2);
1232 AVERAGE_CDF(ctx_left->seg.tree_cdf, ctx_tr->seg.tree_cdf, MAX_SEGMENTS);
1233 AVERAGE_CDF(ctx_left->seg.pred_cdf, ctx_tr->seg.pred_cdf, 2);
1234 AVERAGE_CDF(ctx_left->seg.spatial_pred_seg_cdf,
1235 ctx_tr->seg.spatial_pred_seg_cdf, MAX_SEGMENTS);
1236 AVERAGE_CDF(ctx_left->filter_intra_cdfs, ctx_tr->filter_intra_cdfs, 2);
1237 AVERAGE_CDF(ctx_left->filter_intra_mode_cdf, ctx_tr->filter_intra_mode_cdf,
1238 FILTER_INTRA_MODES);
1239 AVERAGE_CDF(ctx_left->switchable_restore_cdf, ctx_tr->switchable_restore_cdf,
1240 RESTORE_SWITCHABLE_TYPES);
1241 AVERAGE_CDF(ctx_left->wiener_restore_cdf, ctx_tr->wiener_restore_cdf, 2);
1242 AVERAGE_CDF(ctx_left->sgrproj_restore_cdf, ctx_tr->sgrproj_restore_cdf, 2);
1243 AVERAGE_CDF(ctx_left->y_mode_cdf, ctx_tr->y_mode_cdf, INTRA_MODES);
1244 AVG_CDF_STRIDE(ctx_left->uv_mode_cdf[0], ctx_tr->uv_mode_cdf[0],
1245 UV_INTRA_MODES - 1, CDF_SIZE(UV_INTRA_MODES));
1246 AVERAGE_CDF(ctx_left->uv_mode_cdf[1], ctx_tr->uv_mode_cdf[1], UV_INTRA_MODES);
liang zhaoc6f775a2020-12-17 11:54:58 -08001247#if CONFIG_SDP
1248 for (int plane_index = 0; plane_index < PARTITION_STRUCTURE_NUM;
1249 plane_index++) {
1250 for (int i = 0; i < PARTITION_CONTEXTS; i++) {
1251 if (i < 4) {
1252 AVG_CDF_STRIDE(ctx_left->partition_cdf[plane_index][i],
1253 ctx_tr->partition_cdf[plane_index][i], 4, CDF_SIZE(10));
1254 } else if (i < 16) {
1255 AVERAGE_CDF(ctx_left->partition_cdf[plane_index][i],
1256 ctx_tr->partition_cdf[plane_index][i], 10);
1257 } else {
1258 AVG_CDF_STRIDE(ctx_left->partition_cdf[plane_index][i],
1259 ctx_tr->partition_cdf[plane_index][i], 8, CDF_SIZE(10));
1260 }
1261 }
1262 }
1263#else
Jayasanker Je9ad4752020-06-30 19:30:03 +05301264 for (int i = 0; i < PARTITION_CONTEXTS; i++) {
1265 if (i < 4) {
1266 AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 4,
1267 CDF_SIZE(10));
1268 } else if (i < 16) {
1269 AVERAGE_CDF(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 10);
1270 } else {
1271 AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 8,
1272 CDF_SIZE(10));
1273 }
1274 }
liang zhaoc6f775a2020-12-17 11:54:58 -08001275#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +05301276 AVERAGE_CDF(ctx_left->switchable_interp_cdf, ctx_tr->switchable_interp_cdf,
1277 SWITCHABLE_FILTERS);
1278 AVERAGE_CDF(ctx_left->kf_y_cdf, ctx_tr->kf_y_cdf, INTRA_MODES);
1279 AVERAGE_CDF(ctx_left->angle_delta_cdf, ctx_tr->angle_delta_cdf,
1280 2 * MAX_ANGLE_DELTA + 1);
1281 AVG_CDF_STRIDE(ctx_left->tx_size_cdf[0], ctx_tr->tx_size_cdf[0], MAX_TX_DEPTH,
1282 CDF_SIZE(MAX_TX_DEPTH + 1));
1283 AVERAGE_CDF(ctx_left->tx_size_cdf[1], ctx_tr->tx_size_cdf[1],
1284 MAX_TX_DEPTH + 1);
1285 AVERAGE_CDF(ctx_left->tx_size_cdf[2], ctx_tr->tx_size_cdf[2],
1286 MAX_TX_DEPTH + 1);
1287 AVERAGE_CDF(ctx_left->tx_size_cdf[3], ctx_tr->tx_size_cdf[3],
1288 MAX_TX_DEPTH + 1);
1289 AVERAGE_CDF(ctx_left->delta_q_cdf, ctx_tr->delta_q_cdf, DELTA_Q_PROBS + 1);
1290 AVERAGE_CDF(ctx_left->delta_lf_cdf, ctx_tr->delta_lf_cdf, DELTA_LF_PROBS + 1);
1291 for (int i = 0; i < FRAME_LF_COUNT; i++) {
1292 AVERAGE_CDF(ctx_left->delta_lf_multi_cdf[i], ctx_tr->delta_lf_multi_cdf[i],
1293 DELTA_LF_PROBS + 1);
1294 }
1295 AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[1], ctx_tr->intra_ext_tx_cdf[1], 7,
1296 CDF_SIZE(TX_TYPES));
1297 AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[2], ctx_tr->intra_ext_tx_cdf[2], 5,
1298 CDF_SIZE(TX_TYPES));
1299 AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[1], ctx_tr->inter_ext_tx_cdf[1], 16,
1300 CDF_SIZE(TX_TYPES));
1301 AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[2], ctx_tr->inter_ext_tx_cdf[2], 12,
1302 CDF_SIZE(TX_TYPES));
1303 AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[3], ctx_tr->inter_ext_tx_cdf[3], 2,
1304 CDF_SIZE(TX_TYPES));
1305 AVERAGE_CDF(ctx_left->cfl_sign_cdf, ctx_tr->cfl_sign_cdf, CFL_JOINT_SIGNS);
1306 AVERAGE_CDF(ctx_left->cfl_alpha_cdf, ctx_tr->cfl_alpha_cdf,
1307 CFL_ALPHABET_SIZE);
1308}
1309
1310// Grade the temporal variation of the source by comparing the current sb and
1311// its collocated block in the last frame.
1312void av1_source_content_sb(AV1_COMP *cpi, MACROBLOCK *x, int offset) {
1313 unsigned int tmp_sse;
1314 unsigned int tmp_variance;
1315 const BLOCK_SIZE bsize = cpi->common.seq_params.sb_size;
1316 uint8_t *src_y = cpi->source->y_buffer;
1317 int src_ystride = cpi->source->y_stride;
1318 uint8_t *last_src_y = cpi->last_source->y_buffer;
1319 int last_src_ystride = cpi->last_source->y_stride;
1320 uint64_t avg_source_sse_threshold = 100000; // ~5*5*(64*64)
1321 uint64_t avg_source_sse_threshold_high = 1000000; // ~15*15*(64*64)
1322 uint64_t sum_sq_thresh = 10000; // sum = sqrt(thresh / 64*64)) ~1.5
Jayasanker Je9ad4752020-06-30 19:30:03 +05301323 MACROBLOCKD *xd = &x->e_mbd;
1324 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) return;
Jayasanker Je9ad4752020-06-30 19:30:03 +05301325 src_y += offset;
1326 last_src_y += offset;
1327 tmp_variance = cpi->fn_ptr[bsize].vf(src_y, src_ystride, last_src_y,
1328 last_src_ystride, &tmp_sse);
1329 // Note: tmp_sse - tmp_variance = ((sum * sum) >> 12)
1330 // Detect large lighting change.
1331 if (tmp_variance < (tmp_sse >> 1) && (tmp_sse - tmp_variance) > sum_sq_thresh)
1332 x->content_state_sb = kLowVarHighSumdiff;
1333 else if (tmp_sse < avg_source_sse_threshold)
1334 x->content_state_sb = kLowSad;
1335 else if (tmp_sse > avg_source_sse_threshold_high)
1336 x->content_state_sb = kHighSad;
1337}
1338
1339// Memset the mbmis at the current superblock to 0
1340void av1_reset_mbmi(CommonModeInfoParams *const mi_params, BLOCK_SIZE sb_size,
1341 int mi_row, int mi_col) {
1342 // size of sb in unit of mi (BLOCK_4X4)
1343 const int sb_size_mi = mi_size_wide[sb_size];
1344 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
1345 // size of sb in unit of allocated mi size
1346 const int sb_size_alloc_mi = mi_size_wide[sb_size] / mi_alloc_size_1d;
1347 assert(mi_params->mi_alloc_stride % sb_size_alloc_mi == 0 &&
1348 "mi is not allocated as a multiple of sb!");
1349 assert(mi_params->mi_stride % sb_size_mi == 0 &&
1350 "mi_grid_base is not allocated as a multiple of sb!");
1351
1352 const int mi_rows = mi_size_high[sb_size];
1353 for (int cur_mi_row = 0; cur_mi_row < mi_rows; cur_mi_row++) {
1354 assert(get_mi_grid_idx(mi_params, 0, mi_col + mi_alloc_size_1d) <
1355 mi_params->mi_stride);
1356 const int mi_grid_idx =
1357 get_mi_grid_idx(mi_params, mi_row + cur_mi_row, mi_col);
1358 const int alloc_mi_idx =
1359 get_alloc_mi_idx(mi_params, mi_row + cur_mi_row, mi_col);
1360 memset(&mi_params->mi_grid_base[mi_grid_idx], 0,
1361 sb_size_mi * sizeof(*mi_params->mi_grid_base));
1362 memset(&mi_params->tx_type_map[mi_grid_idx], 0,
1363 sb_size_mi * sizeof(*mi_params->tx_type_map));
1364 if (cur_mi_row % mi_alloc_size_1d == 0) {
1365 memset(&mi_params->mi_alloc[alloc_mi_idx], 0,
1366 sb_size_alloc_mi * sizeof(*mi_params->mi_alloc));
1367 }
1368 }
1369}
1370
1371void av1_backup_sb_state(SB_FIRST_PASS_STATS *sb_fp_stats, const AV1_COMP *cpi,
1372 ThreadData *td, const TileDataEnc *tile_data,
1373 int mi_row, int mi_col) {
1374 MACROBLOCK *x = &td->mb;
1375 MACROBLOCKD *xd = &x->e_mbd;
1376 const TileInfo *tile_info = &tile_data->tile_info;
1377
1378 const AV1_COMMON *cm = &cpi->common;
1379 const int num_planes = av1_num_planes(cm);
1380 const BLOCK_SIZE sb_size = cm->seq_params.sb_size;
1381
1382 xd->above_txfm_context =
1383 cm->above_contexts.txfm[tile_info->tile_row] + mi_col;
1384 xd->left_txfm_context =
1385 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
1386 av1_save_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size, num_planes);
1387
1388 sb_fp_stats->rd_count = cpi->td.rd_counts;
1389 sb_fp_stats->split_count = x->txfm_search_info.txb_split_count;
1390
1391 sb_fp_stats->fc = *td->counts;
1392
1393 memcpy(sb_fp_stats->inter_mode_rd_models, tile_data->inter_mode_rd_models,
1394 sizeof(sb_fp_stats->inter_mode_rd_models));
1395
1396 memcpy(sb_fp_stats->thresh_freq_fact, x->thresh_freq_fact,
1397 sizeof(sb_fp_stats->thresh_freq_fact));
1398
1399 const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
1400 sb_fp_stats->current_qindex =
1401 cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex;
1402
1403#if CONFIG_INTERNAL_STATS
1404 memcpy(sb_fp_stats->mode_chosen_counts, cpi->mode_chosen_counts,
1405 sizeof(sb_fp_stats->mode_chosen_counts));
1406#endif // CONFIG_INTERNAL_STATS
1407}
1408
1409void av1_restore_sb_state(const SB_FIRST_PASS_STATS *sb_fp_stats, AV1_COMP *cpi,
1410 ThreadData *td, TileDataEnc *tile_data, int mi_row,
1411 int mi_col) {
1412 MACROBLOCK *x = &td->mb;
1413
1414 const AV1_COMMON *cm = &cpi->common;
1415 const int num_planes = av1_num_planes(cm);
1416 const BLOCK_SIZE sb_size = cm->seq_params.sb_size;
1417
1418 av1_restore_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size,
1419 num_planes);
1420
1421 cpi->td.rd_counts = sb_fp_stats->rd_count;
1422 x->txfm_search_info.txb_split_count = sb_fp_stats->split_count;
1423
1424 *td->counts = sb_fp_stats->fc;
1425
1426 memcpy(tile_data->inter_mode_rd_models, sb_fp_stats->inter_mode_rd_models,
1427 sizeof(sb_fp_stats->inter_mode_rd_models));
1428 memcpy(x->thresh_freq_fact, sb_fp_stats->thresh_freq_fact,
1429 sizeof(sb_fp_stats->thresh_freq_fact));
1430
1431 const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
1432 cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex =
1433 sb_fp_stats->current_qindex;
1434
1435#if CONFIG_INTERNAL_STATS
1436 memcpy(cpi->mode_chosen_counts, sb_fp_stats->mode_chosen_counts,
1437 sizeof(sb_fp_stats->mode_chosen_counts));
1438#endif // CONFIG_INTERNAL_STATS
1439}
1440
1441// Update the rate costs of some symbols according to the frequency directed
1442// by speed features
1443void av1_set_cost_upd_freq(AV1_COMP *cpi, ThreadData *td,
1444 const TileInfo *const tile_info, const int mi_row,
1445 const int mi_col) {
1446 AV1_COMMON *const cm = &cpi->common;
1447 const int num_planes = av1_num_planes(cm);
1448 MACROBLOCK *const x = &td->mb;
1449 MACROBLOCKD *const xd = &x->e_mbd;
1450
1451 switch (cpi->oxcf.cost_upd_freq.coeff) {
1452 case COST_UPD_TILE: // Tile level
1453 if (mi_row != tile_info->mi_row_start) break;
1454 AOM_FALLTHROUGH_INTENDED;
1455 case COST_UPD_SBROW: // SB row level in tile
1456 if (mi_col != tile_info->mi_col_start) break;
1457 AOM_FALLTHROUGH_INTENDED;
1458 case COST_UPD_SB: // SB level
1459 if (cpi->sf.inter_sf.disable_sb_level_coeff_cost_upd &&
1460 mi_col != tile_info->mi_col_start)
1461 break;
1462 av1_fill_coeff_costs(&x->coeff_costs, xd->tile_ctx, num_planes);
1463 break;
1464 default: assert(0);
1465 }
1466
1467 switch (cpi->oxcf.cost_upd_freq.mode) {
1468 case COST_UPD_TILE: // Tile level
1469 if (mi_row != tile_info->mi_row_start) break;
1470 AOM_FALLTHROUGH_INTENDED;
1471 case COST_UPD_SBROW: // SB row level in tile
1472 if (mi_col != tile_info->mi_col_start) break;
1473 AOM_FALLTHROUGH_INTENDED;
1474 case COST_UPD_SB: // SB level
leolzhao3db7cca2021-01-26 16:53:07 -08001475#if CONFIG_SDP
1476 av1_fill_mode_rates(cm, xd, &x->mode_costs, xd->tile_ctx);
1477#else
Jayasanker Je9ad4752020-06-30 19:30:03 +05301478 av1_fill_mode_rates(cm, &x->mode_costs, xd->tile_ctx);
leolzhao3db7cca2021-01-26 16:53:07 -08001479#endif
Jayasanker Je9ad4752020-06-30 19:30:03 +05301480 break;
1481 default: assert(0);
1482 }
1483 switch (cpi->oxcf.cost_upd_freq.mv) {
1484 case COST_UPD_OFF: break;
1485 case COST_UPD_TILE: // Tile level
1486 if (mi_row != tile_info->mi_row_start) break;
1487 AOM_FALLTHROUGH_INTENDED;
1488 case COST_UPD_SBROW: // SB row level in tile
1489 if (mi_col != tile_info->mi_col_start) break;
1490 AOM_FALLTHROUGH_INTENDED;
1491 case COST_UPD_SB: // SB level
1492 if (cpi->sf.inter_sf.disable_sb_level_mv_cost_upd &&
1493 mi_col != tile_info->mi_col_start)
1494 break;
1495 av1_fill_mv_costs(xd->tile_ctx, cm->features.cur_frame_force_integer_mv,
1496 cm->features.allow_high_precision_mv, &x->mv_costs);
1497 break;
1498 default: assert(0);
1499 }
1500}