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