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