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