xref: /aosp_15_r20/external/libaom/av1/encoder/encoder.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 <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <float.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <stdbool.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
18*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
19*77c1e3ccSAndroid Build Coastguard Worker #include <time.h>
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/scale.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
24*77c1e3ccSAndroid Build Coastguard Worker 
25*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomcx.h"
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_DENOISE
28*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/grain_table.h"
29*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/noise_util.h"
30*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/noise_model.h"
31*77c1e3ccSAndroid Build Coastguard Worker #endif
32*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/flow_estimation/corner_detect.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/psnr.h"
34*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
35*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/ssim.h"
36*77c1e3ccSAndroid Build Coastguard Worker #endif
37*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/aom_timer.h"
38*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem.h"
39*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/aom_pthread.h"
40*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
41*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/debug_util.h"
42*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_BITSTREAM_DEBUG
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/alloccommon.h"
45*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/debugmodes.h"
46*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/filter.h"
47*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/idct.h"
48*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconinter.h"
49*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconintra.h"
50*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/resize.h"
51*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/tile_common.h"
52*77c1e3ccSAndroid Build Coastguard Worker 
53*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/allintra_vis.h"
54*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_complexity.h"
55*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_cyclicrefresh.h"
56*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_variance.h"
57*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/bitstream.h"
58*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
59*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/blockiness.h"
60*77c1e3ccSAndroid Build Coastguard Worker #endif
61*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/context_tree.h"
62*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/dwt.h"
63*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe.h"
64*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemv.h"
65*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encode_strategy.h"
66*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
67*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder_alloc.h"
68*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder_utils.h"
69*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodetxb.h"
70*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/ethread.h"
71*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/firstpass.h"
72*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/hash_motion.h"
73*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/hybrid_fwd_txfm.h"
74*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/intra_mode_search.h"
75*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/mv_prec.h"
76*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/pass2_strategy.h"
77*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/pickcdef.h"
78*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/picklpf.h"
79*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/pickrst.h"
80*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/random.h"
81*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/ratectrl.h"
82*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/rc_utils.h"
83*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/rd.h"
84*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/rdopt.h"
85*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_SALIENCY_MAP
86*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/saliency_map.h"
87*77c1e3ccSAndroid Build Coastguard Worker #endif
88*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/segmentation.h"
89*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/speed_features.h"
90*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/superres_scale.h"
91*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS
92*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/thirdpass.h"
93*77c1e3ccSAndroid Build Coastguard Worker #endif
94*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tpl_model.h"
95*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/reconinter_enc.h"
96*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/var_based_part.h"
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker #define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
99*77c1e3ccSAndroid Build Coastguard Worker 
100*77c1e3ccSAndroid Build Coastguard Worker // #define OUTPUT_YUV_REC
101*77c1e3ccSAndroid Build Coastguard Worker #ifdef OUTPUT_YUV_REC
102*77c1e3ccSAndroid Build Coastguard Worker FILE *yuv_rec_file;
103*77c1e3ccSAndroid Build Coastguard Worker #define FILE_NAME_LEN 100
104*77c1e3ccSAndroid Build Coastguard Worker #endif
105*77c1e3ccSAndroid Build Coastguard Worker 
106*77c1e3ccSAndroid Build Coastguard Worker #ifdef OUTPUT_YUV_DENOISED
107*77c1e3ccSAndroid Build Coastguard Worker FILE *yuv_denoised_file = NULL;
108*77c1e3ccSAndroid Build Coastguard Worker #endif
109*77c1e3ccSAndroid Build Coastguard Worker 
Scale2Ratio(AOM_SCALING_MODE mode,int * hr,int * hs)110*77c1e3ccSAndroid Build Coastguard Worker static inline void Scale2Ratio(AOM_SCALING_MODE mode, int *hr, int *hs) {
111*77c1e3ccSAndroid Build Coastguard Worker   switch (mode) {
112*77c1e3ccSAndroid Build Coastguard Worker     case AOME_NORMAL:
113*77c1e3ccSAndroid Build Coastguard Worker       *hr = 1;
114*77c1e3ccSAndroid Build Coastguard Worker       *hs = 1;
115*77c1e3ccSAndroid Build Coastguard Worker       break;
116*77c1e3ccSAndroid Build Coastguard Worker     case AOME_FOURFIVE:
117*77c1e3ccSAndroid Build Coastguard Worker       *hr = 4;
118*77c1e3ccSAndroid Build Coastguard Worker       *hs = 5;
119*77c1e3ccSAndroid Build Coastguard Worker       break;
120*77c1e3ccSAndroid Build Coastguard Worker     case AOME_THREEFIVE:
121*77c1e3ccSAndroid Build Coastguard Worker       *hr = 3;
122*77c1e3ccSAndroid Build Coastguard Worker       *hs = 5;
123*77c1e3ccSAndroid Build Coastguard Worker       break;
124*77c1e3ccSAndroid Build Coastguard Worker     case AOME_THREEFOUR:
125*77c1e3ccSAndroid Build Coastguard Worker       *hr = 3;
126*77c1e3ccSAndroid Build Coastguard Worker       *hs = 4;
127*77c1e3ccSAndroid Build Coastguard Worker       break;
128*77c1e3ccSAndroid Build Coastguard Worker     case AOME_ONEFOUR:
129*77c1e3ccSAndroid Build Coastguard Worker       *hr = 1;
130*77c1e3ccSAndroid Build Coastguard Worker       *hs = 4;
131*77c1e3ccSAndroid Build Coastguard Worker       break;
132*77c1e3ccSAndroid Build Coastguard Worker     case AOME_ONEEIGHT:
133*77c1e3ccSAndroid Build Coastguard Worker       *hr = 1;
134*77c1e3ccSAndroid Build Coastguard Worker       *hs = 8;
135*77c1e3ccSAndroid Build Coastguard Worker       break;
136*77c1e3ccSAndroid Build Coastguard Worker     case AOME_ONETWO:
137*77c1e3ccSAndroid Build Coastguard Worker       *hr = 1;
138*77c1e3ccSAndroid Build Coastguard Worker       *hs = 2;
139*77c1e3ccSAndroid Build Coastguard Worker       break;
140*77c1e3ccSAndroid Build Coastguard Worker     case AOME_TWOTHREE:
141*77c1e3ccSAndroid Build Coastguard Worker       *hr = 2;
142*77c1e3ccSAndroid Build Coastguard Worker       *hs = 3;
143*77c1e3ccSAndroid Build Coastguard Worker       break;
144*77c1e3ccSAndroid Build Coastguard Worker     case AOME_ONETHREE:
145*77c1e3ccSAndroid Build Coastguard Worker       *hr = 1;
146*77c1e3ccSAndroid Build Coastguard Worker       *hs = 3;
147*77c1e3ccSAndroid Build Coastguard Worker       break;
148*77c1e3ccSAndroid Build Coastguard Worker     default:
149*77c1e3ccSAndroid Build Coastguard Worker       *hr = 1;
150*77c1e3ccSAndroid Build Coastguard Worker       *hs = 1;
151*77c1e3ccSAndroid Build Coastguard Worker       assert(0);
152*77c1e3ccSAndroid Build Coastguard Worker       break;
153*77c1e3ccSAndroid Build Coastguard Worker   }
154*77c1e3ccSAndroid Build Coastguard Worker }
155*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_active_map(AV1_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)156*77c1e3ccSAndroid Build Coastguard Worker int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
157*77c1e3ccSAndroid Build Coastguard Worker                        int cols) {
158*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
159*77c1e3ccSAndroid Build Coastguard Worker   if (rows == mi_params->mb_rows && cols == mi_params->mb_cols) {
160*77c1e3ccSAndroid Build Coastguard Worker     unsigned char *const active_map_4x4 = cpi->active_map.map;
161*77c1e3ccSAndroid Build Coastguard Worker     const int mi_rows = mi_params->mi_rows;
162*77c1e3ccSAndroid Build Coastguard Worker     const int mi_cols = mi_params->mi_cols;
163*77c1e3ccSAndroid Build Coastguard Worker     cpi->active_map.update = 0;
164*77c1e3ccSAndroid Build Coastguard Worker     cpi->rc.percent_blocks_inactive = 0;
165*77c1e3ccSAndroid Build Coastguard Worker     assert(mi_rows % 2 == 0 && mi_rows > 0);
166*77c1e3ccSAndroid Build Coastguard Worker     assert(mi_cols % 2 == 0 && mi_cols > 0);
167*77c1e3ccSAndroid Build Coastguard Worker     if (new_map_16x16) {
168*77c1e3ccSAndroid Build Coastguard Worker       int num_samples = 0;
169*77c1e3ccSAndroid Build Coastguard Worker       int num_blocks_inactive = 0;
170*77c1e3ccSAndroid Build Coastguard Worker       for (int r = 0; r < mi_rows; r += 4) {
171*77c1e3ccSAndroid Build Coastguard Worker         for (int c = 0; c < mi_cols; c += 4) {
172*77c1e3ccSAndroid Build Coastguard Worker           const uint8_t val = new_map_16x16[(r >> 2) * cols + (c >> 2)]
173*77c1e3ccSAndroid Build Coastguard Worker                                   ? AM_SEGMENT_ID_ACTIVE
174*77c1e3ccSAndroid Build Coastguard Worker                                   : AM_SEGMENT_ID_INACTIVE;
175*77c1e3ccSAndroid Build Coastguard Worker           num_samples++;
176*77c1e3ccSAndroid Build Coastguard Worker           if (val == AM_SEGMENT_ID_INACTIVE) num_blocks_inactive++;
177*77c1e3ccSAndroid Build Coastguard Worker           const int row_max = AOMMIN(4, mi_rows - r);
178*77c1e3ccSAndroid Build Coastguard Worker           const int col_max = AOMMIN(4, mi_cols - c);
179*77c1e3ccSAndroid Build Coastguard Worker           for (int x = 0; x < row_max; ++x) {
180*77c1e3ccSAndroid Build Coastguard Worker             for (int y = 0; y < col_max; ++y) {
181*77c1e3ccSAndroid Build Coastguard Worker               active_map_4x4[(r + x) * mi_cols + (c + y)] = val;
182*77c1e3ccSAndroid Build Coastguard Worker             }
183*77c1e3ccSAndroid Build Coastguard Worker           }
184*77c1e3ccSAndroid Build Coastguard Worker         }
185*77c1e3ccSAndroid Build Coastguard Worker       }
186*77c1e3ccSAndroid Build Coastguard Worker       cpi->active_map.enabled = 1;
187*77c1e3ccSAndroid Build Coastguard Worker       cpi->active_map.update = 1;
188*77c1e3ccSAndroid Build Coastguard Worker       assert(num_samples);
189*77c1e3ccSAndroid Build Coastguard Worker       cpi->rc.percent_blocks_inactive =
190*77c1e3ccSAndroid Build Coastguard Worker           (num_blocks_inactive * 100) / num_samples;
191*77c1e3ccSAndroid Build Coastguard Worker     }
192*77c1e3ccSAndroid Build Coastguard Worker     return 0;
193*77c1e3ccSAndroid Build Coastguard Worker   }
194*77c1e3ccSAndroid Build Coastguard Worker 
195*77c1e3ccSAndroid Build Coastguard Worker   return -1;
196*77c1e3ccSAndroid Build Coastguard Worker }
197*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_active_map(AV1_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)198*77c1e3ccSAndroid Build Coastguard Worker int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
199*77c1e3ccSAndroid Build Coastguard Worker                        int cols) {
200*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
201*77c1e3ccSAndroid Build Coastguard Worker   if (rows == mi_params->mb_rows && cols == mi_params->mb_cols &&
202*77c1e3ccSAndroid Build Coastguard Worker       new_map_16x16) {
203*77c1e3ccSAndroid Build Coastguard Worker     unsigned char *const seg_map_8x8 = cpi->enc_seg.map;
204*77c1e3ccSAndroid Build Coastguard Worker     const int mi_rows = mi_params->mi_rows;
205*77c1e3ccSAndroid Build Coastguard Worker     const int mi_cols = mi_params->mi_cols;
206*77c1e3ccSAndroid Build Coastguard Worker     const int row_scale = mi_size_high_log2[BLOCK_16X16];
207*77c1e3ccSAndroid Build Coastguard Worker     const int col_scale = mi_size_wide_log2[BLOCK_16X16];
208*77c1e3ccSAndroid Build Coastguard Worker     assert(mi_rows % 2 == 0);
209*77c1e3ccSAndroid Build Coastguard Worker     assert(mi_cols % 2 == 0);
210*77c1e3ccSAndroid Build Coastguard Worker 
211*77c1e3ccSAndroid Build Coastguard Worker     memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
212*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->active_map.enabled) {
213*77c1e3ccSAndroid Build Coastguard Worker       for (int r = 0; r < (mi_rows >> row_scale); ++r) {
214*77c1e3ccSAndroid Build Coastguard Worker         for (int c = 0; c < (mi_cols >> col_scale); ++c) {
215*77c1e3ccSAndroid Build Coastguard Worker           // Cyclic refresh segments are considered active despite not having
216*77c1e3ccSAndroid Build Coastguard Worker           // AM_SEGMENT_ID_ACTIVE
217*77c1e3ccSAndroid Build Coastguard Worker           uint8_t temp = 0;
218*77c1e3ccSAndroid Build Coastguard Worker           temp |= seg_map_8x8[(2 * r + 0) * mi_cols + (2 * c + 0)] !=
219*77c1e3ccSAndroid Build Coastguard Worker                   AM_SEGMENT_ID_INACTIVE;
220*77c1e3ccSAndroid Build Coastguard Worker           temp |= seg_map_8x8[(2 * r + 0) * mi_cols + (2 * c + 1)] !=
221*77c1e3ccSAndroid Build Coastguard Worker                   AM_SEGMENT_ID_INACTIVE;
222*77c1e3ccSAndroid Build Coastguard Worker           temp |= seg_map_8x8[(2 * r + 1) * mi_cols + (2 * c + 0)] !=
223*77c1e3ccSAndroid Build Coastguard Worker                   AM_SEGMENT_ID_INACTIVE;
224*77c1e3ccSAndroid Build Coastguard Worker           temp |= seg_map_8x8[(2 * r + 1) * mi_cols + (2 * c + 1)] !=
225*77c1e3ccSAndroid Build Coastguard Worker                   AM_SEGMENT_ID_INACTIVE;
226*77c1e3ccSAndroid Build Coastguard Worker           new_map_16x16[r * cols + c] |= temp;
227*77c1e3ccSAndroid Build Coastguard Worker         }
228*77c1e3ccSAndroid Build Coastguard Worker       }
229*77c1e3ccSAndroid Build Coastguard Worker     }
230*77c1e3ccSAndroid Build Coastguard Worker     return 0;
231*77c1e3ccSAndroid Build Coastguard Worker   }
232*77c1e3ccSAndroid Build Coastguard Worker 
233*77c1e3ccSAndroid Build Coastguard Worker   return -1;
234*77c1e3ccSAndroid Build Coastguard Worker }
235*77c1e3ccSAndroid Build Coastguard Worker 
av1_initialize_enc(unsigned int usage,enum aom_rc_mode end_usage)236*77c1e3ccSAndroid Build Coastguard Worker void av1_initialize_enc(unsigned int usage, enum aom_rc_mode end_usage) {
237*77c1e3ccSAndroid Build Coastguard Worker   bool is_allintra = usage == ALLINTRA;
238*77c1e3ccSAndroid Build Coastguard Worker 
239*77c1e3ccSAndroid Build Coastguard Worker   av1_rtcd();
240*77c1e3ccSAndroid Build Coastguard Worker   aom_dsp_rtcd();
241*77c1e3ccSAndroid Build Coastguard Worker   aom_scale_rtcd();
242*77c1e3ccSAndroid Build Coastguard Worker   av1_init_intra_predictors();
243*77c1e3ccSAndroid Build Coastguard Worker   av1_init_me_luts();
244*77c1e3ccSAndroid Build Coastguard Worker   if (!is_allintra) av1_init_wedge_masks();
245*77c1e3ccSAndroid Build Coastguard Worker   if (!is_allintra || end_usage != AOM_Q) av1_rc_init_minq_luts();
246*77c1e3ccSAndroid Build Coastguard Worker }
247*77c1e3ccSAndroid Build Coastguard Worker 
av1_new_framerate(AV1_COMP * cpi,double framerate)248*77c1e3ccSAndroid Build Coastguard Worker void av1_new_framerate(AV1_COMP *cpi, double framerate) {
249*77c1e3ccSAndroid Build Coastguard Worker   cpi->framerate = framerate < 0.1 ? 30 : framerate;
250*77c1e3ccSAndroid Build Coastguard Worker   av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
251*77c1e3ccSAndroid Build Coastguard Worker }
252*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_compression_ratio(const AV1_COMMON * const cm,size_t encoded_frame_size)253*77c1e3ccSAndroid Build Coastguard Worker double av1_get_compression_ratio(const AV1_COMMON *const cm,
254*77c1e3ccSAndroid Build Coastguard Worker                                  size_t encoded_frame_size) {
255*77c1e3ccSAndroid Build Coastguard Worker   const int upscaled_width = cm->superres_upscaled_width;
256*77c1e3ccSAndroid Build Coastguard Worker   const int height = cm->height;
257*77c1e3ccSAndroid Build Coastguard Worker   const int64_t luma_pic_size = (int64_t)upscaled_width * height;
258*77c1e3ccSAndroid Build Coastguard Worker   const SequenceHeader *const seq_params = cm->seq_params;
259*77c1e3ccSAndroid Build Coastguard Worker   const BITSTREAM_PROFILE profile = seq_params->profile;
260*77c1e3ccSAndroid Build Coastguard Worker   const int pic_size_profile_factor =
261*77c1e3ccSAndroid Build Coastguard Worker       profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
262*77c1e3ccSAndroid Build Coastguard Worker   encoded_frame_size =
263*77c1e3ccSAndroid Build Coastguard Worker       (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
264*77c1e3ccSAndroid Build Coastguard Worker   const int64_t uncompressed_frame_size =
265*77c1e3ccSAndroid Build Coastguard Worker       (luma_pic_size * pic_size_profile_factor) >> 3;
266*77c1e3ccSAndroid Build Coastguard Worker   return (double)uncompressed_frame_size / encoded_frame_size;
267*77c1e3ccSAndroid Build Coastguard Worker }
268*77c1e3ccSAndroid Build Coastguard Worker 
auto_tile_size_balancing(AV1_COMMON * const cm,int num_sbs,int num_tiles_lg,int tile_col_row)269*77c1e3ccSAndroid Build Coastguard Worker static void auto_tile_size_balancing(AV1_COMMON *const cm, int num_sbs,
270*77c1e3ccSAndroid Build Coastguard Worker                                      int num_tiles_lg, int tile_col_row) {
271*77c1e3ccSAndroid Build Coastguard Worker   CommonTileParams *const tiles = &cm->tiles;
272*77c1e3ccSAndroid Build Coastguard Worker   int i, start_sb;
273*77c1e3ccSAndroid Build Coastguard Worker   int size_sb = num_sbs >> num_tiles_lg;
274*77c1e3ccSAndroid Build Coastguard Worker   int res_sbs = num_sbs - (size_sb << num_tiles_lg);
275*77c1e3ccSAndroid Build Coastguard Worker   int num_tiles = 1 << num_tiles_lg;
276*77c1e3ccSAndroid Build Coastguard Worker   int inc_index = num_tiles - res_sbs;
277*77c1e3ccSAndroid Build Coastguard Worker 
278*77c1e3ccSAndroid Build Coastguard Worker   tiles->uniform_spacing = 0;
279*77c1e3ccSAndroid Build Coastguard Worker 
280*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0, start_sb = 0; start_sb < num_sbs && i < MAX_TILE_COLS; ++i) {
281*77c1e3ccSAndroid Build Coastguard Worker     if (i == inc_index) ++size_sb;
282*77c1e3ccSAndroid Build Coastguard Worker     if (tile_col_row)
283*77c1e3ccSAndroid Build Coastguard Worker       tiles->col_start_sb[i] = start_sb;
284*77c1e3ccSAndroid Build Coastguard Worker     else
285*77c1e3ccSAndroid Build Coastguard Worker       tiles->row_start_sb[i] = start_sb;
286*77c1e3ccSAndroid Build Coastguard Worker 
287*77c1e3ccSAndroid Build Coastguard Worker     start_sb += AOMMIN(size_sb, tiles->max_width_sb);
288*77c1e3ccSAndroid Build Coastguard Worker   }
289*77c1e3ccSAndroid Build Coastguard Worker 
290*77c1e3ccSAndroid Build Coastguard Worker   if (tile_col_row) {
291*77c1e3ccSAndroid Build Coastguard Worker     tiles->cols = i;
292*77c1e3ccSAndroid Build Coastguard Worker     tiles->col_start_sb[i] = num_sbs;
293*77c1e3ccSAndroid Build Coastguard Worker   } else {
294*77c1e3ccSAndroid Build Coastguard Worker     tiles->rows = i;
295*77c1e3ccSAndroid Build Coastguard Worker     tiles->row_start_sb[i] = num_sbs;
296*77c1e3ccSAndroid Build Coastguard Worker   }
297*77c1e3ccSAndroid Build Coastguard Worker }
298*77c1e3ccSAndroid Build Coastguard Worker 
set_tile_info(AV1_COMMON * const cm,const TileConfig * const tile_cfg)299*77c1e3ccSAndroid Build Coastguard Worker static void set_tile_info(AV1_COMMON *const cm,
300*77c1e3ccSAndroid Build Coastguard Worker                           const TileConfig *const tile_cfg) {
301*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
302*77c1e3ccSAndroid Build Coastguard Worker   const SequenceHeader *const seq_params = cm->seq_params;
303*77c1e3ccSAndroid Build Coastguard Worker   CommonTileParams *const tiles = &cm->tiles;
304*77c1e3ccSAndroid Build Coastguard Worker   int i, start_sb;
305*77c1e3ccSAndroid Build Coastguard Worker 
306*77c1e3ccSAndroid Build Coastguard Worker   av1_get_tile_limits(cm);
307*77c1e3ccSAndroid Build Coastguard Worker 
308*77c1e3ccSAndroid Build Coastguard Worker   int sb_cols =
309*77c1e3ccSAndroid Build Coastguard Worker       CEIL_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
310*77c1e3ccSAndroid Build Coastguard Worker   // configure tile columns
311*77c1e3ccSAndroid Build Coastguard Worker   if (tile_cfg->tile_width_count == 0 || tile_cfg->tile_height_count == 0) {
312*77c1e3ccSAndroid Build Coastguard Worker     tiles->uniform_spacing = 1;
313*77c1e3ccSAndroid Build Coastguard Worker     tiles->log2_cols = AOMMAX(tile_cfg->tile_columns, tiles->min_log2_cols);
314*77c1e3ccSAndroid Build Coastguard Worker     // Add a special case to handle super resolution
315*77c1e3ccSAndroid Build Coastguard Worker     sb_cols = coded_to_superres_mi(sb_cols, cm->superres_scale_denominator);
316*77c1e3ccSAndroid Build Coastguard Worker     int min_log2_cols = 0;
317*77c1e3ccSAndroid Build Coastguard Worker     for (; (tiles->max_width_sb << min_log2_cols) <= sb_cols; ++min_log2_cols) {
318*77c1e3ccSAndroid Build Coastguard Worker     }
319*77c1e3ccSAndroid Build Coastguard Worker     tiles->log2_cols = AOMMAX(tiles->log2_cols, min_log2_cols);
320*77c1e3ccSAndroid Build Coastguard Worker 
321*77c1e3ccSAndroid Build Coastguard Worker     tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
322*77c1e3ccSAndroid Build Coastguard Worker   } else if (tile_cfg->tile_widths[0] < 0) {
323*77c1e3ccSAndroid Build Coastguard Worker     auto_tile_size_balancing(cm, sb_cols, tile_cfg->tile_columns, 1);
324*77c1e3ccSAndroid Build Coastguard Worker   } else {
325*77c1e3ccSAndroid Build Coastguard Worker     int size_sb, j = 0;
326*77c1e3ccSAndroid Build Coastguard Worker     tiles->uniform_spacing = 0;
327*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
328*77c1e3ccSAndroid Build Coastguard Worker       tiles->col_start_sb[i] = start_sb;
329*77c1e3ccSAndroid Build Coastguard Worker       size_sb = tile_cfg->tile_widths[j++];
330*77c1e3ccSAndroid Build Coastguard Worker       if (j >= tile_cfg->tile_width_count) j = 0;
331*77c1e3ccSAndroid Build Coastguard Worker       start_sb += AOMMIN(size_sb, tiles->max_width_sb);
332*77c1e3ccSAndroid Build Coastguard Worker     }
333*77c1e3ccSAndroid Build Coastguard Worker     tiles->cols = i;
334*77c1e3ccSAndroid Build Coastguard Worker     tiles->col_start_sb[i] = sb_cols;
335*77c1e3ccSAndroid Build Coastguard Worker   }
336*77c1e3ccSAndroid Build Coastguard Worker   av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols,
337*77c1e3ccSAndroid Build Coastguard Worker                           tiles);
338*77c1e3ccSAndroid Build Coastguard Worker 
339*77c1e3ccSAndroid Build Coastguard Worker   // configure tile rows
340*77c1e3ccSAndroid Build Coastguard Worker   int sb_rows =
341*77c1e3ccSAndroid Build Coastguard Worker       CEIL_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
342*77c1e3ccSAndroid Build Coastguard Worker   if (tiles->uniform_spacing) {
343*77c1e3ccSAndroid Build Coastguard Worker     tiles->log2_rows = AOMMAX(tile_cfg->tile_rows, tiles->min_log2_rows);
344*77c1e3ccSAndroid Build Coastguard Worker     tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
345*77c1e3ccSAndroid Build Coastguard Worker   } else if (tile_cfg->tile_heights[0] < 0) {
346*77c1e3ccSAndroid Build Coastguard Worker     auto_tile_size_balancing(cm, sb_rows, tile_cfg->tile_rows, 0);
347*77c1e3ccSAndroid Build Coastguard Worker   } else {
348*77c1e3ccSAndroid Build Coastguard Worker     int size_sb, j = 0;
349*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
350*77c1e3ccSAndroid Build Coastguard Worker       tiles->row_start_sb[i] = start_sb;
351*77c1e3ccSAndroid Build Coastguard Worker       size_sb = tile_cfg->tile_heights[j++];
352*77c1e3ccSAndroid Build Coastguard Worker       if (j >= tile_cfg->tile_height_count) j = 0;
353*77c1e3ccSAndroid Build Coastguard Worker       start_sb += AOMMIN(size_sb, tiles->max_height_sb);
354*77c1e3ccSAndroid Build Coastguard Worker     }
355*77c1e3ccSAndroid Build Coastguard Worker     tiles->rows = i;
356*77c1e3ccSAndroid Build Coastguard Worker     tiles->row_start_sb[i] = sb_rows;
357*77c1e3ccSAndroid Build Coastguard Worker   }
358*77c1e3ccSAndroid Build Coastguard Worker   av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles);
359*77c1e3ccSAndroid Build Coastguard Worker }
360*77c1e3ccSAndroid Build Coastguard Worker 
av1_update_frame_size(AV1_COMP * cpi)361*77c1e3ccSAndroid Build Coastguard Worker void av1_update_frame_size(AV1_COMP *cpi) {
362*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
363*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
364*77c1e3ccSAndroid Build Coastguard Worker 
365*77c1e3ccSAndroid Build Coastguard Worker   // Setup mi_params here in case we need more mi's.
366*77c1e3ccSAndroid Build Coastguard Worker   CommonModeInfoParams *const mi_params = &cm->mi_params;
367*77c1e3ccSAndroid Build Coastguard Worker   mi_params->set_mb_mi(mi_params, cm->width, cm->height,
368*77c1e3ccSAndroid Build Coastguard Worker                        cpi->sf.part_sf.default_min_partition_size);
369*77c1e3ccSAndroid Build Coastguard Worker 
370*77c1e3ccSAndroid Build Coastguard Worker   av1_init_macroblockd(cm, xd);
371*77c1e3ccSAndroid Build Coastguard Worker 
372*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->ppi->seq_params_locked)
373*77c1e3ccSAndroid Build Coastguard Worker     set_sb_size(cm->seq_params,
374*77c1e3ccSAndroid Build Coastguard Worker                 av1_select_sb_size(&cpi->oxcf, cm->width, cm->height,
375*77c1e3ccSAndroid Build Coastguard Worker                                    cpi->ppi->number_spatial_layers));
376*77c1e3ccSAndroid Build Coastguard Worker 
377*77c1e3ccSAndroid Build Coastguard Worker   set_tile_info(cm, &cpi->oxcf.tile_cfg);
378*77c1e3ccSAndroid Build Coastguard Worker }
379*77c1e3ccSAndroid Build Coastguard Worker 
does_level_match(int width,int height,double fps,int lvl_width,int lvl_height,double lvl_fps,int lvl_dim_mult)380*77c1e3ccSAndroid Build Coastguard Worker static inline int does_level_match(int width, int height, double fps,
381*77c1e3ccSAndroid Build Coastguard Worker                                    int lvl_width, int lvl_height,
382*77c1e3ccSAndroid Build Coastguard Worker                                    double lvl_fps, int lvl_dim_mult) {
383*77c1e3ccSAndroid Build Coastguard Worker   const int64_t lvl_luma_pels = (int64_t)lvl_width * lvl_height;
384*77c1e3ccSAndroid Build Coastguard Worker   const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
385*77c1e3ccSAndroid Build Coastguard Worker   const int64_t luma_pels = (int64_t)width * height;
386*77c1e3ccSAndroid Build Coastguard Worker   const double display_sample_rate = luma_pels * fps;
387*77c1e3ccSAndroid Build Coastguard Worker   return luma_pels <= lvl_luma_pels &&
388*77c1e3ccSAndroid Build Coastguard Worker          display_sample_rate <= lvl_display_sample_rate &&
389*77c1e3ccSAndroid Build Coastguard Worker          width <= lvl_width * lvl_dim_mult &&
390*77c1e3ccSAndroid Build Coastguard Worker          height <= lvl_height * lvl_dim_mult;
391*77c1e3ccSAndroid Build Coastguard Worker }
392*77c1e3ccSAndroid Build Coastguard Worker 
set_bitstream_level_tier(AV1_PRIMARY * const ppi,int width,int height,double init_framerate)393*77c1e3ccSAndroid Build Coastguard Worker static void set_bitstream_level_tier(AV1_PRIMARY *const ppi, int width,
394*77c1e3ccSAndroid Build Coastguard Worker                                      int height, double init_framerate) {
395*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader *const seq_params = &ppi->seq_params;
396*77c1e3ccSAndroid Build Coastguard Worker   const AV1LevelParams *const level_params = &ppi->level_params;
397*77c1e3ccSAndroid Build Coastguard Worker   // TODO(any): This is a placeholder function that only addresses dimensions
398*77c1e3ccSAndroid Build Coastguard Worker   // and max display sample rates.
399*77c1e3ccSAndroid Build Coastguard Worker   // Need to add checks for max bit rate, max decoded luma sample rate, header
400*77c1e3ccSAndroid Build Coastguard Worker   // rate, etc. that are not covered by this function.
401*77c1e3ccSAndroid Build Coastguard Worker   AV1_LEVEL level = SEQ_LEVEL_MAX;
402*77c1e3ccSAndroid Build Coastguard Worker   if (does_level_match(width, height, init_framerate, 512, 288, 30.0, 4)) {
403*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_2_0;
404*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 704, 396, 30.0,
405*77c1e3ccSAndroid Build Coastguard Worker                               4)) {
406*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_2_1;
407*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 1088, 612, 30.0,
408*77c1e3ccSAndroid Build Coastguard Worker                               4)) {
409*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_3_0;
410*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 1376, 774, 30.0,
411*77c1e3ccSAndroid Build Coastguard Worker                               4)) {
412*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_3_1;
413*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 2048, 1152, 30.0,
414*77c1e3ccSAndroid Build Coastguard Worker                               3)) {
415*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_4_0;
416*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 2048, 1152, 60.0,
417*77c1e3ccSAndroid Build Coastguard Worker                               3)) {
418*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_4_1;
419*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 4096, 2176, 30.0,
420*77c1e3ccSAndroid Build Coastguard Worker                               2)) {
421*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_5_0;
422*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 4096, 2176, 60.0,
423*77c1e3ccSAndroid Build Coastguard Worker                               2)) {
424*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_5_1;
425*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 4096, 2176, 120.0,
426*77c1e3ccSAndroid Build Coastguard Worker                               2)) {
427*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_5_2;
428*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 8192, 4352, 30.0,
429*77c1e3ccSAndroid Build Coastguard Worker                               2)) {
430*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_6_0;
431*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 8192, 4352, 60.0,
432*77c1e3ccSAndroid Build Coastguard Worker                               2)) {
433*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_6_1;
434*77c1e3ccSAndroid Build Coastguard Worker   } else if (does_level_match(width, height, init_framerate, 8192, 4352, 120.0,
435*77c1e3ccSAndroid Build Coastguard Worker                               2)) {
436*77c1e3ccSAndroid Build Coastguard Worker     level = SEQ_LEVEL_6_2;
437*77c1e3ccSAndroid Build Coastguard Worker   }
438*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_CWG_C013
439*77c1e3ccSAndroid Build Coastguard Worker   // TODO(bohanli): currently target level is only working for the 0th operating
440*77c1e3ccSAndroid Build Coastguard Worker   // point, so scalable coding is not supported.
441*77c1e3ccSAndroid Build Coastguard Worker   else if (level_params->target_seq_level_idx[0] >= SEQ_LEVEL_7_0 &&
442*77c1e3ccSAndroid Build Coastguard Worker            level_params->target_seq_level_idx[0] <= SEQ_LEVEL_8_3) {
443*77c1e3ccSAndroid Build Coastguard Worker     // Only use level 7.x to 8.x when explicitly asked to.
444*77c1e3ccSAndroid Build Coastguard Worker     if (does_level_match(width, height, init_framerate, 16384, 8704, 30.0, 2)) {
445*77c1e3ccSAndroid Build Coastguard Worker       level = SEQ_LEVEL_7_0;
446*77c1e3ccSAndroid Build Coastguard Worker     } else if (does_level_match(width, height, init_framerate, 16384, 8704,
447*77c1e3ccSAndroid Build Coastguard Worker                                 60.0, 2)) {
448*77c1e3ccSAndroid Build Coastguard Worker       level = SEQ_LEVEL_7_1;
449*77c1e3ccSAndroid Build Coastguard Worker     } else if (does_level_match(width, height, init_framerate, 16384, 8704,
450*77c1e3ccSAndroid Build Coastguard Worker                                 120.0, 2)) {
451*77c1e3ccSAndroid Build Coastguard Worker       level = SEQ_LEVEL_7_2;
452*77c1e3ccSAndroid Build Coastguard Worker     } else if (does_level_match(width, height, init_framerate, 32768, 17408,
453*77c1e3ccSAndroid Build Coastguard Worker                                 30.0, 2)) {
454*77c1e3ccSAndroid Build Coastguard Worker       level = SEQ_LEVEL_8_0;
455*77c1e3ccSAndroid Build Coastguard Worker     } else if (does_level_match(width, height, init_framerate, 32768, 17408,
456*77c1e3ccSAndroid Build Coastguard Worker                                 60.0, 2)) {
457*77c1e3ccSAndroid Build Coastguard Worker       level = SEQ_LEVEL_8_1;
458*77c1e3ccSAndroid Build Coastguard Worker     } else if (does_level_match(width, height, init_framerate, 32768, 17408,
459*77c1e3ccSAndroid Build Coastguard Worker                                 120.0, 2)) {
460*77c1e3ccSAndroid Build Coastguard Worker       level = SEQ_LEVEL_8_2;
461*77c1e3ccSAndroid Build Coastguard Worker     }
462*77c1e3ccSAndroid Build Coastguard Worker   }
463*77c1e3ccSAndroid Build Coastguard Worker #endif
464*77c1e3ccSAndroid Build Coastguard Worker 
465*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
466*77c1e3ccSAndroid Build Coastguard Worker     assert(is_valid_seq_level_idx(level_params->target_seq_level_idx[i]) ||
467*77c1e3ccSAndroid Build Coastguard Worker            level_params->target_seq_level_idx[i] == SEQ_LEVEL_KEEP_STATS);
468*77c1e3ccSAndroid Build Coastguard Worker     // If a higher target level is specified, it is then used rather than the
469*77c1e3ccSAndroid Build Coastguard Worker     // inferred one from resolution and framerate.
470*77c1e3ccSAndroid Build Coastguard Worker     seq_params->seq_level_idx[i] =
471*77c1e3ccSAndroid Build Coastguard Worker         level_params->target_seq_level_idx[i] < SEQ_LEVELS &&
472*77c1e3ccSAndroid Build Coastguard Worker                 level_params->target_seq_level_idx[i] > level
473*77c1e3ccSAndroid Build Coastguard Worker             ? level_params->target_seq_level_idx[i]
474*77c1e3ccSAndroid Build Coastguard Worker             : level;
475*77c1e3ccSAndroid Build Coastguard Worker     // Set the maximum parameters for bitrate and buffer size for this profile,
476*77c1e3ccSAndroid Build Coastguard Worker     // level, and tier
477*77c1e3ccSAndroid Build Coastguard Worker     seq_params->op_params[i].bitrate = av1_max_level_bitrate(
478*77c1e3ccSAndroid Build Coastguard Worker         seq_params->profile, seq_params->seq_level_idx[i], seq_params->tier[i]);
479*77c1e3ccSAndroid Build Coastguard Worker     // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
480*77c1e3ccSAndroid Build Coastguard Worker     // check
481*77c1e3ccSAndroid Build Coastguard Worker     if (seq_params->op_params[i].bitrate == 0)
482*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(
483*77c1e3ccSAndroid Build Coastguard Worker           &ppi->error, AOM_CODEC_UNSUP_BITSTREAM,
484*77c1e3ccSAndroid Build Coastguard Worker           "AV1 does not support this combination of profile, level, and tier.");
485*77c1e3ccSAndroid Build Coastguard Worker     // Buffer size in bits/s is bitrate in bits/s * 1 s
486*77c1e3ccSAndroid Build Coastguard Worker     seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
487*77c1e3ccSAndroid Build Coastguard Worker   }
488*77c1e3ccSAndroid Build Coastguard Worker }
489*77c1e3ccSAndroid Build Coastguard Worker 
init_seq_coding_tools(AV1_PRIMARY * const ppi,const AV1EncoderConfig * oxcf,int disable_frame_id_numbers)490*77c1e3ccSAndroid Build Coastguard Worker static void init_seq_coding_tools(AV1_PRIMARY *const ppi,
491*77c1e3ccSAndroid Build Coastguard Worker                                   const AV1EncoderConfig *oxcf,
492*77c1e3ccSAndroid Build Coastguard Worker                                   int disable_frame_id_numbers) {
493*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader *const seq = &ppi->seq_params;
494*77c1e3ccSAndroid Build Coastguard Worker   const FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg;
495*77c1e3ccSAndroid Build Coastguard Worker   const ToolCfg *const tool_cfg = &oxcf->tool_cfg;
496*77c1e3ccSAndroid Build Coastguard Worker 
497*77c1e3ccSAndroid Build Coastguard Worker   seq->still_picture =
498*77c1e3ccSAndroid Build Coastguard Worker       !tool_cfg->force_video_mode && (oxcf->input_cfg.limit == 1);
499*77c1e3ccSAndroid Build Coastguard Worker   seq->reduced_still_picture_hdr =
500*77c1e3ccSAndroid Build Coastguard Worker       seq->still_picture && !tool_cfg->full_still_picture_hdr;
501*77c1e3ccSAndroid Build Coastguard Worker   seq->force_screen_content_tools = 2;
502*77c1e3ccSAndroid Build Coastguard Worker   seq->force_integer_mv = 2;
503*77c1e3ccSAndroid Build Coastguard Worker   seq->order_hint_info.enable_order_hint = tool_cfg->enable_order_hint;
504*77c1e3ccSAndroid Build Coastguard Worker   seq->frame_id_numbers_present_flag =
505*77c1e3ccSAndroid Build Coastguard Worker       !seq->reduced_still_picture_hdr &&
506*77c1e3ccSAndroid Build Coastguard Worker       !oxcf->tile_cfg.enable_large_scale_tile &&
507*77c1e3ccSAndroid Build Coastguard Worker       tool_cfg->error_resilient_mode && !disable_frame_id_numbers;
508*77c1e3ccSAndroid Build Coastguard Worker   if (seq->reduced_still_picture_hdr) {
509*77c1e3ccSAndroid Build Coastguard Worker     seq->order_hint_info.enable_order_hint = 0;
510*77c1e3ccSAndroid Build Coastguard Worker     seq->force_screen_content_tools = 2;
511*77c1e3ccSAndroid Build Coastguard Worker     seq->force_integer_mv = 2;
512*77c1e3ccSAndroid Build Coastguard Worker   }
513*77c1e3ccSAndroid Build Coastguard Worker   seq->order_hint_info.order_hint_bits_minus_1 =
514*77c1e3ccSAndroid Build Coastguard Worker       seq->order_hint_info.enable_order_hint
515*77c1e3ccSAndroid Build Coastguard Worker           ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
516*77c1e3ccSAndroid Build Coastguard Worker           : -1;
517*77c1e3ccSAndroid Build Coastguard Worker 
518*77c1e3ccSAndroid Build Coastguard Worker   seq->max_frame_width = frm_dim_cfg->forced_max_frame_width
519*77c1e3ccSAndroid Build Coastguard Worker                              ? frm_dim_cfg->forced_max_frame_width
520*77c1e3ccSAndroid Build Coastguard Worker                              : frm_dim_cfg->width;
521*77c1e3ccSAndroid Build Coastguard Worker   seq->max_frame_height = frm_dim_cfg->forced_max_frame_height
522*77c1e3ccSAndroid Build Coastguard Worker                               ? frm_dim_cfg->forced_max_frame_height
523*77c1e3ccSAndroid Build Coastguard Worker                               : frm_dim_cfg->height;
524*77c1e3ccSAndroid Build Coastguard Worker   seq->num_bits_width =
525*77c1e3ccSAndroid Build Coastguard Worker       (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
526*77c1e3ccSAndroid Build Coastguard Worker   seq->num_bits_height =
527*77c1e3ccSAndroid Build Coastguard Worker       (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
528*77c1e3ccSAndroid Build Coastguard Worker   assert(seq->num_bits_width <= 16);
529*77c1e3ccSAndroid Build Coastguard Worker   assert(seq->num_bits_height <= 16);
530*77c1e3ccSAndroid Build Coastguard Worker 
531*77c1e3ccSAndroid Build Coastguard Worker   seq->frame_id_length = FRAME_ID_LENGTH;
532*77c1e3ccSAndroid Build Coastguard Worker   seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
533*77c1e3ccSAndroid Build Coastguard Worker 
534*77c1e3ccSAndroid Build Coastguard Worker   seq->enable_dual_filter = tool_cfg->enable_dual_filter;
535*77c1e3ccSAndroid Build Coastguard Worker   seq->order_hint_info.enable_dist_wtd_comp =
536*77c1e3ccSAndroid Build Coastguard Worker       oxcf->comp_type_cfg.enable_dist_wtd_comp;
537*77c1e3ccSAndroid Build Coastguard Worker   seq->order_hint_info.enable_dist_wtd_comp &=
538*77c1e3ccSAndroid Build Coastguard Worker       seq->order_hint_info.enable_order_hint;
539*77c1e3ccSAndroid Build Coastguard Worker   seq->order_hint_info.enable_ref_frame_mvs = tool_cfg->ref_frame_mvs_present;
540*77c1e3ccSAndroid Build Coastguard Worker   seq->order_hint_info.enable_ref_frame_mvs &=
541*77c1e3ccSAndroid Build Coastguard Worker       seq->order_hint_info.enable_order_hint;
542*77c1e3ccSAndroid Build Coastguard Worker   seq->enable_superres = oxcf->superres_cfg.enable_superres;
543*77c1e3ccSAndroid Build Coastguard Worker   seq->enable_cdef = tool_cfg->cdef_control != CDEF_NONE ? 1 : 0;
544*77c1e3ccSAndroid Build Coastguard Worker   seq->enable_restoration = tool_cfg->enable_restoration;
545*77c1e3ccSAndroid Build Coastguard Worker   seq->enable_warped_motion = oxcf->motion_mode_cfg.enable_warped_motion;
546*77c1e3ccSAndroid Build Coastguard Worker   seq->enable_interintra_compound = tool_cfg->enable_interintra_comp;
547*77c1e3ccSAndroid Build Coastguard Worker   seq->enable_masked_compound = oxcf->comp_type_cfg.enable_masked_comp;
548*77c1e3ccSAndroid Build Coastguard Worker   seq->enable_intra_edge_filter = oxcf->intra_mode_cfg.enable_intra_edge_filter;
549*77c1e3ccSAndroid Build Coastguard Worker   seq->enable_filter_intra = oxcf->intra_mode_cfg.enable_filter_intra;
550*77c1e3ccSAndroid Build Coastguard Worker 
551*77c1e3ccSAndroid Build Coastguard Worker   set_bitstream_level_tier(ppi, frm_dim_cfg->width, frm_dim_cfg->height,
552*77c1e3ccSAndroid Build Coastguard Worker                            oxcf->input_cfg.init_framerate);
553*77c1e3ccSAndroid Build Coastguard Worker 
554*77c1e3ccSAndroid Build Coastguard Worker   if (seq->operating_points_cnt_minus_1 == 0) {
555*77c1e3ccSAndroid Build Coastguard Worker     seq->operating_point_idc[0] = 0;
556*77c1e3ccSAndroid Build Coastguard Worker     seq->has_nonzero_operating_point_idc = false;
557*77c1e3ccSAndroid Build Coastguard Worker   } else {
558*77c1e3ccSAndroid Build Coastguard Worker     // Set operating_point_idc[] such that the i=0 point corresponds to the
559*77c1e3ccSAndroid Build Coastguard Worker     // highest quality operating point (all layers), and subsequent
560*77c1e3ccSAndroid Build Coastguard Worker     // operarting points (i > 0) are lower quality corresponding to
561*77c1e3ccSAndroid Build Coastguard Worker     // skip decoding enhancement  layers (temporal first).
562*77c1e3ccSAndroid Build Coastguard Worker     int i = 0;
563*77c1e3ccSAndroid Build Coastguard Worker     assert(seq->operating_points_cnt_minus_1 ==
564*77c1e3ccSAndroid Build Coastguard Worker            (int)(ppi->number_spatial_layers * ppi->number_temporal_layers - 1));
565*77c1e3ccSAndroid Build Coastguard Worker     for (unsigned int sl = 0; sl < ppi->number_spatial_layers; sl++) {
566*77c1e3ccSAndroid Build Coastguard Worker       for (unsigned int tl = 0; tl < ppi->number_temporal_layers; tl++) {
567*77c1e3ccSAndroid Build Coastguard Worker         seq->operating_point_idc[i] =
568*77c1e3ccSAndroid Build Coastguard Worker             (~(~0u << (ppi->number_spatial_layers - sl)) << 8) |
569*77c1e3ccSAndroid Build Coastguard Worker             ~(~0u << (ppi->number_temporal_layers - tl));
570*77c1e3ccSAndroid Build Coastguard Worker         assert(seq->operating_point_idc[i] != 0);
571*77c1e3ccSAndroid Build Coastguard Worker         i++;
572*77c1e3ccSAndroid Build Coastguard Worker       }
573*77c1e3ccSAndroid Build Coastguard Worker     }
574*77c1e3ccSAndroid Build Coastguard Worker     seq->has_nonzero_operating_point_idc = true;
575*77c1e3ccSAndroid Build Coastguard Worker   }
576*77c1e3ccSAndroid Build Coastguard Worker }
577*77c1e3ccSAndroid Build Coastguard Worker 
init_config_sequence(struct AV1_PRIMARY * ppi,const AV1EncoderConfig * oxcf)578*77c1e3ccSAndroid Build Coastguard Worker static void init_config_sequence(struct AV1_PRIMARY *ppi,
579*77c1e3ccSAndroid Build Coastguard Worker                                  const AV1EncoderConfig *oxcf) {
580*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader *const seq_params = &ppi->seq_params;
581*77c1e3ccSAndroid Build Coastguard Worker   const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
582*77c1e3ccSAndroid Build Coastguard Worker   const ColorCfg *const color_cfg = &oxcf->color_cfg;
583*77c1e3ccSAndroid Build Coastguard Worker 
584*77c1e3ccSAndroid Build Coastguard Worker   ppi->use_svc = 0;
585*77c1e3ccSAndroid Build Coastguard Worker   ppi->number_spatial_layers = 1;
586*77c1e3ccSAndroid Build Coastguard Worker   ppi->number_temporal_layers = 1;
587*77c1e3ccSAndroid Build Coastguard Worker 
588*77c1e3ccSAndroid Build Coastguard Worker   seq_params->profile = oxcf->profile;
589*77c1e3ccSAndroid Build Coastguard Worker   seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
590*77c1e3ccSAndroid Build Coastguard Worker   seq_params->use_highbitdepth = oxcf->use_highbitdepth;
591*77c1e3ccSAndroid Build Coastguard Worker   seq_params->color_primaries = color_cfg->color_primaries;
592*77c1e3ccSAndroid Build Coastguard Worker   seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
593*77c1e3ccSAndroid Build Coastguard Worker   seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
594*77c1e3ccSAndroid Build Coastguard Worker   seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
595*77c1e3ccSAndroid Build Coastguard Worker   seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
596*77c1e3ccSAndroid Build Coastguard Worker   seq_params->color_range = color_cfg->color_range;
597*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info_present = dec_model_cfg->timing_info_present;
598*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info.num_units_in_display_tick =
599*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->timing_info.num_units_in_display_tick;
600*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
601*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info.equal_picture_interval =
602*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->timing_info.equal_picture_interval;
603*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info.num_ticks_per_picture =
604*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->timing_info.num_ticks_per_picture;
605*77c1e3ccSAndroid Build Coastguard Worker 
606*77c1e3ccSAndroid Build Coastguard Worker   seq_params->display_model_info_present_flag =
607*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->display_model_info_present_flag;
608*77c1e3ccSAndroid Build Coastguard Worker   seq_params->decoder_model_info_present_flag =
609*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->decoder_model_info_present_flag;
610*77c1e3ccSAndroid Build Coastguard Worker   if (dec_model_cfg->decoder_model_info_present_flag) {
611*77c1e3ccSAndroid Build Coastguard Worker     // set the decoder model parameters in schedule mode
612*77c1e3ccSAndroid Build Coastguard Worker     seq_params->decoder_model_info.num_units_in_decoding_tick =
613*77c1e3ccSAndroid Build Coastguard Worker         dec_model_cfg->num_units_in_decoding_tick;
614*77c1e3ccSAndroid Build Coastguard Worker     ppi->buffer_removal_time_present = 1;
615*77c1e3ccSAndroid Build Coastguard Worker     av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
616*77c1e3ccSAndroid Build Coastguard Worker     av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
617*77c1e3ccSAndroid Build Coastguard Worker   } else if (seq_params->timing_info_present &&
618*77c1e3ccSAndroid Build Coastguard Worker              seq_params->timing_info.equal_picture_interval &&
619*77c1e3ccSAndroid Build Coastguard Worker              !seq_params->decoder_model_info_present_flag) {
620*77c1e3ccSAndroid Build Coastguard Worker     // set the decoder model parameters in resource availability mode
621*77c1e3ccSAndroid Build Coastguard Worker     av1_set_resource_availability_parameters(&seq_params->op_params[0]);
622*77c1e3ccSAndroid Build Coastguard Worker   } else {
623*77c1e3ccSAndroid Build Coastguard Worker     seq_params->op_params[0].initial_display_delay =
624*77c1e3ccSAndroid Build Coastguard Worker         10;  // Default value (not signaled)
625*77c1e3ccSAndroid Build Coastguard Worker   }
626*77c1e3ccSAndroid Build Coastguard Worker 
627*77c1e3ccSAndroid Build Coastguard Worker   if (seq_params->monochrome) {
628*77c1e3ccSAndroid Build Coastguard Worker     seq_params->subsampling_x = 1;
629*77c1e3ccSAndroid Build Coastguard Worker     seq_params->subsampling_y = 1;
630*77c1e3ccSAndroid Build Coastguard Worker   } else if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
631*77c1e3ccSAndroid Build Coastguard Worker              seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
632*77c1e3ccSAndroid Build Coastguard Worker              seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
633*77c1e3ccSAndroid Build Coastguard Worker     seq_params->subsampling_x = 0;
634*77c1e3ccSAndroid Build Coastguard Worker     seq_params->subsampling_y = 0;
635*77c1e3ccSAndroid Build Coastguard Worker   } else {
636*77c1e3ccSAndroid Build Coastguard Worker     if (seq_params->profile == 0) {
637*77c1e3ccSAndroid Build Coastguard Worker       seq_params->subsampling_x = 1;
638*77c1e3ccSAndroid Build Coastguard Worker       seq_params->subsampling_y = 1;
639*77c1e3ccSAndroid Build Coastguard Worker     } else if (seq_params->profile == 1) {
640*77c1e3ccSAndroid Build Coastguard Worker       seq_params->subsampling_x = 0;
641*77c1e3ccSAndroid Build Coastguard Worker       seq_params->subsampling_y = 0;
642*77c1e3ccSAndroid Build Coastguard Worker     } else {
643*77c1e3ccSAndroid Build Coastguard Worker       if (seq_params->bit_depth == AOM_BITS_12) {
644*77c1e3ccSAndroid Build Coastguard Worker         seq_params->subsampling_x = oxcf->input_cfg.chroma_subsampling_x;
645*77c1e3ccSAndroid Build Coastguard Worker         seq_params->subsampling_y = oxcf->input_cfg.chroma_subsampling_y;
646*77c1e3ccSAndroid Build Coastguard Worker       } else {
647*77c1e3ccSAndroid Build Coastguard Worker         seq_params->subsampling_x = 1;
648*77c1e3ccSAndroid Build Coastguard Worker         seq_params->subsampling_y = 0;
649*77c1e3ccSAndroid Build Coastguard Worker       }
650*77c1e3ccSAndroid Build Coastguard Worker     }
651*77c1e3ccSAndroid Build Coastguard Worker   }
652*77c1e3ccSAndroid Build Coastguard Worker   av1_change_config_seq(ppi, oxcf, NULL);
653*77c1e3ccSAndroid Build Coastguard Worker }
654*77c1e3ccSAndroid Build Coastguard Worker 
init_config(struct AV1_COMP * cpi,const AV1EncoderConfig * oxcf)655*77c1e3ccSAndroid Build Coastguard Worker static void init_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
656*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
657*77c1e3ccSAndroid Build Coastguard Worker   ResizePendingParams *resize_pending_params = &cpi->resize_pending_params;
658*77c1e3ccSAndroid Build Coastguard Worker 
659*77c1e3ccSAndroid Build Coastguard Worker   cpi->oxcf = *oxcf;
660*77c1e3ccSAndroid Build Coastguard Worker   cpi->framerate = oxcf->input_cfg.init_framerate;
661*77c1e3ccSAndroid Build Coastguard Worker 
662*77c1e3ccSAndroid Build Coastguard Worker   cm->width = oxcf->frm_dim_cfg.width;
663*77c1e3ccSAndroid Build Coastguard Worker   cm->height = oxcf->frm_dim_cfg.height;
664*77c1e3ccSAndroid Build Coastguard Worker   cpi->is_dropped_frame = false;
665*77c1e3ccSAndroid Build Coastguard Worker 
666*77c1e3ccSAndroid Build Coastguard Worker   alloc_compressor_data(cpi);
667*77c1e3ccSAndroid Build Coastguard Worker 
668*77c1e3ccSAndroid Build Coastguard Worker   cpi->data_alloc_width = cm->width;
669*77c1e3ccSAndroid Build Coastguard Worker   cpi->data_alloc_height = cm->height;
670*77c1e3ccSAndroid Build Coastguard Worker   cpi->frame_size_related_setup_done = false;
671*77c1e3ccSAndroid Build Coastguard Worker 
672*77c1e3ccSAndroid Build Coastguard Worker   // Single thread case: use counts in common.
673*77c1e3ccSAndroid Build Coastguard Worker   cpi->td.counts = &cpi->counts;
674*77c1e3ccSAndroid Build Coastguard Worker 
675*77c1e3ccSAndroid Build Coastguard Worker   // Init SVC parameters.
676*77c1e3ccSAndroid Build Coastguard Worker   cpi->svc.number_spatial_layers = 1;
677*77c1e3ccSAndroid Build Coastguard Worker   cpi->svc.number_temporal_layers = 1;
678*77c1e3ccSAndroid Build Coastguard Worker   cm->spatial_layer_id = 0;
679*77c1e3ccSAndroid Build Coastguard Worker   cm->temporal_layer_id = 0;
680*77c1e3ccSAndroid Build Coastguard Worker   // Init rtc_ref parameters.
681*77c1e3ccSAndroid Build Coastguard Worker   cpi->ppi->rtc_ref.set_ref_frame_config = 0;
682*77c1e3ccSAndroid Build Coastguard Worker   cpi->ppi->rtc_ref.non_reference_frame = 0;
683*77c1e3ccSAndroid Build Coastguard Worker   cpi->ppi->rtc_ref.ref_frame_comp[0] = 0;
684*77c1e3ccSAndroid Build Coastguard Worker   cpi->ppi->rtc_ref.ref_frame_comp[1] = 0;
685*77c1e3ccSAndroid Build Coastguard Worker   cpi->ppi->rtc_ref.ref_frame_comp[2] = 0;
686*77c1e3ccSAndroid Build Coastguard Worker 
687*77c1e3ccSAndroid Build Coastguard Worker   // change includes all joint functionality
688*77c1e3ccSAndroid Build Coastguard Worker   av1_change_config(cpi, oxcf, false);
689*77c1e3ccSAndroid Build Coastguard Worker 
690*77c1e3ccSAndroid Build Coastguard Worker   cpi->ref_frame_flags = 0;
691*77c1e3ccSAndroid Build Coastguard Worker 
692*77c1e3ccSAndroid Build Coastguard Worker   // Reset resize pending flags
693*77c1e3ccSAndroid Build Coastguard Worker   resize_pending_params->width = 0;
694*77c1e3ccSAndroid Build Coastguard Worker   resize_pending_params->height = 0;
695*77c1e3ccSAndroid Build Coastguard Worker 
696*77c1e3ccSAndroid Build Coastguard Worker   // Setup identity scale factor
697*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_scale_factors_for_frame(&cm->sf_identity, 1, 1, 1, 1);
698*77c1e3ccSAndroid Build Coastguard Worker 
699*77c1e3ccSAndroid Build Coastguard Worker   init_buffer_indices(&cpi->force_intpel_info, cm->remapped_ref_idx);
700*77c1e3ccSAndroid Build Coastguard Worker 
701*77c1e3ccSAndroid Build Coastguard Worker   av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
702*77c1e3ccSAndroid Build Coastguard Worker }
703*77c1e3ccSAndroid Build Coastguard Worker 
av1_change_config_seq(struct AV1_PRIMARY * ppi,const AV1EncoderConfig * oxcf,bool * is_sb_size_changed)704*77c1e3ccSAndroid Build Coastguard Worker void av1_change_config_seq(struct AV1_PRIMARY *ppi,
705*77c1e3ccSAndroid Build Coastguard Worker                            const AV1EncoderConfig *oxcf,
706*77c1e3ccSAndroid Build Coastguard Worker                            bool *is_sb_size_changed) {
707*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader *const seq_params = &ppi->seq_params;
708*77c1e3ccSAndroid Build Coastguard Worker   const FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg;
709*77c1e3ccSAndroid Build Coastguard Worker   const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
710*77c1e3ccSAndroid Build Coastguard Worker   const ColorCfg *const color_cfg = &oxcf->color_cfg;
711*77c1e3ccSAndroid Build Coastguard Worker 
712*77c1e3ccSAndroid Build Coastguard Worker   if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
713*77c1e3ccSAndroid Build Coastguard Worker   seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
714*77c1e3ccSAndroid Build Coastguard Worker   seq_params->color_primaries = color_cfg->color_primaries;
715*77c1e3ccSAndroid Build Coastguard Worker   seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
716*77c1e3ccSAndroid Build Coastguard Worker   seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
717*77c1e3ccSAndroid Build Coastguard Worker   seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
718*77c1e3ccSAndroid Build Coastguard Worker   seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
719*77c1e3ccSAndroid Build Coastguard Worker   seq_params->color_range = color_cfg->color_range;
720*77c1e3ccSAndroid Build Coastguard Worker 
721*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(seq_params->profile <= PROFILE_1,
722*77c1e3ccSAndroid Build Coastguard Worker                  seq_params->bit_depth <= AOM_BITS_10));
723*77c1e3ccSAndroid Build Coastguard Worker 
724*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info_present = dec_model_cfg->timing_info_present;
725*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info.num_units_in_display_tick =
726*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->timing_info.num_units_in_display_tick;
727*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
728*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info.equal_picture_interval =
729*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->timing_info.equal_picture_interval;
730*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info.num_ticks_per_picture =
731*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->timing_info.num_ticks_per_picture;
732*77c1e3ccSAndroid Build Coastguard Worker 
733*77c1e3ccSAndroid Build Coastguard Worker   seq_params->display_model_info_present_flag =
734*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->display_model_info_present_flag;
735*77c1e3ccSAndroid Build Coastguard Worker   seq_params->decoder_model_info_present_flag =
736*77c1e3ccSAndroid Build Coastguard Worker       dec_model_cfg->decoder_model_info_present_flag;
737*77c1e3ccSAndroid Build Coastguard Worker   if (dec_model_cfg->decoder_model_info_present_flag) {
738*77c1e3ccSAndroid Build Coastguard Worker     // set the decoder model parameters in schedule mode
739*77c1e3ccSAndroid Build Coastguard Worker     seq_params->decoder_model_info.num_units_in_decoding_tick =
740*77c1e3ccSAndroid Build Coastguard Worker         dec_model_cfg->num_units_in_decoding_tick;
741*77c1e3ccSAndroid Build Coastguard Worker     ppi->buffer_removal_time_present = 1;
742*77c1e3ccSAndroid Build Coastguard Worker     av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
743*77c1e3ccSAndroid Build Coastguard Worker     av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
744*77c1e3ccSAndroid Build Coastguard Worker   } else if (seq_params->timing_info_present &&
745*77c1e3ccSAndroid Build Coastguard Worker              seq_params->timing_info.equal_picture_interval &&
746*77c1e3ccSAndroid Build Coastguard Worker              !seq_params->decoder_model_info_present_flag) {
747*77c1e3ccSAndroid Build Coastguard Worker     // set the decoder model parameters in resource availability mode
748*77c1e3ccSAndroid Build Coastguard Worker     av1_set_resource_availability_parameters(&seq_params->op_params[0]);
749*77c1e3ccSAndroid Build Coastguard Worker   } else {
750*77c1e3ccSAndroid Build Coastguard Worker     seq_params->op_params[0].initial_display_delay =
751*77c1e3ccSAndroid Build Coastguard Worker         10;  // Default value (not signaled)
752*77c1e3ccSAndroid Build Coastguard Worker   }
753*77c1e3ccSAndroid Build Coastguard Worker 
754*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
755*77c1e3ccSAndroid Build Coastguard Worker   av1_update_film_grain_parameters_seq(ppi, oxcf);
756*77c1e3ccSAndroid Build Coastguard Worker #endif
757*77c1e3ccSAndroid Build Coastguard Worker 
758*77c1e3ccSAndroid Build Coastguard Worker   int sb_size = seq_params->sb_size;
759*77c1e3ccSAndroid Build Coastguard Worker   // Superblock size should not be updated after the first key frame.
760*77c1e3ccSAndroid Build Coastguard Worker   if (!ppi->seq_params_locked) {
761*77c1e3ccSAndroid Build Coastguard Worker     set_sb_size(seq_params, av1_select_sb_size(oxcf, frm_dim_cfg->width,
762*77c1e3ccSAndroid Build Coastguard Worker                                                frm_dim_cfg->height,
763*77c1e3ccSAndroid Build Coastguard Worker                                                ppi->number_spatial_layers));
764*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
765*77c1e3ccSAndroid Build Coastguard Worker       seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
766*77c1e3ccSAndroid Build Coastguard Worker   }
767*77c1e3ccSAndroid Build Coastguard Worker   if (is_sb_size_changed != NULL && sb_size != seq_params->sb_size)
768*77c1e3ccSAndroid Build Coastguard Worker     *is_sb_size_changed = true;
769*77c1e3ccSAndroid Build Coastguard Worker 
770*77c1e3ccSAndroid Build Coastguard Worker   // Init sequence level coding tools
771*77c1e3ccSAndroid Build Coastguard Worker   // This should not be called after the first key frame.
772*77c1e3ccSAndroid Build Coastguard Worker   if (!ppi->seq_params_locked) {
773*77c1e3ccSAndroid Build Coastguard Worker     seq_params->operating_points_cnt_minus_1 =
774*77c1e3ccSAndroid Build Coastguard Worker         (ppi->number_spatial_layers > 1 || ppi->number_temporal_layers > 1)
775*77c1e3ccSAndroid Build Coastguard Worker             ? ppi->number_spatial_layers * ppi->number_temporal_layers - 1
776*77c1e3ccSAndroid Build Coastguard Worker             : 0;
777*77c1e3ccSAndroid Build Coastguard Worker     init_seq_coding_tools(ppi, oxcf,
778*77c1e3ccSAndroid Build Coastguard Worker                           ppi->use_svc || ppi->rtc_ref.set_ref_frame_config);
779*77c1e3ccSAndroid Build Coastguard Worker   }
780*77c1e3ccSAndroid Build Coastguard Worker   seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr;
781*77c1e3ccSAndroid Build Coastguard Worker 
782*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
783*77c1e3ccSAndroid Build Coastguard Worker   highbd_set_var_fns(ppi);
784*77c1e3ccSAndroid Build Coastguard Worker #endif
785*77c1e3ccSAndroid Build Coastguard Worker 
786*77c1e3ccSAndroid Build Coastguard Worker   set_primary_rc_buffer_sizes(oxcf, ppi);
787*77c1e3ccSAndroid Build Coastguard Worker }
788*77c1e3ccSAndroid Build Coastguard Worker 
av1_change_config(struct AV1_COMP * cpi,const AV1EncoderConfig * oxcf,bool is_sb_size_changed)789*77c1e3ccSAndroid Build Coastguard Worker void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf,
790*77c1e3ccSAndroid Build Coastguard Worker                        bool is_sb_size_changed) {
791*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
792*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader *const seq_params = cm->seq_params;
793*77c1e3ccSAndroid Build Coastguard Worker   RATE_CONTROL *const rc = &cpi->rc;
794*77c1e3ccSAndroid Build Coastguard Worker   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
795*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *const x = &cpi->td.mb;
796*77c1e3ccSAndroid Build Coastguard Worker   AV1LevelParams *const level_params = &cpi->ppi->level_params;
797*77c1e3ccSAndroid Build Coastguard Worker   RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
798*77c1e3ccSAndroid Build Coastguard Worker   const FrameDimensionCfg *const frm_dim_cfg = &cpi->oxcf.frm_dim_cfg;
799*77c1e3ccSAndroid Build Coastguard Worker   const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
800*77c1e3ccSAndroid Build Coastguard Worker   FeatureFlags *const features = &cm->features;
801*77c1e3ccSAndroid Build Coastguard Worker 
802*77c1e3ccSAndroid Build Coastguard Worker   // in case of LAP, lag in frames is set according to number of lap buffers
803*77c1e3ccSAndroid Build Coastguard Worker   // calculated at init time. This stores and restores LAP's lag in frames to
804*77c1e3ccSAndroid Build Coastguard Worker   // prevent override by new cfg.
805*77c1e3ccSAndroid Build Coastguard Worker   int lap_lag_in_frames = -1;
806*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->ppi->lap_enabled && cpi->compressor_stage == LAP_STAGE) {
807*77c1e3ccSAndroid Build Coastguard Worker     lap_lag_in_frames = cpi->oxcf.gf_cfg.lag_in_frames;
808*77c1e3ccSAndroid Build Coastguard Worker   }
809*77c1e3ccSAndroid Build Coastguard Worker 
810*77c1e3ccSAndroid Build Coastguard Worker   cpi->oxcf = *oxcf;
811*77c1e3ccSAndroid Build Coastguard Worker 
812*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
813*77c1e3ccSAndroid Build Coastguard Worker   av1_update_film_grain_parameters(cpi, oxcf);
814*77c1e3ccSAndroid Build Coastguard Worker #endif
815*77c1e3ccSAndroid Build Coastguard Worker 
816*77c1e3ccSAndroid Build Coastguard Worker   // When user provides superres_mode = AOM_SUPERRES_AUTO, we still initialize
817*77c1e3ccSAndroid Build Coastguard Worker   // superres mode for current encoding = AOM_SUPERRES_NONE. This is to ensure
818*77c1e3ccSAndroid Build Coastguard Worker   // that any analysis (e.g. TPL) happening outside the main encoding loop still
819*77c1e3ccSAndroid Build Coastguard Worker   // happens at full resolution.
820*77c1e3ccSAndroid Build Coastguard Worker   // This value will later be set appropriately just before main encoding loop.
821*77c1e3ccSAndroid Build Coastguard Worker   cpi->superres_mode = oxcf->superres_cfg.superres_mode == AOM_SUPERRES_AUTO
822*77c1e3ccSAndroid Build Coastguard Worker                            ? AOM_SUPERRES_NONE
823*77c1e3ccSAndroid Build Coastguard Worker                            : oxcf->superres_cfg.superres_mode;  // default
824*77c1e3ccSAndroid Build Coastguard Worker   x->e_mbd.bd = (int)seq_params->bit_depth;
825*77c1e3ccSAndroid Build Coastguard Worker   x->e_mbd.global_motion = cm->global_motion;
826*77c1e3ccSAndroid Build Coastguard Worker 
827*77c1e3ccSAndroid Build Coastguard Worker   memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
828*77c1e3ccSAndroid Build Coastguard Worker          sizeof(level_params->target_seq_level_idx));
829*77c1e3ccSAndroid Build Coastguard Worker   level_params->keep_level_stats = 0;
830*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
831*77c1e3ccSAndroid Build Coastguard Worker     if (level_params->target_seq_level_idx[i] < SEQ_LEVELS ||
832*77c1e3ccSAndroid Build Coastguard Worker         level_params->target_seq_level_idx[i] == SEQ_LEVEL_KEEP_STATS) {
833*77c1e3ccSAndroid Build Coastguard Worker       level_params->keep_level_stats |= 1u << i;
834*77c1e3ccSAndroid Build Coastguard Worker       if (!level_params->level_info[i]) {
835*77c1e3ccSAndroid Build Coastguard Worker         CHECK_MEM_ERROR(cm, level_params->level_info[i],
836*77c1e3ccSAndroid Build Coastguard Worker                         aom_calloc(1, sizeof(*level_params->level_info[i])));
837*77c1e3ccSAndroid Build Coastguard Worker       }
838*77c1e3ccSAndroid Build Coastguard Worker     }
839*77c1e3ccSAndroid Build Coastguard Worker   }
840*77c1e3ccSAndroid Build Coastguard Worker 
841*77c1e3ccSAndroid Build Coastguard Worker   // TODO(huisu@): level targeting currently only works for the 0th operating
842*77c1e3ccSAndroid Build Coastguard Worker   // point, so scalable coding is not supported yet.
843*77c1e3ccSAndroid Build Coastguard Worker   if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) {
844*77c1e3ccSAndroid Build Coastguard Worker     // Adjust encoder config in order to meet target level.
845*77c1e3ccSAndroid Build Coastguard Worker     config_target_level(cpi, level_params->target_seq_level_idx[0],
846*77c1e3ccSAndroid Build Coastguard Worker                         seq_params->tier[0]);
847*77c1e3ccSAndroid Build Coastguard Worker   }
848*77c1e3ccSAndroid Build Coastguard Worker 
849*77c1e3ccSAndroid Build Coastguard Worker   if (has_no_stats_stage(cpi) && (rc_cfg->mode == AOM_Q)) {
850*77c1e3ccSAndroid Build Coastguard Worker     p_rc->baseline_gf_interval = FIXED_GF_INTERVAL;
851*77c1e3ccSAndroid Build Coastguard Worker   } else if (!is_one_pass_rt_params(cpi) ||
852*77c1e3ccSAndroid Build Coastguard Worker              cm->current_frame.frame_number == 0) {
853*77c1e3ccSAndroid Build Coastguard Worker     // For rtc mode: logic for setting the baseline_gf_interval is done
854*77c1e3ccSAndroid Build Coastguard Worker     // in av1_get_one_pass_rt_params(), and it should not be reset here in
855*77c1e3ccSAndroid Build Coastguard Worker     // change_config(), unless after init_config (first frame).
856*77c1e3ccSAndroid Build Coastguard Worker     p_rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
857*77c1e3ccSAndroid Build Coastguard Worker   }
858*77c1e3ccSAndroid Build Coastguard Worker 
859*77c1e3ccSAndroid Build Coastguard Worker   refresh_frame->golden_frame = false;
860*77c1e3ccSAndroid Build Coastguard Worker   refresh_frame->bwd_ref_frame = false;
861*77c1e3ccSAndroid Build Coastguard Worker 
862*77c1e3ccSAndroid Build Coastguard Worker   features->refresh_frame_context =
863*77c1e3ccSAndroid Build Coastguard Worker       (oxcf->tool_cfg.frame_parallel_decoding_mode)
864*77c1e3ccSAndroid Build Coastguard Worker           ? REFRESH_FRAME_CONTEXT_DISABLED
865*77c1e3ccSAndroid Build Coastguard Worker           : REFRESH_FRAME_CONTEXT_BACKWARD;
866*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->tile_cfg.enable_large_scale_tile)
867*77c1e3ccSAndroid Build Coastguard Worker     features->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
868*77c1e3ccSAndroid Build Coastguard Worker 
869*77c1e3ccSAndroid Build Coastguard Worker   if (x->palette_buffer == NULL) {
870*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(cm, x->palette_buffer,
871*77c1e3ccSAndroid Build Coastguard Worker                     aom_memalign(16, sizeof(*x->palette_buffer)));
872*77c1e3ccSAndroid Build Coastguard Worker   }
873*77c1e3ccSAndroid Build Coastguard Worker 
874*77c1e3ccSAndroid Build Coastguard Worker   if (x->tmp_conv_dst == NULL) {
875*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(
876*77c1e3ccSAndroid Build Coastguard Worker         cm, x->tmp_conv_dst,
877*77c1e3ccSAndroid Build Coastguard Worker         aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
878*77c1e3ccSAndroid Build Coastguard Worker     x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
879*77c1e3ccSAndroid Build Coastguard Worker   }
880*77c1e3ccSAndroid Build Coastguard Worker   // The buffers 'tmp_pred_bufs[]' and 'comp_rd_buffer' are used in inter frames
881*77c1e3ccSAndroid Build Coastguard Worker   // to store intermediate inter mode prediction results and are not required
882*77c1e3ccSAndroid Build Coastguard Worker   // for allintra encoding mode. Hence, the memory allocations for these buffers
883*77c1e3ccSAndroid Build Coastguard Worker   // are avoided for allintra encoding mode.
884*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.kf_cfg.key_freq_max != 0) {
885*77c1e3ccSAndroid Build Coastguard Worker     if (x->comp_rd_buffer.pred0 == NULL)
886*77c1e3ccSAndroid Build Coastguard Worker       alloc_compound_type_rd_buffers(cm->error, &x->comp_rd_buffer);
887*77c1e3ccSAndroid Build Coastguard Worker 
888*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 2; ++i) {
889*77c1e3ccSAndroid Build Coastguard Worker       if (x->tmp_pred_bufs[i] == NULL) {
890*77c1e3ccSAndroid Build Coastguard Worker         CHECK_MEM_ERROR(cm, x->tmp_pred_bufs[i],
891*77c1e3ccSAndroid Build Coastguard Worker                         aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
892*77c1e3ccSAndroid Build Coastguard Worker                                              sizeof(*x->tmp_pred_bufs[i])));
893*77c1e3ccSAndroid Build Coastguard Worker         x->e_mbd.tmp_obmc_bufs[i] = x->tmp_pred_bufs[i];
894*77c1e3ccSAndroid Build Coastguard Worker       }
895*77c1e3ccSAndroid Build Coastguard Worker     }
896*77c1e3ccSAndroid Build Coastguard Worker   }
897*77c1e3ccSAndroid Build Coastguard Worker 
898*77c1e3ccSAndroid Build Coastguard Worker   av1_reset_segment_features(cm);
899*77c1e3ccSAndroid Build Coastguard Worker 
900*77c1e3ccSAndroid Build Coastguard Worker   av1_set_high_precision_mv(cpi, 1, 0);
901*77c1e3ccSAndroid Build Coastguard Worker 
902*77c1e3ccSAndroid Build Coastguard Worker   // Under a configuration change, where maximum_buffer_size may change,
903*77c1e3ccSAndroid Build Coastguard Worker   // keep buffer level clipped to the maximum allowed buffer size.
904*77c1e3ccSAndroid Build Coastguard Worker   p_rc->bits_off_target =
905*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(p_rc->bits_off_target, p_rc->maximum_buffer_size);
906*77c1e3ccSAndroid Build Coastguard Worker   p_rc->buffer_level = AOMMIN(p_rc->buffer_level, p_rc->maximum_buffer_size);
907*77c1e3ccSAndroid Build Coastguard Worker 
908*77c1e3ccSAndroid Build Coastguard Worker   // Set up frame rate and related parameters rate control values.
909*77c1e3ccSAndroid Build Coastguard Worker   av1_new_framerate(cpi, cpi->framerate);
910*77c1e3ccSAndroid Build Coastguard Worker 
911*77c1e3ccSAndroid Build Coastguard Worker   // Set absolute upper and lower quality limits
912*77c1e3ccSAndroid Build Coastguard Worker   rc->worst_quality = rc_cfg->worst_allowed_q;
913*77c1e3ccSAndroid Build Coastguard Worker   rc->best_quality = rc_cfg->best_allowed_q;
914*77c1e3ccSAndroid Build Coastguard Worker 
915*77c1e3ccSAndroid Build Coastguard Worker   // If lossless has been requested make sure average Q accumulators are reset.
916*77c1e3ccSAndroid Build Coastguard Worker   if (is_lossless_requested(&cpi->oxcf.rc_cfg)) {
917*77c1e3ccSAndroid Build Coastguard Worker     int i;
918*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < FRAME_TYPES; ++i) {
919*77c1e3ccSAndroid Build Coastguard Worker       p_rc->avg_frame_qindex[i] = 0;
920*77c1e3ccSAndroid Build Coastguard Worker     }
921*77c1e3ccSAndroid Build Coastguard Worker   }
922*77c1e3ccSAndroid Build Coastguard Worker 
923*77c1e3ccSAndroid Build Coastguard Worker   features->interp_filter =
924*77c1e3ccSAndroid Build Coastguard Worker       oxcf->tile_cfg.enable_large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
925*77c1e3ccSAndroid Build Coastguard Worker   features->switchable_motion_mode = is_switchable_motion_mode_allowed(
926*77c1e3ccSAndroid Build Coastguard Worker       features->allow_warped_motion, oxcf->motion_mode_cfg.enable_obmc);
927*77c1e3ccSAndroid Build Coastguard Worker 
928*77c1e3ccSAndroid Build Coastguard Worker   if (frm_dim_cfg->render_width > 0 && frm_dim_cfg->render_height > 0) {
929*77c1e3ccSAndroid Build Coastguard Worker     cm->render_width = frm_dim_cfg->render_width;
930*77c1e3ccSAndroid Build Coastguard Worker     cm->render_height = frm_dim_cfg->render_height;
931*77c1e3ccSAndroid Build Coastguard Worker   } else {
932*77c1e3ccSAndroid Build Coastguard Worker     cm->render_width = frm_dim_cfg->width;
933*77c1e3ccSAndroid Build Coastguard Worker     cm->render_height = frm_dim_cfg->height;
934*77c1e3ccSAndroid Build Coastguard Worker   }
935*77c1e3ccSAndroid Build Coastguard Worker   cm->width = frm_dim_cfg->width;
936*77c1e3ccSAndroid Build Coastguard Worker   cm->height = frm_dim_cfg->height;
937*77c1e3ccSAndroid Build Coastguard Worker 
938*77c1e3ccSAndroid Build Coastguard Worker   if (cm->width > cpi->data_alloc_width ||
939*77c1e3ccSAndroid Build Coastguard Worker       cm->height > cpi->data_alloc_height || is_sb_size_changed) {
940*77c1e3ccSAndroid Build Coastguard Worker     av1_free_context_buffers(cm);
941*77c1e3ccSAndroid Build Coastguard Worker     av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
942*77c1e3ccSAndroid Build Coastguard Worker     av1_free_sms_tree(&cpi->td);
943*77c1e3ccSAndroid Build Coastguard Worker     av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
944*77c1e3ccSAndroid Build Coastguard Worker     cpi->td.firstpass_ctx = NULL;
945*77c1e3ccSAndroid Build Coastguard Worker     alloc_compressor_data(cpi);
946*77c1e3ccSAndroid Build Coastguard Worker     realloc_segmentation_maps(cpi);
947*77c1e3ccSAndroid Build Coastguard Worker     cpi->data_alloc_width = cm->width;
948*77c1e3ccSAndroid Build Coastguard Worker     cpi->data_alloc_height = cm->height;
949*77c1e3ccSAndroid Build Coastguard Worker     cpi->frame_size_related_setup_done = false;
950*77c1e3ccSAndroid Build Coastguard Worker   }
951*77c1e3ccSAndroid Build Coastguard Worker   av1_update_frame_size(cpi);
952*77c1e3ccSAndroid Build Coastguard Worker 
953*77c1e3ccSAndroid Build Coastguard Worker   rc->is_src_frame_alt_ref = 0;
954*77c1e3ccSAndroid Build Coastguard Worker 
955*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->ppi->rtc_ref.set_ref_frame_config)
956*77c1e3ccSAndroid Build Coastguard Worker     cpi->ext_flags.refresh_frame.update_pending = 0;
957*77c1e3ccSAndroid Build Coastguard Worker   cpi->ext_flags.refresh_frame_context_pending = 0;
958*77c1e3ccSAndroid Build Coastguard Worker 
959*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->ppi->use_svc)
960*77c1e3ccSAndroid Build Coastguard Worker     av1_update_layer_context_change_config(cpi, rc_cfg->target_bandwidth);
961*77c1e3ccSAndroid Build Coastguard Worker 
962*77c1e3ccSAndroid Build Coastguard Worker   check_reset_rc_flag(cpi);
963*77c1e3ccSAndroid Build Coastguard Worker 
964*77c1e3ccSAndroid Build Coastguard Worker   // restore the value of lag_in_frame for LAP stage.
965*77c1e3ccSAndroid Build Coastguard Worker   if (lap_lag_in_frames != -1) {
966*77c1e3ccSAndroid Build Coastguard Worker     cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
967*77c1e3ccSAndroid Build Coastguard Worker   }
968*77c1e3ccSAndroid Build Coastguard Worker 
969*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
970*77c1e3ccSAndroid Build Coastguard Worker   assert(!oxcf->tool_cfg.enable_global_motion);
971*77c1e3ccSAndroid Build Coastguard Worker   cpi->alloc_pyramid = false;
972*77c1e3ccSAndroid Build Coastguard Worker #else
973*77c1e3ccSAndroid Build Coastguard Worker   cpi->alloc_pyramid = oxcf->tool_cfg.enable_global_motion;
974*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_REALTIME_ONLY
975*77c1e3ccSAndroid Build Coastguard Worker }
976*77c1e3ccSAndroid Build Coastguard Worker 
init_frame_info(FRAME_INFO * frame_info,const AV1_COMMON * const cm)977*77c1e3ccSAndroid Build Coastguard Worker static inline void init_frame_info(FRAME_INFO *frame_info,
978*77c1e3ccSAndroid Build Coastguard Worker                                    const AV1_COMMON *const cm) {
979*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
980*77c1e3ccSAndroid Build Coastguard Worker   const SequenceHeader *const seq_params = cm->seq_params;
981*77c1e3ccSAndroid Build Coastguard Worker   frame_info->frame_width = cm->width;
982*77c1e3ccSAndroid Build Coastguard Worker   frame_info->frame_height = cm->height;
983*77c1e3ccSAndroid Build Coastguard Worker   frame_info->mi_cols = mi_params->mi_cols;
984*77c1e3ccSAndroid Build Coastguard Worker   frame_info->mi_rows = mi_params->mi_rows;
985*77c1e3ccSAndroid Build Coastguard Worker   frame_info->mb_cols = mi_params->mb_cols;
986*77c1e3ccSAndroid Build Coastguard Worker   frame_info->mb_rows = mi_params->mb_rows;
987*77c1e3ccSAndroid Build Coastguard Worker   frame_info->num_mbs = mi_params->MBs;
988*77c1e3ccSAndroid Build Coastguard Worker   frame_info->bit_depth = seq_params->bit_depth;
989*77c1e3ccSAndroid Build Coastguard Worker   frame_info->subsampling_x = seq_params->subsampling_x;
990*77c1e3ccSAndroid Build Coastguard Worker   frame_info->subsampling_y = seq_params->subsampling_y;
991*77c1e3ccSAndroid Build Coastguard Worker }
992*77c1e3ccSAndroid Build Coastguard Worker 
init_frame_index_set(FRAME_INDEX_SET * frame_index_set)993*77c1e3ccSAndroid Build Coastguard Worker static inline void init_frame_index_set(FRAME_INDEX_SET *frame_index_set) {
994*77c1e3ccSAndroid Build Coastguard Worker   frame_index_set->show_frame_count = 0;
995*77c1e3ccSAndroid Build Coastguard Worker }
996*77c1e3ccSAndroid Build Coastguard Worker 
update_counters_for_show_frame(AV1_COMP * const cpi)997*77c1e3ccSAndroid Build Coastguard Worker static inline void update_counters_for_show_frame(AV1_COMP *const cpi) {
998*77c1e3ccSAndroid Build Coastguard Worker   assert(cpi->common.show_frame);
999*77c1e3ccSAndroid Build Coastguard Worker   cpi->frame_index_set.show_frame_count++;
1000*77c1e3ccSAndroid Build Coastguard Worker   cpi->common.current_frame.frame_number++;
1001*77c1e3ccSAndroid Build Coastguard Worker }
1002*77c1e3ccSAndroid Build Coastguard Worker 
av1_create_primary_compressor(struct aom_codec_pkt_list * pkt_list_head,int num_lap_buffers,const AV1EncoderConfig * oxcf)1003*77c1e3ccSAndroid Build Coastguard Worker AV1_PRIMARY *av1_create_primary_compressor(
1004*77c1e3ccSAndroid Build Coastguard Worker     struct aom_codec_pkt_list *pkt_list_head, int num_lap_buffers,
1005*77c1e3ccSAndroid Build Coastguard Worker     const AV1EncoderConfig *oxcf) {
1006*77c1e3ccSAndroid Build Coastguard Worker   AV1_PRIMARY *volatile const ppi = aom_memalign(32, sizeof(AV1_PRIMARY));
1007*77c1e3ccSAndroid Build Coastguard Worker   if (!ppi) return NULL;
1008*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(*ppi);
1009*77c1e3ccSAndroid Build Coastguard Worker 
1010*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
1011*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
1012*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
1013*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(ppi->error.jmp)) {
1014*77c1e3ccSAndroid Build Coastguard Worker     ppi->error.setjmp = 0;
1015*77c1e3ccSAndroid Build Coastguard Worker     av1_remove_primary_compressor(ppi);
1016*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1017*77c1e3ccSAndroid Build Coastguard Worker   }
1018*77c1e3ccSAndroid Build Coastguard Worker   ppi->error.setjmp = 1;
1019*77c1e3ccSAndroid Build Coastguard Worker 
1020*77c1e3ccSAndroid Build Coastguard Worker   ppi->seq_params_locked = 0;
1021*77c1e3ccSAndroid Build Coastguard Worker   ppi->lap_enabled = num_lap_buffers > 0;
1022*77c1e3ccSAndroid Build Coastguard Worker   ppi->output_pkt_list = pkt_list_head;
1023*77c1e3ccSAndroid Build Coastguard Worker   ppi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1024*77c1e3ccSAndroid Build Coastguard Worker   ppi->frames_left = oxcf->input_cfg.limit;
1025*77c1e3ccSAndroid Build Coastguard Worker   ppi->num_fp_contexts = 1;
1026*77c1e3ccSAndroid Build Coastguard Worker 
1027*77c1e3ccSAndroid Build Coastguard Worker   init_config_sequence(ppi, oxcf);
1028*77c1e3ccSAndroid Build Coastguard Worker 
1029*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1030*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(ppi->aggregate_fc);
1031*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
1032*77c1e3ccSAndroid Build Coastguard Worker 
1033*77c1e3ccSAndroid Build Coastguard Worker   av1_primary_rc_init(oxcf, &ppi->p_rc);
1034*77c1e3ccSAndroid Build Coastguard Worker 
1035*77c1e3ccSAndroid Build Coastguard Worker   // For two pass and lag_in_frames > 33 in LAP.
1036*77c1e3ccSAndroid Build Coastguard Worker   ppi->p_rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_2;
1037*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->lap_enabled) {
1038*77c1e3ccSAndroid Build Coastguard Worker     if ((num_lap_buffers <
1039*77c1e3ccSAndroid Build Coastguard Worker          (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)) &&
1040*77c1e3ccSAndroid Build Coastguard Worker         num_lap_buffers >= (MAX_GF_LENGTH_LAP + 3)) {
1041*77c1e3ccSAndroid Build Coastguard Worker       /*
1042*77c1e3ccSAndroid Build Coastguard Worker        * For lag in frames >= 19 and <33, enable scenecut
1043*77c1e3ccSAndroid Build Coastguard Worker        * with limited future frame prediction.
1044*77c1e3ccSAndroid Build Coastguard Worker        */
1045*77c1e3ccSAndroid Build Coastguard Worker       ppi->p_rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_1;
1046*77c1e3ccSAndroid Build Coastguard Worker     } else if (num_lap_buffers < (MAX_GF_LENGTH_LAP + 3)) {
1047*77c1e3ccSAndroid Build Coastguard Worker       // Disable scenecut when lag_in_frames < 19.
1048*77c1e3ccSAndroid Build Coastguard Worker       ppi->p_rc.enable_scenecut_detection = DISABLE_SCENECUT;
1049*77c1e3ccSAndroid Build Coastguard Worker     }
1050*77c1e3ccSAndroid Build Coastguard Worker   }
1051*77c1e3ccSAndroid Build Coastguard Worker 
1052*77c1e3ccSAndroid Build Coastguard Worker #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, SDX3DF, JSDAF, JSVAF) \
1053*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].sdf = SDF;                                            \
1054*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].sdaf = SDAF;                                          \
1055*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].vf = VF;                                              \
1056*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].svf = SVF;                                            \
1057*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].svaf = SVAF;                                          \
1058*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].sdx4df = SDX4DF;                                      \
1059*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].jsdaf = JSDAF;                                        \
1060*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].jsvaf = JSVAF;                                        \
1061*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].sdx3df = SDX3DF;
1062*77c1e3ccSAndroid Build Coastguard Worker 
1063*77c1e3ccSAndroid Build Coastguard Worker // Realtime mode doesn't use 4x rectangular blocks.
1064*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1065*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
1066*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
1067*77c1e3ccSAndroid Build Coastguard Worker       aom_sad4x16x4d, aom_sad4x16x3d, aom_dist_wtd_sad4x16_avg,
1068*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance4x16)
1069*77c1e3ccSAndroid Build Coastguard Worker 
1070*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
1071*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
1072*77c1e3ccSAndroid Build Coastguard Worker       aom_sad16x4x4d, aom_sad16x4x3d, aom_dist_wtd_sad16x4_avg,
1073*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance16x4)
1074*77c1e3ccSAndroid Build Coastguard Worker 
1075*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
1076*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
1077*77c1e3ccSAndroid Build Coastguard Worker       aom_sad8x32x4d, aom_sad8x32x3d, aom_dist_wtd_sad8x32_avg,
1078*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance8x32)
1079*77c1e3ccSAndroid Build Coastguard Worker 
1080*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
1081*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
1082*77c1e3ccSAndroid Build Coastguard Worker       aom_sad32x8x4d, aom_sad32x8x3d, aom_dist_wtd_sad32x8_avg,
1083*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance32x8)
1084*77c1e3ccSAndroid Build Coastguard Worker 
1085*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
1086*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
1087*77c1e3ccSAndroid Build Coastguard Worker       aom_sad16x64x4d, aom_sad16x64x3d, aom_dist_wtd_sad16x64_avg,
1088*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance16x64)
1089*77c1e3ccSAndroid Build Coastguard Worker 
1090*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
1091*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
1092*77c1e3ccSAndroid Build Coastguard Worker       aom_sad64x16x4d, aom_sad64x16x3d, aom_dist_wtd_sad64x16_avg,
1093*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance64x16)
1094*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
1095*77c1e3ccSAndroid Build Coastguard Worker 
1096*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
1097*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
1098*77c1e3ccSAndroid Build Coastguard Worker       aom_sad128x128x4d, aom_sad128x128x3d, aom_dist_wtd_sad128x128_avg,
1099*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance128x128)
1100*77c1e3ccSAndroid Build Coastguard Worker 
1101*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
1102*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
1103*77c1e3ccSAndroid Build Coastguard Worker       aom_sad128x64x4d, aom_sad128x64x3d, aom_dist_wtd_sad128x64_avg,
1104*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance128x64)
1105*77c1e3ccSAndroid Build Coastguard Worker 
1106*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
1107*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
1108*77c1e3ccSAndroid Build Coastguard Worker       aom_sad64x128x4d, aom_sad64x128x3d, aom_dist_wtd_sad64x128_avg,
1109*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance64x128)
1110*77c1e3ccSAndroid Build Coastguard Worker 
1111*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
1112*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
1113*77c1e3ccSAndroid Build Coastguard Worker       aom_sad32x16x4d, aom_sad32x16x3d, aom_dist_wtd_sad32x16_avg,
1114*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance32x16)
1115*77c1e3ccSAndroid Build Coastguard Worker 
1116*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
1117*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
1118*77c1e3ccSAndroid Build Coastguard Worker       aom_sad16x32x4d, aom_sad16x32x3d, aom_dist_wtd_sad16x32_avg,
1119*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance16x32)
1120*77c1e3ccSAndroid Build Coastguard Worker 
1121*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
1122*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
1123*77c1e3ccSAndroid Build Coastguard Worker       aom_sad64x32x4d, aom_sad64x32x3d, aom_dist_wtd_sad64x32_avg,
1124*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance64x32)
1125*77c1e3ccSAndroid Build Coastguard Worker 
1126*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
1127*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
1128*77c1e3ccSAndroid Build Coastguard Worker       aom_sad32x64x4d, aom_sad32x64x3d, aom_dist_wtd_sad32x64_avg,
1129*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance32x64)
1130*77c1e3ccSAndroid Build Coastguard Worker 
1131*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
1132*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
1133*77c1e3ccSAndroid Build Coastguard Worker       aom_sad32x32x4d, aom_sad32x32x3d, aom_dist_wtd_sad32x32_avg,
1134*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance32x32)
1135*77c1e3ccSAndroid Build Coastguard Worker 
1136*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
1137*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
1138*77c1e3ccSAndroid Build Coastguard Worker       aom_sad64x64x4d, aom_sad64x64x3d, aom_dist_wtd_sad64x64_avg,
1139*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance64x64)
1140*77c1e3ccSAndroid Build Coastguard Worker 
1141*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
1142*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
1143*77c1e3ccSAndroid Build Coastguard Worker       aom_sad16x16x4d, aom_sad16x16x3d, aom_dist_wtd_sad16x16_avg,
1144*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance16x16)
1145*77c1e3ccSAndroid Build Coastguard Worker 
1146*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
1147*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
1148*77c1e3ccSAndroid Build Coastguard Worker       aom_sad16x8x4d, aom_sad16x8x3d, aom_dist_wtd_sad16x8_avg,
1149*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance16x8)
1150*77c1e3ccSAndroid Build Coastguard Worker 
1151*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
1152*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
1153*77c1e3ccSAndroid Build Coastguard Worker       aom_sad8x16x4d, aom_sad8x16x3d, aom_dist_wtd_sad8x16_avg,
1154*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance8x16)
1155*77c1e3ccSAndroid Build Coastguard Worker 
1156*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
1157*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
1158*77c1e3ccSAndroid Build Coastguard Worker       aom_sad8x8x3d, aom_dist_wtd_sad8x8_avg,
1159*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance8x8)
1160*77c1e3ccSAndroid Build Coastguard Worker 
1161*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
1162*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
1163*77c1e3ccSAndroid Build Coastguard Worker       aom_sad8x4x3d, aom_dist_wtd_sad8x4_avg,
1164*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance8x4)
1165*77c1e3ccSAndroid Build Coastguard Worker 
1166*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
1167*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
1168*77c1e3ccSAndroid Build Coastguard Worker       aom_sad4x8x3d, aom_dist_wtd_sad4x8_avg,
1169*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance4x8)
1170*77c1e3ccSAndroid Build Coastguard Worker 
1171*77c1e3ccSAndroid Build Coastguard Worker   BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
1172*77c1e3ccSAndroid Build Coastguard Worker       aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
1173*77c1e3ccSAndroid Build Coastguard Worker       aom_sad4x4x3d, aom_dist_wtd_sad4x4_avg,
1174*77c1e3ccSAndroid Build Coastguard Worker       aom_dist_wtd_sub_pixel_avg_variance4x4)
1175*77c1e3ccSAndroid Build Coastguard Worker 
1176*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1177*77c1e3ccSAndroid Build Coastguard Worker #define OBFP(BT, OSDF, OVF, OSVF) \
1178*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].osdf = OSDF;    \
1179*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].ovf = OVF;      \
1180*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].osvf = OSVF;
1181*77c1e3ccSAndroid Build Coastguard Worker 
1182*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
1183*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance128x128)
1184*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
1185*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance128x64)
1186*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
1187*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance64x128)
1188*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
1189*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance64x64)
1190*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
1191*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance64x32)
1192*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
1193*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance32x64)
1194*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
1195*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance32x32)
1196*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
1197*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance32x16)
1198*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
1199*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance16x32)
1200*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
1201*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance16x16)
1202*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
1203*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance16x8)
1204*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
1205*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance8x16)
1206*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
1207*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance8x8)
1208*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
1209*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance4x8)
1210*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
1211*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance8x4)
1212*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
1213*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance4x4)
1214*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
1215*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance4x16)
1216*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
1217*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance16x4)
1218*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
1219*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance8x32)
1220*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
1221*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance32x8)
1222*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
1223*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance16x64)
1224*77c1e3ccSAndroid Build Coastguard Worker   OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
1225*77c1e3ccSAndroid Build Coastguard Worker        aom_obmc_sub_pixel_variance64x16)
1226*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
1227*77c1e3ccSAndroid Build Coastguard Worker 
1228*77c1e3ccSAndroid Build Coastguard Worker #define MBFP(BT, MCSDF, MCSVF)  \
1229*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].msdf = MCSDF; \
1230*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].msvf = MCSVF;
1231*77c1e3ccSAndroid Build Coastguard Worker 
1232*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_128X128, aom_masked_sad128x128,
1233*77c1e3ccSAndroid Build Coastguard Worker        aom_masked_sub_pixel_variance128x128)
1234*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
1235*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
1236*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
1237*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
1238*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
1239*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
1240*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
1241*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
1242*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
1243*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
1244*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
1245*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
1246*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
1247*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
1248*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
1249*77c1e3ccSAndroid Build Coastguard Worker 
1250*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1251*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
1252*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
1253*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
1254*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
1255*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
1256*77c1e3ccSAndroid Build Coastguard Worker   MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
1257*77c1e3ccSAndroid Build Coastguard Worker #endif
1258*77c1e3ccSAndroid Build Coastguard Worker 
1259*77c1e3ccSAndroid Build Coastguard Worker #define SDSFP(BT, SDSF, SDSX4DF) \
1260*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].sdsf = SDSF;   \
1261*77c1e3ccSAndroid Build Coastguard Worker   ppi->fn_ptr[BT].sdsx4df = SDSX4DF;
1262*77c1e3ccSAndroid Build Coastguard Worker 
1263*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_128X128, aom_sad_skip_128x128, aom_sad_skip_128x128x4d)
1264*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_128X64, aom_sad_skip_128x64, aom_sad_skip_128x64x4d)
1265*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_64X128, aom_sad_skip_64x128, aom_sad_skip_64x128x4d)
1266*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_64X64, aom_sad_skip_64x64, aom_sad_skip_64x64x4d)
1267*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_64X32, aom_sad_skip_64x32, aom_sad_skip_64x32x4d)
1268*77c1e3ccSAndroid Build Coastguard Worker 
1269*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_32X64, aom_sad_skip_32x64, aom_sad_skip_32x64x4d)
1270*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_32X32, aom_sad_skip_32x32, aom_sad_skip_32x32x4d)
1271*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_32X16, aom_sad_skip_32x16, aom_sad_skip_32x16x4d)
1272*77c1e3ccSAndroid Build Coastguard Worker 
1273*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_16X32, aom_sad_skip_16x32, aom_sad_skip_16x32x4d)
1274*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_16X16, aom_sad_skip_16x16, aom_sad_skip_16x16x4d)
1275*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_16X8, aom_sad_skip_16x8, aom_sad_skip_16x8x4d)
1276*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_8X16, aom_sad_skip_8x16, aom_sad_skip_8x16x4d)
1277*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_8X8, aom_sad_skip_8x8, aom_sad_skip_8x8x4d)
1278*77c1e3ccSAndroid Build Coastguard Worker 
1279*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_4X8, aom_sad_skip_4x8, aom_sad_skip_4x8x4d)
1280*77c1e3ccSAndroid Build Coastguard Worker 
1281*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1282*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_64X16, aom_sad_skip_64x16, aom_sad_skip_64x16x4d)
1283*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_16X64, aom_sad_skip_16x64, aom_sad_skip_16x64x4d)
1284*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_32X8, aom_sad_skip_32x8, aom_sad_skip_32x8x4d)
1285*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_8X32, aom_sad_skip_8x32, aom_sad_skip_8x32x4d)
1286*77c1e3ccSAndroid Build Coastguard Worker   SDSFP(BLOCK_4X16, aom_sad_skip_4x16, aom_sad_skip_4x16x4d)
1287*77c1e3ccSAndroid Build Coastguard Worker #endif
1288*77c1e3ccSAndroid Build Coastguard Worker #undef SDSFP
1289*77c1e3ccSAndroid Build Coastguard Worker 
1290*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
1291*77c1e3ccSAndroid Build Coastguard Worker   highbd_set_var_fns(ppi);
1292*77c1e3ccSAndroid Build Coastguard Worker #endif
1293*77c1e3ccSAndroid Build Coastguard Worker 
1294*77c1e3ccSAndroid Build Coastguard Worker   {
1295*77c1e3ccSAndroid Build Coastguard Worker     // As cm->mi_params is a part of the frame level context (cpi), it is
1296*77c1e3ccSAndroid Build Coastguard Worker     // unavailable at this point. mi_params is created as a local temporary
1297*77c1e3ccSAndroid Build Coastguard Worker     // variable, to be passed into the functions used for allocating tpl
1298*77c1e3ccSAndroid Build Coastguard Worker     // buffers. The values in this variable are populated according to initial
1299*77c1e3ccSAndroid Build Coastguard Worker     // width and height of the frame.
1300*77c1e3ccSAndroid Build Coastguard Worker     CommonModeInfoParams mi_params;
1301*77c1e3ccSAndroid Build Coastguard Worker     enc_set_mb_mi(&mi_params, oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
1302*77c1e3ccSAndroid Build Coastguard Worker                   BLOCK_4X4);
1303*77c1e3ccSAndroid Build Coastguard Worker 
1304*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE bsize = BLOCK_16X16;
1305*77c1e3ccSAndroid Build Coastguard Worker     const int w = mi_size_wide[bsize];
1306*77c1e3ccSAndroid Build Coastguard Worker     const int h = mi_size_high[bsize];
1307*77c1e3ccSAndroid Build Coastguard Worker     const int num_cols = (mi_params.mi_cols + w - 1) / w;
1308*77c1e3ccSAndroid Build Coastguard Worker     const int num_rows = (mi_params.mi_rows + h - 1) / h;
1309*77c1e3ccSAndroid Build Coastguard Worker     AOM_CHECK_MEM_ERROR(
1310*77c1e3ccSAndroid Build Coastguard Worker         &ppi->error, ppi->tpl_sb_rdmult_scaling_factors,
1311*77c1e3ccSAndroid Build Coastguard Worker         aom_calloc(num_rows * num_cols,
1312*77c1e3ccSAndroid Build Coastguard Worker                    sizeof(*ppi->tpl_sb_rdmult_scaling_factors)));
1313*77c1e3ccSAndroid Build Coastguard Worker 
1314*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
1315*77c1e3ccSAndroid Build Coastguard Worker     ppi->b_calculate_blockiness = 1;
1316*77c1e3ccSAndroid Build Coastguard Worker     ppi->b_calculate_consistency = 1;
1317*77c1e3ccSAndroid Build Coastguard Worker 
1318*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i <= STAT_ALL; i++) {
1319*77c1e3ccSAndroid Build Coastguard Worker       ppi->psnr[0].stat[i] = 0;
1320*77c1e3ccSAndroid Build Coastguard Worker       ppi->psnr[1].stat[i] = 0;
1321*77c1e3ccSAndroid Build Coastguard Worker 
1322*77c1e3ccSAndroid Build Coastguard Worker       ppi->fastssim.stat[i] = 0;
1323*77c1e3ccSAndroid Build Coastguard Worker       ppi->psnrhvs.stat[i] = 0;
1324*77c1e3ccSAndroid Build Coastguard Worker     }
1325*77c1e3ccSAndroid Build Coastguard Worker 
1326*77c1e3ccSAndroid Build Coastguard Worker     ppi->psnr[0].worst = 100.0;
1327*77c1e3ccSAndroid Build Coastguard Worker     ppi->psnr[1].worst = 100.0;
1328*77c1e3ccSAndroid Build Coastguard Worker     ppi->worst_ssim = 100.0;
1329*77c1e3ccSAndroid Build Coastguard Worker     ppi->worst_ssim_hbd = 100.0;
1330*77c1e3ccSAndroid Build Coastguard Worker 
1331*77c1e3ccSAndroid Build Coastguard Worker     ppi->count[0] = 0;
1332*77c1e3ccSAndroid Build Coastguard Worker     ppi->count[1] = 0;
1333*77c1e3ccSAndroid Build Coastguard Worker     ppi->total_bytes = 0;
1334*77c1e3ccSAndroid Build Coastguard Worker 
1335*77c1e3ccSAndroid Build Coastguard Worker     if (ppi->b_calculate_psnr) {
1336*77c1e3ccSAndroid Build Coastguard Worker       ppi->total_sq_error[0] = 0;
1337*77c1e3ccSAndroid Build Coastguard Worker       ppi->total_samples[0] = 0;
1338*77c1e3ccSAndroid Build Coastguard Worker       ppi->total_sq_error[1] = 0;
1339*77c1e3ccSAndroid Build Coastguard Worker       ppi->total_samples[1] = 0;
1340*77c1e3ccSAndroid Build Coastguard Worker       ppi->total_recode_hits = 0;
1341*77c1e3ccSAndroid Build Coastguard Worker       ppi->summed_quality = 0;
1342*77c1e3ccSAndroid Build Coastguard Worker       ppi->summed_weights = 0;
1343*77c1e3ccSAndroid Build Coastguard Worker       ppi->summed_quality_hbd = 0;
1344*77c1e3ccSAndroid Build Coastguard Worker       ppi->summed_weights_hbd = 0;
1345*77c1e3ccSAndroid Build Coastguard Worker     }
1346*77c1e3ccSAndroid Build Coastguard Worker 
1347*77c1e3ccSAndroid Build Coastguard Worker     ppi->fastssim.worst = 100.0;
1348*77c1e3ccSAndroid Build Coastguard Worker     ppi->psnrhvs.worst = 100.0;
1349*77c1e3ccSAndroid Build Coastguard Worker 
1350*77c1e3ccSAndroid Build Coastguard Worker     if (ppi->b_calculate_blockiness) {
1351*77c1e3ccSAndroid Build Coastguard Worker       ppi->total_blockiness = 0;
1352*77c1e3ccSAndroid Build Coastguard Worker       ppi->worst_blockiness = 0.0;
1353*77c1e3ccSAndroid Build Coastguard Worker     }
1354*77c1e3ccSAndroid Build Coastguard Worker 
1355*77c1e3ccSAndroid Build Coastguard Worker     ppi->total_inconsistency = 0;
1356*77c1e3ccSAndroid Build Coastguard Worker     ppi->worst_consistency = 100.0;
1357*77c1e3ccSAndroid Build Coastguard Worker     if (ppi->b_calculate_consistency) {
1358*77c1e3ccSAndroid Build Coastguard Worker       AOM_CHECK_MEM_ERROR(&ppi->error, ppi->ssim_vars,
1359*77c1e3ccSAndroid Build Coastguard Worker                           aom_malloc(sizeof(*ppi->ssim_vars) * 4 *
1360*77c1e3ccSAndroid Build Coastguard Worker                                      mi_params.mi_rows * mi_params.mi_cols));
1361*77c1e3ccSAndroid Build Coastguard Worker     }
1362*77c1e3ccSAndroid Build Coastguard Worker #endif
1363*77c1e3ccSAndroid Build Coastguard Worker   }
1364*77c1e3ccSAndroid Build Coastguard Worker 
1365*77c1e3ccSAndroid Build Coastguard Worker   ppi->error.setjmp = 0;
1366*77c1e3ccSAndroid Build Coastguard Worker   return ppi;
1367*77c1e3ccSAndroid Build Coastguard Worker }
1368*77c1e3ccSAndroid Build Coastguard Worker 
av1_create_compressor(AV1_PRIMARY * ppi,const AV1EncoderConfig * oxcf,BufferPool * const pool,COMPRESSOR_STAGE stage,int lap_lag_in_frames)1369*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *av1_create_compressor(AV1_PRIMARY *ppi, const AV1EncoderConfig *oxcf,
1370*77c1e3ccSAndroid Build Coastguard Worker                                 BufferPool *const pool, COMPRESSOR_STAGE stage,
1371*77c1e3ccSAndroid Build Coastguard Worker                                 int lap_lag_in_frames) {
1372*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
1373*77c1e3ccSAndroid Build Coastguard Worker 
1374*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi) return NULL;
1375*77c1e3ccSAndroid Build Coastguard Worker 
1376*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(*cpi);
1377*77c1e3ccSAndroid Build Coastguard Worker 
1378*77c1e3ccSAndroid Build Coastguard Worker   cpi->ppi = ppi;
1379*77c1e3ccSAndroid Build Coastguard Worker 
1380*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *volatile const cm = &cpi->common;
1381*77c1e3ccSAndroid Build Coastguard Worker   cm->seq_params = &ppi->seq_params;
1382*77c1e3ccSAndroid Build Coastguard Worker   cm->error =
1383*77c1e3ccSAndroid Build Coastguard Worker       (struct aom_internal_error_info *)aom_calloc(1, sizeof(*cm->error));
1384*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->error) {
1385*77c1e3ccSAndroid Build Coastguard Worker     aom_free(cpi);
1386*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
1387*77c1e3ccSAndroid Build Coastguard Worker   }
1388*77c1e3ccSAndroid Build Coastguard Worker 
1389*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
1390*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
1391*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
1392*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(cm->error->jmp)) {
1393*77c1e3ccSAndroid Build Coastguard Worker     cm->error->setjmp = 0;
1394*77c1e3ccSAndroid Build Coastguard Worker     av1_remove_compressor(cpi);
1395*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
1396*77c1e3ccSAndroid Build Coastguard Worker   }
1397*77c1e3ccSAndroid Build Coastguard Worker 
1398*77c1e3ccSAndroid Build Coastguard Worker   cm->error->setjmp = 1;
1399*77c1e3ccSAndroid Build Coastguard Worker   cpi->compressor_stage = stage;
1400*77c1e3ccSAndroid Build Coastguard Worker 
1401*77c1e3ccSAndroid Build Coastguard Worker   cpi->do_frame_data_update = true;
1402*77c1e3ccSAndroid Build Coastguard Worker 
1403*77c1e3ccSAndroid Build Coastguard Worker   CommonModeInfoParams *const mi_params = &cm->mi_params;
1404*77c1e3ccSAndroid Build Coastguard Worker   mi_params->free_mi = enc_free_mi;
1405*77c1e3ccSAndroid Build Coastguard Worker   mi_params->setup_mi = enc_setup_mi;
1406*77c1e3ccSAndroid Build Coastguard Worker   mi_params->set_mb_mi =
1407*77c1e3ccSAndroid Build Coastguard Worker       (oxcf->pass == AOM_RC_FIRST_PASS || cpi->compressor_stage == LAP_STAGE)
1408*77c1e3ccSAndroid Build Coastguard Worker           ? stat_stage_set_mb_mi
1409*77c1e3ccSAndroid Build Coastguard Worker           : enc_set_mb_mi;
1410*77c1e3ccSAndroid Build Coastguard Worker 
1411*77c1e3ccSAndroid Build Coastguard Worker   mi_params->mi_alloc_bsize = BLOCK_4X4;
1412*77c1e3ccSAndroid Build Coastguard Worker 
1413*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(cm, cm->fc,
1414*77c1e3ccSAndroid Build Coastguard Worker                   (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
1415*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(
1416*77c1e3ccSAndroid Build Coastguard Worker       cm, cm->default_frame_context,
1417*77c1e3ccSAndroid Build Coastguard Worker       (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
1418*77c1e3ccSAndroid Build Coastguard Worker   memset(cm->fc, 0, sizeof(*cm->fc));
1419*77c1e3ccSAndroid Build Coastguard Worker   memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
1420*77c1e3ccSAndroid Build Coastguard Worker 
1421*77c1e3ccSAndroid Build Coastguard Worker   cpi->common.buffer_pool = pool;
1422*77c1e3ccSAndroid Build Coastguard Worker 
1423*77c1e3ccSAndroid Build Coastguard Worker   init_config(cpi, oxcf);
1424*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->compressor_stage == LAP_STAGE) {
1425*77c1e3ccSAndroid Build Coastguard Worker     cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
1426*77c1e3ccSAndroid Build Coastguard Worker   }
1427*77c1e3ccSAndroid Build Coastguard Worker 
1428*77c1e3ccSAndroid Build Coastguard Worker   av1_rc_init(&cpi->oxcf, &cpi->rc);
1429*77c1e3ccSAndroid Build Coastguard Worker 
1430*77c1e3ccSAndroid Build Coastguard Worker   init_frame_info(&cpi->frame_info, cm);
1431*77c1e3ccSAndroid Build Coastguard Worker   init_frame_index_set(&cpi->frame_index_set);
1432*77c1e3ccSAndroid Build Coastguard Worker 
1433*77c1e3ccSAndroid Build Coastguard Worker   cm->current_frame.frame_number = 0;
1434*77c1e3ccSAndroid Build Coastguard Worker   cpi->rc.frame_number_encoded = 0;
1435*77c1e3ccSAndroid Build Coastguard Worker   cpi->rc.prev_frame_is_dropped = 0;
1436*77c1e3ccSAndroid Build Coastguard Worker   cpi->rc.max_consec_drop = INT_MAX;
1437*77c1e3ccSAndroid Build Coastguard Worker   cpi->rc.drop_count_consec = 0;
1438*77c1e3ccSAndroid Build Coastguard Worker   cm->current_frame_id = -1;
1439*77c1e3ccSAndroid Build Coastguard Worker   cpi->tile_data = NULL;
1440*77c1e3ccSAndroid Build Coastguard Worker   cpi->last_show_frame_buf = NULL;
1441*77c1e3ccSAndroid Build Coastguard Worker   realloc_segmentation_maps(cpi);
1442*77c1e3ccSAndroid Build Coastguard Worker 
1443*77c1e3ccSAndroid Build Coastguard Worker   cpi->refresh_frame.alt_ref_frame = false;
1444*77c1e3ccSAndroid Build Coastguard Worker 
1445*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_SPEED_STATS
1446*77c1e3ccSAndroid Build Coastguard Worker   cpi->tx_search_count = 0;
1447*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_SPEED_STATS
1448*77c1e3ccSAndroid Build Coastguard Worker 
1449*77c1e3ccSAndroid Build Coastguard Worker   cpi->time_stamps.first_ts_start = INT64_MAX;
1450*77c1e3ccSAndroid Build Coastguard Worker 
1451*77c1e3ccSAndroid Build Coastguard Worker #ifdef OUTPUT_YUV_REC
1452*77c1e3ccSAndroid Build Coastguard Worker   yuv_rec_file = fopen("rec.yuv", "wb");
1453*77c1e3ccSAndroid Build Coastguard Worker #endif
1454*77c1e3ccSAndroid Build Coastguard Worker #ifdef OUTPUT_YUV_DENOISED
1455*77c1e3ccSAndroid Build Coastguard Worker   yuv_denoised_file = fopen("denoised.yuv", "wb");
1456*77c1e3ccSAndroid Build Coastguard Worker #endif
1457*77c1e3ccSAndroid Build Coastguard Worker 
1458*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1459*77c1e3ccSAndroid Build Coastguard Worker   if (is_stat_consumption_stage(cpi)) {
1460*77c1e3ccSAndroid Build Coastguard Worker     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1461*77c1e3ccSAndroid Build Coastguard Worker     const int packets = (int)(oxcf->twopass_stats_in.sz / packet_sz);
1462*77c1e3ccSAndroid Build Coastguard Worker 
1463*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->ppi->lap_enabled) {
1464*77c1e3ccSAndroid Build Coastguard Worker       /*Re-initialize to stats buffer, populated by application in the case of
1465*77c1e3ccSAndroid Build Coastguard Worker        * two pass*/
1466*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->twopass.stats_buf_ctx->stats_in_start =
1467*77c1e3ccSAndroid Build Coastguard Worker           oxcf->twopass_stats_in.buf;
1468*77c1e3ccSAndroid Build Coastguard Worker       cpi->twopass_frame.stats_in =
1469*77c1e3ccSAndroid Build Coastguard Worker           cpi->ppi->twopass.stats_buf_ctx->stats_in_start;
1470*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->twopass.stats_buf_ctx->stats_in_end =
1471*77c1e3ccSAndroid Build Coastguard Worker           &cpi->ppi->twopass.stats_buf_ctx->stats_in_start[packets - 1];
1472*77c1e3ccSAndroid Build Coastguard Worker 
1473*77c1e3ccSAndroid Build Coastguard Worker       // The buffer size is packets - 1 because the last packet is total_stats.
1474*77c1e3ccSAndroid Build Coastguard Worker       av1_firstpass_info_init(&cpi->ppi->twopass.firstpass_info,
1475*77c1e3ccSAndroid Build Coastguard Worker                               oxcf->twopass_stats_in.buf, packets - 1);
1476*77c1e3ccSAndroid Build Coastguard Worker       av1_init_second_pass(cpi);
1477*77c1e3ccSAndroid Build Coastguard Worker     } else {
1478*77c1e3ccSAndroid Build Coastguard Worker       av1_firstpass_info_init(&cpi->ppi->twopass.firstpass_info, NULL, 0);
1479*77c1e3ccSAndroid Build Coastguard Worker       av1_init_single_pass_lap(cpi);
1480*77c1e3ccSAndroid Build Coastguard Worker     }
1481*77c1e3ccSAndroid Build Coastguard Worker   }
1482*77c1e3ccSAndroid Build Coastguard Worker #endif
1483*77c1e3ccSAndroid Build Coastguard Worker 
1484*77c1e3ccSAndroid Build Coastguard Worker   // The buffer "obmc_buffer" is used in inter frames for fast obmc search.
1485*77c1e3ccSAndroid Build Coastguard Worker   // Hence, the memory allocation for the same is avoided for allintra encoding
1486*77c1e3ccSAndroid Build Coastguard Worker   // mode.
1487*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.kf_cfg.key_freq_max != 0)
1488*77c1e3ccSAndroid Build Coastguard Worker     alloc_obmc_buffers(&cpi->td.mb.obmc_buffer, cm->error);
1489*77c1e3ccSAndroid Build Coastguard Worker 
1490*77c1e3ccSAndroid Build Coastguard Worker   for (int x = 0; x < 2; x++)
1491*77c1e3ccSAndroid Build Coastguard Worker     for (int y = 0; y < 2; y++)
1492*77c1e3ccSAndroid Build Coastguard Worker       CHECK_MEM_ERROR(
1493*77c1e3ccSAndroid Build Coastguard Worker           cm, cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
1494*77c1e3ccSAndroid Build Coastguard Worker           (uint32_t *)aom_malloc(
1495*77c1e3ccSAndroid Build Coastguard Worker               AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
1496*77c1e3ccSAndroid Build Coastguard Worker               sizeof(*cpi->td.mb.intrabc_hash_info.hash_value_buffer[0][0])));
1497*77c1e3ccSAndroid Build Coastguard Worker 
1498*77c1e3ccSAndroid Build Coastguard Worker   cpi->td.mb.intrabc_hash_info.g_crc_initialized = 0;
1499*77c1e3ccSAndroid Build Coastguard Worker 
1500*77c1e3ccSAndroid Build Coastguard Worker   av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
1501*77c1e3ccSAndroid Build Coastguard Worker   av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
1502*77c1e3ccSAndroid Build Coastguard Worker 
1503*77c1e3ccSAndroid Build Coastguard Worker   int max_mi_cols = mi_params->mi_cols;
1504*77c1e3ccSAndroid Build Coastguard Worker   int max_mi_rows = mi_params->mi_rows;
1505*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->frm_dim_cfg.forced_max_frame_width) {
1506*77c1e3ccSAndroid Build Coastguard Worker     max_mi_cols = size_in_mi(oxcf->frm_dim_cfg.forced_max_frame_width);
1507*77c1e3ccSAndroid Build Coastguard Worker   }
1508*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->frm_dim_cfg.forced_max_frame_height) {
1509*77c1e3ccSAndroid Build Coastguard Worker     max_mi_rows = size_in_mi(oxcf->frm_dim_cfg.forced_max_frame_height);
1510*77c1e3ccSAndroid Build Coastguard Worker   }
1511*77c1e3ccSAndroid Build Coastguard Worker 
1512*77c1e3ccSAndroid Build Coastguard Worker   const int consec_zero_mv_alloc_size = (max_mi_rows * max_mi_cols) >> 2;
1513*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(
1514*77c1e3ccSAndroid Build Coastguard Worker       cm, cpi->consec_zero_mv,
1515*77c1e3ccSAndroid Build Coastguard Worker       aom_calloc(consec_zero_mv_alloc_size, sizeof(*cpi->consec_zero_mv)));
1516*77c1e3ccSAndroid Build Coastguard Worker   cpi->consec_zero_mv_alloc_size = consec_zero_mv_alloc_size;
1517*77c1e3ccSAndroid Build Coastguard Worker 
1518*77c1e3ccSAndroid Build Coastguard Worker   cpi->mb_weber_stats = NULL;
1519*77c1e3ccSAndroid Build Coastguard Worker   cpi->mb_delta_q = NULL;
1520*77c1e3ccSAndroid Build Coastguard Worker   cpi->palette_pixel_num = 0;
1521*77c1e3ccSAndroid Build Coastguard Worker   cpi->scaled_last_source_available = 0;
1522*77c1e3ccSAndroid Build Coastguard Worker 
1523*77c1e3ccSAndroid Build Coastguard Worker   {
1524*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE bsize = BLOCK_16X16;
1525*77c1e3ccSAndroid Build Coastguard Worker     const int w = mi_size_wide[bsize];
1526*77c1e3ccSAndroid Build Coastguard Worker     const int h = mi_size_high[bsize];
1527*77c1e3ccSAndroid Build Coastguard Worker     const int num_cols = (max_mi_cols + w - 1) / w;
1528*77c1e3ccSAndroid Build Coastguard Worker     const int num_rows = (max_mi_rows + h - 1) / h;
1529*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
1530*77c1e3ccSAndroid Build Coastguard Worker                     aom_calloc(num_rows * num_cols,
1531*77c1e3ccSAndroid Build Coastguard Worker                                sizeof(*cpi->ssim_rdmult_scaling_factors)));
1532*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
1533*77c1e3ccSAndroid Build Coastguard Worker                     aom_calloc(num_rows * num_cols,
1534*77c1e3ccSAndroid Build Coastguard Worker                                sizeof(*cpi->tpl_rdmult_scaling_factors)));
1535*77c1e3ccSAndroid Build Coastguard Worker   }
1536*77c1e3ccSAndroid Build Coastguard Worker 
1537*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
1538*77c1e3ccSAndroid Build Coastguard Worker   {
1539*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE bsize = BLOCK_64X64;
1540*77c1e3ccSAndroid Build Coastguard Worker     const int w = mi_size_wide[bsize];
1541*77c1e3ccSAndroid Build Coastguard Worker     const int h = mi_size_high[bsize];
1542*77c1e3ccSAndroid Build Coastguard Worker     const int num_cols = (mi_params->mi_cols + w - 1) / w;
1543*77c1e3ccSAndroid Build Coastguard Worker     const int num_rows = (mi_params->mi_rows + h - 1) / h;
1544*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(cm, cpi->vmaf_info.rdmult_scaling_factors,
1545*77c1e3ccSAndroid Build Coastguard Worker                     aom_calloc(num_rows * num_cols,
1546*77c1e3ccSAndroid Build Coastguard Worker                                sizeof(*cpi->vmaf_info.rdmult_scaling_factors)));
1547*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < MAX_ARF_LAYERS; i++) {
1548*77c1e3ccSAndroid Build Coastguard Worker       cpi->vmaf_info.last_frame_unsharp_amount[i] = -1.0;
1549*77c1e3ccSAndroid Build Coastguard Worker       cpi->vmaf_info.last_frame_ysse[i] = -1.0;
1550*77c1e3ccSAndroid Build Coastguard Worker       cpi->vmaf_info.last_frame_vmaf[i] = -1.0;
1551*77c1e3ccSAndroid Build Coastguard Worker     }
1552*77c1e3ccSAndroid Build Coastguard Worker     cpi->vmaf_info.original_qindex = -1;
1553*77c1e3ccSAndroid Build Coastguard Worker     cpi->vmaf_info.vmaf_model = NULL;
1554*77c1e3ccSAndroid Build Coastguard Worker   }
1555*77c1e3ccSAndroid Build Coastguard Worker #endif
1556*77c1e3ccSAndroid Build Coastguard Worker 
1557*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_BUTTERAUGLI
1558*77c1e3ccSAndroid Build Coastguard Worker   {
1559*77c1e3ccSAndroid Build Coastguard Worker     const int w = mi_size_wide[butteraugli_rdo_bsize];
1560*77c1e3ccSAndroid Build Coastguard Worker     const int h = mi_size_high[butteraugli_rdo_bsize];
1561*77c1e3ccSAndroid Build Coastguard Worker     const int num_cols = (mi_params->mi_cols + w - 1) / w;
1562*77c1e3ccSAndroid Build Coastguard Worker     const int num_rows = (mi_params->mi_rows + h - 1) / h;
1563*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(
1564*77c1e3ccSAndroid Build Coastguard Worker         cm, cpi->butteraugli_info.rdmult_scaling_factors,
1565*77c1e3ccSAndroid Build Coastguard Worker         aom_malloc(num_rows * num_cols *
1566*77c1e3ccSAndroid Build Coastguard Worker                    sizeof(*cpi->butteraugli_info.rdmult_scaling_factors)));
1567*77c1e3ccSAndroid Build Coastguard Worker     memset(&cpi->butteraugli_info.source, 0,
1568*77c1e3ccSAndroid Build Coastguard Worker            sizeof(cpi->butteraugli_info.source));
1569*77c1e3ccSAndroid Build Coastguard Worker     memset(&cpi->butteraugli_info.resized_source, 0,
1570*77c1e3ccSAndroid Build Coastguard Worker            sizeof(cpi->butteraugli_info.resized_source));
1571*77c1e3ccSAndroid Build Coastguard Worker     cpi->butteraugli_info.recon_set = false;
1572*77c1e3ccSAndroid Build Coastguard Worker   }
1573*77c1e3ccSAndroid Build Coastguard Worker #endif
1574*77c1e3ccSAndroid Build Coastguard Worker 
1575*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_SALIENCY_MAP
1576*77c1e3ccSAndroid Build Coastguard Worker   {
1577*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(cm, cpi->saliency_map,
1578*77c1e3ccSAndroid Build Coastguard Worker                     (uint8_t *)aom_calloc(cm->height * cm->width,
1579*77c1e3ccSAndroid Build Coastguard Worker                                           sizeof(*cpi->saliency_map)));
1580*77c1e3ccSAndroid Build Coastguard Worker     // Buffer initialization based on MIN_MIB_SIZE_LOG2 to ensure that
1581*77c1e3ccSAndroid Build Coastguard Worker     // cpi->sm_scaling_factor buffer is allocated big enough, since we have no
1582*77c1e3ccSAndroid Build Coastguard Worker     // idea of the actual superblock size we are going to use yet.
1583*77c1e3ccSAndroid Build Coastguard Worker     const int min_mi_w_sb = (1 << MIN_MIB_SIZE_LOG2);
1584*77c1e3ccSAndroid Build Coastguard Worker     const int min_mi_h_sb = (1 << MIN_MIB_SIZE_LOG2);
1585*77c1e3ccSAndroid Build Coastguard Worker     const int max_sb_cols =
1586*77c1e3ccSAndroid Build Coastguard Worker         (cm->mi_params.mi_cols + min_mi_w_sb - 1) / min_mi_w_sb;
1587*77c1e3ccSAndroid Build Coastguard Worker     const int max_sb_rows =
1588*77c1e3ccSAndroid Build Coastguard Worker         (cm->mi_params.mi_rows + min_mi_h_sb - 1) / min_mi_h_sb;
1589*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(cm, cpi->sm_scaling_factor,
1590*77c1e3ccSAndroid Build Coastguard Worker                     (double *)aom_calloc(max_sb_rows * max_sb_cols,
1591*77c1e3ccSAndroid Build Coastguard Worker                                          sizeof(*cpi->sm_scaling_factor)));
1592*77c1e3ccSAndroid Build Coastguard Worker   }
1593*77c1e3ccSAndroid Build Coastguard Worker #endif
1594*77c1e3ccSAndroid Build Coastguard Worker 
1595*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS
1596*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(cpi->partition_stats);
1597*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_COLLECT_PARTITION_STATS
1598*77c1e3ccSAndroid Build Coastguard Worker 
1599*77c1e3ccSAndroid Build Coastguard Worker   // Initialize the members of DeltaQuantParams with INT_MAX to ensure that
1600*77c1e3ccSAndroid Build Coastguard Worker   // the quantizer tables are correctly initialized using the default deltaq
1601*77c1e3ccSAndroid Build Coastguard Worker   // parameters when av1_init_quantizer is called for the first time.
1602*77c1e3ccSAndroid Build Coastguard Worker   DeltaQuantParams *const prev_deltaq_params =
1603*77c1e3ccSAndroid Build Coastguard Worker       &cpi->enc_quant_dequant_params.prev_deltaq_params;
1604*77c1e3ccSAndroid Build Coastguard Worker   prev_deltaq_params->y_dc_delta_q = INT_MAX;
1605*77c1e3ccSAndroid Build Coastguard Worker   prev_deltaq_params->u_dc_delta_q = INT_MAX;
1606*77c1e3ccSAndroid Build Coastguard Worker   prev_deltaq_params->v_dc_delta_q = INT_MAX;
1607*77c1e3ccSAndroid Build Coastguard Worker   prev_deltaq_params->u_ac_delta_q = INT_MAX;
1608*77c1e3ccSAndroid Build Coastguard Worker   prev_deltaq_params->v_ac_delta_q = INT_MAX;
1609*77c1e3ccSAndroid Build Coastguard Worker 
1610*77c1e3ccSAndroid Build Coastguard Worker   av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
1611*77c1e3ccSAndroid Build Coastguard Worker                      cm->seq_params->bit_depth);
1612*77c1e3ccSAndroid Build Coastguard Worker   av1_qm_init(&cm->quant_params, av1_num_planes(cm));
1613*77c1e3ccSAndroid Build Coastguard Worker 
1614*77c1e3ccSAndroid Build Coastguard Worker   av1_loop_filter_init(cm);
1615*77c1e3ccSAndroid Build Coastguard Worker   cm->superres_scale_denominator = SCALE_NUMERATOR;
1616*77c1e3ccSAndroid Build Coastguard Worker   cm->superres_upscaled_width = oxcf->frm_dim_cfg.width;
1617*77c1e3ccSAndroid Build Coastguard Worker   cm->superres_upscaled_height = oxcf->frm_dim_cfg.height;
1618*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1619*77c1e3ccSAndroid Build Coastguard Worker   av1_loop_restoration_precal();
1620*77c1e3ccSAndroid Build Coastguard Worker #endif
1621*77c1e3ccSAndroid Build Coastguard Worker 
1622*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS
1623*77c1e3ccSAndroid Build Coastguard Worker   cpi->third_pass_ctx = NULL;
1624*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.pass == AOM_RC_THIRD_PASS) {
1625*77c1e3ccSAndroid Build Coastguard Worker     av1_init_thirdpass_ctx(cm, &cpi->third_pass_ctx, NULL);
1626*77c1e3ccSAndroid Build Coastguard Worker   }
1627*77c1e3ccSAndroid Build Coastguard Worker #endif
1628*77c1e3ccSAndroid Build Coastguard Worker 
1629*77c1e3ccSAndroid Build Coastguard Worker   cpi->second_pass_log_stream = NULL;
1630*77c1e3ccSAndroid Build Coastguard Worker   cpi->use_ducky_encode = 0;
1631*77c1e3ccSAndroid Build Coastguard Worker 
1632*77c1e3ccSAndroid Build Coastguard Worker   cm->error->setjmp = 0;
1633*77c1e3ccSAndroid Build Coastguard Worker   return cpi;
1634*77c1e3ccSAndroid Build Coastguard Worker }
1635*77c1e3ccSAndroid Build Coastguard Worker 
1636*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
1637*77c1e3ccSAndroid Build Coastguard Worker #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
1638*77c1e3ccSAndroid Build Coastguard Worker 
1639*77c1e3ccSAndroid Build Coastguard Worker #define SNPRINT2(H, T, V) \
1640*77c1e3ccSAndroid Build Coastguard Worker   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
1641*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_INTERNAL_STATS
1642*77c1e3ccSAndroid Build Coastguard Worker 
av1_remove_primary_compressor(AV1_PRIMARY * ppi)1643*77c1e3ccSAndroid Build Coastguard Worker void av1_remove_primary_compressor(AV1_PRIMARY *ppi) {
1644*77c1e3ccSAndroid Build Coastguard Worker   if (!ppi) return;
1645*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1646*77c1e3ccSAndroid Build Coastguard Worker   av1_tf_info_free(&ppi->tf_info);
1647*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
1648*77c1e3ccSAndroid Build Coastguard Worker 
1649*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
1650*77c1e3ccSAndroid Build Coastguard Worker     aom_free(ppi->level_params.level_info[i]);
1651*77c1e3ccSAndroid Build Coastguard Worker   }
1652*77c1e3ccSAndroid Build Coastguard Worker   av1_lookahead_destroy(ppi->lookahead);
1653*77c1e3ccSAndroid Build Coastguard Worker 
1654*77c1e3ccSAndroid Build Coastguard Worker   aom_free(ppi->tpl_sb_rdmult_scaling_factors);
1655*77c1e3ccSAndroid Build Coastguard Worker   ppi->tpl_sb_rdmult_scaling_factors = NULL;
1656*77c1e3ccSAndroid Build Coastguard Worker 
1657*77c1e3ccSAndroid Build Coastguard Worker   TplParams *const tpl_data = &ppi->tpl_data;
1658*77c1e3ccSAndroid Build Coastguard Worker   aom_free(tpl_data->txfm_stats_list);
1659*77c1e3ccSAndroid Build Coastguard Worker 
1660*77c1e3ccSAndroid Build Coastguard Worker   for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
1661*77c1e3ccSAndroid Build Coastguard Worker     aom_free(tpl_data->tpl_stats_pool[frame]);
1662*77c1e3ccSAndroid Build Coastguard Worker     aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]);
1663*77c1e3ccSAndroid Build Coastguard Worker     tpl_data->tpl_stats_pool[frame] = NULL;
1664*77c1e3ccSAndroid Build Coastguard Worker   }
1665*77c1e3ccSAndroid Build Coastguard Worker 
1666*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1667*77c1e3ccSAndroid Build Coastguard Worker   av1_tpl_dealloc(&tpl_data->tpl_mt_sync);
1668*77c1e3ccSAndroid Build Coastguard Worker #endif
1669*77c1e3ccSAndroid Build Coastguard Worker 
1670*77c1e3ccSAndroid Build Coastguard Worker   av1_terminate_workers(ppi);
1671*77c1e3ccSAndroid Build Coastguard Worker   free_thread_data(ppi);
1672*77c1e3ccSAndroid Build Coastguard Worker 
1673*77c1e3ccSAndroid Build Coastguard Worker   aom_free(ppi->p_mt_info.tile_thr_data);
1674*77c1e3ccSAndroid Build Coastguard Worker   ppi->p_mt_info.tile_thr_data = NULL;
1675*77c1e3ccSAndroid Build Coastguard Worker   aom_free(ppi->p_mt_info.workers);
1676*77c1e3ccSAndroid Build Coastguard Worker   ppi->p_mt_info.workers = NULL;
1677*77c1e3ccSAndroid Build Coastguard Worker   ppi->p_mt_info.num_workers = 0;
1678*77c1e3ccSAndroid Build Coastguard Worker 
1679*77c1e3ccSAndroid Build Coastguard Worker   aom_free(ppi);
1680*77c1e3ccSAndroid Build Coastguard Worker }
1681*77c1e3ccSAndroid Build Coastguard Worker 
av1_remove_compressor(AV1_COMP * cpi)1682*77c1e3ccSAndroid Build Coastguard Worker void av1_remove_compressor(AV1_COMP *cpi) {
1683*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi) return;
1684*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RATECTRL_LOG
1685*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.pass == 3) {
1686*77c1e3ccSAndroid Build Coastguard Worker     rc_log_show(&cpi->rc_log);
1687*77c1e3ccSAndroid Build Coastguard Worker   }
1688*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_RATECTRL_LOG
1689*77c1e3ccSAndroid Build Coastguard Worker 
1690*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
1691*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.frame_number > 0) {
1692*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_SPEED_STATS
1693*77c1e3ccSAndroid Build Coastguard Worker     if (!is_stat_generation_stage(cpi)) {
1694*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
1695*77c1e3ccSAndroid Build Coastguard Worker     }
1696*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_SPEED_STATS
1697*77c1e3ccSAndroid Build Coastguard Worker 
1698*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_PARTITION_STATS == 2
1699*77c1e3ccSAndroid Build Coastguard Worker     if (!is_stat_generation_stage(cpi)) {
1700*77c1e3ccSAndroid Build Coastguard Worker       av1_print_fr_partition_timing_stats(&cpi->partition_stats,
1701*77c1e3ccSAndroid Build Coastguard Worker                                           "fr_part_timing_data.csv");
1702*77c1e3ccSAndroid Build Coastguard Worker     }
1703*77c1e3ccSAndroid Build Coastguard Worker #endif
1704*77c1e3ccSAndroid Build Coastguard Worker   }
1705*77c1e3ccSAndroid Build Coastguard Worker 
1706*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
1707*77c1e3ccSAndroid Build Coastguard Worker   av1_denoiser_free(&(cpi->denoiser));
1708*77c1e3ccSAndroid Build Coastguard Worker #endif
1709*77c1e3ccSAndroid Build Coastguard Worker 
1710*77c1e3ccSAndroid Build Coastguard Worker   if (cm->error) {
1711*77c1e3ccSAndroid Build Coastguard Worker     // Help detect use after free of the error detail string.
1712*77c1e3ccSAndroid Build Coastguard Worker     memset(cm->error->detail, 'A', sizeof(cm->error->detail) - 1);
1713*77c1e3ccSAndroid Build Coastguard Worker     cm->error->detail[sizeof(cm->error->detail) - 1] = '\0';
1714*77c1e3ccSAndroid Build Coastguard Worker     aom_free(cm->error);
1715*77c1e3ccSAndroid Build Coastguard Worker   }
1716*77c1e3ccSAndroid Build Coastguard Worker   aom_free(cpi->td.tctx);
1717*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
1718*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
1719*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *const enc_row_mt_mutex_ = mt_info->enc_row_mt.mutex_;
1720*77c1e3ccSAndroid Build Coastguard Worker   pthread_cond_t *const enc_row_mt_cond_ = mt_info->enc_row_mt.cond_;
1721*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *const gm_mt_mutex_ = mt_info->gm_sync.mutex_;
1722*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *const tpl_error_mutex_ = mt_info->tpl_row_mt.mutex_;
1723*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *const pack_bs_mt_mutex_ = mt_info->pack_bs_sync.mutex_;
1724*77c1e3ccSAndroid Build Coastguard Worker   if (enc_row_mt_mutex_ != NULL) {
1725*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_destroy(enc_row_mt_mutex_);
1726*77c1e3ccSAndroid Build Coastguard Worker     aom_free(enc_row_mt_mutex_);
1727*77c1e3ccSAndroid Build Coastguard Worker   }
1728*77c1e3ccSAndroid Build Coastguard Worker   if (enc_row_mt_cond_ != NULL) {
1729*77c1e3ccSAndroid Build Coastguard Worker     pthread_cond_destroy(enc_row_mt_cond_);
1730*77c1e3ccSAndroid Build Coastguard Worker     aom_free(enc_row_mt_cond_);
1731*77c1e3ccSAndroid Build Coastguard Worker   }
1732*77c1e3ccSAndroid Build Coastguard Worker   if (gm_mt_mutex_ != NULL) {
1733*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_destroy(gm_mt_mutex_);
1734*77c1e3ccSAndroid Build Coastguard Worker     aom_free(gm_mt_mutex_);
1735*77c1e3ccSAndroid Build Coastguard Worker   }
1736*77c1e3ccSAndroid Build Coastguard Worker   if (tpl_error_mutex_ != NULL) {
1737*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_destroy(tpl_error_mutex_);
1738*77c1e3ccSAndroid Build Coastguard Worker     aom_free(tpl_error_mutex_);
1739*77c1e3ccSAndroid Build Coastguard Worker   }
1740*77c1e3ccSAndroid Build Coastguard Worker   if (pack_bs_mt_mutex_ != NULL) {
1741*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_destroy(pack_bs_mt_mutex_);
1742*77c1e3ccSAndroid Build Coastguard Worker     aom_free(pack_bs_mt_mutex_);
1743*77c1e3ccSAndroid Build Coastguard Worker   }
1744*77c1e3ccSAndroid Build Coastguard Worker #endif
1745*77c1e3ccSAndroid Build Coastguard Worker   av1_row_mt_mem_dealloc(cpi);
1746*77c1e3ccSAndroid Build Coastguard Worker 
1747*77c1e3ccSAndroid Build Coastguard Worker   if (mt_info->num_workers > 1) {
1748*77c1e3ccSAndroid Build Coastguard Worker     av1_row_mt_sync_mem_dealloc(&cpi->ppi->intra_row_mt_sync);
1749*77c1e3ccSAndroid Build Coastguard Worker     av1_loop_filter_dealloc(&mt_info->lf_row_sync);
1750*77c1e3ccSAndroid Build Coastguard Worker     av1_cdef_mt_dealloc(&mt_info->cdef_sync);
1751*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1752*77c1e3ccSAndroid Build Coastguard Worker     av1_loop_restoration_dealloc(&mt_info->lr_row_sync);
1753*77c1e3ccSAndroid Build Coastguard Worker     av1_tf_mt_dealloc(&mt_info->tf_sync);
1754*77c1e3ccSAndroid Build Coastguard Worker #endif
1755*77c1e3ccSAndroid Build Coastguard Worker   }
1756*77c1e3ccSAndroid Build Coastguard Worker 
1757*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS
1758*77c1e3ccSAndroid Build Coastguard Worker   av1_free_thirdpass_ctx(cpi->third_pass_ctx);
1759*77c1e3ccSAndroid Build Coastguard Worker 
1760*77c1e3ccSAndroid Build Coastguard Worker   av1_close_second_pass_log(cpi);
1761*77c1e3ccSAndroid Build Coastguard Worker #endif
1762*77c1e3ccSAndroid Build Coastguard Worker 
1763*77c1e3ccSAndroid Build Coastguard Worker   dealloc_compressor_data(cpi);
1764*77c1e3ccSAndroid Build Coastguard Worker 
1765*77c1e3ccSAndroid Build Coastguard Worker   av1_ext_part_delete(&cpi->ext_part_controller);
1766*77c1e3ccSAndroid Build Coastguard Worker 
1767*77c1e3ccSAndroid Build Coastguard Worker   av1_remove_common(cm);
1768*77c1e3ccSAndroid Build Coastguard Worker 
1769*77c1e3ccSAndroid Build Coastguard Worker   aom_free(cpi);
1770*77c1e3ccSAndroid Build Coastguard Worker 
1771*77c1e3ccSAndroid Build Coastguard Worker #ifdef OUTPUT_YUV_REC
1772*77c1e3ccSAndroid Build Coastguard Worker   fclose(yuv_rec_file);
1773*77c1e3ccSAndroid Build Coastguard Worker #endif
1774*77c1e3ccSAndroid Build Coastguard Worker 
1775*77c1e3ccSAndroid Build Coastguard Worker #ifdef OUTPUT_YUV_DENOISED
1776*77c1e3ccSAndroid Build Coastguard Worker   fclose(yuv_denoised_file);
1777*77c1e3ccSAndroid Build Coastguard Worker #endif
1778*77c1e3ccSAndroid Build Coastguard Worker }
1779*77c1e3ccSAndroid Build Coastguard Worker 
generate_psnr_packet(AV1_COMP * cpi)1780*77c1e3ccSAndroid Build Coastguard Worker static void generate_psnr_packet(AV1_COMP *cpi) {
1781*77c1e3ccSAndroid Build Coastguard Worker   struct aom_codec_cx_pkt pkt;
1782*77c1e3ccSAndroid Build Coastguard Worker   int i;
1783*77c1e3ccSAndroid Build Coastguard Worker   PSNR_STATS psnr;
1784*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
1785*77c1e3ccSAndroid Build Coastguard Worker   const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
1786*77c1e3ccSAndroid Build Coastguard Worker   const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
1787*77c1e3ccSAndroid Build Coastguard Worker   aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
1788*77c1e3ccSAndroid Build Coastguard Worker                        bit_depth, in_bit_depth);
1789*77c1e3ccSAndroid Build Coastguard Worker #else
1790*77c1e3ccSAndroid Build Coastguard Worker   aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
1791*77c1e3ccSAndroid Build Coastguard Worker #endif
1792*77c1e3ccSAndroid Build Coastguard Worker 
1793*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < 4; ++i) {
1794*77c1e3ccSAndroid Build Coastguard Worker     pkt.data.psnr.samples[i] = psnr.samples[i];
1795*77c1e3ccSAndroid Build Coastguard Worker     pkt.data.psnr.sse[i] = psnr.sse[i];
1796*77c1e3ccSAndroid Build Coastguard Worker     pkt.data.psnr.psnr[i] = psnr.psnr[i];
1797*77c1e3ccSAndroid Build Coastguard Worker   }
1798*77c1e3ccSAndroid Build Coastguard Worker 
1799*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
1800*77c1e3ccSAndroid Build Coastguard Worker   if ((cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) &&
1801*77c1e3ccSAndroid Build Coastguard Worker       (in_bit_depth < bit_depth)) {
1802*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < 4; ++i) {
1803*77c1e3ccSAndroid Build Coastguard Worker       pkt.data.psnr.samples_hbd[i] = psnr.samples_hbd[i];
1804*77c1e3ccSAndroid Build Coastguard Worker       pkt.data.psnr.sse_hbd[i] = psnr.sse_hbd[i];
1805*77c1e3ccSAndroid Build Coastguard Worker       pkt.data.psnr.psnr_hbd[i] = psnr.psnr_hbd[i];
1806*77c1e3ccSAndroid Build Coastguard Worker     }
1807*77c1e3ccSAndroid Build Coastguard Worker   }
1808*77c1e3ccSAndroid Build Coastguard Worker #endif
1809*77c1e3ccSAndroid Build Coastguard Worker 
1810*77c1e3ccSAndroid Build Coastguard Worker   pkt.kind = AOM_CODEC_PSNR_PKT;
1811*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_pkt_list_add(cpi->ppi->output_pkt_list, &pkt);
1812*77c1e3ccSAndroid Build Coastguard Worker }
1813*77c1e3ccSAndroid Build Coastguard Worker 
av1_use_as_reference(int * ext_ref_frame_flags,int ref_frame_flags)1814*77c1e3ccSAndroid Build Coastguard Worker int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags) {
1815*77c1e3ccSAndroid Build Coastguard Worker   if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
1816*77c1e3ccSAndroid Build Coastguard Worker 
1817*77c1e3ccSAndroid Build Coastguard Worker   *ext_ref_frame_flags = ref_frame_flags;
1818*77c1e3ccSAndroid Build Coastguard Worker   return 0;
1819*77c1e3ccSAndroid Build Coastguard Worker }
1820*77c1e3ccSAndroid Build Coastguard Worker 
av1_copy_reference_enc(AV1_COMP * cpi,int idx,YV12_BUFFER_CONFIG * sd)1821*77c1e3ccSAndroid Build Coastguard Worker int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1822*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1823*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
1824*77c1e3ccSAndroid Build Coastguard Worker   YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
1825*77c1e3ccSAndroid Build Coastguard Worker   if (cfg) {
1826*77c1e3ccSAndroid Build Coastguard Worker     aom_yv12_copy_frame(cfg, sd, num_planes);
1827*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1828*77c1e3ccSAndroid Build Coastguard Worker   } else {
1829*77c1e3ccSAndroid Build Coastguard Worker     return -1;
1830*77c1e3ccSAndroid Build Coastguard Worker   }
1831*77c1e3ccSAndroid Build Coastguard Worker }
1832*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_reference_enc(AV1_COMP * cpi,int idx,YV12_BUFFER_CONFIG * sd)1833*77c1e3ccSAndroid Build Coastguard Worker int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1834*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1835*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
1836*77c1e3ccSAndroid Build Coastguard Worker   YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
1837*77c1e3ccSAndroid Build Coastguard Worker   if (cfg) {
1838*77c1e3ccSAndroid Build Coastguard Worker     aom_yv12_copy_frame(sd, cfg, num_planes);
1839*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1840*77c1e3ccSAndroid Build Coastguard Worker   } else {
1841*77c1e3ccSAndroid Build Coastguard Worker     return -1;
1842*77c1e3ccSAndroid Build Coastguard Worker   }
1843*77c1e3ccSAndroid Build Coastguard Worker }
1844*77c1e3ccSAndroid Build Coastguard Worker 
1845*77c1e3ccSAndroid Build Coastguard Worker #ifdef OUTPUT_YUV_REC
aom_write_one_yuv_frame(AV1_COMMON * cm,YV12_BUFFER_CONFIG * s)1846*77c1e3ccSAndroid Build Coastguard Worker void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
1847*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *src = s->y_buffer;
1848*77c1e3ccSAndroid Build Coastguard Worker   int h = cm->height;
1849*77c1e3ccSAndroid Build Coastguard Worker   if (yuv_rec_file == NULL) return;
1850*77c1e3ccSAndroid Build Coastguard Worker   if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
1851*77c1e3ccSAndroid Build Coastguard Worker     uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
1852*77c1e3ccSAndroid Build Coastguard Worker 
1853*77c1e3ccSAndroid Build Coastguard Worker     do {
1854*77c1e3ccSAndroid Build Coastguard Worker       fwrite(src16, s->y_width, 2, yuv_rec_file);
1855*77c1e3ccSAndroid Build Coastguard Worker       src16 += s->y_stride;
1856*77c1e3ccSAndroid Build Coastguard Worker     } while (--h);
1857*77c1e3ccSAndroid Build Coastguard Worker 
1858*77c1e3ccSAndroid Build Coastguard Worker     src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
1859*77c1e3ccSAndroid Build Coastguard Worker     h = s->uv_height;
1860*77c1e3ccSAndroid Build Coastguard Worker 
1861*77c1e3ccSAndroid Build Coastguard Worker     do {
1862*77c1e3ccSAndroid Build Coastguard Worker       fwrite(src16, s->uv_width, 2, yuv_rec_file);
1863*77c1e3ccSAndroid Build Coastguard Worker       src16 += s->uv_stride;
1864*77c1e3ccSAndroid Build Coastguard Worker     } while (--h);
1865*77c1e3ccSAndroid Build Coastguard Worker 
1866*77c1e3ccSAndroid Build Coastguard Worker     src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
1867*77c1e3ccSAndroid Build Coastguard Worker     h = s->uv_height;
1868*77c1e3ccSAndroid Build Coastguard Worker 
1869*77c1e3ccSAndroid Build Coastguard Worker     do {
1870*77c1e3ccSAndroid Build Coastguard Worker       fwrite(src16, s->uv_width, 2, yuv_rec_file);
1871*77c1e3ccSAndroid Build Coastguard Worker       src16 += s->uv_stride;
1872*77c1e3ccSAndroid Build Coastguard Worker     } while (--h);
1873*77c1e3ccSAndroid Build Coastguard Worker 
1874*77c1e3ccSAndroid Build Coastguard Worker     fflush(yuv_rec_file);
1875*77c1e3ccSAndroid Build Coastguard Worker     return;
1876*77c1e3ccSAndroid Build Coastguard Worker   }
1877*77c1e3ccSAndroid Build Coastguard Worker 
1878*77c1e3ccSAndroid Build Coastguard Worker   do {
1879*77c1e3ccSAndroid Build Coastguard Worker     fwrite(src, s->y_width, 1, yuv_rec_file);
1880*77c1e3ccSAndroid Build Coastguard Worker     src += s->y_stride;
1881*77c1e3ccSAndroid Build Coastguard Worker   } while (--h);
1882*77c1e3ccSAndroid Build Coastguard Worker 
1883*77c1e3ccSAndroid Build Coastguard Worker   src = s->u_buffer;
1884*77c1e3ccSAndroid Build Coastguard Worker   h = s->uv_height;
1885*77c1e3ccSAndroid Build Coastguard Worker 
1886*77c1e3ccSAndroid Build Coastguard Worker   do {
1887*77c1e3ccSAndroid Build Coastguard Worker     fwrite(src, s->uv_width, 1, yuv_rec_file);
1888*77c1e3ccSAndroid Build Coastguard Worker     src += s->uv_stride;
1889*77c1e3ccSAndroid Build Coastguard Worker   } while (--h);
1890*77c1e3ccSAndroid Build Coastguard Worker 
1891*77c1e3ccSAndroid Build Coastguard Worker   src = s->v_buffer;
1892*77c1e3ccSAndroid Build Coastguard Worker   h = s->uv_height;
1893*77c1e3ccSAndroid Build Coastguard Worker 
1894*77c1e3ccSAndroid Build Coastguard Worker   do {
1895*77c1e3ccSAndroid Build Coastguard Worker     fwrite(src, s->uv_width, 1, yuv_rec_file);
1896*77c1e3ccSAndroid Build Coastguard Worker     src += s->uv_stride;
1897*77c1e3ccSAndroid Build Coastguard Worker   } while (--h);
1898*77c1e3ccSAndroid Build Coastguard Worker 
1899*77c1e3ccSAndroid Build Coastguard Worker   fflush(yuv_rec_file);
1900*77c1e3ccSAndroid Build Coastguard Worker }
1901*77c1e3ccSAndroid Build Coastguard Worker #endif  // OUTPUT_YUV_REC
1902*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_mv_search_params(AV1_COMP * cpi)1903*77c1e3ccSAndroid Build Coastguard Worker void av1_set_mv_search_params(AV1_COMP *cpi) {
1904*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
1905*77c1e3ccSAndroid Build Coastguard Worker   MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
1906*77c1e3ccSAndroid Build Coastguard Worker   const int max_mv_def = AOMMAX(cm->width, cm->height);
1907*77c1e3ccSAndroid Build Coastguard Worker 
1908*77c1e3ccSAndroid Build Coastguard Worker   // Default based on max resolution.
1909*77c1e3ccSAndroid Build Coastguard Worker   mv_search_params->mv_step_param = av1_init_search_range(max_mv_def);
1910*77c1e3ccSAndroid Build Coastguard Worker 
1911*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.mv_sf.auto_mv_step_size) {
1912*77c1e3ccSAndroid Build Coastguard Worker     if (frame_is_intra_only(cm)) {
1913*77c1e3ccSAndroid Build Coastguard Worker       // Initialize max_mv_magnitude for use in the first INTER frame
1914*77c1e3ccSAndroid Build Coastguard Worker       // after a key/intra-only frame.
1915*77c1e3ccSAndroid Build Coastguard Worker       mv_search_params->max_mv_magnitude = max_mv_def;
1916*77c1e3ccSAndroid Build Coastguard Worker     } else {
1917*77c1e3ccSAndroid Build Coastguard Worker       // Use adaptive mv steps based on previous frame stats for show frames and
1918*77c1e3ccSAndroid Build Coastguard Worker       // internal arfs.
1919*77c1e3ccSAndroid Build Coastguard Worker       FRAME_UPDATE_TYPE cur_update_type =
1920*77c1e3ccSAndroid Build Coastguard Worker           cpi->ppi->gf_group.update_type[cpi->gf_frame_index];
1921*77c1e3ccSAndroid Build Coastguard Worker       int use_auto_mv_step =
1922*77c1e3ccSAndroid Build Coastguard Worker           (cm->show_frame || cur_update_type == INTNL_ARF_UPDATE) &&
1923*77c1e3ccSAndroid Build Coastguard Worker           mv_search_params->max_mv_magnitude != -1 &&
1924*77c1e3ccSAndroid Build Coastguard Worker           cpi->sf.mv_sf.auto_mv_step_size >= 2;
1925*77c1e3ccSAndroid Build Coastguard Worker       if (use_auto_mv_step) {
1926*77c1e3ccSAndroid Build Coastguard Worker         // Allow mv_steps to correspond to twice the max mv magnitude found
1927*77c1e3ccSAndroid Build Coastguard Worker         // in the previous frame, capped by the default max_mv_magnitude based
1928*77c1e3ccSAndroid Build Coastguard Worker         // on resolution.
1929*77c1e3ccSAndroid Build Coastguard Worker         mv_search_params->mv_step_param = av1_init_search_range(
1930*77c1e3ccSAndroid Build Coastguard Worker             AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude));
1931*77c1e3ccSAndroid Build Coastguard Worker       }
1932*77c1e3ccSAndroid Build Coastguard Worker       // Reset max_mv_magnitude based on update flag.
1933*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->do_frame_data_update) mv_search_params->max_mv_magnitude = -1;
1934*77c1e3ccSAndroid Build Coastguard Worker     }
1935*77c1e3ccSAndroid Build Coastguard Worker   }
1936*77c1e3ccSAndroid Build Coastguard Worker }
1937*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_screen_content_options(AV1_COMP * cpi,FeatureFlags * features)1938*77c1e3ccSAndroid Build Coastguard Worker void av1_set_screen_content_options(AV1_COMP *cpi, FeatureFlags *features) {
1939*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
1940*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1941*77c1e3ccSAndroid Build Coastguard Worker 
1942*77c1e3ccSAndroid Build Coastguard Worker   if (cm->seq_params->force_screen_content_tools != 2) {
1943*77c1e3ccSAndroid Build Coastguard Worker     features->allow_screen_content_tools = features->allow_intrabc =
1944*77c1e3ccSAndroid Build Coastguard Worker         cm->seq_params->force_screen_content_tools;
1945*77c1e3ccSAndroid Build Coastguard Worker     return;
1946*77c1e3ccSAndroid Build Coastguard Worker   }
1947*77c1e3ccSAndroid Build Coastguard Worker 
1948*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
1949*77c1e3ccSAndroid Build Coastguard Worker     features->allow_screen_content_tools = 1;
1950*77c1e3ccSAndroid Build Coastguard Worker     features->allow_intrabc = cpi->oxcf.mode == REALTIME ? 0 : 1;
1951*77c1e3ccSAndroid Build Coastguard Worker     cpi->is_screen_content_type = 1;
1952*77c1e3ccSAndroid Build Coastguard Worker     cpi->use_screen_content_tools = 1;
1953*77c1e3ccSAndroid Build Coastguard Worker     return;
1954*77c1e3ccSAndroid Build Coastguard Worker   }
1955*77c1e3ccSAndroid Build Coastguard Worker 
1956*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.mode == REALTIME) {
1957*77c1e3ccSAndroid Build Coastguard Worker     features->allow_screen_content_tools = features->allow_intrabc = 0;
1958*77c1e3ccSAndroid Build Coastguard Worker     return;
1959*77c1e3ccSAndroid Build Coastguard Worker   }
1960*77c1e3ccSAndroid Build Coastguard Worker 
1961*77c1e3ccSAndroid Build Coastguard Worker   // Screen content tools are not evaluated in non-RD encoding mode unless
1962*77c1e3ccSAndroid Build Coastguard Worker   // content type is not set explicitly, i.e., when
1963*77c1e3ccSAndroid Build Coastguard Worker   // cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN, use_nonrd_pick_mode = 1
1964*77c1e3ccSAndroid Build Coastguard Worker   // and hybrid_intra_pickmode = 0. Hence, screen content detection is
1965*77c1e3ccSAndroid Build Coastguard Worker   // disabled.
1966*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.rt_sf.use_nonrd_pick_mode &&
1967*77c1e3ccSAndroid Build Coastguard Worker       !cpi->sf.rt_sf.hybrid_intra_pickmode) {
1968*77c1e3ccSAndroid Build Coastguard Worker     features->allow_screen_content_tools = features->allow_intrabc = 0;
1969*77c1e3ccSAndroid Build Coastguard Worker     return;
1970*77c1e3ccSAndroid Build Coastguard Worker   }
1971*77c1e3ccSAndroid Build Coastguard Worker 
1972*77c1e3ccSAndroid Build Coastguard Worker   // Estimate if the source frame is screen content, based on the portion of
1973*77c1e3ccSAndroid Build Coastguard Worker   // blocks that have few luma colors.
1974*77c1e3ccSAndroid Build Coastguard Worker   const uint8_t *src = cpi->unfiltered_source->y_buffer;
1975*77c1e3ccSAndroid Build Coastguard Worker   assert(src != NULL);
1976*77c1e3ccSAndroid Build Coastguard Worker   const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
1977*77c1e3ccSAndroid Build Coastguard Worker   const int stride = cpi->unfiltered_source->y_stride;
1978*77c1e3ccSAndroid Build Coastguard Worker   const int width = cpi->unfiltered_source->y_width;
1979*77c1e3ccSAndroid Build Coastguard Worker   const int height = cpi->unfiltered_source->y_height;
1980*77c1e3ccSAndroid Build Coastguard Worker   const int64_t area = (int64_t)width * height;
1981*77c1e3ccSAndroid Build Coastguard Worker   const int bd = cm->seq_params->bit_depth;
1982*77c1e3ccSAndroid Build Coastguard Worker   const int blk_w = 16;
1983*77c1e3ccSAndroid Build Coastguard Worker   const int blk_h = 16;
1984*77c1e3ccSAndroid Build Coastguard Worker   // These threshold values are selected experimentally.
1985*77c1e3ccSAndroid Build Coastguard Worker   const int color_thresh = 4;
1986*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int var_thresh = 0;
1987*77c1e3ccSAndroid Build Coastguard Worker   // Counts of blocks with no more than color_thresh colors.
1988*77c1e3ccSAndroid Build Coastguard Worker   int64_t counts_1 = 0;
1989*77c1e3ccSAndroid Build Coastguard Worker   // Counts of blocks with no more than color_thresh colors and variance larger
1990*77c1e3ccSAndroid Build Coastguard Worker   // than var_thresh.
1991*77c1e3ccSAndroid Build Coastguard Worker   int64_t counts_2 = 0;
1992*77c1e3ccSAndroid Build Coastguard Worker 
1993*77c1e3ccSAndroid Build Coastguard Worker   for (int r = 0; r + blk_h <= height; r += blk_h) {
1994*77c1e3ccSAndroid Build Coastguard Worker     for (int c = 0; c + blk_w <= width; c += blk_w) {
1995*77c1e3ccSAndroid Build Coastguard Worker       int count_buf[1 << 8];  // Maximum (1 << 8) bins for hbd path.
1996*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t *const this_src = src + r * stride + c;
1997*77c1e3ccSAndroid Build Coastguard Worker       int n_colors;
1998*77c1e3ccSAndroid Build Coastguard Worker       if (use_hbd)
1999*77c1e3ccSAndroid Build Coastguard Worker         av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd, NULL,
2000*77c1e3ccSAndroid Build Coastguard Worker                                 count_buf, &n_colors, NULL);
2001*77c1e3ccSAndroid Build Coastguard Worker       else
2002*77c1e3ccSAndroid Build Coastguard Worker         av1_count_colors(this_src, stride, blk_w, blk_h, count_buf, &n_colors);
2003*77c1e3ccSAndroid Build Coastguard Worker       if (n_colors > 1 && n_colors <= color_thresh) {
2004*77c1e3ccSAndroid Build Coastguard Worker         ++counts_1;
2005*77c1e3ccSAndroid Build Coastguard Worker         struct buf_2d buf;
2006*77c1e3ccSAndroid Build Coastguard Worker         buf.stride = stride;
2007*77c1e3ccSAndroid Build Coastguard Worker         buf.buf = (uint8_t *)this_src;
2008*77c1e3ccSAndroid Build Coastguard Worker         const unsigned int var = av1_get_perpixel_variance(
2009*77c1e3ccSAndroid Build Coastguard Worker             cpi, xd, &buf, BLOCK_16X16, AOM_PLANE_Y, use_hbd);
2010*77c1e3ccSAndroid Build Coastguard Worker         if (var > var_thresh) ++counts_2;
2011*77c1e3ccSAndroid Build Coastguard Worker       }
2012*77c1e3ccSAndroid Build Coastguard Worker     }
2013*77c1e3ccSAndroid Build Coastguard Worker   }
2014*77c1e3ccSAndroid Build Coastguard Worker 
2015*77c1e3ccSAndroid Build Coastguard Worker   // The threshold values are selected experimentally.
2016*77c1e3ccSAndroid Build Coastguard Worker   features->allow_screen_content_tools = counts_1 * blk_h * blk_w * 10 > area;
2017*77c1e3ccSAndroid Build Coastguard Worker   // IntraBC would force loop filters off, so we use more strict rules that also
2018*77c1e3ccSAndroid Build Coastguard Worker   // requires that the block has high variance.
2019*77c1e3ccSAndroid Build Coastguard Worker   features->allow_intrabc = features->allow_screen_content_tools &&
2020*77c1e3ccSAndroid Build Coastguard Worker                             counts_2 * blk_h * blk_w * 12 > area;
2021*77c1e3ccSAndroid Build Coastguard Worker   cpi->use_screen_content_tools = features->allow_screen_content_tools;
2022*77c1e3ccSAndroid Build Coastguard Worker   cpi->is_screen_content_type =
2023*77c1e3ccSAndroid Build Coastguard Worker       features->allow_intrabc || (counts_1 * blk_h * blk_w * 10 > area * 4 &&
2024*77c1e3ccSAndroid Build Coastguard Worker                                   counts_2 * blk_h * blk_w * 30 > area);
2025*77c1e3ccSAndroid Build Coastguard Worker }
2026*77c1e3ccSAndroid Build Coastguard Worker 
init_motion_estimation(AV1_COMP * cpi)2027*77c1e3ccSAndroid Build Coastguard Worker static void init_motion_estimation(AV1_COMP *cpi) {
2028*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2029*77c1e3ccSAndroid Build Coastguard Worker   MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
2030*77c1e3ccSAndroid Build Coastguard Worker   const int aligned_width = (cm->width + 7) & ~7;
2031*77c1e3ccSAndroid Build Coastguard Worker   const int y_stride =
2032*77c1e3ccSAndroid Build Coastguard Worker       aom_calc_y_stride(aligned_width, cpi->oxcf.border_in_pixels);
2033*77c1e3ccSAndroid Build Coastguard Worker   const int y_stride_src = ((cpi->oxcf.frm_dim_cfg.width != cm->width ||
2034*77c1e3ccSAndroid Build Coastguard Worker                              cpi->oxcf.frm_dim_cfg.height != cm->height) ||
2035*77c1e3ccSAndroid Build Coastguard Worker                             av1_superres_scaled(cm))
2036*77c1e3ccSAndroid Build Coastguard Worker                                ? y_stride
2037*77c1e3ccSAndroid Build Coastguard Worker                                : cpi->ppi->lookahead->buf->img.y_stride;
2038*77c1e3ccSAndroid Build Coastguard Worker   int fpf_y_stride =
2039*77c1e3ccSAndroid Build Coastguard Worker       cm->cur_frame != NULL ? cm->cur_frame->buf.y_stride : y_stride;
2040*77c1e3ccSAndroid Build Coastguard Worker 
2041*77c1e3ccSAndroid Build Coastguard Worker   // Update if search_site_cfg is uninitialized or the current frame has a new
2042*77c1e3ccSAndroid Build Coastguard Worker   // stride
2043*77c1e3ccSAndroid Build Coastguard Worker   const int should_update =
2044*77c1e3ccSAndroid Build Coastguard Worker       !mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride ||
2045*77c1e3ccSAndroid Build Coastguard Worker       !mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][DIAMOND].stride ||
2046*77c1e3ccSAndroid Build Coastguard Worker       (y_stride !=
2047*77c1e3ccSAndroid Build Coastguard Worker        mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride);
2048*77c1e3ccSAndroid Build Coastguard Worker 
2049*77c1e3ccSAndroid Build Coastguard Worker   if (!should_update) {
2050*77c1e3ccSAndroid Build Coastguard Worker     return;
2051*77c1e3ccSAndroid Build Coastguard Worker   }
2052*77c1e3ccSAndroid Build Coastguard Worker 
2053*77c1e3ccSAndroid Build Coastguard Worker   // Initialization of search_site_cfg for NUM_DISTINCT_SEARCH_METHODS.
2054*77c1e3ccSAndroid Build Coastguard Worker   for (SEARCH_METHODS i = DIAMOND; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
2055*77c1e3ccSAndroid Build Coastguard Worker     const int level = ((i == NSTEP_8PT) || (i == CLAMPED_DIAMOND)) ? 1 : 0;
2056*77c1e3ccSAndroid Build Coastguard Worker     av1_init_motion_compensation[i](
2057*77c1e3ccSAndroid Build Coastguard Worker         &mv_search_params->search_site_cfg[SS_CFG_SRC][i], y_stride, level);
2058*77c1e3ccSAndroid Build Coastguard Worker     av1_init_motion_compensation[i](
2059*77c1e3ccSAndroid Build Coastguard Worker         &mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][i], y_stride_src,
2060*77c1e3ccSAndroid Build Coastguard Worker         level);
2061*77c1e3ccSAndroid Build Coastguard Worker   }
2062*77c1e3ccSAndroid Build Coastguard Worker 
2063*77c1e3ccSAndroid Build Coastguard Worker   // First pass search site config initialization.
2064*77c1e3ccSAndroid Build Coastguard Worker   av1_init_motion_fpf(&mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
2065*77c1e3ccSAndroid Build Coastguard Worker                       fpf_y_stride);
2066*77c1e3ccSAndroid Build Coastguard Worker   for (SEARCH_METHODS i = NSTEP; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
2067*77c1e3ccSAndroid Build Coastguard Worker     memcpy(&mv_search_params->search_site_cfg[SS_CFG_FPF][i],
2068*77c1e3ccSAndroid Build Coastguard Worker            &mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
2069*77c1e3ccSAndroid Build Coastguard Worker            sizeof(search_site_config));
2070*77c1e3ccSAndroid Build Coastguard Worker   }
2071*77c1e3ccSAndroid Build Coastguard Worker }
2072*77c1e3ccSAndroid Build Coastguard Worker 
init_ref_frame_bufs(AV1_COMP * cpi)2073*77c1e3ccSAndroid Build Coastguard Worker static void init_ref_frame_bufs(AV1_COMP *cpi) {
2074*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2075*77c1e3ccSAndroid Build Coastguard Worker   int i;
2076*77c1e3ccSAndroid Build Coastguard Worker   if (cm->cur_frame) {
2077*77c1e3ccSAndroid Build Coastguard Worker     cm->cur_frame->ref_count--;
2078*77c1e3ccSAndroid Build Coastguard Worker     cm->cur_frame = NULL;
2079*77c1e3ccSAndroid Build Coastguard Worker   }
2080*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < REF_FRAMES; ++i) {
2081*77c1e3ccSAndroid Build Coastguard Worker     if (cm->ref_frame_map[i]) {
2082*77c1e3ccSAndroid Build Coastguard Worker       cm->ref_frame_map[i]->ref_count--;
2083*77c1e3ccSAndroid Build Coastguard Worker       cm->ref_frame_map[i] = NULL;
2084*77c1e3ccSAndroid Build Coastguard Worker     }
2085*77c1e3ccSAndroid Build Coastguard Worker   }
2086*77c1e3ccSAndroid Build Coastguard Worker #ifndef NDEBUG
2087*77c1e3ccSAndroid Build Coastguard Worker   BufferPool *const pool = cm->buffer_pool;
2088*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < pool->num_frame_bufs; ++i) {
2089*77c1e3ccSAndroid Build Coastguard Worker     assert(pool->frame_bufs[i].ref_count == 0);
2090*77c1e3ccSAndroid Build Coastguard Worker   }
2091*77c1e3ccSAndroid Build Coastguard Worker #endif
2092*77c1e3ccSAndroid Build Coastguard Worker }
2093*77c1e3ccSAndroid Build Coastguard Worker 
2094*77c1e3ccSAndroid Build Coastguard Worker // TODO(chengchen): consider renaming this function as it is necessary
2095*77c1e3ccSAndroid Build Coastguard Worker // for the encoder to setup critical parameters, and it does not
2096*77c1e3ccSAndroid Build Coastguard Worker // deal with initial width any longer.
av1_check_initial_width(AV1_COMP * cpi,int use_highbitdepth,int subsampling_x,int subsampling_y)2097*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
2098*77c1e3ccSAndroid Build Coastguard Worker                                         int subsampling_x, int subsampling_y) {
2099*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2100*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader *const seq_params = cm->seq_params;
2101*77c1e3ccSAndroid Build Coastguard Worker 
2102*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->frame_size_related_setup_done ||
2103*77c1e3ccSAndroid Build Coastguard Worker       seq_params->use_highbitdepth != use_highbitdepth ||
2104*77c1e3ccSAndroid Build Coastguard Worker       seq_params->subsampling_x != subsampling_x ||
2105*77c1e3ccSAndroid Build Coastguard Worker       seq_params->subsampling_y != subsampling_y) {
2106*77c1e3ccSAndroid Build Coastguard Worker     seq_params->subsampling_x = subsampling_x;
2107*77c1e3ccSAndroid Build Coastguard Worker     seq_params->subsampling_y = subsampling_y;
2108*77c1e3ccSAndroid Build Coastguard Worker     seq_params->use_highbitdepth = use_highbitdepth;
2109*77c1e3ccSAndroid Build Coastguard Worker 
2110*77c1e3ccSAndroid Build Coastguard Worker     av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
2111*77c1e3ccSAndroid Build Coastguard Worker     av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
2112*77c1e3ccSAndroid Build Coastguard Worker 
2113*77c1e3ccSAndroid Build Coastguard Worker     if (!is_stat_generation_stage(cpi)) {
2114*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
2115*77c1e3ccSAndroid Build Coastguard Worker       if (!av1_tf_info_alloc(&cpi->ppi->tf_info, cpi))
2116*77c1e3ccSAndroid Build Coastguard Worker         return AOM_CODEC_MEM_ERROR;
2117*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
2118*77c1e3ccSAndroid Build Coastguard Worker     }
2119*77c1e3ccSAndroid Build Coastguard Worker     init_ref_frame_bufs(cpi);
2120*77c1e3ccSAndroid Build Coastguard Worker 
2121*77c1e3ccSAndroid Build Coastguard Worker     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
2122*77c1e3ccSAndroid Build Coastguard Worker 
2123*77c1e3ccSAndroid Build Coastguard Worker     cpi->initial_mbs = cm->mi_params.MBs;
2124*77c1e3ccSAndroid Build Coastguard Worker     cpi->frame_size_related_setup_done = true;
2125*77c1e3ccSAndroid Build Coastguard Worker   }
2126*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
2127*77c1e3ccSAndroid Build Coastguard Worker }
2128*77c1e3ccSAndroid Build Coastguard Worker 
2129*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
setup_denoiser_buffer(AV1_COMP * cpi)2130*77c1e3ccSAndroid Build Coastguard Worker static void setup_denoiser_buffer(AV1_COMP *cpi) {
2131*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2132*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.noise_sensitivity > 0 &&
2133*77c1e3ccSAndroid Build Coastguard Worker       !cpi->denoiser.frame_buffer_initialized) {
2134*77c1e3ccSAndroid Build Coastguard Worker     if (av1_denoiser_alloc(
2135*77c1e3ccSAndroid Build Coastguard Worker             cm, &cpi->svc, &cpi->denoiser, cpi->ppi->use_svc,
2136*77c1e3ccSAndroid Build Coastguard Worker             cpi->oxcf.noise_sensitivity, cm->width, cm->height,
2137*77c1e3ccSAndroid Build Coastguard Worker             cm->seq_params->subsampling_x, cm->seq_params->subsampling_y,
2138*77c1e3ccSAndroid Build Coastguard Worker             cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS))
2139*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2140*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate denoiser");
2141*77c1e3ccSAndroid Build Coastguard Worker   }
2142*77c1e3ccSAndroid Build Coastguard Worker }
2143*77c1e3ccSAndroid Build Coastguard Worker #endif
2144*77c1e3ccSAndroid Build Coastguard Worker 
2145*77c1e3ccSAndroid Build Coastguard Worker // Returns 1 if the assigned width or height was <= 0.
set_size_literal(AV1_COMP * cpi,int width,int height)2146*77c1e3ccSAndroid Build Coastguard Worker static int set_size_literal(AV1_COMP *cpi, int width, int height) {
2147*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
2148*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t err = av1_check_initial_width(
2149*77c1e3ccSAndroid Build Coastguard Worker       cpi, cm->seq_params->use_highbitdepth, cm->seq_params->subsampling_x,
2150*77c1e3ccSAndroid Build Coastguard Worker       cm->seq_params->subsampling_y);
2151*77c1e3ccSAndroid Build Coastguard Worker   if (err != AOM_CODEC_OK) {
2152*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(cm->error, err, "av1_check_initial_width() failed");
2153*77c1e3ccSAndroid Build Coastguard Worker   }
2154*77c1e3ccSAndroid Build Coastguard Worker 
2155*77c1e3ccSAndroid Build Coastguard Worker   if (width <= 0 || height <= 0) return 1;
2156*77c1e3ccSAndroid Build Coastguard Worker 
2157*77c1e3ccSAndroid Build Coastguard Worker   cm->width = width;
2158*77c1e3ccSAndroid Build Coastguard Worker   cm->height = height;
2159*77c1e3ccSAndroid Build Coastguard Worker 
2160*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
2161*77c1e3ccSAndroid Build Coastguard Worker   setup_denoiser_buffer(cpi);
2162*77c1e3ccSAndroid Build Coastguard Worker #endif
2163*77c1e3ccSAndroid Build Coastguard Worker 
2164*77c1e3ccSAndroid Build Coastguard Worker   if (cm->width > cpi->data_alloc_width ||
2165*77c1e3ccSAndroid Build Coastguard Worker       cm->height > cpi->data_alloc_height) {
2166*77c1e3ccSAndroid Build Coastguard Worker     av1_free_context_buffers(cm);
2167*77c1e3ccSAndroid Build Coastguard Worker     av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
2168*77c1e3ccSAndroid Build Coastguard Worker     av1_free_sms_tree(&cpi->td);
2169*77c1e3ccSAndroid Build Coastguard Worker     av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
2170*77c1e3ccSAndroid Build Coastguard Worker     cpi->td.firstpass_ctx = NULL;
2171*77c1e3ccSAndroid Build Coastguard Worker     alloc_compressor_data(cpi);
2172*77c1e3ccSAndroid Build Coastguard Worker     realloc_segmentation_maps(cpi);
2173*77c1e3ccSAndroid Build Coastguard Worker     cpi->data_alloc_width = cm->width;
2174*77c1e3ccSAndroid Build Coastguard Worker     cpi->data_alloc_height = cm->height;
2175*77c1e3ccSAndroid Build Coastguard Worker     cpi->frame_size_related_setup_done = false;
2176*77c1e3ccSAndroid Build Coastguard Worker   }
2177*77c1e3ccSAndroid Build Coastguard Worker   alloc_mb_mode_info_buffers(cpi);
2178*77c1e3ccSAndroid Build Coastguard Worker   av1_update_frame_size(cpi);
2179*77c1e3ccSAndroid Build Coastguard Worker 
2180*77c1e3ccSAndroid Build Coastguard Worker   return 0;
2181*77c1e3ccSAndroid Build Coastguard Worker }
2182*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_frame_size(AV1_COMP * cpi,int width,int height)2183*77c1e3ccSAndroid Build Coastguard Worker void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
2184*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2185*77c1e3ccSAndroid Build Coastguard Worker   const SequenceHeader *const seq_params = cm->seq_params;
2186*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
2187*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
2188*77c1e3ccSAndroid Build Coastguard Worker   int ref_frame;
2189*77c1e3ccSAndroid Build Coastguard Worker 
2190*77c1e3ccSAndroid Build Coastguard Worker   if (width != cm->width || height != cm->height) {
2191*77c1e3ccSAndroid Build Coastguard Worker     // There has been a change in the encoded frame size
2192*77c1e3ccSAndroid Build Coastguard Worker     set_size_literal(cpi, width, height);
2193*77c1e3ccSAndroid Build Coastguard Worker     // Recalculate 'all_lossless' in case super-resolution was (un)selected.
2194*77c1e3ccSAndroid Build Coastguard Worker     cm->features.all_lossless =
2195*77c1e3ccSAndroid Build Coastguard Worker         cm->features.coded_lossless && !av1_superres_scaled(cm);
2196*77c1e3ccSAndroid Build Coastguard Worker 
2197*77c1e3ccSAndroid Build Coastguard Worker     av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
2198*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
2199*77c1e3ccSAndroid Build Coastguard Worker     // Reset the denoiser on the resized frame.
2200*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->oxcf.noise_sensitivity > 0) {
2201*77c1e3ccSAndroid Build Coastguard Worker       av1_denoiser_free(&(cpi->denoiser));
2202*77c1e3ccSAndroid Build Coastguard Worker       setup_denoiser_buffer(cpi);
2203*77c1e3ccSAndroid Build Coastguard Worker     }
2204*77c1e3ccSAndroid Build Coastguard Worker #endif
2205*77c1e3ccSAndroid Build Coastguard Worker   }
2206*77c1e3ccSAndroid Build Coastguard Worker   if (is_stat_consumption_stage(cpi)) {
2207*77c1e3ccSAndroid Build Coastguard Worker     av1_set_target_rate(cpi, cm->width, cm->height);
2208*77c1e3ccSAndroid Build Coastguard Worker   }
2209*77c1e3ccSAndroid Build Coastguard Worker 
2210*77c1e3ccSAndroid Build Coastguard Worker   alloc_frame_mvs(cm, cm->cur_frame);
2211*77c1e3ccSAndroid Build Coastguard Worker 
2212*77c1e3ccSAndroid Build Coastguard Worker   // Allocate above context buffers
2213*77c1e3ccSAndroid Build Coastguard Worker   CommonContexts *const above_contexts = &cm->above_contexts;
2214*77c1e3ccSAndroid Build Coastguard Worker   if (above_contexts->num_planes < av1_num_planes(cm) ||
2215*77c1e3ccSAndroid Build Coastguard Worker       above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
2216*77c1e3ccSAndroid Build Coastguard Worker       above_contexts->num_tile_rows < cm->tiles.rows) {
2217*77c1e3ccSAndroid Build Coastguard Worker     av1_free_above_context_buffers(above_contexts);
2218*77c1e3ccSAndroid Build Coastguard Worker     if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
2219*77c1e3ccSAndroid Build Coastguard Worker                                         cm->mi_params.mi_cols,
2220*77c1e3ccSAndroid Build Coastguard Worker                                         av1_num_planes(cm)))
2221*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2222*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate context buffers");
2223*77c1e3ccSAndroid Build Coastguard Worker   }
2224*77c1e3ccSAndroid Build Coastguard Worker 
2225*77c1e3ccSAndroid Build Coastguard Worker   AV1EncoderConfig *oxcf = &cpi->oxcf;
2226*77c1e3ccSAndroid Build Coastguard Worker   oxcf->border_in_pixels = av1_get_enc_border_size(
2227*77c1e3ccSAndroid Build Coastguard Worker       av1_is_resize_needed(oxcf), oxcf->kf_cfg.key_freq_max == 0,
2228*77c1e3ccSAndroid Build Coastguard Worker       cm->seq_params->sb_size);
2229*77c1e3ccSAndroid Build Coastguard Worker 
2230*77c1e3ccSAndroid Build Coastguard Worker   // Reset the frame pointers to the current frame size.
2231*77c1e3ccSAndroid Build Coastguard Worker   if (aom_realloc_frame_buffer(
2232*77c1e3ccSAndroid Build Coastguard Worker           &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
2233*77c1e3ccSAndroid Build Coastguard Worker           seq_params->subsampling_y, seq_params->use_highbitdepth,
2234*77c1e3ccSAndroid Build Coastguard Worker           cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
2235*77c1e3ccSAndroid Build Coastguard Worker           NULL, cpi->alloc_pyramid, 0))
2236*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2237*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to allocate frame buffer");
2238*77c1e3ccSAndroid Build Coastguard Worker 
2239*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi)) av1_init_cdef_worker(cpi);
2240*77c1e3ccSAndroid Build Coastguard Worker 
2241*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
2242*77c1e3ccSAndroid Build Coastguard Worker   if (is_restoration_used(cm)) {
2243*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < num_planes; ++i)
2244*77c1e3ccSAndroid Build Coastguard Worker       cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
2245*77c1e3ccSAndroid Build Coastguard Worker 
2246*77c1e3ccSAndroid Build Coastguard Worker     const bool is_sgr_enabled = !cpi->sf.lpf_sf.disable_sgr_filter;
2247*77c1e3ccSAndroid Build Coastguard Worker     av1_alloc_restoration_buffers(cm, is_sgr_enabled);
2248*77c1e3ccSAndroid Build Coastguard Worker     // Store the allocated restoration buffers in MT object.
2249*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->p_mt_info.num_workers > 1) {
2250*77c1e3ccSAndroid Build Coastguard Worker       av1_init_lr_mt_buffers(cpi);
2251*77c1e3ccSAndroid Build Coastguard Worker     }
2252*77c1e3ccSAndroid Build Coastguard Worker   }
2253*77c1e3ccSAndroid Build Coastguard Worker #endif
2254*77c1e3ccSAndroid Build Coastguard Worker 
2255*77c1e3ccSAndroid Build Coastguard Worker   init_motion_estimation(cpi);
2256*77c1e3ccSAndroid Build Coastguard Worker 
2257*77c1e3ccSAndroid Build Coastguard Worker   int has_valid_ref_frame = 0;
2258*77c1e3ccSAndroid Build Coastguard Worker   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2259*77c1e3ccSAndroid Build Coastguard Worker     RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
2260*77c1e3ccSAndroid Build Coastguard Worker     if (buf != NULL) {
2261*77c1e3ccSAndroid Build Coastguard Worker       struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
2262*77c1e3ccSAndroid Build Coastguard Worker       av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
2263*77c1e3ccSAndroid Build Coastguard Worker                                         buf->buf.y_crop_height, cm->width,
2264*77c1e3ccSAndroid Build Coastguard Worker                                         cm->height);
2265*77c1e3ccSAndroid Build Coastguard Worker       has_valid_ref_frame |= av1_is_valid_scale(sf);
2266*77c1e3ccSAndroid Build Coastguard Worker       if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
2267*77c1e3ccSAndroid Build Coastguard Worker     }
2268*77c1e3ccSAndroid Build Coastguard Worker   }
2269*77c1e3ccSAndroid Build Coastguard Worker   if (!frame_is_intra_only(cm) && !has_valid_ref_frame) {
2270*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(
2271*77c1e3ccSAndroid Build Coastguard Worker         cm->error, AOM_CODEC_CORRUPT_FRAME,
2272*77c1e3ccSAndroid Build Coastguard Worker         "Can't find at least one reference frame with valid size");
2273*77c1e3ccSAndroid Build Coastguard Worker   }
2274*77c1e3ccSAndroid Build Coastguard Worker 
2275*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
2276*77c1e3ccSAndroid Build Coastguard Worker                                     cm->width, cm->height);
2277*77c1e3ccSAndroid Build Coastguard Worker 
2278*77c1e3ccSAndroid Build Coastguard Worker   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2279*77c1e3ccSAndroid Build Coastguard Worker }
2280*77c1e3ccSAndroid Build Coastguard Worker 
extend_borders_mt(const AV1_COMP * cpi,MULTI_THREADED_MODULES stage,int plane)2281*77c1e3ccSAndroid Build Coastguard Worker static inline int extend_borders_mt(const AV1_COMP *cpi,
2282*77c1e3ccSAndroid Build Coastguard Worker                                     MULTI_THREADED_MODULES stage, int plane) {
2283*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
2284*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->mt_info.num_mod_workers[stage] < 2) return 0;
2285*77c1e3ccSAndroid Build Coastguard Worker   switch (stage) {
2286*77c1e3ccSAndroid Build Coastguard Worker     // TODO([email protected]): When cdef and loop-restoration are disabled,
2287*77c1e3ccSAndroid Build Coastguard Worker     // multi-thread frame border extension along with loop filter frame.
2288*77c1e3ccSAndroid Build Coastguard Worker     // As loop-filtering of a superblock row modifies the pixels of the
2289*77c1e3ccSAndroid Build Coastguard Worker     // above superblock row, border extension requires that loop filtering
2290*77c1e3ccSAndroid Build Coastguard Worker     // of the current and above superblock row is complete.
2291*77c1e3ccSAndroid Build Coastguard Worker     case MOD_LPF: return 0;
2292*77c1e3ccSAndroid Build Coastguard Worker     case MOD_CDEF:
2293*77c1e3ccSAndroid Build Coastguard Worker       return is_cdef_used(cm) && !cpi->ppi->rtc_ref.non_reference_frame &&
2294*77c1e3ccSAndroid Build Coastguard Worker              !is_restoration_used(cm) && !av1_superres_scaled(cm);
2295*77c1e3ccSAndroid Build Coastguard Worker     case MOD_LR:
2296*77c1e3ccSAndroid Build Coastguard Worker       return is_restoration_used(cm) &&
2297*77c1e3ccSAndroid Build Coastguard Worker              (cm->rst_info[plane].frame_restoration_type != RESTORE_NONE);
2298*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0);
2299*77c1e3ccSAndroid Build Coastguard Worker   }
2300*77c1e3ccSAndroid Build Coastguard Worker   return 0;
2301*77c1e3ccSAndroid Build Coastguard Worker }
2302*77c1e3ccSAndroid Build Coastguard Worker 
2303*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Select and apply cdef filters and switchable restoration filters
2304*77c1e3ccSAndroid Build Coastguard Worker  *
2305*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup high_level_algo
2306*77c1e3ccSAndroid Build Coastguard Worker  */
cdef_restoration_frame(AV1_COMP * cpi,AV1_COMMON * cm,MACROBLOCKD * xd,int use_restoration,int use_cdef,unsigned int skip_apply_postproc_filters)2307*77c1e3ccSAndroid Build Coastguard Worker static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
2308*77c1e3ccSAndroid Build Coastguard Worker                                    MACROBLOCKD *xd, int use_restoration,
2309*77c1e3ccSAndroid Build Coastguard Worker                                    int use_cdef,
2310*77c1e3ccSAndroid Build Coastguard Worker                                    unsigned int skip_apply_postproc_filters) {
2311*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
2312*77c1e3ccSAndroid Build Coastguard Worker   if (use_restoration)
2313*77c1e3ccSAndroid Build Coastguard Worker     av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
2314*77c1e3ccSAndroid Build Coastguard Worker #else
2315*77c1e3ccSAndroid Build Coastguard Worker   (void)use_restoration;
2316*77c1e3ccSAndroid Build Coastguard Worker #endif
2317*77c1e3ccSAndroid Build Coastguard Worker 
2318*77c1e3ccSAndroid Build Coastguard Worker   if (use_cdef) {
2319*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2320*77c1e3ccSAndroid Build Coastguard Worker     start_timing(cpi, cdef_time);
2321*77c1e3ccSAndroid Build Coastguard Worker #endif
2322*77c1e3ccSAndroid Build Coastguard Worker     const int num_workers = cpi->mt_info.num_mod_workers[MOD_CDEF];
2323*77c1e3ccSAndroid Build Coastguard Worker     // Find CDEF parameters
2324*77c1e3ccSAndroid Build Coastguard Worker     av1_cdef_search(cpi);
2325*77c1e3ccSAndroid Build Coastguard Worker 
2326*77c1e3ccSAndroid Build Coastguard Worker     // Apply the filter
2327*77c1e3ccSAndroid Build Coastguard Worker     if ((skip_apply_postproc_filters & SKIP_APPLY_CDEF) == 0) {
2328*77c1e3ccSAndroid Build Coastguard Worker       assert(!cpi->ppi->rtc_ref.non_reference_frame);
2329*77c1e3ccSAndroid Build Coastguard Worker       if (num_workers > 1) {
2330*77c1e3ccSAndroid Build Coastguard Worker         // Extension of frame borders is multi-threaded along with cdef.
2331*77c1e3ccSAndroid Build Coastguard Worker         const int do_extend_border =
2332*77c1e3ccSAndroid Build Coastguard Worker             extend_borders_mt(cpi, MOD_CDEF, /* plane */ 0);
2333*77c1e3ccSAndroid Build Coastguard Worker         av1_cdef_frame_mt(cm, xd, cpi->mt_info.cdef_worker,
2334*77c1e3ccSAndroid Build Coastguard Worker                           cpi->mt_info.workers, &cpi->mt_info.cdef_sync,
2335*77c1e3ccSAndroid Build Coastguard Worker                           num_workers, av1_cdef_init_fb_row_mt,
2336*77c1e3ccSAndroid Build Coastguard Worker                           do_extend_border);
2337*77c1e3ccSAndroid Build Coastguard Worker       } else {
2338*77c1e3ccSAndroid Build Coastguard Worker         av1_cdef_frame(&cm->cur_frame->buf, cm, xd, av1_cdef_init_fb_row);
2339*77c1e3ccSAndroid Build Coastguard Worker       }
2340*77c1e3ccSAndroid Build Coastguard Worker     }
2341*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2342*77c1e3ccSAndroid Build Coastguard Worker     end_timing(cpi, cdef_time);
2343*77c1e3ccSAndroid Build Coastguard Worker #endif
2344*77c1e3ccSAndroid Build Coastguard Worker   }
2345*77c1e3ccSAndroid Build Coastguard Worker 
2346*77c1e3ccSAndroid Build Coastguard Worker   const int use_superres = av1_superres_scaled(cm);
2347*77c1e3ccSAndroid Build Coastguard Worker   if (use_superres) {
2348*77c1e3ccSAndroid Build Coastguard Worker     if ((skip_apply_postproc_filters & SKIP_APPLY_SUPERRES) == 0) {
2349*77c1e3ccSAndroid Build Coastguard Worker       av1_superres_post_encode(cpi);
2350*77c1e3ccSAndroid Build Coastguard Worker     }
2351*77c1e3ccSAndroid Build Coastguard Worker   }
2352*77c1e3ccSAndroid Build Coastguard Worker 
2353*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
2354*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2355*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, loop_restoration_time);
2356*77c1e3ccSAndroid Build Coastguard Worker #endif
2357*77c1e3ccSAndroid Build Coastguard Worker   if (use_restoration) {
2358*77c1e3ccSAndroid Build Coastguard Worker     MultiThreadInfo *const mt_info = &cpi->mt_info;
2359*77c1e3ccSAndroid Build Coastguard Worker     const int num_workers = mt_info->num_mod_workers[MOD_LR];
2360*77c1e3ccSAndroid Build Coastguard Worker     av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
2361*77c1e3ccSAndroid Build Coastguard Worker     av1_pick_filter_restoration(cpi->source, cpi);
2362*77c1e3ccSAndroid Build Coastguard Worker     if ((skip_apply_postproc_filters & SKIP_APPLY_RESTORATION) == 0 &&
2363*77c1e3ccSAndroid Build Coastguard Worker         (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2364*77c1e3ccSAndroid Build Coastguard Worker          cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2365*77c1e3ccSAndroid Build Coastguard Worker          cm->rst_info[2].frame_restoration_type != RESTORE_NONE)) {
2366*77c1e3ccSAndroid Build Coastguard Worker       if (num_workers > 1) {
2367*77c1e3ccSAndroid Build Coastguard Worker         // Extension of frame borders is multi-threaded along with loop
2368*77c1e3ccSAndroid Build Coastguard Worker         // restoration filter.
2369*77c1e3ccSAndroid Build Coastguard Worker         const int do_extend_border = 1;
2370*77c1e3ccSAndroid Build Coastguard Worker         av1_loop_restoration_filter_frame_mt(
2371*77c1e3ccSAndroid Build Coastguard Worker             &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
2372*77c1e3ccSAndroid Build Coastguard Worker             &mt_info->lr_row_sync, &cpi->lr_ctxt, do_extend_border);
2373*77c1e3ccSAndroid Build Coastguard Worker       } else {
2374*77c1e3ccSAndroid Build Coastguard Worker         av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
2375*77c1e3ccSAndroid Build Coastguard Worker                                           &cpi->lr_ctxt);
2376*77c1e3ccSAndroid Build Coastguard Worker       }
2377*77c1e3ccSAndroid Build Coastguard Worker     }
2378*77c1e3ccSAndroid Build Coastguard Worker   }
2379*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2380*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, loop_restoration_time);
2381*77c1e3ccSAndroid Build Coastguard Worker #endif
2382*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
2383*77c1e3ccSAndroid Build Coastguard Worker }
2384*77c1e3ccSAndroid Build Coastguard Worker 
extend_frame_borders(AV1_COMP * cpi)2385*77c1e3ccSAndroid Build Coastguard Worker static void extend_frame_borders(AV1_COMP *cpi) {
2386*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
2387*77c1e3ccSAndroid Build Coastguard Worker   // TODO(debargha): Fix mv search range on encoder side
2388*77c1e3ccSAndroid Build Coastguard Worker   for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
2389*77c1e3ccSAndroid Build Coastguard Worker     const bool extend_border_done = extend_borders_mt(cpi, MOD_CDEF, plane) ||
2390*77c1e3ccSAndroid Build Coastguard Worker                                     extend_borders_mt(cpi, MOD_LR, plane);
2391*77c1e3ccSAndroid Build Coastguard Worker     if (!extend_border_done) {
2392*77c1e3ccSAndroid Build Coastguard Worker       const YV12_BUFFER_CONFIG *const ybf = &cm->cur_frame->buf;
2393*77c1e3ccSAndroid Build Coastguard Worker       aom_extend_frame_borders_plane_row(ybf, plane, 0,
2394*77c1e3ccSAndroid Build Coastguard Worker                                          ybf->crop_heights[plane > 0]);
2395*77c1e3ccSAndroid Build Coastguard Worker     }
2396*77c1e3ccSAndroid Build Coastguard Worker   }
2397*77c1e3ccSAndroid Build Coastguard Worker }
2398*77c1e3ccSAndroid Build Coastguard Worker 
2399*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Select and apply deblocking filters, cdef filters, and restoration
2400*77c1e3ccSAndroid Build Coastguard Worker  * filters.
2401*77c1e3ccSAndroid Build Coastguard Worker  *
2402*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup high_level_algo
2403*77c1e3ccSAndroid Build Coastguard Worker  */
loopfilter_frame(AV1_COMP * cpi,AV1_COMMON * cm)2404*77c1e3ccSAndroid Build Coastguard Worker static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
2405*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
2406*77c1e3ccSAndroid Build Coastguard Worker   const int num_workers = mt_info->num_mod_workers[MOD_LPF];
2407*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
2408*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2409*77c1e3ccSAndroid Build Coastguard Worker   cpi->td.mb.rdmult = cpi->rd.RDMULT;
2410*77c1e3ccSAndroid Build Coastguard Worker 
2411*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(is_lossless_requested(&cpi->oxcf.rc_cfg),
2412*77c1e3ccSAndroid Build Coastguard Worker                  cm->features.coded_lossless && cm->features.all_lossless));
2413*77c1e3ccSAndroid Build Coastguard Worker 
2414*77c1e3ccSAndroid Build Coastguard Worker   const int use_loopfilter =
2415*77c1e3ccSAndroid Build Coastguard Worker       is_loopfilter_used(cm) && !cpi->mt_info.pipeline_lpf_mt_with_enc;
2416*77c1e3ccSAndroid Build Coastguard Worker   const int use_cdef = is_cdef_used(cm);
2417*77c1e3ccSAndroid Build Coastguard Worker   const int use_superres = av1_superres_scaled(cm);
2418*77c1e3ccSAndroid Build Coastguard Worker   const int use_restoration = is_restoration_used(cm);
2419*77c1e3ccSAndroid Build Coastguard Worker 
2420*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int skip_apply_postproc_filters =
2421*77c1e3ccSAndroid Build Coastguard Worker       derive_skip_apply_postproc_filters(cpi, use_loopfilter, use_cdef,
2422*77c1e3ccSAndroid Build Coastguard Worker                                          use_superres, use_restoration);
2423*77c1e3ccSAndroid Build Coastguard Worker 
2424*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2425*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, loop_filter_time);
2426*77c1e3ccSAndroid Build Coastguard Worker #endif
2427*77c1e3ccSAndroid Build Coastguard Worker   if (use_loopfilter) {
2428*77c1e3ccSAndroid Build Coastguard Worker     av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
2429*77c1e3ccSAndroid Build Coastguard Worker     struct loopfilter *lf = &cm->lf;
2430*77c1e3ccSAndroid Build Coastguard Worker     if ((lf->filter_level[0] || lf->filter_level[1]) &&
2431*77c1e3ccSAndroid Build Coastguard Worker         (skip_apply_postproc_filters & SKIP_APPLY_LOOPFILTER) == 0) {
2432*77c1e3ccSAndroid Build Coastguard Worker       assert(!cpi->ppi->rtc_ref.non_reference_frame);
2433*77c1e3ccSAndroid Build Coastguard Worker       // lpf_opt_level = 1 : Enables dual/quad loop-filtering.
2434*77c1e3ccSAndroid Build Coastguard Worker       // lpf_opt_level is set to 1 if transform size search depth in inter
2435*77c1e3ccSAndroid Build Coastguard Worker       // blocks is limited to one as quad loop filtering assumes that all the
2436*77c1e3ccSAndroid Build Coastguard Worker       // transform blocks within a 16x8/8x16/16x16 prediction block are of the
2437*77c1e3ccSAndroid Build Coastguard Worker       // same size. lpf_opt_level = 2 : Filters both chroma planes together, in
2438*77c1e3ccSAndroid Build Coastguard Worker       // addition to enabling dual/quad loop-filtering. This is enabled when lpf
2439*77c1e3ccSAndroid Build Coastguard Worker       // pick method is LPF_PICK_FROM_Q as u and v plane filter levels are
2440*77c1e3ccSAndroid Build Coastguard Worker       // equal.
2441*77c1e3ccSAndroid Build Coastguard Worker       int lpf_opt_level = get_lpf_opt_level(&cpi->sf);
2442*77c1e3ccSAndroid Build Coastguard Worker       av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
2443*77c1e3ccSAndroid Build Coastguard Worker                                mt_info->workers, num_workers,
2444*77c1e3ccSAndroid Build Coastguard Worker                                &mt_info->lf_row_sync, lpf_opt_level);
2445*77c1e3ccSAndroid Build Coastguard Worker     }
2446*77c1e3ccSAndroid Build Coastguard Worker   }
2447*77c1e3ccSAndroid Build Coastguard Worker 
2448*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2449*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, loop_filter_time);
2450*77c1e3ccSAndroid Build Coastguard Worker #endif
2451*77c1e3ccSAndroid Build Coastguard Worker 
2452*77c1e3ccSAndroid Build Coastguard Worker   cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef,
2453*77c1e3ccSAndroid Build Coastguard Worker                          skip_apply_postproc_filters);
2454*77c1e3ccSAndroid Build Coastguard Worker }
2455*77c1e3ccSAndroid Build Coastguard Worker 
update_motion_stat(AV1_COMP * const cpi)2456*77c1e3ccSAndroid Build Coastguard Worker static void update_motion_stat(AV1_COMP *const cpi) {
2457*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2458*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
2459*77c1e3ccSAndroid Build Coastguard Worker   RATE_CONTROL *const rc = &cpi->rc;
2460*77c1e3ccSAndroid Build Coastguard Worker   SVC *const svc = &cpi->svc;
2461*77c1e3ccSAndroid Build Coastguard Worker   const int avg_cnt_zeromv =
2462*77c1e3ccSAndroid Build Coastguard Worker       100 * cpi->rc.cnt_zeromv / (mi_params->mi_rows * mi_params->mi_cols);
2463*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->ppi->use_svc ||
2464*77c1e3ccSAndroid Build Coastguard Worker       (cpi->ppi->use_svc &&
2465*77c1e3ccSAndroid Build Coastguard Worker        !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame &&
2466*77c1e3ccSAndroid Build Coastguard Worker        cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
2467*77c1e3ccSAndroid Build Coastguard Worker     rc->avg_frame_low_motion =
2468*77c1e3ccSAndroid Build Coastguard Worker         (rc->avg_frame_low_motion == 0)
2469*77c1e3ccSAndroid Build Coastguard Worker             ? avg_cnt_zeromv
2470*77c1e3ccSAndroid Build Coastguard Worker             : (3 * rc->avg_frame_low_motion + avg_cnt_zeromv) / 4;
2471*77c1e3ccSAndroid Build Coastguard Worker     // For SVC: set avg_frame_low_motion (only computed on top spatial layer)
2472*77c1e3ccSAndroid Build Coastguard Worker     // to all lower spatial layers.
2473*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->use_svc &&
2474*77c1e3ccSAndroid Build Coastguard Worker         svc->spatial_layer_id == svc->number_spatial_layers - 1) {
2475*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < svc->number_spatial_layers - 1; ++i) {
2476*77c1e3ccSAndroid Build Coastguard Worker         const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
2477*77c1e3ccSAndroid Build Coastguard Worker                                            svc->number_temporal_layers);
2478*77c1e3ccSAndroid Build Coastguard Worker         LAYER_CONTEXT *const lc = &svc->layer_context[layer];
2479*77c1e3ccSAndroid Build Coastguard Worker         RATE_CONTROL *const lrc = &lc->rc;
2480*77c1e3ccSAndroid Build Coastguard Worker         lrc->avg_frame_low_motion = rc->avg_frame_low_motion;
2481*77c1e3ccSAndroid Build Coastguard Worker       }
2482*77c1e3ccSAndroid Build Coastguard Worker     }
2483*77c1e3ccSAndroid Build Coastguard Worker   }
2484*77c1e3ccSAndroid Build Coastguard Worker }
2485*77c1e3ccSAndroid Build Coastguard Worker 
2486*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Encode a frame without the recode loop, usually used in one-pass
2487*77c1e3ccSAndroid Build Coastguard Worker  * encoding and realtime coding.
2488*77c1e3ccSAndroid Build Coastguard Worker  *
2489*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup high_level_algo
2490*77c1e3ccSAndroid Build Coastguard Worker  *
2491*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi             Top-level encoder structure
2492*77c1e3ccSAndroid Build Coastguard Worker  *
2493*77c1e3ccSAndroid Build Coastguard Worker  * \return Returns a value to indicate if the encoding is done successfully.
2494*77c1e3ccSAndroid Build Coastguard Worker  * \retval #AOM_CODEC_OK
2495*77c1e3ccSAndroid Build Coastguard Worker  * \retval #AOM_CODEC_ERROR
2496*77c1e3ccSAndroid Build Coastguard Worker  */
encode_without_recode(AV1_COMP * cpi)2497*77c1e3ccSAndroid Build Coastguard Worker static int encode_without_recode(AV1_COMP *cpi) {
2498*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2499*77c1e3ccSAndroid Build Coastguard Worker   const QuantizationCfg *const q_cfg = &cpi->oxcf.q_cfg;
2500*77c1e3ccSAndroid Build Coastguard Worker   SVC *const svc = &cpi->svc;
2501*77c1e3ccSAndroid Build Coastguard Worker   const int resize_pending = is_frame_resize_pending(cpi);
2502*77c1e3ccSAndroid Build Coastguard Worker   int top_index = 0, bottom_index = 0, q = 0;
2503*77c1e3ccSAndroid Build Coastguard Worker   YV12_BUFFER_CONFIG *unscaled = cpi->unscaled_source;
2504*77c1e3ccSAndroid Build Coastguard Worker   InterpFilter filter_scaler =
2505*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->use_svc ? svc->downsample_filter_type[svc->spatial_layer_id]
2506*77c1e3ccSAndroid Build Coastguard Worker                         : EIGHTTAP_SMOOTH;
2507*77c1e3ccSAndroid Build Coastguard Worker   int phase_scaler = cpi->ppi->use_svc
2508*77c1e3ccSAndroid Build Coastguard Worker                          ? svc->downsample_filter_phase[svc->spatial_layer_id]
2509*77c1e3ccSAndroid Build Coastguard Worker                          : 0;
2510*77c1e3ccSAndroid Build Coastguard Worker 
2511*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->rc.postencode_drop && allow_postencode_drop_rtc(cpi))
2512*77c1e3ccSAndroid Build Coastguard Worker     av1_save_all_coding_context(cpi);
2513*77c1e3ccSAndroid Build Coastguard Worker 
2514*77c1e3ccSAndroid Build Coastguard Worker   set_size_independent_vars(cpi);
2515*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_frame_size(cpi);
2516*77c1e3ccSAndroid Build Coastguard Worker   cm->prev_frame = get_primary_ref_frame_buf(cm);
2517*77c1e3ccSAndroid Build Coastguard Worker   av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2518*77c1e3ccSAndroid Build Coastguard Worker   av1_set_mv_search_params(cpi);
2519*77c1e3ccSAndroid Build Coastguard Worker 
2520*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.frame_number == 0 &&
2521*77c1e3ccSAndroid Build Coastguard Worker       (cpi->ppi->use_svc || cpi->oxcf.rc_cfg.drop_frames_water_mark > 0) &&
2522*77c1e3ccSAndroid Build Coastguard Worker       cpi->svc.temporal_layer_id == 0) {
2523*77c1e3ccSAndroid Build Coastguard Worker     const SequenceHeader *seq_params = cm->seq_params;
2524*77c1e3ccSAndroid Build Coastguard Worker     if (aom_alloc_frame_buffer(
2525*77c1e3ccSAndroid Build Coastguard Worker             &cpi->svc.source_last_TL0, cpi->oxcf.frm_dim_cfg.width,
2526*77c1e3ccSAndroid Build Coastguard Worker             cpi->oxcf.frm_dim_cfg.height, seq_params->subsampling_x,
2527*77c1e3ccSAndroid Build Coastguard Worker             seq_params->subsampling_y, seq_params->use_highbitdepth,
2528*77c1e3ccSAndroid Build Coastguard Worker             cpi->oxcf.border_in_pixels, cm->features.byte_alignment, false,
2529*77c1e3ccSAndroid Build Coastguard Worker             0)) {
2530*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2531*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate buffer for source_last_TL0");
2532*77c1e3ccSAndroid Build Coastguard Worker     }
2533*77c1e3ccSAndroid Build Coastguard Worker   }
2534*77c1e3ccSAndroid Build Coastguard Worker 
2535*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->ppi->use_svc) {
2536*77c1e3ccSAndroid Build Coastguard Worker     phase_scaler = 8;
2537*77c1e3ccSAndroid Build Coastguard Worker     // 2:1 scaling.
2538*77c1e3ccSAndroid Build Coastguard Worker     if ((cm->width << 1) == unscaled->y_crop_width &&
2539*77c1e3ccSAndroid Build Coastguard Worker         (cm->height << 1) == unscaled->y_crop_height) {
2540*77c1e3ccSAndroid Build Coastguard Worker       filter_scaler = BILINEAR;
2541*77c1e3ccSAndroid Build Coastguard Worker       // For lower resolutions use eighttap_smooth.
2542*77c1e3ccSAndroid Build Coastguard Worker       if (cm->width * cm->height <= 320 * 180) filter_scaler = EIGHTTAP_SMOOTH;
2543*77c1e3ccSAndroid Build Coastguard Worker     } else if ((cm->width << 2) == unscaled->y_crop_width &&
2544*77c1e3ccSAndroid Build Coastguard Worker                (cm->height << 2) == unscaled->y_crop_height) {
2545*77c1e3ccSAndroid Build Coastguard Worker       // 4:1 scaling.
2546*77c1e3ccSAndroid Build Coastguard Worker       filter_scaler = EIGHTTAP_SMOOTH;
2547*77c1e3ccSAndroid Build Coastguard Worker     } else if ((cm->width << 2) == 3 * unscaled->y_crop_width &&
2548*77c1e3ccSAndroid Build Coastguard Worker                (cm->height << 2) == 3 * unscaled->y_crop_height) {
2549*77c1e3ccSAndroid Build Coastguard Worker       // 4:3 scaling.
2550*77c1e3ccSAndroid Build Coastguard Worker       filter_scaler = EIGHTTAP_REGULAR;
2551*77c1e3ccSAndroid Build Coastguard Worker     }
2552*77c1e3ccSAndroid Build Coastguard Worker   }
2553*77c1e3ccSAndroid Build Coastguard Worker 
2554*77c1e3ccSAndroid Build Coastguard Worker   allocate_gradient_info_for_hog(cpi);
2555*77c1e3ccSAndroid Build Coastguard Worker 
2556*77c1e3ccSAndroid Build Coastguard Worker   allocate_src_var_of_4x4_sub_block_buf(cpi);
2557*77c1e3ccSAndroid Build Coastguard Worker 
2558*77c1e3ccSAndroid Build Coastguard Worker   const SPEED_FEATURES *sf = &cpi->sf;
2559*77c1e3ccSAndroid Build Coastguard Worker   if (sf->part_sf.partition_search_type == VAR_BASED_PARTITION)
2560*77c1e3ccSAndroid Build Coastguard Worker     variance_partition_alloc(cpi);
2561*77c1e3ccSAndroid Build Coastguard Worker 
2562*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.frame_type == KEY_FRAME ||
2563*77c1e3ccSAndroid Build Coastguard Worker       ((sf->inter_sf.extra_prune_warped && cpi->refresh_frame.golden_frame)))
2564*77c1e3ccSAndroid Build Coastguard Worker     copy_frame_prob_info(cpi);
2565*77c1e3ccSAndroid Build Coastguard Worker 
2566*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2567*77c1e3ccSAndroid Build Coastguard Worker   printf("\n Encoding a frame: \n");
2568*77c1e3ccSAndroid Build Coastguard Worker #endif
2569*77c1e3ccSAndroid Build Coastguard Worker 
2570*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_BUTTERAUGLI
2571*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) {
2572*77c1e3ccSAndroid Build Coastguard Worker     av1_setup_butteraugli_rdmult(cpi);
2573*77c1e3ccSAndroid Build Coastguard Worker   }
2574*77c1e3ccSAndroid Build Coastguard Worker #endif
2575*77c1e3ccSAndroid Build Coastguard Worker 
2576*77c1e3ccSAndroid Build Coastguard Worker   cpi->source = av1_realloc_and_scale_if_required(
2577*77c1e3ccSAndroid Build Coastguard Worker       cm, unscaled, &cpi->scaled_source, filter_scaler, phase_scaler, true,
2578*77c1e3ccSAndroid Build Coastguard Worker       false, cpi->oxcf.border_in_pixels, cpi->alloc_pyramid);
2579*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm) || resize_pending != 0) {
2580*77c1e3ccSAndroid Build Coastguard Worker     const int current_size =
2581*77c1e3ccSAndroid Build Coastguard Worker         (cm->mi_params.mi_rows * cm->mi_params.mi_cols) >> 2;
2582*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->consec_zero_mv &&
2583*77c1e3ccSAndroid Build Coastguard Worker         (cpi->consec_zero_mv_alloc_size < current_size)) {
2584*77c1e3ccSAndroid Build Coastguard Worker       aom_free(cpi->consec_zero_mv);
2585*77c1e3ccSAndroid Build Coastguard Worker       cpi->consec_zero_mv_alloc_size = 0;
2586*77c1e3ccSAndroid Build Coastguard Worker       CHECK_MEM_ERROR(cm, cpi->consec_zero_mv,
2587*77c1e3ccSAndroid Build Coastguard Worker                       aom_malloc(current_size * sizeof(*cpi->consec_zero_mv)));
2588*77c1e3ccSAndroid Build Coastguard Worker       cpi->consec_zero_mv_alloc_size = current_size;
2589*77c1e3ccSAndroid Build Coastguard Worker     }
2590*77c1e3ccSAndroid Build Coastguard Worker     assert(cpi->consec_zero_mv != NULL);
2591*77c1e3ccSAndroid Build Coastguard Worker     memset(cpi->consec_zero_mv, 0, current_size * sizeof(*cpi->consec_zero_mv));
2592*77c1e3ccSAndroid Build Coastguard Worker   }
2593*77c1e3ccSAndroid Build Coastguard Worker 
2594*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->scaled_last_source_available) {
2595*77c1e3ccSAndroid Build Coastguard Worker     cpi->last_source = &cpi->scaled_last_source;
2596*77c1e3ccSAndroid Build Coastguard Worker     cpi->scaled_last_source_available = 0;
2597*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->unscaled_last_source != NULL) {
2598*77c1e3ccSAndroid Build Coastguard Worker     cpi->last_source = av1_realloc_and_scale_if_required(
2599*77c1e3ccSAndroid Build Coastguard Worker         cm, cpi->unscaled_last_source, &cpi->scaled_last_source, filter_scaler,
2600*77c1e3ccSAndroid Build Coastguard Worker         phase_scaler, true, false, cpi->oxcf.border_in_pixels,
2601*77c1e3ccSAndroid Build Coastguard Worker         cpi->alloc_pyramid);
2602*77c1e3ccSAndroid Build Coastguard Worker   }
2603*77c1e3ccSAndroid Build Coastguard Worker 
2604*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.rt_sf.use_temporal_noise_estimate) {
2605*77c1e3ccSAndroid Build Coastguard Worker     av1_update_noise_estimate(cpi);
2606*77c1e3ccSAndroid Build Coastguard Worker   }
2607*77c1e3ccSAndroid Build Coastguard Worker 
2608*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
2609*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.noise_sensitivity > 0 && cpi->ppi->use_svc)
2610*77c1e3ccSAndroid Build Coastguard Worker     av1_denoiser_reset_on_first_frame(cpi);
2611*77c1e3ccSAndroid Build Coastguard Worker #endif
2612*77c1e3ccSAndroid Build Coastguard Worker 
2613*77c1e3ccSAndroid Build Coastguard Worker   // For 1 spatial layer encoding: if the (non-LAST) reference has different
2614*77c1e3ccSAndroid Build Coastguard Worker   // resolution from the source then disable that reference. This is to avoid
2615*77c1e3ccSAndroid Build Coastguard Worker   // significant increase in encode time from scaling the references in
2616*77c1e3ccSAndroid Build Coastguard Worker   // av1_scale_references. Note GOLDEN is forced to update on the (first/tigger)
2617*77c1e3ccSAndroid Build Coastguard Worker   // resized frame and ALTREF will be refreshed ~4 frames later, so both
2618*77c1e3ccSAndroid Build Coastguard Worker   // references become available again after few frames.
2619*77c1e3ccSAndroid Build Coastguard Worker   // For superres: don't disable golden reference.
2620*77c1e3ccSAndroid Build Coastguard Worker   if (svc->number_spatial_layers == 1) {
2621*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->oxcf.superres_cfg.enable_superres) {
2622*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->ref_frame_flags & av1_ref_frame_flag_list[GOLDEN_FRAME]) {
2623*77c1e3ccSAndroid Build Coastguard Worker         const YV12_BUFFER_CONFIG *const ref =
2624*77c1e3ccSAndroid Build Coastguard Worker             get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
2625*77c1e3ccSAndroid Build Coastguard Worker         if (ref == NULL || ref->y_crop_width != cm->width ||
2626*77c1e3ccSAndroid Build Coastguard Worker             ref->y_crop_height != cm->height) {
2627*77c1e3ccSAndroid Build Coastguard Worker           cpi->ref_frame_flags ^= AOM_GOLD_FLAG;
2628*77c1e3ccSAndroid Build Coastguard Worker         }
2629*77c1e3ccSAndroid Build Coastguard Worker       }
2630*77c1e3ccSAndroid Build Coastguard Worker     }
2631*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ALTREF_FRAME]) {
2632*77c1e3ccSAndroid Build Coastguard Worker       const YV12_BUFFER_CONFIG *const ref =
2633*77c1e3ccSAndroid Build Coastguard Worker           get_ref_frame_yv12_buf(cm, ALTREF_FRAME);
2634*77c1e3ccSAndroid Build Coastguard Worker       if (ref == NULL || ref->y_crop_width != cm->width ||
2635*77c1e3ccSAndroid Build Coastguard Worker           ref->y_crop_height != cm->height) {
2636*77c1e3ccSAndroid Build Coastguard Worker         cpi->ref_frame_flags ^= AOM_ALT_FLAG;
2637*77c1e3ccSAndroid Build Coastguard Worker       }
2638*77c1e3ccSAndroid Build Coastguard Worker     }
2639*77c1e3ccSAndroid Build Coastguard Worker   }
2640*77c1e3ccSAndroid Build Coastguard Worker 
2641*77c1e3ccSAndroid Build Coastguard Worker   int scale_references = 0;
2642*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2643*77c1e3ccSAndroid Build Coastguard Worker   scale_references =
2644*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE ? 1 : 0;
2645*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_FPMT_TEST
2646*77c1e3ccSAndroid Build Coastguard Worker   if (scale_references ||
2647*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) {
2648*77c1e3ccSAndroid Build Coastguard Worker     if (!frame_is_intra_only(cm)) {
2649*77c1e3ccSAndroid Build Coastguard Worker       av1_scale_references(cpi, filter_scaler, phase_scaler, 1);
2650*77c1e3ccSAndroid Build Coastguard Worker     }
2651*77c1e3ccSAndroid Build Coastguard Worker   }
2652*77c1e3ccSAndroid Build Coastguard Worker 
2653*77c1e3ccSAndroid Build Coastguard Worker   av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2654*77c1e3ccSAndroid Build Coastguard Worker                     q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq);
2655*77c1e3ccSAndroid Build Coastguard Worker   av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
2656*77c1e3ccSAndroid Build Coastguard Worker   av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
2657*77c1e3ccSAndroid Build Coastguard Worker                      cm->seq_params->bit_depth);
2658*77c1e3ccSAndroid Build Coastguard Worker   av1_set_variance_partition_thresholds(cpi, q, 0);
2659*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_frame(cpi);
2660*77c1e3ccSAndroid Build Coastguard Worker 
2661*77c1e3ccSAndroid Build Coastguard Worker   // Check if this high_source_sad (scene/slide change) frame should be
2662*77c1e3ccSAndroid Build Coastguard Worker   // encoded at high/max QP, and if so, set the q and adjust some rate
2663*77c1e3ccSAndroid Build Coastguard Worker   // control parameters.
2664*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.rt_sf.overshoot_detection_cbr == FAST_DETECTION_MAXQ &&
2665*77c1e3ccSAndroid Build Coastguard Worker       cpi->rc.high_source_sad) {
2666*77c1e3ccSAndroid Build Coastguard Worker     if (av1_encodedframe_overshoot_cbr(cpi, &q)) {
2667*77c1e3ccSAndroid Build Coastguard Worker       av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2668*77c1e3ccSAndroid Build Coastguard Worker                         q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq);
2669*77c1e3ccSAndroid Build Coastguard Worker       av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
2670*77c1e3ccSAndroid Build Coastguard Worker       av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
2671*77c1e3ccSAndroid Build Coastguard Worker                          cm->seq_params->bit_depth);
2672*77c1e3ccSAndroid Build Coastguard Worker       av1_set_variance_partition_thresholds(cpi, q, 0);
2673*77c1e3ccSAndroid Build Coastguard Worker       if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
2674*77c1e3ccSAndroid Build Coastguard Worker           cm->features.primary_ref_frame == PRIMARY_REF_NONE)
2675*77c1e3ccSAndroid Build Coastguard Worker         av1_setup_frame(cpi);
2676*77c1e3ccSAndroid Build Coastguard Worker     }
2677*77c1e3ccSAndroid Build Coastguard Worker   }
2678*77c1e3ccSAndroid Build Coastguard Worker   av1_apply_active_map(cpi);
2679*77c1e3ccSAndroid Build Coastguard Worker   if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ) av1_cyclic_refresh_setup(cpi);
2680*77c1e3ccSAndroid Build Coastguard Worker   if (cm->seg.enabled) {
2681*77c1e3ccSAndroid Build Coastguard Worker     if (!cm->seg.update_data && cm->prev_frame) {
2682*77c1e3ccSAndroid Build Coastguard Worker       segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
2683*77c1e3ccSAndroid Build Coastguard Worker       cm->seg.enabled = cm->prev_frame->seg.enabled;
2684*77c1e3ccSAndroid Build Coastguard Worker     } else {
2685*77c1e3ccSAndroid Build Coastguard Worker       av1_calculate_segdata(&cm->seg);
2686*77c1e3ccSAndroid Build Coastguard Worker     }
2687*77c1e3ccSAndroid Build Coastguard Worker   } else {
2688*77c1e3ccSAndroid Build Coastguard Worker     memset(&cm->seg, 0, sizeof(cm->seg));
2689*77c1e3ccSAndroid Build Coastguard Worker   }
2690*77c1e3ccSAndroid Build Coastguard Worker   segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
2691*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->seg.enabled = cm->seg.enabled;
2692*77c1e3ccSAndroid Build Coastguard Worker 
2693*77c1e3ccSAndroid Build Coastguard Worker   // This is for rtc temporal filtering case.
2694*77c1e3ccSAndroid Build Coastguard Worker   if (is_psnr_calc_enabled(cpi) && cpi->sf.rt_sf.use_rtc_tf) {
2695*77c1e3ccSAndroid Build Coastguard Worker     const SequenceHeader *seq_params = cm->seq_params;
2696*77c1e3ccSAndroid Build Coastguard Worker 
2697*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->orig_source.buffer_alloc_sz == 0 ||
2698*77c1e3ccSAndroid Build Coastguard Worker         cpi->rc.prev_coded_width != cpi->oxcf.frm_dim_cfg.width ||
2699*77c1e3ccSAndroid Build Coastguard Worker         cpi->rc.prev_coded_height != cpi->oxcf.frm_dim_cfg.height) {
2700*77c1e3ccSAndroid Build Coastguard Worker       // Allocate a source buffer to store the true source for psnr calculation.
2701*77c1e3ccSAndroid Build Coastguard Worker       if (aom_alloc_frame_buffer(
2702*77c1e3ccSAndroid Build Coastguard Worker               &cpi->orig_source, cpi->oxcf.frm_dim_cfg.width,
2703*77c1e3ccSAndroid Build Coastguard Worker               cpi->oxcf.frm_dim_cfg.height, seq_params->subsampling_x,
2704*77c1e3ccSAndroid Build Coastguard Worker               seq_params->subsampling_y, seq_params->use_highbitdepth,
2705*77c1e3ccSAndroid Build Coastguard Worker               cpi->oxcf.border_in_pixels, cm->features.byte_alignment, false,
2706*77c1e3ccSAndroid Build Coastguard Worker               0))
2707*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2708*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to allocate scaled buffer");
2709*77c1e3ccSAndroid Build Coastguard Worker     }
2710*77c1e3ccSAndroid Build Coastguard Worker 
2711*77c1e3ccSAndroid Build Coastguard Worker     aom_yv12_copy_y(cpi->source, &cpi->orig_source, 1);
2712*77c1e3ccSAndroid Build Coastguard Worker     aom_yv12_copy_u(cpi->source, &cpi->orig_source, 1);
2713*77c1e3ccSAndroid Build Coastguard Worker     aom_yv12_copy_v(cpi->source, &cpi->orig_source, 1);
2714*77c1e3ccSAndroid Build Coastguard Worker   }
2715*77c1e3ccSAndroid Build Coastguard Worker 
2716*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2717*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, av1_encode_frame_time);
2718*77c1e3ccSAndroid Build Coastguard Worker #endif
2719*77c1e3ccSAndroid Build Coastguard Worker 
2720*77c1e3ccSAndroid Build Coastguard Worker   // Set the motion vector precision based on mv stats from the last coded
2721*77c1e3ccSAndroid Build Coastguard Worker   // frame.
2722*77c1e3ccSAndroid Build Coastguard Worker   if (!frame_is_intra_only(cm)) av1_pick_and_set_high_precision_mv(cpi, q);
2723*77c1e3ccSAndroid Build Coastguard Worker 
2724*77c1e3ccSAndroid Build Coastguard Worker   // transform / motion compensation build reconstruction frame
2725*77c1e3ccSAndroid Build Coastguard Worker   av1_encode_frame(cpi);
2726*77c1e3ccSAndroid Build Coastguard Worker 
2727*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->rc.rtc_external_ratectrl && !frame_is_intra_only(cm))
2728*77c1e3ccSAndroid Build Coastguard Worker     update_motion_stat(cpi);
2729*77c1e3ccSAndroid Build Coastguard Worker 
2730*77c1e3ccSAndroid Build Coastguard Worker   // Adjust the refresh of the golden (longer-term) reference based on QP
2731*77c1e3ccSAndroid Build Coastguard Worker   // selected for this frame. This is for CBR real-time mode, and only
2732*77c1e3ccSAndroid Build Coastguard Worker   // for single layer without usage of the set_ref_frame_config (so
2733*77c1e3ccSAndroid Build Coastguard Worker   // reference structure for 1 layer is set internally).
2734*77c1e3ccSAndroid Build Coastguard Worker   if (!frame_is_intra_only(cm) && cpi->oxcf.rc_cfg.mode == AOM_CBR &&
2735*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.mode == REALTIME && svc->number_spatial_layers == 1 &&
2736*77c1e3ccSAndroid Build Coastguard Worker       svc->number_temporal_layers == 1 && !cpi->rc.rtc_external_ratectrl &&
2737*77c1e3ccSAndroid Build Coastguard Worker       !cpi->ppi->rtc_ref.set_ref_frame_config &&
2738*77c1e3ccSAndroid Build Coastguard Worker       sf->rt_sf.gf_refresh_based_on_qp)
2739*77c1e3ccSAndroid Build Coastguard Worker     av1_adjust_gf_refresh_qp_one_pass_rt(cpi);
2740*77c1e3ccSAndroid Build Coastguard Worker 
2741*77c1e3ccSAndroid Build Coastguard Worker   // For non-svc: if scaling is required, copy scaled_source
2742*77c1e3ccSAndroid Build Coastguard Worker   // into scaled_last_source.
2743*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.frame_number > 1 && !cpi->ppi->use_svc &&
2744*77c1e3ccSAndroid Build Coastguard Worker       cpi->scaled_source.y_buffer != NULL &&
2745*77c1e3ccSAndroid Build Coastguard Worker       cpi->scaled_last_source.y_buffer != NULL &&
2746*77c1e3ccSAndroid Build Coastguard Worker       cpi->scaled_source.y_crop_width == cpi->scaled_last_source.y_crop_width &&
2747*77c1e3ccSAndroid Build Coastguard Worker       cpi->scaled_source.y_crop_height ==
2748*77c1e3ccSAndroid Build Coastguard Worker           cpi->scaled_last_source.y_crop_height &&
2749*77c1e3ccSAndroid Build Coastguard Worker       (cm->width != cpi->unscaled_source->y_crop_width ||
2750*77c1e3ccSAndroid Build Coastguard Worker        cm->height != cpi->unscaled_source->y_crop_height)) {
2751*77c1e3ccSAndroid Build Coastguard Worker     cpi->scaled_last_source_available = 1;
2752*77c1e3ccSAndroid Build Coastguard Worker     aom_yv12_copy_y(&cpi->scaled_source, &cpi->scaled_last_source, 1);
2753*77c1e3ccSAndroid Build Coastguard Worker     aom_yv12_copy_u(&cpi->scaled_source, &cpi->scaled_last_source, 1);
2754*77c1e3ccSAndroid Build Coastguard Worker     aom_yv12_copy_v(&cpi->scaled_source, &cpi->scaled_last_source, 1);
2755*77c1e3ccSAndroid Build Coastguard Worker   }
2756*77c1e3ccSAndroid Build Coastguard Worker 
2757*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2758*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, av1_encode_frame_time);
2759*77c1e3ccSAndroid Build Coastguard Worker #endif
2760*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
2761*77c1e3ccSAndroid Build Coastguard Worker   ++cpi->frame_recode_hits;
2762*77c1e3ccSAndroid Build Coastguard Worker #endif
2763*77c1e3ccSAndroid Build Coastguard Worker 
2764*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
2765*77c1e3ccSAndroid Build Coastguard Worker }
2766*77c1e3ccSAndroid Build Coastguard Worker 
2767*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
2768*77c1e3ccSAndroid Build Coastguard Worker 
2769*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Recode loop for encoding one frame. the purpose of encoding one frame
2770*77c1e3ccSAndroid Build Coastguard Worker  * for multiple times can be approaching a target bitrate or adjusting the usage
2771*77c1e3ccSAndroid Build Coastguard Worker  * of global motions.
2772*77c1e3ccSAndroid Build Coastguard Worker  *
2773*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup high_level_algo
2774*77c1e3ccSAndroid Build Coastguard Worker  *
2775*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi             Top-level encoder structure
2776*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    size            Bitstream size
2777*77c1e3ccSAndroid Build Coastguard Worker  * \param[out]   dest            Bitstream output buffer
2778*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    dest_size       Bitstream output buffer size
2779*77c1e3ccSAndroid Build Coastguard Worker  *
2780*77c1e3ccSAndroid Build Coastguard Worker  * \return Returns a value to indicate if the encoding is done successfully.
2781*77c1e3ccSAndroid Build Coastguard Worker  * \retval #AOM_CODEC_OK
2782*77c1e3ccSAndroid Build Coastguard Worker  * \retval -1
2783*77c1e3ccSAndroid Build Coastguard Worker  * \retval #AOM_CODEC_ERROR
2784*77c1e3ccSAndroid Build Coastguard Worker  */
encode_with_recode_loop(AV1_COMP * cpi,size_t * size,uint8_t * dest,size_t dest_size)2785*77c1e3ccSAndroid Build Coastguard Worker static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest,
2786*77c1e3ccSAndroid Build Coastguard Worker                                    size_t dest_size) {
2787*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2788*77c1e3ccSAndroid Build Coastguard Worker   RATE_CONTROL *const rc = &cpi->rc;
2789*77c1e3ccSAndroid Build Coastguard Worker   GlobalMotionInfo *const gm_info = &cpi->gm_info;
2790*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2791*77c1e3ccSAndroid Build Coastguard Worker   const QuantizationCfg *const q_cfg = &oxcf->q_cfg;
2792*77c1e3ccSAndroid Build Coastguard Worker   const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
2793*77c1e3ccSAndroid Build Coastguard Worker   // Must allow recode if minimum compression ratio is set.
2794*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(oxcf->rc_cfg.min_cr > 0, allow_recode));
2795*77c1e3ccSAndroid Build Coastguard Worker 
2796*77c1e3ccSAndroid Build Coastguard Worker   set_size_independent_vars(cpi);
2797*77c1e3ccSAndroid Build Coastguard Worker   if (is_stat_consumption_stage_twopass(cpi) &&
2798*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.interp_sf.adaptive_interp_filter_search)
2799*77c1e3ccSAndroid Build Coastguard Worker     cpi->interp_search_flags.interp_filter_search_mask =
2800*77c1e3ccSAndroid Build Coastguard Worker         av1_setup_interp_filter_search_mask(cpi);
2801*77c1e3ccSAndroid Build Coastguard Worker 
2802*77c1e3ccSAndroid Build Coastguard Worker   av1_setup_frame_size(cpi);
2803*77c1e3ccSAndroid Build Coastguard Worker 
2804*77c1e3ccSAndroid Build Coastguard Worker   if (av1_superres_in_recode_allowed(cpi) &&
2805*77c1e3ccSAndroid Build Coastguard Worker       cpi->superres_mode != AOM_SUPERRES_NONE &&
2806*77c1e3ccSAndroid Build Coastguard Worker       cm->superres_scale_denominator == SCALE_NUMERATOR) {
2807*77c1e3ccSAndroid Build Coastguard Worker     // Superres mode is currently enabled, but the denominator selected will
2808*77c1e3ccSAndroid Build Coastguard Worker     // disable superres. So no need to continue, as we will go through another
2809*77c1e3ccSAndroid Build Coastguard Worker     // recode loop for full-resolution after this anyway.
2810*77c1e3ccSAndroid Build Coastguard Worker     return -1;
2811*77c1e3ccSAndroid Build Coastguard Worker   }
2812*77c1e3ccSAndroid Build Coastguard Worker 
2813*77c1e3ccSAndroid Build Coastguard Worker   int top_index = 0, bottom_index = 0;
2814*77c1e3ccSAndroid Build Coastguard Worker   int q = 0, q_low = 0, q_high = 0;
2815*77c1e3ccSAndroid Build Coastguard Worker   av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2816*77c1e3ccSAndroid Build Coastguard Worker   q_low = bottom_index;
2817*77c1e3ccSAndroid Build Coastguard Worker   q_high = top_index;
2818*77c1e3ccSAndroid Build Coastguard Worker 
2819*77c1e3ccSAndroid Build Coastguard Worker   av1_set_mv_search_params(cpi);
2820*77c1e3ccSAndroid Build Coastguard Worker 
2821*77c1e3ccSAndroid Build Coastguard Worker   allocate_gradient_info_for_hog(cpi);
2822*77c1e3ccSAndroid Build Coastguard Worker 
2823*77c1e3ccSAndroid Build Coastguard Worker   allocate_src_var_of_4x4_sub_block_buf(cpi);
2824*77c1e3ccSAndroid Build Coastguard Worker 
2825*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION)
2826*77c1e3ccSAndroid Build Coastguard Worker     variance_partition_alloc(cpi);
2827*77c1e3ccSAndroid Build Coastguard Worker 
2828*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi);
2829*77c1e3ccSAndroid Build Coastguard Worker 
2830*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
2831*77c1e3ccSAndroid Build Coastguard Worker   printf("\n Encoding a frame: \n");
2832*77c1e3ccSAndroid Build Coastguard Worker #endif
2833*77c1e3ccSAndroid Build Coastguard Worker 
2834*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_RD_COMMAND
2835*77c1e3ccSAndroid Build Coastguard Worker   // Determine whether to use screen content tools using two fast encoding.
2836*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->sf.hl_sf.disable_extra_sc_testing && !cpi->use_ducky_encode)
2837*77c1e3ccSAndroid Build Coastguard Worker     av1_determine_sc_tools_with_encoding(cpi, q);
2838*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_RD_COMMAND
2839*77c1e3ccSAndroid Build Coastguard Worker 
2840*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
2841*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2842*77c1e3ccSAndroid Build Coastguard Worker     av1_vmaf_neg_preprocessing(cpi, cpi->unscaled_source);
2843*77c1e3ccSAndroid Build Coastguard Worker   }
2844*77c1e3ccSAndroid Build Coastguard Worker #endif
2845*77c1e3ccSAndroid Build Coastguard Worker 
2846*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_BUTTERAUGLI
2847*77c1e3ccSAndroid Build Coastguard Worker   cpi->butteraugli_info.recon_set = false;
2848*77c1e3ccSAndroid Build Coastguard Worker   int original_q = 0;
2849*77c1e3ccSAndroid Build Coastguard Worker #endif
2850*77c1e3ccSAndroid Build Coastguard Worker 
2851*77c1e3ccSAndroid Build Coastguard Worker   cpi->num_frame_recode = 0;
2852*77c1e3ccSAndroid Build Coastguard Worker 
2853*77c1e3ccSAndroid Build Coastguard Worker   // Loop variables
2854*77c1e3ccSAndroid Build Coastguard Worker   int loop = 0;
2855*77c1e3ccSAndroid Build Coastguard Worker   int loop_count = 0;
2856*77c1e3ccSAndroid Build Coastguard Worker   int overshoot_seen = 0;
2857*77c1e3ccSAndroid Build Coastguard Worker   int undershoot_seen = 0;
2858*77c1e3ccSAndroid Build Coastguard Worker   int low_cr_seen = 0;
2859*77c1e3ccSAndroid Build Coastguard Worker   int last_loop_allow_hp = 0;
2860*77c1e3ccSAndroid Build Coastguard Worker 
2861*77c1e3ccSAndroid Build Coastguard Worker   do {
2862*77c1e3ccSAndroid Build Coastguard Worker     loop = 0;
2863*77c1e3ccSAndroid Build Coastguard Worker     int do_mv_stats_collection = 1;
2864*77c1e3ccSAndroid Build Coastguard Worker 
2865*77c1e3ccSAndroid Build Coastguard Worker     // if frame was scaled calculate global_motion_search again if already
2866*77c1e3ccSAndroid Build Coastguard Worker     // done
2867*77c1e3ccSAndroid Build Coastguard Worker     if (loop_count > 0 && cpi->source && gm_info->search_done) {
2868*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->source->y_crop_width != cm->width ||
2869*77c1e3ccSAndroid Build Coastguard Worker           cpi->source->y_crop_height != cm->height) {
2870*77c1e3ccSAndroid Build Coastguard Worker         gm_info->search_done = 0;
2871*77c1e3ccSAndroid Build Coastguard Worker       }
2872*77c1e3ccSAndroid Build Coastguard Worker     }
2873*77c1e3ccSAndroid Build Coastguard Worker     cpi->source = av1_realloc_and_scale_if_required(
2874*77c1e3ccSAndroid Build Coastguard Worker         cm, cpi->unscaled_source, &cpi->scaled_source, EIGHTTAP_REGULAR, 0,
2875*77c1e3ccSAndroid Build Coastguard Worker         false, false, cpi->oxcf.border_in_pixels, cpi->alloc_pyramid);
2876*77c1e3ccSAndroid Build Coastguard Worker 
2877*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_BUTTERAUGLI
2878*77c1e3ccSAndroid Build Coastguard Worker     if (oxcf->tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) {
2879*77c1e3ccSAndroid Build Coastguard Worker       if (loop_count == 0) {
2880*77c1e3ccSAndroid Build Coastguard Worker         original_q = q;
2881*77c1e3ccSAndroid Build Coastguard Worker         // TODO(sdeng): different q here does not make big difference. Use a
2882*77c1e3ccSAndroid Build Coastguard Worker         // faster pass instead.
2883*77c1e3ccSAndroid Build Coastguard Worker         q = 96;
2884*77c1e3ccSAndroid Build Coastguard Worker         av1_setup_butteraugli_source(cpi);
2885*77c1e3ccSAndroid Build Coastguard Worker       } else {
2886*77c1e3ccSAndroid Build Coastguard Worker         q = original_q;
2887*77c1e3ccSAndroid Build Coastguard Worker       }
2888*77c1e3ccSAndroid Build Coastguard Worker     }
2889*77c1e3ccSAndroid Build Coastguard Worker #endif
2890*77c1e3ccSAndroid Build Coastguard Worker 
2891*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->unscaled_last_source != NULL) {
2892*77c1e3ccSAndroid Build Coastguard Worker       cpi->last_source = av1_realloc_and_scale_if_required(
2893*77c1e3ccSAndroid Build Coastguard Worker           cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
2894*77c1e3ccSAndroid Build Coastguard Worker           EIGHTTAP_REGULAR, 0, false, false, cpi->oxcf.border_in_pixels,
2895*77c1e3ccSAndroid Build Coastguard Worker           cpi->alloc_pyramid);
2896*77c1e3ccSAndroid Build Coastguard Worker     }
2897*77c1e3ccSAndroid Build Coastguard Worker 
2898*77c1e3ccSAndroid Build Coastguard Worker     int scale_references = 0;
2899*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
2900*77c1e3ccSAndroid Build Coastguard Worker     scale_references =
2901*77c1e3ccSAndroid Build Coastguard Worker         cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE ? 1 : 0;
2902*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_FPMT_TEST
2903*77c1e3ccSAndroid Build Coastguard Worker     if (scale_references ||
2904*77c1e3ccSAndroid Build Coastguard Worker         cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) {
2905*77c1e3ccSAndroid Build Coastguard Worker       if (!frame_is_intra_only(cm)) {
2906*77c1e3ccSAndroid Build Coastguard Worker         if (loop_count > 0) {
2907*77c1e3ccSAndroid Build Coastguard Worker           release_scaled_references(cpi);
2908*77c1e3ccSAndroid Build Coastguard Worker         }
2909*77c1e3ccSAndroid Build Coastguard Worker         av1_scale_references(cpi, EIGHTTAP_REGULAR, 0, 0);
2910*77c1e3ccSAndroid Build Coastguard Worker       }
2911*77c1e3ccSAndroid Build Coastguard Worker     }
2912*77c1e3ccSAndroid Build Coastguard Worker 
2913*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
2914*77c1e3ccSAndroid Build Coastguard Worker     if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
2915*77c1e3ccSAndroid Build Coastguard Worker         oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2916*77c1e3ccSAndroid Build Coastguard Worker       cpi->vmaf_info.original_qindex = q;
2917*77c1e3ccSAndroid Build Coastguard Worker       q = av1_get_vmaf_base_qindex(cpi, q);
2918*77c1e3ccSAndroid Build Coastguard Worker     }
2919*77c1e3ccSAndroid Build Coastguard Worker #endif
2920*77c1e3ccSAndroid Build Coastguard Worker 
2921*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_COMMAND
2922*77c1e3ccSAndroid Build Coastguard Worker     RD_COMMAND *rd_command = &cpi->rd_command;
2923*77c1e3ccSAndroid Build Coastguard Worker     RD_OPTION option = rd_command->option_ls[rd_command->frame_index];
2924*77c1e3ccSAndroid Build Coastguard Worker     if (option == RD_OPTION_SET_Q || option == RD_OPTION_SET_Q_RDMULT) {
2925*77c1e3ccSAndroid Build Coastguard Worker       q = rd_command->q_index_ls[rd_command->frame_index];
2926*77c1e3ccSAndroid Build Coastguard Worker     }
2927*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_RD_COMMAND
2928*77c1e3ccSAndroid Build Coastguard Worker 
2929*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY
2930*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS
2931*77c1e3ccSAndroid Build Coastguard Worker     if (oxcf->pass == AOM_RC_THIRD_PASS && cpi->vbr_rc_info.ready == 1) {
2932*77c1e3ccSAndroid Build Coastguard Worker       int frame_coding_idx =
2933*77c1e3ccSAndroid Build Coastguard Worker           av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, cpi->gf_frame_index);
2934*77c1e3ccSAndroid Build Coastguard Worker       if (frame_coding_idx < cpi->vbr_rc_info.total_frame_count) {
2935*77c1e3ccSAndroid Build Coastguard Worker         q = cpi->vbr_rc_info.q_index_list[frame_coding_idx];
2936*77c1e3ccSAndroid Build Coastguard Worker       } else {
2937*77c1e3ccSAndroid Build Coastguard Worker         // TODO(angiebird): Investigate why sometimes there is an extra frame
2938*77c1e3ccSAndroid Build Coastguard Worker         // after the last GOP.
2939*77c1e3ccSAndroid Build Coastguard Worker         q = cpi->vbr_rc_info.base_q_index;
2940*77c1e3ccSAndroid Build Coastguard Worker       }
2941*77c1e3ccSAndroid Build Coastguard Worker     }
2942*77c1e3ccSAndroid Build Coastguard Worker #else
2943*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->vbr_rc_info.q_index_list_ready) {
2944*77c1e3ccSAndroid Build Coastguard Worker       q = cpi->vbr_rc_info.q_index_list[cpi->gf_frame_index];
2945*77c1e3ccSAndroid Build Coastguard Worker     }
2946*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_THREE_PASS
2947*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_BITRATE_ACCURACY
2948*77c1e3ccSAndroid Build Coastguard Worker 
2949*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY
2950*77c1e3ccSAndroid Build Coastguard Worker     // TODO(angiebird): Move this into a function.
2951*77c1e3ccSAndroid Build Coastguard Worker     if (oxcf->pass == AOM_RC_THIRD_PASS) {
2952*77c1e3ccSAndroid Build Coastguard Worker       int frame_coding_idx =
2953*77c1e3ccSAndroid Build Coastguard Worker           av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, cpi->gf_frame_index);
2954*77c1e3ccSAndroid Build Coastguard Worker       double qstep_ratio = cpi->vbr_rc_info.qstep_ratio_list[frame_coding_idx];
2955*77c1e3ccSAndroid Build Coastguard Worker       FRAME_UPDATE_TYPE update_type =
2956*77c1e3ccSAndroid Build Coastguard Worker           cpi->vbr_rc_info.update_type_list[frame_coding_idx];
2957*77c1e3ccSAndroid Build Coastguard Worker       rc_log_frame_encode_param(&cpi->rc_log, frame_coding_idx, qstep_ratio, q,
2958*77c1e3ccSAndroid Build Coastguard Worker                                 update_type);
2959*77c1e3ccSAndroid Build Coastguard Worker     }
2960*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY
2961*77c1e3ccSAndroid Build Coastguard Worker 
2962*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->use_ducky_encode) {
2963*77c1e3ccSAndroid Build Coastguard Worker       const DuckyEncodeFrameInfo *frame_info =
2964*77c1e3ccSAndroid Build Coastguard Worker           &cpi->ducky_encode_info.frame_info;
2965*77c1e3ccSAndroid Build Coastguard Worker       if (frame_info->qp_mode == DUCKY_ENCODE_FRAME_MODE_QINDEX) {
2966*77c1e3ccSAndroid Build Coastguard Worker         q = frame_info->q_index;
2967*77c1e3ccSAndroid Build Coastguard Worker         cm->delta_q_info.delta_q_present_flag = frame_info->delta_q_enabled;
2968*77c1e3ccSAndroid Build Coastguard Worker       }
2969*77c1e3ccSAndroid Build Coastguard Worker     }
2970*77c1e3ccSAndroid Build Coastguard Worker 
2971*77c1e3ccSAndroid Build Coastguard Worker     av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2972*77c1e3ccSAndroid Build Coastguard Worker                       q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq);
2973*77c1e3ccSAndroid Build Coastguard Worker     av1_set_speed_features_qindex_dependent(cpi, oxcf->speed);
2974*77c1e3ccSAndroid Build Coastguard Worker     av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
2975*77c1e3ccSAndroid Build Coastguard Worker                        cm->seq_params->bit_depth);
2976*77c1e3ccSAndroid Build Coastguard Worker 
2977*77c1e3ccSAndroid Build Coastguard Worker     av1_set_variance_partition_thresholds(cpi, q, 0);
2978*77c1e3ccSAndroid Build Coastguard Worker 
2979*77c1e3ccSAndroid Build Coastguard Worker     // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
2980*77c1e3ccSAndroid Build Coastguard Worker     //        cm->current_frame.frame_number, cm->show_frame, q,
2981*77c1e3ccSAndroid Build Coastguard Worker     //        cm->current_frame.frame_type, cm->superres_scale_denominator);
2982*77c1e3ccSAndroid Build Coastguard Worker 
2983*77c1e3ccSAndroid Build Coastguard Worker     if (loop_count == 0) {
2984*77c1e3ccSAndroid Build Coastguard Worker       av1_setup_frame(cpi);
2985*77c1e3ccSAndroid Build Coastguard Worker     } else if (get_primary_ref_frame_buf(cm) == NULL) {
2986*77c1e3ccSAndroid Build Coastguard Worker       // Base q-index may have changed, so we need to assign proper default coef
2987*77c1e3ccSAndroid Build Coastguard Worker       // probs before every iteration.
2988*77c1e3ccSAndroid Build Coastguard Worker       av1_default_coef_probs(cm);
2989*77c1e3ccSAndroid Build Coastguard Worker       av1_setup_frame_contexts(cm);
2990*77c1e3ccSAndroid Build Coastguard Worker     }
2991*77c1e3ccSAndroid Build Coastguard Worker 
2992*77c1e3ccSAndroid Build Coastguard Worker     if (q_cfg->aq_mode == VARIANCE_AQ) {
2993*77c1e3ccSAndroid Build Coastguard Worker       av1_vaq_frame_setup(cpi);
2994*77c1e3ccSAndroid Build Coastguard Worker     } else if (q_cfg->aq_mode == COMPLEXITY_AQ) {
2995*77c1e3ccSAndroid Build Coastguard Worker       av1_setup_in_frame_q_adj(cpi);
2996*77c1e3ccSAndroid Build Coastguard Worker     }
2997*77c1e3ccSAndroid Build Coastguard Worker 
2998*77c1e3ccSAndroid Build Coastguard Worker     if (cm->seg.enabled) {
2999*77c1e3ccSAndroid Build Coastguard Worker       if (!cm->seg.update_data && cm->prev_frame) {
3000*77c1e3ccSAndroid Build Coastguard Worker         segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
3001*77c1e3ccSAndroid Build Coastguard Worker         cm->seg.enabled = cm->prev_frame->seg.enabled;
3002*77c1e3ccSAndroid Build Coastguard Worker       } else {
3003*77c1e3ccSAndroid Build Coastguard Worker         av1_calculate_segdata(&cm->seg);
3004*77c1e3ccSAndroid Build Coastguard Worker       }
3005*77c1e3ccSAndroid Build Coastguard Worker     } else {
3006*77c1e3ccSAndroid Build Coastguard Worker       memset(&cm->seg, 0, sizeof(cm->seg));
3007*77c1e3ccSAndroid Build Coastguard Worker     }
3008*77c1e3ccSAndroid Build Coastguard Worker     segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
3009*77c1e3ccSAndroid Build Coastguard Worker     cm->cur_frame->seg.enabled = cm->seg.enabled;
3010*77c1e3ccSAndroid Build Coastguard Worker 
3011*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
3012*77c1e3ccSAndroid Build Coastguard Worker     start_timing(cpi, av1_encode_frame_time);
3013*77c1e3ccSAndroid Build Coastguard Worker #endif
3014*77c1e3ccSAndroid Build Coastguard Worker     // Set the motion vector precision based on mv stats from the last coded
3015*77c1e3ccSAndroid Build Coastguard Worker     // frame.
3016*77c1e3ccSAndroid Build Coastguard Worker     if (!frame_is_intra_only(cm)) {
3017*77c1e3ccSAndroid Build Coastguard Worker       av1_pick_and_set_high_precision_mv(cpi, q);
3018*77c1e3ccSAndroid Build Coastguard Worker 
3019*77c1e3ccSAndroid Build Coastguard Worker       // If the precision has changed during different iteration of the loop,
3020*77c1e3ccSAndroid Build Coastguard Worker       // then we need to reset the global motion vectors
3021*77c1e3ccSAndroid Build Coastguard Worker       if (loop_count > 0 &&
3022*77c1e3ccSAndroid Build Coastguard Worker           cm->features.allow_high_precision_mv != last_loop_allow_hp) {
3023*77c1e3ccSAndroid Build Coastguard Worker         gm_info->search_done = 0;
3024*77c1e3ccSAndroid Build Coastguard Worker       }
3025*77c1e3ccSAndroid Build Coastguard Worker       last_loop_allow_hp = cm->features.allow_high_precision_mv;
3026*77c1e3ccSAndroid Build Coastguard Worker     }
3027*77c1e3ccSAndroid Build Coastguard Worker 
3028*77c1e3ccSAndroid Build Coastguard Worker     // transform / motion compensation build reconstruction frame
3029*77c1e3ccSAndroid Build Coastguard Worker     av1_encode_frame(cpi);
3030*77c1e3ccSAndroid Build Coastguard Worker 
3031*77c1e3ccSAndroid Build Coastguard Worker     // Disable mv_stats collection for parallel frames based on update flag.
3032*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->do_frame_data_update) do_mv_stats_collection = 0;
3033*77c1e3ccSAndroid Build Coastguard Worker 
3034*77c1e3ccSAndroid Build Coastguard Worker     // Reset the mv_stats in case we are interrupted by an intraframe or an
3035*77c1e3ccSAndroid Build Coastguard Worker     // overlay frame.
3036*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->mv_stats.valid && do_mv_stats_collection) av1_zero(cpi->mv_stats);
3037*77c1e3ccSAndroid Build Coastguard Worker 
3038*77c1e3ccSAndroid Build Coastguard Worker     // Gather the mv_stats for the next frame
3039*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
3040*77c1e3ccSAndroid Build Coastguard Worker         av1_frame_allows_smart_mv(cpi) && do_mv_stats_collection) {
3041*77c1e3ccSAndroid Build Coastguard Worker       av1_collect_mv_stats(cpi, q);
3042*77c1e3ccSAndroid Build Coastguard Worker     }
3043*77c1e3ccSAndroid Build Coastguard Worker 
3044*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
3045*77c1e3ccSAndroid Build Coastguard Worker     end_timing(cpi, av1_encode_frame_time);
3046*77c1e3ccSAndroid Build Coastguard Worker #endif
3047*77c1e3ccSAndroid Build Coastguard Worker 
3048*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND
3049*77c1e3ccSAndroid Build Coastguard Worker     const int do_dummy_pack = 1;
3050*77c1e3ccSAndroid Build Coastguard Worker #else   // CONFIG_BITRATE_ACCURACY
3051*77c1e3ccSAndroid Build Coastguard Worker     // Dummy pack of the bitstream using up to date stats to get an
3052*77c1e3ccSAndroid Build Coastguard Worker     // accurate estimate of output frame size to determine if we need
3053*77c1e3ccSAndroid Build Coastguard Worker     // to recode.
3054*77c1e3ccSAndroid Build Coastguard Worker     const int do_dummy_pack =
3055*77c1e3ccSAndroid Build Coastguard Worker         (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
3056*77c1e3ccSAndroid Build Coastguard Worker          oxcf->rc_cfg.mode != AOM_Q) ||
3057*77c1e3ccSAndroid Build Coastguard Worker         oxcf->rc_cfg.min_cr > 0;
3058*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_BITRATE_ACCURACY
3059*77c1e3ccSAndroid Build Coastguard Worker     if (do_dummy_pack) {
3060*77c1e3ccSAndroid Build Coastguard Worker       av1_finalize_encoded_frame(cpi);
3061*77c1e3ccSAndroid Build Coastguard Worker       int largest_tile_id = 0;  // Output from bitstream: unused here
3062*77c1e3ccSAndroid Build Coastguard Worker       rc->coefficient_size = 0;
3063*77c1e3ccSAndroid Build Coastguard Worker       if (av1_pack_bitstream(cpi, dest, dest_size, size, &largest_tile_id) !=
3064*77c1e3ccSAndroid Build Coastguard Worker           AOM_CODEC_OK) {
3065*77c1e3ccSAndroid Build Coastguard Worker         return AOM_CODEC_ERROR;
3066*77c1e3ccSAndroid Build Coastguard Worker       }
3067*77c1e3ccSAndroid Build Coastguard Worker 
3068*77c1e3ccSAndroid Build Coastguard Worker       // bits used for this frame
3069*77c1e3ccSAndroid Build Coastguard Worker       rc->projected_frame_size = (int)(*size) << 3;
3070*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_COMMAND
3071*77c1e3ccSAndroid Build Coastguard Worker       PSNR_STATS psnr;
3072*77c1e3ccSAndroid Build Coastguard Worker       aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3073*77c1e3ccSAndroid Build Coastguard Worker       printf("q %d rdmult %d rate %d dist %" PRIu64 "\n", q, cpi->rd.RDMULT,
3074*77c1e3ccSAndroid Build Coastguard Worker              rc->projected_frame_size, psnr.sse[0]);
3075*77c1e3ccSAndroid Build Coastguard Worker       ++rd_command->frame_index;
3076*77c1e3ccSAndroid Build Coastguard Worker       if (rd_command->frame_index == rd_command->frame_count) {
3077*77c1e3ccSAndroid Build Coastguard Worker         return AOM_CODEC_ERROR;
3078*77c1e3ccSAndroid Build Coastguard Worker       }
3079*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_RD_COMMAND
3080*77c1e3ccSAndroid Build Coastguard Worker 
3081*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY
3082*77c1e3ccSAndroid Build Coastguard Worker       if (oxcf->pass == AOM_RC_THIRD_PASS) {
3083*77c1e3ccSAndroid Build Coastguard Worker         int frame_coding_idx =
3084*77c1e3ccSAndroid Build Coastguard Worker             av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, cpi->gf_frame_index);
3085*77c1e3ccSAndroid Build Coastguard Worker         rc_log_frame_entropy(&cpi->rc_log, frame_coding_idx,
3086*77c1e3ccSAndroid Build Coastguard Worker                              rc->projected_frame_size, rc->coefficient_size);
3087*77c1e3ccSAndroid Build Coastguard Worker       }
3088*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY
3089*77c1e3ccSAndroid Build Coastguard Worker     }
3090*77c1e3ccSAndroid Build Coastguard Worker 
3091*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
3092*77c1e3ccSAndroid Build Coastguard Worker     if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
3093*77c1e3ccSAndroid Build Coastguard Worker         oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
3094*77c1e3ccSAndroid Build Coastguard Worker       q = cpi->vmaf_info.original_qindex;
3095*77c1e3ccSAndroid Build Coastguard Worker     }
3096*77c1e3ccSAndroid Build Coastguard Worker #endif
3097*77c1e3ccSAndroid Build Coastguard Worker     if (allow_recode) {
3098*77c1e3ccSAndroid Build Coastguard Worker       // Update q and decide whether to do a recode loop
3099*77c1e3ccSAndroid Build Coastguard Worker       recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
3100*77c1e3ccSAndroid Build Coastguard Worker                            bottom_index, &undershoot_seen, &overshoot_seen,
3101*77c1e3ccSAndroid Build Coastguard Worker                            &low_cr_seen, loop_count);
3102*77c1e3ccSAndroid Build Coastguard Worker     }
3103*77c1e3ccSAndroid Build Coastguard Worker 
3104*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_BUTTERAUGLI
3105*77c1e3ccSAndroid Build Coastguard Worker     if (loop_count == 0 && oxcf->tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) {
3106*77c1e3ccSAndroid Build Coastguard Worker       loop = 1;
3107*77c1e3ccSAndroid Build Coastguard Worker       av1_setup_butteraugli_rdmult_and_restore_source(cpi, 0.4);
3108*77c1e3ccSAndroid Build Coastguard Worker     }
3109*77c1e3ccSAndroid Build Coastguard Worker #endif
3110*77c1e3ccSAndroid Build Coastguard Worker 
3111*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->use_ducky_encode) {
3112*77c1e3ccSAndroid Build Coastguard Worker       // Ducky encode currently does not support recode loop.
3113*77c1e3ccSAndroid Build Coastguard Worker       loop = 0;
3114*77c1e3ccSAndroid Build Coastguard Worker     }
3115*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND
3116*77c1e3ccSAndroid Build Coastguard Worker     loop = 0;  // turn off recode loop when CONFIG_BITRATE_ACCURACY is on
3117*77c1e3ccSAndroid Build Coastguard Worker #endif         // CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND
3118*77c1e3ccSAndroid Build Coastguard Worker 
3119*77c1e3ccSAndroid Build Coastguard Worker     if (loop) {
3120*77c1e3ccSAndroid Build Coastguard Worker       ++loop_count;
3121*77c1e3ccSAndroid Build Coastguard Worker       cpi->num_frame_recode =
3122*77c1e3ccSAndroid Build Coastguard Worker           (cpi->num_frame_recode < (NUM_RECODES_PER_FRAME - 1))
3123*77c1e3ccSAndroid Build Coastguard Worker               ? (cpi->num_frame_recode + 1)
3124*77c1e3ccSAndroid Build Coastguard Worker               : (NUM_RECODES_PER_FRAME - 1);
3125*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
3126*77c1e3ccSAndroid Build Coastguard Worker       ++cpi->frame_recode_hits;
3127*77c1e3ccSAndroid Build Coastguard Worker #endif
3128*77c1e3ccSAndroid Build Coastguard Worker     }
3129*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
3130*77c1e3ccSAndroid Build Coastguard Worker     if (loop) printf("\n Recoding:");
3131*77c1e3ccSAndroid Build Coastguard Worker #endif
3132*77c1e3ccSAndroid Build Coastguard Worker   } while (loop);
3133*77c1e3ccSAndroid Build Coastguard Worker 
3134*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
3135*77c1e3ccSAndroid Build Coastguard Worker }
3136*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
3137*77c1e3ccSAndroid Build Coastguard Worker 
3138*77c1e3ccSAndroid Build Coastguard Worker // TODO(jingning, paulwilkins): Set up high grain level to test
3139*77c1e3ccSAndroid Build Coastguard Worker // hardware decoders. Need to adapt the actual noise variance
3140*77c1e3ccSAndroid Build Coastguard Worker // according to the difference between reconstructed frame and the
3141*77c1e3ccSAndroid Build Coastguard Worker // source signal.
set_grain_syn_params(AV1_COMMON * cm)3142*77c1e3ccSAndroid Build Coastguard Worker static void set_grain_syn_params(AV1_COMMON *cm) {
3143*77c1e3ccSAndroid Build Coastguard Worker   aom_film_grain_t *film_grain_params = &cm->film_grain_params;
3144*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->apply_grain = 1;
3145*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->update_parameters = 1;
3146*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->random_seed = rand() & 0xffff;
3147*77c1e3ccSAndroid Build Coastguard Worker 
3148*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->num_y_points = 1;
3149*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->scaling_points_y[0][0] = 128;
3150*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->scaling_points_y[0][1] = 100;
3151*77c1e3ccSAndroid Build Coastguard Worker 
3152*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->seq_params->monochrome) {
3153*77c1e3ccSAndroid Build Coastguard Worker     film_grain_params->num_cb_points = 1;
3154*77c1e3ccSAndroid Build Coastguard Worker     film_grain_params->scaling_points_cb[0][0] = 128;
3155*77c1e3ccSAndroid Build Coastguard Worker     film_grain_params->scaling_points_cb[0][1] = 100;
3156*77c1e3ccSAndroid Build Coastguard Worker 
3157*77c1e3ccSAndroid Build Coastguard Worker     film_grain_params->num_cr_points = 1;
3158*77c1e3ccSAndroid Build Coastguard Worker     film_grain_params->scaling_points_cr[0][0] = 128;
3159*77c1e3ccSAndroid Build Coastguard Worker     film_grain_params->scaling_points_cr[0][1] = 100;
3160*77c1e3ccSAndroid Build Coastguard Worker   } else {
3161*77c1e3ccSAndroid Build Coastguard Worker     film_grain_params->num_cb_points = 0;
3162*77c1e3ccSAndroid Build Coastguard Worker     film_grain_params->num_cr_points = 0;
3163*77c1e3ccSAndroid Build Coastguard Worker   }
3164*77c1e3ccSAndroid Build Coastguard Worker 
3165*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->chroma_scaling_from_luma = 0;
3166*77c1e3ccSAndroid Build Coastguard Worker 
3167*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->scaling_shift = 1;
3168*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->ar_coeff_lag = 0;
3169*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->ar_coeff_shift = 1;
3170*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->overlap_flag = 1;
3171*77c1e3ccSAndroid Build Coastguard Worker   film_grain_params->grain_scale_shift = 0;
3172*77c1e3ccSAndroid Build Coastguard Worker }
3173*77c1e3ccSAndroid Build Coastguard Worker 
3174*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Recode loop or a single loop for encoding one frame, followed by
3175*77c1e3ccSAndroid Build Coastguard Worker  * in-loop deblocking filters, CDEF filters, and restoration filters.
3176*77c1e3ccSAndroid Build Coastguard Worker  *
3177*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup high_level_algo
3178*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
3179*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
3180*77c1e3ccSAndroid Build Coastguard Worker  *
3181*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi             Top-level encoder structure
3182*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    size            Bitstream size
3183*77c1e3ccSAndroid Build Coastguard Worker  * \param[out]   dest            Bitstream output buffer
3184*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    dest_size       Bitstream output buffer size
3185*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    sse             Total distortion of the frame
3186*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rate            Total rate of the frame
3187*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    largest_tile_id Tile id of the last tile
3188*77c1e3ccSAndroid Build Coastguard Worker  *
3189*77c1e3ccSAndroid Build Coastguard Worker  * \return Returns a value to indicate if the encoding is done successfully.
3190*77c1e3ccSAndroid Build Coastguard Worker  * \retval #AOM_CODEC_OK
3191*77c1e3ccSAndroid Build Coastguard Worker  * \retval #AOM_CODEC_ERROR
3192*77c1e3ccSAndroid Build Coastguard Worker  */
encode_with_recode_loop_and_filter(AV1_COMP * cpi,size_t * size,uint8_t * dest,size_t dest_size,int64_t * sse,int64_t * rate,int * largest_tile_id)3193*77c1e3ccSAndroid Build Coastguard Worker static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
3194*77c1e3ccSAndroid Build Coastguard Worker                                               uint8_t *dest, size_t dest_size,
3195*77c1e3ccSAndroid Build Coastguard Worker                                               int64_t *sse, int64_t *rate,
3196*77c1e3ccSAndroid Build Coastguard Worker                                               int *largest_tile_id) {
3197*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
3198*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, encode_with_or_without_recode_time);
3199*77c1e3ccSAndroid Build Coastguard Worker #endif
3200*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < NUM_RECODES_PER_FRAME; i++) {
3201*77c1e3ccSAndroid Build Coastguard Worker     cpi->do_update_frame_probs_txtype[i] = 0;
3202*77c1e3ccSAndroid Build Coastguard Worker     cpi->do_update_frame_probs_obmc[i] = 0;
3203*77c1e3ccSAndroid Build Coastguard Worker     cpi->do_update_frame_probs_warp[i] = 0;
3204*77c1e3ccSAndroid Build Coastguard Worker     cpi->do_update_frame_probs_interpfilter[i] = 0;
3205*77c1e3ccSAndroid Build Coastguard Worker   }
3206*77c1e3ccSAndroid Build Coastguard Worker 
3207*77c1e3ccSAndroid Build Coastguard Worker   cpi->do_update_vbr_bits_off_target_fast = 0;
3208*77c1e3ccSAndroid Build Coastguard Worker   int err;
3209*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
3210*77c1e3ccSAndroid Build Coastguard Worker   err = encode_without_recode(cpi);
3211*77c1e3ccSAndroid Build Coastguard Worker #else
3212*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.hl_sf.recode_loop == DISALLOW_RECODE)
3213*77c1e3ccSAndroid Build Coastguard Worker     err = encode_without_recode(cpi);
3214*77c1e3ccSAndroid Build Coastguard Worker   else
3215*77c1e3ccSAndroid Build Coastguard Worker     err = encode_with_recode_loop(cpi, size, dest, dest_size);
3216*77c1e3ccSAndroid Build Coastguard Worker #endif
3217*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
3218*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, encode_with_or_without_recode_time);
3219*77c1e3ccSAndroid Build Coastguard Worker #endif
3220*77c1e3ccSAndroid Build Coastguard Worker   if (err != AOM_CODEC_OK) {
3221*77c1e3ccSAndroid Build Coastguard Worker     if (err == -1) {
3222*77c1e3ccSAndroid Build Coastguard Worker       // special case as described in encode_with_recode_loop().
3223*77c1e3ccSAndroid Build Coastguard Worker       // Encoding was skipped.
3224*77c1e3ccSAndroid Build Coastguard Worker       err = AOM_CODEC_OK;
3225*77c1e3ccSAndroid Build Coastguard Worker       if (sse != NULL) *sse = INT64_MAX;
3226*77c1e3ccSAndroid Build Coastguard Worker       if (rate != NULL) *rate = INT64_MAX;
3227*77c1e3ccSAndroid Build Coastguard Worker       *largest_tile_id = 0;
3228*77c1e3ccSAndroid Build Coastguard Worker     }
3229*77c1e3ccSAndroid Build Coastguard Worker     return err;
3230*77c1e3ccSAndroid Build Coastguard Worker   }
3231*77c1e3ccSAndroid Build Coastguard Worker 
3232*77c1e3ccSAndroid Build Coastguard Worker #ifdef OUTPUT_YUV_DENOISED
3233*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3234*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
3235*77c1e3ccSAndroid Build Coastguard Worker     aom_write_yuv_frame(yuv_denoised_file,
3236*77c1e3ccSAndroid Build Coastguard Worker                         &cpi->denoiser.running_avg_y[INTRA_FRAME]);
3237*77c1e3ccSAndroid Build Coastguard Worker   }
3238*77c1e3ccSAndroid Build Coastguard Worker #endif
3239*77c1e3ccSAndroid Build Coastguard Worker 
3240*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
3241*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader *const seq_params = cm->seq_params;
3242*77c1e3ccSAndroid Build Coastguard Worker 
3243*77c1e3ccSAndroid Build Coastguard Worker   // Special case code to reduce pulsing when key frames are forced at a
3244*77c1e3ccSAndroid Build Coastguard Worker   // fixed interval. Note the reconstruction error if it is the frame before
3245*77c1e3ccSAndroid Build Coastguard Worker   // the force key frame
3246*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->ppi->p_rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
3247*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
3248*77c1e3ccSAndroid Build Coastguard Worker     if (seq_params->use_highbitdepth) {
3249*77c1e3ccSAndroid Build Coastguard Worker       cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
3250*77c1e3ccSAndroid Build Coastguard Worker     } else {
3251*77c1e3ccSAndroid Build Coastguard Worker       cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
3252*77c1e3ccSAndroid Build Coastguard Worker     }
3253*77c1e3ccSAndroid Build Coastguard Worker #else
3254*77c1e3ccSAndroid Build Coastguard Worker     cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
3255*77c1e3ccSAndroid Build Coastguard Worker #endif
3256*77c1e3ccSAndroid Build Coastguard Worker   }
3257*77c1e3ccSAndroid Build Coastguard Worker 
3258*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
3259*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->buf.transfer_characteristics =
3260*77c1e3ccSAndroid Build Coastguard Worker       seq_params->transfer_characteristics;
3261*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
3262*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->buf.monochrome = seq_params->monochrome;
3263*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->buf.chroma_sample_position =
3264*77c1e3ccSAndroid Build Coastguard Worker       seq_params->chroma_sample_position;
3265*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->buf.color_range = seq_params->color_range;
3266*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->buf.render_width = cm->render_width;
3267*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->buf.render_height = cm->render_height;
3268*77c1e3ccSAndroid Build Coastguard Worker 
3269*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->mt_info.pipeline_lpf_mt_with_enc)
3270*77c1e3ccSAndroid Build Coastguard Worker     set_postproc_filter_default_params(&cpi->common);
3271*77c1e3ccSAndroid Build Coastguard Worker 
3272*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->features.allow_intrabc) {
3273*77c1e3ccSAndroid Build Coastguard Worker     loopfilter_frame(cpi, cm);
3274*77c1e3ccSAndroid Build Coastguard Worker   }
3275*77c1e3ccSAndroid Build Coastguard Worker 
3276*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.mode != ALLINTRA && !cpi->ppi->rtc_ref.non_reference_frame) {
3277*77c1e3ccSAndroid Build Coastguard Worker     extend_frame_borders(cpi);
3278*77c1e3ccSAndroid Build Coastguard Worker   }
3279*77c1e3ccSAndroid Build Coastguard Worker 
3280*77c1e3ccSAndroid Build Coastguard Worker #ifdef OUTPUT_YUV_REC
3281*77c1e3ccSAndroid Build Coastguard Worker   aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
3282*77c1e3ccSAndroid Build Coastguard Worker #endif
3283*77c1e3ccSAndroid Build Coastguard Worker 
3284*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_FILM) {
3285*77c1e3ccSAndroid Build Coastguard Worker     set_grain_syn_params(cm);
3286*77c1e3ccSAndroid Build Coastguard Worker   }
3287*77c1e3ccSAndroid Build Coastguard Worker 
3288*77c1e3ccSAndroid Build Coastguard Worker   av1_finalize_encoded_frame(cpi);
3289*77c1e3ccSAndroid Build Coastguard Worker   // Build the bitstream
3290*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
3291*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, av1_pack_bitstream_final_time);
3292*77c1e3ccSAndroid Build Coastguard Worker #endif
3293*77c1e3ccSAndroid Build Coastguard Worker   cpi->rc.coefficient_size = 0;
3294*77c1e3ccSAndroid Build Coastguard Worker   if (av1_pack_bitstream(cpi, dest, dest_size, size, largest_tile_id) !=
3295*77c1e3ccSAndroid Build Coastguard Worker       AOM_CODEC_OK)
3296*77c1e3ccSAndroid Build Coastguard Worker     return AOM_CODEC_ERROR;
3297*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
3298*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, av1_pack_bitstream_final_time);
3299*77c1e3ccSAndroid Build Coastguard Worker #endif
3300*77c1e3ccSAndroid Build Coastguard Worker 
3301*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->rc.postencode_drop && allow_postencode_drop_rtc(cpi) &&
3302*77c1e3ccSAndroid Build Coastguard Worker       av1_postencode_drop_cbr(cpi, size)) {
3303*77c1e3ccSAndroid Build Coastguard Worker     return AOM_CODEC_OK;
3304*77c1e3ccSAndroid Build Coastguard Worker   }
3305*77c1e3ccSAndroid Build Coastguard Worker 
3306*77c1e3ccSAndroid Build Coastguard Worker   // Compute sse and rate.
3307*77c1e3ccSAndroid Build Coastguard Worker   if (sse != NULL) {
3308*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
3309*77c1e3ccSAndroid Build Coastguard Worker     *sse = (seq_params->use_highbitdepth)
3310*77c1e3ccSAndroid Build Coastguard Worker                ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
3311*77c1e3ccSAndroid Build Coastguard Worker                : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
3312*77c1e3ccSAndroid Build Coastguard Worker #else
3313*77c1e3ccSAndroid Build Coastguard Worker     *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
3314*77c1e3ccSAndroid Build Coastguard Worker #endif
3315*77c1e3ccSAndroid Build Coastguard Worker   }
3316*77c1e3ccSAndroid Build Coastguard Worker   if (rate != NULL) {
3317*77c1e3ccSAndroid Build Coastguard Worker     const int64_t bits = (*size << 3);
3318*77c1e3ccSAndroid Build Coastguard Worker     *rate = (bits << 5);  // To match scale.
3319*77c1e3ccSAndroid Build Coastguard Worker   }
3320*77c1e3ccSAndroid Build Coastguard Worker 
3321*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
3322*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->use_ducky_encode) {
3323*77c1e3ccSAndroid Build Coastguard Worker     PSNR_STATS psnr;
3324*77c1e3ccSAndroid Build Coastguard Worker     aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3325*77c1e3ccSAndroid Build Coastguard Worker     DuckyEncodeFrameResult *frame_result = &cpi->ducky_encode_info.frame_result;
3326*77c1e3ccSAndroid Build Coastguard Worker     frame_result->global_order_idx = cm->cur_frame->display_order_hint;
3327*77c1e3ccSAndroid Build Coastguard Worker     frame_result->q_index = cm->quant_params.base_qindex;
3328*77c1e3ccSAndroid Build Coastguard Worker     frame_result->rdmult = cpi->rd.RDMULT;
3329*77c1e3ccSAndroid Build Coastguard Worker     frame_result->rate = (int)(*size) * 8;
3330*77c1e3ccSAndroid Build Coastguard Worker     frame_result->dist = psnr.sse[0];
3331*77c1e3ccSAndroid Build Coastguard Worker     frame_result->psnr = psnr.psnr[0];
3332*77c1e3ccSAndroid Build Coastguard Worker   }
3333*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
3334*77c1e3ccSAndroid Build Coastguard Worker 
3335*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
3336*77c1e3ccSAndroid Build Coastguard Worker }
3337*77c1e3ccSAndroid Build Coastguard Worker 
encode_with_and_without_superres(AV1_COMP * cpi,size_t * size,uint8_t * dest,size_t dest_size,int * largest_tile_id)3338*77c1e3ccSAndroid Build Coastguard Worker static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
3339*77c1e3ccSAndroid Build Coastguard Worker                                             uint8_t *dest, size_t dest_size,
3340*77c1e3ccSAndroid Build Coastguard Worker                                             int *largest_tile_id) {
3341*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
3342*77c1e3ccSAndroid Build Coastguard Worker   assert(cm->seq_params->enable_superres);
3343*77c1e3ccSAndroid Build Coastguard Worker   assert(av1_superres_in_recode_allowed(cpi));
3344*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t err = AOM_CODEC_OK;
3345*77c1e3ccSAndroid Build Coastguard Worker   av1_save_all_coding_context(cpi);
3346*77c1e3ccSAndroid Build Coastguard Worker 
3347*77c1e3ccSAndroid Build Coastguard Worker   int64_t sse1 = INT64_MAX;
3348*77c1e3ccSAndroid Build Coastguard Worker   int64_t rate1 = INT64_MAX;
3349*77c1e3ccSAndroid Build Coastguard Worker   int largest_tile_id1 = 0;
3350*77c1e3ccSAndroid Build Coastguard Worker   int64_t sse2 = INT64_MAX;
3351*77c1e3ccSAndroid Build Coastguard Worker   int64_t rate2 = INT64_MAX;
3352*77c1e3ccSAndroid Build Coastguard Worker   int largest_tile_id2;
3353*77c1e3ccSAndroid Build Coastguard Worker   double proj_rdcost1 = DBL_MAX;
3354*77c1e3ccSAndroid Build Coastguard Worker   const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
3355*77c1e3ccSAndroid Build Coastguard Worker   const FRAME_UPDATE_TYPE update_type =
3356*77c1e3ccSAndroid Build Coastguard Worker       gf_group->update_type[cpi->gf_frame_index];
3357*77c1e3ccSAndroid Build Coastguard Worker   const aom_bit_depth_t bit_depth = cm->seq_params->bit_depth;
3358*77c1e3ccSAndroid Build Coastguard Worker 
3359*77c1e3ccSAndroid Build Coastguard Worker   // Encode with superres.
3360*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_ALL) {
3361*77c1e3ccSAndroid Build Coastguard Worker     SuperResCfg *const superres_cfg = &cpi->oxcf.superres_cfg;
3362*77c1e3ccSAndroid Build Coastguard Worker     int64_t superres_sses[SCALE_NUMERATOR];
3363*77c1e3ccSAndroid Build Coastguard Worker     int64_t superres_rates[SCALE_NUMERATOR];
3364*77c1e3ccSAndroid Build Coastguard Worker     int superres_largest_tile_ids[SCALE_NUMERATOR];
3365*77c1e3ccSAndroid Build Coastguard Worker     // Use superres for Key-frames and Alt-ref frames only.
3366*77c1e3ccSAndroid Build Coastguard Worker     if (update_type != OVERLAY_UPDATE && update_type != INTNL_OVERLAY_UPDATE) {
3367*77c1e3ccSAndroid Build Coastguard Worker       for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
3368*77c1e3ccSAndroid Build Coastguard Worker            ++denom) {
3369*77c1e3ccSAndroid Build Coastguard Worker         superres_cfg->superres_scale_denominator = denom;
3370*77c1e3ccSAndroid Build Coastguard Worker         superres_cfg->superres_kf_scale_denominator = denom;
3371*77c1e3ccSAndroid Build Coastguard Worker         const int this_index = denom - (SCALE_NUMERATOR + 1);
3372*77c1e3ccSAndroid Build Coastguard Worker 
3373*77c1e3ccSAndroid Build Coastguard Worker         cpi->superres_mode = AOM_SUPERRES_AUTO;  // Super-res on for this loop.
3374*77c1e3ccSAndroid Build Coastguard Worker         err = encode_with_recode_loop_and_filter(
3375*77c1e3ccSAndroid Build Coastguard Worker             cpi, size, dest, dest_size, &superres_sses[this_index],
3376*77c1e3ccSAndroid Build Coastguard Worker             &superres_rates[this_index],
3377*77c1e3ccSAndroid Build Coastguard Worker             &superres_largest_tile_ids[this_index]);
3378*77c1e3ccSAndroid Build Coastguard Worker         cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
3379*77c1e3ccSAndroid Build Coastguard Worker         if (err != AOM_CODEC_OK) return err;
3380*77c1e3ccSAndroid Build Coastguard Worker         restore_all_coding_context(cpi);
3381*77c1e3ccSAndroid Build Coastguard Worker       }
3382*77c1e3ccSAndroid Build Coastguard Worker       // Reset.
3383*77c1e3ccSAndroid Build Coastguard Worker       superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
3384*77c1e3ccSAndroid Build Coastguard Worker       superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
3385*77c1e3ccSAndroid Build Coastguard Worker     } else {
3386*77c1e3ccSAndroid Build Coastguard Worker       for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
3387*77c1e3ccSAndroid Build Coastguard Worker            ++denom) {
3388*77c1e3ccSAndroid Build Coastguard Worker         const int this_index = denom - (SCALE_NUMERATOR + 1);
3389*77c1e3ccSAndroid Build Coastguard Worker         superres_sses[this_index] = INT64_MAX;
3390*77c1e3ccSAndroid Build Coastguard Worker         superres_rates[this_index] = INT64_MAX;
3391*77c1e3ccSAndroid Build Coastguard Worker       }
3392*77c1e3ccSAndroid Build Coastguard Worker     }
3393*77c1e3ccSAndroid Build Coastguard Worker     // Encode without superres.
3394*77c1e3ccSAndroid Build Coastguard Worker     assert(cpi->superres_mode == AOM_SUPERRES_NONE);
3395*77c1e3ccSAndroid Build Coastguard Worker     err = encode_with_recode_loop_and_filter(cpi, size, dest, dest_size, &sse2,
3396*77c1e3ccSAndroid Build Coastguard Worker                                              &rate2, &largest_tile_id2);
3397*77c1e3ccSAndroid Build Coastguard Worker     if (err != AOM_CODEC_OK) return err;
3398*77c1e3ccSAndroid Build Coastguard Worker 
3399*77c1e3ccSAndroid Build Coastguard Worker     // Note: Both use common rdmult based on base qindex of fullres.
3400*77c1e3ccSAndroid Build Coastguard Worker     const int64_t rdmult = av1_compute_rd_mult_based_on_qindex(
3401*77c1e3ccSAndroid Build Coastguard Worker         bit_depth, update_type, cm->quant_params.base_qindex);
3402*77c1e3ccSAndroid Build Coastguard Worker 
3403*77c1e3ccSAndroid Build Coastguard Worker     // Find the best rdcost among all superres denoms.
3404*77c1e3ccSAndroid Build Coastguard Worker     int best_denom = -1;
3405*77c1e3ccSAndroid Build Coastguard Worker     for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
3406*77c1e3ccSAndroid Build Coastguard Worker          ++denom) {
3407*77c1e3ccSAndroid Build Coastguard Worker       const int this_index = denom - (SCALE_NUMERATOR + 1);
3408*77c1e3ccSAndroid Build Coastguard Worker       const int64_t this_sse = superres_sses[this_index];
3409*77c1e3ccSAndroid Build Coastguard Worker       const int64_t this_rate = superres_rates[this_index];
3410*77c1e3ccSAndroid Build Coastguard Worker       const int this_largest_tile_id = superres_largest_tile_ids[this_index];
3411*77c1e3ccSAndroid Build Coastguard Worker       const double this_rdcost = RDCOST_DBL_WITH_NATIVE_BD_DIST(
3412*77c1e3ccSAndroid Build Coastguard Worker           rdmult, this_rate, this_sse, bit_depth);
3413*77c1e3ccSAndroid Build Coastguard Worker       if (this_rdcost < proj_rdcost1) {
3414*77c1e3ccSAndroid Build Coastguard Worker         sse1 = this_sse;
3415*77c1e3ccSAndroid Build Coastguard Worker         rate1 = this_rate;
3416*77c1e3ccSAndroid Build Coastguard Worker         largest_tile_id1 = this_largest_tile_id;
3417*77c1e3ccSAndroid Build Coastguard Worker         proj_rdcost1 = this_rdcost;
3418*77c1e3ccSAndroid Build Coastguard Worker         best_denom = denom;
3419*77c1e3ccSAndroid Build Coastguard Worker       }
3420*77c1e3ccSAndroid Build Coastguard Worker     }
3421*77c1e3ccSAndroid Build Coastguard Worker     const double proj_rdcost2 =
3422*77c1e3ccSAndroid Build Coastguard Worker         RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate2, sse2, bit_depth);
3423*77c1e3ccSAndroid Build Coastguard Worker     // Re-encode with superres if it's better.
3424*77c1e3ccSAndroid Build Coastguard Worker     if (proj_rdcost1 < proj_rdcost2) {
3425*77c1e3ccSAndroid Build Coastguard Worker       restore_all_coding_context(cpi);
3426*77c1e3ccSAndroid Build Coastguard Worker       // TODO(urvang): We should avoid rerunning the recode loop by saving
3427*77c1e3ccSAndroid Build Coastguard Worker       // previous output+state, or running encode only for the selected 'q' in
3428*77c1e3ccSAndroid Build Coastguard Worker       // previous step.
3429*77c1e3ccSAndroid Build Coastguard Worker       // Again, temporarily force the best denom.
3430*77c1e3ccSAndroid Build Coastguard Worker       superres_cfg->superres_scale_denominator = best_denom;
3431*77c1e3ccSAndroid Build Coastguard Worker       superres_cfg->superres_kf_scale_denominator = best_denom;
3432*77c1e3ccSAndroid Build Coastguard Worker       int64_t sse3 = INT64_MAX;
3433*77c1e3ccSAndroid Build Coastguard Worker       int64_t rate3 = INT64_MAX;
3434*77c1e3ccSAndroid Build Coastguard Worker       cpi->superres_mode =
3435*77c1e3ccSAndroid Build Coastguard Worker           AOM_SUPERRES_AUTO;  // Super-res on for this recode loop.
3436*77c1e3ccSAndroid Build Coastguard Worker       err = encode_with_recode_loop_and_filter(cpi, size, dest, dest_size,
3437*77c1e3ccSAndroid Build Coastguard Worker                                                &sse3, &rate3, largest_tile_id);
3438*77c1e3ccSAndroid Build Coastguard Worker       cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
3439*77c1e3ccSAndroid Build Coastguard Worker       assert(sse1 == sse3);
3440*77c1e3ccSAndroid Build Coastguard Worker       assert(rate1 == rate3);
3441*77c1e3ccSAndroid Build Coastguard Worker       assert(largest_tile_id1 == *largest_tile_id);
3442*77c1e3ccSAndroid Build Coastguard Worker       // Reset.
3443*77c1e3ccSAndroid Build Coastguard Worker       superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
3444*77c1e3ccSAndroid Build Coastguard Worker       superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
3445*77c1e3ccSAndroid Build Coastguard Worker     } else {
3446*77c1e3ccSAndroid Build Coastguard Worker       *largest_tile_id = largest_tile_id2;
3447*77c1e3ccSAndroid Build Coastguard Worker     }
3448*77c1e3ccSAndroid Build Coastguard Worker   } else {
3449*77c1e3ccSAndroid Build Coastguard Worker     assert(cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_DUAL);
3450*77c1e3ccSAndroid Build Coastguard Worker     cpi->superres_mode =
3451*77c1e3ccSAndroid Build Coastguard Worker         AOM_SUPERRES_AUTO;  // Super-res on for this recode loop.
3452*77c1e3ccSAndroid Build Coastguard Worker     err = encode_with_recode_loop_and_filter(cpi, size, dest, dest_size, &sse1,
3453*77c1e3ccSAndroid Build Coastguard Worker                                              &rate1, &largest_tile_id1);
3454*77c1e3ccSAndroid Build Coastguard Worker     cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
3455*77c1e3ccSAndroid Build Coastguard Worker     if (err != AOM_CODEC_OK) return err;
3456*77c1e3ccSAndroid Build Coastguard Worker     restore_all_coding_context(cpi);
3457*77c1e3ccSAndroid Build Coastguard Worker     // Encode without superres.
3458*77c1e3ccSAndroid Build Coastguard Worker     assert(cpi->superres_mode == AOM_SUPERRES_NONE);
3459*77c1e3ccSAndroid Build Coastguard Worker     err = encode_with_recode_loop_and_filter(cpi, size, dest, dest_size, &sse2,
3460*77c1e3ccSAndroid Build Coastguard Worker                                              &rate2, &largest_tile_id2);
3461*77c1e3ccSAndroid Build Coastguard Worker     if (err != AOM_CODEC_OK) return err;
3462*77c1e3ccSAndroid Build Coastguard Worker 
3463*77c1e3ccSAndroid Build Coastguard Worker     // Note: Both use common rdmult based on base qindex of fullres.
3464*77c1e3ccSAndroid Build Coastguard Worker     const int64_t rdmult = av1_compute_rd_mult_based_on_qindex(
3465*77c1e3ccSAndroid Build Coastguard Worker         bit_depth, update_type, cm->quant_params.base_qindex);
3466*77c1e3ccSAndroid Build Coastguard Worker     proj_rdcost1 =
3467*77c1e3ccSAndroid Build Coastguard Worker         RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate1, sse1, bit_depth);
3468*77c1e3ccSAndroid Build Coastguard Worker     const double proj_rdcost2 =
3469*77c1e3ccSAndroid Build Coastguard Worker         RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate2, sse2, bit_depth);
3470*77c1e3ccSAndroid Build Coastguard Worker     // Re-encode with superres if it's better.
3471*77c1e3ccSAndroid Build Coastguard Worker     if (proj_rdcost1 < proj_rdcost2) {
3472*77c1e3ccSAndroid Build Coastguard Worker       restore_all_coding_context(cpi);
3473*77c1e3ccSAndroid Build Coastguard Worker       // TODO(urvang): We should avoid rerunning the recode loop by saving
3474*77c1e3ccSAndroid Build Coastguard Worker       // previous output+state, or running encode only for the selected 'q' in
3475*77c1e3ccSAndroid Build Coastguard Worker       // previous step.
3476*77c1e3ccSAndroid Build Coastguard Worker       int64_t sse3 = INT64_MAX;
3477*77c1e3ccSAndroid Build Coastguard Worker       int64_t rate3 = INT64_MAX;
3478*77c1e3ccSAndroid Build Coastguard Worker       cpi->superres_mode =
3479*77c1e3ccSAndroid Build Coastguard Worker           AOM_SUPERRES_AUTO;  // Super-res on for this recode loop.
3480*77c1e3ccSAndroid Build Coastguard Worker       err = encode_with_recode_loop_and_filter(cpi, size, dest, dest_size,
3481*77c1e3ccSAndroid Build Coastguard Worker                                                &sse3, &rate3, largest_tile_id);
3482*77c1e3ccSAndroid Build Coastguard Worker       cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
3483*77c1e3ccSAndroid Build Coastguard Worker       assert(sse1 == sse3);
3484*77c1e3ccSAndroid Build Coastguard Worker       assert(rate1 == rate3);
3485*77c1e3ccSAndroid Build Coastguard Worker       assert(largest_tile_id1 == *largest_tile_id);
3486*77c1e3ccSAndroid Build Coastguard Worker     } else {
3487*77c1e3ccSAndroid Build Coastguard Worker       *largest_tile_id = largest_tile_id2;
3488*77c1e3ccSAndroid Build Coastguard Worker     }
3489*77c1e3ccSAndroid Build Coastguard Worker   }
3490*77c1e3ccSAndroid Build Coastguard Worker 
3491*77c1e3ccSAndroid Build Coastguard Worker   return err;
3492*77c1e3ccSAndroid Build Coastguard Worker }
3493*77c1e3ccSAndroid Build Coastguard Worker 
3494*77c1e3ccSAndroid Build Coastguard Worker // Conditions to disable cdf_update mode in selective mode for real-time.
3495*77c1e3ccSAndroid Build Coastguard Worker // Handle case for layers, scene change, and resizing.
selective_disable_cdf_rtc(const AV1_COMP * cpi)3496*77c1e3ccSAndroid Build Coastguard Worker static inline int selective_disable_cdf_rtc(const AV1_COMP *cpi) {
3497*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
3498*77c1e3ccSAndroid Build Coastguard Worker   const RATE_CONTROL *const rc = &cpi->rc;
3499*77c1e3ccSAndroid Build Coastguard Worker   // For single layer.
3500*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->svc.number_spatial_layers == 1 &&
3501*77c1e3ccSAndroid Build Coastguard Worker       cpi->svc.number_temporal_layers == 1) {
3502*77c1e3ccSAndroid Build Coastguard Worker     // Don't disable on intra_only, scene change (high_source_sad = 1),
3503*77c1e3ccSAndroid Build Coastguard Worker     // or resized frame. To avoid quality loss force enable at
3504*77c1e3ccSAndroid Build Coastguard Worker     // for ~30 frames after key or scene/slide change, and
3505*77c1e3ccSAndroid Build Coastguard Worker     // after 8 frames since last update if frame_source_sad > 0.
3506*77c1e3ccSAndroid Build Coastguard Worker     if (frame_is_intra_only(cm) || is_frame_resize_pending(cpi) ||
3507*77c1e3ccSAndroid Build Coastguard Worker         rc->high_source_sad || rc->frames_since_key < 30 ||
3508*77c1e3ccSAndroid Build Coastguard Worker         (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
3509*77c1e3ccSAndroid Build Coastguard Worker          cpi->cyclic_refresh->counter_encode_maxq_scene_change < 30) ||
3510*77c1e3ccSAndroid Build Coastguard Worker         (cpi->frames_since_last_update > 8 && cpi->rc.frame_source_sad > 0))
3511*77c1e3ccSAndroid Build Coastguard Worker       return 0;
3512*77c1e3ccSAndroid Build Coastguard Worker     else
3513*77c1e3ccSAndroid Build Coastguard Worker       return 1;
3514*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->svc.number_temporal_layers > 1) {
3515*77c1e3ccSAndroid Build Coastguard Worker     // Disable only on top temporal enhancement layer for now.
3516*77c1e3ccSAndroid Build Coastguard Worker     return cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1;
3517*77c1e3ccSAndroid Build Coastguard Worker   }
3518*77c1e3ccSAndroid Build Coastguard Worker   return 1;
3519*77c1e3ccSAndroid Build Coastguard Worker }
3520*77c1e3ccSAndroid Build Coastguard Worker 
3521*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
subtract_stats(FIRSTPASS_STATS * section,const FIRSTPASS_STATS * frame)3522*77c1e3ccSAndroid Build Coastguard Worker static void subtract_stats(FIRSTPASS_STATS *section,
3523*77c1e3ccSAndroid Build Coastguard Worker                            const FIRSTPASS_STATS *frame) {
3524*77c1e3ccSAndroid Build Coastguard Worker   section->frame -= frame->frame;
3525*77c1e3ccSAndroid Build Coastguard Worker   section->weight -= frame->weight;
3526*77c1e3ccSAndroid Build Coastguard Worker   section->intra_error -= frame->intra_error;
3527*77c1e3ccSAndroid Build Coastguard Worker   section->frame_avg_wavelet_energy -= frame->frame_avg_wavelet_energy;
3528*77c1e3ccSAndroid Build Coastguard Worker   section->coded_error -= frame->coded_error;
3529*77c1e3ccSAndroid Build Coastguard Worker   section->sr_coded_error -= frame->sr_coded_error;
3530*77c1e3ccSAndroid Build Coastguard Worker   section->pcnt_inter -= frame->pcnt_inter;
3531*77c1e3ccSAndroid Build Coastguard Worker   section->pcnt_motion -= frame->pcnt_motion;
3532*77c1e3ccSAndroid Build Coastguard Worker   section->pcnt_second_ref -= frame->pcnt_second_ref;
3533*77c1e3ccSAndroid Build Coastguard Worker   section->pcnt_neutral -= frame->pcnt_neutral;
3534*77c1e3ccSAndroid Build Coastguard Worker   section->intra_skip_pct -= frame->intra_skip_pct;
3535*77c1e3ccSAndroid Build Coastguard Worker   section->inactive_zone_rows -= frame->inactive_zone_rows;
3536*77c1e3ccSAndroid Build Coastguard Worker   section->inactive_zone_cols -= frame->inactive_zone_cols;
3537*77c1e3ccSAndroid Build Coastguard Worker   section->MVr -= frame->MVr;
3538*77c1e3ccSAndroid Build Coastguard Worker   section->mvr_abs -= frame->mvr_abs;
3539*77c1e3ccSAndroid Build Coastguard Worker   section->MVc -= frame->MVc;
3540*77c1e3ccSAndroid Build Coastguard Worker   section->mvc_abs -= frame->mvc_abs;
3541*77c1e3ccSAndroid Build Coastguard Worker   section->MVrv -= frame->MVrv;
3542*77c1e3ccSAndroid Build Coastguard Worker   section->MVcv -= frame->MVcv;
3543*77c1e3ccSAndroid Build Coastguard Worker   section->mv_in_out_count -= frame->mv_in_out_count;
3544*77c1e3ccSAndroid Build Coastguard Worker   section->new_mv_count -= frame->new_mv_count;
3545*77c1e3ccSAndroid Build Coastguard Worker   section->count -= frame->count;
3546*77c1e3ccSAndroid Build Coastguard Worker   section->duration -= frame->duration;
3547*77c1e3ccSAndroid Build Coastguard Worker }
3548*77c1e3ccSAndroid Build Coastguard Worker 
calculate_frame_avg_haar_energy(AV1_COMP * cpi)3549*77c1e3ccSAndroid Build Coastguard Worker static void calculate_frame_avg_haar_energy(AV1_COMP *cpi) {
3550*77c1e3ccSAndroid Build Coastguard Worker   TWO_PASS *const twopass = &cpi->ppi->twopass;
3551*77c1e3ccSAndroid Build Coastguard Worker   const FIRSTPASS_STATS *const total_stats =
3552*77c1e3ccSAndroid Build Coastguard Worker       twopass->stats_buf_ctx->total_stats;
3553*77c1e3ccSAndroid Build Coastguard Worker 
3554*77c1e3ccSAndroid Build Coastguard Worker   if (is_one_pass_rt_params(cpi) ||
3555*77c1e3ccSAndroid Build Coastguard Worker       (cpi->oxcf.q_cfg.deltaq_mode != DELTA_Q_PERCEPTUAL) ||
3556*77c1e3ccSAndroid Build Coastguard Worker       (is_fp_wavelet_energy_invalid(total_stats) == 0))
3557*77c1e3ccSAndroid Build Coastguard Worker     return;
3558*77c1e3ccSAndroid Build Coastguard Worker 
3559*77c1e3ccSAndroid Build Coastguard Worker   const int num_mbs = (cpi->oxcf.resize_cfg.resize_mode != RESIZE_NONE)
3560*77c1e3ccSAndroid Build Coastguard Worker                           ? cpi->initial_mbs
3561*77c1e3ccSAndroid Build Coastguard Worker                           : cpi->common.mi_params.MBs;
3562*77c1e3ccSAndroid Build Coastguard Worker   const YV12_BUFFER_CONFIG *const unfiltered_source = cpi->unfiltered_source;
3563*77c1e3ccSAndroid Build Coastguard Worker   const uint8_t *const src = unfiltered_source->y_buffer;
3564*77c1e3ccSAndroid Build Coastguard Worker   const int hbd = unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
3565*77c1e3ccSAndroid Build Coastguard Worker   const int stride = unfiltered_source->y_stride;
3566*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE fp_block_size =
3567*77c1e3ccSAndroid Build Coastguard Worker       get_fp_block_size(cpi->is_screen_content_type);
3568*77c1e3ccSAndroid Build Coastguard Worker   const int fp_block_size_width = block_size_wide[fp_block_size];
3569*77c1e3ccSAndroid Build Coastguard Worker   const int fp_block_size_height = block_size_high[fp_block_size];
3570*77c1e3ccSAndroid Build Coastguard Worker   const int num_unit_cols =
3571*77c1e3ccSAndroid Build Coastguard Worker       get_num_blocks(unfiltered_source->y_crop_width, fp_block_size_width);
3572*77c1e3ccSAndroid Build Coastguard Worker   const int num_unit_rows =
3573*77c1e3ccSAndroid Build Coastguard Worker       get_num_blocks(unfiltered_source->y_crop_height, fp_block_size_height);
3574*77c1e3ccSAndroid Build Coastguard Worker   const int num_8x8_cols = num_unit_cols * (fp_block_size_width / 8);
3575*77c1e3ccSAndroid Build Coastguard Worker   const int num_8x8_rows = num_unit_rows * (fp_block_size_height / 8);
3576*77c1e3ccSAndroid Build Coastguard Worker   int64_t frame_avg_wavelet_energy = av1_haar_ac_sad_mxn_uint8_input(
3577*77c1e3ccSAndroid Build Coastguard Worker       src, stride, hbd, num_8x8_rows, num_8x8_cols);
3578*77c1e3ccSAndroid Build Coastguard Worker 
3579*77c1e3ccSAndroid Build Coastguard Worker   cpi->twopass_frame.frame_avg_haar_energy =
3580*77c1e3ccSAndroid Build Coastguard Worker       log1p((double)frame_avg_wavelet_energy / num_mbs);
3581*77c1e3ccSAndroid Build Coastguard Worker }
3582*77c1e3ccSAndroid Build Coastguard Worker #endif
3583*77c1e3ccSAndroid Build Coastguard Worker 
3584*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Run the final pass encoding for 1-pass/2-pass encoding mode, and pack
3585*77c1e3ccSAndroid Build Coastguard Worker  * the bitstream
3586*77c1e3ccSAndroid Build Coastguard Worker  *
3587*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup high_level_algo
3588*77c1e3ccSAndroid Build Coastguard Worker  * \callgraph
3589*77c1e3ccSAndroid Build Coastguard Worker  * \callergraph
3590*77c1e3ccSAndroid Build Coastguard Worker  *
3591*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi             Top-level encoder structure
3592*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    size            Bitstream size
3593*77c1e3ccSAndroid Build Coastguard Worker  * \param[out]   dest            Bitstream output buffer
3594*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    dest_size       Bitstream output buffer size
3595*77c1e3ccSAndroid Build Coastguard Worker  *
3596*77c1e3ccSAndroid Build Coastguard Worker  * \return Returns a value to indicate if the encoding is done successfully.
3597*77c1e3ccSAndroid Build Coastguard Worker  * \retval #AOM_CODEC_OK
3598*77c1e3ccSAndroid Build Coastguard Worker  * \retval #AOM_CODEC_ERROR
3599*77c1e3ccSAndroid Build Coastguard Worker  */
encode_frame_to_data_rate(AV1_COMP * cpi,size_t * size,uint8_t * dest,size_t dest_size)3600*77c1e3ccSAndroid Build Coastguard Worker static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size, uint8_t *dest,
3601*77c1e3ccSAndroid Build Coastguard Worker                                      size_t dest_size) {
3602*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
3603*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader *const seq_params = cm->seq_params;
3604*77c1e3ccSAndroid Build Coastguard Worker   CurrentFrame *const current_frame = &cm->current_frame;
3605*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3606*77c1e3ccSAndroid Build Coastguard Worker   struct segmentation *const seg = &cm->seg;
3607*77c1e3ccSAndroid Build Coastguard Worker   FeatureFlags *const features = &cm->features;
3608*77c1e3ccSAndroid Build Coastguard Worker   const TileConfig *const tile_cfg = &oxcf->tile_cfg;
3609*77c1e3ccSAndroid Build Coastguard Worker   assert(cpi->source != NULL);
3610*77c1e3ccSAndroid Build Coastguard Worker   cpi->td.mb.e_mbd.cur_buf = cpi->source;
3611*77c1e3ccSAndroid Build Coastguard Worker 
3612*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
3613*77c1e3ccSAndroid Build Coastguard Worker   start_timing(cpi, encode_frame_to_data_rate_time);
3614*77c1e3ccSAndroid Build Coastguard Worker #endif
3615*77c1e3ccSAndroid Build Coastguard Worker 
3616*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
3617*77c1e3ccSAndroid Build Coastguard Worker   calculate_frame_avg_haar_energy(cpi);
3618*77c1e3ccSAndroid Build Coastguard Worker #endif
3619*77c1e3ccSAndroid Build Coastguard Worker 
3620*77c1e3ccSAndroid Build Coastguard Worker   // frame type has been decided outside of this function call
3621*77c1e3ccSAndroid Build Coastguard Worker   cm->cur_frame->frame_type = current_frame->frame_type;
3622*77c1e3ccSAndroid Build Coastguard Worker 
3623*77c1e3ccSAndroid Build Coastguard Worker   cm->tiles.large_scale = tile_cfg->enable_large_scale_tile;
3624*77c1e3ccSAndroid Build Coastguard Worker   cm->tiles.single_tile_decoding = tile_cfg->enable_single_tile_decoding;
3625*77c1e3ccSAndroid Build Coastguard Worker 
3626*77c1e3ccSAndroid Build Coastguard Worker   features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
3627*77c1e3ccSAndroid Build Coastguard Worker   // features->allow_ref_frame_mvs needs to be written into the frame header
3628*77c1e3ccSAndroid Build Coastguard Worker   // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case
3629*77c1e3ccSAndroid Build Coastguard Worker   // is separated from frame_might_allow_ref_frame_mvs().
3630*77c1e3ccSAndroid Build Coastguard Worker   features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
3631*77c1e3ccSAndroid Build Coastguard Worker 
3632*77c1e3ccSAndroid Build Coastguard Worker   features->allow_warped_motion = oxcf->motion_mode_cfg.allow_warped_motion &&
3633*77c1e3ccSAndroid Build Coastguard Worker                                   frame_might_allow_warped_motion(cm);
3634*77c1e3ccSAndroid Build Coastguard Worker 
3635*77c1e3ccSAndroid Build Coastguard Worker   cpi->last_frame_type = current_frame->frame_type;
3636*77c1e3ccSAndroid Build Coastguard Worker 
3637*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm)) {
3638*77c1e3ccSAndroid Build Coastguard Worker     cpi->frames_since_last_update = 0;
3639*77c1e3ccSAndroid Build Coastguard Worker   }
3640*77c1e3ccSAndroid Build Coastguard Worker 
3641*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_sframe(cm)) {
3642*77c1e3ccSAndroid Build Coastguard Worker     GF_GROUP *gf_group = &cpi->ppi->gf_group;
3643*77c1e3ccSAndroid Build Coastguard Worker     // S frame will wipe out any previously encoded altref so we cannot place
3644*77c1e3ccSAndroid Build Coastguard Worker     // an overlay frame
3645*77c1e3ccSAndroid Build Coastguard Worker     gf_group->update_type[gf_group->size] = GF_UPDATE;
3646*77c1e3ccSAndroid Build Coastguard Worker   }
3647*77c1e3ccSAndroid Build Coastguard Worker 
3648*77c1e3ccSAndroid Build Coastguard Worker   if (encode_show_existing_frame(cm)) {
3649*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY
3650*77c1e3ccSAndroid Build Coastguard Worker     // TODO(angiebird): Move this into a function.
3651*77c1e3ccSAndroid Build Coastguard Worker     if (oxcf->pass == AOM_RC_THIRD_PASS) {
3652*77c1e3ccSAndroid Build Coastguard Worker       int frame_coding_idx =
3653*77c1e3ccSAndroid Build Coastguard Worker           av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, cpi->gf_frame_index);
3654*77c1e3ccSAndroid Build Coastguard Worker       rc_log_frame_encode_param(
3655*77c1e3ccSAndroid Build Coastguard Worker           &cpi->rc_log, frame_coding_idx, 1, 255,
3656*77c1e3ccSAndroid Build Coastguard Worker           cpi->ppi->gf_group.update_type[cpi->gf_frame_index]);
3657*77c1e3ccSAndroid Build Coastguard Worker     }
3658*77c1e3ccSAndroid Build Coastguard Worker #endif
3659*77c1e3ccSAndroid Build Coastguard Worker     av1_finalize_encoded_frame(cpi);
3660*77c1e3ccSAndroid Build Coastguard Worker     // Build the bitstream
3661*77c1e3ccSAndroid Build Coastguard Worker     int largest_tile_id = 0;  // Output from bitstream: unused here
3662*77c1e3ccSAndroid Build Coastguard Worker     cpi->rc.coefficient_size = 0;
3663*77c1e3ccSAndroid Build Coastguard Worker     if (av1_pack_bitstream(cpi, dest, dest_size, size, &largest_tile_id) !=
3664*77c1e3ccSAndroid Build Coastguard Worker         AOM_CODEC_OK)
3665*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_ERROR;
3666*77c1e3ccSAndroid Build Coastguard Worker 
3667*77c1e3ccSAndroid Build Coastguard Worker     if (seq_params->frame_id_numbers_present_flag &&
3668*77c1e3ccSAndroid Build Coastguard Worker         current_frame->frame_type == KEY_FRAME) {
3669*77c1e3ccSAndroid Build Coastguard Worker       // Displaying a forward key-frame, so reset the ref buffer IDs
3670*77c1e3ccSAndroid Build Coastguard Worker       int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
3671*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < REF_FRAMES; i++)
3672*77c1e3ccSAndroid Build Coastguard Worker         cm->ref_frame_id[i] = display_frame_id;
3673*77c1e3ccSAndroid Build Coastguard Worker     }
3674*77c1e3ccSAndroid Build Coastguard Worker 
3675*77c1e3ccSAndroid Build Coastguard Worker #if DUMP_RECON_FRAMES == 1
3676*77c1e3ccSAndroid Build Coastguard Worker     // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
3677*77c1e3ccSAndroid Build Coastguard Worker     av1_dump_filtered_recon_frames(cpi);
3678*77c1e3ccSAndroid Build Coastguard Worker #endif  // DUMP_RECON_FRAMES
3679*77c1e3ccSAndroid Build Coastguard Worker 
3680*77c1e3ccSAndroid Build Coastguard Worker     // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3681*77c1e3ccSAndroid Build Coastguard Worker     //       for the purpose to verify no mismatch between encoder and decoder.
3682*77c1e3ccSAndroid Build Coastguard Worker     if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
3683*77c1e3ccSAndroid Build Coastguard Worker 
3684*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
3685*77c1e3ccSAndroid Build Coastguard Worker     av1_denoiser_update_ref_frame(cpi);
3686*77c1e3ccSAndroid Build Coastguard Worker #endif
3687*77c1e3ccSAndroid Build Coastguard Worker 
3688*77c1e3ccSAndroid Build Coastguard Worker     // Since we allocate a spot for the OVERLAY frame in the gf group, we need
3689*77c1e3ccSAndroid Build Coastguard Worker     // to do post-encoding update accordingly.
3690*77c1e3ccSAndroid Build Coastguard Worker     av1_set_target_rate(cpi, cm->width, cm->height);
3691*77c1e3ccSAndroid Build Coastguard Worker 
3692*77c1e3ccSAndroid Build Coastguard Worker     if (is_psnr_calc_enabled(cpi)) {
3693*77c1e3ccSAndroid Build Coastguard Worker       cpi->source =
3694*77c1e3ccSAndroid Build Coastguard Worker           realloc_and_scale_source(cpi, cm->cur_frame->buf.y_crop_width,
3695*77c1e3ccSAndroid Build Coastguard Worker                                    cm->cur_frame->buf.y_crop_height);
3696*77c1e3ccSAndroid Build Coastguard Worker     }
3697*77c1e3ccSAndroid Build Coastguard Worker 
3698*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
3699*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->use_ducky_encode) {
3700*77c1e3ccSAndroid Build Coastguard Worker       PSNR_STATS psnr;
3701*77c1e3ccSAndroid Build Coastguard Worker       aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3702*77c1e3ccSAndroid Build Coastguard Worker       DuckyEncodeFrameResult *frame_result =
3703*77c1e3ccSAndroid Build Coastguard Worker           &cpi->ducky_encode_info.frame_result;
3704*77c1e3ccSAndroid Build Coastguard Worker       frame_result->global_order_idx = cm->cur_frame->display_order_hint;
3705*77c1e3ccSAndroid Build Coastguard Worker       frame_result->q_index = cm->quant_params.base_qindex;
3706*77c1e3ccSAndroid Build Coastguard Worker       frame_result->rdmult = cpi->rd.RDMULT;
3707*77c1e3ccSAndroid Build Coastguard Worker       frame_result->rate = (int)(*size) * 8;
3708*77c1e3ccSAndroid Build Coastguard Worker       frame_result->dist = psnr.sse[0];
3709*77c1e3ccSAndroid Build Coastguard Worker       frame_result->psnr = psnr.psnr[0];
3710*77c1e3ccSAndroid Build Coastguard Worker     }
3711*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
3712*77c1e3ccSAndroid Build Coastguard Worker 
3713*77c1e3ccSAndroid Build Coastguard Worker     update_counters_for_show_frame(cpi);
3714*77c1e3ccSAndroid Build Coastguard Worker     return AOM_CODEC_OK;
3715*77c1e3ccSAndroid Build Coastguard Worker   }
3716*77c1e3ccSAndroid Build Coastguard Worker 
3717*77c1e3ccSAndroid Build Coastguard Worker   // Work out whether to force_integer_mv this frame
3718*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi) &&
3719*77c1e3ccSAndroid Build Coastguard Worker       cpi->common.features.allow_screen_content_tools &&
3720*77c1e3ccSAndroid Build Coastguard Worker       !frame_is_intra_only(cm) && !cpi->sf.rt_sf.use_nonrd_pick_mode) {
3721*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->common.seq_params->force_integer_mv == 2) {
3722*77c1e3ccSAndroid Build Coastguard Worker       // Adaptive mode: see what previous frame encoded did
3723*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->unscaled_last_source != NULL) {
3724*77c1e3ccSAndroid Build Coastguard Worker         features->cur_frame_force_integer_mv = av1_is_integer_mv(
3725*77c1e3ccSAndroid Build Coastguard Worker             cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info);
3726*77c1e3ccSAndroid Build Coastguard Worker       } else {
3727*77c1e3ccSAndroid Build Coastguard Worker         cpi->common.features.cur_frame_force_integer_mv = 0;
3728*77c1e3ccSAndroid Build Coastguard Worker       }
3729*77c1e3ccSAndroid Build Coastguard Worker     } else {
3730*77c1e3ccSAndroid Build Coastguard Worker       cpi->common.features.cur_frame_force_integer_mv =
3731*77c1e3ccSAndroid Build Coastguard Worker           cpi->common.seq_params->force_integer_mv;
3732*77c1e3ccSAndroid Build Coastguard Worker     }
3733*77c1e3ccSAndroid Build Coastguard Worker   } else {
3734*77c1e3ccSAndroid Build Coastguard Worker     cpi->common.features.cur_frame_force_integer_mv = 0;
3735*77c1e3ccSAndroid Build Coastguard Worker   }
3736*77c1e3ccSAndroid Build Coastguard Worker 
3737*77c1e3ccSAndroid Build Coastguard Worker   // This is used by av1_pack_bitstream. So this needs to be set in case of
3738*77c1e3ccSAndroid Build Coastguard Worker   // row-mt where the encoding code will use a temporary structure.
3739*77c1e3ccSAndroid Build Coastguard Worker   cpi->td.mb.e_mbd.cur_frame_force_integer_mv =
3740*77c1e3ccSAndroid Build Coastguard Worker       cpi->common.features.cur_frame_force_integer_mv;
3741*77c1e3ccSAndroid Build Coastguard Worker 
3742*77c1e3ccSAndroid Build Coastguard Worker   // Set default state for segment based loop filter update flags.
3743*77c1e3ccSAndroid Build Coastguard Worker   cm->lf.mode_ref_delta_update = 0;
3744*77c1e3ccSAndroid Build Coastguard Worker 
3745*77c1e3ccSAndroid Build Coastguard Worker   // Set various flags etc to special state if it is a key frame.
3746*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
3747*77c1e3ccSAndroid Build Coastguard Worker     // Reset the loop filter deltas and segmentation map.
3748*77c1e3ccSAndroid Build Coastguard Worker     av1_reset_segment_features(cm);
3749*77c1e3ccSAndroid Build Coastguard Worker 
3750*77c1e3ccSAndroid Build Coastguard Worker     // If segmentation is enabled force a map update for key frames.
3751*77c1e3ccSAndroid Build Coastguard Worker     if (seg->enabled) {
3752*77c1e3ccSAndroid Build Coastguard Worker       seg->update_map = 1;
3753*77c1e3ccSAndroid Build Coastguard Worker       seg->update_data = 1;
3754*77c1e3ccSAndroid Build Coastguard Worker     }
3755*77c1e3ccSAndroid Build Coastguard Worker   }
3756*77c1e3ccSAndroid Build Coastguard Worker   if (tile_cfg->mtu == 0) {
3757*77c1e3ccSAndroid Build Coastguard Worker     cpi->num_tg = tile_cfg->num_tile_groups;
3758*77c1e3ccSAndroid Build Coastguard Worker   } else {
3759*77c1e3ccSAndroid Build Coastguard Worker     // Use a default value for the purposes of weighting costs in probability
3760*77c1e3ccSAndroid Build Coastguard Worker     // updates
3761*77c1e3ccSAndroid Build Coastguard Worker     cpi->num_tg = DEFAULT_MAX_NUM_TG;
3762*77c1e3ccSAndroid Build Coastguard Worker   }
3763*77c1e3ccSAndroid Build Coastguard Worker 
3764*77c1e3ccSAndroid Build Coastguard Worker   // For 1 pass CBR mode: check if we are dropping this frame.
3765*77c1e3ccSAndroid Build Coastguard Worker   if (has_no_stats_stage(cpi) && oxcf->rc_cfg.mode == AOM_CBR) {
3766*77c1e3ccSAndroid Build Coastguard Worker     // Always drop for spatial enhancement layer if layer bandwidth is 0.
3767*77c1e3ccSAndroid Build Coastguard Worker     // Otherwise check for frame-dropping based on buffer level in
3768*77c1e3ccSAndroid Build Coastguard Worker     // av1_rc_drop_frame().
3769*77c1e3ccSAndroid Build Coastguard Worker     if ((cpi->svc.spatial_layer_id > 0 &&
3770*77c1e3ccSAndroid Build Coastguard Worker          cpi->oxcf.rc_cfg.target_bandwidth == 0) ||
3771*77c1e3ccSAndroid Build Coastguard Worker         av1_rc_drop_frame(cpi)) {
3772*77c1e3ccSAndroid Build Coastguard Worker       cpi->is_dropped_frame = true;
3773*77c1e3ccSAndroid Build Coastguard Worker     }
3774*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->is_dropped_frame) {
3775*77c1e3ccSAndroid Build Coastguard Worker       av1_setup_frame_size(cpi);
3776*77c1e3ccSAndroid Build Coastguard Worker       av1_set_mv_search_params(cpi);
3777*77c1e3ccSAndroid Build Coastguard Worker       av1_rc_postencode_update_drop_frame(cpi);
3778*77c1e3ccSAndroid Build Coastguard Worker       release_scaled_references(cpi);
3779*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.is_frame_dropped[cpi->gf_frame_index] = true;
3780*77c1e3ccSAndroid Build Coastguard Worker       // A dropped frame might not be shown but it always takes a slot in the gf
3781*77c1e3ccSAndroid Build Coastguard Worker       // group. Therefore, even when it is not shown, we still need to update
3782*77c1e3ccSAndroid Build Coastguard Worker       // the relevant frame counters.
3783*77c1e3ccSAndroid Build Coastguard Worker       if (cm->show_frame) {
3784*77c1e3ccSAndroid Build Coastguard Worker         update_counters_for_show_frame(cpi);
3785*77c1e3ccSAndroid Build Coastguard Worker       }
3786*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_OK;
3787*77c1e3ccSAndroid Build Coastguard Worker     }
3788*77c1e3ccSAndroid Build Coastguard Worker   }
3789*77c1e3ccSAndroid Build Coastguard Worker 
3790*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->tune_cfg.tuning == AOM_TUNE_SSIM) {
3791*77c1e3ccSAndroid Build Coastguard Worker     av1_set_mb_ssim_rdmult_scaling(cpi);
3792*77c1e3ccSAndroid Build Coastguard Worker   }
3793*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_SALIENCY_MAP
3794*77c1e3ccSAndroid Build Coastguard Worker   else if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_SALIENCY_MAP &&
3795*77c1e3ccSAndroid Build Coastguard Worker            !(cpi->source->flags & YV12_FLAG_HIGHBITDEPTH)) {
3796*77c1e3ccSAndroid Build Coastguard Worker     if (av1_set_saliency_map(cpi) == 0) {
3797*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_MEM_ERROR;
3798*77c1e3ccSAndroid Build Coastguard Worker     }
3799*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
3800*77c1e3ccSAndroid Build Coastguard Worker     double motion_ratio = av1_setup_motion_ratio(cpi);
3801*77c1e3ccSAndroid Build Coastguard Worker #else
3802*77c1e3ccSAndroid Build Coastguard Worker     double motion_ratio = 1.0;
3803*77c1e3ccSAndroid Build Coastguard Worker #endif
3804*77c1e3ccSAndroid Build Coastguard Worker     if (av1_setup_sm_rdmult_scaling_factor(cpi, motion_ratio) == 0) {
3805*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_MEM_ERROR;
3806*77c1e3ccSAndroid Build Coastguard Worker     }
3807*77c1e3ccSAndroid Build Coastguard Worker   }
3808*77c1e3ccSAndroid Build Coastguard Worker #endif
3809*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
3810*77c1e3ccSAndroid Build Coastguard Worker   else if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
3811*77c1e3ccSAndroid Build Coastguard Worker            oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN ||
3812*77c1e3ccSAndroid Build Coastguard Worker            oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
3813*77c1e3ccSAndroid Build Coastguard Worker     av1_set_mb_vmaf_rdmult_scaling(cpi);
3814*77c1e3ccSAndroid Build Coastguard Worker   }
3815*77c1e3ccSAndroid Build Coastguard Worker #endif
3816*77c1e3ccSAndroid Build Coastguard Worker 
3817*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL_AI &&
3818*77c1e3ccSAndroid Build Coastguard Worker       cpi->sf.rt_sf.use_nonrd_pick_mode == 0) {
3819*77c1e3ccSAndroid Build Coastguard Worker     av1_init_mb_wiener_var_buffer(cpi);
3820*77c1e3ccSAndroid Build Coastguard Worker     av1_set_mb_wiener_variance(cpi);
3821*77c1e3ccSAndroid Build Coastguard Worker   }
3822*77c1e3ccSAndroid Build Coastguard Worker 
3823*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_USER_RATING_BASED) {
3824*77c1e3ccSAndroid Build Coastguard Worker     av1_init_mb_ur_var_buffer(cpi);
3825*77c1e3ccSAndroid Build Coastguard Worker     av1_set_mb_ur_variance(cpi);
3826*77c1e3ccSAndroid Build Coastguard Worker   }
3827*77c1e3ccSAndroid Build Coastguard Worker 
3828*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
3829*77c1e3ccSAndroid Build Coastguard Worker   memset(cpi->mode_chosen_counts, 0,
3830*77c1e3ccSAndroid Build Coastguard Worker          MAX_MODES * sizeof(*cpi->mode_chosen_counts));
3831*77c1e3ccSAndroid Build Coastguard Worker #endif
3832*77c1e3ccSAndroid Build Coastguard Worker 
3833*77c1e3ccSAndroid Build Coastguard Worker   if (seq_params->frame_id_numbers_present_flag) {
3834*77c1e3ccSAndroid Build Coastguard Worker     /* Non-normative definition of current_frame_id ("frame counter" with
3835*77c1e3ccSAndroid Build Coastguard Worker      * wraparound) */
3836*77c1e3ccSAndroid Build Coastguard Worker     if (cm->current_frame_id == -1) {
3837*77c1e3ccSAndroid Build Coastguard Worker       int lsb, msb;
3838*77c1e3ccSAndroid Build Coastguard Worker       /* quasi-random initialization of current_frame_id for a key frame */
3839*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
3840*77c1e3ccSAndroid Build Coastguard Worker         lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
3841*77c1e3ccSAndroid Build Coastguard Worker         msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
3842*77c1e3ccSAndroid Build Coastguard Worker       } else {
3843*77c1e3ccSAndroid Build Coastguard Worker         lsb = cpi->source->y_buffer[0] & 0xff;
3844*77c1e3ccSAndroid Build Coastguard Worker         msb = cpi->source->y_buffer[1] & 0xff;
3845*77c1e3ccSAndroid Build Coastguard Worker       }
3846*77c1e3ccSAndroid Build Coastguard Worker       cm->current_frame_id =
3847*77c1e3ccSAndroid Build Coastguard Worker           ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
3848*77c1e3ccSAndroid Build Coastguard Worker 
3849*77c1e3ccSAndroid Build Coastguard Worker       // S_frame is meant for stitching different streams of different
3850*77c1e3ccSAndroid Build Coastguard Worker       // resolutions together, so current_frame_id must be the
3851*77c1e3ccSAndroid Build Coastguard Worker       // same across different streams of the same content current_frame_id
3852*77c1e3ccSAndroid Build Coastguard Worker       // should be the same and not random. 0x37 is a chosen number as start
3853*77c1e3ccSAndroid Build Coastguard Worker       // point
3854*77c1e3ccSAndroid Build Coastguard Worker       if (oxcf->kf_cfg.sframe_dist != 0) cm->current_frame_id = 0x37;
3855*77c1e3ccSAndroid Build Coastguard Worker     } else {
3856*77c1e3ccSAndroid Build Coastguard Worker       cm->current_frame_id =
3857*77c1e3ccSAndroid Build Coastguard Worker           (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
3858*77c1e3ccSAndroid Build Coastguard Worker           (1 << seq_params->frame_id_length);
3859*77c1e3ccSAndroid Build Coastguard Worker     }
3860*77c1e3ccSAndroid Build Coastguard Worker   }
3861*77c1e3ccSAndroid Build Coastguard Worker 
3862*77c1e3ccSAndroid Build Coastguard Worker   switch (oxcf->algo_cfg.cdf_update_mode) {
3863*77c1e3ccSAndroid Build Coastguard Worker     case 0:  // No CDF update for any frames(4~6% compression loss).
3864*77c1e3ccSAndroid Build Coastguard Worker       features->disable_cdf_update = 1;
3865*77c1e3ccSAndroid Build Coastguard Worker       break;
3866*77c1e3ccSAndroid Build Coastguard Worker     case 1:  // Enable CDF update for all frames.
3867*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->sf.rt_sf.disable_cdf_update_non_reference_frame &&
3868*77c1e3ccSAndroid Build Coastguard Worker           cpi->ppi->rtc_ref.non_reference_frame && cpi->rc.frames_since_key > 2)
3869*77c1e3ccSAndroid Build Coastguard Worker         features->disable_cdf_update = 1;
3870*77c1e3ccSAndroid Build Coastguard Worker       else if (cpi->sf.rt_sf.selective_cdf_update)
3871*77c1e3ccSAndroid Build Coastguard Worker         features->disable_cdf_update = selective_disable_cdf_rtc(cpi);
3872*77c1e3ccSAndroid Build Coastguard Worker       else
3873*77c1e3ccSAndroid Build Coastguard Worker         features->disable_cdf_update = 0;
3874*77c1e3ccSAndroid Build Coastguard Worker       break;
3875*77c1e3ccSAndroid Build Coastguard Worker     case 2:
3876*77c1e3ccSAndroid Build Coastguard Worker       // Strategically determine at which frames to do CDF update.
3877*77c1e3ccSAndroid Build Coastguard Worker       // Currently only enable CDF update for all-intra and no-show frames(1.5%
3878*77c1e3ccSAndroid Build Coastguard Worker       // compression loss) for good qualiy or allintra mode.
3879*77c1e3ccSAndroid Build Coastguard Worker       if (oxcf->mode == GOOD || oxcf->mode == ALLINTRA) {
3880*77c1e3ccSAndroid Build Coastguard Worker         features->disable_cdf_update =
3881*77c1e3ccSAndroid Build Coastguard Worker             (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
3882*77c1e3ccSAndroid Build Coastguard Worker       } else {
3883*77c1e3ccSAndroid Build Coastguard Worker         features->disable_cdf_update = selective_disable_cdf_rtc(cpi);
3884*77c1e3ccSAndroid Build Coastguard Worker       }
3885*77c1e3ccSAndroid Build Coastguard Worker       break;
3886*77c1e3ccSAndroid Build Coastguard Worker   }
3887*77c1e3ccSAndroid Build Coastguard Worker 
3888*77c1e3ccSAndroid Build Coastguard Worker   // Disable cdf update for the INTNL_ARF_UPDATE frame with
3889*77c1e3ccSAndroid Build Coastguard Worker   // frame_parallel_level 1.
3890*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->do_frame_data_update &&
3891*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
3892*77c1e3ccSAndroid Build Coastguard Worker     assert(cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 1);
3893*77c1e3ccSAndroid Build Coastguard Worker     features->disable_cdf_update = 1;
3894*77c1e3ccSAndroid Build Coastguard Worker   }
3895*77c1e3ccSAndroid Build Coastguard Worker 
3896*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
3897*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.tool_cfg.enable_global_motion && !frame_is_intra_only(cm)) {
3898*77c1e3ccSAndroid Build Coastguard Worker     // Flush any stale global motion information, which may be left over
3899*77c1e3ccSAndroid Build Coastguard Worker     // from a previous frame
3900*77c1e3ccSAndroid Build Coastguard Worker     aom_invalidate_pyramid(cpi->source->y_pyramid);
3901*77c1e3ccSAndroid Build Coastguard Worker     av1_invalidate_corner_list(cpi->source->corners);
3902*77c1e3ccSAndroid Build Coastguard Worker   }
3903*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
3904*77c1e3ccSAndroid Build Coastguard Worker 
3905*77c1e3ccSAndroid Build Coastguard Worker   int largest_tile_id = 0;
3906*77c1e3ccSAndroid Build Coastguard Worker   if (av1_superres_in_recode_allowed(cpi)) {
3907*77c1e3ccSAndroid Build Coastguard Worker     if (encode_with_and_without_superres(cpi, size, dest, dest_size,
3908*77c1e3ccSAndroid Build Coastguard Worker                                          &largest_tile_id) != AOM_CODEC_OK) {
3909*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_ERROR;
3910*77c1e3ccSAndroid Build Coastguard Worker     }
3911*77c1e3ccSAndroid Build Coastguard Worker   } else {
3912*77c1e3ccSAndroid Build Coastguard Worker     const aom_superres_mode orig_superres_mode = cpi->superres_mode;  // save
3913*77c1e3ccSAndroid Build Coastguard Worker     cpi->superres_mode = cpi->oxcf.superres_cfg.superres_mode;
3914*77c1e3ccSAndroid Build Coastguard Worker     if (encode_with_recode_loop_and_filter(cpi, size, dest, dest_size, NULL,
3915*77c1e3ccSAndroid Build Coastguard Worker                                            NULL,
3916*77c1e3ccSAndroid Build Coastguard Worker                                            &largest_tile_id) != AOM_CODEC_OK) {
3917*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_ERROR;
3918*77c1e3ccSAndroid Build Coastguard Worker     }
3919*77c1e3ccSAndroid Build Coastguard Worker     cpi->superres_mode = orig_superres_mode;  // restore
3920*77c1e3ccSAndroid Build Coastguard Worker   }
3921*77c1e3ccSAndroid Build Coastguard Worker 
3922*77c1e3ccSAndroid Build Coastguard Worker   // Update reference frame ids for reference frames this frame will overwrite
3923*77c1e3ccSAndroid Build Coastguard Worker   if (seq_params->frame_id_numbers_present_flag) {
3924*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < REF_FRAMES; i++) {
3925*77c1e3ccSAndroid Build Coastguard Worker       if ((current_frame->refresh_frame_flags >> i) & 1) {
3926*77c1e3ccSAndroid Build Coastguard Worker         cm->ref_frame_id[i] = cm->current_frame_id;
3927*77c1e3ccSAndroid Build Coastguard Worker       }
3928*77c1e3ccSAndroid Build Coastguard Worker     }
3929*77c1e3ccSAndroid Build Coastguard Worker   }
3930*77c1e3ccSAndroid Build Coastguard Worker 
3931*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
3932*77c1e3ccSAndroid Build Coastguard Worker     cpi->svc.num_encoded_top_layer++;
3933*77c1e3ccSAndroid Build Coastguard Worker 
3934*77c1e3ccSAndroid Build Coastguard Worker #if DUMP_RECON_FRAMES == 1
3935*77c1e3ccSAndroid Build Coastguard Worker   // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
3936*77c1e3ccSAndroid Build Coastguard Worker   av1_dump_filtered_recon_frames(cpi);
3937*77c1e3ccSAndroid Build Coastguard Worker #endif  // DUMP_RECON_FRAMES
3938*77c1e3ccSAndroid Build Coastguard Worker 
3939*77c1e3ccSAndroid Build Coastguard Worker   if (cm->seg.enabled) {
3940*77c1e3ccSAndroid Build Coastguard Worker     if (cm->seg.update_map == 0 && cm->last_frame_seg_map) {
3941*77c1e3ccSAndroid Build Coastguard Worker       memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
3942*77c1e3ccSAndroid Build Coastguard Worker              cm->cur_frame->mi_cols * cm->cur_frame->mi_rows *
3943*77c1e3ccSAndroid Build Coastguard Worker                  sizeof(*cm->cur_frame->seg_map));
3944*77c1e3ccSAndroid Build Coastguard Worker     }
3945*77c1e3ccSAndroid Build Coastguard Worker   }
3946*77c1e3ccSAndroid Build Coastguard Worker 
3947*77c1e3ccSAndroid Build Coastguard Worker   int release_scaled_refs = 0;
3948*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
3949*77c1e3ccSAndroid Build Coastguard Worker   release_scaled_refs =
3950*77c1e3ccSAndroid Build Coastguard Worker       (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 1 : 0;
3951*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_FPMT_TEST
3952*77c1e3ccSAndroid Build Coastguard Worker   if (release_scaled_refs ||
3953*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) {
3954*77c1e3ccSAndroid Build Coastguard Worker     if (frame_is_intra_only(cm) == 0) {
3955*77c1e3ccSAndroid Build Coastguard Worker       release_scaled_references(cpi);
3956*77c1e3ccSAndroid Build Coastguard Worker     }
3957*77c1e3ccSAndroid Build Coastguard Worker   }
3958*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
3959*77c1e3ccSAndroid Build Coastguard Worker   av1_denoiser_update_ref_frame(cpi);
3960*77c1e3ccSAndroid Build Coastguard Worker #endif
3961*77c1e3ccSAndroid Build Coastguard Worker 
3962*77c1e3ccSAndroid Build Coastguard Worker   // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3963*77c1e3ccSAndroid Build Coastguard Worker   //       for the purpose to verify no mismatch between encoder and decoder.
3964*77c1e3ccSAndroid Build Coastguard Worker   if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
3965*77c1e3ccSAndroid Build Coastguard Worker 
3966*77c1e3ccSAndroid Build Coastguard Worker   if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3967*77c1e3ccSAndroid Build Coastguard Worker     *cm->fc = cpi->tile_data[largest_tile_id].tctx;
3968*77c1e3ccSAndroid Build Coastguard Worker     av1_reset_cdf_symbol_counters(cm->fc);
3969*77c1e3ccSAndroid Build Coastguard Worker   }
3970*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->tiles.large_scale) {
3971*77c1e3ccSAndroid Build Coastguard Worker     cm->cur_frame->frame_context = *cm->fc;
3972*77c1e3ccSAndroid Build Coastguard Worker   }
3973*77c1e3ccSAndroid Build Coastguard Worker 
3974*77c1e3ccSAndroid Build Coastguard Worker   if (tile_cfg->enable_ext_tile_debug) {
3975*77c1e3ccSAndroid Build Coastguard Worker     // (yunqing) This test ensures the correctness of large scale tile coding.
3976*77c1e3ccSAndroid Build Coastguard Worker     if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) {
3977*77c1e3ccSAndroid Build Coastguard Worker       char fn[20] = "./fc";
3978*77c1e3ccSAndroid Build Coastguard Worker       fn[4] = current_frame->frame_number / 100 + '0';
3979*77c1e3ccSAndroid Build Coastguard Worker       fn[5] = (current_frame->frame_number % 100) / 10 + '0';
3980*77c1e3ccSAndroid Build Coastguard Worker       fn[6] = (current_frame->frame_number % 10) + '0';
3981*77c1e3ccSAndroid Build Coastguard Worker       fn[7] = '\0';
3982*77c1e3ccSAndroid Build Coastguard Worker       av1_print_frame_contexts(cm->fc, fn);
3983*77c1e3ccSAndroid Build Coastguard Worker     }
3984*77c1e3ccSAndroid Build Coastguard Worker   }
3985*77c1e3ccSAndroid Build Coastguard Worker 
3986*77c1e3ccSAndroid Build Coastguard Worker   cpi->last_frame_type = current_frame->frame_type;
3987*77c1e3ccSAndroid Build Coastguard Worker 
3988*77c1e3ccSAndroid Build Coastguard Worker   if (cm->features.disable_cdf_update) {
3989*77c1e3ccSAndroid Build Coastguard Worker     cpi->frames_since_last_update++;
3990*77c1e3ccSAndroid Build Coastguard Worker   } else {
3991*77c1e3ccSAndroid Build Coastguard Worker     cpi->frames_since_last_update = 1;
3992*77c1e3ccSAndroid Build Coastguard Worker   }
3993*77c1e3ccSAndroid Build Coastguard Worker 
3994*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
3995*77c1e3ccSAndroid Build Coastguard Worker     cpi->svc.prev_number_spatial_layers = cpi->svc.number_spatial_layers;
3996*77c1e3ccSAndroid Build Coastguard Worker 
3997*77c1e3ccSAndroid Build Coastguard Worker   // Clear the one shot update flags for segmentation map and mode/ref loop
3998*77c1e3ccSAndroid Build Coastguard Worker   // filter deltas.
3999*77c1e3ccSAndroid Build Coastguard Worker   cm->seg.update_map = 0;
4000*77c1e3ccSAndroid Build Coastguard Worker   cm->seg.update_data = 0;
4001*77c1e3ccSAndroid Build Coastguard Worker   cm->lf.mode_ref_delta_update = 0;
4002*77c1e3ccSAndroid Build Coastguard Worker 
4003*77c1e3ccSAndroid Build Coastguard Worker   if (cm->show_frame) {
4004*77c1e3ccSAndroid Build Coastguard Worker     update_counters_for_show_frame(cpi);
4005*77c1e3ccSAndroid Build Coastguard Worker   }
4006*77c1e3ccSAndroid Build Coastguard Worker 
4007*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
4008*77c1e3ccSAndroid Build Coastguard Worker   end_timing(cpi, encode_frame_to_data_rate_time);
4009*77c1e3ccSAndroid Build Coastguard Worker #endif
4010*77c1e3ccSAndroid Build Coastguard Worker 
4011*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
4012*77c1e3ccSAndroid Build Coastguard Worker }
4013*77c1e3ccSAndroid Build Coastguard Worker 
av1_encode(AV1_COMP * const cpi,uint8_t * const dest,size_t dest_size,const EncodeFrameInput * const frame_input,const EncodeFrameParams * const frame_params,size_t * const frame_size)4014*77c1e3ccSAndroid Build Coastguard Worker int av1_encode(AV1_COMP *const cpi, uint8_t *const dest, size_t dest_size,
4015*77c1e3ccSAndroid Build Coastguard Worker                const EncodeFrameInput *const frame_input,
4016*77c1e3ccSAndroid Build Coastguard Worker                const EncodeFrameParams *const frame_params,
4017*77c1e3ccSAndroid Build Coastguard Worker                size_t *const frame_size) {
4018*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
4019*77c1e3ccSAndroid Build Coastguard Worker   CurrentFrame *const current_frame = &cm->current_frame;
4020*77c1e3ccSAndroid Build Coastguard Worker 
4021*77c1e3ccSAndroid Build Coastguard Worker   cpi->unscaled_source = frame_input->source;
4022*77c1e3ccSAndroid Build Coastguard Worker   cpi->source = frame_input->source;
4023*77c1e3ccSAndroid Build Coastguard Worker   cpi->unscaled_last_source = frame_input->last_source;
4024*77c1e3ccSAndroid Build Coastguard Worker 
4025*77c1e3ccSAndroid Build Coastguard Worker   current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
4026*77c1e3ccSAndroid Build Coastguard Worker   cm->features.error_resilient_mode = frame_params->error_resilient_mode;
4027*77c1e3ccSAndroid Build Coastguard Worker   cm->features.primary_ref_frame = frame_params->primary_ref_frame;
4028*77c1e3ccSAndroid Build Coastguard Worker   cm->current_frame.frame_type = frame_params->frame_type;
4029*77c1e3ccSAndroid Build Coastguard Worker   cm->show_frame = frame_params->show_frame;
4030*77c1e3ccSAndroid Build Coastguard Worker   cpi->ref_frame_flags = frame_params->ref_frame_flags;
4031*77c1e3ccSAndroid Build Coastguard Worker   cpi->speed = frame_params->speed;
4032*77c1e3ccSAndroid Build Coastguard Worker   cm->show_existing_frame = frame_params->show_existing_frame;
4033*77c1e3ccSAndroid Build Coastguard Worker   cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
4034*77c1e3ccSAndroid Build Coastguard Worker 
4035*77c1e3ccSAndroid Build Coastguard Worker   memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
4036*77c1e3ccSAndroid Build Coastguard Worker          REF_FRAMES * sizeof(*cm->remapped_ref_idx));
4037*77c1e3ccSAndroid Build Coastguard Worker 
4038*77c1e3ccSAndroid Build Coastguard Worker   memcpy(&cpi->refresh_frame, &frame_params->refresh_frame,
4039*77c1e3ccSAndroid Build Coastguard Worker          sizeof(cpi->refresh_frame));
4040*77c1e3ccSAndroid Build Coastguard Worker 
4041*77c1e3ccSAndroid Build Coastguard Worker   if (current_frame->frame_type == KEY_FRAME &&
4042*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) {
4043*77c1e3ccSAndroid Build Coastguard Worker     current_frame->frame_number = 0;
4044*77c1e3ccSAndroid Build Coastguard Worker   }
4045*77c1e3ccSAndroid Build Coastguard Worker 
4046*77c1e3ccSAndroid Build Coastguard Worker   current_frame->order_hint =
4047*77c1e3ccSAndroid Build Coastguard Worker       current_frame->frame_number + frame_params->order_offset;
4048*77c1e3ccSAndroid Build Coastguard Worker 
4049*77c1e3ccSAndroid Build Coastguard Worker   current_frame->display_order_hint = current_frame->order_hint;
4050*77c1e3ccSAndroid Build Coastguard Worker   current_frame->order_hint %=
4051*77c1e3ccSAndroid Build Coastguard Worker       (1 << (cm->seq_params->order_hint_info.order_hint_bits_minus_1 + 1));
4052*77c1e3ccSAndroid Build Coastguard Worker 
4053*77c1e3ccSAndroid Build Coastguard Worker   current_frame->pyramid_level = get_true_pyr_level(
4054*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index],
4055*77c1e3ccSAndroid Build Coastguard Worker       current_frame->display_order_hint, cpi->ppi->gf_group.max_layer_depth);
4056*77c1e3ccSAndroid Build Coastguard Worker 
4057*77c1e3ccSAndroid Build Coastguard Worker   if (is_stat_generation_stage(cpi)) {
4058*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
4059*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->oxcf.q_cfg.use_fixed_qp_offsets)
4060*77c1e3ccSAndroid Build Coastguard Worker       av1_noop_first_pass_frame(cpi, frame_input->ts_duration);
4061*77c1e3ccSAndroid Build Coastguard Worker     else
4062*77c1e3ccSAndroid Build Coastguard Worker       av1_first_pass(cpi, frame_input->ts_duration);
4063*77c1e3ccSAndroid Build Coastguard Worker #endif
4064*77c1e3ccSAndroid Build Coastguard Worker   } else if (cpi->oxcf.pass == AOM_RC_ONE_PASS ||
4065*77c1e3ccSAndroid Build Coastguard Worker              cpi->oxcf.pass >= AOM_RC_SECOND_PASS) {
4066*77c1e3ccSAndroid Build Coastguard Worker     if (encode_frame_to_data_rate(cpi, frame_size, dest, dest_size) !=
4067*77c1e3ccSAndroid Build Coastguard Worker         AOM_CODEC_OK) {
4068*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_ERROR;
4069*77c1e3ccSAndroid Build Coastguard Worker     }
4070*77c1e3ccSAndroid Build Coastguard Worker   } else {
4071*77c1e3ccSAndroid Build Coastguard Worker     return AOM_CODEC_ERROR;
4072*77c1e3ccSAndroid Build Coastguard Worker   }
4073*77c1e3ccSAndroid Build Coastguard Worker 
4074*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
4075*77c1e3ccSAndroid Build Coastguard Worker }
4076*77c1e3ccSAndroid Build Coastguard Worker 
4077*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_DENOISE && !CONFIG_REALTIME_ONLY
apply_denoise_2d(AV1_COMP * cpi,const YV12_BUFFER_CONFIG * sd,int block_size,float noise_level,int64_t time_stamp,int64_t end_time)4078*77c1e3ccSAndroid Build Coastguard Worker static int apply_denoise_2d(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *sd,
4079*77c1e3ccSAndroid Build Coastguard Worker                             int block_size, float noise_level,
4080*77c1e3ccSAndroid Build Coastguard Worker                             int64_t time_stamp, int64_t end_time) {
4081*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
4082*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->denoise_and_model) {
4083*77c1e3ccSAndroid Build Coastguard Worker     cpi->denoise_and_model = aom_denoise_and_model_alloc(
4084*77c1e3ccSAndroid Build Coastguard Worker         cm->seq_params->bit_depth, block_size, noise_level);
4085*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->denoise_and_model) {
4086*77c1e3ccSAndroid Build Coastguard Worker       aom_set_error(cm->error, AOM_CODEC_MEM_ERROR,
4087*77c1e3ccSAndroid Build Coastguard Worker                     "Error allocating denoise and model");
4088*77c1e3ccSAndroid Build Coastguard Worker       return -1;
4089*77c1e3ccSAndroid Build Coastguard Worker     }
4090*77c1e3ccSAndroid Build Coastguard Worker   }
4091*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->film_grain_table) {
4092*77c1e3ccSAndroid Build Coastguard Worker     cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
4093*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->film_grain_table) {
4094*77c1e3ccSAndroid Build Coastguard Worker       aom_set_error(cm->error, AOM_CODEC_MEM_ERROR,
4095*77c1e3ccSAndroid Build Coastguard Worker                     "Error allocating grain table");
4096*77c1e3ccSAndroid Build Coastguard Worker       return -1;
4097*77c1e3ccSAndroid Build Coastguard Worker     }
4098*77c1e3ccSAndroid Build Coastguard Worker     memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
4099*77c1e3ccSAndroid Build Coastguard Worker   }
4100*77c1e3ccSAndroid Build Coastguard Worker   if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
4101*77c1e3ccSAndroid Build Coastguard Worker                                 &cm->film_grain_params,
4102*77c1e3ccSAndroid Build Coastguard Worker                                 cpi->oxcf.enable_dnl_denoising)) {
4103*77c1e3ccSAndroid Build Coastguard Worker     if (cm->film_grain_params.apply_grain) {
4104*77c1e3ccSAndroid Build Coastguard Worker       aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
4105*77c1e3ccSAndroid Build Coastguard Worker                                   &cm->film_grain_params);
4106*77c1e3ccSAndroid Build Coastguard Worker     }
4107*77c1e3ccSAndroid Build Coastguard Worker   }
4108*77c1e3ccSAndroid Build Coastguard Worker   return 0;
4109*77c1e3ccSAndroid Build Coastguard Worker }
4110*77c1e3ccSAndroid Build Coastguard Worker #endif
4111*77c1e3ccSAndroid Build Coastguard Worker 
av1_receive_raw_frame(AV1_COMP * cpi,aom_enc_frame_flags_t frame_flags,const YV12_BUFFER_CONFIG * sd,int64_t time_stamp,int64_t end_time)4112*77c1e3ccSAndroid Build Coastguard Worker int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
4113*77c1e3ccSAndroid Build Coastguard Worker                           const YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4114*77c1e3ccSAndroid Build Coastguard Worker                           int64_t end_time) {
4115*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
4116*77c1e3ccSAndroid Build Coastguard Worker   const SequenceHeader *const seq_params = cm->seq_params;
4117*77c1e3ccSAndroid Build Coastguard Worker   int res = 0;
4118*77c1e3ccSAndroid Build Coastguard Worker   const int subsampling_x = sd->subsampling_x;
4119*77c1e3ccSAndroid Build Coastguard Worker   const int subsampling_y = sd->subsampling_y;
4120*77c1e3ccSAndroid Build Coastguard Worker   const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
4121*77c1e3ccSAndroid Build Coastguard Worker 
4122*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
4123*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi) &&
4124*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
4125*77c1e3ccSAndroid Build Coastguard Worker     av1_vmaf_frame_preprocessing(cpi, sd);
4126*77c1e3ccSAndroid Build Coastguard Worker   }
4127*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi) &&
4128*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
4129*77c1e3ccSAndroid Build Coastguard Worker     av1_vmaf_blk_preprocessing(cpi, sd);
4130*77c1e3ccSAndroid Build Coastguard Worker   }
4131*77c1e3ccSAndroid Build Coastguard Worker #endif
4132*77c1e3ccSAndroid Build Coastguard Worker 
4133*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
4134*77c1e3ccSAndroid Build Coastguard Worker   struct aom_usec_timer timer;
4135*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_start(&timer);
4136*77c1e3ccSAndroid Build Coastguard Worker #endif
4137*77c1e3ccSAndroid Build Coastguard Worker 
4138*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
4139*77c1e3ccSAndroid Build Coastguard Worker   setup_denoiser_buffer(cpi);
4140*77c1e3ccSAndroid Build Coastguard Worker #endif
4141*77c1e3ccSAndroid Build Coastguard Worker 
4142*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_DENOISE
4143*77c1e3ccSAndroid Build Coastguard Worker   // even if denoise_noise_level is > 0, we don't need need to denoise on pass
4144*77c1e3ccSAndroid Build Coastguard Worker   // 1 of 2 if enable_dnl_denoising is disabled since the 2nd pass will be
4145*77c1e3ccSAndroid Build Coastguard Worker   // encoding the original (non-denoised) frame
4146*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.noise_level > 0 && !(cpi->oxcf.pass == AOM_RC_FIRST_PASS &&
4147*77c1e3ccSAndroid Build Coastguard Worker                                      !cpi->oxcf.enable_dnl_denoising)) {
4148*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
4149*77c1e3ccSAndroid Build Coastguard Worker     // Choose a synthetic noise level for still images for enhanced perceptual
4150*77c1e3ccSAndroid Build Coastguard Worker     // quality based on an estimated noise level in the source, but only if
4151*77c1e3ccSAndroid Build Coastguard Worker     // the noise level is set on the command line to > 0.
4152*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->oxcf.mode == ALLINTRA) {
4153*77c1e3ccSAndroid Build Coastguard Worker       // No noise synthesis if source is very clean.
4154*77c1e3ccSAndroid Build Coastguard Worker       // Uses a low edge threshold to focus on smooth areas.
4155*77c1e3ccSAndroid Build Coastguard Worker       // Increase output noise setting a little compared to measured value.
4156*77c1e3ccSAndroid Build Coastguard Worker       double y_noise_level = 0.0;
4157*77c1e3ccSAndroid Build Coastguard Worker       av1_estimate_noise_level(sd, &y_noise_level, AOM_PLANE_Y, AOM_PLANE_Y,
4158*77c1e3ccSAndroid Build Coastguard Worker                                cm->seq_params->bit_depth, 16);
4159*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.noise_level = (float)(y_noise_level - 0.1);
4160*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.noise_level = (float)AOMMAX(0.0, cpi->oxcf.noise_level);
4161*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->oxcf.noise_level > 0.0) {
4162*77c1e3ccSAndroid Build Coastguard Worker         cpi->oxcf.noise_level += (float)0.5;
4163*77c1e3ccSAndroid Build Coastguard Worker       }
4164*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.noise_level = (float)AOMMIN(5.0, cpi->oxcf.noise_level);
4165*77c1e3ccSAndroid Build Coastguard Worker     }
4166*77c1e3ccSAndroid Build Coastguard Worker 
4167*77c1e3ccSAndroid Build Coastguard Worker     if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
4168*77c1e3ccSAndroid Build Coastguard Worker                          cpi->oxcf.noise_level, time_stamp, end_time) < 0)
4169*77c1e3ccSAndroid Build Coastguard Worker       res = -1;
4170*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
4171*77c1e3ccSAndroid Build Coastguard Worker   }
4172*77c1e3ccSAndroid Build Coastguard Worker #endif  //  CONFIG_DENOISE
4173*77c1e3ccSAndroid Build Coastguard Worker 
4174*77c1e3ccSAndroid Build Coastguard Worker   if (av1_lookahead_push(cpi->ppi->lookahead, sd, time_stamp, end_time,
4175*77c1e3ccSAndroid Build Coastguard Worker                          use_highbitdepth, cpi->alloc_pyramid, frame_flags)) {
4176*77c1e3ccSAndroid Build Coastguard Worker     aom_set_error(cm->error, AOM_CODEC_ERROR, "av1_lookahead_push() failed");
4177*77c1e3ccSAndroid Build Coastguard Worker     res = -1;
4178*77c1e3ccSAndroid Build Coastguard Worker   }
4179*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
4180*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_mark(&timer);
4181*77c1e3ccSAndroid Build Coastguard Worker   cpi->ppi->total_time_receive_data += aom_usec_timer_elapsed(&timer);
4182*77c1e3ccSAndroid Build Coastguard Worker #endif
4183*77c1e3ccSAndroid Build Coastguard Worker 
4184*77c1e3ccSAndroid Build Coastguard Worker   // Note: Regarding profile setting, the following checks are added to help
4185*77c1e3ccSAndroid Build Coastguard Worker   // choose a proper profile for the input video. The criterion is that all
4186*77c1e3ccSAndroid Build Coastguard Worker   // bitstreams must be designated as the lowest profile that match its content.
4187*77c1e3ccSAndroid Build Coastguard Worker   // E.G. A bitstream that contains 4:4:4 video must be designated as High
4188*77c1e3ccSAndroid Build Coastguard Worker   // Profile in the seq header, and likewise a bitstream that contains 4:2:2
4189*77c1e3ccSAndroid Build Coastguard Worker   // bitstream must be designated as Professional Profile in the sequence
4190*77c1e3ccSAndroid Build Coastguard Worker   // header.
4191*77c1e3ccSAndroid Build Coastguard Worker   if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
4192*77c1e3ccSAndroid Build Coastguard Worker       (subsampling_x != 1 || subsampling_y != 1)) {
4193*77c1e3ccSAndroid Build Coastguard Worker     aom_set_error(cm->error, AOM_CODEC_INVALID_PARAM,
4194*77c1e3ccSAndroid Build Coastguard Worker                   "Non-4:2:0 color format requires profile 1 or 2");
4195*77c1e3ccSAndroid Build Coastguard Worker     res = -1;
4196*77c1e3ccSAndroid Build Coastguard Worker   }
4197*77c1e3ccSAndroid Build Coastguard Worker   if ((seq_params->profile == PROFILE_1) &&
4198*77c1e3ccSAndroid Build Coastguard Worker       !(subsampling_x == 0 && subsampling_y == 0)) {
4199*77c1e3ccSAndroid Build Coastguard Worker     aom_set_error(cm->error, AOM_CODEC_INVALID_PARAM,
4200*77c1e3ccSAndroid Build Coastguard Worker                   "Profile 1 requires 4:4:4 color format");
4201*77c1e3ccSAndroid Build Coastguard Worker     res = -1;
4202*77c1e3ccSAndroid Build Coastguard Worker   }
4203*77c1e3ccSAndroid Build Coastguard Worker   if ((seq_params->profile == PROFILE_2) &&
4204*77c1e3ccSAndroid Build Coastguard Worker       (seq_params->bit_depth <= AOM_BITS_10) &&
4205*77c1e3ccSAndroid Build Coastguard Worker       !(subsampling_x == 1 && subsampling_y == 0)) {
4206*77c1e3ccSAndroid Build Coastguard Worker     aom_set_error(cm->error, AOM_CODEC_INVALID_PARAM,
4207*77c1e3ccSAndroid Build Coastguard Worker                   "Profile 2 bit-depth <= 10 requires 4:2:2 color format");
4208*77c1e3ccSAndroid Build Coastguard Worker     res = -1;
4209*77c1e3ccSAndroid Build Coastguard Worker   }
4210*77c1e3ccSAndroid Build Coastguard Worker 
4211*77c1e3ccSAndroid Build Coastguard Worker   return res;
4212*77c1e3ccSAndroid Build Coastguard Worker }
4213*77c1e3ccSAndroid Build Coastguard Worker 
4214*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
print_entropy_stats(AV1_PRIMARY * const ppi)4215*77c1e3ccSAndroid Build Coastguard Worker void print_entropy_stats(AV1_PRIMARY *const ppi) {
4216*77c1e3ccSAndroid Build Coastguard Worker   if (!ppi->cpi) return;
4217*77c1e3ccSAndroid Build Coastguard Worker 
4218*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->cpi->oxcf.pass != 1 &&
4219*77c1e3ccSAndroid Build Coastguard Worker       ppi->cpi->common.current_frame.frame_number > 0) {
4220*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "Writing counts.stt\n");
4221*77c1e3ccSAndroid Build Coastguard Worker     FILE *f = fopen("counts.stt", "wb");
4222*77c1e3ccSAndroid Build Coastguard Worker     fwrite(&ppi->aggregate_fc, sizeof(ppi->aggregate_fc), 1, f);
4223*77c1e3ccSAndroid Build Coastguard Worker     fclose(f);
4224*77c1e3ccSAndroid Build Coastguard Worker   }
4225*77c1e3ccSAndroid Build Coastguard Worker }
4226*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_ENTROPY_STATS
4227*77c1e3ccSAndroid Build Coastguard Worker 
4228*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
adjust_image_stat(double y,double u,double v,double all,ImageStat * s)4229*77c1e3ccSAndroid Build Coastguard Worker static void adjust_image_stat(double y, double u, double v, double all,
4230*77c1e3ccSAndroid Build Coastguard Worker                               ImageStat *s) {
4231*77c1e3ccSAndroid Build Coastguard Worker   s->stat[STAT_Y] += y;
4232*77c1e3ccSAndroid Build Coastguard Worker   s->stat[STAT_U] += u;
4233*77c1e3ccSAndroid Build Coastguard Worker   s->stat[STAT_V] += v;
4234*77c1e3ccSAndroid Build Coastguard Worker   s->stat[STAT_ALL] += all;
4235*77c1e3ccSAndroid Build Coastguard Worker   s->worst = AOMMIN(s->worst, all);
4236*77c1e3ccSAndroid Build Coastguard Worker }
4237*77c1e3ccSAndroid Build Coastguard Worker 
compute_internal_stats(AV1_COMP * cpi,int frame_bytes)4238*77c1e3ccSAndroid Build Coastguard Worker static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
4239*77c1e3ccSAndroid Build Coastguard Worker   AV1_PRIMARY *const ppi = cpi->ppi;
4240*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
4241*77c1e3ccSAndroid Build Coastguard Worker   double samples = 0.0;
4242*77c1e3ccSAndroid Build Coastguard Worker   const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
4243*77c1e3ccSAndroid Build Coastguard Worker   const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
4244*77c1e3ccSAndroid Build Coastguard Worker 
4245*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->ppi->use_svc &&
4246*77c1e3ccSAndroid Build Coastguard Worker       cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1)
4247*77c1e3ccSAndroid Build Coastguard Worker     return;
4248*77c1e3ccSAndroid Build Coastguard Worker 
4249*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTER_STATS_ONLY
4250*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.frame_type == KEY_FRAME) return;  // skip key frame
4251*77c1e3ccSAndroid Build Coastguard Worker #endif
4252*77c1e3ccSAndroid Build Coastguard Worker   cpi->bytes += frame_bytes;
4253*77c1e3ccSAndroid Build Coastguard Worker   if (cm->show_frame) {
4254*77c1e3ccSAndroid Build Coastguard Worker     const YV12_BUFFER_CONFIG *orig = cpi->source;
4255*77c1e3ccSAndroid Build Coastguard Worker     const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
4256*77c1e3ccSAndroid Build Coastguard Worker     double y, u, v, frame_all;
4257*77c1e3ccSAndroid Build Coastguard Worker 
4258*77c1e3ccSAndroid Build Coastguard Worker     ppi->count[0]++;
4259*77c1e3ccSAndroid Build Coastguard Worker     ppi->count[1]++;
4260*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->b_calculate_psnr) {
4261*77c1e3ccSAndroid Build Coastguard Worker       PSNR_STATS psnr;
4262*77c1e3ccSAndroid Build Coastguard Worker       double weight[2] = { 0.0, 0.0 };
4263*77c1e3ccSAndroid Build Coastguard Worker       double frame_ssim2[2] = { 0.0, 0.0 };
4264*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
4265*77c1e3ccSAndroid Build Coastguard Worker       aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
4266*77c1e3ccSAndroid Build Coastguard Worker #else
4267*77c1e3ccSAndroid Build Coastguard Worker       aom_calc_psnr(orig, recon, &psnr);
4268*77c1e3ccSAndroid Build Coastguard Worker #endif
4269*77c1e3ccSAndroid Build Coastguard Worker       adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
4270*77c1e3ccSAndroid Build Coastguard Worker                         &(ppi->psnr[0]));
4271*77c1e3ccSAndroid Build Coastguard Worker       ppi->total_sq_error[0] += psnr.sse[0];
4272*77c1e3ccSAndroid Build Coastguard Worker       ppi->total_samples[0] += psnr.samples[0];
4273*77c1e3ccSAndroid Build Coastguard Worker       samples = psnr.samples[0];
4274*77c1e3ccSAndroid Build Coastguard Worker 
4275*77c1e3ccSAndroid Build Coastguard Worker       aom_calc_ssim(orig, recon, bit_depth, in_bit_depth,
4276*77c1e3ccSAndroid Build Coastguard Worker                     cm->seq_params->use_highbitdepth, weight, frame_ssim2);
4277*77c1e3ccSAndroid Build Coastguard Worker 
4278*77c1e3ccSAndroid Build Coastguard Worker       ppi->worst_ssim = AOMMIN(ppi->worst_ssim, frame_ssim2[0]);
4279*77c1e3ccSAndroid Build Coastguard Worker       ppi->summed_quality += frame_ssim2[0] * weight[0];
4280*77c1e3ccSAndroid Build Coastguard Worker       ppi->summed_weights += weight[0];
4281*77c1e3ccSAndroid Build Coastguard Worker 
4282*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
4283*77c1e3ccSAndroid Build Coastguard Worker       // Compute PSNR based on stream bit depth
4284*77c1e3ccSAndroid Build Coastguard Worker       if ((cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) &&
4285*77c1e3ccSAndroid Build Coastguard Worker           (in_bit_depth < bit_depth)) {
4286*77c1e3ccSAndroid Build Coastguard Worker         adjust_image_stat(psnr.psnr_hbd[1], psnr.psnr_hbd[2], psnr.psnr_hbd[3],
4287*77c1e3ccSAndroid Build Coastguard Worker                           psnr.psnr_hbd[0], &ppi->psnr[1]);
4288*77c1e3ccSAndroid Build Coastguard Worker         ppi->total_sq_error[1] += psnr.sse_hbd[0];
4289*77c1e3ccSAndroid Build Coastguard Worker         ppi->total_samples[1] += psnr.samples_hbd[0];
4290*77c1e3ccSAndroid Build Coastguard Worker 
4291*77c1e3ccSAndroid Build Coastguard Worker         ppi->worst_ssim_hbd = AOMMIN(ppi->worst_ssim_hbd, frame_ssim2[1]);
4292*77c1e3ccSAndroid Build Coastguard Worker         ppi->summed_quality_hbd += frame_ssim2[1] * weight[1];
4293*77c1e3ccSAndroid Build Coastguard Worker         ppi->summed_weights_hbd += weight[1];
4294*77c1e3ccSAndroid Build Coastguard Worker       }
4295*77c1e3ccSAndroid Build Coastguard Worker #endif
4296*77c1e3ccSAndroid Build Coastguard Worker 
4297*77c1e3ccSAndroid Build Coastguard Worker #if 0
4298*77c1e3ccSAndroid Build Coastguard Worker       {
4299*77c1e3ccSAndroid Build Coastguard Worker         FILE *f = fopen("q_used.stt", "a");
4300*77c1e3ccSAndroid Build Coastguard Worker         double y2 = psnr.psnr[1];
4301*77c1e3ccSAndroid Build Coastguard Worker         double u2 = psnr.psnr[2];
4302*77c1e3ccSAndroid Build Coastguard Worker         double v2 = psnr.psnr[3];
4303*77c1e3ccSAndroid Build Coastguard Worker         double frame_psnr2 = psnr.psnr[0];
4304*77c1e3ccSAndroid Build Coastguard Worker         fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
4305*77c1e3ccSAndroid Build Coastguard Worker                 cm->current_frame.frame_number, y2, u2, v2,
4306*77c1e3ccSAndroid Build Coastguard Worker                 frame_psnr2, frame_ssim2);
4307*77c1e3ccSAndroid Build Coastguard Worker         fclose(f);
4308*77c1e3ccSAndroid Build Coastguard Worker       }
4309*77c1e3ccSAndroid Build Coastguard Worker #endif
4310*77c1e3ccSAndroid Build Coastguard Worker     }
4311*77c1e3ccSAndroid Build Coastguard Worker     if (ppi->b_calculate_blockiness) {
4312*77c1e3ccSAndroid Build Coastguard Worker       if (!cm->seq_params->use_highbitdepth) {
4313*77c1e3ccSAndroid Build Coastguard Worker         const double frame_blockiness =
4314*77c1e3ccSAndroid Build Coastguard Worker             av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
4315*77c1e3ccSAndroid Build Coastguard Worker                                recon->y_stride, orig->y_width, orig->y_height);
4316*77c1e3ccSAndroid Build Coastguard Worker         ppi->worst_blockiness = AOMMAX(ppi->worst_blockiness, frame_blockiness);
4317*77c1e3ccSAndroid Build Coastguard Worker         ppi->total_blockiness += frame_blockiness;
4318*77c1e3ccSAndroid Build Coastguard Worker       }
4319*77c1e3ccSAndroid Build Coastguard Worker 
4320*77c1e3ccSAndroid Build Coastguard Worker       if (ppi->b_calculate_consistency) {
4321*77c1e3ccSAndroid Build Coastguard Worker         if (!cm->seq_params->use_highbitdepth) {
4322*77c1e3ccSAndroid Build Coastguard Worker           const double this_inconsistency = aom_get_ssim_metrics(
4323*77c1e3ccSAndroid Build Coastguard Worker               orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
4324*77c1e3ccSAndroid Build Coastguard Worker               orig->y_width, orig->y_height, ppi->ssim_vars, &ppi->metrics, 1);
4325*77c1e3ccSAndroid Build Coastguard Worker 
4326*77c1e3ccSAndroid Build Coastguard Worker           const double peak = (double)((1 << in_bit_depth) - 1);
4327*77c1e3ccSAndroid Build Coastguard Worker           const double consistency =
4328*77c1e3ccSAndroid Build Coastguard Worker               aom_sse_to_psnr(samples, peak, ppi->total_inconsistency);
4329*77c1e3ccSAndroid Build Coastguard Worker           if (consistency > 0.0)
4330*77c1e3ccSAndroid Build Coastguard Worker             ppi->worst_consistency =
4331*77c1e3ccSAndroid Build Coastguard Worker                 AOMMIN(ppi->worst_consistency, consistency);
4332*77c1e3ccSAndroid Build Coastguard Worker           ppi->total_inconsistency += this_inconsistency;
4333*77c1e3ccSAndroid Build Coastguard Worker         }
4334*77c1e3ccSAndroid Build Coastguard Worker       }
4335*77c1e3ccSAndroid Build Coastguard Worker     }
4336*77c1e3ccSAndroid Build Coastguard Worker 
4337*77c1e3ccSAndroid Build Coastguard Worker     frame_all =
4338*77c1e3ccSAndroid Build Coastguard Worker         aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
4339*77c1e3ccSAndroid Build Coastguard Worker     adjust_image_stat(y, u, v, frame_all, &ppi->fastssim);
4340*77c1e3ccSAndroid Build Coastguard Worker     frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
4341*77c1e3ccSAndroid Build Coastguard Worker     adjust_image_stat(y, u, v, frame_all, &ppi->psnrhvs);
4342*77c1e3ccSAndroid Build Coastguard Worker   }
4343*77c1e3ccSAndroid Build Coastguard Worker }
4344*77c1e3ccSAndroid Build Coastguard Worker 
print_internal_stats(AV1_PRIMARY * ppi)4345*77c1e3ccSAndroid Build Coastguard Worker void print_internal_stats(AV1_PRIMARY *ppi) {
4346*77c1e3ccSAndroid Build Coastguard Worker   if (!ppi->cpi) return;
4347*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *const cpi = ppi->cpi;
4348*77c1e3ccSAndroid Build Coastguard Worker 
4349*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->cpi->oxcf.pass != 1 &&
4350*77c1e3ccSAndroid Build Coastguard Worker       ppi->cpi->common.current_frame.frame_number > 0) {
4351*77c1e3ccSAndroid Build Coastguard Worker     char headings[512] = { 0 };
4352*77c1e3ccSAndroid Build Coastguard Worker     char results[512] = { 0 };
4353*77c1e3ccSAndroid Build Coastguard Worker     FILE *f = fopen("opsnr.stt", "a");
4354*77c1e3ccSAndroid Build Coastguard Worker     double time_encoded =
4355*77c1e3ccSAndroid Build Coastguard Worker         (cpi->time_stamps.prev_ts_end - cpi->time_stamps.first_ts_start) /
4356*77c1e3ccSAndroid Build Coastguard Worker         10000000.000;
4357*77c1e3ccSAndroid Build Coastguard Worker     double total_encode_time =
4358*77c1e3ccSAndroid Build Coastguard Worker         (ppi->total_time_receive_data + ppi->total_time_compress_data) /
4359*77c1e3ccSAndroid Build Coastguard Worker         1000.000;
4360*77c1e3ccSAndroid Build Coastguard Worker     const double dr =
4361*77c1e3ccSAndroid Build Coastguard Worker         (double)ppi->total_bytes * (double)8 / (double)1000 / time_encoded;
4362*77c1e3ccSAndroid Build Coastguard Worker     const double peak =
4363*77c1e3ccSAndroid Build Coastguard Worker         (double)((1 << ppi->cpi->oxcf.input_cfg.input_bit_depth) - 1);
4364*77c1e3ccSAndroid Build Coastguard Worker     const double target_rate =
4365*77c1e3ccSAndroid Build Coastguard Worker         (double)ppi->cpi->oxcf.rc_cfg.target_bandwidth / 1000;
4366*77c1e3ccSAndroid Build Coastguard Worker     const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
4367*77c1e3ccSAndroid Build Coastguard Worker 
4368*77c1e3ccSAndroid Build Coastguard Worker     if (ppi->b_calculate_psnr) {
4369*77c1e3ccSAndroid Build Coastguard Worker       const double total_psnr = aom_sse_to_psnr(
4370*77c1e3ccSAndroid Build Coastguard Worker           (double)ppi->total_samples[0], peak, (double)ppi->total_sq_error[0]);
4371*77c1e3ccSAndroid Build Coastguard Worker       const double total_ssim =
4372*77c1e3ccSAndroid Build Coastguard Worker           100 * pow(ppi->summed_quality / ppi->summed_weights, 8.0);
4373*77c1e3ccSAndroid Build Coastguard Worker       snprintf(headings, sizeof(headings),
4374*77c1e3ccSAndroid Build Coastguard Worker                "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
4375*77c1e3ccSAndroid Build Coastguard Worker                "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
4376*77c1e3ccSAndroid Build Coastguard Worker                "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
4377*77c1e3ccSAndroid Build Coastguard Worker                "AVPsrnY\tAPsnrCb\tAPsnrCr");
4378*77c1e3ccSAndroid Build Coastguard Worker       snprintf(results, sizeof(results),
4379*77c1e3ccSAndroid Build Coastguard Worker                "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
4380*77c1e3ccSAndroid Build Coastguard Worker                "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
4381*77c1e3ccSAndroid Build Coastguard Worker                "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
4382*77c1e3ccSAndroid Build Coastguard Worker                "%7.3f\t%7.3f\t%7.3f",
4383*77c1e3ccSAndroid Build Coastguard Worker                dr, ppi->psnr[0].stat[STAT_ALL] / ppi->count[0], total_psnr,
4384*77c1e3ccSAndroid Build Coastguard Worker                ppi->psnr[0].stat[STAT_ALL] / ppi->count[0], total_psnr,
4385*77c1e3ccSAndroid Build Coastguard Worker                total_ssim, total_ssim,
4386*77c1e3ccSAndroid Build Coastguard Worker                ppi->fastssim.stat[STAT_ALL] / ppi->count[0],
4387*77c1e3ccSAndroid Build Coastguard Worker                ppi->psnrhvs.stat[STAT_ALL] / ppi->count[0], ppi->psnr[0].worst,
4388*77c1e3ccSAndroid Build Coastguard Worker                ppi->worst_ssim, ppi->fastssim.worst, ppi->psnrhvs.worst,
4389*77c1e3ccSAndroid Build Coastguard Worker                ppi->psnr[0].stat[STAT_Y] / ppi->count[0],
4390*77c1e3ccSAndroid Build Coastguard Worker                ppi->psnr[0].stat[STAT_U] / ppi->count[0],
4391*77c1e3ccSAndroid Build Coastguard Worker                ppi->psnr[0].stat[STAT_V] / ppi->count[0]);
4392*77c1e3ccSAndroid Build Coastguard Worker 
4393*77c1e3ccSAndroid Build Coastguard Worker       if (ppi->b_calculate_blockiness) {
4394*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT(headings, "\t  Block\tWstBlck");
4395*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "\t%7.3f", ppi->total_blockiness / ppi->count[0]);
4396*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "\t%7.3f", ppi->worst_blockiness);
4397*77c1e3ccSAndroid Build Coastguard Worker       }
4398*77c1e3ccSAndroid Build Coastguard Worker 
4399*77c1e3ccSAndroid Build Coastguard Worker       if (ppi->b_calculate_consistency) {
4400*77c1e3ccSAndroid Build Coastguard Worker         double consistency =
4401*77c1e3ccSAndroid Build Coastguard Worker             aom_sse_to_psnr((double)ppi->total_samples[0], peak,
4402*77c1e3ccSAndroid Build Coastguard Worker                             (double)ppi->total_inconsistency);
4403*77c1e3ccSAndroid Build Coastguard Worker 
4404*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT(headings, "\tConsist\tWstCons");
4405*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "\t%7.3f", consistency);
4406*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "\t%7.3f", ppi->worst_consistency);
4407*77c1e3ccSAndroid Build Coastguard Worker       }
4408*77c1e3ccSAndroid Build Coastguard Worker 
4409*77c1e3ccSAndroid Build Coastguard Worker       SNPRINT(headings, "\t   Time\tRcErr\tAbsErr");
4410*77c1e3ccSAndroid Build Coastguard Worker       SNPRINT2(results, "\t%8.0f", total_encode_time);
4411*77c1e3ccSAndroid Build Coastguard Worker       SNPRINT2(results, " %7.2f", rate_err);
4412*77c1e3ccSAndroid Build Coastguard Worker       SNPRINT2(results, " %7.2f", fabs(rate_err));
4413*77c1e3ccSAndroid Build Coastguard Worker 
4414*77c1e3ccSAndroid Build Coastguard Worker       SNPRINT(headings, "\tAPsnr611");
4415*77c1e3ccSAndroid Build Coastguard Worker       SNPRINT2(results, " %7.3f",
4416*77c1e3ccSAndroid Build Coastguard Worker                (6 * ppi->psnr[0].stat[STAT_Y] + ppi->psnr[0].stat[STAT_U] +
4417*77c1e3ccSAndroid Build Coastguard Worker                 ppi->psnr[0].stat[STAT_V]) /
4418*77c1e3ccSAndroid Build Coastguard Worker                    (ppi->count[0] * 8));
4419*77c1e3ccSAndroid Build Coastguard Worker 
4420*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
4421*77c1e3ccSAndroid Build Coastguard Worker       const uint32_t in_bit_depth = ppi->cpi->oxcf.input_cfg.input_bit_depth;
4422*77c1e3ccSAndroid Build Coastguard Worker       const uint32_t bit_depth = ppi->seq_params.bit_depth;
4423*77c1e3ccSAndroid Build Coastguard Worker       // Since cpi->source->flags is not available here, but total_samples[1]
4424*77c1e3ccSAndroid Build Coastguard Worker       // will be non-zero if cpi->source->flags & YV12_FLAG_HIGHBITDEPTH was
4425*77c1e3ccSAndroid Build Coastguard Worker       // true in compute_internal_stats
4426*77c1e3ccSAndroid Build Coastguard Worker       if ((ppi->total_samples[1] > 0) && (in_bit_depth < bit_depth)) {
4427*77c1e3ccSAndroid Build Coastguard Worker         const double peak_hbd = (double)((1 << bit_depth) - 1);
4428*77c1e3ccSAndroid Build Coastguard Worker         const double total_psnr_hbd =
4429*77c1e3ccSAndroid Build Coastguard Worker             aom_sse_to_psnr((double)ppi->total_samples[1], peak_hbd,
4430*77c1e3ccSAndroid Build Coastguard Worker                             (double)ppi->total_sq_error[1]);
4431*77c1e3ccSAndroid Build Coastguard Worker         const double total_ssim_hbd =
4432*77c1e3ccSAndroid Build Coastguard Worker             100 * pow(ppi->summed_quality_hbd / ppi->summed_weights_hbd, 8.0);
4433*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT(headings,
4434*77c1e3ccSAndroid Build Coastguard Worker                 "\t AVGPsnrH GLBPsnrH AVPsnrPH GLPsnrPH"
4435*77c1e3ccSAndroid Build Coastguard Worker                 " AVPsnrYH APsnrCbH APsnrCrH WstPsnrH"
4436*77c1e3ccSAndroid Build Coastguard Worker                 " AOMSSIMH VPSSIMPH WstSsimH");
4437*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "\t%7.3f",
4438*77c1e3ccSAndroid Build Coastguard Worker                  ppi->psnr[1].stat[STAT_ALL] / ppi->count[1]);
4439*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f", total_psnr_hbd);
4440*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f",
4441*77c1e3ccSAndroid Build Coastguard Worker                  ppi->psnr[1].stat[STAT_ALL] / ppi->count[1]);
4442*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f", total_psnr_hbd);
4443*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f", ppi->psnr[1].stat[STAT_Y] / ppi->count[1]);
4444*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f", ppi->psnr[1].stat[STAT_U] / ppi->count[1]);
4445*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f", ppi->psnr[1].stat[STAT_V] / ppi->count[1]);
4446*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f", ppi->psnr[1].worst);
4447*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f", total_ssim_hbd);
4448*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f", total_ssim_hbd);
4449*77c1e3ccSAndroid Build Coastguard Worker         SNPRINT2(results, "  %7.3f", ppi->worst_ssim_hbd);
4450*77c1e3ccSAndroid Build Coastguard Worker       }
4451*77c1e3ccSAndroid Build Coastguard Worker #endif
4452*77c1e3ccSAndroid Build Coastguard Worker       fprintf(f, "%s\n", headings);
4453*77c1e3ccSAndroid Build Coastguard Worker       fprintf(f, "%s\n", results);
4454*77c1e3ccSAndroid Build Coastguard Worker     }
4455*77c1e3ccSAndroid Build Coastguard Worker 
4456*77c1e3ccSAndroid Build Coastguard Worker     fclose(f);
4457*77c1e3ccSAndroid Build Coastguard Worker 
4458*77c1e3ccSAndroid Build Coastguard Worker     aom_free(ppi->ssim_vars);
4459*77c1e3ccSAndroid Build Coastguard Worker     ppi->ssim_vars = NULL;
4460*77c1e3ccSAndroid Build Coastguard Worker   }
4461*77c1e3ccSAndroid Build Coastguard Worker }
4462*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_INTERNAL_STATS
4463*77c1e3ccSAndroid Build Coastguard Worker 
update_keyframe_counters(AV1_COMP * cpi)4464*77c1e3ccSAndroid Build Coastguard Worker static inline void update_keyframe_counters(AV1_COMP *cpi) {
4465*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->common.show_frame && cpi->rc.frames_to_key) {
4466*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
4467*77c1e3ccSAndroid Build Coastguard Worker     FIRSTPASS_INFO *firstpass_info = &cpi->ppi->twopass.firstpass_info;
4468*77c1e3ccSAndroid Build Coastguard Worker     if (firstpass_info->past_stats_count > FIRSTPASS_INFO_STATS_PAST_MIN) {
4469*77c1e3ccSAndroid Build Coastguard Worker       av1_firstpass_info_move_cur_index_and_pop(firstpass_info);
4470*77c1e3ccSAndroid Build Coastguard Worker     } else {
4471*77c1e3ccSAndroid Build Coastguard Worker       // When there is not enough past stats, we move the current
4472*77c1e3ccSAndroid Build Coastguard Worker       // index without popping the past stats
4473*77c1e3ccSAndroid Build Coastguard Worker       av1_firstpass_info_move_cur_index(firstpass_info);
4474*77c1e3ccSAndroid Build Coastguard Worker     }
4475*77c1e3ccSAndroid Build Coastguard Worker #endif
4476*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
4477*77c1e3ccSAndroid Build Coastguard Worker       cpi->rc.frames_since_key++;
4478*77c1e3ccSAndroid Build Coastguard Worker       cpi->rc.frames_to_key--;
4479*77c1e3ccSAndroid Build Coastguard Worker       cpi->rc.frames_to_fwd_kf--;
4480*77c1e3ccSAndroid Build Coastguard Worker       cpi->rc.frames_since_scene_change++;
4481*77c1e3ccSAndroid Build Coastguard Worker     }
4482*77c1e3ccSAndroid Build Coastguard Worker   }
4483*77c1e3ccSAndroid Build Coastguard Worker }
4484*77c1e3ccSAndroid Build Coastguard Worker 
update_frames_till_gf_update(AV1_COMP * cpi)4485*77c1e3ccSAndroid Build Coastguard Worker static inline void update_frames_till_gf_update(AV1_COMP *cpi) {
4486*77c1e3ccSAndroid Build Coastguard Worker   // TODO(weitinglin): Updating this counter for is_frame_droppable
4487*77c1e3ccSAndroid Build Coastguard Worker   // is a work-around to handle the condition when a frame is drop.
4488*77c1e3ccSAndroid Build Coastguard Worker   // We should fix the cpi->common.show_frame flag
4489*77c1e3ccSAndroid Build Coastguard Worker   // instead of checking the other condition to update the counter properly.
4490*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->common.show_frame ||
4491*77c1e3ccSAndroid Build Coastguard Worker       is_frame_droppable(&cpi->ppi->rtc_ref, &cpi->ext_flags.refresh_frame)) {
4492*77c1e3ccSAndroid Build Coastguard Worker     // Decrement count down till next gf
4493*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->rc.frames_till_gf_update_due > 0)
4494*77c1e3ccSAndroid Build Coastguard Worker       cpi->rc.frames_till_gf_update_due--;
4495*77c1e3ccSAndroid Build Coastguard Worker   }
4496*77c1e3ccSAndroid Build Coastguard Worker }
4497*77c1e3ccSAndroid Build Coastguard Worker 
update_gf_group_index(AV1_COMP * cpi)4498*77c1e3ccSAndroid Build Coastguard Worker static inline void update_gf_group_index(AV1_COMP *cpi) {
4499*77c1e3ccSAndroid Build Coastguard Worker   // Increment the gf group index ready for the next frame.
4500*77c1e3ccSAndroid Build Coastguard Worker   if (is_one_pass_rt_params(cpi) &&
4501*77c1e3ccSAndroid Build Coastguard Worker       cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
4502*77c1e3ccSAndroid Build Coastguard Worker     ++cpi->gf_frame_index;
4503*77c1e3ccSAndroid Build Coastguard Worker     // Reset gf_frame_index in case it reaches MAX_STATIC_GF_GROUP_LENGTH
4504*77c1e3ccSAndroid Build Coastguard Worker     // for real time encoding.
4505*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->gf_frame_index == MAX_STATIC_GF_GROUP_LENGTH)
4506*77c1e3ccSAndroid Build Coastguard Worker       cpi->gf_frame_index = 0;
4507*77c1e3ccSAndroid Build Coastguard Worker   } else {
4508*77c1e3ccSAndroid Build Coastguard Worker     ++cpi->gf_frame_index;
4509*77c1e3ccSAndroid Build Coastguard Worker   }
4510*77c1e3ccSAndroid Build Coastguard Worker }
4511*77c1e3ccSAndroid Build Coastguard Worker 
update_fb_of_context_type(const AV1_COMP * const cpi,int * const fb_of_context_type)4512*77c1e3ccSAndroid Build Coastguard Worker static void update_fb_of_context_type(const AV1_COMP *const cpi,
4513*77c1e3ccSAndroid Build Coastguard Worker                                       int *const fb_of_context_type) {
4514*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
4515*77c1e3ccSAndroid Build Coastguard Worker   const int current_frame_ref_type = get_current_frame_ref_type(cpi);
4516*77c1e3ccSAndroid Build Coastguard Worker 
4517*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
4518*77c1e3ccSAndroid Build Coastguard Worker       cpi->ext_flags.use_primary_ref_none) {
4519*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < REF_FRAMES; i++) {
4520*77c1e3ccSAndroid Build Coastguard Worker       fb_of_context_type[i] = -1;
4521*77c1e3ccSAndroid Build Coastguard Worker     }
4522*77c1e3ccSAndroid Build Coastguard Worker     fb_of_context_type[current_frame_ref_type] =
4523*77c1e3ccSAndroid Build Coastguard Worker         cm->show_frame ? get_ref_frame_map_idx(cm, GOLDEN_FRAME)
4524*77c1e3ccSAndroid Build Coastguard Worker                        : get_ref_frame_map_idx(cm, ALTREF_FRAME);
4525*77c1e3ccSAndroid Build Coastguard Worker   }
4526*77c1e3ccSAndroid Build Coastguard Worker 
4527*77c1e3ccSAndroid Build Coastguard Worker   if (!encode_show_existing_frame(cm)) {
4528*77c1e3ccSAndroid Build Coastguard Worker     // Refresh fb_of_context_type[]: see encoder.h for explanation
4529*77c1e3ccSAndroid Build Coastguard Worker     if (cm->current_frame.frame_type == KEY_FRAME) {
4530*77c1e3ccSAndroid Build Coastguard Worker       // All ref frames are refreshed, pick one that will live long enough
4531*77c1e3ccSAndroid Build Coastguard Worker       fb_of_context_type[current_frame_ref_type] = 0;
4532*77c1e3ccSAndroid Build Coastguard Worker     } else {
4533*77c1e3ccSAndroid Build Coastguard Worker       // If more than one frame is refreshed, it doesn't matter which one we
4534*77c1e3ccSAndroid Build Coastguard Worker       // pick so pick the first.  LST sometimes doesn't refresh any: this is ok
4535*77c1e3ccSAndroid Build Coastguard Worker 
4536*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < REF_FRAMES; i++) {
4537*77c1e3ccSAndroid Build Coastguard Worker         if (cm->current_frame.refresh_frame_flags & (1 << i)) {
4538*77c1e3ccSAndroid Build Coastguard Worker           fb_of_context_type[current_frame_ref_type] = i;
4539*77c1e3ccSAndroid Build Coastguard Worker           break;
4540*77c1e3ccSAndroid Build Coastguard Worker         }
4541*77c1e3ccSAndroid Build Coastguard Worker       }
4542*77c1e3ccSAndroid Build Coastguard Worker     }
4543*77c1e3ccSAndroid Build Coastguard Worker   }
4544*77c1e3ccSAndroid Build Coastguard Worker }
4545*77c1e3ccSAndroid Build Coastguard Worker 
update_rc_counts(AV1_COMP * cpi)4546*77c1e3ccSAndroid Build Coastguard Worker static void update_rc_counts(AV1_COMP *cpi) {
4547*77c1e3ccSAndroid Build Coastguard Worker   update_keyframe_counters(cpi);
4548*77c1e3ccSAndroid Build Coastguard Worker   update_frames_till_gf_update(cpi);
4549*77c1e3ccSAndroid Build Coastguard Worker   update_gf_group_index(cpi);
4550*77c1e3ccSAndroid Build Coastguard Worker }
4551*77c1e3ccSAndroid Build Coastguard Worker 
update_end_of_frame_stats(AV1_COMP * cpi)4552*77c1e3ccSAndroid Build Coastguard Worker static void update_end_of_frame_stats(AV1_COMP *cpi) {
4553*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->do_frame_data_update) {
4554*77c1e3ccSAndroid Build Coastguard Worker     // Store current frame loopfilter levels in ppi, if update flag is set.
4555*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->common.show_existing_frame) {
4556*77c1e3ccSAndroid Build Coastguard Worker       AV1_COMMON *const cm = &cpi->common;
4557*77c1e3ccSAndroid Build Coastguard Worker       struct loopfilter *const lf = &cm->lf;
4558*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->filter_level[0] = lf->filter_level[0];
4559*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->filter_level[1] = lf->filter_level[1];
4560*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->filter_level_u = lf->filter_level_u;
4561*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->filter_level_v = lf->filter_level_v;
4562*77c1e3ccSAndroid Build Coastguard Worker     }
4563*77c1e3ccSAndroid Build Coastguard Worker   }
4564*77c1e3ccSAndroid Build Coastguard Worker   // Store frame level mv_stats from cpi to ppi.
4565*77c1e3ccSAndroid Build Coastguard Worker   cpi->ppi->mv_stats = cpi->mv_stats;
4566*77c1e3ccSAndroid Build Coastguard Worker }
4567*77c1e3ccSAndroid Build Coastguard Worker 
4568*77c1e3ccSAndroid Build Coastguard Worker // Updates frame level stats related to global motion
update_gm_stats(AV1_COMP * cpi)4569*77c1e3ccSAndroid Build Coastguard Worker static inline void update_gm_stats(AV1_COMP *cpi) {
4570*77c1e3ccSAndroid Build Coastguard Worker   FRAME_UPDATE_TYPE update_type =
4571*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->gf_group.update_type[cpi->gf_frame_index];
4572*77c1e3ccSAndroid Build Coastguard Worker   int i, is_gm_present = 0;
4573*77c1e3ccSAndroid Build Coastguard Worker 
4574*77c1e3ccSAndroid Build Coastguard Worker   // Check if the current frame has any valid global motion model across its
4575*77c1e3ccSAndroid Build Coastguard Worker   // reference frames
4576*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < REF_FRAMES; i++) {
4577*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->common.global_motion[i].wmtype != IDENTITY) {
4578*77c1e3ccSAndroid Build Coastguard Worker       is_gm_present = 1;
4579*77c1e3ccSAndroid Build Coastguard Worker       break;
4580*77c1e3ccSAndroid Build Coastguard Worker     }
4581*77c1e3ccSAndroid Build Coastguard Worker   }
4582*77c1e3ccSAndroid Build Coastguard Worker   int update_actual_stats = 1;
4583*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_FPMT_TEST
4584*77c1e3ccSAndroid Build Coastguard Worker   update_actual_stats =
4585*77c1e3ccSAndroid Build Coastguard Worker       (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 0 : 1;
4586*77c1e3ccSAndroid Build Coastguard Worker   if (!update_actual_stats) {
4587*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->temp_valid_gm_model_found[update_type] == INT32_MAX) {
4588*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->temp_valid_gm_model_found[update_type] = is_gm_present;
4589*77c1e3ccSAndroid Build Coastguard Worker     } else {
4590*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->temp_valid_gm_model_found[update_type] |= is_gm_present;
4591*77c1e3ccSAndroid Build Coastguard Worker     }
4592*77c1e3ccSAndroid Build Coastguard Worker     int show_existing_between_parallel_frames =
4593*77c1e3ccSAndroid Build Coastguard Worker         (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] ==
4594*77c1e3ccSAndroid Build Coastguard Worker              INTNL_OVERLAY_UPDATE &&
4595*77c1e3ccSAndroid Build Coastguard Worker          cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2);
4596*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->do_frame_data_update == 1 &&
4597*77c1e3ccSAndroid Build Coastguard Worker         !show_existing_between_parallel_frames) {
4598*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < FRAME_UPDATE_TYPES; i++) {
4599*77c1e3ccSAndroid Build Coastguard Worker         cpi->ppi->valid_gm_model_found[i] =
4600*77c1e3ccSAndroid Build Coastguard Worker             cpi->ppi->temp_valid_gm_model_found[i];
4601*77c1e3ccSAndroid Build Coastguard Worker       }
4602*77c1e3ccSAndroid Build Coastguard Worker     }
4603*77c1e3ccSAndroid Build Coastguard Worker   }
4604*77c1e3ccSAndroid Build Coastguard Worker #endif
4605*77c1e3ccSAndroid Build Coastguard Worker   if (update_actual_stats) {
4606*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->valid_gm_model_found[update_type] == INT32_MAX) {
4607*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->valid_gm_model_found[update_type] = is_gm_present;
4608*77c1e3ccSAndroid Build Coastguard Worker     } else {
4609*77c1e3ccSAndroid Build Coastguard Worker       cpi->ppi->valid_gm_model_found[update_type] |= is_gm_present;
4610*77c1e3ccSAndroid Build Coastguard Worker     }
4611*77c1e3ccSAndroid Build Coastguard Worker   }
4612*77c1e3ccSAndroid Build Coastguard Worker }
4613*77c1e3ccSAndroid Build Coastguard Worker 
av1_post_encode_updates(AV1_COMP * const cpi,const AV1_COMP_DATA * const cpi_data)4614*77c1e3ccSAndroid Build Coastguard Worker void av1_post_encode_updates(AV1_COMP *const cpi,
4615*77c1e3ccSAndroid Build Coastguard Worker                              const AV1_COMP_DATA *const cpi_data) {
4616*77c1e3ccSAndroid Build Coastguard Worker   AV1_PRIMARY *const ppi = cpi->ppi;
4617*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
4618*77c1e3ccSAndroid Build Coastguard Worker 
4619*77c1e3ccSAndroid Build Coastguard Worker   update_gm_stats(cpi);
4620*77c1e3ccSAndroid Build Coastguard Worker 
4621*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
4622*77c1e3ccSAndroid Build Coastguard Worker   // Update the total stats remaining structure.
4623*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->twopass_frame.this_frame != NULL &&
4624*77c1e3ccSAndroid Build Coastguard Worker       ppi->twopass.stats_buf_ctx->total_left_stats) {
4625*77c1e3ccSAndroid Build Coastguard Worker     subtract_stats(ppi->twopass.stats_buf_ctx->total_left_stats,
4626*77c1e3ccSAndroid Build Coastguard Worker                    cpi->twopass_frame.this_frame);
4627*77c1e3ccSAndroid Build Coastguard Worker   }
4628*77c1e3ccSAndroid Build Coastguard Worker #endif
4629*77c1e3ccSAndroid Build Coastguard Worker 
4630*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_OUTPUT_FRAME_SIZE
4631*77c1e3ccSAndroid Build Coastguard Worker   FILE *f = fopen("frame_sizes.csv", "a");
4632*77c1e3ccSAndroid Build Coastguard Worker   fprintf(f, "%d,", 8 * (int)cpi_data->frame_size);
4633*77c1e3ccSAndroid Build Coastguard Worker   fprintf(f, "%d\n", cm->quant_params.base_qindex);
4634*77c1e3ccSAndroid Build Coastguard Worker   fclose(f);
4635*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_OUTPUT_FRAME_SIZE
4636*77c1e3ccSAndroid Build Coastguard Worker 
4637*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi) && !cpi->is_dropped_frame) {
4638*77c1e3ccSAndroid Build Coastguard Worker     // Before calling refresh_reference_frames(), copy ppi->ref_frame_map_copy
4639*77c1e3ccSAndroid Build Coastguard Worker     // to cm->ref_frame_map for frame_parallel_level 2 frame in a parallel
4640*77c1e3ccSAndroid Build Coastguard Worker     // encode set of lower layer frames.
4641*77c1e3ccSAndroid Build Coastguard Worker     // TODO(Remya): Move ref_frame_map from AV1_COMMON to AV1_PRIMARY to avoid
4642*77c1e3ccSAndroid Build Coastguard Worker     // copy.
4643*77c1e3ccSAndroid Build Coastguard Worker     if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 2 &&
4644*77c1e3ccSAndroid Build Coastguard Worker         ppi->gf_group.frame_parallel_level[cpi->gf_frame_index - 1] == 1 &&
4645*77c1e3ccSAndroid Build Coastguard Worker         ppi->gf_group.update_type[cpi->gf_frame_index - 1] ==
4646*77c1e3ccSAndroid Build Coastguard Worker             INTNL_ARF_UPDATE) {
4647*77c1e3ccSAndroid Build Coastguard Worker       memcpy(cm->ref_frame_map, ppi->ref_frame_map_copy,
4648*77c1e3ccSAndroid Build Coastguard Worker              sizeof(cm->ref_frame_map));
4649*77c1e3ccSAndroid Build Coastguard Worker     }
4650*77c1e3ccSAndroid Build Coastguard Worker     refresh_reference_frames(cpi);
4651*77c1e3ccSAndroid Build Coastguard Worker     // For frame_parallel_level 1 frame in a parallel encode set of lower layer
4652*77c1e3ccSAndroid Build Coastguard Worker     // frames, store the updated cm->ref_frame_map in ppi->ref_frame_map_copy.
4653*77c1e3ccSAndroid Build Coastguard Worker     if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 1 &&
4654*77c1e3ccSAndroid Build Coastguard Worker         ppi->gf_group.update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
4655*77c1e3ccSAndroid Build Coastguard Worker       memcpy(ppi->ref_frame_map_copy, cm->ref_frame_map,
4656*77c1e3ccSAndroid Build Coastguard Worker              sizeof(cm->ref_frame_map));
4657*77c1e3ccSAndroid Build Coastguard Worker     }
4658*77c1e3ccSAndroid Build Coastguard Worker     av1_rc_postencode_update(cpi, cpi_data->frame_size);
4659*77c1e3ccSAndroid Build Coastguard Worker   }
4660*77c1e3ccSAndroid Build Coastguard Worker 
4661*77c1e3ccSAndroid Build Coastguard Worker   if (cpi_data->pop_lookahead == 1) {
4662*77c1e3ccSAndroid Build Coastguard Worker     av1_lookahead_pop(cpi->ppi->lookahead, cpi_data->flush,
4663*77c1e3ccSAndroid Build Coastguard Worker                       cpi->compressor_stage);
4664*77c1e3ccSAndroid Build Coastguard Worker   }
4665*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->common.show_frame) {
4666*77c1e3ccSAndroid Build Coastguard Worker     cpi->ppi->ts_start_last_show_frame = cpi_data->ts_frame_start;
4667*77c1e3ccSAndroid Build Coastguard Worker     cpi->ppi->ts_end_last_show_frame = cpi_data->ts_frame_end;
4668*77c1e3ccSAndroid Build Coastguard Worker   }
4669*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) {
4670*77c1e3ccSAndroid Build Coastguard Worker     // Initialize level info. at the beginning of each sequence.
4671*77c1e3ccSAndroid Build Coastguard Worker     if (cm->current_frame.frame_type == KEY_FRAME &&
4672*77c1e3ccSAndroid Build Coastguard Worker         ppi->gf_group.refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) {
4673*77c1e3ccSAndroid Build Coastguard Worker       av1_init_level_info(cpi);
4674*77c1e3ccSAndroid Build Coastguard Worker     }
4675*77c1e3ccSAndroid Build Coastguard Worker     av1_update_level_info(cpi, cpi_data->frame_size, cpi_data->ts_frame_start,
4676*77c1e3ccSAndroid Build Coastguard Worker                           cpi_data->ts_frame_end);
4677*77c1e3ccSAndroid Build Coastguard Worker   }
4678*77c1e3ccSAndroid Build Coastguard Worker 
4679*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi)) {
4680*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
4681*77c1e3ccSAndroid Build Coastguard Worker     if (!has_no_stats_stage(cpi)) av1_twopass_postencode_update(cpi);
4682*77c1e3ccSAndroid Build Coastguard Worker #endif
4683*77c1e3ccSAndroid Build Coastguard Worker     update_fb_of_context_type(cpi, ppi->fb_of_context_type);
4684*77c1e3ccSAndroid Build Coastguard Worker     update_rc_counts(cpi);
4685*77c1e3ccSAndroid Build Coastguard Worker     update_end_of_frame_stats(cpi);
4686*77c1e3ccSAndroid Build Coastguard Worker   }
4687*77c1e3ccSAndroid Build Coastguard Worker 
4688*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS
4689*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.pass == AOM_RC_THIRD_PASS && cpi->third_pass_ctx) {
4690*77c1e3ccSAndroid Build Coastguard Worker     av1_pop_third_pass_info(cpi->third_pass_ctx);
4691*77c1e3ccSAndroid Build Coastguard Worker   }
4692*77c1e3ccSAndroid Build Coastguard Worker #endif
4693*77c1e3ccSAndroid Build Coastguard Worker 
4694*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->rtc_ref.set_ref_frame_config && !cpi->is_dropped_frame) {
4695*77c1e3ccSAndroid Build Coastguard Worker     av1_svc_update_buffer_slot_refreshed(cpi);
4696*77c1e3ccSAndroid Build Coastguard Worker     av1_svc_set_reference_was_previous(cpi);
4697*77c1e3ccSAndroid Build Coastguard Worker   }
4698*77c1e3ccSAndroid Build Coastguard Worker 
4699*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->use_svc) av1_save_layer_context(cpi);
4700*77c1e3ccSAndroid Build Coastguard Worker 
4701*77c1e3ccSAndroid Build Coastguard Worker   // Note *size = 0 indicates a dropped frame for which psnr is not calculated
4702*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->b_calculate_psnr && cpi_data->frame_size > 0) {
4703*77c1e3ccSAndroid Build Coastguard Worker     if (cm->show_existing_frame ||
4704*77c1e3ccSAndroid Build Coastguard Worker         (!is_stat_generation_stage(cpi) && cm->show_frame)) {
4705*77c1e3ccSAndroid Build Coastguard Worker       generate_psnr_packet(cpi);
4706*77c1e3ccSAndroid Build Coastguard Worker     }
4707*77c1e3ccSAndroid Build Coastguard Worker   }
4708*77c1e3ccSAndroid Build Coastguard Worker 
4709*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
4710*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi)) {
4711*77c1e3ccSAndroid Build Coastguard Worker     compute_internal_stats(cpi, (int)cpi_data->frame_size);
4712*77c1e3ccSAndroid Build Coastguard Worker   }
4713*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_INTERNAL_STATS
4714*77c1e3ccSAndroid Build Coastguard Worker 
4715*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS
4716*77c1e3ccSAndroid Build Coastguard Worker   // Write frame info. Subtract 1 from frame index since if was incremented in
4717*77c1e3ccSAndroid Build Coastguard Worker   // update_rc_counts.
4718*77c1e3ccSAndroid Build Coastguard Worker   av1_write_second_pass_per_frame_info(cpi, cpi->gf_frame_index - 1);
4719*77c1e3ccSAndroid Build Coastguard Worker #endif
4720*77c1e3ccSAndroid Build Coastguard Worker }
4721*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_compressed_data(AV1_COMP * cpi,AV1_COMP_DATA * const cpi_data)4722*77c1e3ccSAndroid Build Coastguard Worker int av1_get_compressed_data(AV1_COMP *cpi, AV1_COMP_DATA *const cpi_data) {
4723*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4724*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
4725*77c1e3ccSAndroid Build Coastguard Worker 
4726*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
4727*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
4728*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
4729*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(cm->error->jmp)) {
4730*77c1e3ccSAndroid Build Coastguard Worker     cm->error->setjmp = 0;
4731*77c1e3ccSAndroid Build Coastguard Worker     return cm->error->error_code;
4732*77c1e3ccSAndroid Build Coastguard Worker   }
4733*77c1e3ccSAndroid Build Coastguard Worker   cm->error->setjmp = 1;
4734*77c1e3ccSAndroid Build Coastguard Worker 
4735*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
4736*77c1e3ccSAndroid Build Coastguard Worker   cpi->frame_recode_hits = 0;
4737*77c1e3ccSAndroid Build Coastguard Worker   cpi->time_compress_data = 0;
4738*77c1e3ccSAndroid Build Coastguard Worker   cpi->bytes = 0;
4739*77c1e3ccSAndroid Build Coastguard Worker #endif
4740*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
4741*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->compressor_stage == ENCODE_STAGE) {
4742*77c1e3ccSAndroid Build Coastguard Worker     av1_zero(cpi->counts);
4743*77c1e3ccSAndroid Build Coastguard Worker   }
4744*77c1e3ccSAndroid Build Coastguard Worker #endif
4745*77c1e3ccSAndroid Build Coastguard Worker 
4746*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
4747*77c1e3ccSAndroid Build Coastguard Worker   assert(cpi->oxcf.max_threads <= 1 &&
4748*77c1e3ccSAndroid Build Coastguard Worker          "bitstream debug tool does not support multithreading");
4749*77c1e3ccSAndroid Build Coastguard Worker   bitstream_queue_record_write();
4750*77c1e3ccSAndroid Build Coastguard Worker 
4751*77c1e3ccSAndroid Build Coastguard Worker   if (cm->seq_params->order_hint_info.enable_order_hint) {
4752*77c1e3ccSAndroid Build Coastguard Worker     aom_bitstream_queue_set_frame_write(cm->current_frame.order_hint * 2 +
4753*77c1e3ccSAndroid Build Coastguard Worker                                         cm->show_frame);
4754*77c1e3ccSAndroid Build Coastguard Worker   } else {
4755*77c1e3ccSAndroid Build Coastguard Worker     // This is currently used in RTC encoding. cm->show_frame is always 1.
4756*77c1e3ccSAndroid Build Coastguard Worker     aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number);
4757*77c1e3ccSAndroid Build Coastguard Worker   }
4758*77c1e3ccSAndroid Build Coastguard Worker #endif
4759*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->ppi->use_svc) {
4760*77c1e3ccSAndroid Build Coastguard Worker     av1_one_pass_cbr_svc_start_layer(cpi);
4761*77c1e3ccSAndroid Build Coastguard Worker   }
4762*77c1e3ccSAndroid Build Coastguard Worker 
4763*77c1e3ccSAndroid Build Coastguard Worker   cpi->is_dropped_frame = false;
4764*77c1e3ccSAndroid Build Coastguard Worker   cm->showable_frame = 0;
4765*77c1e3ccSAndroid Build Coastguard Worker   cpi_data->frame_size = 0;
4766*77c1e3ccSAndroid Build Coastguard Worker   cpi->available_bs_size = cpi_data->cx_data_sz;
4767*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
4768*77c1e3ccSAndroid Build Coastguard Worker   struct aom_usec_timer cmptimer;
4769*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_start(&cmptimer);
4770*77c1e3ccSAndroid Build Coastguard Worker #endif
4771*77c1e3ccSAndroid Build Coastguard Worker   av1_set_high_precision_mv(cpi, 1, 0);
4772*77c1e3ccSAndroid Build Coastguard Worker 
4773*77c1e3ccSAndroid Build Coastguard Worker   // Normal defaults
4774*77c1e3ccSAndroid Build Coastguard Worker   cm->features.refresh_frame_context =
4775*77c1e3ccSAndroid Build Coastguard Worker       oxcf->tool_cfg.frame_parallel_decoding_mode
4776*77c1e3ccSAndroid Build Coastguard Worker           ? REFRESH_FRAME_CONTEXT_DISABLED
4777*77c1e3ccSAndroid Build Coastguard Worker           : REFRESH_FRAME_CONTEXT_BACKWARD;
4778*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->tile_cfg.enable_large_scale_tile)
4779*77c1e3ccSAndroid Build Coastguard Worker     cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
4780*77c1e3ccSAndroid Build Coastguard Worker 
4781*77c1e3ccSAndroid Build Coastguard Worker   if (assign_cur_frame_new_fb(cm) == NULL) {
4782*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
4783*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to allocate new cur_frame");
4784*77c1e3ccSAndroid Build Coastguard Worker   }
4785*77c1e3ccSAndroid Build Coastguard Worker 
4786*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
4787*77c1e3ccSAndroid Build Coastguard Worker   // Accumulate 2nd pass time in 2-pass case or 1 pass time in 1-pass case.
4788*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0)
4789*77c1e3ccSAndroid Build Coastguard Worker     start_timing(cpi, av1_encode_strategy_time);
4790*77c1e3ccSAndroid Build Coastguard Worker #endif
4791*77c1e3ccSAndroid Build Coastguard Worker 
4792*77c1e3ccSAndroid Build Coastguard Worker   const int result = av1_encode_strategy(
4793*77c1e3ccSAndroid Build Coastguard Worker       cpi, &cpi_data->frame_size, cpi_data->cx_data, cpi_data->cx_data_sz,
4794*77c1e3ccSAndroid Build Coastguard Worker       &cpi_data->lib_flags, &cpi_data->ts_frame_start, &cpi_data->ts_frame_end,
4795*77c1e3ccSAndroid Build Coastguard Worker       cpi_data->timestamp_ratio, &cpi_data->pop_lookahead, cpi_data->flush);
4796*77c1e3ccSAndroid Build Coastguard Worker 
4797*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_COLLECT_COMPONENT_TIMING
4798*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0)
4799*77c1e3ccSAndroid Build Coastguard Worker     end_timing(cpi, av1_encode_strategy_time);
4800*77c1e3ccSAndroid Build Coastguard Worker 
4801*77c1e3ccSAndroid Build Coastguard Worker   // Print out timing information.
4802*77c1e3ccSAndroid Build Coastguard Worker   // Note: Use "cpi->frame_component_time[0] > 100 us" to avoid showing of
4803*77c1e3ccSAndroid Build Coastguard Worker   // show_existing_frame and lag-in-frames.
4804*77c1e3ccSAndroid Build Coastguard Worker   if ((cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0) &&
4805*77c1e3ccSAndroid Build Coastguard Worker       cpi->frame_component_time[0] > 100) {
4806*77c1e3ccSAndroid Build Coastguard Worker     int i;
4807*77c1e3ccSAndroid Build Coastguard Worker     uint64_t frame_total = 0, total = 0;
4808*77c1e3ccSAndroid Build Coastguard Worker     const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
4809*77c1e3ccSAndroid Build Coastguard Worker     FRAME_UPDATE_TYPE frame_update_type =
4810*77c1e3ccSAndroid Build Coastguard Worker         get_frame_update_type(gf_group, cpi->gf_frame_index);
4811*77c1e3ccSAndroid Build Coastguard Worker 
4812*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr,
4813*77c1e3ccSAndroid Build Coastguard Worker             "\n Frame number: %d, Frame type: %s, Show Frame: %d, Frame Update "
4814*77c1e3ccSAndroid Build Coastguard Worker             "Type: %d, Q: %d\n",
4815*77c1e3ccSAndroid Build Coastguard Worker             cm->current_frame.frame_number,
4816*77c1e3ccSAndroid Build Coastguard Worker             get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame,
4817*77c1e3ccSAndroid Build Coastguard Worker             frame_update_type, cm->quant_params.base_qindex);
4818*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < kTimingComponents; i++) {
4819*77c1e3ccSAndroid Build Coastguard Worker       cpi->component_time[i] += cpi->frame_component_time[i];
4820*77c1e3ccSAndroid Build Coastguard Worker       // Use av1_encode_strategy_time (i = 0) as the total time.
4821*77c1e3ccSAndroid Build Coastguard Worker       if (i == 0) {
4822*77c1e3ccSAndroid Build Coastguard Worker         frame_total = cpi->frame_component_time[0];
4823*77c1e3ccSAndroid Build Coastguard Worker         total = cpi->component_time[0];
4824*77c1e3ccSAndroid Build Coastguard Worker       }
4825*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr,
4826*77c1e3ccSAndroid Build Coastguard Worker               " %50s:  %15" PRId64 " us [%6.2f%%] (total: %15" PRId64
4827*77c1e3ccSAndroid Build Coastguard Worker               " us [%6.2f%%])\n",
4828*77c1e3ccSAndroid Build Coastguard Worker               get_component_name(i), cpi->frame_component_time[i],
4829*77c1e3ccSAndroid Build Coastguard Worker               (float)((float)cpi->frame_component_time[i] * 100.0 /
4830*77c1e3ccSAndroid Build Coastguard Worker                       (float)frame_total),
4831*77c1e3ccSAndroid Build Coastguard Worker               cpi->component_time[i],
4832*77c1e3ccSAndroid Build Coastguard Worker               (float)((float)cpi->component_time[i] * 100.0 / (float)total));
4833*77c1e3ccSAndroid Build Coastguard Worker       cpi->frame_component_time[i] = 0;
4834*77c1e3ccSAndroid Build Coastguard Worker     }
4835*77c1e3ccSAndroid Build Coastguard Worker   }
4836*77c1e3ccSAndroid Build Coastguard Worker #endif
4837*77c1e3ccSAndroid Build Coastguard Worker 
4838*77c1e3ccSAndroid Build Coastguard Worker   // Reset the flag to 0 afer encoding.
4839*77c1e3ccSAndroid Build Coastguard Worker   cpi->rc.use_external_qp_one_pass = 0;
4840*77c1e3ccSAndroid Build Coastguard Worker 
4841*77c1e3ccSAndroid Build Coastguard Worker   if (result == -1) {
4842*77c1e3ccSAndroid Build Coastguard Worker     cm->error->setjmp = 0;
4843*77c1e3ccSAndroid Build Coastguard Worker     // Returning -1 indicates no frame encoded; more input is required
4844*77c1e3ccSAndroid Build Coastguard Worker     return -1;
4845*77c1e3ccSAndroid Build Coastguard Worker   }
4846*77c1e3ccSAndroid Build Coastguard Worker   if (result != AOM_CODEC_OK) {
4847*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
4848*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to encode frame");
4849*77c1e3ccSAndroid Build Coastguard Worker   }
4850*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
4851*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_mark(&cmptimer);
4852*77c1e3ccSAndroid Build Coastguard Worker   cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
4853*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_INTERNAL_STATS
4854*77c1e3ccSAndroid Build Coastguard Worker 
4855*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_SPEED_STATS
4856*77c1e3ccSAndroid Build Coastguard Worker   if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
4857*77c1e3ccSAndroid Build Coastguard Worker     cpi->tx_search_count += cpi->td.mb.txfm_search_info.tx_search_count;
4858*77c1e3ccSAndroid Build Coastguard Worker     cpi->td.mb.txfm_search_info.tx_search_count = 0;
4859*77c1e3ccSAndroid Build Coastguard Worker   }
4860*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_SPEED_STATS
4861*77c1e3ccSAndroid Build Coastguard Worker 
4862*77c1e3ccSAndroid Build Coastguard Worker   cm->error->setjmp = 0;
4863*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
4864*77c1e3ccSAndroid Build Coastguard Worker }
4865*77c1e3ccSAndroid Build Coastguard Worker 
4866*77c1e3ccSAndroid Build Coastguard Worker // Populates cpi->scaled_ref_buf corresponding to frames in a parallel encode
4867*77c1e3ccSAndroid Build Coastguard Worker // set. Also sets the bitmask 'ref_buffers_used_map'.
scale_references_fpmt(AV1_COMP * cpi,int * ref_buffers_used_map)4868*77c1e3ccSAndroid Build Coastguard Worker static void scale_references_fpmt(AV1_COMP *cpi, int *ref_buffers_used_map) {
4869*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
4870*77c1e3ccSAndroid Build Coastguard Worker   MV_REFERENCE_FRAME ref_frame;
4871*77c1e3ccSAndroid Build Coastguard Worker 
4872*77c1e3ccSAndroid Build Coastguard Worker   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4873*77c1e3ccSAndroid Build Coastguard Worker     // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
4874*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
4875*77c1e3ccSAndroid Build Coastguard Worker       const YV12_BUFFER_CONFIG *const ref =
4876*77c1e3ccSAndroid Build Coastguard Worker           get_ref_frame_yv12_buf(cm, ref_frame);
4877*77c1e3ccSAndroid Build Coastguard Worker 
4878*77c1e3ccSAndroid Build Coastguard Worker       if (ref == NULL) {
4879*77c1e3ccSAndroid Build Coastguard Worker         cpi->scaled_ref_buf[ref_frame - 1] = NULL;
4880*77c1e3ccSAndroid Build Coastguard Worker         continue;
4881*77c1e3ccSAndroid Build Coastguard Worker       }
4882*77c1e3ccSAndroid Build Coastguard Worker 
4883*77c1e3ccSAndroid Build Coastguard Worker       // FPMT does not support scaling yet.
4884*77c1e3ccSAndroid Build Coastguard Worker       assert(ref->y_crop_width == cm->width &&
4885*77c1e3ccSAndroid Build Coastguard Worker              ref->y_crop_height == cm->height);
4886*77c1e3ccSAndroid Build Coastguard Worker 
4887*77c1e3ccSAndroid Build Coastguard Worker       RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
4888*77c1e3ccSAndroid Build Coastguard Worker       cpi->scaled_ref_buf[ref_frame - 1] = buf;
4889*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < cm->buffer_pool->num_frame_bufs; ++i) {
4890*77c1e3ccSAndroid Build Coastguard Worker         if (&cm->buffer_pool->frame_bufs[i] == buf) {
4891*77c1e3ccSAndroid Build Coastguard Worker           *ref_buffers_used_map |= (1 << i);
4892*77c1e3ccSAndroid Build Coastguard Worker         }
4893*77c1e3ccSAndroid Build Coastguard Worker       }
4894*77c1e3ccSAndroid Build Coastguard Worker     } else {
4895*77c1e3ccSAndroid Build Coastguard Worker       if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
4896*77c1e3ccSAndroid Build Coastguard Worker     }
4897*77c1e3ccSAndroid Build Coastguard Worker   }
4898*77c1e3ccSAndroid Build Coastguard Worker }
4899*77c1e3ccSAndroid Build Coastguard Worker 
4900*77c1e3ccSAndroid Build Coastguard Worker // Increments the ref_count of frame buffers referenced by cpi->scaled_ref_buf
4901*77c1e3ccSAndroid Build Coastguard Worker // corresponding to frames in a parallel encode set.
increment_scaled_ref_counts_fpmt(BufferPool * buffer_pool,int ref_buffers_used_map)4902*77c1e3ccSAndroid Build Coastguard Worker static void increment_scaled_ref_counts_fpmt(BufferPool *buffer_pool,
4903*77c1e3ccSAndroid Build Coastguard Worker                                              int ref_buffers_used_map) {
4904*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < buffer_pool->num_frame_bufs; ++i) {
4905*77c1e3ccSAndroid Build Coastguard Worker     if (ref_buffers_used_map & (1 << i)) {
4906*77c1e3ccSAndroid Build Coastguard Worker       ++buffer_pool->frame_bufs[i].ref_count;
4907*77c1e3ccSAndroid Build Coastguard Worker     }
4908*77c1e3ccSAndroid Build Coastguard Worker   }
4909*77c1e3ccSAndroid Build Coastguard Worker }
4910*77c1e3ccSAndroid Build Coastguard Worker 
4911*77c1e3ccSAndroid Build Coastguard Worker // Releases cpi->scaled_ref_buf corresponding to frames in a parallel encode
4912*77c1e3ccSAndroid Build Coastguard Worker // set.
av1_release_scaled_references_fpmt(AV1_COMP * cpi)4913*77c1e3ccSAndroid Build Coastguard Worker void av1_release_scaled_references_fpmt(AV1_COMP *cpi) {
4914*77c1e3ccSAndroid Build Coastguard Worker   // TODO(isbs): only refresh the necessary frames, rather than all of them
4915*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4916*77c1e3ccSAndroid Build Coastguard Worker     RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
4917*77c1e3ccSAndroid Build Coastguard Worker     if (buf != NULL) {
4918*77c1e3ccSAndroid Build Coastguard Worker       cpi->scaled_ref_buf[i] = NULL;
4919*77c1e3ccSAndroid Build Coastguard Worker     }
4920*77c1e3ccSAndroid Build Coastguard Worker   }
4921*77c1e3ccSAndroid Build Coastguard Worker }
4922*77c1e3ccSAndroid Build Coastguard Worker 
4923*77c1e3ccSAndroid Build Coastguard Worker // Decrements the ref_count of frame buffers referenced by cpi->scaled_ref_buf
4924*77c1e3ccSAndroid Build Coastguard Worker // corresponding to frames in a parallel encode set.
av1_decrement_ref_counts_fpmt(BufferPool * buffer_pool,int ref_buffers_used_map)4925*77c1e3ccSAndroid Build Coastguard Worker void av1_decrement_ref_counts_fpmt(BufferPool *buffer_pool,
4926*77c1e3ccSAndroid Build Coastguard Worker                                    int ref_buffers_used_map) {
4927*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < buffer_pool->num_frame_bufs; ++i) {
4928*77c1e3ccSAndroid Build Coastguard Worker     if (ref_buffers_used_map & (1 << i)) {
4929*77c1e3ccSAndroid Build Coastguard Worker       --buffer_pool->frame_bufs[i].ref_count;
4930*77c1e3ccSAndroid Build Coastguard Worker     }
4931*77c1e3ccSAndroid Build Coastguard Worker   }
4932*77c1e3ccSAndroid Build Coastguard Worker }
4933*77c1e3ccSAndroid Build Coastguard Worker 
4934*77c1e3ccSAndroid Build Coastguard Worker // Initialize parallel frame contexts with screen content decisions.
av1_init_sc_decisions(AV1_PRIMARY * const ppi)4935*77c1e3ccSAndroid Build Coastguard Worker void av1_init_sc_decisions(AV1_PRIMARY *const ppi) {
4936*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *const first_cpi = ppi->cpi;
4937*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 1; i < ppi->num_fp_contexts; ++i) {
4938*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *cur_cpi = ppi->parallel_cpi[i];
4939*77c1e3ccSAndroid Build Coastguard Worker     cur_cpi->common.features.allow_screen_content_tools =
4940*77c1e3ccSAndroid Build Coastguard Worker         first_cpi->common.features.allow_screen_content_tools;
4941*77c1e3ccSAndroid Build Coastguard Worker     cur_cpi->common.features.allow_intrabc =
4942*77c1e3ccSAndroid Build Coastguard Worker         first_cpi->common.features.allow_intrabc;
4943*77c1e3ccSAndroid Build Coastguard Worker     cur_cpi->use_screen_content_tools = first_cpi->use_screen_content_tools;
4944*77c1e3ccSAndroid Build Coastguard Worker     cur_cpi->is_screen_content_type = first_cpi->is_screen_content_type;
4945*77c1e3ccSAndroid Build Coastguard Worker   }
4946*77c1e3ccSAndroid Build Coastguard Worker }
4947*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_parallel_frame_enc_data(AV1_PRIMARY * const ppi,AV1_COMP_DATA * const first_cpi_data)4948*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *av1_get_parallel_frame_enc_data(AV1_PRIMARY *const ppi,
4949*77c1e3ccSAndroid Build Coastguard Worker                                           AV1_COMP_DATA *const first_cpi_data) {
4950*77c1e3ccSAndroid Build Coastguard Worker   int cpi_idx = 0;
4951*77c1e3ccSAndroid Build Coastguard Worker 
4952*77c1e3ccSAndroid Build Coastguard Worker   // Loop over parallel_cpi to find the cpi that processed the current
4953*77c1e3ccSAndroid Build Coastguard Worker   // gf_frame_index ahead of time.
4954*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 1; i < ppi->num_fp_contexts; i++) {
4955*77c1e3ccSAndroid Build Coastguard Worker     if (ppi->cpi->gf_frame_index == ppi->parallel_cpi[i]->gf_frame_index) {
4956*77c1e3ccSAndroid Build Coastguard Worker       cpi_idx = i;
4957*77c1e3ccSAndroid Build Coastguard Worker       break;
4958*77c1e3ccSAndroid Build Coastguard Worker     }
4959*77c1e3ccSAndroid Build Coastguard Worker   }
4960*77c1e3ccSAndroid Build Coastguard Worker 
4961*77c1e3ccSAndroid Build Coastguard Worker   assert(cpi_idx > 0);
4962*77c1e3ccSAndroid Build Coastguard Worker   assert(!ppi->parallel_cpi[cpi_idx]->common.show_existing_frame);
4963*77c1e3ccSAndroid Build Coastguard Worker 
4964*77c1e3ccSAndroid Build Coastguard Worker   // Release the previously-used frame-buffer.
4965*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->cpi->common.cur_frame != NULL) {
4966*77c1e3ccSAndroid Build Coastguard Worker     --ppi->cpi->common.cur_frame->ref_count;
4967*77c1e3ccSAndroid Build Coastguard Worker     ppi->cpi->common.cur_frame = NULL;
4968*77c1e3ccSAndroid Build Coastguard Worker   }
4969*77c1e3ccSAndroid Build Coastguard Worker 
4970*77c1e3ccSAndroid Build Coastguard Worker   // Swap the appropriate parallel_cpi with the parallel_cpi[0].
4971*77c1e3ccSAndroid Build Coastguard Worker   ppi->cpi = ppi->parallel_cpi[cpi_idx];
4972*77c1e3ccSAndroid Build Coastguard Worker   ppi->parallel_cpi[cpi_idx] = ppi->parallel_cpi[0];
4973*77c1e3ccSAndroid Build Coastguard Worker   ppi->parallel_cpi[0] = ppi->cpi;
4974*77c1e3ccSAndroid Build Coastguard Worker 
4975*77c1e3ccSAndroid Build Coastguard Worker   // Copy appropriate parallel_frames_data to local data.
4976*77c1e3ccSAndroid Build Coastguard Worker   {
4977*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP_DATA *data = &ppi->parallel_frames_data[cpi_idx - 1];
4978*77c1e3ccSAndroid Build Coastguard Worker     assert(data->frame_size > 0);
4979*77c1e3ccSAndroid Build Coastguard Worker     if (data->frame_size > first_cpi_data->cx_data_sz) {
4980*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(&ppi->error, AOM_CODEC_ERROR,
4981*77c1e3ccSAndroid Build Coastguard Worker                          "first_cpi_data->cx_data buffer full");
4982*77c1e3ccSAndroid Build Coastguard Worker     }
4983*77c1e3ccSAndroid Build Coastguard Worker 
4984*77c1e3ccSAndroid Build Coastguard Worker     first_cpi_data->lib_flags = data->lib_flags;
4985*77c1e3ccSAndroid Build Coastguard Worker     first_cpi_data->ts_frame_start = data->ts_frame_start;
4986*77c1e3ccSAndroid Build Coastguard Worker     first_cpi_data->ts_frame_end = data->ts_frame_end;
4987*77c1e3ccSAndroid Build Coastguard Worker     memcpy(first_cpi_data->cx_data, data->cx_data, data->frame_size);
4988*77c1e3ccSAndroid Build Coastguard Worker     first_cpi_data->frame_size = data->frame_size;
4989*77c1e3ccSAndroid Build Coastguard Worker     if (ppi->cpi->common.show_frame) {
4990*77c1e3ccSAndroid Build Coastguard Worker       first_cpi_data->pop_lookahead = 1;
4991*77c1e3ccSAndroid Build Coastguard Worker     }
4992*77c1e3ccSAndroid Build Coastguard Worker   }
4993*77c1e3ccSAndroid Build Coastguard Worker 
4994*77c1e3ccSAndroid Build Coastguard Worker   return ppi->cpi;
4995*77c1e3ccSAndroid Build Coastguard Worker }
4996*77c1e3ccSAndroid Build Coastguard Worker 
4997*77c1e3ccSAndroid Build Coastguard Worker // Initialises frames belonging to a parallel encode set.
av1_init_parallel_frame_context(const AV1_COMP_DATA * const first_cpi_data,AV1_PRIMARY * const ppi,int * ref_buffers_used_map)4998*77c1e3ccSAndroid Build Coastguard Worker int av1_init_parallel_frame_context(const AV1_COMP_DATA *const first_cpi_data,
4999*77c1e3ccSAndroid Build Coastguard Worker                                     AV1_PRIMARY *const ppi,
5000*77c1e3ccSAndroid Build Coastguard Worker                                     int *ref_buffers_used_map) {
5001*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *const first_cpi = ppi->cpi;
5002*77c1e3ccSAndroid Build Coastguard Worker   GF_GROUP *const gf_group = &ppi->gf_group;
5003*77c1e3ccSAndroid Build Coastguard Worker   int gf_index_start = first_cpi->gf_frame_index;
5004*77c1e3ccSAndroid Build Coastguard Worker   assert(gf_group->frame_parallel_level[gf_index_start] == 1);
5005*77c1e3ccSAndroid Build Coastguard Worker   int parallel_frame_count = 0;
5006*77c1e3ccSAndroid Build Coastguard Worker   int cur_frame_num = first_cpi->common.current_frame.frame_number;
5007*77c1e3ccSAndroid Build Coastguard Worker   int show_frame_count = first_cpi->frame_index_set.show_frame_count;
5008*77c1e3ccSAndroid Build Coastguard Worker   int frames_since_key = first_cpi->rc.frames_since_key;
5009*77c1e3ccSAndroid Build Coastguard Worker   int frames_to_key = first_cpi->rc.frames_to_key;
5010*77c1e3ccSAndroid Build Coastguard Worker   int frames_to_fwd_kf = first_cpi->rc.frames_to_fwd_kf;
5011*77c1e3ccSAndroid Build Coastguard Worker   int cur_frame_disp = cur_frame_num + gf_group->arf_src_offset[gf_index_start];
5012*77c1e3ccSAndroid Build Coastguard Worker   const FIRSTPASS_STATS *stats_in = first_cpi->twopass_frame.stats_in;
5013*77c1e3ccSAndroid Build Coastguard Worker 
5014*77c1e3ccSAndroid Build Coastguard Worker   assert(*ref_buffers_used_map == 0);
5015*77c1e3ccSAndroid Build Coastguard Worker 
5016*77c1e3ccSAndroid Build Coastguard Worker   // Release the previously used frame-buffer by a frame_parallel_level 1 frame.
5017*77c1e3ccSAndroid Build Coastguard Worker   if (first_cpi->common.cur_frame != NULL) {
5018*77c1e3ccSAndroid Build Coastguard Worker     --first_cpi->common.cur_frame->ref_count;
5019*77c1e3ccSAndroid Build Coastguard Worker     first_cpi->common.cur_frame = NULL;
5020*77c1e3ccSAndroid Build Coastguard Worker   }
5021*77c1e3ccSAndroid Build Coastguard Worker 
5022*77c1e3ccSAndroid Build Coastguard Worker   RefFrameMapPair ref_frame_map_pairs[REF_FRAMES];
5023*77c1e3ccSAndroid Build Coastguard Worker   RefFrameMapPair first_ref_frame_map_pairs[REF_FRAMES];
5024*77c1e3ccSAndroid Build Coastguard Worker   init_ref_map_pair(first_cpi, first_ref_frame_map_pairs);
5025*77c1e3ccSAndroid Build Coastguard Worker   memcpy(ref_frame_map_pairs, first_ref_frame_map_pairs,
5026*77c1e3ccSAndroid Build Coastguard Worker          sizeof(RefFrameMapPair) * REF_FRAMES);
5027*77c1e3ccSAndroid Build Coastguard Worker 
5028*77c1e3ccSAndroid Build Coastguard Worker   // Store the reference refresh index of frame_parallel_level 1 frame in a
5029*77c1e3ccSAndroid Build Coastguard Worker   // parallel encode set of lower layer frames.
5030*77c1e3ccSAndroid Build Coastguard Worker   if (gf_group->update_type[gf_index_start] == INTNL_ARF_UPDATE) {
5031*77c1e3ccSAndroid Build Coastguard Worker     first_cpi->ref_refresh_index = av1_calc_refresh_idx_for_intnl_arf(
5032*77c1e3ccSAndroid Build Coastguard Worker         first_cpi, ref_frame_map_pairs, gf_index_start);
5033*77c1e3ccSAndroid Build Coastguard Worker     assert(first_cpi->ref_refresh_index != INVALID_IDX &&
5034*77c1e3ccSAndroid Build Coastguard Worker            first_cpi->ref_refresh_index < REF_FRAMES);
5035*77c1e3ccSAndroid Build Coastguard Worker     first_cpi->refresh_idx_available = true;
5036*77c1e3ccSAndroid Build Coastguard Worker     // Update ref_frame_map_pairs.
5037*77c1e3ccSAndroid Build Coastguard Worker     ref_frame_map_pairs[first_cpi->ref_refresh_index].disp_order =
5038*77c1e3ccSAndroid Build Coastguard Worker         gf_group->display_idx[gf_index_start];
5039*77c1e3ccSAndroid Build Coastguard Worker     ref_frame_map_pairs[first_cpi->ref_refresh_index].pyr_level =
5040*77c1e3ccSAndroid Build Coastguard Worker         gf_group->layer_depth[gf_index_start];
5041*77c1e3ccSAndroid Build Coastguard Worker   }
5042*77c1e3ccSAndroid Build Coastguard Worker 
5043*77c1e3ccSAndroid Build Coastguard Worker   // Set do_frame_data_update flag as false for frame_parallel_level 1 frame.
5044*77c1e3ccSAndroid Build Coastguard Worker   first_cpi->do_frame_data_update = false;
5045*77c1e3ccSAndroid Build Coastguard Worker   if (gf_group->arf_src_offset[gf_index_start] == 0) {
5046*77c1e3ccSAndroid Build Coastguard Worker     first_cpi->time_stamps.prev_ts_start = ppi->ts_start_last_show_frame;
5047*77c1e3ccSAndroid Build Coastguard Worker     first_cpi->time_stamps.prev_ts_end = ppi->ts_end_last_show_frame;
5048*77c1e3ccSAndroid Build Coastguard Worker   }
5049*77c1e3ccSAndroid Build Coastguard Worker 
5050*77c1e3ccSAndroid Build Coastguard Worker   av1_get_ref_frames(first_ref_frame_map_pairs, cur_frame_disp, first_cpi,
5051*77c1e3ccSAndroid Build Coastguard Worker                      gf_index_start, 1, first_cpi->common.remapped_ref_idx);
5052*77c1e3ccSAndroid Build Coastguard Worker 
5053*77c1e3ccSAndroid Build Coastguard Worker   scale_references_fpmt(first_cpi, ref_buffers_used_map);
5054*77c1e3ccSAndroid Build Coastguard Worker   parallel_frame_count++;
5055*77c1e3ccSAndroid Build Coastguard Worker 
5056*77c1e3ccSAndroid Build Coastguard Worker   // Iterate through the GF_GROUP to find the remaining frame_parallel_level 2
5057*77c1e3ccSAndroid Build Coastguard Worker   // frames which are part of the current parallel encode set and initialize the
5058*77c1e3ccSAndroid Build Coastguard Worker   // required cpi elements.
5059*77c1e3ccSAndroid Build Coastguard Worker   for (int i = gf_index_start + 1; i < gf_group->size; i++) {
5060*77c1e3ccSAndroid Build Coastguard Worker     // Update frame counters if previous frame was show frame or show existing
5061*77c1e3ccSAndroid Build Coastguard Worker     // frame.
5062*77c1e3ccSAndroid Build Coastguard Worker     if (gf_group->arf_src_offset[i - 1] == 0) {
5063*77c1e3ccSAndroid Build Coastguard Worker       cur_frame_num++;
5064*77c1e3ccSAndroid Build Coastguard Worker       show_frame_count++;
5065*77c1e3ccSAndroid Build Coastguard Worker       if (frames_to_fwd_kf <= 0)
5066*77c1e3ccSAndroid Build Coastguard Worker         frames_to_fwd_kf = first_cpi->oxcf.kf_cfg.fwd_kf_dist;
5067*77c1e3ccSAndroid Build Coastguard Worker       if (frames_to_key) {
5068*77c1e3ccSAndroid Build Coastguard Worker         frames_since_key++;
5069*77c1e3ccSAndroid Build Coastguard Worker         frames_to_key--;
5070*77c1e3ccSAndroid Build Coastguard Worker         frames_to_fwd_kf--;
5071*77c1e3ccSAndroid Build Coastguard Worker       }
5072*77c1e3ccSAndroid Build Coastguard Worker       stats_in++;
5073*77c1e3ccSAndroid Build Coastguard Worker     }
5074*77c1e3ccSAndroid Build Coastguard Worker     cur_frame_disp = cur_frame_num + gf_group->arf_src_offset[i];
5075*77c1e3ccSAndroid Build Coastguard Worker     if (gf_group->frame_parallel_level[i] == 2) {
5076*77c1e3ccSAndroid Build Coastguard Worker       AV1_COMP *cur_cpi = ppi->parallel_cpi[parallel_frame_count];
5077*77c1e3ccSAndroid Build Coastguard Worker       AV1_COMP_DATA *cur_cpi_data =
5078*77c1e3ccSAndroid Build Coastguard Worker           &ppi->parallel_frames_data[parallel_frame_count - 1];
5079*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->gf_frame_index = i;
5080*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->framerate = first_cpi->framerate;
5081*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->common.current_frame.frame_number = cur_frame_num;
5082*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->common.current_frame.frame_type = gf_group->frame_type[i];
5083*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->frame_index_set.show_frame_count = show_frame_count;
5084*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->rc.frames_since_key = frames_since_key;
5085*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->rc.frames_to_key = frames_to_key;
5086*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->rc.frames_to_fwd_kf = frames_to_fwd_kf;
5087*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->rc.active_worst_quality = first_cpi->rc.active_worst_quality;
5088*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->rc.avg_frame_bandwidth = first_cpi->rc.avg_frame_bandwidth;
5089*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->rc.max_frame_bandwidth = first_cpi->rc.max_frame_bandwidth;
5090*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->rc.min_frame_bandwidth = first_cpi->rc.min_frame_bandwidth;
5091*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->rc.intervals_till_gf_calculate_due =
5092*77c1e3ccSAndroid Build Coastguard Worker           first_cpi->rc.intervals_till_gf_calculate_due;
5093*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->mv_search_params.max_mv_magnitude =
5094*77c1e3ccSAndroid Build Coastguard Worker           first_cpi->mv_search_params.max_mv_magnitude;
5095*77c1e3ccSAndroid Build Coastguard Worker       if (gf_group->update_type[cur_cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
5096*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi->common.lf.mode_ref_delta_enabled = 1;
5097*77c1e3ccSAndroid Build Coastguard Worker       }
5098*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->do_frame_data_update = false;
5099*77c1e3ccSAndroid Build Coastguard Worker       // Initialize prev_ts_start and prev_ts_end for show frame(s) and show
5100*77c1e3ccSAndroid Build Coastguard Worker       // existing frame(s).
5101*77c1e3ccSAndroid Build Coastguard Worker       if (gf_group->arf_src_offset[i] == 0) {
5102*77c1e3ccSAndroid Build Coastguard Worker         // Choose source of prev frame.
5103*77c1e3ccSAndroid Build Coastguard Worker         int src_index = gf_group->src_offset[i];
5104*77c1e3ccSAndroid Build Coastguard Worker         struct lookahead_entry *prev_source = av1_lookahead_peek(
5105*77c1e3ccSAndroid Build Coastguard Worker             ppi->lookahead, src_index - 1, cur_cpi->compressor_stage);
5106*77c1e3ccSAndroid Build Coastguard Worker         // Save timestamps of prev frame.
5107*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi->time_stamps.prev_ts_start = prev_source->ts_start;
5108*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi->time_stamps.prev_ts_end = prev_source->ts_end;
5109*77c1e3ccSAndroid Build Coastguard Worker       }
5110*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->time_stamps.first_ts_start =
5111*77c1e3ccSAndroid Build Coastguard Worker           first_cpi->time_stamps.first_ts_start;
5112*77c1e3ccSAndroid Build Coastguard Worker 
5113*77c1e3ccSAndroid Build Coastguard Worker       memcpy(cur_cpi->common.ref_frame_map, first_cpi->common.ref_frame_map,
5114*77c1e3ccSAndroid Build Coastguard Worker              sizeof(first_cpi->common.ref_frame_map));
5115*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi_data->lib_flags = 0;
5116*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi_data->timestamp_ratio = first_cpi_data->timestamp_ratio;
5117*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi_data->flush = first_cpi_data->flush;
5118*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi_data->frame_size = 0;
5119*77c1e3ccSAndroid Build Coastguard Worker       if (gf_group->update_type[gf_index_start] == INTNL_ARF_UPDATE) {
5120*77c1e3ccSAndroid Build Coastguard Worker         // If the first frame in a parallel encode set is INTNL_ARF_UPDATE
5121*77c1e3ccSAndroid Build Coastguard Worker         // frame, initialize lib_flags of frame_parallel_level 2 frame in the
5122*77c1e3ccSAndroid Build Coastguard Worker         // set with that of frame_parallel_level 1 frame.
5123*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi_data->lib_flags = first_cpi_data->lib_flags;
5124*77c1e3ccSAndroid Build Coastguard Worker         // Store the reference refresh index of frame_parallel_level 2 frame in
5125*77c1e3ccSAndroid Build Coastguard Worker         // a parallel encode set of lower layer frames.
5126*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi->ref_refresh_index =
5127*77c1e3ccSAndroid Build Coastguard Worker             av1_calc_refresh_idx_for_intnl_arf(cur_cpi, ref_frame_map_pairs, i);
5128*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi->refresh_idx_available = true;
5129*77c1e3ccSAndroid Build Coastguard Worker         // Skip the reference frame which will be refreshed by
5130*77c1e3ccSAndroid Build Coastguard Worker         // frame_parallel_level 1 frame in a parallel encode set of lower layer
5131*77c1e3ccSAndroid Build Coastguard Worker         // frames.
5132*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi->ref_idx_to_skip = first_cpi->ref_refresh_index;
5133*77c1e3ccSAndroid Build Coastguard Worker       } else {
5134*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi->ref_idx_to_skip = INVALID_IDX;
5135*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi->ref_refresh_index = INVALID_IDX;
5136*77c1e3ccSAndroid Build Coastguard Worker         cur_cpi->refresh_idx_available = false;
5137*77c1e3ccSAndroid Build Coastguard Worker       }
5138*77c1e3ccSAndroid Build Coastguard Worker       cur_cpi->twopass_frame.stats_in = stats_in;
5139*77c1e3ccSAndroid Build Coastguard Worker 
5140*77c1e3ccSAndroid Build Coastguard Worker       av1_get_ref_frames(first_ref_frame_map_pairs, cur_frame_disp, cur_cpi, i,
5141*77c1e3ccSAndroid Build Coastguard Worker                          1, cur_cpi->common.remapped_ref_idx);
5142*77c1e3ccSAndroid Build Coastguard Worker       scale_references_fpmt(cur_cpi, ref_buffers_used_map);
5143*77c1e3ccSAndroid Build Coastguard Worker       parallel_frame_count++;
5144*77c1e3ccSAndroid Build Coastguard Worker     }
5145*77c1e3ccSAndroid Build Coastguard Worker 
5146*77c1e3ccSAndroid Build Coastguard Worker     // Set do_frame_data_update to true for the last frame_parallel_level 2
5147*77c1e3ccSAndroid Build Coastguard Worker     // frame in the current parallel encode set.
5148*77c1e3ccSAndroid Build Coastguard Worker     if (i == (gf_group->size - 1) ||
5149*77c1e3ccSAndroid Build Coastguard Worker         (gf_group->frame_parallel_level[i + 1] == 0 &&
5150*77c1e3ccSAndroid Build Coastguard Worker          (gf_group->update_type[i + 1] == ARF_UPDATE ||
5151*77c1e3ccSAndroid Build Coastguard Worker           gf_group->update_type[i + 1] == INTNL_ARF_UPDATE)) ||
5152*77c1e3ccSAndroid Build Coastguard Worker         gf_group->frame_parallel_level[i + 1] == 1) {
5153*77c1e3ccSAndroid Build Coastguard Worker       ppi->parallel_cpi[parallel_frame_count - 1]->do_frame_data_update = true;
5154*77c1e3ccSAndroid Build Coastguard Worker       break;
5155*77c1e3ccSAndroid Build Coastguard Worker     }
5156*77c1e3ccSAndroid Build Coastguard Worker   }
5157*77c1e3ccSAndroid Build Coastguard Worker 
5158*77c1e3ccSAndroid Build Coastguard Worker   increment_scaled_ref_counts_fpmt(first_cpi->common.buffer_pool,
5159*77c1e3ccSAndroid Build Coastguard Worker                                    *ref_buffers_used_map);
5160*77c1e3ccSAndroid Build Coastguard Worker 
5161*77c1e3ccSAndroid Build Coastguard Worker   // Return the number of frames in the parallel encode set.
5162*77c1e3ccSAndroid Build Coastguard Worker   return parallel_frame_count;
5163*77c1e3ccSAndroid Build Coastguard Worker }
5164*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_preview_raw_frame(AV1_COMP * cpi,YV12_BUFFER_CONFIG * dest)5165*77c1e3ccSAndroid Build Coastguard Worker int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5166*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
5167*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->show_frame) {
5168*77c1e3ccSAndroid Build Coastguard Worker     return -1;
5169*77c1e3ccSAndroid Build Coastguard Worker   } else {
5170*77c1e3ccSAndroid Build Coastguard Worker     int ret;
5171*77c1e3ccSAndroid Build Coastguard Worker     if (cm->cur_frame != NULL && !cpi->oxcf.algo_cfg.skip_postproc_filtering) {
5172*77c1e3ccSAndroid Build Coastguard Worker       *dest = cm->cur_frame->buf;
5173*77c1e3ccSAndroid Build Coastguard Worker       dest->y_width = cm->width;
5174*77c1e3ccSAndroid Build Coastguard Worker       dest->y_height = cm->height;
5175*77c1e3ccSAndroid Build Coastguard Worker       dest->uv_width = cm->width >> cm->seq_params->subsampling_x;
5176*77c1e3ccSAndroid Build Coastguard Worker       dest->uv_height = cm->height >> cm->seq_params->subsampling_y;
5177*77c1e3ccSAndroid Build Coastguard Worker       ret = 0;
5178*77c1e3ccSAndroid Build Coastguard Worker     } else {
5179*77c1e3ccSAndroid Build Coastguard Worker       ret = -1;
5180*77c1e3ccSAndroid Build Coastguard Worker     }
5181*77c1e3ccSAndroid Build Coastguard Worker     return ret;
5182*77c1e3ccSAndroid Build Coastguard Worker   }
5183*77c1e3ccSAndroid Build Coastguard Worker }
5184*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_last_show_frame(AV1_COMP * cpi,YV12_BUFFER_CONFIG * frame)5185*77c1e3ccSAndroid Build Coastguard Worker int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
5186*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->last_show_frame_buf == NULL ||
5187*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.algo_cfg.skip_postproc_filtering)
5188*77c1e3ccSAndroid Build Coastguard Worker     return -1;
5189*77c1e3ccSAndroid Build Coastguard Worker 
5190*77c1e3ccSAndroid Build Coastguard Worker   *frame = cpi->last_show_frame_buf->buf;
5191*77c1e3ccSAndroid Build Coastguard Worker   return 0;
5192*77c1e3ccSAndroid Build Coastguard Worker }
5193*77c1e3ccSAndroid Build Coastguard Worker 
av1_copy_new_frame_enc(AV1_COMMON * cm,YV12_BUFFER_CONFIG * new_frame,YV12_BUFFER_CONFIG * sd)5194*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
5195*77c1e3ccSAndroid Build Coastguard Worker                                        YV12_BUFFER_CONFIG *new_frame,
5196*77c1e3ccSAndroid Build Coastguard Worker                                        YV12_BUFFER_CONFIG *sd) {
5197*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
5198*77c1e3ccSAndroid Build Coastguard Worker   if (!equal_dimensions_and_border(new_frame, sd))
5199*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(cm->error, AOM_CODEC_ERROR,
5200*77c1e3ccSAndroid Build Coastguard Worker                        "Incorrect buffer dimensions");
5201*77c1e3ccSAndroid Build Coastguard Worker   else
5202*77c1e3ccSAndroid Build Coastguard Worker     aom_yv12_copy_frame(new_frame, sd, num_planes);
5203*77c1e3ccSAndroid Build Coastguard Worker 
5204*77c1e3ccSAndroid Build Coastguard Worker   return cm->error->error_code;
5205*77c1e3ccSAndroid Build Coastguard Worker }
5206*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_internal_size(AV1EncoderConfig * const oxcf,ResizePendingParams * resize_pending_params,AOM_SCALING_MODE horiz_mode,AOM_SCALING_MODE vert_mode)5207*77c1e3ccSAndroid Build Coastguard Worker int av1_set_internal_size(AV1EncoderConfig *const oxcf,
5208*77c1e3ccSAndroid Build Coastguard Worker                           ResizePendingParams *resize_pending_params,
5209*77c1e3ccSAndroid Build Coastguard Worker                           AOM_SCALING_MODE horiz_mode,
5210*77c1e3ccSAndroid Build Coastguard Worker                           AOM_SCALING_MODE vert_mode) {
5211*77c1e3ccSAndroid Build Coastguard Worker   int hr = 0, hs = 0, vr = 0, vs = 0;
5212*77c1e3ccSAndroid Build Coastguard Worker 
5213*77c1e3ccSAndroid Build Coastguard Worker   // Checks for invalid AOM_SCALING_MODE values.
5214*77c1e3ccSAndroid Build Coastguard Worker   if (horiz_mode > AOME_ONETHREE || vert_mode > AOME_ONETHREE) return -1;
5215*77c1e3ccSAndroid Build Coastguard Worker 
5216*77c1e3ccSAndroid Build Coastguard Worker   Scale2Ratio(horiz_mode, &hr, &hs);
5217*77c1e3ccSAndroid Build Coastguard Worker   Scale2Ratio(vert_mode, &vr, &vs);
5218*77c1e3ccSAndroid Build Coastguard Worker 
5219*77c1e3ccSAndroid Build Coastguard Worker   // always go to the next whole number
5220*77c1e3ccSAndroid Build Coastguard Worker   resize_pending_params->width = (hs - 1 + oxcf->frm_dim_cfg.width * hr) / hs;
5221*77c1e3ccSAndroid Build Coastguard Worker   resize_pending_params->height = (vs - 1 + oxcf->frm_dim_cfg.height * vr) / vs;
5222*77c1e3ccSAndroid Build Coastguard Worker 
5223*77c1e3ccSAndroid Build Coastguard Worker   if (horiz_mode != AOME_NORMAL || vert_mode != AOME_NORMAL) {
5224*77c1e3ccSAndroid Build Coastguard Worker     oxcf->resize_cfg.resize_mode = RESIZE_FIXED;
5225*77c1e3ccSAndroid Build Coastguard Worker     oxcf->algo_cfg.enable_tpl_model = 0;
5226*77c1e3ccSAndroid Build Coastguard Worker   }
5227*77c1e3ccSAndroid Build Coastguard Worker   return 0;
5228*77c1e3ccSAndroid Build Coastguard Worker }
5229*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_quantizer(AV1_COMP * cpi)5230*77c1e3ccSAndroid Build Coastguard Worker int av1_get_quantizer(AV1_COMP *cpi) {
5231*77c1e3ccSAndroid Build Coastguard Worker   return cpi->common.quant_params.base_qindex;
5232*77c1e3ccSAndroid Build Coastguard Worker }
5233*77c1e3ccSAndroid Build Coastguard Worker 
av1_convert_sect5obus_to_annexb(uint8_t * buffer,size_t buffer_size,size_t * frame_size)5234*77c1e3ccSAndroid Build Coastguard Worker int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t buffer_size,
5235*77c1e3ccSAndroid Build Coastguard Worker                                     size_t *frame_size) {
5236*77c1e3ccSAndroid Build Coastguard Worker   assert(*frame_size <= buffer_size);
5237*77c1e3ccSAndroid Build Coastguard Worker   size_t output_size = 0;
5238*77c1e3ccSAndroid Build Coastguard Worker   size_t remaining_size = *frame_size;
5239*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *buff_ptr = buffer;
5240*77c1e3ccSAndroid Build Coastguard Worker 
5241*77c1e3ccSAndroid Build Coastguard Worker   // go through each OBUs
5242*77c1e3ccSAndroid Build Coastguard Worker   while (remaining_size > 0) {
5243*77c1e3ccSAndroid Build Coastguard Worker     uint8_t saved_obu_header[2];
5244*77c1e3ccSAndroid Build Coastguard Worker     uint64_t obu_payload_size;
5245*77c1e3ccSAndroid Build Coastguard Worker     size_t length_of_payload_size;
5246*77c1e3ccSAndroid Build Coastguard Worker     size_t length_of_obu_size;
5247*77c1e3ccSAndroid Build Coastguard Worker     const uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
5248*77c1e3ccSAndroid Build Coastguard Worker     size_t obu_bytes_read = obu_header_size;  // bytes read for current obu
5249*77c1e3ccSAndroid Build Coastguard Worker 
5250*77c1e3ccSAndroid Build Coastguard Worker     // save the obu header (1 or 2 bytes)
5251*77c1e3ccSAndroid Build Coastguard Worker     memcpy(saved_obu_header, buff_ptr, obu_header_size);
5252*77c1e3ccSAndroid Build Coastguard Worker     // clear the obu_has_size_field
5253*77c1e3ccSAndroid Build Coastguard Worker     saved_obu_header[0] &= ~0x2;
5254*77c1e3ccSAndroid Build Coastguard Worker 
5255*77c1e3ccSAndroid Build Coastguard Worker     // get the payload_size and length of payload_size
5256*77c1e3ccSAndroid Build Coastguard Worker     if (aom_uleb_decode(buff_ptr + obu_header_size,
5257*77c1e3ccSAndroid Build Coastguard Worker                         remaining_size - obu_header_size, &obu_payload_size,
5258*77c1e3ccSAndroid Build Coastguard Worker                         &length_of_payload_size) != 0) {
5259*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_ERROR;
5260*77c1e3ccSAndroid Build Coastguard Worker     }
5261*77c1e3ccSAndroid Build Coastguard Worker     obu_bytes_read += length_of_payload_size;
5262*77c1e3ccSAndroid Build Coastguard Worker 
5263*77c1e3ccSAndroid Build Coastguard Worker     // calculate the length of size of the obu header plus payload
5264*77c1e3ccSAndroid Build Coastguard Worker     const uint64_t obu_size = obu_header_size + obu_payload_size;
5265*77c1e3ccSAndroid Build Coastguard Worker     length_of_obu_size = aom_uleb_size_in_bytes(obu_size);
5266*77c1e3ccSAndroid Build Coastguard Worker 
5267*77c1e3ccSAndroid Build Coastguard Worker     if (length_of_obu_size + obu_header_size >
5268*77c1e3ccSAndroid Build Coastguard Worker         buffer_size - output_size - (remaining_size - obu_bytes_read)) {
5269*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_ERROR;
5270*77c1e3ccSAndroid Build Coastguard Worker     }
5271*77c1e3ccSAndroid Build Coastguard Worker     // move the rest of data to new location
5272*77c1e3ccSAndroid Build Coastguard Worker     memmove(buff_ptr + length_of_obu_size + obu_header_size,
5273*77c1e3ccSAndroid Build Coastguard Worker             buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
5274*77c1e3ccSAndroid Build Coastguard Worker     obu_bytes_read += (size_t)obu_payload_size;
5275*77c1e3ccSAndroid Build Coastguard Worker 
5276*77c1e3ccSAndroid Build Coastguard Worker     // write the new obu size
5277*77c1e3ccSAndroid Build Coastguard Worker     size_t coded_obu_size;
5278*77c1e3ccSAndroid Build Coastguard Worker     if (aom_uleb_encode(obu_size, length_of_obu_size, buff_ptr,
5279*77c1e3ccSAndroid Build Coastguard Worker                         &coded_obu_size) != 0 ||
5280*77c1e3ccSAndroid Build Coastguard Worker         coded_obu_size != length_of_obu_size) {
5281*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_ERROR;
5282*77c1e3ccSAndroid Build Coastguard Worker     }
5283*77c1e3ccSAndroid Build Coastguard Worker 
5284*77c1e3ccSAndroid Build Coastguard Worker     // write the saved (modified) obu_header following obu size
5285*77c1e3ccSAndroid Build Coastguard Worker     memcpy(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
5286*77c1e3ccSAndroid Build Coastguard Worker 
5287*77c1e3ccSAndroid Build Coastguard Worker     remaining_size -= obu_bytes_read;
5288*77c1e3ccSAndroid Build Coastguard Worker     buff_ptr += length_of_obu_size + (size_t)obu_size;
5289*77c1e3ccSAndroid Build Coastguard Worker     output_size += length_of_obu_size + (size_t)obu_size;
5290*77c1e3ccSAndroid Build Coastguard Worker   }
5291*77c1e3ccSAndroid Build Coastguard Worker 
5292*77c1e3ccSAndroid Build Coastguard Worker   *frame_size = output_size;
5293*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
5294*77c1e3ccSAndroid Build Coastguard Worker }
5295*77c1e3ccSAndroid Build Coastguard Worker 
rtc_set_updates_ref_frame_config(ExtRefreshFrameFlagsInfo * const ext_refresh_frame_flags,RTC_REF * const rtc_ref)5296*77c1e3ccSAndroid Build Coastguard Worker static void rtc_set_updates_ref_frame_config(
5297*77c1e3ccSAndroid Build Coastguard Worker     ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags,
5298*77c1e3ccSAndroid Build Coastguard Worker     RTC_REF *const rtc_ref) {
5299*77c1e3ccSAndroid Build Coastguard Worker   ext_refresh_frame_flags->update_pending = 1;
5300*77c1e3ccSAndroid Build Coastguard Worker   ext_refresh_frame_flags->last_frame = rtc_ref->refresh[rtc_ref->ref_idx[0]];
5301*77c1e3ccSAndroid Build Coastguard Worker   ext_refresh_frame_flags->golden_frame = rtc_ref->refresh[rtc_ref->ref_idx[3]];
5302*77c1e3ccSAndroid Build Coastguard Worker   ext_refresh_frame_flags->bwd_ref_frame =
5303*77c1e3ccSAndroid Build Coastguard Worker       rtc_ref->refresh[rtc_ref->ref_idx[4]];
5304*77c1e3ccSAndroid Build Coastguard Worker   ext_refresh_frame_flags->alt2_ref_frame =
5305*77c1e3ccSAndroid Build Coastguard Worker       rtc_ref->refresh[rtc_ref->ref_idx[5]];
5306*77c1e3ccSAndroid Build Coastguard Worker   ext_refresh_frame_flags->alt_ref_frame =
5307*77c1e3ccSAndroid Build Coastguard Worker       rtc_ref->refresh[rtc_ref->ref_idx[6]];
5308*77c1e3ccSAndroid Build Coastguard Worker   rtc_ref->non_reference_frame = 1;
5309*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < REF_FRAMES; i++) {
5310*77c1e3ccSAndroid Build Coastguard Worker     if (rtc_ref->refresh[i] == 1) {
5311*77c1e3ccSAndroid Build Coastguard Worker       rtc_ref->non_reference_frame = 0;
5312*77c1e3ccSAndroid Build Coastguard Worker       break;
5313*77c1e3ccSAndroid Build Coastguard Worker     }
5314*77c1e3ccSAndroid Build Coastguard Worker   }
5315*77c1e3ccSAndroid Build Coastguard Worker }
5316*77c1e3ccSAndroid Build Coastguard Worker 
rtc_set_references_external_ref_frame_config(AV1_COMP * cpi)5317*77c1e3ccSAndroid Build Coastguard Worker static int rtc_set_references_external_ref_frame_config(AV1_COMP *cpi) {
5318*77c1e3ccSAndroid Build Coastguard Worker   // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
5319*77c1e3ccSAndroid Build Coastguard Worker   // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
5320*77c1e3ccSAndroid Build Coastguard Worker   int ref = AOM_REFFRAME_ALL;
5321*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
5322*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->ppi->rtc_ref.reference[i]) ref ^= (1 << i);
5323*77c1e3ccSAndroid Build Coastguard Worker   }
5324*77c1e3ccSAndroid Build Coastguard Worker   return ref;
5325*77c1e3ccSAndroid Build Coastguard Worker }
5326*77c1e3ccSAndroid Build Coastguard Worker 
av1_apply_encoding_flags(AV1_COMP * cpi,aom_enc_frame_flags_t flags)5327*77c1e3ccSAndroid Build Coastguard Worker void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
5328*77c1e3ccSAndroid Build Coastguard Worker   // TODO(yunqingwang): For what references to use, external encoding flags
5329*77c1e3ccSAndroid Build Coastguard Worker   // should be consistent with internal reference frame selection. Need to
5330*77c1e3ccSAndroid Build Coastguard Worker   // ensure that there is not conflict between the two. In AV1 encoder, the
5331*77c1e3ccSAndroid Build Coastguard Worker   // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
5332*77c1e3ccSAndroid Build Coastguard Worker   // GOLDEN, BWDREF, ALTREF2.
5333*77c1e3ccSAndroid Build Coastguard Worker 
5334*77c1e3ccSAndroid Build Coastguard Worker   ExternalFlags *const ext_flags = &cpi->ext_flags;
5335*77c1e3ccSAndroid Build Coastguard Worker   ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
5336*77c1e3ccSAndroid Build Coastguard Worker       &ext_flags->refresh_frame;
5337*77c1e3ccSAndroid Build Coastguard Worker   ext_flags->ref_frame_flags = AOM_REFFRAME_ALL;
5338*77c1e3ccSAndroid Build Coastguard Worker   if (flags &
5339*77c1e3ccSAndroid Build Coastguard Worker       (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
5340*77c1e3ccSAndroid Build Coastguard Worker        AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
5341*77c1e3ccSAndroid Build Coastguard Worker        AOM_EFLAG_NO_REF_ARF2)) {
5342*77c1e3ccSAndroid Build Coastguard Worker     int ref = AOM_REFFRAME_ALL;
5343*77c1e3ccSAndroid Build Coastguard Worker 
5344*77c1e3ccSAndroid Build Coastguard Worker     if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
5345*77c1e3ccSAndroid Build Coastguard Worker     if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
5346*77c1e3ccSAndroid Build Coastguard Worker     if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
5347*77c1e3ccSAndroid Build Coastguard Worker 
5348*77c1e3ccSAndroid Build Coastguard Worker     if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
5349*77c1e3ccSAndroid Build Coastguard Worker 
5350*77c1e3ccSAndroid Build Coastguard Worker     if (flags & AOM_EFLAG_NO_REF_ARF) {
5351*77c1e3ccSAndroid Build Coastguard Worker       ref ^= AOM_ALT_FLAG;
5352*77c1e3ccSAndroid Build Coastguard Worker       ref ^= AOM_BWD_FLAG;
5353*77c1e3ccSAndroid Build Coastguard Worker       ref ^= AOM_ALT2_FLAG;
5354*77c1e3ccSAndroid Build Coastguard Worker     } else {
5355*77c1e3ccSAndroid Build Coastguard Worker       if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
5356*77c1e3ccSAndroid Build Coastguard Worker       if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
5357*77c1e3ccSAndroid Build Coastguard Worker     }
5358*77c1e3ccSAndroid Build Coastguard Worker 
5359*77c1e3ccSAndroid Build Coastguard Worker     av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
5360*77c1e3ccSAndroid Build Coastguard Worker   } else {
5361*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->rtc_ref.set_ref_frame_config) {
5362*77c1e3ccSAndroid Build Coastguard Worker       int ref = rtc_set_references_external_ref_frame_config(cpi);
5363*77c1e3ccSAndroid Build Coastguard Worker       av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
5364*77c1e3ccSAndroid Build Coastguard Worker     }
5365*77c1e3ccSAndroid Build Coastguard Worker   }
5366*77c1e3ccSAndroid Build Coastguard Worker 
5367*77c1e3ccSAndroid Build Coastguard Worker   if (flags &
5368*77c1e3ccSAndroid Build Coastguard Worker       (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
5369*77c1e3ccSAndroid Build Coastguard Worker     int upd = AOM_REFFRAME_ALL;
5370*77c1e3ccSAndroid Build Coastguard Worker 
5371*77c1e3ccSAndroid Build Coastguard Worker     // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
5372*77c1e3ccSAndroid Build Coastguard Worker     if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
5373*77c1e3ccSAndroid Build Coastguard Worker 
5374*77c1e3ccSAndroid Build Coastguard Worker     if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
5375*77c1e3ccSAndroid Build Coastguard Worker 
5376*77c1e3ccSAndroid Build Coastguard Worker     if (flags & AOM_EFLAG_NO_UPD_ARF) {
5377*77c1e3ccSAndroid Build Coastguard Worker       upd ^= AOM_ALT_FLAG;
5378*77c1e3ccSAndroid Build Coastguard Worker       upd ^= AOM_BWD_FLAG;
5379*77c1e3ccSAndroid Build Coastguard Worker       upd ^= AOM_ALT2_FLAG;
5380*77c1e3ccSAndroid Build Coastguard Worker     }
5381*77c1e3ccSAndroid Build Coastguard Worker 
5382*77c1e3ccSAndroid Build Coastguard Worker     ext_refresh_frame_flags->last_frame = (upd & AOM_LAST_FLAG) != 0;
5383*77c1e3ccSAndroid Build Coastguard Worker     ext_refresh_frame_flags->golden_frame = (upd & AOM_GOLD_FLAG) != 0;
5384*77c1e3ccSAndroid Build Coastguard Worker     ext_refresh_frame_flags->alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
5385*77c1e3ccSAndroid Build Coastguard Worker     ext_refresh_frame_flags->bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
5386*77c1e3ccSAndroid Build Coastguard Worker     ext_refresh_frame_flags->alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
5387*77c1e3ccSAndroid Build Coastguard Worker     ext_refresh_frame_flags->update_pending = 1;
5388*77c1e3ccSAndroid Build Coastguard Worker   } else {
5389*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->rtc_ref.set_ref_frame_config)
5390*77c1e3ccSAndroid Build Coastguard Worker       rtc_set_updates_ref_frame_config(ext_refresh_frame_flags,
5391*77c1e3ccSAndroid Build Coastguard Worker                                        &cpi->ppi->rtc_ref);
5392*77c1e3ccSAndroid Build Coastguard Worker     else
5393*77c1e3ccSAndroid Build Coastguard Worker       ext_refresh_frame_flags->update_pending = 0;
5394*77c1e3ccSAndroid Build Coastguard Worker   }
5395*77c1e3ccSAndroid Build Coastguard Worker 
5396*77c1e3ccSAndroid Build Coastguard Worker   ext_flags->use_ref_frame_mvs = cpi->oxcf.tool_cfg.enable_ref_frame_mvs &
5397*77c1e3ccSAndroid Build Coastguard Worker                                  ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
5398*77c1e3ccSAndroid Build Coastguard Worker   ext_flags->use_error_resilient = cpi->oxcf.tool_cfg.error_resilient_mode |
5399*77c1e3ccSAndroid Build Coastguard Worker                                    ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
5400*77c1e3ccSAndroid Build Coastguard Worker   ext_flags->use_s_frame =
5401*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.kf_cfg.enable_sframe | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
5402*77c1e3ccSAndroid Build Coastguard Worker   ext_flags->use_primary_ref_none =
5403*77c1e3ccSAndroid Build Coastguard Worker       (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
5404*77c1e3ccSAndroid Build Coastguard Worker 
5405*77c1e3ccSAndroid Build Coastguard Worker   if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
5406*77c1e3ccSAndroid Build Coastguard Worker     update_entropy(&ext_flags->refresh_frame_context,
5407*77c1e3ccSAndroid Build Coastguard Worker                    &ext_flags->refresh_frame_context_pending, 0);
5408*77c1e3ccSAndroid Build Coastguard Worker   }
5409*77c1e3ccSAndroid Build Coastguard Worker }
5410*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_global_headers(AV1_PRIMARY * ppi)5411*77c1e3ccSAndroid Build Coastguard Worker aom_fixed_buf_t *av1_get_global_headers(AV1_PRIMARY *ppi) {
5412*77c1e3ccSAndroid Build Coastguard Worker   if (!ppi) return NULL;
5413*77c1e3ccSAndroid Build Coastguard Worker 
5414*77c1e3ccSAndroid Build Coastguard Worker   uint8_t header_buf[512] = { 0 };
5415*77c1e3ccSAndroid Build Coastguard Worker   const uint32_t sequence_header_size = av1_write_sequence_header_obu(
5416*77c1e3ccSAndroid Build Coastguard Worker       &ppi->seq_params, &header_buf[0], sizeof(header_buf));
5417*77c1e3ccSAndroid Build Coastguard Worker   assert(sequence_header_size <= sizeof(header_buf));
5418*77c1e3ccSAndroid Build Coastguard Worker   if (sequence_header_size == 0) return NULL;
5419*77c1e3ccSAndroid Build Coastguard Worker 
5420*77c1e3ccSAndroid Build Coastguard Worker   const size_t obu_header_size = 1;
5421*77c1e3ccSAndroid Build Coastguard Worker   const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
5422*77c1e3ccSAndroid Build Coastguard Worker   const size_t payload_offset = obu_header_size + size_field_size;
5423*77c1e3ccSAndroid Build Coastguard Worker 
5424*77c1e3ccSAndroid Build Coastguard Worker   if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
5425*77c1e3ccSAndroid Build Coastguard Worker   memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
5426*77c1e3ccSAndroid Build Coastguard Worker 
5427*77c1e3ccSAndroid Build Coastguard Worker   if (av1_write_obu_header(&ppi->level_params, &ppi->cpi->frame_header_count,
5428*77c1e3ccSAndroid Build Coastguard Worker                            OBU_SEQUENCE_HEADER,
5429*77c1e3ccSAndroid Build Coastguard Worker                            ppi->seq_params.has_nonzero_operating_point_idc, 0,
5430*77c1e3ccSAndroid Build Coastguard Worker                            &header_buf[0]) != obu_header_size) {
5431*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
5432*77c1e3ccSAndroid Build Coastguard Worker   }
5433*77c1e3ccSAndroid Build Coastguard Worker 
5434*77c1e3ccSAndroid Build Coastguard Worker   size_t coded_size_field_size = 0;
5435*77c1e3ccSAndroid Build Coastguard Worker   if (aom_uleb_encode(sequence_header_size, size_field_size,
5436*77c1e3ccSAndroid Build Coastguard Worker                       &header_buf[obu_header_size],
5437*77c1e3ccSAndroid Build Coastguard Worker                       &coded_size_field_size) != 0) {
5438*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
5439*77c1e3ccSAndroid Build Coastguard Worker   }
5440*77c1e3ccSAndroid Build Coastguard Worker   assert(coded_size_field_size == size_field_size);
5441*77c1e3ccSAndroid Build Coastguard Worker 
5442*77c1e3ccSAndroid Build Coastguard Worker   aom_fixed_buf_t *global_headers =
5443*77c1e3ccSAndroid Build Coastguard Worker       (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
5444*77c1e3ccSAndroid Build Coastguard Worker   if (!global_headers) return NULL;
5445*77c1e3ccSAndroid Build Coastguard Worker 
5446*77c1e3ccSAndroid Build Coastguard Worker   const size_t global_header_buf_size =
5447*77c1e3ccSAndroid Build Coastguard Worker       obu_header_size + size_field_size + sequence_header_size;
5448*77c1e3ccSAndroid Build Coastguard Worker 
5449*77c1e3ccSAndroid Build Coastguard Worker   global_headers->buf = malloc(global_header_buf_size);
5450*77c1e3ccSAndroid Build Coastguard Worker   if (!global_headers->buf) {
5451*77c1e3ccSAndroid Build Coastguard Worker     free(global_headers);
5452*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
5453*77c1e3ccSAndroid Build Coastguard Worker   }
5454*77c1e3ccSAndroid Build Coastguard Worker 
5455*77c1e3ccSAndroid Build Coastguard Worker   memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
5456*77c1e3ccSAndroid Build Coastguard Worker   global_headers->sz = global_header_buf_size;
5457*77c1e3ccSAndroid Build Coastguard Worker   return global_headers;
5458*77c1e3ccSAndroid Build Coastguard Worker }
5459