xref: /aosp_15_r20/external/libaom/av1/encoder/encodeframe.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, 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 <limits.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <float.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <stdbool.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "config/av1_rtcd.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/binary_codes_writer.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/aom_timer.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/aom_pthread.h"
27*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MISMATCH_DEBUG
28*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/debug_util.h"
29*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MISMATCH_DEBUG
30*77c1e3ccSAndroid Build Coastguard Worker 
31*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/cfl.h"
32*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/common.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/common_data.h"
34*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropy.h"
35*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropymode.h"
36*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/idct.h"
37*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/mv.h"
38*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/mvref_common.h"
39*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/pred_common.h"
40*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/quant_common.h"
41*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconintra.h"
42*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconinter.h"
43*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/seg_common.h"
44*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/tile_common.h"
45*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/warped_motion.h"
46*77c1e3ccSAndroid Build Coastguard Worker 
47*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/allintra_vis.h"
48*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_complexity.h"
49*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_cyclicrefresh.h"
50*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_variance.h"
51*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/global_motion_facade.h"
52*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe.h"
53*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe_utils.h"
54*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemb.h"
55*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemv.h"
56*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodetxb.h"
57*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/ethread.h"
58*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/extend.h"
59*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/intra_mode_search_utils.h"
60*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/ml.h"
61*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/motion_search_facade.h"
62*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_strategy.h"
63*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
64*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_model_weights.h"
65*77c1e3ccSAndroid Build Coastguard Worker #endif
66*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_search.h"
67*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/rd.h"
68*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/rdopt.h"
69*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/reconinter_enc.h"
70*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/segmentation.h"
71*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tokenize.h"
72*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tpl_model.h"
73*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/var_based_part.h"
74*77c1e3ccSAndroid Build Coastguard Worker 
75*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
76*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tune_vmaf.h"
77*77c1e3ccSAndroid Build Coastguard Worker #endif
78*77c1e3ccSAndroid Build Coastguard Worker 
79*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
80*77c1e3ccSAndroid Build Coastguard Worker // This is used as a reference when computing the source variance for the
81*77c1e3ccSAndroid Build Coastguard Worker //  purposes of activity masking.
82*77c1e3ccSAndroid Build Coastguard Worker // Eventually this should be replaced by custom no-reference routines,
83*77c1e3ccSAndroid Build Coastguard Worker //  which will be faster.
84*77c1e3ccSAndroid Build Coastguard Worker static const uint8_t AV1_VAR_OFFS[MAX_SB_SIZE] = {
85*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
86*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
87*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
88*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
89*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
90*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
91*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
92*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
93*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128
94*77c1e3ccSAndroid Build Coastguard Worker };
95*77c1e3ccSAndroid Build Coastguard Worker 
96*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
97*77c1e3ccSAndroid Build Coastguard Worker static const uint16_t AV1_HIGH_VAR_OFFS_8[MAX_SB_SIZE] = {
98*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
99*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
100*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
101*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
102*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
103*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
104*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
105*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
106*77c1e3ccSAndroid Build Coastguard Worker   128, 128, 128, 128, 128, 128, 128, 128
107*77c1e3ccSAndroid Build Coastguard Worker };
108*77c1e3ccSAndroid Build Coastguard Worker 
109*77c1e3ccSAndroid Build Coastguard Worker static const uint16_t AV1_HIGH_VAR_OFFS_10[MAX_SB_SIZE] = {
110*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
111*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
112*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
113*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
114*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
115*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
116*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
117*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
118*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
119*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
120*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
121*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
122*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
123*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
124*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
125*77c1e3ccSAndroid Build Coastguard Worker   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4
126*77c1e3ccSAndroid Build Coastguard Worker };
127*77c1e3ccSAndroid Build Coastguard Worker 
128*77c1e3ccSAndroid Build Coastguard Worker static const uint16_t AV1_HIGH_VAR_OFFS_12[MAX_SB_SIZE] = {
129*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
130*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
131*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
132*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
133*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
134*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
135*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
136*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
137*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
138*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
139*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
140*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
141*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
142*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
143*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
144*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
145*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
146*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
147*77c1e3ccSAndroid Build Coastguard Worker   128 * 16, 128 * 16
148*77c1e3ccSAndroid Build Coastguard Worker };
149*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_AV1_HIGHBITDEPTH
150*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
151*77c1e3ccSAndroid Build Coastguard Worker 
152*77c1e3ccSAndroid Build Coastguard Worker // For the given bit depth, returns a constant array used to assist the
153*77c1e3ccSAndroid Build Coastguard Worker // calculation of source block variance, which will then be used to decide
154*77c1e3ccSAndroid Build Coastguard Worker // adaptive quantizers.
get_var_offs(int use_hbd,int bd)155*77c1e3ccSAndroid Build Coastguard Worker static const uint8_t *get_var_offs(int use_hbd, int bd) {
156*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
157*77c1e3ccSAndroid Build Coastguard Worker   if (use_hbd) {
158*77c1e3ccSAndroid Build Coastguard Worker     assert(bd == 8 || bd == 10 || bd == 12);
159*77c1e3ccSAndroid Build Coastguard Worker     const int off_index = (bd - 8) >> 1;
160*77c1e3ccSAndroid Build Coastguard Worker     static const uint16_t *high_var_offs[3] = { AV1_HIGH_VAR_OFFS_8,
161*77c1e3ccSAndroid Build Coastguard Worker                                                 AV1_HIGH_VAR_OFFS_10,
162*77c1e3ccSAndroid Build Coastguard Worker                                                 AV1_HIGH_VAR_OFFS_12 };
163*77c1e3ccSAndroid Build Coastguard Worker     return CONVERT_TO_BYTEPTR(high_var_offs[off_index]);
164*77c1e3ccSAndroid Build Coastguard Worker   }
165*77c1e3ccSAndroid Build Coastguard Worker #else
166*77c1e3ccSAndroid Build Coastguard Worker   (void)use_hbd;
167*77c1e3ccSAndroid Build Coastguard Worker   (void)bd;
168*77c1e3ccSAndroid Build Coastguard Worker   assert(!use_hbd);
169*77c1e3ccSAndroid Build Coastguard Worker #endif
170*77c1e3ccSAndroid Build Coastguard Worker   assert(bd == 8);
171*77c1e3ccSAndroid Build Coastguard Worker   return AV1_VAR_OFFS;
172*77c1e3ccSAndroid Build Coastguard Worker }
173*77c1e3ccSAndroid Build Coastguard Worker 
av1_init_rtc_counters(MACROBLOCK * const x)174*77c1e3ccSAndroid Build Coastguard Worker void av1_init_rtc_counters(MACROBLOCK *const x) {
175*77c1e3ccSAndroid Build Coastguard Worker   av1_init_cyclic_refresh_counters(x);
176*77c1e3ccSAndroid Build Coastguard Worker   x->cnt_zeromv = 0;
177*77c1e3ccSAndroid Build Coastguard Worker }
178*77c1e3ccSAndroid Build Coastguard Worker 
av1_accumulate_rtc_counters(AV1_COMP * cpi,const MACROBLOCK * const x)179*77c1e3ccSAndroid Build Coastguard Worker void av1_accumulate_rtc_counters(AV1_COMP *cpi, const MACROBLOCK *const x) {
180*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ)
181*77c1e3ccSAndroid Build Coastguard Worker     av1_accumulate_cyclic_refresh_counters(cpi->cyclic_refresh, x);
182*77c1e3ccSAndroid Build Coastguard Worker   cpi->rc.cnt_zeromv += x->cnt_zeromv;
183*77c1e3ccSAndroid Build Coastguard Worker }
184*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_perpixel_variance(const AV1_COMP * cpi,const MACROBLOCKD * xd,const struct buf_2d * ref,BLOCK_SIZE bsize,int plane,int use_hbd)185*77c1e3ccSAndroid Build Coastguard Worker unsigned int av1_get_perpixel_variance(const AV1_COMP *cpi,
186*77c1e3ccSAndroid Build Coastguard Worker                                        const MACROBLOCKD *xd,
187*77c1e3ccSAndroid Build Coastguard Worker                                        const struct buf_2d *ref,
188*77c1e3ccSAndroid Build Coastguard Worker                                        BLOCK_SIZE bsize, int plane,
189*77c1e3ccSAndroid Build Coastguard Worker                                        int use_hbd) {
190*77c1e3ccSAndroid Build Coastguard Worker   const int subsampling_x = xd->plane[plane].subsampling_x;
191*77c1e3ccSAndroid Build Coastguard Worker   const int subsampling_y = xd->plane[plane].subsampling_y;
192*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE plane_bsize =
193*77c1e3ccSAndroid Build Coastguard Worker       get_plane_block_size(bsize, subsampling_x, subsampling_y);
194*77c1e3ccSAndroid Build Coastguard Worker   unsigned int sse;
195*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int var = cpi->ppi->fn_ptr[plane_bsize].vf(
196*77c1e3ccSAndroid Build Coastguard Worker       ref->buf, ref->stride, get_var_offs(use_hbd, xd->bd), 0, &sse);
197*77c1e3ccSAndroid Build Coastguard Worker   return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[plane_bsize]);
198*77c1e3ccSAndroid Build Coastguard Worker }
199*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_perpixel_variance_facade(const AV1_COMP * cpi,const MACROBLOCKD * xd,const struct buf_2d * ref,BLOCK_SIZE bsize,int plane)200*77c1e3ccSAndroid Build Coastguard Worker unsigned int av1_get_perpixel_variance_facade(const AV1_COMP *cpi,
201*77c1e3ccSAndroid Build Coastguard Worker                                               const MACROBLOCKD *xd,
202*77c1e3ccSAndroid Build Coastguard Worker                                               const struct buf_2d *ref,
203*77c1e3ccSAndroid Build Coastguard Worker                                               BLOCK_SIZE bsize, int plane) {
204*77c1e3ccSAndroid Build Coastguard Worker   const int use_hbd = is_cur_buf_hbd(xd);
205*77c1e3ccSAndroid Build Coastguard Worker   return av1_get_perpixel_variance(cpi, xd, ref, bsize, plane, use_hbd);
206*77c1e3ccSAndroid Build Coastguard Worker }
207*77c1e3ccSAndroid Build Coastguard Worker 
av1_setup_src_planes(MACROBLOCK * x,const YV12_BUFFER_CONFIG * src,int mi_row,int mi_col,const int num_planes,BLOCK_SIZE bsize)208*77c1e3ccSAndroid Build Coastguard Worker void av1_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
209*77c1e3ccSAndroid Build Coastguard Worker                           int mi_row, int mi_col, const int num_planes,
210*77c1e3ccSAndroid Build Coastguard Worker                           BLOCK_SIZE bsize) {
211*77c1e3ccSAndroid Build Coastguard Worker   // Set current frame pointer.
212*77c1e3ccSAndroid Build Coastguard Worker   x->e_mbd.cur_buf = src;
213*77c1e3ccSAndroid Build Coastguard Worker 
214*77c1e3ccSAndroid Build Coastguard Worker   // We use AOMMIN(num_planes, MAX_MB_PLANE) instead of num_planes to quiet
215*77c1e3ccSAndroid Build Coastguard Worker   // the static analysis warnings.
216*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < AOMMIN(num_planes, MAX_MB_PLANE); i++) {
217*77c1e3ccSAndroid Build Coastguard Worker     const int is_uv = i > 0;
218*77c1e3ccSAndroid Build Coastguard Worker     setup_pred_plane(
219*77c1e3ccSAndroid Build Coastguard Worker         &x->plane[i].src, bsize, src->buffers[i], src->crop_widths[is_uv],
220*77c1e3ccSAndroid Build Coastguard Worker         src->crop_heights[is_uv], src->strides[is_uv], mi_row, mi_col, NULL,
221*77c1e3ccSAndroid Build Coastguard Worker         x->e_mbd.plane[i].subsampling_x, x->e_mbd.plane[i].subsampling_y);
222*77c1e3ccSAndroid Build Coastguard Worker   }
223*77c1e3ccSAndroid Build Coastguard Worker }
224*77c1e3ccSAndroid Build Coastguard Worker 
225*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
226*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Assigns different quantization parameters to each super
227*77c1e3ccSAndroid Build Coastguard Worker  * block based on its TPL weight.
228*77c1e3ccSAndroid Build Coastguard Worker  *
229*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup tpl_modelling
230*77c1e3ccSAndroid Build Coastguard Worker  *
231*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]     cpi         Top level encoder instance structure
232*77c1e3ccSAndroid Build Coastguard Worker  * \param[in,out] td          Thread data structure
233*77c1e3ccSAndroid Build Coastguard Worker  * \param[in,out] x           Macro block level data for this block.
234*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]     tile_info   Tile infromation / identification
235*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]     mi_row      Block row (in "MI_SIZE" units) index
236*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]     mi_col      Block column (in "MI_SIZE" units) index
237*77c1e3ccSAndroid Build Coastguard Worker  * \param[out]    num_planes  Number of image planes (e.g. Y,U,V)
238*77c1e3ccSAndroid Build Coastguard Worker  *
239*77c1e3ccSAndroid Build Coastguard Worker  * \remark No return value but updates macroblock and thread data
240*77c1e3ccSAndroid Build Coastguard Worker  * related to the q / q delta to be used.
241*77c1e3ccSAndroid Build Coastguard Worker  */
setup_delta_q(AV1_COMP * const cpi,ThreadData * td,MACROBLOCK * const x,const TileInfo * const tile_info,int mi_row,int mi_col,int num_planes)242*77c1e3ccSAndroid Build Coastguard Worker static inline void setup_delta_q(AV1_COMP *const cpi, ThreadData *td,
243*77c1e3ccSAndroid Build Coastguard Worker                                  MACROBLOCK *const x,
244*77c1e3ccSAndroid Build Coastguard Worker                                  const TileInfo *const tile_info, int mi_row,
245*77c1e3ccSAndroid Build Coastguard Worker                                  int mi_col, int num_planes) {
246*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
247*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
248*77c1e3ccSAndroid Build Coastguard Worker   const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
249*77c1e3ccSAndroid Build Coastguard Worker   assert(delta_q_info->delta_q_present_flag);
250*77c1e3ccSAndroid Build Coastguard Worker 
251*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
252*77c1e3ccSAndroid Build Coastguard Worker   // Delta-q modulation based on variance
253*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_src_planes(x, cpi->source, mi_row, mi_col, num_planes, sb_size);
254*77c1e3ccSAndroid Build Coastguard Worker 
255*77c1e3ccSAndroid Build Coastguard Worker   const int delta_q_res = delta_q_info->delta_q_res;
256*77c1e3ccSAndroid Build Coastguard Worker   int current_qindex = cm->quant_params.base_qindex;
257*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->use_ducky_encode && cpi->ducky_encode_info.frame_info.qp_mode ==
258*77c1e3ccSAndroid Build Coastguard Worker                                    DUCKY_ENCODE_FRAME_MODE_QINDEX) {
259*77c1e3ccSAndroid Build Coastguard Worker     const int sb_row = mi_row >> cm->seq_params->mib_size_log2;
260*77c1e3ccSAndroid Build Coastguard Worker     const int sb_col = mi_col >> cm->seq_params->mib_size_log2;
261*77c1e3ccSAndroid Build Coastguard Worker     const int sb_cols =
262*77c1e3ccSAndroid Build Coastguard Worker         CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, cm->seq_params->mib_size_log2);
263*77c1e3ccSAndroid Build Coastguard Worker     const int sb_index = sb_row * sb_cols + sb_col;
264*77c1e3ccSAndroid Build Coastguard Worker     current_qindex =
265*77c1e3ccSAndroid Build Coastguard Worker         cpi->ducky_encode_info.frame_info.superblock_encode_qindex[sb_index];
266*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL) {
267*77c1e3ccSAndroid Build Coastguard Worker     if (DELTA_Q_PERCEPTUAL_MODULATION == 1) {
268*77c1e3ccSAndroid Build Coastguard Worker       const int block_wavelet_energy_level =
269*77c1e3ccSAndroid Build Coastguard Worker           av1_block_wavelet_energy_level(cpi, x, sb_size);
270*77c1e3ccSAndroid Build Coastguard Worker       x->sb_energy_level = block_wavelet_energy_level;
271*77c1e3ccSAndroid Build Coastguard Worker       current_qindex = av1_compute_q_from_energy_level_deltaq_mode(
272*77c1e3ccSAndroid Build Coastguard Worker           cpi, block_wavelet_energy_level);
273*77c1e3ccSAndroid Build Coastguard Worker     } else {
274*77c1e3ccSAndroid Build Coastguard Worker       const int block_var_level = av1_log_block_var(cpi, x, sb_size);
275*77c1e3ccSAndroid Build Coastguard Worker       x->sb_energy_level = block_var_level;
276*77c1e3ccSAndroid Build Coastguard Worker       current_qindex =
277*77c1e3ccSAndroid Build Coastguard Worker           av1_compute_q_from_energy_level_deltaq_mode(cpi, block_var_level);
278*77c1e3ccSAndroid Build Coastguard Worker     }
279*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_OBJECTIVE &&
280*77c1e3ccSAndroid Build Coastguard Worker              cpi->oxcf.algo_cfg.enable_tpl_model) {
281*77c1e3ccSAndroid Build Coastguard Worker     // Setup deltaq based on tpl stats
282*77c1e3ccSAndroid Build Coastguard Worker     current_qindex =
283*77c1e3ccSAndroid Build Coastguard Worker         av1_get_q_for_deltaq_objective(cpi, td, NULL, sb_size, mi_row, mi_col);
284*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL_AI) {
285*77c1e3ccSAndroid Build Coastguard Worker     current_qindex = av1_get_sbq_perceptual_ai(cpi, sb_size, mi_row, mi_col);
286*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_USER_RATING_BASED) {
287*77c1e3ccSAndroid Build Coastguard Worker     current_qindex = av1_get_sbq_user_rating_based(cpi, mi_row, mi_col);
288*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->oxcf.q_cfg.enable_hdr_deltaq) {
289*77c1e3ccSAndroid Build Coastguard Worker     current_qindex = av1_get_q_for_hdr(cpi, x, sb_size, mi_row, mi_col);
290*77c1e3ccSAndroid Build Coastguard Worker   }
291*77c1e3ccSAndroid Build Coastguard Worker 
292*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult_cur_qindex = current_qindex;
293*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
294*77c1e3ccSAndroid Build Coastguard Worker   const int adjusted_qindex = av1_adjust_q_from_delta_q_res(
295*77c1e3ccSAndroid Build Coastguard Worker       delta_q_res, xd->current_base_qindex, current_qindex);
296*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->use_ducky_encode) {
297*77c1e3ccSAndroid Build Coastguard Worker     assert(adjusted_qindex == current_qindex);
298*77c1e3ccSAndroid Build Coastguard Worker   }
299*77c1e3ccSAndroid Build Coastguard Worker   current_qindex = adjusted_qindex;
300*77c1e3ccSAndroid Build Coastguard Worker 
301*77c1e3ccSAndroid Build Coastguard Worker   x->delta_qindex = current_qindex - cm->quant_params.base_qindex;
302*77c1e3ccSAndroid Build Coastguard Worker   x->rdmult_delta_qindex = x->delta_qindex;
303*77c1e3ccSAndroid Build Coastguard Worker 
304*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
305*77c1e3ccSAndroid Build Coastguard Worker   xd->mi[0]->current_qindex = current_qindex;
306*77c1e3ccSAndroid Build Coastguard Worker   av1_init_plane_quantizers(cpi, x, xd->mi[0]->segment_id, 0);
307*77c1e3ccSAndroid Build Coastguard Worker 
308*77c1e3ccSAndroid Build Coastguard Worker   // keep track of any non-zero delta-q used
309*77c1e3ccSAndroid Build Coastguard Worker   td->deltaq_used |= (x->delta_qindex != 0);
310*77c1e3ccSAndroid Build Coastguard Worker 
311*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.tool_cfg.enable_deltalf_mode) {
312*77c1e3ccSAndroid Build Coastguard Worker     const int delta_lf_res = delta_q_info->delta_lf_res;
313*77c1e3ccSAndroid Build Coastguard Worker     const int lfmask = ~(delta_lf_res - 1);
314*77c1e3ccSAndroid Build Coastguard Worker     const int delta_lf_from_base =
315*77c1e3ccSAndroid Build Coastguard Worker         ((x->delta_qindex / 4 + delta_lf_res / 2) & lfmask);
316*77c1e3ccSAndroid Build Coastguard Worker     const int8_t delta_lf =
317*77c1e3ccSAndroid Build Coastguard Worker         (int8_t)clamp(delta_lf_from_base, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
318*77c1e3ccSAndroid Build Coastguard Worker     const int frame_lf_count =
319*77c1e3ccSAndroid Build Coastguard Worker         av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
320*77c1e3ccSAndroid Build Coastguard Worker     const int mib_size = cm->seq_params->mib_size;
321*77c1e3ccSAndroid Build Coastguard Worker 
322*77c1e3ccSAndroid Build Coastguard Worker     // pre-set the delta lf for loop filter. Note that this value is set
323*77c1e3ccSAndroid Build Coastguard Worker     // before mi is assigned for each block in current superblock
324*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < AOMMIN(mib_size, mi_params->mi_rows - mi_row); j++) {
325*77c1e3ccSAndroid Build Coastguard Worker       for (int k = 0; k < AOMMIN(mib_size, mi_params->mi_cols - mi_col); k++) {
326*77c1e3ccSAndroid Build Coastguard Worker         const int grid_idx = get_mi_grid_idx(mi_params, mi_row + j, mi_col + k);
327*77c1e3ccSAndroid Build Coastguard Worker         mi_params->mi_alloc[grid_idx].delta_lf_from_base = delta_lf;
328*77c1e3ccSAndroid Build Coastguard Worker         for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
329*77c1e3ccSAndroid Build Coastguard Worker           mi_params->mi_alloc[grid_idx].delta_lf[lf_id] = delta_lf;
330*77c1e3ccSAndroid Build Coastguard Worker         }
331*77c1e3ccSAndroid Build Coastguard Worker       }
332*77c1e3ccSAndroid Build Coastguard Worker     }
333*77c1e3ccSAndroid Build Coastguard Worker   }
334*77c1e3ccSAndroid Build Coastguard Worker }
335*77c1e3ccSAndroid Build Coastguard Worker 
init_ref_frame_space(AV1_COMP * cpi,ThreadData * td,int mi_row,int mi_col)336*77c1e3ccSAndroid Build Coastguard Worker static void init_ref_frame_space(AV1_COMP *cpi, ThreadData *td, int mi_row,
337*77c1e3ccSAndroid Build Coastguard Worker                                  int mi_col) {
338*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *cm = &cpi->common;
339*77c1e3ccSAndroid Build Coastguard Worker   const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
340*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
341*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *x = &td->mb;
342*77c1e3ccSAndroid Build Coastguard Worker   const int frame_idx = cpi->gf_frame_index;
343*77c1e3ccSAndroid Build Coastguard Worker   TplParams *const tpl_data = &cpi->ppi->tpl_data;
344*77c1e3ccSAndroid Build Coastguard Worker   const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
345*77c1e3ccSAndroid Build Coastguard Worker 
346*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(x->tpl_keep_ref_frame);
347*77c1e3ccSAndroid Build Coastguard Worker 
348*77c1e3ccSAndroid Build Coastguard Worker   if (!av1_tpl_stats_ready(tpl_data, frame_idx)) return;
349*77c1e3ccSAndroid Build Coastguard Worker   if (!is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) return;
350*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return;
351*77c1e3ccSAndroid Build Coastguard Worker 
352*77c1e3ccSAndroid Build Coastguard Worker   const int is_overlay =
353*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.update_type[frame_idx] == OVERLAY_UPDATE;
354*77c1e3ccSAndroid Build Coastguard Worker   if (is_overlay) {
355*77c1e3ccSAndroid Build Coastguard Worker     memset(x->tpl_keep_ref_frame, 1, sizeof(x->tpl_keep_ref_frame));
356*77c1e3ccSAndroid Build Coastguard Worker     return;
357*77c1e3ccSAndroid Build Coastguard Worker   }
358*77c1e3ccSAndroid Build Coastguard Worker 
359*77c1e3ccSAndroid Build Coastguard Worker   TplDepFrame *tpl_frame = &tpl_data->tpl_frame[frame_idx];
360*77c1e3ccSAndroid Build Coastguard Worker   TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
361*77c1e3ccSAndroid Build Coastguard Worker   const int tpl_stride = tpl_frame->stride;
362*77c1e3ccSAndroid Build Coastguard Worker   int64_t inter_cost[INTER_REFS_PER_FRAME] = { 0 };
363*77c1e3ccSAndroid Build Coastguard Worker   const int step = 1 << block_mis_log2;
364*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
365*77c1e3ccSAndroid Build Coastguard Worker 
366*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row_end =
367*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mi_size_high[sb_size] + mi_row, mi_params->mi_rows);
368*77c1e3ccSAndroid Build Coastguard Worker   const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
369*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col_sr =
370*77c1e3ccSAndroid Build Coastguard Worker       coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
371*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col_end_sr =
372*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(coded_to_superres_mi(mi_col + mi_size_wide[sb_size],
373*77c1e3ccSAndroid Build Coastguard Worker                                   cm->superres_scale_denominator),
374*77c1e3ccSAndroid Build Coastguard Worker              mi_cols_sr);
375*77c1e3ccSAndroid Build Coastguard Worker   const int row_step = step;
376*77c1e3ccSAndroid Build Coastguard Worker   const int col_step_sr =
377*77c1e3ccSAndroid Build Coastguard Worker       coded_to_superres_mi(step, cm->superres_scale_denominator);
378*77c1e3ccSAndroid Build Coastguard Worker   for (int row = mi_row; row < mi_row_end; row += row_step) {
379*77c1e3ccSAndroid Build Coastguard Worker     for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
380*77c1e3ccSAndroid Build Coastguard Worker       const TplDepStats *this_stats =
381*77c1e3ccSAndroid Build Coastguard Worker           &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
382*77c1e3ccSAndroid Build Coastguard Worker       int64_t tpl_pred_error[INTER_REFS_PER_FRAME] = { 0 };
383*77c1e3ccSAndroid Build Coastguard Worker       // Find the winner ref frame idx for the current block
384*77c1e3ccSAndroid Build Coastguard Worker       int64_t best_inter_cost = this_stats->pred_error[0];
385*77c1e3ccSAndroid Build Coastguard Worker       int best_rf_idx = 0;
386*77c1e3ccSAndroid Build Coastguard Worker       for (int idx = 1; idx < INTER_REFS_PER_FRAME; ++idx) {
387*77c1e3ccSAndroid Build Coastguard Worker         if ((this_stats->pred_error[idx] < best_inter_cost) &&
388*77c1e3ccSAndroid Build Coastguard Worker             (this_stats->pred_error[idx] != 0)) {
389*77c1e3ccSAndroid Build Coastguard Worker           best_inter_cost = this_stats->pred_error[idx];
390*77c1e3ccSAndroid Build Coastguard Worker           best_rf_idx = idx;
391*77c1e3ccSAndroid Build Coastguard Worker         }
392*77c1e3ccSAndroid Build Coastguard Worker       }
393*77c1e3ccSAndroid Build Coastguard Worker       // tpl_pred_error is the pred_error reduction of best_ref w.r.t.
394*77c1e3ccSAndroid Build Coastguard Worker       // LAST_FRAME.
395*77c1e3ccSAndroid Build Coastguard Worker       tpl_pred_error[best_rf_idx] = this_stats->pred_error[best_rf_idx] -
396*77c1e3ccSAndroid Build Coastguard Worker                                     this_stats->pred_error[LAST_FRAME - 1];
397*77c1e3ccSAndroid Build Coastguard Worker 
398*77c1e3ccSAndroid Build Coastguard Worker       for (int rf_idx = 1; rf_idx < INTER_REFS_PER_FRAME; ++rf_idx)
399*77c1e3ccSAndroid Build Coastguard Worker         inter_cost[rf_idx] += tpl_pred_error[rf_idx];
400*77c1e3ccSAndroid Build Coastguard Worker     }
401*77c1e3ccSAndroid Build Coastguard Worker   }
402*77c1e3ccSAndroid Build Coastguard Worker 
403*77c1e3ccSAndroid Build Coastguard Worker   int rank_index[INTER_REFS_PER_FRAME - 1];
404*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < INTER_REFS_PER_FRAME - 1; ++idx) {
405*77c1e3ccSAndroid Build Coastguard Worker     rank_index[idx] = idx + 1;
406*77c1e3ccSAndroid Build Coastguard Worker     for (int i = idx; i > 0; --i) {
407*77c1e3ccSAndroid Build Coastguard Worker       if (inter_cost[rank_index[i - 1]] > inter_cost[rank_index[i]]) {
408*77c1e3ccSAndroid Build Coastguard Worker         const int tmp = rank_index[i - 1];
409*77c1e3ccSAndroid Build Coastguard Worker         rank_index[i - 1] = rank_index[i];
410*77c1e3ccSAndroid Build Coastguard Worker         rank_index[i] = tmp;
411*77c1e3ccSAndroid Build Coastguard Worker       }
412*77c1e3ccSAndroid Build Coastguard Worker     }
413*77c1e3ccSAndroid Build Coastguard Worker   }
414*77c1e3ccSAndroid Build Coastguard Worker 
415*77c1e3ccSAndroid Build Coastguard Worker   x->tpl_keep_ref_frame[INTRA_FRAME] = 1;
416*77c1e3ccSAndroid Build Coastguard Worker   x->tpl_keep_ref_frame[LAST_FRAME] = 1;
417*77c1e3ccSAndroid Build Coastguard Worker 
418*77c1e3ccSAndroid Build Coastguard Worker   int cutoff_ref = 0;
419*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < INTER_REFS_PER_FRAME - 1; ++idx) {
420*77c1e3ccSAndroid Build Coastguard Worker     x->tpl_keep_ref_frame[rank_index[idx] + LAST_FRAME] = 1;
421*77c1e3ccSAndroid Build Coastguard Worker     if (idx > 2) {
422*77c1e3ccSAndroid Build Coastguard Worker       if (!cutoff_ref) {
423*77c1e3ccSAndroid Build Coastguard Worker         // If the predictive coding gains are smaller than the previous more
424*77c1e3ccSAndroid Build Coastguard Worker         // relevant frame over certain amount, discard this frame and all the
425*77c1e3ccSAndroid Build Coastguard Worker         // frames afterwards.
426*77c1e3ccSAndroid Build Coastguard Worker         if (llabs(inter_cost[rank_index[idx]]) <
427*77c1e3ccSAndroid Build Coastguard Worker                 llabs(inter_cost[rank_index[idx - 1]]) / 8 ||
428*77c1e3ccSAndroid Build Coastguard Worker             inter_cost[rank_index[idx]] == 0)
429*77c1e3ccSAndroid Build Coastguard Worker           cutoff_ref = 1;
430*77c1e3ccSAndroid Build Coastguard Worker       }
431*77c1e3ccSAndroid Build Coastguard Worker 
432*77c1e3ccSAndroid Build Coastguard Worker       if (cutoff_ref) x->tpl_keep_ref_frame[rank_index[idx] + LAST_FRAME] = 0;
433*77c1e3ccSAndroid Build Coastguard Worker     }
434*77c1e3ccSAndroid Build Coastguard Worker   }
435*77c1e3ccSAndroid Build Coastguard Worker }
436*77c1e3ccSAndroid Build Coastguard Worker 
adjust_rdmult_tpl_model(AV1_COMP * cpi,MACROBLOCK * x,int mi_row,int mi_col)437*77c1e3ccSAndroid Build Coastguard Worker static inline void adjust_rdmult_tpl_model(AV1_COMP *cpi, MACROBLOCK *x,
438*77c1e3ccSAndroid Build Coastguard Worker                                            int mi_row, int mi_col) {
439*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sb_size = cpi->common.seq_params->sb_size;
440*77c1e3ccSAndroid Build Coastguard Worker   const int orig_rdmult = cpi->rd.RDMULT;
441*77c1e3ccSAndroid Build Coastguard Worker 
442*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(cpi->ppi->gf_group.size > 0,
443*77c1e3ccSAndroid Build Coastguard Worker                  cpi->gf_frame_index < cpi->ppi->gf_group.size));
444*77c1e3ccSAndroid Build Coastguard Worker   const int gf_group_index = cpi->gf_frame_index;
445*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.algo_cfg.enable_tpl_model && cpi->oxcf.q_cfg.aq_mode == NO_AQ &&
446*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q && gf_group_index > 0 &&
447*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.update_type[gf_group_index] == ARF_UPDATE) {
448*77c1e3ccSAndroid Build Coastguard Worker     const int dr =
449*77c1e3ccSAndroid Build Coastguard Worker         av1_get_rdmult_delta(cpi, sb_size, mi_row, mi_col, orig_rdmult);
450*77c1e3ccSAndroid Build Coastguard Worker     x->rdmult = dr;
451*77c1e3ccSAndroid Build Coastguard Worker   }
452*77c1e3ccSAndroid Build Coastguard Worker }
453*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
454*77c1e3ccSAndroid Build Coastguard Worker 
455*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RT_ML_PARTITIONING
456*77c1e3ccSAndroid Build Coastguard Worker // Get a prediction(stored in x->est_pred) for the whole superblock.
get_estimated_pred(AV1_COMP * cpi,const TileInfo * const tile,MACROBLOCK * x,int mi_row,int mi_col)457*77c1e3ccSAndroid Build Coastguard Worker static void get_estimated_pred(AV1_COMP *cpi, const TileInfo *const tile,
458*77c1e3ccSAndroid Build Coastguard Worker                                MACROBLOCK *x, int mi_row, int mi_col) {
459*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
460*77c1e3ccSAndroid Build Coastguard Worker   const int is_key_frame = frame_is_intra_only(cm);
461*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &x->e_mbd;
462*77c1e3ccSAndroid Build Coastguard Worker 
463*77c1e3ccSAndroid Build Coastguard Worker   // TODO(kyslov) Extend to 128x128
464*77c1e3ccSAndroid Build Coastguard Worker   assert(cm->seq_params->sb_size == BLOCK_64X64);
465*77c1e3ccSAndroid Build Coastguard Worker 
466*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets(cpi, tile, x, mi_row, mi_col, BLOCK_64X64);
467*77c1e3ccSAndroid Build Coastguard Worker 
468*77c1e3ccSAndroid Build Coastguard Worker   if (!is_key_frame) {
469*77c1e3ccSAndroid Build Coastguard Worker     MB_MODE_INFO *mi = xd->mi[0];
470*77c1e3ccSAndroid Build Coastguard Worker     const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, LAST_FRAME);
471*77c1e3ccSAndroid Build Coastguard Worker 
472*77c1e3ccSAndroid Build Coastguard Worker     assert(yv12 != NULL);
473*77c1e3ccSAndroid Build Coastguard Worker 
474*77c1e3ccSAndroid Build Coastguard Worker     av1_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
475*77c1e3ccSAndroid Build Coastguard Worker                          get_ref_scale_factors(cm, LAST_FRAME), 1);
476*77c1e3ccSAndroid Build Coastguard Worker     mi->ref_frame[0] = LAST_FRAME;
477*77c1e3ccSAndroid Build Coastguard Worker     mi->ref_frame[1] = NONE;
478*77c1e3ccSAndroid Build Coastguard Worker     mi->bsize = BLOCK_64X64;
479*77c1e3ccSAndroid Build Coastguard Worker     mi->mv[0].as_int = 0;
480*77c1e3ccSAndroid Build Coastguard Worker     mi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
481*77c1e3ccSAndroid Build Coastguard Worker 
482*77c1e3ccSAndroid Build Coastguard Worker     set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
483*77c1e3ccSAndroid Build Coastguard Worker 
484*77c1e3ccSAndroid Build Coastguard Worker     xd->plane[0].dst.buf = x->est_pred;
485*77c1e3ccSAndroid Build Coastguard Worker     xd->plane[0].dst.stride = 64;
486*77c1e3ccSAndroid Build Coastguard Worker     av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
487*77c1e3ccSAndroid Build Coastguard Worker   } else {
488*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
489*77c1e3ccSAndroid Build Coastguard Worker     switch (xd->bd) {
490*77c1e3ccSAndroid Build Coastguard Worker       case 8: memset(x->est_pred, 128, 64 * 64 * sizeof(x->est_pred[0])); break;
491*77c1e3ccSAndroid Build Coastguard Worker       case 10:
492*77c1e3ccSAndroid Build Coastguard Worker         memset(x->est_pred, 128 * 4, 64 * 64 * sizeof(x->est_pred[0]));
493*77c1e3ccSAndroid Build Coastguard Worker         break;
494*77c1e3ccSAndroid Build Coastguard Worker       case 12:
495*77c1e3ccSAndroid Build Coastguard Worker         memset(x->est_pred, 128 * 16, 64 * 64 * sizeof(x->est_pred[0]));
496*77c1e3ccSAndroid Build Coastguard Worker         break;
497*77c1e3ccSAndroid Build Coastguard Worker     }
498*77c1e3ccSAndroid Build Coastguard Worker #else
499*77c1e3ccSAndroid Build Coastguard Worker     memset(x->est_pred, 128, 64 * 64 * sizeof(x->est_pred[0]));
500*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_VP9_HIGHBITDEPTH
501*77c1e3ccSAndroid Build Coastguard Worker   }
502*77c1e3ccSAndroid Build Coastguard Worker }
503*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_RT_ML_PARTITIONING
504*77c1e3ccSAndroid Build Coastguard Worker 
505*77c1e3ccSAndroid Build Coastguard Worker #define AVG_CDF_WEIGHT_LEFT 3
506*77c1e3ccSAndroid Build Coastguard Worker #define AVG_CDF_WEIGHT_TOP_RIGHT 1
507*77c1e3ccSAndroid Build Coastguard Worker 
508*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Encode a superblock (minimal RD search involved)
509*77c1e3ccSAndroid Build Coastguard Worker  *
510*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
511*77c1e3ccSAndroid Build Coastguard Worker  * Encodes the superblock by a pre-determined partition pattern, only minor
512*77c1e3ccSAndroid Build Coastguard Worker  * rd-based searches are allowed to adjust the initial pattern. It is only used
513*77c1e3ccSAndroid Build Coastguard Worker  * by realtime encoding.
514*77c1e3ccSAndroid Build Coastguard Worker  */
encode_nonrd_sb(AV1_COMP * cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,const int mi_row,const int mi_col,const int seg_skip)515*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_nonrd_sb(AV1_COMP *cpi, ThreadData *td,
516*77c1e3ccSAndroid Build Coastguard Worker                                    TileDataEnc *tile_data, TokenExtra **tp,
517*77c1e3ccSAndroid Build Coastguard Worker                                    const int mi_row, const int mi_col,
518*77c1e3ccSAndroid Build Coastguard Worker                                    const int seg_skip) {
519*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
520*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
521*77c1e3ccSAndroid Build Coastguard Worker   const SPEED_FEATURES *const sf = &cpi->sf;
522*77c1e3ccSAndroid Build Coastguard Worker   const TileInfo *const tile_info = &tile_data->tile_info;
523*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO **mi = cm->mi_params.mi_grid_base +
524*77c1e3ccSAndroid Build Coastguard Worker                       get_mi_grid_idx(&cm->mi_params, mi_row, mi_col);
525*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
526*77c1e3ccSAndroid Build Coastguard Worker   PC_TREE *const pc_root = td->pc_root;
527*77c1e3ccSAndroid Build Coastguard Worker 
528*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RT_ML_PARTITIONING
529*77c1e3ccSAndroid Build Coastguard Worker   if (sf->part_sf.partition_search_type == ML_BASED_PARTITION) {
530*77c1e3ccSAndroid Build Coastguard Worker     RD_STATS dummy_rdc;
531*77c1e3ccSAndroid Build Coastguard Worker     get_estimated_pred(cpi, tile_info, x, mi_row, mi_col);
532*77c1e3ccSAndroid Build Coastguard Worker     av1_nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col,
533*77c1e3ccSAndroid Build Coastguard Worker                              BLOCK_64X64, &dummy_rdc, 1, INT64_MAX, pc_root);
534*77c1e3ccSAndroid Build Coastguard Worker     return;
535*77c1e3ccSAndroid Build Coastguard Worker   }
536*77c1e3ccSAndroid Build Coastguard Worker #endif
537*77c1e3ccSAndroid Build Coastguard Worker   // Set the partition
538*77c1e3ccSAndroid Build Coastguard Worker   if (sf->part_sf.partition_search_type == FIXED_PARTITION || seg_skip ||
539*77c1e3ccSAndroid Build Coastguard Worker       (sf->rt_sf.use_fast_fixed_part && x->sb_force_fixed_part == 1 &&
540*77c1e3ccSAndroid Build Coastguard Worker        (!frame_is_intra_only(cm) &&
541*77c1e3ccSAndroid Build Coastguard Worker         (!cpi->ppi->use_svc ||
542*77c1e3ccSAndroid Build Coastguard Worker          !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame)))) {
543*77c1e3ccSAndroid Build Coastguard Worker     // set a fixed-size partition
544*77c1e3ccSAndroid Build Coastguard Worker     av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
545*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE bsize_select = sf->part_sf.fixed_partition_size;
546*77c1e3ccSAndroid Build Coastguard Worker     if (sf->rt_sf.use_fast_fixed_part &&
547*77c1e3ccSAndroid Build Coastguard Worker         x->content_state_sb.source_sad_nonrd < kLowSad) {
548*77c1e3ccSAndroid Build Coastguard Worker       bsize_select = cm->seq_params->sb_size;
549*77c1e3ccSAndroid Build Coastguard Worker     }
550*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE bsize = seg_skip ? sb_size : bsize_select;
551*77c1e3ccSAndroid Build Coastguard Worker     av1_set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
552*77c1e3ccSAndroid Build Coastguard Worker   } else if (sf->part_sf.partition_search_type == VAR_BASED_PARTITION) {
553*77c1e3ccSAndroid Build Coastguard Worker     // set a variance-based partition
554*77c1e3ccSAndroid Build Coastguard Worker     av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
555*77c1e3ccSAndroid Build Coastguard Worker     av1_choose_var_based_partitioning(cpi, tile_info, td, x, mi_row, mi_col);
556*77c1e3ccSAndroid Build Coastguard Worker   }
557*77c1e3ccSAndroid Build Coastguard Worker   assert(sf->part_sf.partition_search_type == FIXED_PARTITION || seg_skip ||
558*77c1e3ccSAndroid Build Coastguard Worker          sf->part_sf.partition_search_type == VAR_BASED_PARTITION);
559*77c1e3ccSAndroid Build Coastguard Worker   set_cb_offsets(td->mb.cb_offset, 0, 0);
560*77c1e3ccSAndroid Build Coastguard Worker 
561*77c1e3ccSAndroid Build Coastguard Worker   // Initialize the flag to skip cdef to 1.
562*77c1e3ccSAndroid Build Coastguard Worker   if (sf->rt_sf.skip_cdef_sb) {
563*77c1e3ccSAndroid Build Coastguard Worker     const int block64_in_sb = (sb_size == BLOCK_128X128) ? 2 : 1;
564*77c1e3ccSAndroid Build Coastguard Worker     // If 128x128 block is used, we need to set the flag for all 4 64x64 sub
565*77c1e3ccSAndroid Build Coastguard Worker     // "blocks".
566*77c1e3ccSAndroid Build Coastguard Worker     for (int r = 0; r < block64_in_sb; ++r) {
567*77c1e3ccSAndroid Build Coastguard Worker       for (int c = 0; c < block64_in_sb; ++c) {
568*77c1e3ccSAndroid Build Coastguard Worker         const int idx_in_sb =
569*77c1e3ccSAndroid Build Coastguard Worker             r * MI_SIZE_64X64 * cm->mi_params.mi_stride + c * MI_SIZE_64X64;
570*77c1e3ccSAndroid Build Coastguard Worker         if (mi[idx_in_sb]) mi[idx_in_sb]->cdef_strength = 1;
571*77c1e3ccSAndroid Build Coastguard Worker       }
572*77c1e3ccSAndroid Build Coastguard Worker     }
573*77c1e3ccSAndroid Build Coastguard Worker   }
574*77c1e3ccSAndroid Build Coastguard Worker 
575*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
576*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, nonrd_use_partition_time);
577*77c1e3ccSAndroid Build Coastguard Worker #endif
578*77c1e3ccSAndroid Build Coastguard Worker   av1_nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, sb_size,
579*77c1e3ccSAndroid Build Coastguard Worker                           pc_root);
580*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
581*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, nonrd_use_partition_time);
582*77c1e3ccSAndroid Build Coastguard Worker #endif
583*77c1e3ccSAndroid Build Coastguard Worker }
584*77c1e3ccSAndroid Build Coastguard Worker 
585*77c1e3ccSAndroid Build Coastguard Worker // This function initializes the stats for encode_rd_sb.
init_encode_rd_sb(AV1_COMP * cpi,ThreadData * td,const TileDataEnc * tile_data,SIMPLE_MOTION_DATA_TREE * sms_root,RD_STATS * rd_cost,int mi_row,int mi_col,int gather_tpl_data)586*77c1e3ccSAndroid Build Coastguard Worker static inline void init_encode_rd_sb(AV1_COMP *cpi, ThreadData *td,
587*77c1e3ccSAndroid Build Coastguard Worker                                      const TileDataEnc *tile_data,
588*77c1e3ccSAndroid Build Coastguard Worker                                      SIMPLE_MOTION_DATA_TREE *sms_root,
589*77c1e3ccSAndroid Build Coastguard Worker                                      RD_STATS *rd_cost, int mi_row, int mi_col,
590*77c1e3ccSAndroid Build Coastguard Worker                                      int gather_tpl_data) {
591*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *cm = &cpi->common;
592*77c1e3ccSAndroid Build Coastguard Worker   const TileInfo *tile_info = &tile_data->tile_info;
593*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *x = &td->mb;
594*77c1e3ccSAndroid Build Coastguard Worker 
595*77c1e3ccSAndroid Build Coastguard Worker   const SPEED_FEATURES *sf = &cpi->sf;
596*77c1e3ccSAndroid Build Coastguard Worker   const int use_simple_motion_search =
597*77c1e3ccSAndroid Build Coastguard Worker       (sf->part_sf.simple_motion_search_split ||
598*77c1e3ccSAndroid Build Coastguard Worker        sf->part_sf.simple_motion_search_prune_rect ||
599*77c1e3ccSAndroid Build Coastguard Worker        sf->part_sf.simple_motion_search_early_term_none ||
600*77c1e3ccSAndroid Build Coastguard Worker        sf->part_sf.ml_early_term_after_part_split_level) &&
601*77c1e3ccSAndroid Build Coastguard Worker       !frame_is_intra_only(cm);
602*77c1e3ccSAndroid Build Coastguard Worker   if (use_simple_motion_search) {
603*77c1e3ccSAndroid Build Coastguard Worker     av1_init_simple_motion_search_mvs_for_sb(cpi, tile_info, x, sms_root,
604*77c1e3ccSAndroid Build Coastguard Worker                                              mi_row, mi_col);
605*77c1e3ccSAndroid Build Coastguard Worker   }
606*77c1e3ccSAndroid Build Coastguard Worker 
607*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
608*77c1e3ccSAndroid Build Coastguard Worker   if (!(has_no_stats_stage(cpi) && cpi->oxcf.mode == REALTIME &&
609*77c1e3ccSAndroid Build Coastguard Worker         cpi->oxcf.gf_cfg.lag_in_frames == 0)) {
610*77c1e3ccSAndroid Build Coastguard Worker     init_ref_frame_space(cpi, td, mi_row, mi_col);
611*77c1e3ccSAndroid Build Coastguard Worker     x->sb_energy_level = 0;
612*77c1e3ccSAndroid Build Coastguard Worker     x->part_search_info.cnn_output_valid = 0;
613*77c1e3ccSAndroid Build Coastguard Worker     if (gather_tpl_data) {
614*77c1e3ccSAndroid Build Coastguard Worker       if (cm->delta_q_info.delta_q_present_flag) {
615*77c1e3ccSAndroid Build Coastguard Worker         const int num_planes = av1_num_planes(cm);
616*77c1e3ccSAndroid Build Coastguard Worker         const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
617*77c1e3ccSAndroid Build Coastguard Worker         setup_delta_q(cpi, td, x, tile_info, mi_row, mi_col, num_planes);
618*77c1e3ccSAndroid Build Coastguard Worker         av1_tpl_rdmult_setup_sb(cpi, x, sb_size, mi_row, mi_col);
619*77c1e3ccSAndroid Build Coastguard Worker       }
620*77c1e3ccSAndroid Build Coastguard Worker 
621*77c1e3ccSAndroid Build Coastguard Worker       // TODO(jingning): revisit this function.
622*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->oxcf.algo_cfg.enable_tpl_model && (0)) {
623*77c1e3ccSAndroid Build Coastguard Worker         adjust_rdmult_tpl_model(cpi, x, mi_row, mi_col);
624*77c1e3ccSAndroid Build Coastguard Worker       }
625*77c1e3ccSAndroid Build Coastguard Worker     }
626*77c1e3ccSAndroid Build Coastguard Worker   }
627*77c1e3ccSAndroid Build Coastguard Worker #else
628*77c1e3ccSAndroid Build Coastguard Worker   (void)tile_info;
629*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_row;
630*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_col;
631*77c1e3ccSAndroid Build Coastguard Worker   (void)gather_tpl_data;
632*77c1e3ccSAndroid Build Coastguard Worker #endif
633*77c1e3ccSAndroid Build Coastguard Worker 
634*77c1e3ccSAndroid Build Coastguard Worker   x->reuse_inter_pred = false;
635*77c1e3ccSAndroid Build Coastguard Worker   x->txfm_search_params.mode_eval_type = DEFAULT_EVAL;
636*77c1e3ccSAndroid Build Coastguard Worker   reset_mb_rd_record(x->txfm_search_info.mb_rd_record);
637*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(x->picked_ref_frames_mask);
638*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(rd_cost);
639*77c1e3ccSAndroid Build Coastguard Worker }
640*77c1e3ccSAndroid Build Coastguard Worker 
641*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
sb_qp_sweep_init_quantizers(AV1_COMP * cpi,ThreadData * td,const TileDataEnc * tile_data,SIMPLE_MOTION_DATA_TREE * sms_tree,RD_STATS * rd_cost,int mi_row,int mi_col,int delta_qp_ofs)642*77c1e3ccSAndroid Build Coastguard Worker static void sb_qp_sweep_init_quantizers(AV1_COMP *cpi, ThreadData *td,
643*77c1e3ccSAndroid Build Coastguard Worker                                         const TileDataEnc *tile_data,
644*77c1e3ccSAndroid Build Coastguard Worker                                         SIMPLE_MOTION_DATA_TREE *sms_tree,
645*77c1e3ccSAndroid Build Coastguard Worker                                         RD_STATS *rd_cost, int mi_row,
646*77c1e3ccSAndroid Build Coastguard Worker                                         int mi_col, int delta_qp_ofs) {
647*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
648*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
649*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
650*77c1e3ccSAndroid Build Coastguard Worker   const TileInfo *tile_info = &tile_data->tile_info;
651*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
652*77c1e3ccSAndroid Build Coastguard Worker   const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
653*77c1e3ccSAndroid Build Coastguard Worker   assert(delta_q_info->delta_q_present_flag);
654*77c1e3ccSAndroid Build Coastguard Worker   const int delta_q_res = delta_q_info->delta_q_res;
655*77c1e3ccSAndroid Build Coastguard Worker 
656*77c1e3ccSAndroid Build Coastguard Worker   const SPEED_FEATURES *sf = &cpi->sf;
657*77c1e3ccSAndroid Build Coastguard Worker   const int use_simple_motion_search =
658*77c1e3ccSAndroid Build Coastguard Worker       (sf->part_sf.simple_motion_search_split ||
659*77c1e3ccSAndroid Build Coastguard Worker        sf->part_sf.simple_motion_search_prune_rect ||
660*77c1e3ccSAndroid Build Coastguard Worker        sf->part_sf.simple_motion_search_early_term_none ||
661*77c1e3ccSAndroid Build Coastguard Worker        sf->part_sf.ml_early_term_after_part_split_level) &&
662*77c1e3ccSAndroid Build Coastguard Worker       !frame_is_intra_only(cm);
663*77c1e3ccSAndroid Build Coastguard Worker   if (use_simple_motion_search) {
664*77c1e3ccSAndroid Build Coastguard Worker     av1_init_simple_motion_search_mvs_for_sb(cpi, tile_info, x, sms_tree,
665*77c1e3ccSAndroid Build Coastguard Worker                                              mi_row, mi_col);
666*77c1e3ccSAndroid Build Coastguard Worker   }
667*77c1e3ccSAndroid Build Coastguard Worker 
668*77c1e3ccSAndroid Build Coastguard Worker   int current_qindex = x->rdmult_cur_qindex + delta_qp_ofs;
669*77c1e3ccSAndroid Build Coastguard Worker 
670*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
671*77c1e3ccSAndroid Build Coastguard Worker   current_qindex = av1_adjust_q_from_delta_q_res(
672*77c1e3ccSAndroid Build Coastguard Worker       delta_q_res, xd->current_base_qindex, current_qindex);
673*77c1e3ccSAndroid Build Coastguard Worker 
674*77c1e3ccSAndroid Build Coastguard Worker   x->delta_qindex = current_qindex - cm->quant_params.base_qindex;
675*77c1e3ccSAndroid Build Coastguard Worker 
676*77c1e3ccSAndroid Build Coastguard Worker   av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
677*77c1e3ccSAndroid Build Coastguard Worker   xd->mi[0]->current_qindex = current_qindex;
678*77c1e3ccSAndroid Build Coastguard Worker   av1_init_plane_quantizers(cpi, x, xd->mi[0]->segment_id, 0);
679*77c1e3ccSAndroid Build Coastguard Worker 
680*77c1e3ccSAndroid Build Coastguard Worker   // keep track of any non-zero delta-q used
681*77c1e3ccSAndroid Build Coastguard Worker   td->deltaq_used |= (x->delta_qindex != 0);
682*77c1e3ccSAndroid Build Coastguard Worker 
683*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.tool_cfg.enable_deltalf_mode) {
684*77c1e3ccSAndroid Build Coastguard Worker     const int delta_lf_res = delta_q_info->delta_lf_res;
685*77c1e3ccSAndroid Build Coastguard Worker     const int lfmask = ~(delta_lf_res - 1);
686*77c1e3ccSAndroid Build Coastguard Worker     const int delta_lf_from_base =
687*77c1e3ccSAndroid Build Coastguard Worker         ((x->delta_qindex / 4 + delta_lf_res / 2) & lfmask);
688*77c1e3ccSAndroid Build Coastguard Worker     const int8_t delta_lf =
689*77c1e3ccSAndroid Build Coastguard Worker         (int8_t)clamp(delta_lf_from_base, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
690*77c1e3ccSAndroid Build Coastguard Worker     const int frame_lf_count =
691*77c1e3ccSAndroid Build Coastguard Worker         av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
692*77c1e3ccSAndroid Build Coastguard Worker     const int mib_size = cm->seq_params->mib_size;
693*77c1e3ccSAndroid Build Coastguard Worker 
694*77c1e3ccSAndroid Build Coastguard Worker     // pre-set the delta lf for loop filter. Note that this value is set
695*77c1e3ccSAndroid Build Coastguard Worker     // before mi is assigned for each block in current superblock
696*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < AOMMIN(mib_size, mi_params->mi_rows - mi_row); j++) {
697*77c1e3ccSAndroid Build Coastguard Worker       for (int k = 0; k < AOMMIN(mib_size, mi_params->mi_cols - mi_col); k++) {
698*77c1e3ccSAndroid Build Coastguard Worker         const int grid_idx = get_mi_grid_idx(mi_params, mi_row + j, mi_col + k);
699*77c1e3ccSAndroid Build Coastguard Worker         mi_params->mi_alloc[grid_idx].delta_lf_from_base = delta_lf;
700*77c1e3ccSAndroid Build Coastguard Worker         for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
701*77c1e3ccSAndroid Build Coastguard Worker           mi_params->mi_alloc[grid_idx].delta_lf[lf_id] = delta_lf;
702*77c1e3ccSAndroid Build Coastguard Worker         }
703*77c1e3ccSAndroid Build Coastguard Worker       }
704*77c1e3ccSAndroid Build Coastguard Worker     }
705*77c1e3ccSAndroid Build Coastguard Worker   }
706*77c1e3ccSAndroid Build Coastguard Worker 
707*77c1e3ccSAndroid Build Coastguard Worker   x->reuse_inter_pred = false;
708*77c1e3ccSAndroid Build Coastguard Worker   x->txfm_search_params.mode_eval_type = DEFAULT_EVAL;
709*77c1e3ccSAndroid Build Coastguard Worker   reset_mb_rd_record(x->txfm_search_info.mb_rd_record);
710*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(x->picked_ref_frames_mask);
711*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(rd_cost);
712*77c1e3ccSAndroid Build Coastguard Worker }
713*77c1e3ccSAndroid Build Coastguard Worker 
sb_qp_sweep(AV1_COMP * const cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,int mi_row,int mi_col,BLOCK_SIZE bsize,SIMPLE_MOTION_DATA_TREE * sms_tree,SB_FIRST_PASS_STATS * sb_org_stats)714*77c1e3ccSAndroid Build Coastguard Worker static int sb_qp_sweep(AV1_COMP *const cpi, ThreadData *td,
715*77c1e3ccSAndroid Build Coastguard Worker                        TileDataEnc *tile_data, TokenExtra **tp, int mi_row,
716*77c1e3ccSAndroid Build Coastguard Worker                        int mi_col, BLOCK_SIZE bsize,
717*77c1e3ccSAndroid Build Coastguard Worker                        SIMPLE_MOTION_DATA_TREE *sms_tree,
718*77c1e3ccSAndroid Build Coastguard Worker                        SB_FIRST_PASS_STATS *sb_org_stats) {
719*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
720*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
721*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS rdc_winner, cur_rdc;
722*77c1e3ccSAndroid Build Coastguard Worker   av1_invalid_rd_stats(&rdc_winner);
723*77c1e3ccSAndroid Build Coastguard Worker 
724*77c1e3ccSAndroid Build Coastguard Worker   int best_qindex = td->mb.rdmult_delta_qindex;
725*77c1e3ccSAndroid Build Coastguard Worker   const int start = cm->current_frame.frame_type == KEY_FRAME ? -20 : -12;
726*77c1e3ccSAndroid Build Coastguard Worker   const int end = cm->current_frame.frame_type == KEY_FRAME ? 20 : 12;
727*77c1e3ccSAndroid Build Coastguard Worker   const int step = cm->delta_q_info.delta_q_res;
728*77c1e3ccSAndroid Build Coastguard Worker 
729*77c1e3ccSAndroid Build Coastguard Worker   for (int sweep_qp_delta = start; sweep_qp_delta <= end;
730*77c1e3ccSAndroid Build Coastguard Worker        sweep_qp_delta += step) {
731*77c1e3ccSAndroid Build Coastguard Worker     sb_qp_sweep_init_quantizers(cpi, td, tile_data, sms_tree, &cur_rdc, mi_row,
732*77c1e3ccSAndroid Build Coastguard Worker                                 mi_col, sweep_qp_delta);
733*77c1e3ccSAndroid Build Coastguard Worker 
734*77c1e3ccSAndroid Build Coastguard Worker     const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
735*77c1e3ccSAndroid Build Coastguard Worker     const int backup_current_qindex =
736*77c1e3ccSAndroid Build Coastguard Worker         cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex;
737*77c1e3ccSAndroid Build Coastguard Worker 
738*77c1e3ccSAndroid Build Coastguard Worker     av1_reset_mbmi(&cm->mi_params, bsize, mi_row, mi_col);
739*77c1e3ccSAndroid Build Coastguard Worker     av1_restore_sb_state(sb_org_stats, cpi, td, tile_data, mi_row, mi_col);
740*77c1e3ccSAndroid Build Coastguard Worker     cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex = backup_current_qindex;
741*77c1e3ccSAndroid Build Coastguard Worker 
742*77c1e3ccSAndroid Build Coastguard Worker     td->pc_root = av1_alloc_pc_tree_node(bsize);
743*77c1e3ccSAndroid Build Coastguard Worker     if (!td->pc_root)
744*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(x->e_mbd.error_info, AOM_CODEC_MEM_ERROR,
745*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PC_TREE");
746*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize,
747*77c1e3ccSAndroid Build Coastguard Worker                           &cur_rdc, cur_rdc, td->pc_root, sms_tree, NULL,
748*77c1e3ccSAndroid Build Coastguard Worker                           SB_DRY_PASS, NULL);
749*77c1e3ccSAndroid Build Coastguard Worker 
750*77c1e3ccSAndroid Build Coastguard Worker     if ((rdc_winner.rdcost > cur_rdc.rdcost) ||
751*77c1e3ccSAndroid Build Coastguard Worker         (abs(sweep_qp_delta) < abs(best_qindex - x->rdmult_delta_qindex) &&
752*77c1e3ccSAndroid Build Coastguard Worker          rdc_winner.rdcost == cur_rdc.rdcost)) {
753*77c1e3ccSAndroid Build Coastguard Worker       rdc_winner = cur_rdc;
754*77c1e3ccSAndroid Build Coastguard Worker       best_qindex = x->rdmult_delta_qindex + sweep_qp_delta;
755*77c1e3ccSAndroid Build Coastguard Worker     }
756*77c1e3ccSAndroid Build Coastguard Worker   }
757*77c1e3ccSAndroid Build Coastguard Worker 
758*77c1e3ccSAndroid Build Coastguard Worker   return best_qindex;
759*77c1e3ccSAndroid Build Coastguard Worker }
760*77c1e3ccSAndroid Build Coastguard Worker #endif  //! CONFIG_REALTIME_ONLY
761*77c1e3ccSAndroid Build Coastguard Worker 
762*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Encode a superblock (RD-search-based)
763*77c1e3ccSAndroid Build Coastguard Worker  *
764*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
765*77c1e3ccSAndroid Build Coastguard Worker  * Conducts partition search for a superblock, based on rate-distortion costs,
766*77c1e3ccSAndroid Build Coastguard Worker  * from scratch or adjusting from a pre-calculated partition pattern.
767*77c1e3ccSAndroid Build Coastguard Worker  */
encode_rd_sb(AV1_COMP * cpi,ThreadData * td,TileDataEnc * tile_data,TokenExtra ** tp,const int mi_row,const int mi_col,const int seg_skip)768*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_rd_sb(AV1_COMP *cpi, ThreadData *td,
769*77c1e3ccSAndroid Build Coastguard Worker                                 TileDataEnc *tile_data, TokenExtra **tp,
770*77c1e3ccSAndroid Build Coastguard Worker                                 const int mi_row, const int mi_col,
771*77c1e3ccSAndroid Build Coastguard Worker                                 const int seg_skip) {
772*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
773*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
774*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
775*77c1e3ccSAndroid Build Coastguard Worker   const SPEED_FEATURES *const sf = &cpi->sf;
776*77c1e3ccSAndroid Build Coastguard Worker   const TileInfo *const tile_info = &tile_data->tile_info;
777*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO **mi = cm->mi_params.mi_grid_base +
778*77c1e3ccSAndroid Build Coastguard Worker                       get_mi_grid_idx(&cm->mi_params, mi_row, mi_col);
779*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
780*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
781*77c1e3ccSAndroid Build Coastguard Worker   int dummy_rate;
782*77c1e3ccSAndroid Build Coastguard Worker   int64_t dummy_dist;
783*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS dummy_rdc;
784*77c1e3ccSAndroid Build Coastguard Worker   SIMPLE_MOTION_DATA_TREE *const sms_root = td->sms_root;
785*77c1e3ccSAndroid Build Coastguard Worker 
786*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
787*77c1e3ccSAndroid Build Coastguard Worker   (void)seg_skip;
788*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_REALTIME_ONLY
789*77c1e3ccSAndroid Build Coastguard Worker 
790*77c1e3ccSAndroid Build Coastguard Worker   init_encode_rd_sb(cpi, td, tile_data, sms_root, &dummy_rdc, mi_row, mi_col,
791*77c1e3ccSAndroid Build Coastguard Worker                     1);
792*77c1e3ccSAndroid Build Coastguard Worker 
793*77c1e3ccSAndroid Build Coastguard Worker   // Encode the superblock
794*77c1e3ccSAndroid Build Coastguard Worker   if (sf->part_sf.partition_search_type == VAR_BASED_PARTITION) {
795*77c1e3ccSAndroid Build Coastguard Worker     // partition search starting from a variance-based partition
796*77c1e3ccSAndroid Build Coastguard Worker     av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
797*77c1e3ccSAndroid Build Coastguard Worker     av1_choose_var_based_partitioning(cpi, tile_info, td, x, mi_row, mi_col);
798*77c1e3ccSAndroid Build Coastguard Worker 
799*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
800*77c1e3ccSAndroid Build Coastguard Worker     start_timing(cpi, rd_use_partition_time);
801*77c1e3ccSAndroid Build Coastguard Worker #endif
802*77c1e3ccSAndroid Build Coastguard Worker     td->pc_root = av1_alloc_pc_tree_node(sb_size);
803*77c1e3ccSAndroid Build Coastguard Worker     if (!td->pc_root)
804*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
805*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PC_TREE");
806*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, sb_size,
807*77c1e3ccSAndroid Build Coastguard Worker                          &dummy_rate, &dummy_dist, 1, td->pc_root);
808*77c1e3ccSAndroid Build Coastguard Worker     av1_free_pc_tree_recursive(td->pc_root, num_planes, 0, 0,
809*77c1e3ccSAndroid Build Coastguard Worker                                sf->part_sf.partition_search_type);
810*77c1e3ccSAndroid Build Coastguard Worker     td->pc_root = NULL;
811*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
812*77c1e3ccSAndroid Build Coastguard Worker     end_timing(cpi, rd_use_partition_time);
813*77c1e3ccSAndroid Build Coastguard Worker #endif
814*77c1e3ccSAndroid Build Coastguard Worker   }
815*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
816*77c1e3ccSAndroid Build Coastguard Worker   else if (sf->part_sf.partition_search_type == FIXED_PARTITION || seg_skip) {
817*77c1e3ccSAndroid Build Coastguard Worker     // partition search by adjusting a fixed-size partition
818*77c1e3ccSAndroid Build Coastguard Worker     av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
819*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE bsize =
820*77c1e3ccSAndroid Build Coastguard Worker         seg_skip ? sb_size : sf->part_sf.fixed_partition_size;
821*77c1e3ccSAndroid Build Coastguard Worker     av1_set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
822*77c1e3ccSAndroid Build Coastguard Worker     td->pc_root = av1_alloc_pc_tree_node(sb_size);
823*77c1e3ccSAndroid Build Coastguard Worker     if (!td->pc_root)
824*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
825*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PC_TREE");
826*77c1e3ccSAndroid Build Coastguard Worker     av1_rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, sb_size,
827*77c1e3ccSAndroid Build Coastguard Worker                          &dummy_rate, &dummy_dist, 1, td->pc_root);
828*77c1e3ccSAndroid Build Coastguard Worker     av1_free_pc_tree_recursive(td->pc_root, num_planes, 0, 0,
829*77c1e3ccSAndroid Build Coastguard Worker                                sf->part_sf.partition_search_type);
830*77c1e3ccSAndroid Build Coastguard Worker     td->pc_root = NULL;
831*77c1e3ccSAndroid Build Coastguard Worker   } else {
832*77c1e3ccSAndroid Build Coastguard Worker     // The most exhaustive recursive partition search
833*77c1e3ccSAndroid Build Coastguard Worker     SuperBlockEnc *sb_enc = &x->sb_enc;
834*77c1e3ccSAndroid Build Coastguard Worker     // No stats for overlay frames. Exclude key frame.
835*77c1e3ccSAndroid Build Coastguard Worker     av1_get_tpl_stats_sb(cpi, sb_size, mi_row, mi_col, sb_enc);
836*77c1e3ccSAndroid Build Coastguard Worker 
837*77c1e3ccSAndroid Build Coastguard Worker     // Reset the tree for simple motion search data
838*77c1e3ccSAndroid Build Coastguard Worker     av1_reset_simple_motion_tree_partition(sms_root, sb_size);
839*77c1e3ccSAndroid Build Coastguard Worker 
840*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
841*77c1e3ccSAndroid Build Coastguard Worker     start_timing(cpi, rd_pick_partition_time);
842*77c1e3ccSAndroid Build Coastguard Worker #endif
843*77c1e3ccSAndroid Build Coastguard Worker 
844*77c1e3ccSAndroid Build Coastguard Worker     // Estimate the maximum square partition block size, which will be used
845*77c1e3ccSAndroid Build Coastguard Worker     // as the starting block size for partitioning the sb
846*77c1e3ccSAndroid Build Coastguard Worker     set_max_min_partition_size(sb_enc, cpi, x, sf, sb_size, mi_row, mi_col);
847*77c1e3ccSAndroid Build Coastguard Worker 
848*77c1e3ccSAndroid Build Coastguard Worker     // The superblock can be searched only once, or twice consecutively for
849*77c1e3ccSAndroid Build Coastguard Worker     // better quality. Note that the meaning of passes here is different from
850*77c1e3ccSAndroid Build Coastguard Worker     // the general concept of 1-pass/2-pass encoders.
851*77c1e3ccSAndroid Build Coastguard Worker     const int num_passes =
852*77c1e3ccSAndroid Build Coastguard Worker         cpi->oxcf.unit_test_cfg.sb_multipass_unit_test ? 2 : 1;
853*77c1e3ccSAndroid Build Coastguard Worker 
854*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->oxcf.sb_qp_sweep &&
855*77c1e3ccSAndroid Build Coastguard Worker         !(has_no_stats_stage(cpi) && cpi->oxcf.mode == REALTIME &&
856*77c1e3ccSAndroid Build Coastguard Worker           cpi->oxcf.gf_cfg.lag_in_frames == 0) &&
857*77c1e3ccSAndroid Build Coastguard Worker         cm->delta_q_info.delta_q_present_flag) {
858*77c1e3ccSAndroid Build Coastguard Worker       AOM_CHECK_MEM_ERROR(
859*77c1e3ccSAndroid Build Coastguard Worker           x->e_mbd.error_info, td->mb.sb_stats_cache,
860*77c1e3ccSAndroid Build Coastguard Worker           (SB_FIRST_PASS_STATS *)aom_malloc(sizeof(*td->mb.sb_stats_cache)));
861*77c1e3ccSAndroid Build Coastguard Worker       av1_backup_sb_state(td->mb.sb_stats_cache, cpi, td, tile_data, mi_row,
862*77c1e3ccSAndroid Build Coastguard Worker                           mi_col);
863*77c1e3ccSAndroid Build Coastguard Worker       assert(x->rdmult_delta_qindex == x->delta_qindex);
864*77c1e3ccSAndroid Build Coastguard Worker 
865*77c1e3ccSAndroid Build Coastguard Worker       const int best_qp_diff =
866*77c1e3ccSAndroid Build Coastguard Worker           sb_qp_sweep(cpi, td, tile_data, tp, mi_row, mi_col, sb_size, sms_root,
867*77c1e3ccSAndroid Build Coastguard Worker                       td->mb.sb_stats_cache) -
868*77c1e3ccSAndroid Build Coastguard Worker           x->rdmult_delta_qindex;
869*77c1e3ccSAndroid Build Coastguard Worker 
870*77c1e3ccSAndroid Build Coastguard Worker       sb_qp_sweep_init_quantizers(cpi, td, tile_data, sms_root, &dummy_rdc,
871*77c1e3ccSAndroid Build Coastguard Worker                                   mi_row, mi_col, best_qp_diff);
872*77c1e3ccSAndroid Build Coastguard Worker 
873*77c1e3ccSAndroid Build Coastguard Worker       const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
874*77c1e3ccSAndroid Build Coastguard Worker       const int backup_current_qindex =
875*77c1e3ccSAndroid Build Coastguard Worker           cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex;
876*77c1e3ccSAndroid Build Coastguard Worker 
877*77c1e3ccSAndroid Build Coastguard Worker       av1_reset_mbmi(&cm->mi_params, sb_size, mi_row, mi_col);
878*77c1e3ccSAndroid Build Coastguard Worker       av1_restore_sb_state(td->mb.sb_stats_cache, cpi, td, tile_data, mi_row,
879*77c1e3ccSAndroid Build Coastguard Worker                            mi_col);
880*77c1e3ccSAndroid Build Coastguard Worker 
881*77c1e3ccSAndroid Build Coastguard Worker       cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex =
882*77c1e3ccSAndroid Build Coastguard Worker           backup_current_qindex;
883*77c1e3ccSAndroid Build Coastguard Worker       aom_free(td->mb.sb_stats_cache);
884*77c1e3ccSAndroid Build Coastguard Worker       td->mb.sb_stats_cache = NULL;
885*77c1e3ccSAndroid Build Coastguard Worker     }
886*77c1e3ccSAndroid Build Coastguard Worker     if (num_passes == 1) {
887*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_PARTITION_SEARCH_ORDER
888*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->ext_part_controller.ready && !frame_is_intra_only(cm)) {
889*77c1e3ccSAndroid Build Coastguard Worker         av1_reset_part_sf(&cpi->sf.part_sf);
890*77c1e3ccSAndroid Build Coastguard Worker         av1_reset_sf_for_ext_part(cpi);
891*77c1e3ccSAndroid Build Coastguard Worker         RD_STATS this_rdc;
892*77c1e3ccSAndroid Build Coastguard Worker         av1_rd_partition_search(cpi, td, tile_data, tp, sms_root, mi_row,
893*77c1e3ccSAndroid Build Coastguard Worker                                 mi_col, sb_size, &this_rdc);
894*77c1e3ccSAndroid Build Coastguard Worker       } else {
895*77c1e3ccSAndroid Build Coastguard Worker         td->pc_root = av1_alloc_pc_tree_node(sb_size);
896*77c1e3ccSAndroid Build Coastguard Worker         if (!td->pc_root)
897*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
898*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate PC_TREE");
899*77c1e3ccSAndroid Build Coastguard Worker         av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, sb_size,
900*77c1e3ccSAndroid Build Coastguard Worker                               &dummy_rdc, dummy_rdc, td->pc_root, sms_root,
901*77c1e3ccSAndroid Build Coastguard Worker                               NULL, SB_SINGLE_PASS, NULL);
902*77c1e3ccSAndroid Build Coastguard Worker       }
903*77c1e3ccSAndroid Build Coastguard Worker #else
904*77c1e3ccSAndroid Build Coastguard Worker       td->pc_root = av1_alloc_pc_tree_node(sb_size);
905*77c1e3ccSAndroid Build Coastguard Worker       if (!td->pc_root)
906*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
907*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to allocate PC_TREE");
908*77c1e3ccSAndroid Build Coastguard Worker       av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, sb_size,
909*77c1e3ccSAndroid Build Coastguard Worker                             &dummy_rdc, dummy_rdc, td->pc_root, sms_root, NULL,
910*77c1e3ccSAndroid Build Coastguard Worker                             SB_SINGLE_PASS, NULL);
911*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_PARTITION_SEARCH_ORDER
912*77c1e3ccSAndroid Build Coastguard Worker     } else {
913*77c1e3ccSAndroid Build Coastguard Worker       // First pass
914*77c1e3ccSAndroid Build Coastguard Worker       AOM_CHECK_MEM_ERROR(
915*77c1e3ccSAndroid Build Coastguard Worker           x->e_mbd.error_info, td->mb.sb_fp_stats,
916*77c1e3ccSAndroid Build Coastguard Worker           (SB_FIRST_PASS_STATS *)aom_malloc(sizeof(*td->mb.sb_fp_stats)));
917*77c1e3ccSAndroid Build Coastguard Worker       av1_backup_sb_state(td->mb.sb_fp_stats, cpi, td, tile_data, mi_row,
918*77c1e3ccSAndroid Build Coastguard Worker                           mi_col);
919*77c1e3ccSAndroid Build Coastguard Worker       td->pc_root = av1_alloc_pc_tree_node(sb_size);
920*77c1e3ccSAndroid Build Coastguard Worker       if (!td->pc_root)
921*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
922*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to allocate PC_TREE");
923*77c1e3ccSAndroid Build Coastguard Worker       av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, sb_size,
924*77c1e3ccSAndroid Build Coastguard Worker                             &dummy_rdc, dummy_rdc, td->pc_root, sms_root, NULL,
925*77c1e3ccSAndroid Build Coastguard Worker                             SB_DRY_PASS, NULL);
926*77c1e3ccSAndroid Build Coastguard Worker 
927*77c1e3ccSAndroid Build Coastguard Worker       // Second pass
928*77c1e3ccSAndroid Build Coastguard Worker       init_encode_rd_sb(cpi, td, tile_data, sms_root, &dummy_rdc, mi_row,
929*77c1e3ccSAndroid Build Coastguard Worker                         mi_col, 0);
930*77c1e3ccSAndroid Build Coastguard Worker       av1_reset_mbmi(&cm->mi_params, sb_size, mi_row, mi_col);
931*77c1e3ccSAndroid Build Coastguard Worker       av1_reset_simple_motion_tree_partition(sms_root, sb_size);
932*77c1e3ccSAndroid Build Coastguard Worker 
933*77c1e3ccSAndroid Build Coastguard Worker       av1_restore_sb_state(td->mb.sb_fp_stats, cpi, td, tile_data, mi_row,
934*77c1e3ccSAndroid Build Coastguard Worker                            mi_col);
935*77c1e3ccSAndroid Build Coastguard Worker 
936*77c1e3ccSAndroid Build Coastguard Worker       td->pc_root = av1_alloc_pc_tree_node(sb_size);
937*77c1e3ccSAndroid Build Coastguard Worker       if (!td->pc_root)
938*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
939*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to allocate PC_TREE");
940*77c1e3ccSAndroid Build Coastguard Worker       av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, sb_size,
941*77c1e3ccSAndroid Build Coastguard Worker                             &dummy_rdc, dummy_rdc, td->pc_root, sms_root, NULL,
942*77c1e3ccSAndroid Build Coastguard Worker                             SB_WET_PASS, NULL);
943*77c1e3ccSAndroid Build Coastguard Worker       aom_free(td->mb.sb_fp_stats);
944*77c1e3ccSAndroid Build Coastguard Worker       td->mb.sb_fp_stats = NULL;
945*77c1e3ccSAndroid Build Coastguard Worker     }
946*77c1e3ccSAndroid Build Coastguard Worker 
947*77c1e3ccSAndroid Build Coastguard Worker     // Reset to 0 so that it wouldn't be used elsewhere mistakenly.
948*77c1e3ccSAndroid Build Coastguard Worker     sb_enc->tpl_data_count = 0;
949*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
950*77c1e3ccSAndroid Build Coastguard Worker     end_timing(cpi, rd_pick_partition_time);
951*77c1e3ccSAndroid Build Coastguard Worker #endif
952*77c1e3ccSAndroid Build Coastguard Worker   }
953*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
954*77c1e3ccSAndroid Build Coastguard Worker 
955*77c1e3ccSAndroid Build Coastguard Worker   // Update the inter rd model
956*77c1e3ccSAndroid Build Coastguard Worker   // TODO(angiebird): Let inter_mode_rd_model_estimation support multi-tile.
957*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.inter_sf.inter_mode_rd_model_estimation == 1 &&
958*77c1e3ccSAndroid Build Coastguard Worker       cm->tiles.cols == 1 && cm->tiles.rows == 1) {
959*77c1e3ccSAndroid Build Coastguard Worker     av1_inter_mode_data_fit(tile_data, x->rdmult);
960*77c1e3ccSAndroid Build Coastguard Worker   }
961*77c1e3ccSAndroid Build Coastguard Worker }
962*77c1e3ccSAndroid Build Coastguard Worker 
963*77c1e3ccSAndroid Build Coastguard Worker // Check if the cost update of symbols mode, coeff and dv are tile or off.
is_mode_coeff_dv_upd_freq_tile_or_off(const AV1_COMP * const cpi)964*77c1e3ccSAndroid Build Coastguard Worker static inline int is_mode_coeff_dv_upd_freq_tile_or_off(
965*77c1e3ccSAndroid Build Coastguard Worker     const AV1_COMP *const cpi) {
966*77c1e3ccSAndroid Build Coastguard Worker   const INTER_MODE_SPEED_FEATURES *const inter_sf = &cpi->sf.inter_sf;
967*77c1e3ccSAndroid Build Coastguard Worker 
968*77c1e3ccSAndroid Build Coastguard Worker   return (inter_sf->coeff_cost_upd_level <= INTERNAL_COST_UPD_TILE &&
969*77c1e3ccSAndroid Build Coastguard Worker           inter_sf->mode_cost_upd_level <= INTERNAL_COST_UPD_TILE &&
970*77c1e3ccSAndroid Build Coastguard Worker           cpi->sf.intra_sf.dv_cost_upd_level <= INTERNAL_COST_UPD_TILE);
971*77c1e3ccSAndroid Build Coastguard Worker }
972*77c1e3ccSAndroid Build Coastguard Worker 
973*77c1e3ccSAndroid Build Coastguard Worker // When row-mt is enabled and cost update frequencies are set to off/tile,
974*77c1e3ccSAndroid Build Coastguard Worker // processing of current SB can start even before processing of top-right SB
975*77c1e3ccSAndroid Build Coastguard Worker // is finished. This function checks if it is sufficient to wait for top SB
976*77c1e3ccSAndroid Build Coastguard Worker // to finish processing before current SB starts processing.
delay_wait_for_top_right_sb(const AV1_COMP * const cpi)977*77c1e3ccSAndroid Build Coastguard Worker static inline int delay_wait_for_top_right_sb(const AV1_COMP *const cpi) {
978*77c1e3ccSAndroid Build Coastguard Worker   const MODE mode = cpi->oxcf.mode;
979*77c1e3ccSAndroid Build Coastguard Worker   if (mode == GOOD) return 0;
980*77c1e3ccSAndroid Build Coastguard Worker 
981*77c1e3ccSAndroid Build Coastguard Worker   if (mode == ALLINTRA)
982*77c1e3ccSAndroid Build Coastguard Worker     return is_mode_coeff_dv_upd_freq_tile_or_off(cpi);
983*77c1e3ccSAndroid Build Coastguard Worker   else if (mode == REALTIME)
984*77c1e3ccSAndroid Build Coastguard Worker     return (is_mode_coeff_dv_upd_freq_tile_or_off(cpi) &&
985*77c1e3ccSAndroid Build Coastguard Worker             cpi->sf.inter_sf.mv_cost_upd_level <= INTERNAL_COST_UPD_TILE);
986*77c1e3ccSAndroid Build Coastguard Worker   else
987*77c1e3ccSAndroid Build Coastguard Worker     return 0;
988*77c1e3ccSAndroid Build Coastguard Worker }
989*77c1e3ccSAndroid Build Coastguard Worker 
990*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Calculate source SAD at superblock level using 64x64 block source SAD
991*77c1e3ccSAndroid Build Coastguard Worker  *
992*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
993*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
994*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
995*77c1e3ccSAndroid Build Coastguard Worker  */
get_sb_source_sad(const AV1_COMP * cpi,int mi_row,int mi_col)996*77c1e3ccSAndroid Build Coastguard Worker static inline uint64_t get_sb_source_sad(const AV1_COMP *cpi, int mi_row,
997*77c1e3ccSAndroid Build Coastguard Worker                                          int mi_col) {
998*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->src_sad_blk_64x64 == NULL) return UINT64_MAX;
999*77c1e3ccSAndroid Build Coastguard Worker 
1000*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
1001*77c1e3ccSAndroid Build Coastguard Worker   const int blk_64x64_in_mis = (cm->seq_params->sb_size == BLOCK_128X128)
1002*77c1e3ccSAndroid Build Coastguard Worker                                    ? (cm->seq_params->mib_size >> 1)
1003*77c1e3ccSAndroid Build Coastguard Worker                                    : cm->seq_params->mib_size;
1004*77c1e3ccSAndroid Build Coastguard Worker   const int num_blk_64x64_cols =
1005*77c1e3ccSAndroid Build Coastguard Worker       (cm->mi_params.mi_cols + blk_64x64_in_mis - 1) / blk_64x64_in_mis;
1006*77c1e3ccSAndroid Build Coastguard Worker   const int num_blk_64x64_rows =
1007*77c1e3ccSAndroid Build Coastguard Worker       (cm->mi_params.mi_rows + blk_64x64_in_mis - 1) / blk_64x64_in_mis;
1008*77c1e3ccSAndroid Build Coastguard Worker   const int blk_64x64_col_index = mi_col / blk_64x64_in_mis;
1009*77c1e3ccSAndroid Build Coastguard Worker   const int blk_64x64_row_index = mi_row / blk_64x64_in_mis;
1010*77c1e3ccSAndroid Build Coastguard Worker   uint64_t curr_sb_sad = UINT64_MAX;
1011*77c1e3ccSAndroid Build Coastguard Worker   // Avoid the border as sad_blk_64x64 may not be set for the border
1012*77c1e3ccSAndroid Build Coastguard Worker   // in the scene detection.
1013*77c1e3ccSAndroid Build Coastguard Worker   if ((blk_64x64_row_index >= num_blk_64x64_rows - 1) ||
1014*77c1e3ccSAndroid Build Coastguard Worker       (blk_64x64_col_index >= num_blk_64x64_cols - 1)) {
1015*77c1e3ccSAndroid Build Coastguard Worker     return curr_sb_sad;
1016*77c1e3ccSAndroid Build Coastguard Worker   }
1017*77c1e3ccSAndroid Build Coastguard Worker   const uint64_t *const src_sad_blk_64x64_data =
1018*77c1e3ccSAndroid Build Coastguard Worker       &cpi->src_sad_blk_64x64[blk_64x64_col_index +
1019*77c1e3ccSAndroid Build Coastguard Worker                               blk_64x64_row_index * num_blk_64x64_cols];
1020*77c1e3ccSAndroid Build Coastguard Worker   if (cm->seq_params->sb_size == BLOCK_128X128) {
1021*77c1e3ccSAndroid Build Coastguard Worker     // Calculate SB source SAD by accumulating source SAD of 64x64 blocks in the
1022*77c1e3ccSAndroid Build Coastguard Worker     // superblock
1023*77c1e3ccSAndroid Build Coastguard Worker     curr_sb_sad = src_sad_blk_64x64_data[0] + src_sad_blk_64x64_data[1] +
1024*77c1e3ccSAndroid Build Coastguard Worker                   src_sad_blk_64x64_data[num_blk_64x64_cols] +
1025*77c1e3ccSAndroid Build Coastguard Worker                   src_sad_blk_64x64_data[num_blk_64x64_cols + 1];
1026*77c1e3ccSAndroid Build Coastguard Worker   } else if (cm->seq_params->sb_size == BLOCK_64X64) {
1027*77c1e3ccSAndroid Build Coastguard Worker     curr_sb_sad = src_sad_blk_64x64_data[0];
1028*77c1e3ccSAndroid Build Coastguard Worker   }
1029*77c1e3ccSAndroid Build Coastguard Worker   return curr_sb_sad;
1030*77c1e3ccSAndroid Build Coastguard Worker }
1031*77c1e3ccSAndroid Build Coastguard Worker 
1032*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Determine whether grading content can be skipped based on sad stat
1033*77c1e3ccSAndroid Build Coastguard Worker  *
1034*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
1035*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
1036*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
1037*77c1e3ccSAndroid Build Coastguard Worker  */
is_calc_src_content_needed(AV1_COMP * cpi,MACROBLOCK * const x,int mi_row,int mi_col)1038*77c1e3ccSAndroid Build Coastguard Worker static inline bool is_calc_src_content_needed(AV1_COMP *cpi,
1039*77c1e3ccSAndroid Build Coastguard Worker                                               MACROBLOCK *const x, int mi_row,
1040*77c1e3ccSAndroid Build Coastguard Worker                                               int mi_col) {
1041*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1)
1042*77c1e3ccSAndroid Build Coastguard Worker     return true;
1043*77c1e3ccSAndroid Build Coastguard Worker   const uint64_t curr_sb_sad = get_sb_source_sad(cpi, mi_row, mi_col);
1044*77c1e3ccSAndroid Build Coastguard Worker   if (curr_sb_sad == UINT64_MAX) return true;
1045*77c1e3ccSAndroid Build Coastguard Worker   if (curr_sb_sad == 0) {
1046*77c1e3ccSAndroid Build Coastguard Worker     x->content_state_sb.source_sad_nonrd = kZeroSad;
1047*77c1e3ccSAndroid Build Coastguard Worker     return false;
1048*77c1e3ccSAndroid Build Coastguard Worker   }
1049*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1050*77c1e3ccSAndroid Build Coastguard Worker   bool do_calc_src_content = true;
1051*77c1e3ccSAndroid Build Coastguard Worker 
1052*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.speed < 9) return do_calc_src_content;
1053*77c1e3ccSAndroid Build Coastguard Worker 
1054*77c1e3ccSAndroid Build Coastguard Worker   // TODO(yunqing): Tune/validate the thresholds for 128x128 SB size.
1055*77c1e3ccSAndroid Build Coastguard Worker   if (AOMMIN(cm->width, cm->height) < 360) {
1056*77c1e3ccSAndroid Build Coastguard Worker     // Derive Average 64x64 block source SAD from SB source SAD
1057*77c1e3ccSAndroid Build Coastguard Worker     const uint64_t avg_64x64_blk_sad =
1058*77c1e3ccSAndroid Build Coastguard Worker         (cm->seq_params->sb_size == BLOCK_128X128) ? ((curr_sb_sad + 2) >> 2)
1059*77c1e3ccSAndroid Build Coastguard Worker                                                    : curr_sb_sad;
1060*77c1e3ccSAndroid Build Coastguard Worker 
1061*77c1e3ccSAndroid Build Coastguard Worker     // The threshold is determined based on kLowSad and kHighSad threshold and
1062*77c1e3ccSAndroid Build Coastguard Worker     // test results.
1063*77c1e3ccSAndroid Build Coastguard Worker     uint64_t thresh_low = 15000;
1064*77c1e3ccSAndroid Build Coastguard Worker     uint64_t thresh_high = 40000;
1065*77c1e3ccSAndroid Build Coastguard Worker 
1066*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.rt_sf.increase_source_sad_thresh) {
1067*77c1e3ccSAndroid Build Coastguard Worker       thresh_low = thresh_low << 1;
1068*77c1e3ccSAndroid Build Coastguard Worker       thresh_high = thresh_high << 1;
1069*77c1e3ccSAndroid Build Coastguard Worker     }
1070*77c1e3ccSAndroid Build Coastguard Worker 
1071*77c1e3ccSAndroid Build Coastguard Worker     if (avg_64x64_blk_sad > thresh_low && avg_64x64_blk_sad < thresh_high) {
1072*77c1e3ccSAndroid Build Coastguard Worker       do_calc_src_content = false;
1073*77c1e3ccSAndroid Build Coastguard Worker       // Note: set x->content_state_sb.source_sad_rd as well if this is extended
1074*77c1e3ccSAndroid Build Coastguard Worker       // to RTC rd path.
1075*77c1e3ccSAndroid Build Coastguard Worker       x->content_state_sb.source_sad_nonrd = kMedSad;
1076*77c1e3ccSAndroid Build Coastguard Worker     }
1077*77c1e3ccSAndroid Build Coastguard Worker   }
1078*77c1e3ccSAndroid Build Coastguard Worker 
1079*77c1e3ccSAndroid Build Coastguard Worker   return do_calc_src_content;
1080*77c1e3ccSAndroid Build Coastguard Worker }
1081*77c1e3ccSAndroid Build Coastguard Worker 
1082*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Determine whether grading content is needed based on sf and frame stat
1083*77c1e3ccSAndroid Build Coastguard Worker  *
1084*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
1085*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
1086*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
1087*77c1e3ccSAndroid Build Coastguard Worker  */
1088*77c1e3ccSAndroid Build Coastguard Worker // TODO(any): consolidate sfs to make interface cleaner
grade_source_content_sb(AV1_COMP * cpi,MACROBLOCK * const x,TileDataEnc * tile_data,int mi_row,int mi_col)1089*77c1e3ccSAndroid Build Coastguard Worker static inline void grade_source_content_sb(AV1_COMP *cpi, MACROBLOCK *const x,
1090*77c1e3ccSAndroid Build Coastguard Worker                                            TileDataEnc *tile_data, int mi_row,
1091*77c1e3ccSAndroid Build Coastguard Worker                                            int mi_col) {
1092*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1093*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.frame_type == KEY_FRAME ||
1094*77c1e3ccSAndroid Build Coastguard Worker       (cpi->ppi->use_svc &&
1095*77c1e3ccSAndroid Build Coastguard Worker        cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame)) {
1096*77c1e3ccSAndroid Build Coastguard Worker     assert(x->content_state_sb.source_sad_nonrd == kMedSad);
1097*77c1e3ccSAndroid Build Coastguard Worker     assert(x->content_state_sb.source_sad_rd == kMedSad);
1098*77c1e3ccSAndroid Build Coastguard Worker     return;
1099*77c1e3ccSAndroid Build Coastguard Worker   }
1100*77c1e3ccSAndroid Build Coastguard Worker   bool calc_src_content = false;
1101*77c1e3ccSAndroid Build Coastguard Worker 
1102*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.rt_sf.source_metrics_sb_nonrd) {
1103*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->sf.rt_sf.check_scene_detection || cpi->rc.frame_source_sad > 0) {
1104*77c1e3ccSAndroid Build Coastguard Worker       calc_src_content = is_calc_src_content_needed(cpi, x, mi_row, mi_col);
1105*77c1e3ccSAndroid Build Coastguard Worker     } else {
1106*77c1e3ccSAndroid Build Coastguard Worker       x->content_state_sb.source_sad_nonrd = kZeroSad;
1107*77c1e3ccSAndroid Build Coastguard Worker     }
1108*77c1e3ccSAndroid Build Coastguard Worker   } else if ((cpi->sf.rt_sf.var_part_based_on_qidx >= 1) &&
1109*77c1e3ccSAndroid Build Coastguard Worker              (cm->width * cm->height <= 352 * 288)) {
1110*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->rc.frame_source_sad > 0)
1111*77c1e3ccSAndroid Build Coastguard Worker       calc_src_content = true;
1112*77c1e3ccSAndroid Build Coastguard Worker     else
1113*77c1e3ccSAndroid Build Coastguard Worker       x->content_state_sb.source_sad_rd = kZeroSad;
1114*77c1e3ccSAndroid Build Coastguard Worker   }
1115*77c1e3ccSAndroid Build Coastguard Worker   if (calc_src_content)
1116*77c1e3ccSAndroid Build Coastguard Worker     av1_source_content_sb(cpi, x, tile_data, mi_row, mi_col);
1117*77c1e3ccSAndroid Build Coastguard Worker }
1118*77c1e3ccSAndroid Build Coastguard Worker 
1119*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Encode a superblock row by breaking it into superblocks
1120*77c1e3ccSAndroid Build Coastguard Worker  *
1121*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
1122*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
1123*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
1124*77c1e3ccSAndroid Build Coastguard Worker  * Do partition and mode search for an sb row: one row of superblocks filling up
1125*77c1e3ccSAndroid Build Coastguard Worker  * the width of the current tile.
1126*77c1e3ccSAndroid Build Coastguard Worker  */
encode_sb_row(AV1_COMP * cpi,ThreadData * td,TileDataEnc * tile_data,int mi_row,TokenExtra ** tp)1127*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_sb_row(AV1_COMP *cpi, ThreadData *td,
1128*77c1e3ccSAndroid Build Coastguard Worker                                  TileDataEnc *tile_data, int mi_row,
1129*77c1e3ccSAndroid Build Coastguard Worker                                  TokenExtra **tp) {
1130*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1131*77c1e3ccSAndroid Build Coastguard Worker   const TileInfo *const tile_info = &tile_data->tile_info;
1132*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
1133*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
1134*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadSync *const row_mt_sync = &tile_data->row_mt_sync;
1135*77c1e3ccSAndroid Build Coastguard Worker   bool row_mt_enabled = mt_info->row_mt_enabled;
1136*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
1137*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
1138*77c1e3ccSAndroid Build Coastguard Worker   const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
1139*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
1140*77c1e3ccSAndroid Build Coastguard Worker   const int mib_size = cm->seq_params->mib_size;
1141*77c1e3ccSAndroid Build Coastguard Worker   const int mib_size_log2 = cm->seq_params->mib_size_log2;
1142*77c1e3ccSAndroid Build Coastguard Worker   const int sb_row = (mi_row - tile_info->mi_row_start) >> mib_size_log2;
1143*77c1e3ccSAndroid Build Coastguard Worker   const int use_nonrd_mode = cpi->sf.rt_sf.use_nonrd_pick_mode;
1144*77c1e3ccSAndroid Build Coastguard Worker 
1145*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
1146*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, encode_sb_row_time);
1147*77c1e3ccSAndroid Build Coastguard Worker #endif
1148*77c1e3ccSAndroid Build Coastguard Worker 
1149*77c1e3ccSAndroid Build Coastguard Worker   // Initialize the left context for the new SB row
1150*77c1e3ccSAndroid Build Coastguard Worker   av1_zero_left_context(xd);
1151*77c1e3ccSAndroid Build Coastguard Worker 
1152*77c1e3ccSAndroid Build Coastguard Worker   // Reset delta for quantizer and loof filters at the beginning of every tile
1153*77c1e3ccSAndroid Build Coastguard Worker   if (mi_row == tile_info->mi_row_start || row_mt_enabled) {
1154*77c1e3ccSAndroid Build Coastguard Worker     if (cm->delta_q_info.delta_q_present_flag)
1155*77c1e3ccSAndroid Build Coastguard Worker       xd->current_base_qindex = cm->quant_params.base_qindex;
1156*77c1e3ccSAndroid Build Coastguard Worker     if (cm->delta_q_info.delta_lf_present_flag) {
1157*77c1e3ccSAndroid Build Coastguard Worker       av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
1158*77c1e3ccSAndroid Build Coastguard Worker     }
1159*77c1e3ccSAndroid Build Coastguard Worker   }
1160*77c1e3ccSAndroid Build Coastguard Worker 
1161*77c1e3ccSAndroid Build Coastguard Worker   reset_thresh_freq_fact(x);
1162*77c1e3ccSAndroid Build Coastguard Worker 
1163*77c1e3ccSAndroid Build Coastguard Worker   // Code each SB in the row
1164*77c1e3ccSAndroid Build Coastguard Worker   for (int mi_col = tile_info->mi_col_start, sb_col_in_tile = 0;
1165*77c1e3ccSAndroid Build Coastguard Worker        mi_col < tile_info->mi_col_end; mi_col += mib_size, sb_col_in_tile++) {
1166*77c1e3ccSAndroid Build Coastguard Worker     // In realtime/allintra mode and when frequency of cost updates is off/tile,
1167*77c1e3ccSAndroid Build Coastguard Worker     // wait for the top superblock to finish encoding. Otherwise, wait for the
1168*77c1e3ccSAndroid Build Coastguard Worker     // top-right superblock to finish encoding.
1169*77c1e3ccSAndroid Build Coastguard Worker     enc_row_mt->sync_read_ptr(
1170*77c1e3ccSAndroid Build Coastguard Worker         row_mt_sync, sb_row, sb_col_in_tile - delay_wait_for_top_right_sb(cpi));
1171*77c1e3ccSAndroid Build Coastguard Worker 
1172*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
1173*77c1e3ccSAndroid Build Coastguard Worker     if (row_mt_enabled) {
1174*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_lock(enc_row_mt->mutex_);
1175*77c1e3ccSAndroid Build Coastguard Worker       const bool row_mt_exit = enc_row_mt->row_mt_exit;
1176*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_unlock(enc_row_mt->mutex_);
1177*77c1e3ccSAndroid Build Coastguard Worker       // Exit in case any worker has encountered an error.
1178*77c1e3ccSAndroid Build Coastguard Worker       if (row_mt_exit) return;
1179*77c1e3ccSAndroid Build Coastguard Worker     }
1180*77c1e3ccSAndroid Build Coastguard Worker #endif
1181*77c1e3ccSAndroid Build Coastguard Worker 
1182*77c1e3ccSAndroid Build Coastguard Worker     const int update_cdf = tile_data->allow_update_cdf && row_mt_enabled;
1183*77c1e3ccSAndroid Build Coastguard Worker     if (update_cdf && (tile_info->mi_row_start != mi_row)) {
1184*77c1e3ccSAndroid Build Coastguard Worker       if ((tile_info->mi_col_start == mi_col)) {
1185*77c1e3ccSAndroid Build Coastguard Worker         // restore frame context at the 1st column sb
1186*77c1e3ccSAndroid Build Coastguard Worker         memcpy(xd->tile_ctx, x->row_ctx, sizeof(*xd->tile_ctx));
1187*77c1e3ccSAndroid Build Coastguard Worker       } else {
1188*77c1e3ccSAndroid Build Coastguard Worker         // update context
1189*77c1e3ccSAndroid Build Coastguard Worker         int wt_left = AVG_CDF_WEIGHT_LEFT;
1190*77c1e3ccSAndroid Build Coastguard Worker         int wt_tr = AVG_CDF_WEIGHT_TOP_RIGHT;
1191*77c1e3ccSAndroid Build Coastguard Worker         if (tile_info->mi_col_end > (mi_col + mib_size))
1192*77c1e3ccSAndroid Build Coastguard Worker           av1_avg_cdf_symbols(xd->tile_ctx, x->row_ctx + sb_col_in_tile,
1193*77c1e3ccSAndroid Build Coastguard Worker                               wt_left, wt_tr);
1194*77c1e3ccSAndroid Build Coastguard Worker         else
1195*77c1e3ccSAndroid Build Coastguard Worker           av1_avg_cdf_symbols(xd->tile_ctx, x->row_ctx + sb_col_in_tile - 1,
1196*77c1e3ccSAndroid Build Coastguard Worker                               wt_left, wt_tr);
1197*77c1e3ccSAndroid Build Coastguard Worker       }
1198*77c1e3ccSAndroid Build Coastguard Worker     }
1199*77c1e3ccSAndroid Build Coastguard Worker 
1200*77c1e3ccSAndroid Build Coastguard Worker     // Update the rate cost tables for some symbols
1201*77c1e3ccSAndroid Build Coastguard Worker     av1_set_cost_upd_freq(cpi, td, tile_info, mi_row, mi_col);
1202*77c1e3ccSAndroid Build Coastguard Worker 
1203*77c1e3ccSAndroid Build Coastguard Worker     // Reset color coding related parameters
1204*77c1e3ccSAndroid Build Coastguard Worker     av1_zero(x->color_sensitivity_sb);
1205*77c1e3ccSAndroid Build Coastguard Worker     av1_zero(x->color_sensitivity_sb_g);
1206*77c1e3ccSAndroid Build Coastguard Worker     av1_zero(x->color_sensitivity_sb_alt);
1207*77c1e3ccSAndroid Build Coastguard Worker     av1_zero(x->color_sensitivity);
1208*77c1e3ccSAndroid Build Coastguard Worker     x->content_state_sb.source_sad_nonrd = kMedSad;
1209*77c1e3ccSAndroid Build Coastguard Worker     x->content_state_sb.source_sad_rd = kMedSad;
1210*77c1e3ccSAndroid Build Coastguard Worker     x->content_state_sb.lighting_change = 0;
1211*77c1e3ccSAndroid Build Coastguard Worker     x->content_state_sb.low_sumdiff = 0;
1212*77c1e3ccSAndroid Build Coastguard Worker     x->force_zeromv_skip_for_sb = 0;
1213*77c1e3ccSAndroid Build Coastguard Worker     x->sb_me_block = 0;
1214*77c1e3ccSAndroid Build Coastguard Worker     x->sb_me_partition = 0;
1215*77c1e3ccSAndroid Build Coastguard Worker     x->sb_me_mv.as_int = 0;
1216*77c1e3ccSAndroid Build Coastguard Worker     x->sb_force_fixed_part = 1;
1217*77c1e3ccSAndroid Build Coastguard Worker     x->color_palette_thresh = 64;
1218*77c1e3ccSAndroid Build Coastguard Worker     x->nonrd_prune_ref_frame_search =
1219*77c1e3ccSAndroid Build Coastguard Worker         cpi->sf.rt_sf.nonrd_prune_ref_frame_search;
1220*77c1e3ccSAndroid Build Coastguard Worker 
1221*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->oxcf.mode == ALLINTRA) {
1222*77c1e3ccSAndroid Build Coastguard Worker       x->intra_sb_rdmult_modifier = 128;
1223*77c1e3ccSAndroid Build Coastguard Worker     }
1224*77c1e3ccSAndroid Build Coastguard Worker 
1225*77c1e3ccSAndroid Build Coastguard Worker     xd->cur_frame_force_integer_mv = cm->features.cur_frame_force_integer_mv;
1226*77c1e3ccSAndroid Build Coastguard Worker     x->source_variance = UINT_MAX;
1227*77c1e3ccSAndroid Build Coastguard Worker     td->mb.cb_coef_buff = av1_get_cb_coeff_buffer(cpi, mi_row, mi_col);
1228*77c1e3ccSAndroid Build Coastguard Worker 
1229*77c1e3ccSAndroid Build Coastguard Worker     // Get segment id and skip flag
1230*77c1e3ccSAndroid Build Coastguard Worker     const struct segmentation *const seg = &cm->seg;
1231*77c1e3ccSAndroid Build Coastguard Worker     int seg_skip = 0;
1232*77c1e3ccSAndroid Build Coastguard Worker     if (seg->enabled) {
1233*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t *const map =
1234*77c1e3ccSAndroid Build Coastguard Worker           seg->update_map ? cpi->enc_seg.map : cm->last_frame_seg_map;
1235*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t segment_id =
1236*77c1e3ccSAndroid Build Coastguard Worker           map ? get_segment_id(&cm->mi_params, map, sb_size, mi_row, mi_col)
1237*77c1e3ccSAndroid Build Coastguard Worker               : 0;
1238*77c1e3ccSAndroid Build Coastguard Worker       seg_skip = segfeature_active(seg, segment_id, SEG_LVL_SKIP);
1239*77c1e3ccSAndroid Build Coastguard Worker     }
1240*77c1e3ccSAndroid Build Coastguard Worker 
1241*77c1e3ccSAndroid Build Coastguard Worker     produce_gradients_for_sb(cpi, x, sb_size, mi_row, mi_col);
1242*77c1e3ccSAndroid Build Coastguard Worker 
1243*77c1e3ccSAndroid Build Coastguard Worker     init_src_var_info_of_4x4_sub_blocks(cpi, x->src_var_info_of_4x4_sub_blocks,
1244*77c1e3ccSAndroid Build Coastguard Worker                                         sb_size);
1245*77c1e3ccSAndroid Build Coastguard Worker 
1246*77c1e3ccSAndroid Build Coastguard Worker     // Grade the temporal variation of the sb, the grade will be used to decide
1247*77c1e3ccSAndroid Build Coastguard Worker     // fast mode search strategy for coding blocks
1248*77c1e3ccSAndroid Build Coastguard Worker     if (!seg_skip) grade_source_content_sb(cpi, x, tile_data, mi_row, mi_col);
1249*77c1e3ccSAndroid Build Coastguard Worker 
1250*77c1e3ccSAndroid Build Coastguard Worker     // encode the superblock
1251*77c1e3ccSAndroid Build Coastguard Worker     if (use_nonrd_mode) {
1252*77c1e3ccSAndroid Build Coastguard Worker       encode_nonrd_sb(cpi, td, tile_data, tp, mi_row, mi_col, seg_skip);
1253*77c1e3ccSAndroid Build Coastguard Worker     } else {
1254*77c1e3ccSAndroid Build Coastguard Worker       encode_rd_sb(cpi, td, tile_data, tp, mi_row, mi_col, seg_skip);
1255*77c1e3ccSAndroid Build Coastguard Worker     }
1256*77c1e3ccSAndroid Build Coastguard Worker 
1257*77c1e3ccSAndroid Build Coastguard Worker     // Update the top-right context in row_mt coding
1258*77c1e3ccSAndroid Build Coastguard Worker     if (update_cdf && (tile_info->mi_row_end > (mi_row + mib_size))) {
1259*77c1e3ccSAndroid Build Coastguard Worker       if (sb_cols_in_tile == 1)
1260*77c1e3ccSAndroid Build Coastguard Worker         memcpy(x->row_ctx, xd->tile_ctx, sizeof(*xd->tile_ctx));
1261*77c1e3ccSAndroid Build Coastguard Worker       else if (sb_col_in_tile >= 1)
1262*77c1e3ccSAndroid Build Coastguard Worker         memcpy(x->row_ctx + sb_col_in_tile - 1, xd->tile_ctx,
1263*77c1e3ccSAndroid Build Coastguard Worker                sizeof(*xd->tile_ctx));
1264*77c1e3ccSAndroid Build Coastguard Worker     }
1265*77c1e3ccSAndroid Build Coastguard Worker     enc_row_mt->sync_write_ptr(row_mt_sync, sb_row, sb_col_in_tile,
1266*77c1e3ccSAndroid Build Coastguard Worker                                sb_cols_in_tile);
1267*77c1e3ccSAndroid Build Coastguard Worker   }
1268*77c1e3ccSAndroid Build Coastguard Worker 
1269*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
1270*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, encode_sb_row_time);
1271*77c1e3ccSAndroid Build Coastguard Worker #endif
1272*77c1e3ccSAndroid Build Coastguard Worker }
1273*77c1e3ccSAndroid Build Coastguard Worker 
init_encode_frame_mb_context(AV1_COMP * cpi)1274*77c1e3ccSAndroid Build Coastguard Worker static inline void init_encode_frame_mb_context(AV1_COMP *cpi) {
1275*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1276*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
1277*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &cpi->td.mb;
1278*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
1279*77c1e3ccSAndroid Build Coastguard Worker 
1280*77c1e3ccSAndroid Build Coastguard Worker   // Copy data over into macro block data structures.
1281*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_src_planes(x, cpi->source, 0, 0, num_planes,
1282*77c1e3ccSAndroid Build Coastguard Worker                        cm->seq_params->sb_size);
1283*77c1e3ccSAndroid Build Coastguard Worker 
1284*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_block_planes(xd, cm->seq_params->subsampling_x,
1285*77c1e3ccSAndroid Build Coastguard Worker                          cm->seq_params->subsampling_y, num_planes);
1286*77c1e3ccSAndroid Build Coastguard Worker }
1287*77c1e3ccSAndroid Build Coastguard Worker 
av1_alloc_tile_data(AV1_COMP * cpi)1288*77c1e3ccSAndroid Build Coastguard Worker void av1_alloc_tile_data(AV1_COMP *cpi) {
1289*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1290*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
1291*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1292*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1293*77c1e3ccSAndroid Build Coastguard Worker 
1294*77c1e3ccSAndroid Build Coastguard Worker   av1_row_mt_mem_dealloc(cpi);
1295*77c1e3ccSAndroid Build Coastguard Worker 
1296*77c1e3ccSAndroid Build Coastguard Worker   aom_free(cpi->tile_data);
1297*77c1e3ccSAndroid Build Coastguard Worker   cpi->allocated_tiles = 0;
1298*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_tile_cols = 0;
1299*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_tile_rows = 0;
1300*77c1e3ccSAndroid Build Coastguard Worker 
1301*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(
1302*77c1e3ccSAndroid Build Coastguard Worker       cm, cpi->tile_data,
1303*77c1e3ccSAndroid Build Coastguard Worker       aom_memalign(32, tile_cols * tile_rows * sizeof(*cpi->tile_data)));
1304*77c1e3ccSAndroid Build Coastguard Worker 
1305*77c1e3ccSAndroid Build Coastguard Worker   cpi->allocated_tiles = tile_cols * tile_rows;
1306*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_tile_cols = tile_cols;
1307*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_tile_rows = tile_rows;
1308*77c1e3ccSAndroid Build Coastguard Worker   for (int tile_row = 0; tile_row < tile_rows; ++tile_row) {
1309*77c1e3ccSAndroid Build Coastguard Worker     for (int tile_col = 0; tile_col < tile_cols; ++tile_col) {
1310*77c1e3ccSAndroid Build Coastguard Worker       const int tile_index = tile_row * tile_cols + tile_col;
1311*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
1312*77c1e3ccSAndroid Build Coastguard Worker       av1_zero(this_tile->row_mt_sync);
1313*77c1e3ccSAndroid Build Coastguard Worker       this_tile->row_ctx = NULL;
1314*77c1e3ccSAndroid Build Coastguard Worker     }
1315*77c1e3ccSAndroid Build Coastguard Worker   }
1316*77c1e3ccSAndroid Build Coastguard Worker }
1317*77c1e3ccSAndroid Build Coastguard Worker 
av1_init_tile_data(AV1_COMP * cpi)1318*77c1e3ccSAndroid Build Coastguard Worker void av1_init_tile_data(AV1_COMP *cpi) {
1319*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1320*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
1321*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1322*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1323*77c1e3ccSAndroid Build Coastguard Worker   int tile_col, tile_row;
1324*77c1e3ccSAndroid Build Coastguard Worker   TokenInfo *const token_info = &cpi->token_info;
1325*77c1e3ccSAndroid Build Coastguard Worker   TokenExtra *pre_tok = token_info->tile_tok[0][0];
1326*77c1e3ccSAndroid Build Coastguard Worker   TokenList *tplist = token_info->tplist[0][0];
1327*77c1e3ccSAndroid Build Coastguard Worker   unsigned int tile_tok = 0;
1328*77c1e3ccSAndroid Build Coastguard Worker   int tplist_count = 0;
1329*77c1e3ccSAndroid Build Coastguard Worker 
1330*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi) &&
1331*77c1e3ccSAndroid Build Coastguard Worker       cm->features.allow_screen_content_tools) {
1332*77c1e3ccSAndroid Build Coastguard Worker     // Number of tokens for which token info needs to be allocated.
1333*77c1e3ccSAndroid Build Coastguard Worker     unsigned int tokens_required =
1334*77c1e3ccSAndroid Build Coastguard Worker         get_token_alloc(cm->mi_params.mb_rows, cm->mi_params.mb_cols,
1335*77c1e3ccSAndroid Build Coastguard Worker                         MAX_SB_SIZE_LOG2, num_planes);
1336*77c1e3ccSAndroid Build Coastguard Worker     // Allocate/reallocate memory for token related info if the number of tokens
1337*77c1e3ccSAndroid Build Coastguard Worker     // required is more than the number of tokens already allocated. This could
1338*77c1e3ccSAndroid Build Coastguard Worker     // occur in case of the following:
1339*77c1e3ccSAndroid Build Coastguard Worker     // 1) If the memory is not yet allocated
1340*77c1e3ccSAndroid Build Coastguard Worker     // 2) If the frame dimensions have changed
1341*77c1e3ccSAndroid Build Coastguard Worker     const bool realloc_tokens = tokens_required > token_info->tokens_allocated;
1342*77c1e3ccSAndroid Build Coastguard Worker     if (realloc_tokens) {
1343*77c1e3ccSAndroid Build Coastguard Worker       free_token_info(token_info);
1344*77c1e3ccSAndroid Build Coastguard Worker       alloc_token_info(cm, token_info, tokens_required);
1345*77c1e3ccSAndroid Build Coastguard Worker       pre_tok = token_info->tile_tok[0][0];
1346*77c1e3ccSAndroid Build Coastguard Worker       tplist = token_info->tplist[0][0];
1347*77c1e3ccSAndroid Build Coastguard Worker     }
1348*77c1e3ccSAndroid Build Coastguard Worker   }
1349*77c1e3ccSAndroid Build Coastguard Worker 
1350*77c1e3ccSAndroid Build Coastguard Worker   for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1351*77c1e3ccSAndroid Build Coastguard Worker     for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1352*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const tile_data =
1353*77c1e3ccSAndroid Build Coastguard Worker           &cpi->tile_data[tile_row * tile_cols + tile_col];
1354*77c1e3ccSAndroid Build Coastguard Worker       TileInfo *const tile_info = &tile_data->tile_info;
1355*77c1e3ccSAndroid Build Coastguard Worker       av1_tile_init(tile_info, cm, tile_row, tile_col);
1356*77c1e3ccSAndroid Build Coastguard Worker       tile_data->firstpass_top_mv = kZeroMv;
1357*77c1e3ccSAndroid Build Coastguard Worker       tile_data->abs_sum_level = 0;
1358*77c1e3ccSAndroid Build Coastguard Worker 
1359*77c1e3ccSAndroid Build Coastguard Worker       if (is_token_info_allocated(token_info)) {
1360*77c1e3ccSAndroid Build Coastguard Worker         token_info->tile_tok[tile_row][tile_col] = pre_tok + tile_tok;
1361*77c1e3ccSAndroid Build Coastguard Worker         pre_tok = token_info->tile_tok[tile_row][tile_col];
1362*77c1e3ccSAndroid Build Coastguard Worker         tile_tok = allocated_tokens(
1363*77c1e3ccSAndroid Build Coastguard Worker             tile_info, cm->seq_params->mib_size_log2 + MI_SIZE_LOG2,
1364*77c1e3ccSAndroid Build Coastguard Worker             num_planes);
1365*77c1e3ccSAndroid Build Coastguard Worker         token_info->tplist[tile_row][tile_col] = tplist + tplist_count;
1366*77c1e3ccSAndroid Build Coastguard Worker         tplist = token_info->tplist[tile_row][tile_col];
1367*77c1e3ccSAndroid Build Coastguard Worker         tplist_count = av1_get_sb_rows_in_tile(cm, tile_info);
1368*77c1e3ccSAndroid Build Coastguard Worker       }
1369*77c1e3ccSAndroid Build Coastguard Worker       tile_data->allow_update_cdf = !cm->tiles.large_scale;
1370*77c1e3ccSAndroid Build Coastguard Worker       tile_data->allow_update_cdf = tile_data->allow_update_cdf &&
1371*77c1e3ccSAndroid Build Coastguard Worker                                     !cm->features.disable_cdf_update &&
1372*77c1e3ccSAndroid Build Coastguard Worker                                     !delay_wait_for_top_right_sb(cpi);
1373*77c1e3ccSAndroid Build Coastguard Worker       tile_data->tctx = *cm->fc;
1374*77c1e3ccSAndroid Build Coastguard Worker     }
1375*77c1e3ccSAndroid Build Coastguard Worker   }
1376*77c1e3ccSAndroid Build Coastguard Worker }
1377*77c1e3ccSAndroid Build Coastguard Worker 
1378*77c1e3ccSAndroid Build Coastguard Worker // Populate the start palette token info prior to encoding an SB row.
get_token_start(AV1_COMP * cpi,const TileInfo * tile_info,int tile_row,int tile_col,int mi_row,TokenExtra ** tp)1379*77c1e3ccSAndroid Build Coastguard Worker static inline void get_token_start(AV1_COMP *cpi, const TileInfo *tile_info,
1380*77c1e3ccSAndroid Build Coastguard Worker                                    int tile_row, int tile_col, int mi_row,
1381*77c1e3ccSAndroid Build Coastguard Worker                                    TokenExtra **tp) {
1382*77c1e3ccSAndroid Build Coastguard Worker   const TokenInfo *token_info = &cpi->token_info;
1383*77c1e3ccSAndroid Build Coastguard Worker   if (!is_token_info_allocated(token_info)) return;
1384*77c1e3ccSAndroid Build Coastguard Worker 
1385*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *cm = &cpi->common;
1386*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
1387*77c1e3ccSAndroid Build Coastguard Worker   TokenList *const tplist = cpi->token_info.tplist[tile_row][tile_col];
1388*77c1e3ccSAndroid Build Coastguard Worker   const int sb_row_in_tile =
1389*77c1e3ccSAndroid Build Coastguard Worker       (mi_row - tile_info->mi_row_start) >> cm->seq_params->mib_size_log2;
1390*77c1e3ccSAndroid Build Coastguard Worker 
1391*77c1e3ccSAndroid Build Coastguard Worker   get_start_tok(cpi, tile_row, tile_col, mi_row, tp,
1392*77c1e3ccSAndroid Build Coastguard Worker                 cm->seq_params->mib_size_log2 + MI_SIZE_LOG2, num_planes);
1393*77c1e3ccSAndroid Build Coastguard Worker   assert(tplist != NULL);
1394*77c1e3ccSAndroid Build Coastguard Worker   tplist[sb_row_in_tile].start = *tp;
1395*77c1e3ccSAndroid Build Coastguard Worker }
1396*77c1e3ccSAndroid Build Coastguard Worker 
1397*77c1e3ccSAndroid Build Coastguard Worker // Populate the token count after encoding an SB row.
populate_token_count(AV1_COMP * cpi,const TileInfo * tile_info,int tile_row,int tile_col,int mi_row,TokenExtra * tok)1398*77c1e3ccSAndroid Build Coastguard Worker static inline void populate_token_count(AV1_COMP *cpi,
1399*77c1e3ccSAndroid Build Coastguard Worker                                         const TileInfo *tile_info, int tile_row,
1400*77c1e3ccSAndroid Build Coastguard Worker                                         int tile_col, int mi_row,
1401*77c1e3ccSAndroid Build Coastguard Worker                                         TokenExtra *tok) {
1402*77c1e3ccSAndroid Build Coastguard Worker   const TokenInfo *token_info = &cpi->token_info;
1403*77c1e3ccSAndroid Build Coastguard Worker   if (!is_token_info_allocated(token_info)) return;
1404*77c1e3ccSAndroid Build Coastguard Worker 
1405*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *cm = &cpi->common;
1406*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
1407*77c1e3ccSAndroid Build Coastguard Worker   TokenList *const tplist = token_info->tplist[tile_row][tile_col];
1408*77c1e3ccSAndroid Build Coastguard Worker   const int sb_row_in_tile =
1409*77c1e3ccSAndroid Build Coastguard Worker       (mi_row - tile_info->mi_row_start) >> cm->seq_params->mib_size_log2;
1410*77c1e3ccSAndroid Build Coastguard Worker   const int tile_mb_cols =
1411*77c1e3ccSAndroid Build Coastguard Worker       (tile_info->mi_col_end - tile_info->mi_col_start + 2) >> 2;
1412*77c1e3ccSAndroid Build Coastguard Worker   const int num_mb_rows_in_sb =
1413*77c1e3ccSAndroid Build Coastguard Worker       ((1 << (cm->seq_params->mib_size_log2 + MI_SIZE_LOG2)) + 8) >> 4;
1414*77c1e3ccSAndroid Build Coastguard Worker   tplist[sb_row_in_tile].count =
1415*77c1e3ccSAndroid Build Coastguard Worker       (unsigned int)(tok - tplist[sb_row_in_tile].start);
1416*77c1e3ccSAndroid Build Coastguard Worker 
1417*77c1e3ccSAndroid Build Coastguard Worker   assert((unsigned int)(tok - tplist[sb_row_in_tile].start) <=
1418*77c1e3ccSAndroid Build Coastguard Worker          get_token_alloc(num_mb_rows_in_sb, tile_mb_cols,
1419*77c1e3ccSAndroid Build Coastguard Worker                          cm->seq_params->mib_size_log2 + MI_SIZE_LOG2,
1420*77c1e3ccSAndroid Build Coastguard Worker                          num_planes));
1421*77c1e3ccSAndroid Build Coastguard Worker 
1422*77c1e3ccSAndroid Build Coastguard Worker   (void)num_planes;
1423*77c1e3ccSAndroid Build Coastguard Worker   (void)tile_mb_cols;
1424*77c1e3ccSAndroid Build Coastguard Worker   (void)num_mb_rows_in_sb;
1425*77c1e3ccSAndroid Build Coastguard Worker }
1426*77c1e3ccSAndroid Build Coastguard Worker 
1427*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Encode a superblock row
1428*77c1e3ccSAndroid Build Coastguard Worker  *
1429*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
1430*77c1e3ccSAndroid Build Coastguard Worker  */
av1_encode_sb_row(AV1_COMP * cpi,ThreadData * td,int tile_row,int tile_col,int mi_row)1431*77c1e3ccSAndroid Build Coastguard Worker void av1_encode_sb_row(AV1_COMP *cpi, ThreadData *td, int tile_row,
1432*77c1e3ccSAndroid Build Coastguard Worker                        int tile_col, int mi_row) {
1433*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1434*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1435*77c1e3ccSAndroid Build Coastguard Worker   TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
1436*77c1e3ccSAndroid Build Coastguard Worker   const TileInfo *const tile_info = &this_tile->tile_info;
1437*77c1e3ccSAndroid Build Coastguard Worker   TokenExtra *tok = NULL;
1438*77c1e3ccSAndroid Build Coastguard Worker 
1439*77c1e3ccSAndroid Build Coastguard Worker   get_token_start(cpi, tile_info, tile_row, tile_col, mi_row, &tok);
1440*77c1e3ccSAndroid Build Coastguard Worker 
1441*77c1e3ccSAndroid Build Coastguard Worker   encode_sb_row(cpi, td, this_tile, mi_row, &tok);
1442*77c1e3ccSAndroid Build Coastguard Worker 
1443*77c1e3ccSAndroid Build Coastguard Worker   populate_token_count(cpi, tile_info, tile_row, tile_col, mi_row, tok);
1444*77c1e3ccSAndroid Build Coastguard Worker }
1445*77c1e3ccSAndroid Build Coastguard Worker 
1446*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Encode a tile
1447*77c1e3ccSAndroid Build Coastguard Worker  *
1448*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
1449*77c1e3ccSAndroid Build Coastguard Worker  */
av1_encode_tile(AV1_COMP * cpi,ThreadData * td,int tile_row,int tile_col)1450*77c1e3ccSAndroid Build Coastguard Worker void av1_encode_tile(AV1_COMP *cpi, ThreadData *td, int tile_row,
1451*77c1e3ccSAndroid Build Coastguard Worker                      int tile_col) {
1452*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1453*77c1e3ccSAndroid Build Coastguard Worker   TileDataEnc *const this_tile =
1454*77c1e3ccSAndroid Build Coastguard Worker       &cpi->tile_data[tile_row * cm->tiles.cols + tile_col];
1455*77c1e3ccSAndroid Build Coastguard Worker   const TileInfo *const tile_info = &this_tile->tile_info;
1456*77c1e3ccSAndroid Build Coastguard Worker 
1457*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->sf.rt_sf.use_nonrd_pick_mode) av1_inter_mode_data_init(this_tile);
1458*77c1e3ccSAndroid Build Coastguard Worker 
1459*77c1e3ccSAndroid Build Coastguard Worker   av1_zero_above_context(cm, &td->mb.e_mbd, tile_info->mi_col_start,
1460*77c1e3ccSAndroid Build Coastguard Worker                          tile_info->mi_col_end, tile_row);
1461*77c1e3ccSAndroid Build Coastguard Worker   av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row,
1462*77c1e3ccSAndroid Build Coastguard Worker                          &td->mb.e_mbd);
1463*77c1e3ccSAndroid Build Coastguard Worker 
1464*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1465*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.intra_mode_cfg.enable_cfl_intra)
1466*77c1e3ccSAndroid Build Coastguard Worker     cfl_init(&td->mb.e_mbd.cfl, cm->seq_params);
1467*77c1e3ccSAndroid Build Coastguard Worker #endif
1468*77c1e3ccSAndroid Build Coastguard Worker 
1469*77c1e3ccSAndroid Build Coastguard Worker   if (td->mb.txfm_search_info.mb_rd_record != NULL) {
1470*77c1e3ccSAndroid Build Coastguard Worker     av1_crc32c_calculator_init(
1471*77c1e3ccSAndroid Build Coastguard Worker         &td->mb.txfm_search_info.mb_rd_record->crc_calculator);
1472*77c1e3ccSAndroid Build Coastguard Worker   }
1473*77c1e3ccSAndroid Build Coastguard Worker 
1474*77c1e3ccSAndroid Build Coastguard Worker   for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
1475*77c1e3ccSAndroid Build Coastguard Worker        mi_row += cm->seq_params->mib_size) {
1476*77c1e3ccSAndroid Build Coastguard Worker     av1_encode_sb_row(cpi, td, tile_row, tile_col, mi_row);
1477*77c1e3ccSAndroid Build Coastguard Worker   }
1478*77c1e3ccSAndroid Build Coastguard Worker   this_tile->abs_sum_level = td->abs_sum_level;
1479*77c1e3ccSAndroid Build Coastguard Worker }
1480*77c1e3ccSAndroid Build Coastguard Worker 
1481*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Break one frame into tiles and encode the tiles
1482*77c1e3ccSAndroid Build Coastguard Worker  *
1483*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup partition_search
1484*77c1e3ccSAndroid Build Coastguard Worker  *
1485*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi    Top-level encoder structure
1486*77c1e3ccSAndroid Build Coastguard Worker  */
encode_tiles(AV1_COMP * cpi)1487*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_tiles(AV1_COMP *cpi) {
1488*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1489*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1490*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1491*77c1e3ccSAndroid Build Coastguard Worker   int tile_col, tile_row;
1492*77c1e3ccSAndroid Build Coastguard Worker 
1493*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const mb = &cpi->td.mb;
1494*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(cpi->tile_data == NULL,
1495*77c1e3ccSAndroid Build Coastguard Worker                  cpi->allocated_tiles < tile_cols * tile_rows));
1496*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->allocated_tiles < tile_cols * tile_rows) av1_alloc_tile_data(cpi);
1497*77c1e3ccSAndroid Build Coastguard Worker 
1498*77c1e3ccSAndroid Build Coastguard Worker   av1_init_tile_data(cpi);
1499*77c1e3ccSAndroid Build Coastguard Worker   av1_alloc_mb_data(cpi, mb);
1500*77c1e3ccSAndroid Build Coastguard Worker 
1501*77c1e3ccSAndroid Build Coastguard Worker   for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1502*77c1e3ccSAndroid Build Coastguard Worker     for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1503*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const this_tile =
1504*77c1e3ccSAndroid Build Coastguard Worker           &cpi->tile_data[tile_row * cm->tiles.cols + tile_col];
1505*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.intrabc_used = 0;
1506*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.deltaq_used = 0;
1507*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.abs_sum_level = 0;
1508*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.rd_counts.seg_tmp_pred_cost[0] = 0;
1509*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.rd_counts.seg_tmp_pred_cost[1] = 0;
1510*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.mb.e_mbd.tile_ctx = &this_tile->tctx;
1511*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.mb.tile_pb_ctx = &this_tile->tctx;
1512*77c1e3ccSAndroid Build Coastguard Worker       av1_init_rtc_counters(&cpi->td.mb);
1513*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.mb.palette_pixels = 0;
1514*77c1e3ccSAndroid Build Coastguard Worker       av1_encode_tile(cpi, &cpi->td, tile_row, tile_col);
1515*77c1e3ccSAndroid Build Coastguard Worker       if (!frame_is_intra_only(&cpi->common))
1516*77c1e3ccSAndroid Build Coastguard Worker         av1_accumulate_rtc_counters(cpi, &cpi->td.mb);
1517*77c1e3ccSAndroid Build Coastguard Worker       cpi->palette_pixel_num += cpi->td.mb.palette_pixels;
1518*77c1e3ccSAndroid Build Coastguard Worker       cpi->intrabc_used |= cpi->td.intrabc_used;
1519*77c1e3ccSAndroid Build Coastguard Worker       cpi->deltaq_used |= cpi->td.deltaq_used;
1520*77c1e3ccSAndroid Build Coastguard Worker     }
1521*77c1e3ccSAndroid Build Coastguard Worker   }
1522*77c1e3ccSAndroid Build Coastguard Worker 
1523*77c1e3ccSAndroid Build Coastguard Worker   av1_dealloc_mb_data(mb, av1_num_planes(cm));
1524*77c1e3ccSAndroid Build Coastguard Worker }
1525*77c1e3ccSAndroid Build Coastguard Worker 
1526*77c1e3ccSAndroid Build Coastguard Worker // Set the relative distance of a reference frame w.r.t. current frame
set_rel_frame_dist(const AV1_COMMON * const cm,RefFrameDistanceInfo * const ref_frame_dist_info,const int ref_frame_flags)1527*77c1e3ccSAndroid Build Coastguard Worker static inline void set_rel_frame_dist(
1528*77c1e3ccSAndroid Build Coastguard Worker     const AV1_COMMON *const cm, RefFrameDistanceInfo *const ref_frame_dist_info,
1529*77c1e3ccSAndroid Build Coastguard Worker     const int ref_frame_flags) {
1530*77c1e3ccSAndroid Build Coastguard Worker   MV_REFERENCE_FRAME ref_frame;
1531*77c1e3ccSAndroid Build Coastguard Worker   int min_past_dist = INT32_MAX, min_future_dist = INT32_MAX;
1532*77c1e3ccSAndroid Build Coastguard Worker   ref_frame_dist_info->nearest_past_ref = NONE_FRAME;
1533*77c1e3ccSAndroid Build Coastguard Worker   ref_frame_dist_info->nearest_future_ref = NONE_FRAME;
1534*77c1e3ccSAndroid Build Coastguard Worker   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1535*77c1e3ccSAndroid Build Coastguard Worker     ref_frame_dist_info->ref_relative_dist[ref_frame - LAST_FRAME] = 0;
1536*77c1e3ccSAndroid Build Coastguard Worker     if (ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
1537*77c1e3ccSAndroid Build Coastguard Worker       int dist = av1_encoder_get_relative_dist(
1538*77c1e3ccSAndroid Build Coastguard Worker           cm->cur_frame->ref_display_order_hint[ref_frame - LAST_FRAME],
1539*77c1e3ccSAndroid Build Coastguard Worker           cm->current_frame.display_order_hint);
1540*77c1e3ccSAndroid Build Coastguard Worker       ref_frame_dist_info->ref_relative_dist[ref_frame - LAST_FRAME] = dist;
1541*77c1e3ccSAndroid Build Coastguard Worker       // Get the nearest ref_frame in the past
1542*77c1e3ccSAndroid Build Coastguard Worker       if (abs(dist) < min_past_dist && dist < 0) {
1543*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_dist_info->nearest_past_ref = ref_frame;
1544*77c1e3ccSAndroid Build Coastguard Worker         min_past_dist = abs(dist);
1545*77c1e3ccSAndroid Build Coastguard Worker       }
1546*77c1e3ccSAndroid Build Coastguard Worker       // Get the nearest ref_frame in the future
1547*77c1e3ccSAndroid Build Coastguard Worker       if (dist < min_future_dist && dist > 0) {
1548*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_dist_info->nearest_future_ref = ref_frame;
1549*77c1e3ccSAndroid Build Coastguard Worker         min_future_dist = dist;
1550*77c1e3ccSAndroid Build Coastguard Worker       }
1551*77c1e3ccSAndroid Build Coastguard Worker     }
1552*77c1e3ccSAndroid Build Coastguard Worker   }
1553*77c1e3ccSAndroid Build Coastguard Worker }
1554*77c1e3ccSAndroid Build Coastguard Worker 
refs_are_one_sided(const AV1_COMMON * cm)1555*77c1e3ccSAndroid Build Coastguard Worker static inline int refs_are_one_sided(const AV1_COMMON *cm) {
1556*77c1e3ccSAndroid Build Coastguard Worker   assert(!frame_is_intra_only(cm));
1557*77c1e3ccSAndroid Build Coastguard Worker 
1558*77c1e3ccSAndroid Build Coastguard Worker   int one_sided_refs = 1;
1559*77c1e3ccSAndroid Build Coastguard Worker   const int cur_display_order_hint = cm->current_frame.display_order_hint;
1560*77c1e3ccSAndroid Build Coastguard Worker   for (int ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
1561*77c1e3ccSAndroid Build Coastguard Worker     const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
1562*77c1e3ccSAndroid Build Coastguard Worker     if (buf == NULL) continue;
1563*77c1e3ccSAndroid Build Coastguard Worker     if (av1_encoder_get_relative_dist(buf->display_order_hint,
1564*77c1e3ccSAndroid Build Coastguard Worker                                       cur_display_order_hint) > 0) {
1565*77c1e3ccSAndroid Build Coastguard Worker       one_sided_refs = 0;  // bwd reference
1566*77c1e3ccSAndroid Build Coastguard Worker       break;
1567*77c1e3ccSAndroid Build Coastguard Worker     }
1568*77c1e3ccSAndroid Build Coastguard Worker   }
1569*77c1e3ccSAndroid Build Coastguard Worker   return one_sided_refs;
1570*77c1e3ccSAndroid Build Coastguard Worker }
1571*77c1e3ccSAndroid Build Coastguard Worker 
get_skip_mode_ref_offsets(const AV1_COMMON * cm,int ref_order_hint[2])1572*77c1e3ccSAndroid Build Coastguard Worker static inline void get_skip_mode_ref_offsets(const AV1_COMMON *cm,
1573*77c1e3ccSAndroid Build Coastguard Worker                                              int ref_order_hint[2]) {
1574*77c1e3ccSAndroid Build Coastguard Worker   const SkipModeInfo *const skip_mode_info = &cm->current_frame.skip_mode_info;
1575*77c1e3ccSAndroid Build Coastguard Worker   ref_order_hint[0] = ref_order_hint[1] = 0;
1576*77c1e3ccSAndroid Build Coastguard Worker   if (!skip_mode_info->skip_mode_allowed) return;
1577*77c1e3ccSAndroid Build Coastguard Worker 
1578*77c1e3ccSAndroid Build Coastguard Worker   const RefCntBuffer *const buf_0 =
1579*77c1e3ccSAndroid Build Coastguard Worker       get_ref_frame_buf(cm, LAST_FRAME + skip_mode_info->ref_frame_idx_0);
1580*77c1e3ccSAndroid Build Coastguard Worker   const RefCntBuffer *const buf_1 =
1581*77c1e3ccSAndroid Build Coastguard Worker       get_ref_frame_buf(cm, LAST_FRAME + skip_mode_info->ref_frame_idx_1);
1582*77c1e3ccSAndroid Build Coastguard Worker   assert(buf_0 != NULL && buf_1 != NULL);
1583*77c1e3ccSAndroid Build Coastguard Worker 
1584*77c1e3ccSAndroid Build Coastguard Worker   ref_order_hint[0] = buf_0->order_hint;
1585*77c1e3ccSAndroid Build Coastguard Worker   ref_order_hint[1] = buf_1->order_hint;
1586*77c1e3ccSAndroid Build Coastguard Worker }
1587*77c1e3ccSAndroid Build Coastguard Worker 
check_skip_mode_enabled(AV1_COMP * const cpi)1588*77c1e3ccSAndroid Build Coastguard Worker static int check_skip_mode_enabled(AV1_COMP *const cpi) {
1589*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1590*77c1e3ccSAndroid Build Coastguard Worker 
1591*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_skip_mode_allowed(cm);
1592*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->current_frame.skip_mode_info.skip_mode_allowed) return 0;
1593*77c1e3ccSAndroid Build Coastguard Worker 
1594*77c1e3ccSAndroid Build Coastguard Worker   // Turn off skip mode if the temporal distances of the reference pair to the
1595*77c1e3ccSAndroid Build Coastguard Worker   // current frame are different by more than 1 frame.
1596*77c1e3ccSAndroid Build Coastguard Worker   const int cur_offset = (int)cm->current_frame.order_hint;
1597*77c1e3ccSAndroid Build Coastguard Worker   int ref_offset[2];
1598*77c1e3ccSAndroid Build Coastguard Worker   get_skip_mode_ref_offsets(cm, ref_offset);
1599*77c1e3ccSAndroid Build Coastguard Worker   const int cur_to_ref0 = get_relative_dist(&cm->seq_params->order_hint_info,
1600*77c1e3ccSAndroid Build Coastguard Worker                                             cur_offset, ref_offset[0]);
1601*77c1e3ccSAndroid Build Coastguard Worker   const int cur_to_ref1 = abs(get_relative_dist(
1602*77c1e3ccSAndroid Build Coastguard Worker       &cm->seq_params->order_hint_info, cur_offset, ref_offset[1]));
1603*77c1e3ccSAndroid Build Coastguard Worker   if (abs(cur_to_ref0 - cur_to_ref1) > 1) return 0;
1604*77c1e3ccSAndroid Build Coastguard Worker 
1605*77c1e3ccSAndroid Build Coastguard Worker   // High Latency: Turn off skip mode if all refs are fwd.
1606*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->all_one_sided_refs && cpi->oxcf.gf_cfg.lag_in_frames > 0) return 0;
1607*77c1e3ccSAndroid Build Coastguard Worker 
1608*77c1e3ccSAndroid Build Coastguard Worker   const int ref_frame[2] = {
1609*77c1e3ccSAndroid Build Coastguard Worker     cm->current_frame.skip_mode_info.ref_frame_idx_0 + LAST_FRAME,
1610*77c1e3ccSAndroid Build Coastguard Worker     cm->current_frame.skip_mode_info.ref_frame_idx_1 + LAST_FRAME
1611*77c1e3ccSAndroid Build Coastguard Worker   };
1612*77c1e3ccSAndroid Build Coastguard Worker   if (!(cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame[0]]) ||
1613*77c1e3ccSAndroid Build Coastguard Worker       !(cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame[1]]))
1614*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1615*77c1e3ccSAndroid Build Coastguard Worker 
1616*77c1e3ccSAndroid Build Coastguard Worker   return 1;
1617*77c1e3ccSAndroid Build Coastguard Worker }
1618*77c1e3ccSAndroid Build Coastguard Worker 
set_default_interp_skip_flags(const AV1_COMMON * cm,InterpSearchFlags * interp_search_flags)1619*77c1e3ccSAndroid Build Coastguard Worker static inline void set_default_interp_skip_flags(
1620*77c1e3ccSAndroid Build Coastguard Worker     const AV1_COMMON *cm, InterpSearchFlags *interp_search_flags) {
1621*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
1622*77c1e3ccSAndroid Build Coastguard Worker   interp_search_flags->default_interp_skip_flags =
1623*77c1e3ccSAndroid Build Coastguard Worker       (num_planes == 1) ? INTERP_SKIP_LUMA_EVAL_CHROMA
1624*77c1e3ccSAndroid Build Coastguard Worker                         : INTERP_SKIP_LUMA_SKIP_CHROMA;
1625*77c1e3ccSAndroid Build Coastguard Worker }
1626*77c1e3ccSAndroid Build Coastguard Worker 
setup_prune_ref_frame_mask(AV1_COMP * cpi)1627*77c1e3ccSAndroid Build Coastguard Worker static inline void setup_prune_ref_frame_mask(AV1_COMP *cpi) {
1628*77c1e3ccSAndroid Build Coastguard Worker   if ((!cpi->oxcf.ref_frm_cfg.enable_onesided_comp ||
1629*77c1e3ccSAndroid Build Coastguard Worker        cpi->sf.inter_sf.disable_onesided_comp) &&
1630*77c1e3ccSAndroid Build Coastguard Worker       cpi->all_one_sided_refs) {
1631*77c1e3ccSAndroid Build Coastguard Worker     // Disable all compound references
1632*77c1e3ccSAndroid Build Coastguard Worker     cpi->prune_ref_frame_mask = (1 << MODE_CTX_REF_FRAMES) - (1 << REF_FRAMES);
1633*77c1e3ccSAndroid Build Coastguard Worker   } else if (!cpi->sf.rt_sf.use_nonrd_pick_mode &&
1634*77c1e3ccSAndroid Build Coastguard Worker              cpi->sf.inter_sf.selective_ref_frame >= 2) {
1635*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMMON *const cm = &cpi->common;
1636*77c1e3ccSAndroid Build Coastguard Worker     const int cur_frame_display_order_hint =
1637*77c1e3ccSAndroid Build Coastguard Worker         cm->current_frame.display_order_hint;
1638*77c1e3ccSAndroid Build Coastguard Worker     unsigned int *ref_display_order_hint =
1639*77c1e3ccSAndroid Build Coastguard Worker         cm->cur_frame->ref_display_order_hint;
1640*77c1e3ccSAndroid Build Coastguard Worker     const int arf2_dist = av1_encoder_get_relative_dist(
1641*77c1e3ccSAndroid Build Coastguard Worker         ref_display_order_hint[ALTREF2_FRAME - LAST_FRAME],
1642*77c1e3ccSAndroid Build Coastguard Worker         cur_frame_display_order_hint);
1643*77c1e3ccSAndroid Build Coastguard Worker     const int bwd_dist = av1_encoder_get_relative_dist(
1644*77c1e3ccSAndroid Build Coastguard Worker         ref_display_order_hint[BWDREF_FRAME - LAST_FRAME],
1645*77c1e3ccSAndroid Build Coastguard Worker         cur_frame_display_order_hint);
1646*77c1e3ccSAndroid Build Coastguard Worker 
1647*77c1e3ccSAndroid Build Coastguard Worker     for (int ref_idx = REF_FRAMES; ref_idx < MODE_CTX_REF_FRAMES; ++ref_idx) {
1648*77c1e3ccSAndroid Build Coastguard Worker       MV_REFERENCE_FRAME rf[2];
1649*77c1e3ccSAndroid Build Coastguard Worker       av1_set_ref_frame(rf, ref_idx);
1650*77c1e3ccSAndroid Build Coastguard Worker       if (!(cpi->ref_frame_flags & av1_ref_frame_flag_list[rf[0]]) ||
1651*77c1e3ccSAndroid Build Coastguard Worker           !(cpi->ref_frame_flags & av1_ref_frame_flag_list[rf[1]])) {
1652*77c1e3ccSAndroid Build Coastguard Worker         continue;
1653*77c1e3ccSAndroid Build Coastguard Worker       }
1654*77c1e3ccSAndroid Build Coastguard Worker 
1655*77c1e3ccSAndroid Build Coastguard Worker       if (!cpi->all_one_sided_refs) {
1656*77c1e3ccSAndroid Build Coastguard Worker         int ref_dist[2];
1657*77c1e3ccSAndroid Build Coastguard Worker         for (int i = 0; i < 2; ++i) {
1658*77c1e3ccSAndroid Build Coastguard Worker           ref_dist[i] = av1_encoder_get_relative_dist(
1659*77c1e3ccSAndroid Build Coastguard Worker               ref_display_order_hint[rf[i] - LAST_FRAME],
1660*77c1e3ccSAndroid Build Coastguard Worker               cur_frame_display_order_hint);
1661*77c1e3ccSAndroid Build Coastguard Worker         }
1662*77c1e3ccSAndroid Build Coastguard Worker 
1663*77c1e3ccSAndroid Build Coastguard Worker         // One-sided compound is used only when all reference frames are
1664*77c1e3ccSAndroid Build Coastguard Worker         // one-sided.
1665*77c1e3ccSAndroid Build Coastguard Worker         if ((ref_dist[0] > 0) == (ref_dist[1] > 0)) {
1666*77c1e3ccSAndroid Build Coastguard Worker           cpi->prune_ref_frame_mask |= 1 << ref_idx;
1667*77c1e3ccSAndroid Build Coastguard Worker         }
1668*77c1e3ccSAndroid Build Coastguard Worker       }
1669*77c1e3ccSAndroid Build Coastguard Worker 
1670*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->sf.inter_sf.selective_ref_frame >= 4 &&
1671*77c1e3ccSAndroid Build Coastguard Worker           (rf[0] == ALTREF2_FRAME || rf[1] == ALTREF2_FRAME) &&
1672*77c1e3ccSAndroid Build Coastguard Worker           (cpi->ref_frame_flags & av1_ref_frame_flag_list[BWDREF_FRAME])) {
1673*77c1e3ccSAndroid Build Coastguard Worker         // Check if both ALTREF2_FRAME and BWDREF_FRAME are future references.
1674*77c1e3ccSAndroid Build Coastguard Worker         if (arf2_dist > 0 && bwd_dist > 0 && bwd_dist <= arf2_dist) {
1675*77c1e3ccSAndroid Build Coastguard Worker           // Drop ALTREF2_FRAME as a reference if BWDREF_FRAME is a closer
1676*77c1e3ccSAndroid Build Coastguard Worker           // reference to the current frame than ALTREF2_FRAME
1677*77c1e3ccSAndroid Build Coastguard Worker           cpi->prune_ref_frame_mask |= 1 << ref_idx;
1678*77c1e3ccSAndroid Build Coastguard Worker         }
1679*77c1e3ccSAndroid Build Coastguard Worker       }
1680*77c1e3ccSAndroid Build Coastguard Worker     }
1681*77c1e3ccSAndroid Build Coastguard Worker   }
1682*77c1e3ccSAndroid Build Coastguard Worker }
1683*77c1e3ccSAndroid Build Coastguard Worker 
allow_deltaq_mode(AV1_COMP * cpi)1684*77c1e3ccSAndroid Build Coastguard Worker static int allow_deltaq_mode(AV1_COMP *cpi) {
1685*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1686*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1687*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE sb_size = cm->seq_params->sb_size;
1688*77c1e3ccSAndroid Build Coastguard Worker   int sbs_wide = mi_size_wide[sb_size];
1689*77c1e3ccSAndroid Build Coastguard Worker   int sbs_high = mi_size_high[sb_size];
1690*77c1e3ccSAndroid Build Coastguard Worker 
1691*77c1e3ccSAndroid Build Coastguard Worker   int64_t delta_rdcost = 0;
1692*77c1e3ccSAndroid Build Coastguard Worker   for (int mi_row = 0; mi_row < cm->mi_params.mi_rows; mi_row += sbs_high) {
1693*77c1e3ccSAndroid Build Coastguard Worker     for (int mi_col = 0; mi_col < cm->mi_params.mi_cols; mi_col += sbs_wide) {
1694*77c1e3ccSAndroid Build Coastguard Worker       int64_t this_delta_rdcost = 0;
1695*77c1e3ccSAndroid Build Coastguard Worker       av1_get_q_for_deltaq_objective(cpi, &cpi->td, &this_delta_rdcost, sb_size,
1696*77c1e3ccSAndroid Build Coastguard Worker                                      mi_row, mi_col);
1697*77c1e3ccSAndroid Build Coastguard Worker       delta_rdcost += this_delta_rdcost;
1698*77c1e3ccSAndroid Build Coastguard Worker     }
1699*77c1e3ccSAndroid Build Coastguard Worker   }
1700*77c1e3ccSAndroid Build Coastguard Worker   return delta_rdcost < 0;
1701*77c1e3ccSAndroid Build Coastguard Worker #else
1702*77c1e3ccSAndroid Build Coastguard Worker   (void)cpi;
1703*77c1e3ccSAndroid Build Coastguard Worker   return 1;
1704*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
1705*77c1e3ccSAndroid Build Coastguard Worker }
1706*77c1e3ccSAndroid Build Coastguard Worker 
1707*77c1e3ccSAndroid Build Coastguard Worker #define FORCE_ZMV_SKIP_128X128_BLK_DIFF 10000
1708*77c1e3ccSAndroid Build Coastguard Worker #define FORCE_ZMV_SKIP_MAX_PER_PIXEL_DIFF 4
1709*77c1e3ccSAndroid Build Coastguard Worker 
1710*77c1e3ccSAndroid Build Coastguard Worker // Populates block level thresholds for force zeromv-skip decision
populate_thresh_to_force_zeromv_skip(AV1_COMP * cpi)1711*77c1e3ccSAndroid Build Coastguard Worker static void populate_thresh_to_force_zeromv_skip(AV1_COMP *cpi) {
1712*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.rt_sf.part_early_exit_zeromv == 0) return;
1713*77c1e3ccSAndroid Build Coastguard Worker 
1714*77c1e3ccSAndroid Build Coastguard Worker   // Threshold for forcing zeromv-skip decision is as below:
1715*77c1e3ccSAndroid Build Coastguard Worker   // For 128x128 blocks, threshold is 10000 and per pixel threshold is 0.6103.
1716*77c1e3ccSAndroid Build Coastguard Worker   // For 64x64 blocks, threshold is 5000 and per pixel threshold is 1.221
1717*77c1e3ccSAndroid Build Coastguard Worker   // allowing slightly higher error for smaller blocks.
1718*77c1e3ccSAndroid Build Coastguard Worker   // Per Pixel Threshold of 64x64 block        Area of 64x64 block         1  1
1719*77c1e3ccSAndroid Build Coastguard Worker   // ------------------------------------=sqrt(---------------------)=sqrt(-)=-
1720*77c1e3ccSAndroid Build Coastguard Worker   // Per Pixel Threshold of 128x128 block      Area of 128x128 block       4  2
1721*77c1e3ccSAndroid Build Coastguard Worker   // Thus, per pixel thresholds for blocks of size 32x32, 16x16,...  can be
1722*77c1e3ccSAndroid Build Coastguard Worker   // chosen as 2.442, 4.884,.... As the per pixel error tends to be higher for
1723*77c1e3ccSAndroid Build Coastguard Worker   // small blocks, the same is clipped to 4.
1724*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int thresh_exit_128x128_part = FORCE_ZMV_SKIP_128X128_BLK_DIFF;
1725*77c1e3ccSAndroid Build Coastguard Worker   const int num_128x128_pix =
1726*77c1e3ccSAndroid Build Coastguard Worker       block_size_wide[BLOCK_128X128] * block_size_high[BLOCK_128X128];
1727*77c1e3ccSAndroid Build Coastguard Worker 
1728*77c1e3ccSAndroid Build Coastguard Worker   for (BLOCK_SIZE bsize = BLOCK_4X4; bsize < BLOCK_SIZES_ALL; bsize++) {
1729*77c1e3ccSAndroid Build Coastguard Worker     const int num_block_pix = block_size_wide[bsize] * block_size_high[bsize];
1730*77c1e3ccSAndroid Build Coastguard Worker 
1731*77c1e3ccSAndroid Build Coastguard Worker     // Calculate the threshold for zeromv-skip decision based on area of the
1732*77c1e3ccSAndroid Build Coastguard Worker     // partition
1733*77c1e3ccSAndroid Build Coastguard Worker     unsigned int thresh_exit_part_blk =
1734*77c1e3ccSAndroid Build Coastguard Worker         (unsigned int)(thresh_exit_128x128_part *
1735*77c1e3ccSAndroid Build Coastguard Worker                            sqrt((double)num_block_pix / num_128x128_pix) +
1736*77c1e3ccSAndroid Build Coastguard Worker                        0.5);
1737*77c1e3ccSAndroid Build Coastguard Worker     thresh_exit_part_blk = AOMMIN(
1738*77c1e3ccSAndroid Build Coastguard Worker         thresh_exit_part_blk,
1739*77c1e3ccSAndroid Build Coastguard Worker         (unsigned int)(FORCE_ZMV_SKIP_MAX_PER_PIXEL_DIFF * num_block_pix));
1740*77c1e3ccSAndroid Build Coastguard Worker     cpi->zeromv_skip_thresh_exit_part[bsize] = thresh_exit_part_blk;
1741*77c1e3ccSAndroid Build Coastguard Worker   }
1742*77c1e3ccSAndroid Build Coastguard Worker }
1743*77c1e3ccSAndroid Build Coastguard Worker 
free_block_hash_buffers(uint32_t * block_hash_values[2][2],int8_t * is_block_same[2][3])1744*77c1e3ccSAndroid Build Coastguard Worker static void free_block_hash_buffers(uint32_t *block_hash_values[2][2],
1745*77c1e3ccSAndroid Build Coastguard Worker                                     int8_t *is_block_same[2][3]) {
1746*77c1e3ccSAndroid Build Coastguard Worker   for (int k = 0; k < 2; ++k) {
1747*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < 2; ++j) {
1748*77c1e3ccSAndroid Build Coastguard Worker       aom_free(block_hash_values[k][j]);
1749*77c1e3ccSAndroid Build Coastguard Worker     }
1750*77c1e3ccSAndroid Build Coastguard Worker 
1751*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < 3; ++j) {
1752*77c1e3ccSAndroid Build Coastguard Worker       aom_free(is_block_same[k][j]);
1753*77c1e3ccSAndroid Build Coastguard Worker     }
1754*77c1e3ccSAndroid Build Coastguard Worker   }
1755*77c1e3ccSAndroid Build Coastguard Worker }
1756*77c1e3ccSAndroid Build Coastguard Worker 
1757*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Encoder setup(only for the current frame), encoding, and recontruction
1758*77c1e3ccSAndroid Build Coastguard Worker  * for a single frame
1759*77c1e3ccSAndroid Build Coastguard Worker  *
1760*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup high_level_algo
1761*77c1e3ccSAndroid Build Coastguard Worker  */
encode_frame_internal(AV1_COMP * cpi)1762*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_frame_internal(AV1_COMP *cpi) {
1763*77c1e3ccSAndroid Build Coastguard Worker   ThreadData *const td = &cpi->td;
1764*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &td->mb;
1765*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1766*77c1e3ccSAndroid Build Coastguard Worker   CommonModeInfoParams *const mi_params = &cm->mi_params;
1767*77c1e3ccSAndroid Build Coastguard Worker   FeatureFlags *const features = &cm->features;
1768*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &x->e_mbd;
1769*77c1e3ccSAndroid Build Coastguard Worker   RD_COUNTS *const rdc = &cpi->td.rd_counts;
1770*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1771*77c1e3ccSAndroid Build Coastguard Worker   FrameProbInfo *const temp_frame_probs = &cpi->ppi->temp_frame_probs;
1772*77c1e3ccSAndroid Build Coastguard Worker   FrameProbInfo *const temp_frame_probs_simulation =
1773*77c1e3ccSAndroid Build Coastguard Worker       &cpi->ppi->temp_frame_probs_simulation;
1774*77c1e3ccSAndroid Build Coastguard Worker #endif
1775*77c1e3ccSAndroid Build Coastguard Worker   FrameProbInfo *const frame_probs = &cpi->ppi->frame_probs;
1776*77c1e3ccSAndroid Build Coastguard Worker   IntraBCHashInfo *const intrabc_hash_info = &x->intrabc_hash_info;
1777*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
1778*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
1779*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1780*77c1e3ccSAndroid Build Coastguard Worker   const DELTAQ_MODE deltaq_mode = oxcf->q_cfg.deltaq_mode;
1781*77c1e3ccSAndroid Build Coastguard Worker   int i;
1782*77c1e3ccSAndroid Build Coastguard Worker 
1783*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->sf.rt_sf.use_nonrd_pick_mode) {
1784*77c1e3ccSAndroid Build Coastguard Worker     mi_params->setup_mi(mi_params);
1785*77c1e3ccSAndroid Build Coastguard Worker   }
1786*77c1e3ccSAndroid Build Coastguard Worker 
1787*77c1e3ccSAndroid Build Coastguard Worker   set_mi_offsets(mi_params, xd, 0, 0);
1788*77c1e3ccSAndroid Build Coastguard Worker 
1789*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(*td->counts);
1790*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(rdc->tx_type_used);
1791*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(rdc->obmc_used);
1792*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(rdc->warped_used);
1793*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(rdc->seg_tmp_pred_cost);
1794*77c1e3ccSAndroid Build Coastguard Worker 
1795*77c1e3ccSAndroid Build Coastguard Worker   // Reset the flag.
1796*77c1e3ccSAndroid Build Coastguard Worker   cpi->intrabc_used = 0;
1797*77c1e3ccSAndroid Build Coastguard Worker   // Need to disable intrabc when superres is selected
1798*77c1e3ccSAndroid Build Coastguard Worker   if (av1_superres_scaled(cm)) {
1799*77c1e3ccSAndroid Build Coastguard Worker     features->allow_intrabc = 0;
1800*77c1e3ccSAndroid Build Coastguard Worker   }
1801*77c1e3ccSAndroid Build Coastguard Worker 
1802*77c1e3ccSAndroid Build Coastguard Worker   features->allow_intrabc &= (oxcf->kf_cfg.enable_intrabc);
1803*77c1e3ccSAndroid Build Coastguard Worker 
1804*77c1e3ccSAndroid Build Coastguard Worker   if (features->allow_warped_motion &&
1805*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.inter_sf.prune_warped_prob_thresh > 0) {
1806*77c1e3ccSAndroid Build Coastguard Worker     const FRAME_UPDATE_TYPE update_type =
1807*77c1e3ccSAndroid Build Coastguard Worker         get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
1808*77c1e3ccSAndroid Build Coastguard Worker     int warped_probability =
1809*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
1810*77c1e3ccSAndroid Build Coastguard Worker         cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE
1811*77c1e3ccSAndroid Build Coastguard Worker             ? temp_frame_probs->warped_probs[update_type]
1812*77c1e3ccSAndroid Build Coastguard Worker             :
1813*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_FPMT_TEST
1814*77c1e3ccSAndroid Build Coastguard Worker             frame_probs->warped_probs[update_type];
1815*77c1e3ccSAndroid Build Coastguard Worker     if (warped_probability < cpi->sf.inter_sf.prune_warped_prob_thresh)
1816*77c1e3ccSAndroid Build Coastguard Worker       features->allow_warped_motion = 0;
1817*77c1e3ccSAndroid Build Coastguard Worker   }
1818*77c1e3ccSAndroid Build Coastguard Worker 
1819*77c1e3ccSAndroid Build Coastguard Worker   int hash_table_created = 0;
1820*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi) && av1_use_hash_me(cpi) &&
1821*77c1e3ccSAndroid Build Coastguard Worker       !cpi->sf.rt_sf.use_nonrd_pick_mode) {
1822*77c1e3ccSAndroid Build Coastguard Worker     // TODO(any): move this outside of the recoding loop to avoid recalculating
1823*77c1e3ccSAndroid Build Coastguard Worker     // the hash table.
1824*77c1e3ccSAndroid Build Coastguard Worker     // add to hash table
1825*77c1e3ccSAndroid Build Coastguard Worker     const int pic_width = cpi->source->y_crop_width;
1826*77c1e3ccSAndroid Build Coastguard Worker     const int pic_height = cpi->source->y_crop_height;
1827*77c1e3ccSAndroid Build Coastguard Worker     uint32_t *block_hash_values[2][2] = { { NULL } };
1828*77c1e3ccSAndroid Build Coastguard Worker     int8_t *is_block_same[2][3] = { { NULL } };
1829*77c1e3ccSAndroid Build Coastguard Worker     int k, j;
1830*77c1e3ccSAndroid Build Coastguard Worker     bool error = false;
1831*77c1e3ccSAndroid Build Coastguard Worker 
1832*77c1e3ccSAndroid Build Coastguard Worker     for (k = 0; k < 2 && !error; ++k) {
1833*77c1e3ccSAndroid Build Coastguard Worker       for (j = 0; j < 2; ++j) {
1834*77c1e3ccSAndroid Build Coastguard Worker         block_hash_values[k][j] = (uint32_t *)aom_malloc(
1835*77c1e3ccSAndroid Build Coastguard Worker             sizeof(*block_hash_values[0][0]) * pic_width * pic_height);
1836*77c1e3ccSAndroid Build Coastguard Worker         if (!block_hash_values[k][j]) {
1837*77c1e3ccSAndroid Build Coastguard Worker           error = true;
1838*77c1e3ccSAndroid Build Coastguard Worker           break;
1839*77c1e3ccSAndroid Build Coastguard Worker         }
1840*77c1e3ccSAndroid Build Coastguard Worker       }
1841*77c1e3ccSAndroid Build Coastguard Worker 
1842*77c1e3ccSAndroid Build Coastguard Worker       for (j = 0; j < 3 && !error; ++j) {
1843*77c1e3ccSAndroid Build Coastguard Worker         is_block_same[k][j] = (int8_t *)aom_malloc(
1844*77c1e3ccSAndroid Build Coastguard Worker             sizeof(*is_block_same[0][0]) * pic_width * pic_height);
1845*77c1e3ccSAndroid Build Coastguard Worker         if (!is_block_same[k][j]) error = true;
1846*77c1e3ccSAndroid Build Coastguard Worker       }
1847*77c1e3ccSAndroid Build Coastguard Worker     }
1848*77c1e3ccSAndroid Build Coastguard Worker 
1849*77c1e3ccSAndroid Build Coastguard Worker     av1_hash_table_init(intrabc_hash_info);
1850*77c1e3ccSAndroid Build Coastguard Worker     if (error ||
1851*77c1e3ccSAndroid Build Coastguard Worker         !av1_hash_table_create(&intrabc_hash_info->intrabc_hash_table)) {
1852*77c1e3ccSAndroid Build Coastguard Worker       free_block_hash_buffers(block_hash_values, is_block_same);
1853*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1854*77c1e3ccSAndroid Build Coastguard Worker                          "Error allocating intrabc_hash_table and buffers");
1855*77c1e3ccSAndroid Build Coastguard Worker     }
1856*77c1e3ccSAndroid Build Coastguard Worker     hash_table_created = 1;
1857*77c1e3ccSAndroid Build Coastguard Worker     av1_generate_block_2x2_hash_value(intrabc_hash_info, cpi->source,
1858*77c1e3ccSAndroid Build Coastguard Worker                                       block_hash_values[0], is_block_same[0]);
1859*77c1e3ccSAndroid Build Coastguard Worker     // Hash data generated for screen contents is used for intraBC ME
1860*77c1e3ccSAndroid Build Coastguard Worker     const int min_alloc_size = block_size_wide[mi_params->mi_alloc_bsize];
1861*77c1e3ccSAndroid Build Coastguard Worker     const int max_sb_size =
1862*77c1e3ccSAndroid Build Coastguard Worker         (1 << (cm->seq_params->mib_size_log2 + MI_SIZE_LOG2));
1863*77c1e3ccSAndroid Build Coastguard Worker     int src_idx = 0;
1864*77c1e3ccSAndroid Build Coastguard Worker     for (int size = 4; size <= max_sb_size; size *= 2, src_idx = !src_idx) {
1865*77c1e3ccSAndroid Build Coastguard Worker       const int dst_idx = !src_idx;
1866*77c1e3ccSAndroid Build Coastguard Worker       av1_generate_block_hash_value(
1867*77c1e3ccSAndroid Build Coastguard Worker           intrabc_hash_info, cpi->source, size, block_hash_values[src_idx],
1868*77c1e3ccSAndroid Build Coastguard Worker           block_hash_values[dst_idx], is_block_same[src_idx],
1869*77c1e3ccSAndroid Build Coastguard Worker           is_block_same[dst_idx]);
1870*77c1e3ccSAndroid Build Coastguard Worker       if (size >= min_alloc_size) {
1871*77c1e3ccSAndroid Build Coastguard Worker         if (!av1_add_to_hash_map_by_row_with_precal_data(
1872*77c1e3ccSAndroid Build Coastguard Worker                 &intrabc_hash_info->intrabc_hash_table,
1873*77c1e3ccSAndroid Build Coastguard Worker                 block_hash_values[dst_idx], is_block_same[dst_idx][2],
1874*77c1e3ccSAndroid Build Coastguard Worker                 pic_width, pic_height, size)) {
1875*77c1e3ccSAndroid Build Coastguard Worker           error = true;
1876*77c1e3ccSAndroid Build Coastguard Worker           break;
1877*77c1e3ccSAndroid Build Coastguard Worker         }
1878*77c1e3ccSAndroid Build Coastguard Worker       }
1879*77c1e3ccSAndroid Build Coastguard Worker     }
1880*77c1e3ccSAndroid Build Coastguard Worker 
1881*77c1e3ccSAndroid Build Coastguard Worker     free_block_hash_buffers(block_hash_values, is_block_same);
1882*77c1e3ccSAndroid Build Coastguard Worker 
1883*77c1e3ccSAndroid Build Coastguard Worker     if (error) {
1884*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1885*77c1e3ccSAndroid Build Coastguard Worker                          "Error adding data to intrabc_hash_table");
1886*77c1e3ccSAndroid Build Coastguard Worker     }
1887*77c1e3ccSAndroid Build Coastguard Worker   }
1888*77c1e3ccSAndroid Build Coastguard Worker 
1889*77c1e3ccSAndroid Build Coastguard Worker   const CommonQuantParams *quant_params = &cm->quant_params;
1890*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < MAX_SEGMENTS; ++i) {
1891*77c1e3ccSAndroid Build Coastguard Worker     const int qindex =
1892*77c1e3ccSAndroid Build Coastguard Worker         cm->seg.enabled ? av1_get_qindex(&cm->seg, i, quant_params->base_qindex)
1893*77c1e3ccSAndroid Build Coastguard Worker                         : quant_params->base_qindex;
1894*77c1e3ccSAndroid Build Coastguard Worker     xd->lossless[i] =
1895*77c1e3ccSAndroid Build Coastguard Worker         qindex == 0 && quant_params->y_dc_delta_q == 0 &&
1896*77c1e3ccSAndroid Build Coastguard Worker         quant_params->u_dc_delta_q == 0 && quant_params->u_ac_delta_q == 0 &&
1897*77c1e3ccSAndroid Build Coastguard Worker         quant_params->v_dc_delta_q == 0 && quant_params->v_ac_delta_q == 0;
1898*77c1e3ccSAndroid Build Coastguard Worker     if (xd->lossless[i]) cpi->enc_seg.has_lossless_segment = 1;
1899*77c1e3ccSAndroid Build Coastguard Worker     xd->qindex[i] = qindex;
1900*77c1e3ccSAndroid Build Coastguard Worker     if (xd->lossless[i]) {
1901*77c1e3ccSAndroid Build Coastguard Worker       cpi->optimize_seg_arr[i] = NO_TRELLIS_OPT;
1902*77c1e3ccSAndroid Build Coastguard Worker     } else {
1903*77c1e3ccSAndroid Build Coastguard Worker       cpi->optimize_seg_arr[i] = cpi->sf.rd_sf.optimize_coefficients;
1904*77c1e3ccSAndroid Build Coastguard Worker     }
1905*77c1e3ccSAndroid Build Coastguard Worker   }
1906*77c1e3ccSAndroid Build Coastguard Worker   features->coded_lossless = is_coded_lossless(cm, xd);
1907*77c1e3ccSAndroid Build Coastguard Worker   features->all_lossless = features->coded_lossless && !av1_superres_scaled(cm);
1908*77c1e3ccSAndroid Build Coastguard Worker 
1909*77c1e3ccSAndroid Build Coastguard Worker   // Fix delta q resolution for the moment
1910*77c1e3ccSAndroid Build Coastguard Worker 
1911*77c1e3ccSAndroid Build Coastguard Worker   cm->delta_q_info.delta_q_res = 0;
1912*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->use_ducky_encode) {
1913*77c1e3ccSAndroid Build Coastguard Worker     cm->delta_q_info.delta_q_res = DEFAULT_DELTA_Q_RES_DUCKY_ENCODE;
1914*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->oxcf.q_cfg.aq_mode != CYCLIC_REFRESH_AQ) {
1915*77c1e3ccSAndroid Build Coastguard Worker     if (deltaq_mode == DELTA_Q_OBJECTIVE)
1916*77c1e3ccSAndroid Build Coastguard Worker       cm->delta_q_info.delta_q_res = DEFAULT_DELTA_Q_RES_OBJECTIVE;
1917*77c1e3ccSAndroid Build Coastguard Worker     else if (deltaq_mode == DELTA_Q_PERCEPTUAL)
1918*77c1e3ccSAndroid Build Coastguard Worker       cm->delta_q_info.delta_q_res = DEFAULT_DELTA_Q_RES_PERCEPTUAL;
1919*77c1e3ccSAndroid Build Coastguard Worker     else if (deltaq_mode == DELTA_Q_PERCEPTUAL_AI)
1920*77c1e3ccSAndroid Build Coastguard Worker       cm->delta_q_info.delta_q_res = DEFAULT_DELTA_Q_RES_PERCEPTUAL;
1921*77c1e3ccSAndroid Build Coastguard Worker     else if (deltaq_mode == DELTA_Q_USER_RATING_BASED)
1922*77c1e3ccSAndroid Build Coastguard Worker       cm->delta_q_info.delta_q_res = DEFAULT_DELTA_Q_RES_PERCEPTUAL;
1923*77c1e3ccSAndroid Build Coastguard Worker     else if (deltaq_mode == DELTA_Q_HDR)
1924*77c1e3ccSAndroid Build Coastguard Worker       cm->delta_q_info.delta_q_res = DEFAULT_DELTA_Q_RES_PERCEPTUAL;
1925*77c1e3ccSAndroid Build Coastguard Worker     // Set delta_q_present_flag before it is used for the first time
1926*77c1e3ccSAndroid Build Coastguard Worker     cm->delta_q_info.delta_lf_res = DEFAULT_DELTA_LF_RES;
1927*77c1e3ccSAndroid Build Coastguard Worker     cm->delta_q_info.delta_q_present_flag = deltaq_mode != NO_DELTA_Q;
1928*77c1e3ccSAndroid Build Coastguard Worker 
1929*77c1e3ccSAndroid Build Coastguard Worker     // Turn off cm->delta_q_info.delta_q_present_flag if objective delta_q
1930*77c1e3ccSAndroid Build Coastguard Worker     // is used for ineligible frames. That effectively will turn off row_mt
1931*77c1e3ccSAndroid Build Coastguard Worker     // usage. Note objective delta_q and tpl eligible frames are only altref
1932*77c1e3ccSAndroid Build Coastguard Worker     // frames currently.
1933*77c1e3ccSAndroid Build Coastguard Worker     const GF_GROUP *gf_group = &cpi->ppi->gf_group;
1934*77c1e3ccSAndroid Build Coastguard Worker     if (cm->delta_q_info.delta_q_present_flag) {
1935*77c1e3ccSAndroid Build Coastguard Worker       if (deltaq_mode == DELTA_Q_OBJECTIVE &&
1936*77c1e3ccSAndroid Build Coastguard Worker           gf_group->update_type[cpi->gf_frame_index] == LF_UPDATE)
1937*77c1e3ccSAndroid Build Coastguard Worker         cm->delta_q_info.delta_q_present_flag = 0;
1938*77c1e3ccSAndroid Build Coastguard Worker 
1939*77c1e3ccSAndroid Build Coastguard Worker       if (deltaq_mode == DELTA_Q_OBJECTIVE &&
1940*77c1e3ccSAndroid Build Coastguard Worker           cm->delta_q_info.delta_q_present_flag) {
1941*77c1e3ccSAndroid Build Coastguard Worker         cm->delta_q_info.delta_q_present_flag &= allow_deltaq_mode(cpi);
1942*77c1e3ccSAndroid Build Coastguard Worker       }
1943*77c1e3ccSAndroid Build Coastguard Worker     }
1944*77c1e3ccSAndroid Build Coastguard Worker 
1945*77c1e3ccSAndroid Build Coastguard Worker     // Reset delta_q_used flag
1946*77c1e3ccSAndroid Build Coastguard Worker     cpi->deltaq_used = 0;
1947*77c1e3ccSAndroid Build Coastguard Worker 
1948*77c1e3ccSAndroid Build Coastguard Worker     cm->delta_q_info.delta_lf_present_flag =
1949*77c1e3ccSAndroid Build Coastguard Worker         cm->delta_q_info.delta_q_present_flag &&
1950*77c1e3ccSAndroid Build Coastguard Worker         oxcf->tool_cfg.enable_deltalf_mode;
1951*77c1e3ccSAndroid Build Coastguard Worker     cm->delta_q_info.delta_lf_multi = DEFAULT_DELTA_LF_MULTI;
1952*77c1e3ccSAndroid Build Coastguard Worker 
1953*77c1e3ccSAndroid Build Coastguard Worker     // update delta_q_present_flag and delta_lf_present_flag based on
1954*77c1e3ccSAndroid Build Coastguard Worker     // base_qindex
1955*77c1e3ccSAndroid Build Coastguard Worker     cm->delta_q_info.delta_q_present_flag &= quant_params->base_qindex > 0;
1956*77c1e3ccSAndroid Build Coastguard Worker     cm->delta_q_info.delta_lf_present_flag &= quant_params->base_qindex > 0;
1957*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->cyclic_refresh->apply_cyclic_refresh ||
1958*77c1e3ccSAndroid Build Coastguard Worker              cpi->svc.number_temporal_layers == 1) {
1959*77c1e3ccSAndroid Build Coastguard Worker     cpi->cyclic_refresh->actual_num_seg1_blocks = 0;
1960*77c1e3ccSAndroid Build Coastguard Worker     cpi->cyclic_refresh->actual_num_seg2_blocks = 0;
1961*77c1e3ccSAndroid Build Coastguard Worker   }
1962*77c1e3ccSAndroid Build Coastguard Worker   cpi->rc.cnt_zeromv = 0;
1963*77c1e3ccSAndroid Build Coastguard Worker 
1964*77c1e3ccSAndroid Build Coastguard Worker   av1_frame_init_quantizer(cpi);
1965*77c1e3ccSAndroid Build Coastguard Worker   init_encode_frame_mb_context(cpi);
1966*77c1e3ccSAndroid Build Coastguard Worker   set_default_interp_skip_flags(cm, &cpi->interp_search_flags);
1967*77c1e3ccSAndroid Build Coastguard Worker 
1968*77c1e3ccSAndroid Build Coastguard Worker   if (cm->prev_frame && cm->prev_frame->seg.enabled)
1969*77c1e3ccSAndroid Build Coastguard Worker     cm->last_frame_seg_map = cm->prev_frame->seg_map;
1970*77c1e3ccSAndroid Build Coastguard Worker   else
1971*77c1e3ccSAndroid Build Coastguard Worker     cm->last_frame_seg_map = NULL;
1972*77c1e3ccSAndroid Build Coastguard Worker   if (features->allow_intrabc || features->coded_lossless) {
1973*77c1e3ccSAndroid Build Coastguard Worker     av1_set_default_ref_deltas(cm->lf.ref_deltas);
1974*77c1e3ccSAndroid Build Coastguard Worker     av1_set_default_mode_deltas(cm->lf.mode_deltas);
1975*77c1e3ccSAndroid Build Coastguard Worker   } else if (cm->prev_frame) {
1976*77c1e3ccSAndroid Build Coastguard Worker     memcpy(cm->lf.ref_deltas, cm->prev_frame->ref_deltas, REF_FRAMES);
1977*77c1e3ccSAndroid Build Coastguard Worker     memcpy(cm->lf.mode_deltas, cm->prev_frame->mode_deltas, MAX_MODE_LF_DELTAS);
1978*77c1e3ccSAndroid Build Coastguard Worker   }
1979*77c1e3ccSAndroid Build Coastguard Worker   memcpy(cm->cur_frame->ref_deltas, cm->lf.ref_deltas, REF_FRAMES);
1980*77c1e3ccSAndroid Build Coastguard Worker   memcpy(cm->cur_frame->mode_deltas, cm->lf.mode_deltas, MAX_MODE_LF_DELTAS);
1981*77c1e3ccSAndroid Build Coastguard Worker 
1982*77c1e3ccSAndroid Build Coastguard Worker   cpi->all_one_sided_refs =
1983*77c1e3ccSAndroid Build Coastguard Worker       frame_is_intra_only(cm) ? 0 : refs_are_one_sided(cm);
1984*77c1e3ccSAndroid Build Coastguard Worker 
1985*77c1e3ccSAndroid Build Coastguard Worker   cpi->prune_ref_frame_mask = 0;
1986*77c1e3ccSAndroid Build Coastguard Worker   // Figure out which ref frames can be skipped at frame level.
1987*77c1e3ccSAndroid Build Coastguard Worker   setup_prune_ref_frame_mask(cpi);
1988*77c1e3ccSAndroid Build Coastguard Worker 
1989*77c1e3ccSAndroid Build Coastguard Worker   x->txfm_search_info.txb_split_count = 0;
1990*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_SPEED_STATS
1991*77c1e3ccSAndroid Build Coastguard Worker   x->txfm_search_info.tx_search_count = 0;
1992*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_SPEED_STATS
1993*77c1e3ccSAndroid Build Coastguard Worker 
1994*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1995*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
1996*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, av1_compute_global_motion_time);
1997*77c1e3ccSAndroid Build Coastguard Worker #endif
1998*77c1e3ccSAndroid Build Coastguard Worker   av1_compute_global_motion_facade(cpi);
1999*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2000*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, av1_compute_global_motion_time);
2001*77c1e3ccSAndroid Build Coastguard Worker #endif
2002*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
2003*77c1e3ccSAndroid Build Coastguard Worker 
2004*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2005*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, av1_setup_motion_field_time);
2006*77c1e3ccSAndroid Build Coastguard Worker #endif
2007*77c1e3ccSAndroid Build Coastguard Worker   av1_calculate_ref_frame_side(cm);
2008*77c1e3ccSAndroid Build Coastguard Worker   if (features->allow_ref_frame_mvs) av1_setup_motion_field(cm);
2009*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2010*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, av1_setup_motion_field_time);
2011*77c1e3ccSAndroid Build Coastguard Worker #endif
2012*77c1e3ccSAndroid Build Coastguard Worker 
2013*77c1e3ccSAndroid Build Coastguard Worker   cm->current_frame.skip_mode_info.skip_mode_flag =
2014*77c1e3ccSAndroid Build Coastguard Worker       check_skip_mode_enabled(cpi);
2015*77c1e3ccSAndroid Build Coastguard Worker 
2016*77c1e3ccSAndroid Build Coastguard Worker   // Initialization of skip mode cost depends on the value of
2017*77c1e3ccSAndroid Build Coastguard Worker   // 'skip_mode_flag'. This initialization happens in the function
2018*77c1e3ccSAndroid Build Coastguard Worker   // av1_fill_mode_rates(), which is in turn called in
2019*77c1e3ccSAndroid Build Coastguard Worker   // av1_initialize_rd_consts(). Thus, av1_initialize_rd_consts()
2020*77c1e3ccSAndroid Build Coastguard Worker   // has to be called after 'skip_mode_flag' is initialized.
2021*77c1e3ccSAndroid Build Coastguard Worker   av1_initialize_rd_consts(cpi);
2022*77c1e3ccSAndroid Build Coastguard Worker   av1_set_sad_per_bit(cpi, &x->sadperbit, quant_params->base_qindex);
2023*77c1e3ccSAndroid Build Coastguard Worker   populate_thresh_to_force_zeromv_skip(cpi);
2024*77c1e3ccSAndroid Build Coastguard Worker 
2025*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->sync_read_ptr = av1_row_mt_sync_read_dummy;
2026*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->sync_write_ptr = av1_row_mt_sync_write_dummy;
2027*77c1e3ccSAndroid Build Coastguard Worker   mt_info->row_mt_enabled = 0;
2028*77c1e3ccSAndroid Build Coastguard Worker   mt_info->pack_bs_mt_enabled = AOMMIN(mt_info->num_mod_workers[MOD_PACK_BS],
2029*77c1e3ccSAndroid Build Coastguard Worker                                        cm->tiles.cols * cm->tiles.rows) > 1;
2030*77c1e3ccSAndroid Build Coastguard Worker 
2031*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->row_mt && (mt_info->num_workers > 1)) {
2032*77c1e3ccSAndroid Build Coastguard Worker     mt_info->row_mt_enabled = 1;
2033*77c1e3ccSAndroid Build Coastguard Worker     enc_row_mt->sync_read_ptr = av1_row_mt_sync_read;
2034*77c1e3ccSAndroid Build Coastguard Worker     enc_row_mt->sync_write_ptr = av1_row_mt_sync_write;
2035*77c1e3ccSAndroid Build Coastguard Worker     av1_encode_tiles_row_mt(cpi);
2036*77c1e3ccSAndroid Build Coastguard Worker   } else {
2037*77c1e3ccSAndroid Build Coastguard Worker     if (AOMMIN(mt_info->num_workers, cm->tiles.cols * cm->tiles.rows) > 1) {
2038*77c1e3ccSAndroid Build Coastguard Worker       av1_encode_tiles_mt(cpi);
2039*77c1e3ccSAndroid Build Coastguard Worker     } else {
2040*77c1e3ccSAndroid Build Coastguard Worker       // Preallocate the pc_tree for realtime coding to reduce the cost of
2041*77c1e3ccSAndroid Build Coastguard Worker       // memory allocation.
2042*77c1e3ccSAndroid Build Coastguard Worker       const int use_nonrd_mode = cpi->sf.rt_sf.use_nonrd_pick_mode;
2043*77c1e3ccSAndroid Build Coastguard Worker       if (use_nonrd_mode) {
2044*77c1e3ccSAndroid Build Coastguard Worker         td->pc_root = av1_alloc_pc_tree_node(cm->seq_params->sb_size);
2045*77c1e3ccSAndroid Build Coastguard Worker         if (!td->pc_root)
2046*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
2047*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate PC_TREE");
2048*77c1e3ccSAndroid Build Coastguard Worker       } else {
2049*77c1e3ccSAndroid Build Coastguard Worker         td->pc_root = NULL;
2050*77c1e3ccSAndroid Build Coastguard Worker       }
2051*77c1e3ccSAndroid Build Coastguard Worker 
2052*77c1e3ccSAndroid Build Coastguard Worker       encode_tiles(cpi);
2053*77c1e3ccSAndroid Build Coastguard Worker       av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
2054*77c1e3ccSAndroid Build Coastguard Worker                                  cpi->sf.part_sf.partition_search_type);
2055*77c1e3ccSAndroid Build Coastguard Worker       td->pc_root = NULL;
2056*77c1e3ccSAndroid Build Coastguard Worker     }
2057*77c1e3ccSAndroid Build Coastguard Worker   }
2058*77c1e3ccSAndroid Build Coastguard Worker 
2059*77c1e3ccSAndroid Build Coastguard Worker   // If intrabc is allowed but never selected, reset the allow_intrabc flag.
2060*77c1e3ccSAndroid Build Coastguard Worker   if (features->allow_intrabc && !cpi->intrabc_used) {
2061*77c1e3ccSAndroid Build Coastguard Worker     features->allow_intrabc = 0;
2062*77c1e3ccSAndroid Build Coastguard Worker   }
2063*77c1e3ccSAndroid Build Coastguard Worker   if (features->allow_intrabc) {
2064*77c1e3ccSAndroid Build Coastguard Worker     cm->delta_q_info.delta_lf_present_flag = 0;
2065*77c1e3ccSAndroid Build Coastguard Worker   }
2066*77c1e3ccSAndroid Build Coastguard Worker 
2067*77c1e3ccSAndroid Build Coastguard Worker   if (cm->delta_q_info.delta_q_present_flag && cpi->deltaq_used == 0) {
2068*77c1e3ccSAndroid Build Coastguard Worker     cm->delta_q_info.delta_q_present_flag = 0;
2069*77c1e3ccSAndroid Build Coastguard Worker   }
2070*77c1e3ccSAndroid Build Coastguard Worker 
2071*77c1e3ccSAndroid Build Coastguard Worker   // Set the transform size appropriately before bitstream creation
2072*77c1e3ccSAndroid Build Coastguard Worker   const MODE_EVAL_TYPE eval_type =
2073*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.winner_mode_sf.enable_winner_mode_for_tx_size_srch
2074*77c1e3ccSAndroid Build Coastguard Worker           ? WINNER_MODE_EVAL
2075*77c1e3ccSAndroid Build Coastguard Worker           : DEFAULT_EVAL;
2076*77c1e3ccSAndroid Build Coastguard Worker   const TX_SIZE_SEARCH_METHOD tx_search_type =
2077*77c1e3ccSAndroid Build Coastguard Worker       cpi->winner_mode_params.tx_size_search_methods[eval_type];
2078*77c1e3ccSAndroid Build Coastguard Worker   assert(oxcf->txfm_cfg.enable_tx64 || tx_search_type != USE_LARGESTALL);
2079*77c1e3ccSAndroid Build Coastguard Worker   features->tx_mode = select_tx_mode(cm, tx_search_type);
2080*77c1e3ccSAndroid Build Coastguard Worker 
2081*77c1e3ccSAndroid Build Coastguard Worker   // Retain the frame level probability update conditions for parallel frames.
2082*77c1e3ccSAndroid Build Coastguard Worker   // These conditions will be consumed during postencode stage to update the
2083*77c1e3ccSAndroid Build Coastguard Worker   // probability.
2084*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
2085*77c1e3ccSAndroid Build Coastguard Worker     cpi->do_update_frame_probs_txtype[cpi->num_frame_recode] =
2086*77c1e3ccSAndroid Build Coastguard Worker         cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats;
2087*77c1e3ccSAndroid Build Coastguard Worker     cpi->do_update_frame_probs_obmc[cpi->num_frame_recode] =
2088*77c1e3ccSAndroid Build Coastguard Worker         (cpi->sf.inter_sf.prune_obmc_prob_thresh > 0 &&
2089*77c1e3ccSAndroid Build Coastguard Worker          cpi->sf.inter_sf.prune_obmc_prob_thresh < INT_MAX);
2090*77c1e3ccSAndroid Build Coastguard Worker     cpi->do_update_frame_probs_warp[cpi->num_frame_recode] =
2091*77c1e3ccSAndroid Build Coastguard Worker         (features->allow_warped_motion &&
2092*77c1e3ccSAndroid Build Coastguard Worker          cpi->sf.inter_sf.prune_warped_prob_thresh > 0);
2093*77c1e3ccSAndroid Build Coastguard Worker     cpi->do_update_frame_probs_interpfilter[cpi->num_frame_recode] =
2094*77c1e3ccSAndroid Build Coastguard Worker         (cm->current_frame.frame_type != KEY_FRAME &&
2095*77c1e3ccSAndroid Build Coastguard Worker          cpi->sf.interp_sf.adaptive_interp_filter_search == 2 &&
2096*77c1e3ccSAndroid Build Coastguard Worker          features->interp_filter == SWITCHABLE);
2097*77c1e3ccSAndroid Build Coastguard Worker   }
2098*77c1e3ccSAndroid Build Coastguard Worker 
2099*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats ||
2100*77c1e3ccSAndroid Build Coastguard Worker       ((cpi->sf.tx_sf.tx_type_search.fast_inter_tx_type_prob_thresh !=
2101*77c1e3ccSAndroid Build Coastguard Worker         INT_MAX) &&
2102*77c1e3ccSAndroid Build Coastguard Worker        (cpi->sf.tx_sf.tx_type_search.fast_inter_tx_type_prob_thresh != 0))) {
2103*77c1e3ccSAndroid Build Coastguard Worker     const FRAME_UPDATE_TYPE update_type =
2104*77c1e3ccSAndroid Build Coastguard Worker         get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
2105*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < TX_SIZES_ALL; i++) {
2106*77c1e3ccSAndroid Build Coastguard Worker       int sum = 0;
2107*77c1e3ccSAndroid Build Coastguard Worker       int j;
2108*77c1e3ccSAndroid Build Coastguard Worker       int left = MAX_TX_TYPE_PROB;
2109*77c1e3ccSAndroid Build Coastguard Worker 
2110*77c1e3ccSAndroid Build Coastguard Worker       for (j = 0; j < TX_TYPES; j++)
2111*77c1e3ccSAndroid Build Coastguard Worker         sum += cpi->td.rd_counts.tx_type_used[i][j];
2112*77c1e3ccSAndroid Build Coastguard Worker 
2113*77c1e3ccSAndroid Build Coastguard Worker       for (j = TX_TYPES - 1; j >= 0; j--) {
2114*77c1e3ccSAndroid Build Coastguard Worker         int update_txtype_frameprobs = 1;
2115*77c1e3ccSAndroid Build Coastguard Worker         const int new_prob =
2116*77c1e3ccSAndroid Build Coastguard Worker             sum ? (int)((int64_t)MAX_TX_TYPE_PROB *
2117*77c1e3ccSAndroid Build Coastguard Worker                         cpi->td.rd_counts.tx_type_used[i][j] / sum)
2118*77c1e3ccSAndroid Build Coastguard Worker                 : (j ? 0 : MAX_TX_TYPE_PROB);
2119*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2120*77c1e3ccSAndroid Build Coastguard Worker         if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
2121*77c1e3ccSAndroid Build Coastguard Worker           if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] ==
2122*77c1e3ccSAndroid Build Coastguard Worker               0) {
2123*77c1e3ccSAndroid Build Coastguard Worker             int prob =
2124*77c1e3ccSAndroid Build Coastguard Worker                 (temp_frame_probs_simulation->tx_type_probs[update_type][i][j] +
2125*77c1e3ccSAndroid Build Coastguard Worker                  new_prob) >>
2126*77c1e3ccSAndroid Build Coastguard Worker                 1;
2127*77c1e3ccSAndroid Build Coastguard Worker             left -= prob;
2128*77c1e3ccSAndroid Build Coastguard Worker             if (j == 0) prob += left;
2129*77c1e3ccSAndroid Build Coastguard Worker             temp_frame_probs_simulation->tx_type_probs[update_type][i][j] =
2130*77c1e3ccSAndroid Build Coastguard Worker                 prob;
2131*77c1e3ccSAndroid Build Coastguard Worker             // Copy temp_frame_probs_simulation to temp_frame_probs
2132*77c1e3ccSAndroid Build Coastguard Worker             for (int update_type_idx = 0; update_type_idx < FRAME_UPDATE_TYPES;
2133*77c1e3ccSAndroid Build Coastguard Worker                  update_type_idx++) {
2134*77c1e3ccSAndroid Build Coastguard Worker               temp_frame_probs->tx_type_probs[update_type_idx][i][j] =
2135*77c1e3ccSAndroid Build Coastguard Worker                   temp_frame_probs_simulation
2136*77c1e3ccSAndroid Build Coastguard Worker                       ->tx_type_probs[update_type_idx][i][j];
2137*77c1e3ccSAndroid Build Coastguard Worker             }
2138*77c1e3ccSAndroid Build Coastguard Worker           }
2139*77c1e3ccSAndroid Build Coastguard Worker           update_txtype_frameprobs = 0;
2140*77c1e3ccSAndroid Build Coastguard Worker         }
2141*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_FPMT_TEST
2142*77c1e3ccSAndroid Build Coastguard Worker         // Track the frame probabilities of parallel encode frames to update
2143*77c1e3ccSAndroid Build Coastguard Worker         // during postencode stage.
2144*77c1e3ccSAndroid Build Coastguard Worker         if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
2145*77c1e3ccSAndroid Build Coastguard Worker           update_txtype_frameprobs = 0;
2146*77c1e3ccSAndroid Build Coastguard Worker           cpi->frame_new_probs[cpi->num_frame_recode]
2147*77c1e3ccSAndroid Build Coastguard Worker               .tx_type_probs[update_type][i][j] = new_prob;
2148*77c1e3ccSAndroid Build Coastguard Worker         }
2149*77c1e3ccSAndroid Build Coastguard Worker         if (update_txtype_frameprobs) {
2150*77c1e3ccSAndroid Build Coastguard Worker           int prob =
2151*77c1e3ccSAndroid Build Coastguard Worker               (frame_probs->tx_type_probs[update_type][i][j] + new_prob) >> 1;
2152*77c1e3ccSAndroid Build Coastguard Worker           left -= prob;
2153*77c1e3ccSAndroid Build Coastguard Worker           if (j == 0) prob += left;
2154*77c1e3ccSAndroid Build Coastguard Worker           frame_probs->tx_type_probs[update_type][i][j] = prob;
2155*77c1e3ccSAndroid Build Coastguard Worker         }
2156*77c1e3ccSAndroid Build Coastguard Worker       }
2157*77c1e3ccSAndroid Build Coastguard Worker     }
2158*77c1e3ccSAndroid Build Coastguard Worker   }
2159*77c1e3ccSAndroid Build Coastguard Worker 
2160*77c1e3ccSAndroid Build Coastguard Worker   if (cm->seg.enabled) {
2161*77c1e3ccSAndroid Build Coastguard Worker     cm->seg.temporal_update = 1;
2162*77c1e3ccSAndroid Build Coastguard Worker     if (rdc->seg_tmp_pred_cost[0] < rdc->seg_tmp_pred_cost[1])
2163*77c1e3ccSAndroid Build Coastguard Worker       cm->seg.temporal_update = 0;
2164*77c1e3ccSAndroid Build Coastguard Worker   }
2165*77c1e3ccSAndroid Build Coastguard Worker 
2166*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.inter_sf.prune_obmc_prob_thresh > 0 &&
2167*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.inter_sf.prune_obmc_prob_thresh < INT_MAX) {
2168*77c1e3ccSAndroid Build Coastguard Worker     const FRAME_UPDATE_TYPE update_type =
2169*77c1e3ccSAndroid Build Coastguard Worker         get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
2170*77c1e3ccSAndroid Build Coastguard Worker 
2171*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < BLOCK_SIZES_ALL; i++) {
2172*77c1e3ccSAndroid Build Coastguard Worker       int sum = 0;
2173*77c1e3ccSAndroid Build Coastguard Worker       int update_obmc_frameprobs = 1;
2174*77c1e3ccSAndroid Build Coastguard Worker       for (int j = 0; j < 2; j++) sum += cpi->td.rd_counts.obmc_used[i][j];
2175*77c1e3ccSAndroid Build Coastguard Worker 
2176*77c1e3ccSAndroid Build Coastguard Worker       const int new_prob =
2177*77c1e3ccSAndroid Build Coastguard Worker           sum ? 128 * cpi->td.rd_counts.obmc_used[i][1] / sum : 0;
2178*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2179*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
2180*77c1e3ccSAndroid Build Coastguard Worker         if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) {
2181*77c1e3ccSAndroid Build Coastguard Worker           temp_frame_probs_simulation->obmc_probs[update_type][i] =
2182*77c1e3ccSAndroid Build Coastguard Worker               (temp_frame_probs_simulation->obmc_probs[update_type][i] +
2183*77c1e3ccSAndroid Build Coastguard Worker                new_prob) >>
2184*77c1e3ccSAndroid Build Coastguard Worker               1;
2185*77c1e3ccSAndroid Build Coastguard Worker           // Copy temp_frame_probs_simulation to temp_frame_probs
2186*77c1e3ccSAndroid Build Coastguard Worker           for (int update_type_idx = 0; update_type_idx < FRAME_UPDATE_TYPES;
2187*77c1e3ccSAndroid Build Coastguard Worker                update_type_idx++) {
2188*77c1e3ccSAndroid Build Coastguard Worker             temp_frame_probs->obmc_probs[update_type_idx][i] =
2189*77c1e3ccSAndroid Build Coastguard Worker                 temp_frame_probs_simulation->obmc_probs[update_type_idx][i];
2190*77c1e3ccSAndroid Build Coastguard Worker           }
2191*77c1e3ccSAndroid Build Coastguard Worker         }
2192*77c1e3ccSAndroid Build Coastguard Worker         update_obmc_frameprobs = 0;
2193*77c1e3ccSAndroid Build Coastguard Worker       }
2194*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_FPMT_TEST
2195*77c1e3ccSAndroid Build Coastguard Worker       // Track the frame probabilities of parallel encode frames to update
2196*77c1e3ccSAndroid Build Coastguard Worker       // during postencode stage.
2197*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
2198*77c1e3ccSAndroid Build Coastguard Worker         update_obmc_frameprobs = 0;
2199*77c1e3ccSAndroid Build Coastguard Worker         cpi->frame_new_probs[cpi->num_frame_recode].obmc_probs[update_type][i] =
2200*77c1e3ccSAndroid Build Coastguard Worker             new_prob;
2201*77c1e3ccSAndroid Build Coastguard Worker       }
2202*77c1e3ccSAndroid Build Coastguard Worker       if (update_obmc_frameprobs) {
2203*77c1e3ccSAndroid Build Coastguard Worker         frame_probs->obmc_probs[update_type][i] =
2204*77c1e3ccSAndroid Build Coastguard Worker             (frame_probs->obmc_probs[update_type][i] + new_prob) >> 1;
2205*77c1e3ccSAndroid Build Coastguard Worker       }
2206*77c1e3ccSAndroid Build Coastguard Worker     }
2207*77c1e3ccSAndroid Build Coastguard Worker   }
2208*77c1e3ccSAndroid Build Coastguard Worker 
2209*77c1e3ccSAndroid Build Coastguard Worker   if (features->allow_warped_motion &&
2210*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.inter_sf.prune_warped_prob_thresh > 0) {
2211*77c1e3ccSAndroid Build Coastguard Worker     const FRAME_UPDATE_TYPE update_type =
2212*77c1e3ccSAndroid Build Coastguard Worker         get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
2213*77c1e3ccSAndroid Build Coastguard Worker     int update_warp_frameprobs = 1;
2214*77c1e3ccSAndroid Build Coastguard Worker     int sum = 0;
2215*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < 2; i++) sum += cpi->td.rd_counts.warped_used[i];
2216*77c1e3ccSAndroid Build Coastguard Worker     const int new_prob = sum ? 128 * cpi->td.rd_counts.warped_used[1] / sum : 0;
2217*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2218*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
2219*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) {
2220*77c1e3ccSAndroid Build Coastguard Worker         temp_frame_probs_simulation->warped_probs[update_type] =
2221*77c1e3ccSAndroid Build Coastguard Worker             (temp_frame_probs_simulation->warped_probs[update_type] +
2222*77c1e3ccSAndroid Build Coastguard Worker              new_prob) >>
2223*77c1e3ccSAndroid Build Coastguard Worker             1;
2224*77c1e3ccSAndroid Build Coastguard Worker         // Copy temp_frame_probs_simulation to temp_frame_probs
2225*77c1e3ccSAndroid Build Coastguard Worker         for (int update_type_idx = 0; update_type_idx < FRAME_UPDATE_TYPES;
2226*77c1e3ccSAndroid Build Coastguard Worker              update_type_idx++) {
2227*77c1e3ccSAndroid Build Coastguard Worker           temp_frame_probs->warped_probs[update_type_idx] =
2228*77c1e3ccSAndroid Build Coastguard Worker               temp_frame_probs_simulation->warped_probs[update_type_idx];
2229*77c1e3ccSAndroid Build Coastguard Worker         }
2230*77c1e3ccSAndroid Build Coastguard Worker       }
2231*77c1e3ccSAndroid Build Coastguard Worker       update_warp_frameprobs = 0;
2232*77c1e3ccSAndroid Build Coastguard Worker     }
2233*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_FPMT_TEST
2234*77c1e3ccSAndroid Build Coastguard Worker     // Track the frame probabilities of parallel encode frames to update
2235*77c1e3ccSAndroid Build Coastguard Worker     // during postencode stage.
2236*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
2237*77c1e3ccSAndroid Build Coastguard Worker       update_warp_frameprobs = 0;
2238*77c1e3ccSAndroid Build Coastguard Worker       cpi->frame_new_probs[cpi->num_frame_recode].warped_probs[update_type] =
2239*77c1e3ccSAndroid Build Coastguard Worker           new_prob;
2240*77c1e3ccSAndroid Build Coastguard Worker     }
2241*77c1e3ccSAndroid Build Coastguard Worker     if (update_warp_frameprobs) {
2242*77c1e3ccSAndroid Build Coastguard Worker       frame_probs->warped_probs[update_type] =
2243*77c1e3ccSAndroid Build Coastguard Worker           (frame_probs->warped_probs[update_type] + new_prob) >> 1;
2244*77c1e3ccSAndroid Build Coastguard Worker     }
2245*77c1e3ccSAndroid Build Coastguard Worker   }
2246*77c1e3ccSAndroid Build Coastguard Worker 
2247*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.frame_type != KEY_FRAME &&
2248*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.interp_sf.adaptive_interp_filter_search == 2 &&
2249*77c1e3ccSAndroid Build Coastguard Worker       features->interp_filter == SWITCHABLE) {
2250*77c1e3ccSAndroid Build Coastguard Worker     const FRAME_UPDATE_TYPE update_type =
2251*77c1e3ccSAndroid Build Coastguard Worker         get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
2252*77c1e3ccSAndroid Build Coastguard Worker 
2253*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
2254*77c1e3ccSAndroid Build Coastguard Worker       int sum = 0;
2255*77c1e3ccSAndroid Build Coastguard Worker       int j;
2256*77c1e3ccSAndroid Build Coastguard Worker       int left = 1536;
2257*77c1e3ccSAndroid Build Coastguard Worker 
2258*77c1e3ccSAndroid Build Coastguard Worker       for (j = 0; j < SWITCHABLE_FILTERS; j++) {
2259*77c1e3ccSAndroid Build Coastguard Worker         sum += cpi->td.counts->switchable_interp[i][j];
2260*77c1e3ccSAndroid Build Coastguard Worker       }
2261*77c1e3ccSAndroid Build Coastguard Worker 
2262*77c1e3ccSAndroid Build Coastguard Worker       for (j = SWITCHABLE_FILTERS - 1; j >= 0; j--) {
2263*77c1e3ccSAndroid Build Coastguard Worker         int update_interpfilter_frameprobs = 1;
2264*77c1e3ccSAndroid Build Coastguard Worker         const int new_prob =
2265*77c1e3ccSAndroid Build Coastguard Worker             sum ? 1536 * cpi->td.counts->switchable_interp[i][j] / sum
2266*77c1e3ccSAndroid Build Coastguard Worker                 : (j ? 0 : 1536);
2267*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2268*77c1e3ccSAndroid Build Coastguard Worker         if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
2269*77c1e3ccSAndroid Build Coastguard Worker           if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] ==
2270*77c1e3ccSAndroid Build Coastguard Worker               0) {
2271*77c1e3ccSAndroid Build Coastguard Worker             int prob = (temp_frame_probs_simulation
2272*77c1e3ccSAndroid Build Coastguard Worker                             ->switchable_interp_probs[update_type][i][j] +
2273*77c1e3ccSAndroid Build Coastguard Worker                         new_prob) >>
2274*77c1e3ccSAndroid Build Coastguard Worker                        1;
2275*77c1e3ccSAndroid Build Coastguard Worker             left -= prob;
2276*77c1e3ccSAndroid Build Coastguard Worker             if (j == 0) prob += left;
2277*77c1e3ccSAndroid Build Coastguard Worker             temp_frame_probs_simulation
2278*77c1e3ccSAndroid Build Coastguard Worker                 ->switchable_interp_probs[update_type][i][j] = prob;
2279*77c1e3ccSAndroid Build Coastguard Worker             // Copy temp_frame_probs_simulation to temp_frame_probs
2280*77c1e3ccSAndroid Build Coastguard Worker             for (int update_type_idx = 0; update_type_idx < FRAME_UPDATE_TYPES;
2281*77c1e3ccSAndroid Build Coastguard Worker                  update_type_idx++) {
2282*77c1e3ccSAndroid Build Coastguard Worker               temp_frame_probs->switchable_interp_probs[update_type_idx][i][j] =
2283*77c1e3ccSAndroid Build Coastguard Worker                   temp_frame_probs_simulation
2284*77c1e3ccSAndroid Build Coastguard Worker                       ->switchable_interp_probs[update_type_idx][i][j];
2285*77c1e3ccSAndroid Build Coastguard Worker             }
2286*77c1e3ccSAndroid Build Coastguard Worker           }
2287*77c1e3ccSAndroid Build Coastguard Worker           update_interpfilter_frameprobs = 0;
2288*77c1e3ccSAndroid Build Coastguard Worker         }
2289*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_FPMT_TEST
2290*77c1e3ccSAndroid Build Coastguard Worker         // Track the frame probabilities of parallel encode frames to update
2291*77c1e3ccSAndroid Build Coastguard Worker         // during postencode stage.
2292*77c1e3ccSAndroid Build Coastguard Worker         if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
2293*77c1e3ccSAndroid Build Coastguard Worker           update_interpfilter_frameprobs = 0;
2294*77c1e3ccSAndroid Build Coastguard Worker           cpi->frame_new_probs[cpi->num_frame_recode]
2295*77c1e3ccSAndroid Build Coastguard Worker               .switchable_interp_probs[update_type][i][j] = new_prob;
2296*77c1e3ccSAndroid Build Coastguard Worker         }
2297*77c1e3ccSAndroid Build Coastguard Worker         if (update_interpfilter_frameprobs) {
2298*77c1e3ccSAndroid Build Coastguard Worker           int prob = (frame_probs->switchable_interp_probs[update_type][i][j] +
2299*77c1e3ccSAndroid Build Coastguard Worker                       new_prob) >>
2300*77c1e3ccSAndroid Build Coastguard Worker                      1;
2301*77c1e3ccSAndroid Build Coastguard Worker           left -= prob;
2302*77c1e3ccSAndroid Build Coastguard Worker           if (j == 0) prob += left;
2303*77c1e3ccSAndroid Build Coastguard Worker           frame_probs->switchable_interp_probs[update_type][i][j] = prob;
2304*77c1e3ccSAndroid Build Coastguard Worker         }
2305*77c1e3ccSAndroid Build Coastguard Worker       }
2306*77c1e3ccSAndroid Build Coastguard Worker     }
2307*77c1e3ccSAndroid Build Coastguard Worker   }
2308*77c1e3ccSAndroid Build Coastguard Worker   if (hash_table_created) {
2309*77c1e3ccSAndroid Build Coastguard Worker     av1_hash_table_destroy(&intrabc_hash_info->intrabc_hash_table);
2310*77c1e3ccSAndroid Build Coastguard Worker   }
2311*77c1e3ccSAndroid Build Coastguard Worker }
2312*77c1e3ccSAndroid Build Coastguard Worker 
2313*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Setup reference frame buffers and encode a frame
2314*77c1e3ccSAndroid Build Coastguard Worker  *
2315*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup high_level_algo
2316*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
2317*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
2318*77c1e3ccSAndroid Build Coastguard Worker  *
2319*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi    Top-level encoder structure
2320*77c1e3ccSAndroid Build Coastguard Worker  */
av1_encode_frame(AV1_COMP * cpi)2321*77c1e3ccSAndroid Build Coastguard Worker void av1_encode_frame(AV1_COMP *cpi) {
2322*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2323*77c1e3ccSAndroid Build Coastguard Worker   CurrentFrame *const current_frame = &cm->current_frame;
2324*77c1e3ccSAndroid Build Coastguard Worker   FeatureFlags *const features = &cm->features;
2325*77c1e3ccSAndroid Build Coastguard Worker   RD_COUNTS *const rdc = &cpi->td.rd_counts;
2326*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2327*77c1e3ccSAndroid Build Coastguard Worker   // Indicates whether or not to use a default reduced set for ext-tx
2328*77c1e3ccSAndroid Build Coastguard Worker   // rather than the potential full set of 16 transforms
2329*77c1e3ccSAndroid Build Coastguard Worker   features->reduced_tx_set_used = oxcf->txfm_cfg.reduced_tx_type_set;
2330*77c1e3ccSAndroid Build Coastguard Worker 
2331*77c1e3ccSAndroid Build Coastguard Worker   // Make sure segment_id is no larger than last_active_segid.
2332*77c1e3ccSAndroid Build Coastguard Worker   if (cm->seg.enabled && cm->seg.update_map) {
2333*77c1e3ccSAndroid Build Coastguard Worker     const int mi_rows = cm->mi_params.mi_rows;
2334*77c1e3ccSAndroid Build Coastguard Worker     const int mi_cols = cm->mi_params.mi_cols;
2335*77c1e3ccSAndroid Build Coastguard Worker     const int last_active_segid = cm->seg.last_active_segid;
2336*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *map = cpi->enc_seg.map;
2337*77c1e3ccSAndroid Build Coastguard Worker     for (int mi_row = 0; mi_row < mi_rows; ++mi_row) {
2338*77c1e3ccSAndroid Build Coastguard Worker       for (int mi_col = 0; mi_col < mi_cols; ++mi_col) {
2339*77c1e3ccSAndroid Build Coastguard Worker         map[mi_col] = AOMMIN(map[mi_col], last_active_segid);
2340*77c1e3ccSAndroid Build Coastguard Worker       }
2341*77c1e3ccSAndroid Build Coastguard Worker       map += mi_cols;
2342*77c1e3ccSAndroid Build Coastguard Worker     }
2343*77c1e3ccSAndroid Build Coastguard Worker   }
2344*77c1e3ccSAndroid Build Coastguard Worker 
2345*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_frame_buf_refs(cm);
2346*77c1e3ccSAndroid Build Coastguard Worker   enforce_max_ref_frames(cpi, &cpi->ref_frame_flags,
2347*77c1e3ccSAndroid Build Coastguard Worker                          cm->cur_frame->ref_display_order_hint,
2348*77c1e3ccSAndroid Build Coastguard Worker                          cm->current_frame.display_order_hint);
2349*77c1e3ccSAndroid Build Coastguard Worker   set_rel_frame_dist(&cpi->common, &cpi->ref_frame_dist_info,
2350*77c1e3ccSAndroid Build Coastguard Worker                      cpi->ref_frame_flags);
2351*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_frame_sign_bias(cm);
2352*77c1e3ccSAndroid Build Coastguard Worker 
2353*77c1e3ccSAndroid Build Coastguard Worker   // If global motion is enabled, then every buffer which is used as either
2354*77c1e3ccSAndroid Build Coastguard Worker   // a source or a ref frame should have an image pyramid allocated.
2355*77c1e3ccSAndroid Build Coastguard Worker   // Check here so that issues can be caught early in debug mode
2356*77c1e3ccSAndroid Build Coastguard Worker #if !defined(NDEBUG) && !CONFIG_REALTIME_ONLY
2357*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->alloc_pyramid) {
2358*77c1e3ccSAndroid Build Coastguard Worker     assert(cpi->source->y_pyramid);
2359*77c1e3ccSAndroid Build Coastguard Worker     for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2360*77c1e3ccSAndroid Build Coastguard Worker       const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
2361*77c1e3ccSAndroid Build Coastguard Worker       if (buf != NULL) {
2362*77c1e3ccSAndroid Build Coastguard Worker         assert(buf->buf.y_pyramid);
2363*77c1e3ccSAndroid Build Coastguard Worker       }
2364*77c1e3ccSAndroid Build Coastguard Worker     }
2365*77c1e3ccSAndroid Build Coastguard Worker   }
2366*77c1e3ccSAndroid Build Coastguard Worker #endif  // !defined(NDEBUG) && !CONFIG_REALTIME_ONLY
2367*77c1e3ccSAndroid Build Coastguard Worker 
2368*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MISMATCH_DEBUG
2369*77c1e3ccSAndroid Build Coastguard Worker   mismatch_reset_frame(av1_num_planes(cm));
2370*77c1e3ccSAndroid Build Coastguard Worker #endif
2371*77c1e3ccSAndroid Build Coastguard Worker 
2372*77c1e3ccSAndroid Build Coastguard Worker   rdc->newmv_or_intra_blocks = 0;
2373*77c1e3ccSAndroid Build Coastguard Worker   cpi->palette_pixel_num = 0;
2374*77c1e3ccSAndroid Build Coastguard Worker 
2375*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.hl_sf.frame_parameter_update ||
2376*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.rt_sf.use_comp_ref_nonrd) {
2377*77c1e3ccSAndroid Build Coastguard Worker     if (frame_is_intra_only(cm))
2378*77c1e3ccSAndroid Build Coastguard Worker       current_frame->reference_mode = SINGLE_REFERENCE;
2379*77c1e3ccSAndroid Build Coastguard Worker     else
2380*77c1e3ccSAndroid Build Coastguard Worker       current_frame->reference_mode = REFERENCE_MODE_SELECT;
2381*77c1e3ccSAndroid Build Coastguard Worker 
2382*77c1e3ccSAndroid Build Coastguard Worker     features->interp_filter = SWITCHABLE;
2383*77c1e3ccSAndroid Build Coastguard Worker     if (cm->tiles.large_scale) features->interp_filter = EIGHTTAP_REGULAR;
2384*77c1e3ccSAndroid Build Coastguard Worker 
2385*77c1e3ccSAndroid Build Coastguard Worker     features->switchable_motion_mode = is_switchable_motion_mode_allowed(
2386*77c1e3ccSAndroid Build Coastguard Worker         features->allow_warped_motion, oxcf->motion_mode_cfg.enable_obmc);
2387*77c1e3ccSAndroid Build Coastguard Worker 
2388*77c1e3ccSAndroid Build Coastguard Worker     rdc->compound_ref_used_flag = 0;
2389*77c1e3ccSAndroid Build Coastguard Worker     rdc->skip_mode_used_flag = 0;
2390*77c1e3ccSAndroid Build Coastguard Worker 
2391*77c1e3ccSAndroid Build Coastguard Worker     encode_frame_internal(cpi);
2392*77c1e3ccSAndroid Build Coastguard Worker 
2393*77c1e3ccSAndroid Build Coastguard Worker     if (current_frame->reference_mode == REFERENCE_MODE_SELECT) {
2394*77c1e3ccSAndroid Build Coastguard Worker       // Use a flag that includes 4x4 blocks
2395*77c1e3ccSAndroid Build Coastguard Worker       if (rdc->compound_ref_used_flag == 0) {
2396*77c1e3ccSAndroid Build Coastguard Worker         current_frame->reference_mode = SINGLE_REFERENCE;
2397*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
2398*77c1e3ccSAndroid Build Coastguard Worker         av1_zero(cpi->td.counts->comp_inter);
2399*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
2400*77c1e3ccSAndroid Build Coastguard Worker       }
2401*77c1e3ccSAndroid Build Coastguard Worker     }
2402*77c1e3ccSAndroid Build Coastguard Worker     // Re-check on the skip mode status as reference mode may have been
2403*77c1e3ccSAndroid Build Coastguard Worker     // changed.
2404*77c1e3ccSAndroid Build Coastguard Worker     SkipModeInfo *const skip_mode_info = &current_frame->skip_mode_info;
2405*77c1e3ccSAndroid Build Coastguard Worker     if (frame_is_intra_only(cm) ||
2406*77c1e3ccSAndroid Build Coastguard Worker         current_frame->reference_mode == SINGLE_REFERENCE) {
2407*77c1e3ccSAndroid Build Coastguard Worker       skip_mode_info->skip_mode_allowed = 0;
2408*77c1e3ccSAndroid Build Coastguard Worker       skip_mode_info->skip_mode_flag = 0;
2409*77c1e3ccSAndroid Build Coastguard Worker     }
2410*77c1e3ccSAndroid Build Coastguard Worker     if (skip_mode_info->skip_mode_flag && rdc->skip_mode_used_flag == 0)
2411*77c1e3ccSAndroid Build Coastguard Worker       skip_mode_info->skip_mode_flag = 0;
2412*77c1e3ccSAndroid Build Coastguard Worker 
2413*77c1e3ccSAndroid Build Coastguard Worker     if (!cm->tiles.large_scale) {
2414*77c1e3ccSAndroid Build Coastguard Worker       if (features->tx_mode == TX_MODE_SELECT &&
2415*77c1e3ccSAndroid Build Coastguard Worker           cpi->td.mb.txfm_search_info.txb_split_count == 0)
2416*77c1e3ccSAndroid Build Coastguard Worker         features->tx_mode = TX_MODE_LARGEST;
2417*77c1e3ccSAndroid Build Coastguard Worker     }
2418*77c1e3ccSAndroid Build Coastguard Worker   } else {
2419*77c1e3ccSAndroid Build Coastguard Worker     // This is needed if real-time speed setting is changed on the fly
2420*77c1e3ccSAndroid Build Coastguard Worker     // from one using compound prediction to one using single reference.
2421*77c1e3ccSAndroid Build Coastguard Worker     if (current_frame->reference_mode == REFERENCE_MODE_SELECT)
2422*77c1e3ccSAndroid Build Coastguard Worker       current_frame->reference_mode = SINGLE_REFERENCE;
2423*77c1e3ccSAndroid Build Coastguard Worker     encode_frame_internal(cpi);
2424*77c1e3ccSAndroid Build Coastguard Worker   }
2425*77c1e3ccSAndroid Build Coastguard Worker }
2426