blob: b44742b960663532d4b2156562ee4f469b616859 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Lester Lu6bc30d62021-12-16 19:13:21 +00002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Lester Lu6bc30d62021-12-16 19:13:21 +00004 * This source code is subject to the terms of the BSD 3-Clause Clear License
5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6 * License was not distributed with this source code in the LICENSE file, you
7 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the
8 * Alliance for Open Media Patent License 1.0 was not distributed with this
9 * source code in the PATENTS file, you can obtain it at
10 * aomedia.org/license/patent-license/.
Yaowu Xuc27fc142016-08-22 16:08:15 -070011 */
12
13#include <limits.h>
Urvang Joshibc82d382019-11-01 17:59:20 -070014#include <float.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070015#include <math.h>
16#include <stdio.h>
Debargha Mukherjeec22e2c52020-07-07 00:06:58 -070017#include <string.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070018
Tom Finegan60e653d2018-05-22 11:34:58 -070019#include "config/aom_config.h"
chiyotsaic814afb2020-08-04 13:12:35 -070020#include "config/aom_dsp_rtcd.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070021
Yaowu Xufa3721d2018-07-30 14:38:49 -070022#if CONFIG_DENOISE
23#include "aom_dsp/grain_table.h"
24#include "aom_dsp/noise_util.h"
25#include "aom_dsp/noise_model.h"
26#endif
27#include "aom_dsp/psnr.h"
28#if CONFIG_INTERNAL_STATS
29#include "aom_dsp/ssim.h"
30#endif
31#include "aom_ports/aom_timer.h"
32#include "aom_ports/mem.h"
33#include "aom_ports/system_state.h"
34#include "aom_scale/aom_scale.h"
David Turner1539bb02019-01-24 15:28:13 +000035#if CONFIG_BITSTREAM_DEBUG
Yaowu Xufa3721d2018-07-30 14:38:49 -070036#include "aom_util/debug_util.h"
David Turner1539bb02019-01-24 15:28:13 +000037#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070038
39#include "av1/common/alloccommon.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070040#include "av1/common/filter.h"
41#include "av1/common/idct.h"
42#include "av1/common/reconinter.h"
43#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070044#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045#include "av1/common/tile_common.h"
46
47#include "av1/encoder/aq_complexity.h"
48#include "av1/encoder/aq_cyclicrefresh.h"
49#include "av1/encoder/aq_variance.h"
50#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070051#include "av1/encoder/context_tree.h"
52#include "av1/encoder/encodeframe.h"
53#include "av1/encoder/encodemv.h"
David Turner056f7cd2019-01-07 17:48:13 +000054#include "av1/encoder/encode_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/encoder/encoder.h"
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +053056#include "av1/encoder/encoder_alloc.h"
Satish Kumar Suman897b7942020-06-11 11:44:29 +053057#include "av1/encoder/encoder_utils.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070058#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070059#include "av1/encoder/ethread.h"
60#include "av1/encoder/firstpass.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070061#include "av1/encoder/hash_motion.h"
chiyotsai73ddf442020-06-01 15:42:23 -070062#include "av1/encoder/intra_mode_search.h"
chiyotsaic666b1f2019-12-20 10:44:58 -080063#include "av1/encoder/mv_prec.h"
David Turner0fa8c492019-02-06 16:38:13 +000064#include "av1/encoder/pass2_strategy.h"
Debargha Mukherjee2d565f42020-06-17 18:07:12 -070065#include "av1/encoder/pickcdef.h"
yixindu368bde52021-06-04 18:30:53 -070066#if CONFIG_CCSO
67#include "av1/encoder/pickccso.h"
68#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070069#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070070#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070071#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070072#include "av1/encoder/ratectrl.h"
Satish Kumar Suman47c06892020-06-10 12:45:25 +053073#include "av1/encoder/rc_utils.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070074#include "av1/encoder/rd.h"
Debargha Mukherjeedf713102018-10-02 12:33:32 -070075#include "av1/encoder/rdopt.h"
Debargha Mukherjeec22e2c52020-07-07 00:06:58 -070076#include "av1/encoder/reconinter_enc.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070077#include "av1/encoder/segmentation.h"
78#include "av1/encoder/speed_features.h"
Debargha Mukherjeec22e2c52020-07-07 00:06:58 -070079#include "av1/encoder/subgop.h"
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +053080#include "av1/encoder/superres_scale.h"
Debargha Mukherjee347c64d2019-05-08 13:53:46 -070081#include "av1/encoder/tpl_model.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070082
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000083#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000084
Debargha Mukherjeea4c6f692021-03-16 19:45:46 -070085#if CONFIG_NEW_INTER_MODES
86#define DEF_MAX_DRL_REFMVS 4
87#endif // CONFIG_NEW_INTER_MODES
88
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080089#if CONFIG_ENTROPY_STATS
90FRAME_COUNTS aggregate_fc;
91#endif // CONFIG_ENTROPY_STATS
92
Yaowu Xuc27fc142016-08-22 16:08:15 -070093// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070094#ifdef OUTPUT_YUV_REC
95FILE *yuv_rec_file;
96#define FILE_NAME_LEN 100
97#endif
98
Yaowu Xuf883b422016-08-30 14:01:10 -070099static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700100 switch (mode) {
101 case NORMAL:
102 *hr = 1;
103 *hs = 1;
104 break;
105 case FOURFIVE:
106 *hr = 4;
107 *hs = 5;
108 break;
109 case THREEFIVE:
110 *hr = 3;
111 *hs = 5;
112 break;
Marco Paniconi4e869372020-06-23 10:22:50 -0700113 case THREEFOUR:
114 *hr = 3;
115 *hs = 4;
116 break;
117 case ONEFOUR:
118 *hr = 1;
119 *hs = 4;
120 break;
121 case ONEEIGHT:
122 *hr = 1;
123 *hs = 8;
124 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 case ONETWO:
126 *hr = 1;
127 *hs = 2;
128 break;
129 default:
130 *hr = 1;
131 *hs = 1;
132 assert(0);
133 break;
134 }
135}
136
Yaowu Xuf883b422016-08-30 14:01:10 -0700137int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
138 int cols) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700139 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
140 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700141 unsigned char *const active_map_8x8 = cpi->active_map.map;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700142 const int mi_rows = mi_params->mi_rows;
143 const int mi_cols = mi_params->mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700144 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
145 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700146 cpi->active_map.update = 1;
147 if (new_map_16x16) {
148 int r, c;
149 for (r = 0; r < mi_rows; ++r) {
150 for (c = 0; c < mi_cols; ++c) {
151 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700152 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700153 ? AM_SEGMENT_ID_ACTIVE
154 : AM_SEGMENT_ID_INACTIVE;
155 }
156 }
157 cpi->active_map.enabled = 1;
158 } else {
159 cpi->active_map.enabled = 0;
160 }
161 return 0;
162 } else {
163 return -1;
164 }
165}
166
Yaowu Xuf883b422016-08-30 14:01:10 -0700167int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
168 int cols) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700169 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
170 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols &&
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171 new_map_16x16) {
Vishesh8c90fff2020-04-13 11:32:56 +0530172 unsigned char *const seg_map_8x8 = cpi->enc_seg.map;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700173 const int mi_rows = mi_params->mi_rows;
174 const int mi_cols = mi_params->mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700175 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
176 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
177
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
179 if (cpi->active_map.enabled) {
180 int r, c;
181 for (r = 0; r < mi_rows; ++r) {
182 for (c = 0; c < mi_cols; ++c) {
183 // Cyclic refresh segments are considered active despite not having
184 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700185 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
187 }
188 }
189 }
190 return 0;
191 } else {
192 return -1;
193 }
194}
195
Yaowu Xuf883b422016-08-30 14:01:10 -0700196void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700197 av1_rtcd();
198 aom_dsp_rtcd();
199 aom_scale_rtcd();
200 av1_init_intra_predictors();
201 av1_init_me_luts();
202 av1_rc_init_minq_luts();
203 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204}
205
Yaowu Xuf883b422016-08-30 14:01:10 -0700206static void update_reference_segmentation_map(AV1_COMP *cpi) {
207 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700208 const CommonModeInfoParams *const mi_params = &cm->mi_params;
209 MB_MODE_INFO **mi_4x4_ptr = mi_params->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +0000210 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700212 for (int row = 0; row < mi_params->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700213 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700214 uint8_t *cache = cache_ptr;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700215 for (int col = 0; col < mi_params->mi_cols; col++, mi_4x4++, cache++)
Yushin Choa7f65922018-04-04 16:06:11 -0700216 cache[0] = mi_4x4[0]->segment_id;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700217 mi_4x4_ptr += mi_params->mi_stride;
218 cache_ptr += mi_params->mi_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 }
220}
221
Yaowu Xuf883b422016-08-30 14:01:10 -0700222void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700224 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225}
226
Hui Suef139e12019-05-20 15:51:22 -0700227double av1_get_compression_ratio(const AV1_COMMON *const cm,
228 size_t encoded_frame_size) {
229 const int upscaled_width = cm->superres_upscaled_width;
230 const int height = cm->height;
231 const int luma_pic_size = upscaled_width * height;
232 const SequenceHeader *const seq_params = &cm->seq_params;
233 const BITSTREAM_PROFILE profile = seq_params->profile;
234 const int pic_size_profile_factor =
235 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
236 encoded_frame_size =
237 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
238 const size_t uncompressed_frame_size =
239 (luma_pic_size * pic_size_profile_factor) >> 3;
240 return uncompressed_frame_size / (double)encoded_frame_size;
241}
242
Vishesh0481a782020-06-11 14:32:46 +0530243static void set_tile_info(AV1_COMMON *const cm,
244 const TileConfig *const tile_cfg) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700245 const CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi54ffae72020-03-23 13:37:10 -0700246 const SequenceHeader *const seq_params = &cm->seq_params;
247 CommonTileParams *const tiles = &cm->tiles;
Dominic Symesf58f1112017-09-25 12:47:40 +0200248 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200249
250 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200251
252 // configure tile columns
Vishesh0481a782020-06-11 14:32:46 +0530253 if (tile_cfg->tile_width_count == 0 || tile_cfg->tile_height_count == 0) {
Urvang Joshi54ffae72020-03-23 13:37:10 -0700254 tiles->uniform_spacing = 1;
Vishesh0481a782020-06-11 14:32:46 +0530255 tiles->log2_cols = AOMMAX(tile_cfg->tile_columns, tiles->min_log2_cols);
Urvang Joshi54ffae72020-03-23 13:37:10 -0700256 tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200257 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700258 int mi_cols =
259 ALIGN_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -0700260 int sb_cols = mi_cols >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200261 int size_sb, j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -0700262 tiles->uniform_spacing = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200263 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -0700264 tiles->col_start_sb[i] = start_sb;
Vishesh0481a782020-06-11 14:32:46 +0530265 size_sb = tile_cfg->tile_widths[j++];
266 if (j >= tile_cfg->tile_width_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -0700267 start_sb += AOMMIN(size_sb, tiles->max_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200268 }
Urvang Joshi54ffae72020-03-23 13:37:10 -0700269 tiles->cols = i;
270 tiles->col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200271 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700272 av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols,
273 tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200274
275 // configure tile rows
Urvang Joshi54ffae72020-03-23 13:37:10 -0700276 if (tiles->uniform_spacing) {
Vishesh0481a782020-06-11 14:32:46 +0530277 tiles->log2_rows = AOMMAX(tile_cfg->tile_rows, tiles->min_log2_rows);
Urvang Joshi54ffae72020-03-23 13:37:10 -0700278 tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200279 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700280 int mi_rows =
281 ALIGN_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -0700282 int sb_rows = mi_rows >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200283 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200284 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -0700285 tiles->row_start_sb[i] = start_sb;
Vishesh0481a782020-06-11 14:32:46 +0530286 size_sb = tile_cfg->tile_heights[j++];
287 if (j >= tile_cfg->tile_height_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -0700288 start_sb += AOMMIN(size_sb, tiles->max_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200289 }
Urvang Joshi54ffae72020-03-23 13:37:10 -0700290 tiles->rows = i;
291 tiles->row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200292 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700293 av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200294}
295
Yaowu Xuf883b422016-08-30 14:01:10 -0700296static void update_frame_size(AV1_COMP *cpi) {
297 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
299
chiyotsaia7091f12019-08-09 16:48:27 -0700300 // We need to reallocate the context buffers here in case we need more mis.
301 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
302 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
303 "Failed to allocate context buffers");
304 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700305 av1_init_mi_buffers(&cm->mi_params);
chiyotsaia7091f12019-08-09 16:48:27 -0700306
Urvang Joshi9543ad72020-04-17 16:59:46 -0700307 av1_init_macroblockd(cm, xd);
chiyotsai426c0662019-08-05 16:15:11 -0700308
Visheshd1317912020-04-07 14:39:44 +0530309 if (!is_stat_generation_stage(cpi))
310 alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
Deepa K Gefb2d242020-07-08 17:43:49 +0530311
312 if (!cpi->seq_params_locked)
Jayasanker Jdee179d2020-07-10 23:47:37 +0530313 set_sb_size(&cm->seq_params, av1_select_sb_size(cpi));
Deepa K Gefb2d242020-07-08 17:43:49 +0530314
Vishesh0481a782020-06-11 14:32:46 +0530315 set_tile_info(cm, &cpi->oxcf.tile_cfg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316}
317
Debargha Mukherjee57498692018-05-11 13:29:31 -0700318static INLINE int does_level_match(int width, int height, double fps,
319 int lvl_width, int lvl_height,
320 double lvl_fps, int lvl_dim_mult) {
321 const int64_t lvl_luma_pels = lvl_width * lvl_height;
322 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
323 const int64_t luma_pels = width * height;
324 const double display_sample_rate = luma_pels * fps;
325 return luma_pels <= lvl_luma_pels &&
326 display_sample_rate <= lvl_display_sample_rate &&
327 width <= lvl_width * lvl_dim_mult &&
328 height <= lvl_height * lvl_dim_mult;
329}
330
Andrey Norkin26495512018-06-20 17:13:11 -0700331static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Vishesh2b8e4792020-06-12 12:11:19 +0530332 int width, int height,
333 double init_framerate) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700334 // TODO(any): This is a placeholder function that only addresses dimensions
335 // and max display sample rates.
336 // Need to add checks for max bit rate, max decoded luma sample rate, header
337 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -0700338 AV1_LEVEL level = SEQ_LEVEL_MAX;
Vishesh2b8e4792020-06-12 12:11:19 +0530339 if (does_level_match(width, height, init_framerate, 512, 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700340 level = SEQ_LEVEL_2_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530341 } else if (does_level_match(width, height, init_framerate, 704, 396, 30.0,
342 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700343 level = SEQ_LEVEL_2_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530344 } else if (does_level_match(width, height, init_framerate, 1088, 612, 30.0,
345 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700346 level = SEQ_LEVEL_3_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530347 } else if (does_level_match(width, height, init_framerate, 1376, 774, 30.0,
348 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700349 level = SEQ_LEVEL_3_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530350 } else if (does_level_match(width, height, init_framerate, 2048, 1152, 30.0,
351 3)) {
Hui Su8427ff22019-03-11 10:14:33 -0700352 level = SEQ_LEVEL_4_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530353 } else if (does_level_match(width, height, init_framerate, 2048, 1152, 60.0,
354 3)) {
Hui Su8427ff22019-03-11 10:14:33 -0700355 level = SEQ_LEVEL_4_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530356 } else if (does_level_match(width, height, init_framerate, 4096, 2176, 30.0,
357 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700358 level = SEQ_LEVEL_5_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530359 } else if (does_level_match(width, height, init_framerate, 4096, 2176, 60.0,
360 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700361 level = SEQ_LEVEL_5_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530362 } else if (does_level_match(width, height, init_framerate, 4096, 2176, 120.0,
363 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700364 level = SEQ_LEVEL_5_2;
Vishesh2b8e4792020-06-12 12:11:19 +0530365 } else if (does_level_match(width, height, init_framerate, 8192, 4352, 30.0,
366 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700367 level = SEQ_LEVEL_6_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530368 } else if (does_level_match(width, height, init_framerate, 8192, 4352, 60.0,
369 2)) {
Todd Nguyenff4c8602019-11-25 13:20:21 -0800370 level = SEQ_LEVEL_6_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530371 } else if (does_level_match(width, height, init_framerate, 8192, 4352, 120.0,
372 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700373 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700374 }
chiyotsaiaa33bbf2019-12-02 12:07:53 -0800375
Urvang Joshi450a9a22020-03-31 16:00:22 -0700376 SequenceHeader *const seq_params = &cm->seq_params;
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700377 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -0700378 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -0700379 // Set the maximum parameters for bitrate and buffer size for this profile,
380 // level, and tier
Urvang Joshi450a9a22020-03-31 16:00:22 -0700381 seq_params->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -0700382 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -0700383 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
384 // check
Urvang Joshi450a9a22020-03-31 16:00:22 -0700385 if (seq_params->op_params[i].bitrate == 0)
Andrey Norkin26495512018-06-20 17:13:11 -0700386 aom_internal_error(
387 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
388 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -0700389 // Buffer size in bits/s is bitrate in bits/s * 1 s
Urvang Joshi450a9a22020-03-31 16:00:22 -0700390 seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700391 }
392}
393
Marco Paniconi2aa13c42020-06-01 11:18:38 -0700394void av1_init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
Lester Lu6bc30d62021-12-16 19:13:21 +0000395 const AV1EncoderConfig *oxcf
396#if CONFIG_SVC_ENCODER
397 ,
398 int use_svc
399#endif // CONFIG_SVC_ENCODER
400) {
Vishesh2b8e4792020-06-12 12:11:19 +0530401 const FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg;
Visheshaa6a1702020-06-30 19:27:22 +0530402 const ToolCfg *const tool_cfg = &oxcf->tool_cfg;
Vishesh2b8e4792020-06-12 12:11:19 +0530403
Vishesh4342d7f2020-06-29 18:49:06 +0530404 seq->still_picture =
Visheshaa6a1702020-06-30 19:27:22 +0530405 (tool_cfg->force_video_mode == 0) && (oxcf->input_cfg.limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700406 seq->reduced_still_picture_hdr = seq->still_picture;
Visheshaa6a1702020-06-30 19:27:22 +0530407 seq->reduced_still_picture_hdr &= !tool_cfg->full_still_picture_hdr;
Fyodor Kyslov2b492d82021-07-11 16:37:35 -0700408 seq->force_screen_content_tools = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700409 seq->force_integer_mv = 2;
Visheshaa6a1702020-06-30 19:27:22 +0530410 seq->order_hint_info.enable_order_hint = tool_cfg->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +0000411 seq->frame_id_numbers_present_flag =
412 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Visheshaa6a1702020-06-30 19:27:22 +0530413 !oxcf->tile_cfg.enable_large_scale_tile &&
Lester Lu6bc30d62021-12-16 19:13:21 +0000414#if CONFIG_SVC_ENCODER
415 !use_svc &&
416#endif // CONFIG_SVC_ENCODER
417 tool_cfg->error_resilient_mode;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700418 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +0000419 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700420 seq->force_screen_content_tools = 2;
421 seq->force_integer_mv = 2;
422 }
David Turnerebf96f42018-11-14 16:57:57 +0000423 seq->order_hint_info.order_hint_bits_minus_1 =
424 seq->order_hint_info.enable_order_hint
425 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
426 : -1;
Lester Lu6bc30d62021-12-16 19:13:21 +0000427#if CONFIG_NEW_REF_SIGNALING
428 seq->explicit_ref_frame_map = oxcf->ref_frm_cfg.explicit_ref_frame_map;
429 seq->max_reference_frames = oxcf->ref_frm_cfg.max_reference_frames;
430#endif // CONFIG_NEW_REF_SIGNALING
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700431
Vishesh2b8e4792020-06-12 12:11:19 +0530432 seq->max_frame_width = frm_dim_cfg->forced_max_frame_width
433 ? frm_dim_cfg->forced_max_frame_width
434 : frm_dim_cfg->width;
435 seq->max_frame_height = frm_dim_cfg->forced_max_frame_height
436 ? frm_dim_cfg->forced_max_frame_height
437 : frm_dim_cfg->height;
David Turner760a2f42018-12-07 15:25:36 +0000438 seq->num_bits_width =
439 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
440 seq->num_bits_height =
441 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
442 assert(seq->num_bits_width <= 16);
443 assert(seq->num_bits_height <= 16);
444
445 seq->frame_id_length = FRAME_ID_LENGTH;
446 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
447
Hui Su93c395b2020-10-05 12:00:20 -0700448#if !CONFIG_REMOVE_DUAL_FILTER
Visheshaa6a1702020-06-30 19:27:22 +0530449 seq->enable_dual_filter = tool_cfg->enable_dual_filter;
Hui Su93c395b2020-10-05 12:00:20 -0700450#endif // !CONFIG_REMOVE_DUAL_FILTER
Visheshaa6a1702020-06-30 19:27:22 +0530451 seq->order_hint_info.enable_ref_frame_mvs = tool_cfg->ref_frame_mvs_present;
David Turnerebf96f42018-11-14 16:57:57 +0000452 seq->order_hint_info.enable_ref_frame_mvs &=
453 seq->order_hint_info.enable_order_hint;
Vishesh0d279aa2020-06-03 15:56:56 +0530454 seq->enable_superres = oxcf->superres_cfg.enable_superres;
Visheshaa6a1702020-06-30 19:27:22 +0530455 seq->enable_cdef = tool_cfg->enable_cdef;
456 seq->enable_restoration = tool_cfg->enable_restoration;
yixindu368bde52021-06-04 18:30:53 -0700457#if CONFIG_CCSO
458 seq->enable_ccso = tool_cfg->enable_ccso;
459#endif
Lester Lu6bc30d62021-12-16 19:13:21 +0000460#if CONFIG_OPTFLOW_REFINEMENT
461 seq->enable_opfl_refine = tool_cfg->enable_opfl_refine;
462#endif // CONFIG_OPTFLOW_REFINEMENT
Vishesh015b4962020-06-15 14:19:55 +0530463 seq->enable_warped_motion = oxcf->motion_mode_cfg.enable_warped_motion;
Visheshaa6a1702020-06-30 19:27:22 +0530464 seq->enable_interintra_compound = tool_cfg->enable_interintra_comp;
Vishesh8db8a202020-06-02 17:09:45 +0530465 seq->enable_masked_compound = oxcf->comp_type_cfg.enable_masked_comp;
Vishesh29649842020-06-01 18:18:36 +0530466 seq->enable_intra_edge_filter = oxcf->intra_mode_cfg.enable_intra_edge_filter;
467 seq->enable_filter_intra = oxcf->intra_mode_cfg.enable_filter_intra;
leolzhao02141602021-02-16 15:06:35 -0800468#if CONFIG_SDP
469 seq->enable_sdp = oxcf->part_cfg.enable_sdp;
470#endif
leolzhao943a08d2021-03-30 15:32:52 -0700471#if CONFIG_MRLS
472 seq->enable_mrls = oxcf->intra_mode_cfg.enable_mrls;
473#endif
SARWER, Mohammed Golam4241ffe2021-06-08 11:06:06 -0700474#if CONFIG_ORIP
475 seq->enable_orip = oxcf->intra_mode_cfg.enable_orip;
476#endif
Madhu Peringassery Krishnanee0d2f02021-06-14 12:08:32 -0700477#if CONFIG_IST
478 seq->enable_ist = oxcf->txfm_cfg.enable_ist;
479#endif
Lester Lu6bc30d62021-12-16 19:13:21 +0000480#if CONFIG_IBP_DC || CONFIG_IBP_DIR
481 seq->enable_ibp = oxcf->intra_mode_cfg.enable_ibp;
482#endif
Vishesh2b8e4792020-06-12 12:11:19 +0530483 set_bitstream_level_tier(seq, cm, frm_dim_cfg->width, frm_dim_cfg->height,
Vishesh4342d7f2020-06-29 18:49:06 +0530484 oxcf->input_cfg.init_framerate);
Adrian Grangec56f6ec2018-05-31 14:19:32 -0700485
486 if (seq->operating_points_cnt_minus_1 == 0) {
487 seq->operating_point_idc[0] = 0;
488 } else {
Marco Paniconi6da983b2019-09-16 20:12:43 -0700489 // Set operating_point_idc[] such that the i=0 point corresponds to the
490 // highest quality operating point (all layers), and subsequent
491 // operarting points (i > 0) are lower quality corresponding to
492 // skip decoding enhancement layers (temporal first).
493 int i = 0;
494 assert(seq->operating_points_cnt_minus_1 ==
495 (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
496 for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
497 for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
498 seq->operating_point_idc[i] =
499 (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
500 ~(~0u << (cm->number_temporal_layers - tl));
501 i++;
502 }
503 }
Adrian Grangec56f6ec2018-05-31 14:19:32 -0700504 }
Ryan Leiccc6ea72021-01-06 11:43:56 -0800505#if CONFIG_EXTQUANT
506 const int is_360p_or_larger =
507 AOMMIN(seq->max_frame_width, seq->max_frame_height) >= 360;
508 const int is_720p_or_larger =
509 AOMMIN(seq->max_frame_width, seq->max_frame_height) >= 720;
510 if (!is_360p_or_larger) {
511 seq->base_y_dc_delta_q = -7;
512 seq->base_uv_dc_delta_q = -6;
513 } else if (!is_720p_or_larger) {
514 seq->base_y_dc_delta_q = -5;
515 seq->base_uv_dc_delta_q = -4;
516 } else {
517 seq->base_y_dc_delta_q = -4;
518 seq->base_uv_dc_delta_q = -3;
519 }
520#endif // CONFIG_EXTQUANT
Lester Lu6bc30d62021-12-16 19:13:21 +0000521#if CONFIG_REF_MV_BANK
522 seq->enable_refmvbank = tool_cfg->enable_refmvbank;
523#endif // CONFIG_REF_MV_BANK
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700524}
525
Yaowu Xuf883b422016-08-30 14:01:10 -0700526static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
527 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700528 SequenceHeader *const seq_params = &cm->seq_params;
Jayasanker J0b1dd292020-04-09 15:47:25 +0530529 ResizePendingParams *resize_pending_params = &cpi->resize_pending_params;
Vishesha9082242020-06-17 18:26:09 +0530530 const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
Vishesh52fdeae2020-06-24 14:37:00 +0530531 const ColorCfg *const color_cfg = &oxcf->color_cfg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700532 cpi->oxcf = *oxcf;
Vishesh4342d7f2020-06-29 18:49:06 +0530533 cpi->framerate = oxcf->input_cfg.init_framerate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534
Urvang Joshi450a9a22020-03-31 16:00:22 -0700535 seq_params->profile = oxcf->profile;
Visheshaa6a1702020-06-30 19:27:22 +0530536 seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700537 seq_params->use_highbitdepth = oxcf->use_highbitdepth;
Vishesh52fdeae2020-06-24 14:37:00 +0530538 seq_params->color_primaries = color_cfg->color_primaries;
539 seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
540 seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
Visheshaa6a1702020-06-30 19:27:22 +0530541 seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
Vishesh073fc962020-07-01 17:39:16 +0530542 seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
Vishesh52fdeae2020-06-24 14:37:00 +0530543 seq_params->color_range = color_cfg->color_range;
Vishesha9082242020-06-17 18:26:09 +0530544 seq_params->timing_info_present = dec_model_cfg->timing_info_present;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700545 seq_params->timing_info.num_units_in_display_tick =
Vishesha9082242020-06-17 18:26:09 +0530546 dec_model_cfg->timing_info.num_units_in_display_tick;
547 seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700548 seq_params->timing_info.equal_picture_interval =
Vishesha9082242020-06-17 18:26:09 +0530549 dec_model_cfg->timing_info.equal_picture_interval;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700550 seq_params->timing_info.num_ticks_per_picture =
Vishesha9082242020-06-17 18:26:09 +0530551 dec_model_cfg->timing_info.num_ticks_per_picture;
Andrey Norkin795ba872018-03-06 13:24:14 -0800552
Urvang Joshi450a9a22020-03-31 16:00:22 -0700553 seq_params->display_model_info_present_flag =
Vishesha9082242020-06-17 18:26:09 +0530554 dec_model_cfg->display_model_info_present_flag;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700555 seq_params->decoder_model_info_present_flag =
Vishesha9082242020-06-17 18:26:09 +0530556 dec_model_cfg->decoder_model_info_present_flag;
557 if (dec_model_cfg->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -0700558 // set the decoder model parameters in schedule mode
Urvang Joshi450a9a22020-03-31 16:00:22 -0700559 seq_params->decoder_model_info.num_units_in_decoding_tick =
Vishesha9082242020-06-17 18:26:09 +0530560 dec_model_cfg->num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -0700561 cm->buffer_removal_time_present = 1;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700562 av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
563 av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
564 } else if (seq_params->timing_info_present &&
565 seq_params->timing_info.equal_picture_interval &&
566 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -0700567 // set the decoder model parameters in resource availability mode
Urvang Joshi450a9a22020-03-31 16:00:22 -0700568 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -0700569 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700570 seq_params->op_params[0].initial_display_delay =
Andrey Norkinc7511de2018-06-22 12:31:06 -0700571 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -0800572 }
Andrey Norkinc7511de2018-06-22 12:31:06 -0700573
Urvang Joshi450a9a22020-03-31 16:00:22 -0700574 if (seq_params->monochrome) {
575 seq_params->subsampling_x = 1;
576 seq_params->subsampling_y = 1;
577 } else if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
578 seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
579 seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
580 seq_params->subsampling_x = 0;
581 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -0700582 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700583 if (seq_params->profile == 0) {
584 seq_params->subsampling_x = 1;
585 seq_params->subsampling_y = 1;
586 } else if (seq_params->profile == 1) {
587 seq_params->subsampling_x = 0;
588 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -0700589 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700590 if (seq_params->bit_depth == AOM_BITS_12) {
Vishesh4342d7f2020-06-29 18:49:06 +0530591 seq_params->subsampling_x = oxcf->input_cfg.chroma_subsampling_x;
592 seq_params->subsampling_y = oxcf->input_cfg.chroma_subsampling_y;
Tom Fineganf8d6a162018-08-21 10:47:55 -0700593 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700594 seq_params->subsampling_x = 1;
595 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -0700596 }
597 }
Tom Finegan02b2a842018-08-24 13:50:00 -0700598 }
599
Vishesh2b8e4792020-06-12 12:11:19 +0530600 cm->width = oxcf->frm_dim_cfg.width;
601 cm->height = oxcf->frm_dim_cfg.height;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700602 set_sb_size(seq_params,
Satish Kumar Suman36adf352020-06-26 11:20:10 +0530603 av1_select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -0700604 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -0700605
Satish Kumar Suman36adf352020-06-26 11:20:10 +0530606 av1_update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607
608 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -0700609 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610
Marco Paniconi882c5ad2019-09-30 21:32:29 -0700611 // Set init SVC parameters.
Lester Lu6bc30d62021-12-16 19:13:21 +0000612#if CONFIG_SVC_ENCODER
Marco Paniconi67142112019-07-24 15:00:31 -0700613 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -0700614 cpi->svc.external_ref_frame_config = 0;
615 cpi->svc.non_reference_frame = 0;
Marco Paniconi57247a82020-04-13 09:19:07 -0700616 cpi->svc.number_spatial_layers = 1;
617 cpi->svc.number_temporal_layers = 1;
Lester Lu6bc30d62021-12-16 19:13:21 +0000618#endif // CONFIG_SVC_ENCODER
Marco Paniconi67142112019-07-24 15:00:31 -0700619 cm->number_spatial_layers = 1;
620 cm->number_temporal_layers = 1;
Marco Paniconi882c5ad2019-09-30 21:32:29 -0700621 cm->spatial_layer_id = 0;
622 cm->temporal_layer_id = 0;
Marco Paniconi67142112019-07-24 15:00:31 -0700623
Yaowu Xuc27fc142016-08-22 16:08:15 -0700624 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -0700625 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626
Lester Lu6bc30d62021-12-16 19:13:21 +0000627 cm->ref_frame_flags = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700628
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700629 // Reset resize pending flags
Jayasanker J0b1dd292020-04-09 15:47:25 +0530630 resize_pending_params->width = 0;
631 resize_pending_params->height = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700632
Vishesh2260a592020-04-06 15:34:51 +0530633 init_buffer_indices(&cpi->force_intpel_info, cm->remapped_ref_idx);
Fyodor Kyslov34b591f2020-06-24 20:13:58 -0700634
635 av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636}
637
Debargha Mukherjee55671702020-08-07 17:31:24 -0700638int aom_strcmp(const char *a, const char *b) {
639 if (a == NULL && b == NULL) return 0;
640 if (a == NULL && b != NULL) return -1;
641 if (a != NULL && b == NULL) return 1;
642 return strcmp(a, b);
643}
644
Debargha Mukherjeea4c6f692021-03-16 19:45:46 -0700645#if CONFIG_NEW_INTER_MODES
646static void set_max_drl_bits(struct AV1_COMP *cpi) {
647 AV1_COMMON *const cm = &cpi->common;
648 // Add logic to choose this in the range [MIN_MAX_DRL_BITS, MAX_MAX_DRL_BITS]
649 if (cpi->oxcf.tool_cfg.max_drl_refmvs == 0) {
650 // TODO(any): Implement an auto mode that potentially adapts the parameter
651 // frame to frame. Currently set at a default value.
652 cm->features.max_drl_bits = DEF_MAX_DRL_REFMVS - 1;
653 } else {
654 cm->features.max_drl_bits = cpi->oxcf.tool_cfg.max_drl_refmvs - 1;
655 }
656 assert(cm->features.max_drl_bits >= MIN_MAX_DRL_BITS &&
657 cm->features.max_drl_bits <= MAX_MAX_DRL_BITS);
658}
659#endif // CONFIG_NEW_INTER_MODES
660
Yaowu Xuf883b422016-08-30 14:01:10 -0700661void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
662 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700663 SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -0700665 MACROBLOCK *const x = &cpi->td.mb;
Vishesh8ac928b2020-04-01 02:36:35 +0530666 AV1LevelParams *const level_params = &cpi->level_params;
Jayasanker J44fdab72020-04-13 20:34:38 +0530667 InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
Lester Lu6bc30d62021-12-16 19:13:21 +0000668#if !CONFIG_NEW_REF_SIGNALING
Jayasanker J24cb9bc2020-04-15 13:43:10 +0530669 RefreshFrameFlagsInfo *const refresh_frame_flags = &cpi->refresh_frame;
Lester Lu6bc30d62021-12-16 19:13:21 +0000670#endif // !CONFIG_NEW_REF_SIGNALING
Vishesh2b8e4792020-06-12 12:11:19 +0530671 const FrameDimensionCfg *const frm_dim_cfg = &cpi->oxcf.frm_dim_cfg;
Vishesha9082242020-06-17 18:26:09 +0530672 const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
Vishesh52fdeae2020-06-24 14:37:00 +0530673 const ColorCfg *const color_cfg = &oxcf->color_cfg;
Vishesh073fc962020-07-01 17:39:16 +0530674 const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
Mufaddal Chakera48e16492020-05-04 16:07:18 +0530675 // in case of LAP, lag in frames is set according to number of lap buffers
676 // calculated at init time. This stores and restores LAP's lag in frames to
677 // prevent override by new cfg.
678 int lap_lag_in_frames = -1;
679 if (cpi->lap_enabled && cpi->compressor_stage == LAP_STAGE) {
Vishesh5b50e6d2020-06-10 19:20:07 +0530680 lap_lag_in_frames = cpi->oxcf.gf_cfg.lag_in_frames;
Mufaddal Chakera48e16492020-05-04 16:07:18 +0530681 }
682
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700683 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
Visheshaa6a1702020-06-30 19:27:22 +0530684 seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
Vishesh52fdeae2020-06-24 14:37:00 +0530685 seq_params->color_primaries = color_cfg->color_primaries;
686 seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
687 seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
Visheshaa6a1702020-06-30 19:27:22 +0530688 seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
Vishesh073fc962020-07-01 17:39:16 +0530689 seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
Vishesh52fdeae2020-06-24 14:37:00 +0530690 seq_params->color_range = color_cfg->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700691
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700692 assert(IMPLIES(seq_params->profile <= PROFILE_1,
693 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694
Vishesha9082242020-06-17 18:26:09 +0530695 seq_params->timing_info_present = dec_model_cfg->timing_info_present;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700696 seq_params->timing_info.num_units_in_display_tick =
Vishesha9082242020-06-17 18:26:09 +0530697 dec_model_cfg->timing_info.num_units_in_display_tick;
698 seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700699 seq_params->timing_info.equal_picture_interval =
Vishesha9082242020-06-17 18:26:09 +0530700 dec_model_cfg->timing_info.equal_picture_interval;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700701 seq_params->timing_info.num_ticks_per_picture =
Vishesha9082242020-06-17 18:26:09 +0530702 dec_model_cfg->timing_info.num_ticks_per_picture;
Andrey Norkin795ba872018-03-06 13:24:14 -0800703
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700704 seq_params->display_model_info_present_flag =
Vishesha9082242020-06-17 18:26:09 +0530705 dec_model_cfg->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700706 seq_params->decoder_model_info_present_flag =
Vishesha9082242020-06-17 18:26:09 +0530707 dec_model_cfg->decoder_model_info_present_flag;
708 if (dec_model_cfg->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -0700709 // set the decoder model parameters in schedule mode
Urvang Joshi450a9a22020-03-31 16:00:22 -0700710 seq_params->decoder_model_info.num_units_in_decoding_tick =
Vishesha9082242020-06-17 18:26:09 +0530711 dec_model_cfg->num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -0700712 cm->buffer_removal_time_present = 1;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700713 av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
714 av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
715 } else if (seq_params->timing_info_present &&
716 seq_params->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700717 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -0700718 // set the decoder model parameters in resource availability mode
Urvang Joshi450a9a22020-03-31 16:00:22 -0700719 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -0700720 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700721 seq_params->op_params[0].initial_display_delay =
Andrey Norkinc7511de2018-06-22 12:31:06 -0700722 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -0800723 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800724
Satish Kumar Suman36adf352020-06-26 11:20:10 +0530725 av1_update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800726
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727 cpi->oxcf = *oxcf;
Urvang Joshidf265712020-10-05 23:23:09 -0700728 // When user provides superres_mode = AOM_SUPERRES_AUTO, we still initialize
729 // superres mode for current encoding = AOM_SUPERRES_NONE. This is to ensure
730 // that any analysis (e.g. TPL) happening outside the main encoding loop still
731 // happens at full resolution.
732 // This value will later be set appropriately just before main encoding loop.
733 cpi->superres_mode = oxcf->superres_cfg.superres_mode == AOM_SUPERRES_AUTO
734 ? AOM_SUPERRES_NONE
735 : oxcf->superres_cfg.superres_mode; // default
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700736 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -0700737 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738
Vishesh8ac928b2020-04-01 02:36:35 +0530739 memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
740 sizeof(level_params->target_seq_level_idx));
741 level_params->keep_level_stats = 0;
Wan-Teh Changbcacb322019-09-11 14:50:20 -0700742 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Vishesh8ac928b2020-04-01 02:36:35 +0530743 if (level_params->target_seq_level_idx[i] <= SEQ_LEVELS) {
744 level_params->keep_level_stats |= 1u << i;
745 if (!level_params->level_info[i]) {
746 CHECK_MEM_ERROR(cm, level_params->level_info[i],
747 aom_calloc(1, sizeof(*level_params->level_info[i])));
Wan-Teh Changbcacb322019-09-11 14:50:20 -0700748 }
749 }
750 }
751
752 // TODO(huisu@): level targeting currently only works for the 0th operating
753 // point, so scalable coding is not supported yet.
Vishesh8ac928b2020-04-01 02:36:35 +0530754 if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) {
Wan-Teh Changbcacb322019-09-11 14:50:20 -0700755 // Adjust encoder config in order to meet target level.
Vishesh8ac928b2020-04-01 02:36:35 +0530756 config_target_level(cpi, level_params->target_seq_level_idx[0],
757 seq_params->tier[0]);
Wan-Teh Changbcacb322019-09-11 14:50:20 -0700758 }
759
Urvang Joshiade2d322020-09-01 13:26:46 -0700760 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700761
Lester Lu6bc30d62021-12-16 19:13:21 +0000762#if !CONFIG_NEW_REF_SIGNALING
Jayasanker J24cb9bc2020-04-15 13:43:10 +0530763 refresh_frame_flags->golden_frame = false;
764 refresh_frame_flags->bwd_ref_frame = false;
Lester Lu6bc30d62021-12-16 19:13:21 +0000765#endif // !CONFIG_NEW_REF_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766
Visheshaa6a1702020-06-30 19:27:22 +0530767 cm->features.refresh_frame_context =
768 (oxcf->tool_cfg.frame_parallel_decoding_mode)
769 ? REFRESH_FRAME_CONTEXT_DISABLED
770 : REFRESH_FRAME_CONTEXT_BACKWARD;
Vishesh0481a782020-06-11 14:32:46 +0530771 if (oxcf->tile_cfg.enable_large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -0700772 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +0100773
Alex Converse74ad0912017-07-18 10:22:58 -0700774 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -0700775 CHECK_MEM_ERROR(cm, x->palette_buffer,
776 aom_memalign(16, sizeof(*x->palette_buffer)));
777 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700778
Hui Su38711e72019-06-11 10:49:47 -0700779 if (x->comp_rd_buffer.pred0 == NULL) {
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +0530780 alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700781 }
782
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700783 if (x->tmp_conv_dst == NULL) {
784 CHECK_MEM_ERROR(
785 cm, x->tmp_conv_dst,
786 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -0700787 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700788 }
789 for (int i = 0; i < 2; ++i) {
chiyotsai2a897eb2020-04-28 19:22:13 -0700790 if (x->tmp_pred_bufs[i] == NULL) {
791 CHECK_MEM_ERROR(cm, x->tmp_pred_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +0800792 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
chiyotsai2a897eb2020-04-28 19:22:13 -0700793 sizeof(*x->tmp_pred_bufs[i])));
794 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_pred_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700795 }
796 }
797
Marco Paniconi177b8c32020-01-26 20:37:29 -0800798 av1_reset_segment_features(cm);
Fyodor Kyslov64aaa092020-01-24 11:44:24 -0800799
Debargha Mukherjeea4c6f692021-03-16 19:45:46 -0700800#if CONFIG_NEW_INTER_MODES
801 // Add logic to choose this in the range [MIN_MAX_DRL_BITS, MAX_MAX_DRL_BITS]
802 set_max_drl_bits(cpi);
803#endif // CONFIG_NEW_INTER_MODES
804
chiyotsaic666b1f2019-12-20 10:44:58 -0800805 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806
Vishesh073fc962020-07-01 17:39:16 +0530807 set_rc_buffer_sizes(rc, rc_cfg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808
809 // Under a configuration change, where maximum_buffer_size may change,
810 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
812 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700813
814 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816
817 // Set absolute upper and lower quality limits
Vishesh073fc962020-07-01 17:39:16 +0530818 rc->worst_quality = rc_cfg->worst_allowed_q;
819 rc->best_quality = rc_cfg->best_allowed_q;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820
Urvang Joshi6237b882020-03-26 15:02:26 -0700821 cm->features.interp_filter =
Vishesh0481a782020-06-11 14:32:46 +0530822 oxcf->tile_cfg.enable_large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Urvang Joshi6237b882020-03-26 15:02:26 -0700823 cm->features.switchable_motion_mode = 1;
Lester Lu6bc30d62021-12-16 19:13:21 +0000824#if CONFIG_OPTFLOW_REFINEMENT
825 cm->features.opfl_refine_type = REFINE_SWITCHABLE;
826#endif // CONFIG_OPTFLOW_REFINEMENT
Yue Chen5380cb52018-02-23 15:33:21 -0800827
Vishesh2b8e4792020-06-12 12:11:19 +0530828 if (frm_dim_cfg->render_width > 0 && frm_dim_cfg->render_height > 0) {
829 cm->render_width = frm_dim_cfg->render_width;
830 cm->render_height = frm_dim_cfg->render_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700831 } else {
Vishesh2b8e4792020-06-12 12:11:19 +0530832 cm->render_width = frm_dim_cfg->width;
833 cm->render_height = frm_dim_cfg->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700834 }
Vishesh2b8e4792020-06-12 12:11:19 +0530835 cm->width = frm_dim_cfg->width;
836 cm->height = frm_dim_cfg->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700837
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700838 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -0800839 // Superblock size should not be updated after the first key frame.
840 if (!cpi->seq_params_locked) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +0530841 set_sb_size(&cm->seq_params, av1_select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -0800842 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
843 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -0800844 }
Dominic Symes917d6c02017-10-11 18:00:52 +0200845
Jayasanker J44fdab72020-04-13 20:34:38 +0530846 if (initial_dimensions->width || sb_size != seq_params->sb_size) {
847 if (cm->width > initial_dimensions->width ||
848 cm->height > initial_dimensions->height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700849 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700850 av1_free_context_buffers(cm);
Yue Cheneb628982019-08-29 15:17:13 -0700851 av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
852 av1_free_sms_tree(&cpi->td);
Mufaddal Chakera6c3b6de2020-04-29 12:27:42 +0530853 av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
854 cpi->td.firstpass_ctx = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -0700855 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856 realloc_segmentation_maps(cpi);
Jayasanker J44fdab72020-04-13 20:34:38 +0530857 initial_dimensions->width = initial_dimensions->height = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700858 }
859 }
860 update_frame_size(cpi);
861
Yaowu Xuc27fc142016-08-22 16:08:15 -0700862 rc->is_src_frame_alt_ref = 0;
863
Vishesh0481a782020-06-11 14:32:46 +0530864 set_tile_info(cm, &cpi->oxcf.tile_cfg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865
Lester Lu6bc30d62021-12-16 19:13:21 +0000866#if CONFIG_SVC_ENCODER
Marco Paniconid8574e32019-08-04 21:30:12 -0700867 if (!cpi->svc.external_ref_frame_config)
Vishesh38c05d72020-04-14 12:19:14 +0530868 cpi->ext_flags.refresh_frame.update_pending = 0;
Lester Lu6bc30d62021-12-16 19:13:21 +0000869#else
870 cpi->ext_flags.refresh_frame.update_pending = 0;
871#endif // CONFIG_SVC_ENCODER
Vishesha195ca32020-04-07 18:46:20 +0530872 cpi->ext_flags.refresh_frame_context_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +0000875
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700876 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -0700877 // This should not be called after the first key frame.
878 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700879 seq_params->operating_points_cnt_minus_1 =
Marco Paniconi6da983b2019-09-16 20:12:43 -0700880 (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
881 ? cm->number_spatial_layers * cm->number_temporal_layers - 1
882 : 0;
Lester Lu6bc30d62021-12-16 19:13:21 +0000883#if CONFIG_SVC_ENCODER
Marco Paniconi2aa13c42020-06-01 11:18:38 -0700884 av1_init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Lester Lu6bc30d62021-12-16 19:13:21 +0000885#else
886 av1_init_seq_coding_tools(&cm->seq_params, cm, oxcf);
887#endif // CONFIG_SVC_ENCODER
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -0700888 }
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700889
Lester Lu6bc30d62021-12-16 19:13:21 +0000890#if CONFIG_SVC_ENCODER
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700891 if (cpi->use_svc)
Vishesh073fc962020-07-01 17:39:16 +0530892 av1_update_layer_context_change_config(cpi, rc_cfg->target_bandwidth);
Lester Lu6bc30d62021-12-16 19:13:21 +0000893#endif // CONFIG_SVC_ENCODER
Mufaddal Chakera48e16492020-05-04 16:07:18 +0530894
895 // restore the value of lag_in_frame for LAP stage.
896 if (lap_lag_in_frames != -1) {
Vishesh5b50e6d2020-06-10 19:20:07 +0530897 cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
Mufaddal Chakera48e16492020-05-04 16:07:18 +0530898 }
Debargha Mukherjeec22e2c52020-07-07 00:06:58 -0700899
Debargha Mukherjee55671702020-08-07 17:31:24 -0700900 bool subgop_config_changed = false;
901 if (aom_strcmp(cpi->subgop_config_path, oxcf->subgop_config_path)) {
902 aom_free(cpi->subgop_config_path);
Cherma Rajan Ac89a2512020-11-20 12:18:20 +0530903 cpi->subgop_config_path = NULL;
Debargha Mukherjee55671702020-08-07 17:31:24 -0700904 if (oxcf->subgop_config_path != NULL) {
905 cpi->subgop_config_path =
906 (char *)aom_malloc((strlen(oxcf->subgop_config_path) + 1) *
907 sizeof(*oxcf->subgop_config_path));
908 strcpy(cpi->subgop_config_path, oxcf->subgop_config_path);
909 }
910 subgop_config_changed = true;
911 }
912 if (aom_strcmp(cpi->subgop_config_str, oxcf->subgop_config_str)) {
Debargha Mukherjeeda898442020-07-10 14:45:20 -0700913 aom_free(cpi->subgop_config_str);
Cherma Rajan Ac89a2512020-11-20 12:18:20 +0530914 cpi->subgop_config_str = NULL;
Debargha Mukherjeeda898442020-07-10 14:45:20 -0700915 if (oxcf->subgop_config_str != NULL) {
916 cpi->subgop_config_str =
917 (char *)aom_malloc((strlen(oxcf->subgop_config_str) + 1) *
918 sizeof(*oxcf->subgop_config_str));
919 strcpy(cpi->subgop_config_str, oxcf->subgop_config_str);
Debargha Mukherjeeda898442020-07-10 14:45:20 -0700920 }
Debargha Mukherjee55671702020-08-07 17:31:24 -0700921 subgop_config_changed = true;
922 }
923 if (subgop_config_changed && cpi->compressor_stage == ENCODE_STAGE) {
924 av1_init_subgop_config_set(&cpi->subgop_config_set);
925 // Parse config file first
926 av1_process_subgop_config_set_fromfile(cpi->subgop_config_path,
927 &cpi->subgop_config_set);
928 // Parse config string next, which may override config file configs
929 // or append to it.
930 av1_process_subgop_config_set(cpi->subgop_config_str,
931 &cpi->subgop_config_set);
Urvang Joshieca7e6b2021-06-29 15:30:16 -0700932 if (cpi->print_per_frame_stats) {
933 printf("Successfully processed %d subgop configs.\n",
934 cpi->subgop_config_set.num_configs);
935 // Print out the configuration. Note the printed configuration
936 // is in fact in the config file format that can be parsed back.
937 av1_print_subgop_config_set(&cpi->subgop_config_set);
938 }
Debargha Mukherjeeda898442020-07-10 14:45:20 -0700939 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700940}
941
Urvang Joshi31a7aa52020-03-31 13:09:13 -0700942static INLINE void init_frame_info(FRAME_INFO *frame_info,
943 const AV1_COMMON *const cm) {
944 const CommonModeInfoParams *const mi_params = &cm->mi_params;
945 const SequenceHeader *const seq_params = &cm->seq_params;
946 frame_info->frame_width = cm->width;
947 frame_info->frame_height = cm->height;
948 frame_info->mi_cols = mi_params->mi_cols;
949 frame_info->mi_rows = mi_params->mi_rows;
950 frame_info->mb_cols = mi_params->mb_cols;
951 frame_info->mb_rows = mi_params->mb_rows;
952 frame_info->num_mbs = mi_params->MBs;
953 frame_info->bit_depth = seq_params->bit_depth;
954 frame_info->subsampling_x = seq_params->subsampling_x;
955 frame_info->subsampling_y = seq_params->subsampling_y;
956}
957
Aasaipriyabd659e42019-12-13 16:12:13 +0530958AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
Mufaddal Chakera5517b282019-12-13 16:39:56 +0530959 FIRSTPASS_STATS *frame_stats_buf,
Akshata Jadhava49be172019-12-18 00:03:53 +0530960 COMPRESSOR_STAGE stage, int num_lap_buffers,
Mufaddal Chakera412efe22020-01-17 14:35:01 +0530961 int lap_lag_in_frames,
Akshata Jadhava49be172019-12-18 00:03:53 +0530962 STATS_BUFFER_CTX *stats_buf_context) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700963 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
964 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700965
966 if (!cm) return NULL;
967
Yaowu Xuf883b422016-08-30 14:01:10 -0700968 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700969
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -0700970 // The jmp_buf is valid only for the duration of the function that calls
971 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
972 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700973 if (setjmp(cm->error.jmp)) {
974 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700975 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700976 return 0;
977 }
978
Ryan Leiccc6ea72021-01-06 11:43:56 -0800979#if DEBUG_EXTQUANT
980 cm->fEncCoeffLog = fopen("EncCoeffLog.txt", "wt");
981#endif
982
Yaowu Xuc27fc142016-08-22 16:08:15 -0700983 cm->error.setjmp = 1;
Mufaddal Chakeraed7b1622020-04-06 12:30:23 +0530984 cpi->lap_enabled = num_lap_buffers > 0;
985 cpi->compressor_stage = stage;
chiyotsaia7091f12019-08-09 16:48:27 -0700986
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700987 CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700988 mi_params->free_mi = enc_free_mi;
989 mi_params->setup_mi = enc_setup_mi;
Debargha Mukherjee7118f422020-06-29 15:49:06 -0700990 mi_params->set_mb_mi = (cpi->compressor_stage == LAP_STAGE)
991 ? stat_stage_set_mb_mi
992 : enc_set_mb_mi;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700993
994 mi_params->mi_alloc_bsize = BLOCK_4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995
Angie Chianga5d96c42016-10-21 16:16:56 -0700996 CHECK_MEM_ERROR(cm, cm->fc,
997 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +0000998 CHECK_MEM_ERROR(
999 cm, cm->default_frame_context,
1000 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07001001 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00001002 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001003
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004 cpi->common.buffer_pool = pool;
1005
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +05301006 init_config(cpi, oxcf);
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05301007 if (cpi->compressor_stage == LAP_STAGE) {
Vishesh5b50e6d2020-06-10 19:20:07 +05301008 cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05301009 }
Mufaddal Chakeraac828682019-12-13 16:31:42 +05301010
Mufaddal Chakera7a090f22020-07-09 15:09:51 +05301011 cpi->frames_left = cpi->oxcf.input_cfg.limit;
1012
Debargha Mukherjee7118f422020-06-29 15:49:06 -07001013 av1_rc_init(&cpi->oxcf, 0, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001014
Aasaipriya196c58a2020-04-23 19:05:45 +05301015 // For two pass and lag_in_frames > 33 in LAP.
1016 cpi->rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_2;
1017 if (cpi->lap_enabled) {
1018 if ((num_lap_buffers <
1019 (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)) &&
1020 num_lap_buffers >= (MAX_GF_LENGTH_LAP + 3)) {
1021 /*
1022 * For lag in frames >= 19 and <33, enable scenecut
1023 * with limited future frame prediction.
1024 */
1025 cpi->rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_1;
1026 } else if (num_lap_buffers < (MAX_GF_LENGTH_LAP + 3)) {
1027 // Disable scenecut when lag_in_frames < 19.
1028 cpi->rc.enable_scenecut_detection = DISABLE_SCENECUT;
1029 }
1030 }
Jingning Han17af7742019-09-17 16:58:03 -07001031 init_frame_info(&cpi->frame_info, cm);
1032
David Turnerd2a592e2018-11-16 14:59:31 +00001033 cm->current_frame.frame_number = 0;
liang zhao7aebbd12020-07-03 22:20:34 -07001034 cm->current_frame.key_frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00001035 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07001036 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001037 cpi->partition_search_skippable_frame = 0;
1038 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00001039 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001040 realloc_segmentation_maps(cpi);
1041
Lester Lu6bc30d62021-12-16 19:13:21 +00001042#if !CONFIG_NEW_REF_SIGNALING
Jayasanker J24cb9bc2020-04-15 13:43:10 +05301043 cpi->refresh_frame.alt_ref_frame = false;
Lester Lu6bc30d62021-12-16 19:13:21 +00001044#endif // !CONFIG_NEW_REF_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07001045
1046 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1047#if CONFIG_INTERNAL_STATS
1048 cpi->b_calculate_blockiness = 1;
1049 cpi->b_calculate_consistency = 1;
1050 cpi->total_inconsistency = 0;
1051 cpi->psnr.worst = 100.0;
1052 cpi->worst_ssim = 100.0;
1053
1054 cpi->count = 0;
1055 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08001056#if CONFIG_SPEED_STATS
1057 cpi->tx_search_count = 0;
1058#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001059
1060 if (cpi->b_calculate_psnr) {
1061 cpi->total_sq_error = 0;
1062 cpi->total_samples = 0;
1063 cpi->tot_recode_hits = 0;
1064 cpi->summed_quality = 0;
1065 cpi->summed_weights = 0;
1066 }
1067
1068 cpi->fastssim.worst = 100.0;
1069 cpi->psnrhvs.worst = 100.0;
1070
1071 if (cpi->b_calculate_blockiness) {
1072 cpi->total_blockiness = 0;
1073 cpi->worst_blockiness = 0.0;
1074 }
1075
1076 if (cpi->b_calculate_consistency) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001077 CHECK_MEM_ERROR(
1078 cm, cpi->ssim_vars,
1079 aom_malloc(sizeof(*cpi->ssim_vars) * 4 * cpi->common.mi_params.mi_rows *
1080 cpi->common.mi_params.mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001081 cpi->worst_consistency = 100.0;
1082 }
1083#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08001084#if CONFIG_ENTROPY_STATS
1085 av1_zero(aggregate_fc);
1086#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001087
Deepa K Gfb89ce02020-04-06 13:34:42 +05301088 cpi->time_stamps.first_ever = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001089
Yaowu Xuc27fc142016-08-22 16:08:15 -07001090#ifdef OUTPUT_YUV_REC
1091 yuv_rec_file = fopen("rec.yuv", "wb");
1092#endif
1093
Mufaddal Chakera5517b282019-12-13 16:39:56 +05301094 assert(MAX_LAP_BUFFERS >= MAX_LAG_BUFFERS);
Akshata Jadhav4be65112019-12-18 00:26:25 +05301095 int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
1096 for (int i = 0; i < size; i++)
Aasaipriyabd659e42019-12-13 16:12:13 +05301097 cpi->twopass.frame_stats_arr[i] = &frame_stats_buf[i];
1098
Akshata Jadhava49be172019-12-18 00:03:53 +05301099 cpi->twopass.stats_buf_ctx = stats_buf_context;
1100 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
1101
Aasaipriyaeb417c12020-04-07 12:08:24 +05301102 if (is_stat_consumption_stage(cpi)) {
Debargha Mukherjee0d0aaed2020-07-17 17:47:06 -07001103 av1_init_single_pass_lap(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001104 }
1105
Remya0cce44c2019-08-16 11:57:24 +05301106 int sb_mi_size = av1_get_sb_mi_size(cm);
1107
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +05301108 alloc_obmc_buffers(&cpi->td.mb.obmc_buffer, cm);
Jingning Hand064cf02017-06-01 10:00:39 -07001109
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05301110 CHECK_MEM_ERROR(
1111 cm, cpi->td.mb.inter_modes_info,
1112 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05301113
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301114 for (int x = 0; x < 2; x++)
1115 for (int y = 0; y < 2; y++)
1116 CHECK_MEM_ERROR(
chiyotsai82f36c92020-04-09 16:18:02 -07001117 cm, cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
1118 (uint32_t *)aom_malloc(
1119 AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
1120 sizeof(*cpi->td.mb.intrabc_hash_info.hash_value_buffer[0][0])));
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301121
chiyotsai82f36c92020-04-09 16:18:02 -07001122 cpi->td.mb.intrabc_hash_info.g_crc_initialized = 0;
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301123
Remya0cce44c2019-08-16 11:57:24 +05301124 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
1125 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
1126
David Turner04b70d82019-01-24 15:39:19 +00001127 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
1128 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001129
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07001130 CHECK_MEM_ERROR(cm, cpi->consec_zero_mv,
1131 aom_calloc((mi_params->mi_rows * mi_params->mi_cols) >> 2,
1132 sizeof(*cpi->consec_zero_mv)));
1133
sdengc23c7f12019-06-11 16:56:50 -07001134 {
1135 const int bsize = BLOCK_16X16;
1136 const int w = mi_size_wide[bsize];
1137 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001138 const int num_cols = (mi_params->mi_cols + w - 1) / w;
1139 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengf46a1062019-08-04 18:43:50 -07001140 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
1141 aom_calloc(num_rows * num_cols,
1142 sizeof(*cpi->tpl_rdmult_scaling_factors)));
1143 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
1144 aom_calloc(num_rows * num_cols,
1145 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
1146 }
1147
1148 {
1149 const int bsize = BLOCK_16X16;
1150 const int w = mi_size_wide[bsize];
1151 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001152 const int num_cols = (mi_params->mi_cols + w - 1) / w;
1153 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengc23c7f12019-06-11 16:56:50 -07001154 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
1155 aom_calloc(num_rows * num_cols,
1156 sizeof(*cpi->ssim_rdmult_scaling_factors)));
1157 }
1158
sdeng01959162019-12-20 10:46:24 -08001159#if CONFIG_TUNE_VMAF
1160 {
1161 const int bsize = BLOCK_64X64;
1162 const int w = mi_size_wide[bsize];
1163 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001164 const int num_cols = (mi_params->mi_cols + w - 1) / w;
1165 const int num_rows = (mi_params->mi_rows + h - 1) / h;
Visheshf18766f2020-04-17 10:22:48 +05301166 CHECK_MEM_ERROR(cm, cpi->vmaf_info.rdmult_scaling_factors,
sdeng01959162019-12-20 10:46:24 -08001167 aom_calloc(num_rows * num_cols,
Visheshf18766f2020-04-17 10:22:48 +05301168 sizeof(*cpi->vmaf_info.rdmult_scaling_factors)));
Sai Deng0cbf0282020-09-17 16:25:52 -07001169 for (int i = 0; i < MAX_ARF_LAYERS; i++) {
1170 cpi->vmaf_info.last_frame_unsharp_amount[i] = -1.0;
1171 cpi->vmaf_info.last_frame_ysse[i] = -1.0;
1172 cpi->vmaf_info.last_frame_vmaf[i] = -1.0;
1173 cpi->vmaf_info.best_unsharp_amount[i] = -1.0;
1174 }
Sai Deng48ebb282020-08-13 15:30:50 -07001175 cpi->vmaf_info.original_qindex = -1;
Sai Deng7275a1a2020-08-31 12:06:09 -07001176
1177#if CONFIG_USE_VMAF_RC
1178 cpi->vmaf_info.vmaf_model = NULL;
1179#endif
sdeng01959162019-12-20 10:46:24 -08001180 }
1181#endif
1182
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05301183 if (!is_stat_generation_stage(cpi)) {
Vishesh39d03622020-03-31 15:18:16 +05301184 setup_tpl_buffers(cm, &cpi->tpl_data);
Jingning Hanf83d6812020-02-27 13:08:19 -08001185 }
Yue Chen7cae98f2018-08-24 10:43:16 -07001186
chiyotsai9c484b32019-03-07 16:01:50 -08001187#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08001188 av1_zero(cpi->partition_stats);
1189#endif
1190
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001191#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1192 cpi->fn_ptr[BT].sdf = SDF; \
1193 cpi->fn_ptr[BT].sdaf = SDAF; \
1194 cpi->fn_ptr[BT].vf = VF; \
1195 cpi->fn_ptr[BT].svf = SVF; \
1196 cpi->fn_ptr[BT].svaf = SVAF; \
1197 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1198 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07001199 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001200
Cheng Chenf78632e2017-10-20 15:30:51 -07001201 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001202 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001203 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
1204 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001205
1206 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001207 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001208 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
1209 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07001210
1211 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001212 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001213 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
1214 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07001215
1216 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001217 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001218 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
1219 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07001220
1221 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001222 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001223 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
1224 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07001225
1226 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001227 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001228 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
1229 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001230
Cheng Chenf78632e2017-10-20 15:30:51 -07001231 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
1232 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001233 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
1234 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07001235
1236 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001237 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001238 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
1239 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07001240
1241 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001242 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001243 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
1244 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07001245
1246 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001247 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001248 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
1249 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001250
1251 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001252 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001253 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
1254 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07001255
1256 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001257 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001258 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
1259 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07001260
1261 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001262 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001263 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
1264 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07001265
1266 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
1267 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001268 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
1269 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07001270
1271 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
1272 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001273 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
1274 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07001275
1276 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
1277 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001278 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
1279 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001280
1281 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001282 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001283 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
1284 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07001285
1286 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001287 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001288 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
1289 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001290
1291 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001292 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001293 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07001294
1295 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001296 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001297 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07001298
1299 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001300 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001301 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07001302
1303 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001304 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001305 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07001306
Yaowu Xuc27fc142016-08-22 16:08:15 -07001307#define OBFP(BT, OSDF, OVF, OSVF) \
1308 cpi->fn_ptr[BT].osdf = OSDF; \
1309 cpi->fn_ptr[BT].ovf = OVF; \
1310 cpi->fn_ptr[BT].osvf = OSVF;
1311
Yaowu Xuf883b422016-08-30 14:01:10 -07001312 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
1313 aom_obmc_sub_pixel_variance128x128)
1314 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
1315 aom_obmc_sub_pixel_variance128x64)
1316 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
1317 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001318 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
1319 aom_obmc_sub_pixel_variance64x64)
1320 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
1321 aom_obmc_sub_pixel_variance64x32)
1322 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
1323 aom_obmc_sub_pixel_variance32x64)
1324 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
1325 aom_obmc_sub_pixel_variance32x32)
1326 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
1327 aom_obmc_sub_pixel_variance32x16)
1328 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
1329 aom_obmc_sub_pixel_variance16x32)
1330 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
1331 aom_obmc_sub_pixel_variance16x16)
1332 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
1333 aom_obmc_sub_pixel_variance16x8)
1334 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
1335 aom_obmc_sub_pixel_variance8x16)
1336 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
1337 aom_obmc_sub_pixel_variance8x8)
1338 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
1339 aom_obmc_sub_pixel_variance4x8)
1340 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
1341 aom_obmc_sub_pixel_variance8x4)
1342 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
1343 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001344 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
1345 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001346 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
1347 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001348 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
1349 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001350 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
1351 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001352 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
1353 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001354 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
1355 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001356
David Barkerf19f35f2017-05-22 16:33:22 +01001357#define MBFP(BT, MCSDF, MCSVF) \
1358 cpi->fn_ptr[BT].msdf = MCSDF; \
1359 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001360
David Barkerf19f35f2017-05-22 16:33:22 +01001361 MBFP(BLOCK_128X128, aom_masked_sad128x128,
1362 aom_masked_sub_pixel_variance128x128)
1363 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
1364 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001365 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
1366 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
1367 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
1368 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
1369 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
1370 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
1371 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
1372 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
1373 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
1374 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
1375 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
1376 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
1377 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001378
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001379 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
1380
1381 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
1382
1383 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
1384
1385 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001386
1387 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
1388
1389 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001390
chiyotsaic814afb2020-08-04 13:12:35 -07001391#define SDSFP(BT, SDSF, SDSX4DF) \
1392 cpi->fn_ptr[BT].sdsf = SDSF; \
1393 cpi->fn_ptr[BT].sdsx4df = SDSX4DF;
1394
1395 SDSFP(BLOCK_128X128, aom_sad_skip_128x128, aom_sad_skip_128x128x4d);
1396 SDSFP(BLOCK_128X64, aom_sad_skip_128x64, aom_sad_skip_128x64x4d);
1397 SDSFP(BLOCK_64X128, aom_sad_skip_64x128, aom_sad_skip_64x128x4d);
1398 SDSFP(BLOCK_64X64, aom_sad_skip_64x64, aom_sad_skip_64x64x4d);
1399 SDSFP(BLOCK_64X32, aom_sad_skip_64x32, aom_sad_skip_64x32x4d);
1400 SDSFP(BLOCK_64X16, aom_sad_skip_64x16, aom_sad_skip_64x16x4d);
1401 SDSFP(BLOCK_32X64, aom_sad_skip_32x64, aom_sad_skip_32x64x4d);
1402 SDSFP(BLOCK_32X32, aom_sad_skip_32x32, aom_sad_skip_32x32x4d);
1403 SDSFP(BLOCK_32X16, aom_sad_skip_32x16, aom_sad_skip_32x16x4d);
1404 SDSFP(BLOCK_32X8, aom_sad_skip_32x8, aom_sad_skip_32x8x4d);
1405
1406 SDSFP(BLOCK_16X64, aom_sad_skip_16x64, aom_sad_skip_16x64x4d);
1407 SDSFP(BLOCK_16X32, aom_sad_skip_16x32, aom_sad_skip_16x32x4d);
1408 SDSFP(BLOCK_16X16, aom_sad_skip_16x16, aom_sad_skip_16x16x4d);
1409 SDSFP(BLOCK_16X8, aom_sad_skip_16x8, aom_sad_skip_16x8x4d);
1410 SDSFP(BLOCK_8X16, aom_sad_skip_8x16, aom_sad_skip_8x16x4d);
1411 SDSFP(BLOCK_8X8, aom_sad_skip_8x8, aom_sad_skip_8x8x4d);
1412 SDSFP(BLOCK_4X16, aom_sad_skip_4x16, aom_sad_skip_4x16x4d);
1413 SDSFP(BLOCK_4X8, aom_sad_skip_4x8, aom_sad_skip_4x8x4d);
chiyotsaic814afb2020-08-04 13:12:35 -07001414 SDSFP(BLOCK_8X32, aom_sad_skip_8x32, aom_sad_skip_8x32x4d);
chiyotsaic814afb2020-08-04 13:12:35 -07001415#undef SDSFP
1416
Yaowu Xuc27fc142016-08-22 16:08:15 -07001417 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001418
Yaowu Xuf883b422016-08-30 14:01:10 -07001419 /* av1_init_quantizer() is first called here. Add check in
1420 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07001421 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07001422 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07001423 */
Ryan Leiccc6ea72021-01-06 11:43:56 -08001424 av1_init_quantizer(&cm->seq_params, &cpi->enc_quant_dequant_params,
1425 &cm->quant_params);
Urvang Joshi17814622020-03-27 17:26:17 -07001426 av1_qm_init(&cm->quant_params, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001427
Yaowu Xuf883b422016-08-30 14:01:10 -07001428 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07001429 cm->superres_scale_denominator = SCALE_NUMERATOR;
Vishesh2b8e4792020-06-12 12:11:19 +05301430 cm->superres_upscaled_width = oxcf->frm_dim_cfg.width;
1431 cm->superres_upscaled_height = oxcf->frm_dim_cfg.height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001432 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07001433
1434 cm->error.setjmp = 0;
1435
1436 return cpi;
1437}
1438
Urvang Joshiee2c8112018-05-04 14:53:15 -07001439#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001440#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
1441
1442#define SNPRINT2(H, T, V) \
1443 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07001444#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001445
Sachin Kumar Garg700af532020-05-15 10:47:41 +05301446// This function will change the state and free the mutex of corresponding
1447// workers and terminate the object. The object can not be re-used unless a call
1448// to reset() is made.
1449static AOM_INLINE void terminate_worker_data(AV1_COMP *cpi) {
1450 MultiThreadInfo *const mt_info = &cpi->mt_info;
1451 for (int t = mt_info->num_workers - 1; t >= 0; --t) {
1452 AVxWorker *const worker = &mt_info->workers[t];
1453 aom_get_worker_interface()->end(worker);
1454 }
1455}
1456
1457// Deallocate allocated thread_data.
1458static AOM_INLINE void free_thread_data(AV1_COMP *cpi) {
1459 MultiThreadInfo *const mt_info = &cpi->mt_info;
1460 AV1_COMMON *cm = &cpi->common;
1461 for (int t = 0; t < mt_info->num_workers; ++t) {
1462 EncWorkerData *const thread_data = &mt_info->tile_thr_data[t];
1463 aom_free(thread_data->td->tctx);
1464 if (t == 0) continue;
1465 aom_free(thread_data->td->palette_buffer);
1466 aom_free(thread_data->td->tmp_conv_dst);
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +05301467 release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Sachin Kumar Garg700af532020-05-15 10:47:41 +05301468 for (int j = 0; j < 2; ++j) {
1469 aom_free(thread_data->td->tmp_pred_bufs[j]);
1470 }
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +05301471 release_obmc_buffers(&thread_data->td->obmc_buffer);
Sachin Kumar Garg700af532020-05-15 10:47:41 +05301472 aom_free(thread_data->td->vt64x64);
1473
1474 aom_free(thread_data->td->inter_modes_info);
1475 for (int x = 0; x < 2; x++) {
1476 for (int y = 0; y < 2; y++) {
1477 aom_free(thread_data->td->hash_value_buffer[x][y]);
1478 thread_data->td->hash_value_buffer[x][y] = NULL;
1479 }
1480 }
1481 aom_free(thread_data->td->counts);
1482 aom_free(thread_data->td->mbmi_ext);
1483 av1_free_pmc(thread_data->td->firstpass_ctx, av1_num_planes(cm));
1484 thread_data->td->firstpass_ctx = NULL;
1485 av1_free_shared_coeff_buffer(&thread_data->td->shared_coeff_buf);
1486 av1_free_sms_tree(thread_data->td);
1487 aom_free(thread_data->td);
1488 }
1489}
1490
Yaowu Xuf883b422016-08-30 14:01:10 -07001491void av1_remove_compressor(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001492 if (!cpi) return;
1493
Yunqing Wang82f140a2020-06-03 18:40:50 -07001494 AV1_COMMON *cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00001495 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08001496#if CONFIG_ENTROPY_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05301497 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08001498 fprintf(stderr, "Writing counts.stt\n");
1499 FILE *f = fopen("counts.stt", "wb");
1500 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
1501 fclose(f);
1502 }
1503#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001504#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001505 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07001506
Mufaddal Chakerae7326122019-12-04 14:49:09 +05301507 if (!is_stat_generation_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001508 char headings[512] = { 0 };
1509 char results[512] = { 0 };
1510 FILE *f = fopen("opsnr.stt", "a");
1511 double time_encoded =
Deepa K Gfb89ce02020-04-06 13:34:42 +05301512 (cpi->time_stamps.prev_end_seen - cpi->time_stamps.first_ever) /
Yaowu Xuc27fc142016-08-22 16:08:15 -07001513 10000000.000;
1514 double total_encode_time =
1515 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
1516 const double dr =
1517 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
Vishesh4342d7f2020-06-29 18:49:06 +05301518 const double peak =
1519 (double)((1 << cpi->oxcf.input_cfg.input_bit_depth) - 1);
Vishesh073fc962020-07-01 17:39:16 +05301520 const double target_rate =
1521 (double)cpi->oxcf.rc_cfg.target_bandwidth / 1000;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001522 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
1523
1524 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001525 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001526 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
1527 const double total_ssim =
1528 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
1529 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08001530 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07001531 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08001532 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07001533 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07001534 snprintf(results, sizeof(results),
1535 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
1536 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08001537 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07001538 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07001539 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
1540 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
1541 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
1542 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08001543 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07001544 cpi->psnr.stat[STAT_Y] / cpi->count,
1545 cpi->psnr.stat[STAT_U] / cpi->count,
Yue Chenf0652ed2019-08-13 16:09:25 -07001546 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001547
1548 if (cpi->b_calculate_blockiness) {
1549 SNPRINT(headings, "\t Block\tWstBlck");
1550 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
1551 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
1552 }
1553
1554 if (cpi->b_calculate_consistency) {
1555 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07001556 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001557 (double)cpi->total_inconsistency);
1558
1559 SNPRINT(headings, "\tConsist\tWstCons");
1560 SNPRINT2(results, "\t%7.3f", consistency);
1561 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
1562 }
Yue Chenf0652ed2019-08-13 16:09:25 -07001563
1564 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
1565 SNPRINT2(results, "\t%8.0f", total_encode_time);
1566 SNPRINT2(results, "\t%7.2f", rate_err);
1567 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
1568
1569 fprintf(f, "%s\tAPsnr611\n", headings);
1570 fprintf(f, "%s\t%7.3f\n", results,
1571 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
1572 cpi->psnr.stat[STAT_V]) /
1573 (cpi->count * 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001574 }
1575
1576 fclose(f);
1577 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07001578#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08001579#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05301580 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08001581 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
1582 }
1583#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08001584
chiyotsai9c484b32019-03-07 16:01:50 -08001585#if CONFIG_COLLECT_PARTITION_STATS == 2
Mufaddal Chakerae7326122019-12-04 14:49:09 +05301586 if (!is_stat_generation_stage(cpi)) {
chiyotsai92ed0dd2019-01-25 14:50:14 -08001587 av1_print_partition_stats(&cpi->partition_stats);
1588 }
1589#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001590 }
1591
Yunqing Wang82f140a2020-06-03 18:40:50 -07001592 TplParams *const tpl_data = &cpi->tpl_data;
Jingning Hanf83d6812020-02-27 13:08:19 -08001593 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
Vishesh39d03622020-03-31 15:18:16 +05301594 aom_free(tpl_data->tpl_stats_pool[frame]);
1595 aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]);
Yue Chen7cae98f2018-08-24 10:43:16 -07001596 }
1597
Mufaddal Chakera91579252020-05-18 17:41:46 +05301598 if (cpi->compressor_stage != LAP_STAGE) {
1599 terminate_worker_data(cpi);
1600 free_thread_data(cpi);
1601 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001602
Yunqing Wang82f140a2020-06-03 18:40:50 -07001603 MultiThreadInfo *const mt_info = &cpi->mt_info;
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05301604#if CONFIG_MULTITHREAD
Yunqing Wang82f140a2020-06-03 18:40:50 -07001605 pthread_mutex_t *const enc_row_mt_mutex_ = mt_info->enc_row_mt.mutex_;
1606 pthread_mutex_t *const gm_mt_mutex_ = mt_info->gm_sync.mutex_;
Deepa K G74de2a02020-04-11 13:09:11 +05301607 if (enc_row_mt_mutex_ != NULL) {
1608 pthread_mutex_destroy(enc_row_mt_mutex_);
1609 aom_free(enc_row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05301610 }
Remya1a090d52020-05-04 11:52:10 +05301611 if (gm_mt_mutex_ != NULL) {
1612 pthread_mutex_destroy(gm_mt_mutex_);
1613 aom_free(gm_mt_mutex_);
1614 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05301615#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05301616 av1_row_mt_mem_dealloc(cpi);
Mufaddal Chakera91579252020-05-18 17:41:46 +05301617 if (cpi->compressor_stage != LAP_STAGE) {
1618 aom_free(mt_info->tile_thr_data);
1619 aom_free(mt_info->workers);
1620 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001621
Sachin Kumar Garg194c57b2020-05-06 08:50:13 +05301622 av1_tpl_dealloc(&tpl_data->tpl_mt_sync);
Deepa K G74de2a02020-04-11 13:09:11 +05301623 if (mt_info->num_workers > 1) {
1624 av1_loop_filter_dealloc(&mt_info->lf_row_sync);
1625 av1_loop_restoration_dealloc(&mt_info->lr_row_sync, mt_info->num_workers);
Remya1a090d52020-05-04 11:52:10 +05301626 av1_gm_dealloc(&mt_info->gm_sync);
Deepa K G964e72e2018-05-16 16:56:01 +05301627 }
1628
Yaowu Xuc27fc142016-08-22 16:08:15 -07001629 dealloc_compressor_data(cpi);
Lester Lu6bc30d62021-12-16 19:13:21 +00001630#if CONFIG_IBP_DIR
1631 free_ibp_info(cm->ibp_directional_weights);
1632#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001633
Debargha Mukherjee5d157212017-01-10 14:44:47 -08001634#if CONFIG_INTERNAL_STATS
1635 aom_free(cpi->ssim_vars);
1636 cpi->ssim_vars = NULL;
1637#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001638
Yaowu Xuf883b422016-08-30 14:01:10 -07001639 av1_remove_common(cm);
1640 av1_free_ref_frame_buffers(cm->buffer_pool);
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05301641
Ryan Leiccc6ea72021-01-06 11:43:56 -08001642#if DEBUG_EXTQUANT
1643 if (cpi->common.fEncCoeffLog != NULL) {
1644 fclose(cpi->common.fEncCoeffLog);
1645 }
1646#endif
1647
Debargha Mukherjeeda898442020-07-10 14:45:20 -07001648 aom_free(cpi->subgop_config_str);
Debargha Mukherjee55671702020-08-07 17:31:24 -07001649 aom_free(cpi->subgop_config_path);
Yaowu Xuf883b422016-08-30 14:01:10 -07001650 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001651
Yaowu Xuc27fc142016-08-22 16:08:15 -07001652#ifdef OUTPUT_YUV_REC
1653 fclose(yuv_rec_file);
1654#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001655}
1656
Yaowu Xuf883b422016-08-30 14:01:10 -07001657static void generate_psnr_packet(AV1_COMP *cpi) {
1658 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001659 int i;
1660 PSNR_STATS psnr;
Vishesh4342d7f2020-06-29 18:49:06 +05301661 const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
Yaowu Xue75b58a2020-01-03 12:56:12 -08001662 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
1663 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
1664 bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001665
1666 for (i = 0; i < 4; ++i) {
1667 pkt.data.psnr.samples[i] = psnr.samples[i];
1668 pkt.data.psnr.sse[i] = psnr.sse[i];
1669 pkt.data.psnr.psnr[i] = psnr.psnr[i];
1670 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001671 pkt.kind = AOM_CODEC_PSNR_PKT;
1672 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001673}
1674
Vishesha195ca32020-04-07 18:46:20 +05301675int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001676 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
1677
Vishesha195ca32020-04-07 18:46:20 +05301678 *ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001679 return 0;
1680}
1681
Thomas Daede497d1952017-08-08 17:33:06 -07001682int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1683 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001684 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07001685 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001686 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001687 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001688 return 0;
1689 } else {
1690 return -1;
1691 }
1692}
1693
Thomas Daede497d1952017-08-08 17:33:06 -07001694int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1695 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001696 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07001697 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07001698 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001699 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07001700 return 0;
1701 } else {
1702 return -1;
1703 }
1704}
1705
Yaowu Xuc27fc142016-08-22 16:08:15 -07001706#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07001707void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001708 uint8_t *src = s->y_buffer;
1709 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07001710 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001711 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
1712 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
1713
1714 do {
1715 fwrite(src16, s->y_width, 2, yuv_rec_file);
1716 src16 += s->y_stride;
1717 } while (--h);
1718
1719 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
1720 h = s->uv_height;
1721
1722 do {
1723 fwrite(src16, s->uv_width, 2, yuv_rec_file);
1724 src16 += s->uv_stride;
1725 } while (--h);
1726
1727 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
1728 h = s->uv_height;
1729
1730 do {
1731 fwrite(src16, s->uv_width, 2, yuv_rec_file);
1732 src16 += s->uv_stride;
1733 } while (--h);
1734
1735 fflush(yuv_rec_file);
1736 return;
1737 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001738
1739 do {
1740 fwrite(src, s->y_width, 1, yuv_rec_file);
1741 src += s->y_stride;
1742 } while (--h);
1743
1744 src = s->u_buffer;
1745 h = s->uv_height;
1746
1747 do {
1748 fwrite(src, s->uv_width, 1, yuv_rec_file);
1749 src += s->uv_stride;
1750 } while (--h);
1751
1752 src = s->v_buffer;
1753 h = s->uv_height;
1754
1755 do {
1756 fwrite(src, s->uv_width, 1, yuv_rec_file);
1757 src += s->uv_stride;
1758 } while (--h);
1759
1760 fflush(yuv_rec_file);
1761}
1762#endif // OUTPUT_YUV_REC
1763
Yaowu Xuf883b422016-08-30 14:01:10 -07001764static void set_mv_search_params(AV1_COMP *cpi) {
1765 const AV1_COMMON *const cm = &cpi->common;
Nithya V Sace29f32020-04-07 16:15:12 +05301766 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
Yunqing Wang4b7bf402020-01-28 15:20:53 -08001767 const int max_mv_def = AOMMAX(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001768
1769 // Default based on max resolution.
Nithya V Sace29f32020-04-07 16:15:12 +05301770 mv_search_params->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001771
chiyotsai8cc054a2019-12-12 14:57:43 -08001772 if (cpi->sf.mv_sf.auto_mv_step_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001773 if (frame_is_intra_only(cm)) {
1774 // Initialize max_mv_magnitude for use in the first INTER frame
1775 // after a key/intra-only frame.
Nithya V Sace29f32020-04-07 16:15:12 +05301776 mv_search_params->max_mv_magnitude = max_mv_def;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001777 } else {
Yunqing Wang4b7bf402020-01-28 15:20:53 -08001778 // Use cpi->max_mv_magnitude == -1 to exclude first pass case.
Nithya V Sace29f32020-04-07 16:15:12 +05301779 if (cm->show_frame && mv_search_params->max_mv_magnitude != -1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001780 // Allow mv_steps to correspond to twice the max mv magnitude found
1781 // in the previous frame, capped by the default max_mv_magnitude based
1782 // on resolution.
Nithya V Sace29f32020-04-07 16:15:12 +05301783 mv_search_params->mv_step_param = av1_init_search_range(
1784 AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001785 }
Nithya V Sace29f32020-04-07 16:15:12 +05301786 mv_search_params->max_mv_magnitude = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001787 }
1788 }
1789}
1790
Yaowu Xubca61902020-04-08 08:41:47 -07001791void av1_set_screen_content_options(const AV1_COMP *cpi,
1792 FeatureFlags *features) {
Urvang Joshib6409e92020-03-23 11:23:27 -07001793 const AV1_COMMON *const cm = &cpi->common;
Hui Subdf0c992019-02-14 14:52:41 -08001794
1795 if (cm->seq_params.force_screen_content_tools != 2) {
Urvang Joshib6409e92020-03-23 11:23:27 -07001796 features->allow_screen_content_tools = features->allow_intrabc =
Hui Subdf0c992019-02-14 14:52:41 -08001797 cm->seq_params.force_screen_content_tools;
1798 return;
1799 }
1800
Vishesh94a65292020-07-01 15:28:53 +05301801 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
Urvang Joshib6409e92020-03-23 11:23:27 -07001802 features->allow_screen_content_tools = features->allow_intrabc = 1;
Hui Subdf0c992019-02-14 14:52:41 -08001803 return;
1804 }
1805
1806 // Estimate if the source frame is screen content, based on the portion of
1807 // blocks that have few luma colors.
Cheng Chen4f666ca2019-11-18 17:05:03 -08001808 const uint8_t *src = cpi->unfiltered_source->y_buffer;
Hui Subdf0c992019-02-14 14:52:41 -08001809 assert(src != NULL);
Cheng Chen4f666ca2019-11-18 17:05:03 -08001810 const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
1811 const int stride = cpi->unfiltered_source->y_stride;
1812 const int width = cpi->unfiltered_source->y_width;
1813 const int height = cpi->unfiltered_source->y_height;
Hui Subdf0c992019-02-14 14:52:41 -08001814 const int bd = cm->seq_params.bit_depth;
1815 const int blk_w = 16;
1816 const int blk_h = 16;
1817 // These threshold values are selected experimentally.
1818 const int color_thresh = 4;
1819 const unsigned int var_thresh = 0;
1820 // Counts of blocks with no more than color_thresh colors.
1821 int counts_1 = 0;
1822 // Counts of blocks with no more than color_thresh colors and variance larger
1823 // than var_thresh.
1824 int counts_2 = 0;
1825
1826 for (int r = 0; r + blk_h <= height; r += blk_h) {
1827 for (int c = 0; c + blk_w <= width; c += blk_w) {
Aasaipriya Chandranfa3ecee2020-09-22 12:46:17 +05301828 int count_buf[1 << 8]; // Maximum (1 << 8) bins for hbd path.
Hui Subdf0c992019-02-14 14:52:41 -08001829 const uint8_t *const this_src = src + r * stride + c;
Aasaipriya chandran7c962c22020-10-08 12:37:42 +05301830 int n_colors;
1831 if (use_hbd)
1832 av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd, NULL,
1833 count_buf, &n_colors, NULL);
1834 else
1835 av1_count_colors(this_src, stride, blk_w, blk_h, count_buf, &n_colors);
Hui Subdf0c992019-02-14 14:52:41 -08001836 if (n_colors > 1 && n_colors <= color_thresh) {
1837 ++counts_1;
1838 struct buf_2d buf;
1839 buf.stride = stride;
1840 buf.buf = (uint8_t *)this_src;
1841 const unsigned int var =
1842 use_hbd
1843 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
1844 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
1845 if (var > var_thresh) ++counts_2;
1846 }
1847 }
1848 }
1849
Debargha Mukherjee0a2f6d12021-07-13 15:50:15 -07001850 const int col_factor = 11;
1851 const int var_factor = 12;
1852
Hui Subdf0c992019-02-14 14:52:41 -08001853 // The threshold values are selected experimentally.
Urvang Joshib6409e92020-03-23 11:23:27 -07001854 features->allow_screen_content_tools =
Debargha Mukherjee0a2f6d12021-07-13 15:50:15 -07001855 counts_1 * blk_h * blk_w * col_factor > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08001856 // IntraBC would force loop filters off, so we use more strict rules that also
1857 // requires that the block has high variance.
Debargha Mukherjee0a2f6d12021-07-13 15:50:15 -07001858 features->allow_intrabc =
1859 features->allow_screen_content_tools &&
1860 counts_2 * blk_h * blk_w * var_factor > width * height;
1861 /*
1862 printf("allow_screen_content_tools = %d, allow_intrabc = %d\n",
1863 features->allow_screen_content_tools, features->allow_intrabc);
1864 printf("c1 %d > %f; c1 %d > %f\n", counts_1,
1865 width * height / ((float)(blk_h * blk_w * col_factor)), counts_2,
1866 width * height / ((float)(blk_h * blk_w * var_factor)));
1867 */
Hui Subdf0c992019-02-14 14:52:41 -08001868}
1869
venkat sanampudi14affd02020-07-23 07:41:05 +05301870// Function pointer to search site config initialization
1871// of different search method functions.
1872typedef void (*av1_init_search_site_config)(search_site_config *cfg,
1873 int stride);
1874
1875av1_init_search_site_config
1876 av1_init_motion_compensation[NUM_DISTINCT_SEARCH_METHODS] = {
1877 av1_init_dsmotion_compensation, av1_init_motion_compensation_nstep,
1878 av1_init_motion_compensation_hex, av1_init_motion_compensation_bigdia,
1879 av1_init_motion_compensation_square
1880 };
1881
Yaowu Xuf883b422016-08-30 14:01:10 -07001882static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08001883 AV1_COMMON *const cm = &cpi->common;
Nithya V Sace29f32020-04-07 16:15:12 +05301884 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
chiyotsai836b69b2019-04-09 13:41:24 -07001885 const int y_stride = cpi->scaled_source.y_stride;
Vishesh2b8e4792020-06-12 12:11:19 +05301886 const int y_stride_src = ((cpi->oxcf.frm_dim_cfg.width != cm->width ||
1887 cpi->oxcf.frm_dim_cfg.height != cm->height) ||
1888 av1_superres_scaled(cm))
1889 ? y_stride
1890 : cpi->lookahead->buf->img.y_stride;
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05301891 int fpf_y_stride = cm->cur_frame != NULL ? cm->cur_frame->buf.y_stride
1892 : cpi->scaled_source.y_stride;
1893
Cheng Chencaf33512020-04-21 14:54:57 -07001894 // Update if search_site_cfg is uninitialized or the current frame has a new
1895 // stride
Nithya V Sace29f32020-04-07 16:15:12 +05301896 const int should_update =
venkat sanampudi14affd02020-07-23 07:41:05 +05301897 !mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride ||
1898 !mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][DIAMOND].stride ||
1899 (y_stride !=
1900 mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001901
chiyotsai836b69b2019-04-09 13:41:24 -07001902 if (!should_update) {
1903 return;
1904 }
1905
venkat sanampudi14affd02020-07-23 07:41:05 +05301906 // Initialization of search_site_cfg for NUM_DISTINCT_SEARCH_METHODS.
1907 for (SEARCH_METHODS i = DIAMOND; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
1908 av1_init_motion_compensation[i](
1909 &mv_search_params->search_site_cfg[SS_CFG_SRC][i], y_stride);
1910 av1_init_motion_compensation[i](
1911 &mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][i], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001912 }
venkat sanampudidc5371c2020-07-10 09:36:01 +05301913
venkat sanampudi14affd02020-07-23 07:41:05 +05301914 // First pass search site config initialization.
1915 av1_init_motion_fpf(&mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
Cheng Chencaf33512020-04-21 14:54:57 -07001916 fpf_y_stride);
venkat sanampudi14affd02020-07-23 07:41:05 +05301917 for (SEARCH_METHODS i = NSTEP; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
1918 memcpy(&mv_search_params->search_site_cfg[SS_CFG_FPF][i],
1919 &mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
1920 sizeof(search_site_config));
venkat sanampudidc5371c2020-07-10 09:36:01 +05301921 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001922}
1923
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07001924#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001925static void set_restoration_unit_size(int width, int height, int sx, int sy,
1926 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001927 (void)width;
1928 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07001929 (void)sx;
1930 (void)sy;
1931#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
1932 int s = AOMMIN(sx, sy);
1933#else
1934 int s = 0;
1935#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
1936
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07001937 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08001938 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07001939 else
Urvang Joshi813186b2018-03-08 15:38:46 -08001940 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001941 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
1942 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001943}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001944
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301945static void init_ref_frame_bufs(AV1_COMP *cpi) {
1946 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07001947 int i;
1948 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00001949 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07001950 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00001951 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07001952 }
1953 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07001954 pool->frame_bufs[i].ref_count = 0;
1955 }
Cheng Chen46f30c72017-09-07 11:13:33 -07001956}
1957
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05301958void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
1959 int subsampling_x, int subsampling_y) {
Cheng Chen46f30c72017-09-07 11:13:33 -07001960 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001961 SequenceHeader *const seq_params = &cm->seq_params;
Jayasanker J44fdab72020-04-13 20:34:38 +05301962 InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
Cheng Chen46f30c72017-09-07 11:13:33 -07001963
Jayasanker J44fdab72020-04-13 20:34:38 +05301964 if (!initial_dimensions->width ||
1965 seq_params->use_highbitdepth != use_highbitdepth ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001966 seq_params->subsampling_x != subsampling_x ||
1967 seq_params->subsampling_y != subsampling_y) {
1968 seq_params->subsampling_x = subsampling_x;
1969 seq_params->subsampling_y = subsampling_y;
1970 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07001971
Jingning Han183b2a82019-12-18 16:03:41 -08001972 av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
1973 av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
1974
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05301975 if (!is_stat_generation_stage(cpi)) {
1976 alloc_altref_frame_buffer(cpi);
1977 alloc_util_frame_buffers(cpi);
1978 }
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301979 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07001980
1981 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
1982
Jayasanker J44fdab72020-04-13 20:34:38 +05301983 initial_dimensions->width = cm->width;
1984 initial_dimensions->height = cm->height;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001985 cpi->initial_mbs = cm->mi_params.MBs;
Cheng Chen46f30c72017-09-07 11:13:33 -07001986 }
1987}
1988
1989// Returns 1 if the assigned width or height was <= 0.
Marco Paniconi63971322019-08-15 21:32:05 -07001990int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Cheng Chen46f30c72017-09-07 11:13:33 -07001991 AV1_COMMON *cm = &cpi->common;
Jayasanker J44fdab72020-04-13 20:34:38 +05301992 InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05301993 av1_check_initial_width(cpi, cm->seq_params.use_highbitdepth,
1994 cm->seq_params.subsampling_x,
1995 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07001996
1997 if (width <= 0 || height <= 0) return 1;
1998
1999 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07002000 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07002001
Jayasanker J44fdab72020-04-13 20:34:38 +05302002 if (initial_dimensions->width && initial_dimensions->height &&
2003 (cm->width > initial_dimensions->width ||
2004 cm->height > initial_dimensions->height)) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07002005 av1_free_context_buffers(cm);
Yue Cheneb628982019-08-29 15:17:13 -07002006 av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
2007 av1_free_sms_tree(&cpi->td);
Mufaddal Chakera6c3b6de2020-04-29 12:27:42 +05302008 av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
2009 cpi->td.firstpass_ctx = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07002010 alloc_compressor_data(cpi);
2011 realloc_segmentation_maps(cpi);
Jayasanker J44fdab72020-04-13 20:34:38 +05302012 initial_dimensions->width = initial_dimensions->height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07002013 }
Cheng Chen46f30c72017-09-07 11:13:33 -07002014 update_frame_size(cpi);
2015
2016 return 0;
2017}
2018
David Turner475a3132019-01-18 15:17:17 +00002019void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07002020 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002021 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002022 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07002023 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002024 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002025
Fergus Simpsonbc189932017-05-16 17:02:39 -07002026 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07002027 // There has been a change in the encoded frame size
Marco Paniconi63971322019-08-15 21:32:05 -07002028 av1_set_size_literal(cpi, width, height);
Urvang Joshic8b52d52018-03-23 13:16:51 -07002029 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Urvang Joshib6409e92020-03-23 11:23:27 -07002030 cm->features.all_lossless =
2031 cm->features.coded_lossless && !av1_superres_scaled(cm);
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07002032
2033 av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002034 }
Yunqing Wang4b7bf402020-01-28 15:20:53 -08002035 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002036
Hamsalekha S37cc1d12019-12-12 19:27:41 +05302037 if (is_stat_consumption_stage(cpi)) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07002038 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002039 }
2040
David Turnere7ebf902018-12-04 14:04:55 +00002041 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002042
Cherma Rajan A71d20db2018-04-27 11:15:32 +05302043 // Allocate above context buffers
Urvang Joshi5c8625a2020-03-30 13:16:37 -07002044 CommonContexts *const above_contexts = &cm->above_contexts;
2045 if (above_contexts->num_planes < av1_num_planes(cm) ||
2046 above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
2047 above_contexts->num_tile_rows < cm->tiles.rows) {
2048 av1_free_above_context_buffers(above_contexts);
2049 if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
2050 cm->mi_params.mi_cols,
2051 av1_num_planes(cm)))
Cherma Rajan A71d20db2018-04-27 11:15:32 +05302052 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
2053 "Failed to allocate context buffers");
2054 }
2055
Yaowu Xuc27fc142016-08-22 16:08:15 -07002056 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002057 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00002058 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
2059 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07002060 cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
2061 NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07002062 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002063 "Failed to allocate frame buffer");
2064
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01002065 const int frame_width = cm->superres_upscaled_width;
2066 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002067 set_restoration_unit_size(frame_width, frame_height,
2068 seq_params->subsampling_x,
2069 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002070 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01002071 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01002072
2073 av1_alloc_restoration_buffers(cm);
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05302074 if (!is_stat_generation_stage(cpi)) alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002075 init_motion_estimation(cpi);
2076
Lester Lu6bc30d62021-12-16 19:13:21 +00002077#if CONFIG_NEW_REF_SIGNALING
2078 for (ref_frame = 0; ref_frame < INTER_REFS_PER_FRAME; ++ref_frame) {
2079#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002080 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Lester Lu6bc30d62021-12-16 19:13:21 +00002081#endif // CONFIG_NEW_REF_SIGNALING
David Turnera21966b2018-12-05 14:48:49 +00002082 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00002083 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00002084 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
2085 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00002086 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08002087 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00002088 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002089 }
2090 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07002091
Hui Su5ebd8702018-01-08 18:09:20 -08002092 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08002093 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002094
Lester Lu6bc30d62021-12-16 19:13:21 +00002095#if CONFIG_NEW_REF_SIGNALING
2096 set_ref_ptrs(cm, xd, 0, 0);
2097#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002098 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
Lester Lu6bc30d62021-12-16 19:13:21 +00002099#endif // CONFIG_NEW_REF_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07002100}
2101
Yue Chen2d8405c2020-06-10 14:42:05 -07002102/*!\brief Select and apply cdef filters and switchable restoration filters
2103 *
2104 * \ingroup high_level_algo
Yue Chen2d8405c2020-06-10 14:42:05 -07002105 */
Yue Chen92271e12019-07-09 16:28:02 -07002106static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
2107 MACROBLOCKD *xd, int use_restoration,
2108 int use_cdef) {
yixindu368bde52021-06-04 18:30:53 -07002109#if CONFIG_CCSO
2110 uint16_t *rec_uv[2];
2111 uint16_t *org_uv[2];
2112 uint16_t *ext_rec_y;
2113 uint8_t *ref_buffer;
2114 const YV12_BUFFER_CONFIG *ref = cpi->source;
2115 int ref_stride;
2116 const int use_ccso = !cm->features.coded_lossless && !cm->tiles.large_scale &&
2117 cm->seq_params.enable_ccso;
2118 const int num_planes = av1_num_planes(cm);
2119 av1_setup_dst_planes(xd->plane, cm->seq_params.sb_size, &cm->cur_frame->buf,
2120 0, 0, 0, num_planes);
2121 const int ccso_stride = xd->plane[0].dst.width;
2122 const int ccso_stride_ext = xd->plane[0].dst.width + (CCSO_PADDING_SIZE << 1);
2123 for (int pli = 0; pli < 2; pli++) {
2124 rec_uv[pli] =
2125 aom_malloc(sizeof(*rec_uv) * xd->plane[0].dst.height * ccso_stride);
2126 org_uv[pli] =
2127 aom_malloc(sizeof(*org_uv) * xd->plane[0].dst.height * ccso_stride);
2128 }
2129 if (use_ccso) {
2130 ext_rec_y =
2131 aom_malloc(sizeof(*ext_rec_y) *
2132 (xd->plane[0].dst.height + (CCSO_PADDING_SIZE << 1)) *
2133 (xd->plane[0].dst.width + (CCSO_PADDING_SIZE << 1)));
2134 for (int pli = 0; pli < 1; pli++) {
2135 const int pic_height = xd->plane[pli].dst.height;
2136 const int pic_width = xd->plane[pli].dst.width;
2137 const int dst_stride = xd->plane[pli].dst.stride;
2138 for (int r = 0; r < pic_height; ++r) {
2139 for (int c = 0; c < pic_width; ++c) {
2140 if (cm->seq_params.use_highbitdepth) {
2141 if (pli == 0)
2142 ext_rec_y[(r + CCSO_PADDING_SIZE) * ccso_stride_ext + c +
2143 CCSO_PADDING_SIZE] =
2144 CONVERT_TO_SHORTPTR(
2145 xd->plane[pli].dst.buf)[r * dst_stride + c];
2146 } else {
2147 if (pli == 0)
2148 ext_rec_y[(r + CCSO_PADDING_SIZE) * ccso_stride_ext + c +
2149 CCSO_PADDING_SIZE] =
2150 xd->plane[pli].dst.buf[r * dst_stride + c];
2151 }
2152 }
2153 }
2154 }
2155 extend_ccso_border(ext_rec_y, CCSO_PADDING_SIZE, xd);
2156 }
2157#endif
2158
Deepa K G74de2a02020-04-11 13:09:11 +05302159 MultiThreadInfo *const mt_info = &cpi->mt_info;
2160 const int num_workers = mt_info->num_workers;
logangwf95c9162019-02-20 12:02:32 -08002161 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00002162 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02002163
logangwf95c9162019-02-20 12:02:32 -08002164 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002165#if CONFIG_COLLECT_COMPONENT_TIMING
2166 start_timing(cpi, cdef_time);
2167#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02002168 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00002169 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
chiyotsaid2f7b412019-12-18 10:16:42 -08002170 cpi->sf.lpf_sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002171
2172 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00002173 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002174#if CONFIG_COLLECT_COMPONENT_TIMING
2175 end_timing(cpi, cdef_time);
2176#endif
logangwf95c9162019-02-20 12:02:32 -08002177 } else {
2178 cm->cdef_info.cdef_bits = 0;
2179 cm->cdef_info.cdef_strengths[0] = 0;
2180 cm->cdef_info.nb_cdef_strengths = 1;
2181 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002182 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002183
yixindu368bde52021-06-04 18:30:53 -07002184#if CONFIG_CCSO
2185 if (use_ccso) {
2186 av1_setup_dst_planes(xd->plane, cm->seq_params.sb_size, &cm->cur_frame->buf,
2187 0, 0, 0, num_planes);
2188 // Reading original and reconstructed chroma samples as input
2189 for (int pli = 1; pli < 3; pli++) {
2190 const int pic_height = xd->plane[pli].dst.height;
2191 const int pic_width = xd->plane[pli].dst.width;
2192 const int dst_stride = xd->plane[pli].dst.stride;
2193 switch (pli) {
2194 case 1:
2195 ref_buffer = ref->u_buffer;
2196 ref_stride = ref->uv_stride;
2197 break;
2198 case 2:
2199 ref_buffer = ref->v_buffer;
2200 ref_stride = ref->uv_stride;
2201 break;
2202 default: ref_stride = 0;
2203 }
2204 for (int r = 0; r < pic_height; ++r) {
2205 for (int c = 0; c < pic_width; ++c) {
2206 if (cm->seq_params.use_highbitdepth) {
2207 rec_uv[pli - 1][r * ccso_stride + c] =
2208 CONVERT_TO_SHORTPTR(xd->plane[pli].dst.buf)[r * dst_stride + c];
2209 org_uv[pli - 1][r * ccso_stride + c] =
2210 CONVERT_TO_SHORTPTR(ref_buffer)[r * ref_stride + c];
2211 } else {
2212 rec_uv[pli - 1][r * ccso_stride + c] =
2213 xd->plane[pli].dst.buf[r * dst_stride + c];
2214 org_uv[pli - 1][r * ccso_stride + c] =
2215 ref_buffer[r * ref_stride + c];
2216 }
2217 }
2218 }
2219 }
2220 ccso_search(cm, xd, cpi->td.mb.rdmult, ext_rec_y, rec_uv, org_uv);
2221 ccso_frame(&cm->cur_frame->buf, cm, xd, ext_rec_y);
2222 aom_free(ext_rec_y);
2223 }
2224 for (int pli = 0; pli < 2; pli++) {
2225 aom_free(rec_uv[pli]);
2226 aom_free(org_uv[pli]);
2227 }
2228#endif
2229
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +05302230 av1_superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002231
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002232#if CONFIG_COLLECT_COMPONENT_TIMING
2233 start_timing(cpi, loop_restoration_time);
2234#endif
yixindu368bde52021-06-04 18:30:53 -07002235
logangwf95c9162019-02-20 12:02:32 -08002236 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00002237 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08002238 av1_pick_filter_restoration(cpi->source, cpi);
2239 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2240 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2241 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Deepa K G74de2a02020-04-11 13:09:11 +05302242 if (num_workers > 1)
2243 av1_loop_restoration_filter_frame_mt(
2244 &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
2245 &mt_info->lr_row_sync, &cpi->lr_ctxt);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05302246 else
David Turnerc29e1a92018-12-06 14:10:14 +00002247 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05302248 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08002249 }
logangwf95c9162019-02-20 12:02:32 -08002250 } else {
2251 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
2252 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
2253 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002254 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002255#if CONFIG_COLLECT_COMPONENT_TIMING
2256 end_timing(cpi, loop_restoration_time);
2257#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07002258}
2259
Yue Chen2d8405c2020-06-10 14:42:05 -07002260/*!\brief Select and apply in-loop deblocking filters, cdef filters, and
2261 * restoration filters
2262 *
2263 * \ingroup high_level_algo
Yue Chen2d8405c2020-06-10 14:42:05 -07002264 */
Yue Chen92271e12019-07-09 16:28:02 -07002265static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Deepa K G74de2a02020-04-11 13:09:11 +05302266 MultiThreadInfo *const mt_info = &cpi->mt_info;
2267 const int num_workers = mt_info->num_workers;
Yue Chen92271e12019-07-09 16:28:02 -07002268 const int num_planes = av1_num_planes(cm);
2269 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2270
Vishesh39e74092020-06-16 17:13:48 +05302271 assert(IMPLIES(is_lossless_requested(&cpi->oxcf.rc_cfg),
Urvang Joshib6409e92020-03-23 11:23:27 -07002272 cm->features.coded_lossless && cm->features.all_lossless));
Yue Chen92271e12019-07-09 16:28:02 -07002273
Ryan Leiefc91662020-10-09 14:36:14 -07002274 const int use_loopfilter = !cm->features.coded_lossless &&
2275 !cm->tiles.large_scale &&
2276 cpi->oxcf.tool_cfg.enable_deblocking;
Urvang Joshib6409e92020-03-23 11:23:27 -07002277 const int use_cdef = cm->seq_params.enable_cdef &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07002278 !cm->features.coded_lossless && !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07002279 const int use_restoration = cm->seq_params.enable_restoration &&
Urvang Joshib6409e92020-03-23 11:23:27 -07002280 !cm->features.all_lossless &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07002281 !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07002282
2283 struct loopfilter *lf = &cm->lf;
2284
2285#if CONFIG_COLLECT_COMPONENT_TIMING
2286 start_timing(cpi, loop_filter_time);
2287#endif
2288 if (use_loopfilter) {
2289 aom_clear_system_state();
chiyotsaid2f7b412019-12-18 10:16:42 -08002290 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
Yue Chen92271e12019-07-09 16:28:02 -07002291 } else {
2292 lf->filter_level[0] = 0;
2293 lf->filter_level[1] = 0;
2294 }
2295
2296 if (lf->filter_level[0] || lf->filter_level[1]) {
Deepa K G74de2a02020-04-11 13:09:11 +05302297 if (num_workers > 1)
Yue Chen92271e12019-07-09 16:28:02 -07002298 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
2299#if CONFIG_LPF_MASK
2300 0,
2301#endif
Deepa K G74de2a02020-04-11 13:09:11 +05302302 mt_info->workers, num_workers,
2303 &mt_info->lf_row_sync);
Yue Chen92271e12019-07-09 16:28:02 -07002304 else
2305 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
2306#if CONFIG_LPF_MASK
2307 0,
2308#endif
2309 0, num_planes, 0);
2310 }
2311#if CONFIG_COLLECT_COMPONENT_TIMING
2312 end_timing(cpi, loop_filter_time);
2313#endif
2314
2315 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
2316}
2317
Yue Chen2d8405c2020-06-10 14:42:05 -07002318/*!\brief Encode a frame without the recode loop, usually used in one-pass
Fyodor Kyslovf2a2eee2021-07-12 13:07:54 -07002319 * encoding.
Yue Chen2d8405c2020-06-10 14:42:05 -07002320 *
2321 * \ingroup high_level_algo
Yue Chen2d8405c2020-06-10 14:42:05 -07002322 *
2323 * \param[in] cpi Top-level encoder structure
2324 *
2325 * \return Returns a value to indicate if the encoding is done successfully.
2326 * \retval #AOM_CODEC_OK
2327 * \retval #AOM_CODEC_ERROR
2328 */
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002329static int encode_without_recode(AV1_COMP *cpi) {
2330 AV1_COMMON *const cm = &cpi->common;
Vishesh734eff92020-06-20 21:46:36 +05302331 const QuantizationCfg *const q_cfg = &cpi->oxcf.q_cfg;
Lester Lu6bc30d62021-12-16 19:13:21 +00002332#if CONFIG_SVC_ENCODER
Jerome Jiang790190e2020-06-12 16:47:45 -07002333 SVC *const svc = &cpi->svc;
Lester Lu6bc30d62021-12-16 19:13:21 +00002334#endif // CONFIG_SVC_ENCODER
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07002335 ResizePendingParams *const resize_pending_params =
2336 &cpi->resize_pending_params;
2337 const int resize_pending =
2338 (resize_pending_params->width && resize_pending_params->height &&
2339 (cpi->common.width != resize_pending_params->width ||
2340 cpi->common.height != resize_pending_params->height));
2341
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002342 int top_index = 0, bottom_index = 0, q = 0;
Jerome Jiang790190e2020-06-12 16:47:45 -07002343 YV12_BUFFER_CONFIG *unscaled = cpi->unscaled_source;
Lester Lu6bc30d62021-12-16 19:13:21 +00002344#if CONFIG_SVC_ENCODER
Jerome Jiangdede2932020-06-23 16:49:41 -07002345 InterpFilter filter_scaler =
Jerome Jiang790190e2020-06-12 16:47:45 -07002346 cpi->use_svc ? svc->downsample_filter_type[svc->spatial_layer_id]
Marco Paniconi327528a2020-06-30 21:19:09 -07002347 : EIGHTTAP_SMOOTH;
Jerome Jiang790190e2020-06-12 16:47:45 -07002348 int phase_scaler =
2349 cpi->use_svc ? svc->downsample_filter_phase[svc->spatial_layer_id] : 0;
Lester Lu6bc30d62021-12-16 19:13:21 +00002350#else
2351 InterpFilter filter_scaler = EIGHTTAP_SMOOTH;
2352 int phase_scaler = 0;
2353#endif // CONFIG_SVC_ENCODER
Jerome Jiangdede2932020-06-23 16:49:41 -07002354
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002355 set_size_independent_vars(cpi);
Urvang Joshi007e18e2021-08-28 01:25:40 +00002356 cpi->source->buf_8bit_valid = 0;
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002357 av1_setup_frame_size(cpi);
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302358 av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002359
Lester Lu6bc30d62021-12-16 19:13:21 +00002360#if CONFIG_SVC_ENCODER
2361 if (!cpi->use_svc)
2362#endif // CONFIG_SVC_ENCODER
2363 {
Jerome Jiangdede2932020-06-23 16:49:41 -07002364 phase_scaler = 8;
Marco Paniconi5fffb442020-07-01 10:49:30 -07002365 // 2:1 scaling.
Jerome Jiangc1b11262020-06-25 13:53:55 -07002366 if ((cm->width << 1) == unscaled->y_crop_width &&
Marco Paniconi5fffb442020-07-01 10:49:30 -07002367 (cm->height << 1) == unscaled->y_crop_height) {
Jerome Jiangc1b11262020-06-25 13:53:55 -07002368 filter_scaler = BILINEAR;
Marco Paniconi5fffb442020-07-01 10:49:30 -07002369 // For lower resolutions use eighttap_smooth.
2370 if (cm->width * cm->height <= 320 * 180) filter_scaler = EIGHTTAP_SMOOTH;
2371 } else if ((cm->width << 2) == unscaled->y_crop_width &&
2372 (cm->height << 2) == unscaled->y_crop_height) {
2373 // 4:1 scaling.
Jerome Jiangc1b11262020-06-25 13:53:55 -07002374 filter_scaler = EIGHTTAP_SMOOTH;
Marco Paniconi5fffb442020-07-01 10:49:30 -07002375 } else if ((cm->width << 2) == 3 * unscaled->y_crop_width &&
2376 (cm->height << 2) == 3 * unscaled->y_crop_height) {
2377 // 4:3 scaling.
Jerome Jiang0da8c242020-07-24 14:24:41 -07002378 // TODO(jianj): Neon optimization for 4:3 scaling for EIGHTTAP has issues.
2379 // See aomedia:2766.
2380 filter_scaler = BILINEAR;
Marco Paniconi5fffb442020-07-01 10:49:30 -07002381 }
Jerome Jiangdede2932020-06-23 16:49:41 -07002382 }
2383
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002384 if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi);
2385
2386#if CONFIG_COLLECT_COMPONENT_TIMING
2387 printf("\n Encoding a frame:");
2388#endif
Marco Paniconid13853d2020-05-07 11:42:16 -07002389
2390 aom_clear_system_state();
2391
Jerome Jiang790190e2020-06-12 16:47:45 -07002392 cpi->source = av1_scale_if_required(cm, unscaled, &cpi->scaled_source,
Urvang Joshibd955852020-10-06 16:05:26 -07002393 filter_scaler, phase_scaler, true, false);
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07002394 if (frame_is_intra_only(cm) || resize_pending != 0) {
2395 memset(cpi->consec_zero_mv, 0,
2396 ((cm->mi_params.mi_rows * cm->mi_params.mi_cols) >> 2) *
2397 sizeof(*cpi->consec_zero_mv));
2398 }
2399
Marco Paniconid13853d2020-05-07 11:42:16 -07002400 if (cpi->unscaled_last_source != NULL) {
Urvang Joshibd955852020-10-06 16:05:26 -07002401 cpi->last_source = av1_scale_if_required(
2402 cm, cpi->unscaled_last_source, &cpi->scaled_last_source, filter_scaler,
2403 phase_scaler, true, false);
Marco Paniconid13853d2020-05-07 11:42:16 -07002404 }
Marco Paniconi2b22a1f2020-06-22 21:55:02 -07002405
Lester Lu6bc30d62021-12-16 19:13:21 +00002406#if CONFIG_SVC_ENCODER
Marco Paniconi69e6dcc2020-07-20 14:19:20 -07002407 // For 1 spatial layer encoding: if the (non-LAST) reference has different
2408 // resolution from the source then disable that reference. This is to avoid
2409 // significant increase in encode time from scaling the references in
2410 // av1_scale_references. Note GOLDEN is forced to update on the (first/tigger)
2411 // resized frame and ALTREF will be refreshed ~4 frames later, so both
2412 // references become available again after few frames.
Lester Lu6bc30d62021-12-16 19:13:21 +00002413 if (svc->number_spatial_layers == 1)
2414#endif // CONFIG_SVC_ENCODER
2415 {
2416#if CONFIG_NEW_REF_SIGNALING
2417 const MV_REFERENCE_FRAME golden_frame = get_best_past_ref_index(cm);
2418 const MV_REFERENCE_FRAME altref_frame = get_furthest_future_ref_index(cm);
2419 if (cm->ref_frame_flags & (1 << golden_frame)) {
2420 const YV12_BUFFER_CONFIG *const ref =
2421 get_ref_frame_yv12_buf(cm, golden_frame);
2422 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
2423 cm->ref_frame_flags ^= (1 << golden_frame);
2424 }
2425 if (cm->ref_frame_flags & (1 << altref_frame)) {
2426 const YV12_BUFFER_CONFIG *const ref =
2427 get_ref_frame_yv12_buf(cm, altref_frame);
2428 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
2429 cm->ref_frame_flags ^= (1 << altref_frame);
2430 }
2431#else
2432 if (cm->ref_frame_flags & av1_ref_frame_flag_list[GOLDEN_FRAME]) {
Marco Paniconi69e6dcc2020-07-20 14:19:20 -07002433 const YV12_BUFFER_CONFIG *const ref =
2434 get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
2435 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
Lester Lu6bc30d62021-12-16 19:13:21 +00002436 cm->ref_frame_flags ^= AOM_GOLD_FLAG;
Marco Paniconi69e6dcc2020-07-20 14:19:20 -07002437 }
Lester Lu6bc30d62021-12-16 19:13:21 +00002438 if (cm->ref_frame_flags & av1_ref_frame_flag_list[ALTREF_FRAME]) {
Marco Paniconi69e6dcc2020-07-20 14:19:20 -07002439 const YV12_BUFFER_CONFIG *const ref =
2440 get_ref_frame_yv12_buf(cm, ALTREF_FRAME);
2441 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
Lester Lu6bc30d62021-12-16 19:13:21 +00002442 cm->ref_frame_flags ^= AOM_ALT_FLAG;
Marco Paniconi69e6dcc2020-07-20 14:19:20 -07002443 }
Lester Lu6bc30d62021-12-16 19:13:21 +00002444#endif // CONFIG_NEW_REF_SIGNALING
Marco Paniconi69e6dcc2020-07-20 14:19:20 -07002445 }
2446
Marco Paniconi2b22a1f2020-06-22 21:55:02 -07002447 // For SVC the inter-layer/spatial prediction is not done for newmv
2448 // (zero_mode is forced), and since the scaled references are only
2449 // use for newmv search, we can avoid scaling here.
Lester Lu6bc30d62021-12-16 19:13:21 +00002450 if (!frame_is_intra_only(cm)
2451#if CONFIG_SVC_ENCODER
2452 && !(cpi->use_svc && cpi->svc.force_zero_mode_spatial_ref)
2453#endif // CONFIG_SVC_ENCODER
2454 )
Jerome Jiang3dddc322020-06-29 16:59:58 -07002455 av1_scale_references(cpi, filter_scaler, phase_scaler, 1);
Marco Paniconid13853d2020-05-07 11:42:16 -07002456
Vishesh734eff92020-06-20 21:46:36 +05302457 av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2458 q_cfg->enable_chroma_deltaq);
Marco Paniconid13853d2020-05-07 11:42:16 -07002459 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
Ryan Leiccc6ea72021-01-06 11:43:56 -08002460#if !CONFIG_EXTQUANT
2461 if (q_cfg->deltaq_mode != NO_DELTA_Q || q_cfg->enable_chroma_deltaq)
2462#endif
2463 av1_init_quantizer(&cm->seq_params, &cpi->enc_quant_dequant_params,
2464 &cm->quant_params);
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302465 av1_setup_frame(cpi);
Marco Paniconid13853d2020-05-07 11:42:16 -07002466
Vishesh734eff92020-06-20 21:46:36 +05302467 if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ) {
Marco Paniconid13853d2020-05-07 11:42:16 -07002468 suppress_active_map(cpi);
2469 av1_cyclic_refresh_setup(cpi);
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302470 av1_apply_active_map(cpi);
Marco Paniconid13853d2020-05-07 11:42:16 -07002471 }
2472 if (cm->seg.enabled) {
2473 if (!cm->seg.update_data && cm->prev_frame) {
2474 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
2475 cm->seg.enabled = cm->prev_frame->seg.enabled;
2476 } else {
2477 av1_calculate_segdata(&cm->seg);
2478 }
2479 } else {
2480 memset(&cm->seg, 0, sizeof(cm->seg));
2481 }
2482 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
2483 cm->cur_frame->seg.enabled = cm->seg.enabled;
2484
2485#if CONFIG_COLLECT_COMPONENT_TIMING
2486 start_timing(cpi, av1_encode_frame_time);
2487#endif
2488
2489 // Set the motion vector precision based on mv stats from the last coded
2490 // frame.
2491 if (!frame_is_intra_only(cm)) av1_pick_and_set_high_precision_mv(cpi, q);
2492
2493 // transform / motion compensation build reconstruction frame
2494 av1_encode_frame(cpi);
2495
2496 // Update some stats from cyclic refresh.
Vishesh734eff92020-06-20 21:46:36 +05302497 if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ && !frame_is_intra_only(cm))
Marco Paniconid13853d2020-05-07 11:42:16 -07002498 av1_cyclic_refresh_postencode(cpi);
2499
2500#if CONFIG_COLLECT_COMPONENT_TIMING
2501 end_timing(cpi, av1_encode_frame_time);
2502#endif
2503#if CONFIG_INTERNAL_STATS
2504 ++cpi->tot_recode_hits;
2505#endif
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002506
Marco Paniconie635eb62020-05-14 14:01:02 -07002507 aom_clear_system_state();
2508
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002509 return AOM_CODEC_OK;
Marco Paniconid13853d2020-05-07 11:42:16 -07002510}
2511
Yue Chen2d8405c2020-06-10 14:42:05 -07002512/*!\brief Recode loop for encoding one frame. the purpose of encoding one frame
2513 * for multiple times can be approaching a target bitrate or adjusting the usage
2514 * of global motions.
2515 *
2516 * \ingroup high_level_algo
Yue Chen2d8405c2020-06-10 14:42:05 -07002517 *
2518 * \param[in] cpi Top-level encoder structure
2519 * \param[in] size Bitstream size
2520 * \param[in] dest Bitstream output
2521 *
2522 * \return Returns a value to indicate if the encoding is done successfully.
2523 * \retval #AOM_CODEC_OK
2524 * \retval -1
2525 * \retval #AOM_CODEC_ERROR
2526 */
Tom Finegane4099e32018-01-23 12:01:51 -08002527static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002528 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002529 RATE_CONTROL *const rc = &cpi->rc;
Vishesh8ba8bfe2020-04-03 14:20:29 +05302530 GlobalMotionInfo *const gm_info = &cpi->gm_info;
Vishesh734eff92020-06-20 21:46:36 +05302531 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2532 const QuantizationCfg *const q_cfg = &oxcf->q_cfg;
chiyotsai6b430132019-12-18 10:33:51 -08002533 const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07002534 // Must allow recode if minimum compression ratio is set.
Vishesh734eff92020-06-20 21:46:36 +05302535 assert(IMPLIES(oxcf->rc_cfg.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002536
2537 set_size_independent_vars(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07002538 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07002539
David Turnerdedd8ff2019-01-23 13:59:46 +00002540 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07002541
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +05302542 if (av1_superres_in_recode_allowed(cpi) &&
Urvang Joshi9d2606c2020-04-14 15:58:01 -07002543 cpi->superres_mode != AOM_SUPERRES_NONE &&
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002544 cm->superres_scale_denominator == SCALE_NUMERATOR) {
Urvang Joshidb4551e2020-04-08 12:30:21 -07002545 // Superres mode is currently enabled, but the denominator selected will
Urvang Joshif9c2bd72020-04-07 15:11:51 -07002546 // disable superres. So no need to continue, as we will go through another
2547 // recode loop for full-resolution after this anyway.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002548 return -1;
2549 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002550
David Turner2f3b5df2019-01-02 14:30:50 +00002551 int top_index = 0, bottom_index = 0;
2552 int q = 0, q_low = 0, q_high = 0;
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302553 av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
David Turner2f3b5df2019-01-02 14:30:50 +00002554 q_low = bottom_index;
2555 q_high = top_index;
2556
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002557 if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi);
Cheng Chen92a926a2020-01-30 17:57:33 -08002558
2559#if CONFIG_COLLECT_COMPONENT_TIMING
2560 printf("\n Encoding a frame:");
2561#endif
2562
Marco Paniconid13853d2020-05-07 11:42:16 -07002563 // Determine whether to use screen content tools using two fast encoding.
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302564 av1_determine_sc_tools_with_encoding(cpi, q);
Marco Paniconid13853d2020-05-07 11:42:16 -07002565
Sai Deng7275a1a2020-08-31 12:06:09 -07002566#if CONFIG_USE_VMAF_RC
2567 if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2568 av1_vmaf_neg_preprocessing(cpi, cpi->unscaled_source);
2569 }
2570#endif
2571
David Turner2f3b5df2019-01-02 14:30:50 +00002572 // Loop variables
Cheng Chen92a926a2020-01-30 17:57:33 -08002573 int loop = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00002574 int loop_count = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00002575 int overshoot_seen = 0;
2576 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07002577 int low_cr_seen = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08002578 int last_loop_allow_hp = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002579
Yaowu Xuc27fc142016-08-22 16:08:15 -07002580 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07002581 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002582 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002583
Urvang Joshif1fa6862018-01-08 16:39:33 -08002584 // if frame was scaled calculate global_motion_search again if already
2585 // done
Vishesh8ba8bfe2020-04-03 14:20:29 +05302586 if (loop_count > 0 && cpi->source && gm_info->search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07002587 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00002588 cpi->source->y_crop_height != cm->height) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05302589 gm_info->search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00002590 }
2591 }
Urvang Joshibd955852020-10-06 16:05:26 -07002592 cpi->source =
2593 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source,
2594 EIGHTTAP_REGULAR, 0, false, false);
Jerome Jiangdede2932020-06-23 16:49:41 -07002595
David Turner2f3b5df2019-01-02 14:30:50 +00002596 if (cpi->unscaled_last_source != NULL) {
Urvang Joshibd955852020-10-06 16:05:26 -07002597 cpi->last_source = av1_scale_if_required(
2598 cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
2599 EIGHTTAP_REGULAR, 0, false, false);
David Turner2f3b5df2019-01-02 14:30:50 +00002600 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07002601
David Turner2f3b5df2019-01-02 14:30:50 +00002602 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002603 if (loop_count > 0) {
2604 release_scaled_references(cpi);
2605 }
Jerome Jiang3dddc322020-06-29 16:59:58 -07002606 av1_scale_references(cpi, EIGHTTAP_REGULAR, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002607 }
sdeng3cd9eec2020-01-23 15:49:50 -08002608#if CONFIG_TUNE_VMAF
Sai Deng7275a1a2020-08-31 12:06:09 -07002609 if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
2610 oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
Sai Deng48ebb282020-08-13 15:30:50 -07002611 cpi->vmaf_info.original_qindex = q;
2612 q = av1_get_vmaf_base_qindex(cpi, q);
sdeng3cd9eec2020-01-23 15:49:50 -08002613 }
2614#endif
Sai Deng48ebb282020-08-13 15:30:50 -07002615 av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2616 q_cfg->enable_chroma_deltaq);
Vishesh734eff92020-06-20 21:46:36 +05302617 av1_set_speed_features_qindex_dependent(cpi, oxcf->speed);
sdeng3cd9eec2020-01-23 15:49:50 -08002618
Ryan Leiccc6ea72021-01-06 11:43:56 -08002619#if !CONFIG_EXTQUANT
Madhu Peringassery Krishnan5fa4fcf2021-02-19 15:48:34 -08002620 if (q_cfg->deltaq_mode != NO_DELTA_Q || q_cfg->enable_chroma_deltaq)
Ryan Leiccc6ea72021-01-06 11:43:56 -08002621#endif
2622 av1_init_quantizer(&cm->seq_params, &cpi->enc_quant_dequant_params,
2623 &cm->quant_params);
kyslov7b9d0d62018-12-21 11:12:26 -08002624
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08002625 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
2626 // cm->current_frame.frame_number, cm->show_frame, q,
2627 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002628
David Turner2f3b5df2019-01-02 14:30:50 +00002629 if (loop_count == 0) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302630 av1_setup_frame(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00002631 } else if (get_primary_ref_frame_buf(cm) == NULL) {
2632 // Base q-index may have changed, so we need to assign proper default coef
2633 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07002634 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08002635 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00002636 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002637
Vishesh734eff92020-06-20 21:46:36 +05302638 if (q_cfg->aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002639 av1_vaq_frame_setup(cpi);
Vishesh734eff92020-06-20 21:46:36 +05302640 } else if (q_cfg->aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002641 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002642 }
David Turner2f3b5df2019-01-02 14:30:50 +00002643
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00002644 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00002645 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00002646 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07002647 cm->seg.enabled = cm->prev_frame->seg.enabled;
David Barker11c93562018-06-05 12:00:07 +01002648 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07002649 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07002650 }
David Barkercab37552018-03-21 11:56:24 +00002651 } else {
2652 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00002653 }
David Barkercab37552018-03-21 11:56:24 +00002654 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07002655 cm->cur_frame->seg.enabled = cm->seg.enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002656
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002657#if CONFIG_COLLECT_COMPONENT_TIMING
2658 start_timing(cpi, av1_encode_frame_time);
2659#endif
chiyotsai8b7cef82020-01-21 16:52:54 -08002660 // Set the motion vector precision based on mv stats from the last coded
2661 // frame.
2662 if (!frame_is_intra_only(cm)) {
2663 av1_pick_and_set_high_precision_mv(cpi, q);
2664
2665 // If the precision has changed during different iteration of the loop,
2666 // then we need to reset the global motion vectors
Urvang Joshib6409e92020-03-23 11:23:27 -07002667 if (loop_count > 0 &&
2668 cm->features.allow_high_precision_mv != last_loop_allow_hp) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05302669 gm_info->search_done = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08002670 }
Urvang Joshib6409e92020-03-23 11:23:27 -07002671 last_loop_allow_hp = cm->features.allow_high_precision_mv;
chiyotsai8b7cef82020-01-21 16:52:54 -08002672 }
chiyotsaic666b1f2019-12-20 10:44:58 -08002673
Yaowu Xuc27fc142016-08-22 16:08:15 -07002674 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07002675 av1_encode_frame(cpi);
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002676
chiyotsai51bd6482019-12-20 10:49:34 -08002677 // Reset the mv_stats in case we are interrupted by an intraframe or an
2678 // overlay frame.
2679 if (cpi->mv_stats.valid) {
2680 av1_zero(cpi->mv_stats);
2681 }
2682 // Gather the mv_stats for the next frame
2683 if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
2684 av1_frame_allows_smart_mv(cpi)) {
2685 av1_collect_mv_stats(cpi, q);
2686 }
chiyotsai51bd6482019-12-20 10:49:34 -08002687
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002688#if CONFIG_COLLECT_COMPONENT_TIMING
2689 end_timing(cpi, av1_encode_frame_time);
2690#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002691
Yaowu Xuf883b422016-08-30 14:01:10 -07002692 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002693
2694 // Dummy pack of the bitstream using up to date stats to get an
2695 // accurate estimate of output frame size to determine if we need
2696 // to recode.
chiyotsai6b430132019-12-18 10:33:51 -08002697 const int do_dummy_pack =
2698 (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
Vishesh734eff92020-06-20 21:46:36 +05302699 oxcf->rc_cfg.mode != AOM_Q) ||
2700 oxcf->rc_cfg.min_cr > 0;
Hui Sua1d71842019-07-31 12:02:24 -07002701 if (do_dummy_pack) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302702 av1_finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00002703 int largest_tile_id = 0; // Output from bitstream: unused here
chiyotsaic666b1f2019-12-20 10:44:58 -08002704 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) !=
2705 AOM_CODEC_OK) {
Tom Finegane4099e32018-01-23 12:01:51 -08002706 return AOM_CODEC_ERROR;
chiyotsaic666b1f2019-12-20 10:44:58 -08002707 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002708
2709 rc->projected_frame_size = (int)(*size) << 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002710 }
2711
Sai Deng48ebb282020-08-13 15:30:50 -07002712#if CONFIG_TUNE_VMAF
Sai Deng7275a1a2020-08-31 12:06:09 -07002713 if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
2714 oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
Sai Deng48ebb282020-08-13 15:30:50 -07002715 q = cpi->vmaf_info.original_qindex;
2716 }
2717#endif
Hui Suef139e12019-05-20 15:51:22 -07002718 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00002719 // Update q and decide whether to do a recode loop
2720 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
2721 bottom_index, &undershoot_seen, &overshoot_seen,
Paul Wilkinsc8d3f112020-07-08 17:58:14 +01002722 &low_cr_seen, loop_count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002723 }
2724
2725 // Special case for overlay frame.
Hui Su7e3eeaa2019-09-11 15:50:41 -07002726 if (loop && rc->is_src_frame_alt_ref &&
2727 rc->projected_frame_size < rc->max_frame_bandwidth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002728 loop = 0;
Hui Su7e3eeaa2019-09-11 15:50:41 -07002729 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002730
chiyotsai76cd2652019-12-18 10:26:36 -08002731 if (allow_recode && !cpi->sf.gm_sf.gm_disable_recode &&
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302732 av1_recode_loop_test_global_motion(cm->global_motion,
2733 cpi->td.rd_counts.global_motion_used,
2734 gm_info->params_cost)) {
David Turner2f3b5df2019-01-02 14:30:50 +00002735 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08002736 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08002737
Yaowu Xuc27fc142016-08-22 16:08:15 -07002738 if (loop) {
2739 ++loop_count;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002740
2741#if CONFIG_INTERNAL_STATS
2742 ++cpi->tot_recode_hits;
2743#endif
2744 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002745#if CONFIG_COLLECT_COMPONENT_TIMING
2746 if (loop) printf("\n Recoding:");
2747#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002748 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08002749
2750 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002751}
2752
Yue Chen2d8405c2020-06-10 14:42:05 -07002753/*!\brief Recode loop or a single loop for encoding one frame, followed by
2754 * in-loop deblocking filters, CDEF filters, and restoration filters.
2755 *
2756 * \ingroup high_level_algo
2757 * \callgraph
2758 * \callergraph
2759 *
2760 * \param[in] cpi Top-level encoder structure
2761 * \param[in] size Bitstream size
2762 * \param[in] dest Bitstream output
2763 * \param[in] sse Total distortion of the frame
2764 * \param[in] rate Total rate of the frame
2765 * \param[in] largest_tile_id Tile id of the last tile
2766 *
2767 * \return Returns a value to indicate if the encoding is done successfully.
2768 * \retval #AOM_CODEC_OK
2769 * \retval #AOM_CODEC_ERROR
2770 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002771static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
2772 uint8_t *dest, int64_t *sse,
2773 int64_t *rate,
2774 int *largest_tile_id) {
2775#if CONFIG_COLLECT_COMPONENT_TIMING
2776 start_timing(cpi, encode_with_recode_loop_time);
2777#endif
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002778 int err;
Jingning Han37af76d2020-07-27 23:17:26 -07002779 if (cpi->sf.hl_sf.recode_loop == DISALLOW_RECODE)
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002780 err = encode_without_recode(cpi);
2781 else
2782 err = encode_with_recode_loop(cpi, size, dest);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002783#if CONFIG_COLLECT_COMPONENT_TIMING
2784 end_timing(cpi, encode_with_recode_loop_time);
2785#endif
2786 if (err != AOM_CODEC_OK) {
2787 if (err == -1) {
2788 // special case as described in encode_with_recode_loop().
2789 // Encoding was skipped.
2790 err = AOM_CODEC_OK;
2791 if (sse != NULL) *sse = INT64_MAX;
2792 if (rate != NULL) *rate = INT64_MAX;
2793 *largest_tile_id = 0;
2794 }
2795 return err;
2796 }
2797
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002798 AV1_COMMON *const cm = &cpi->common;
2799 SequenceHeader *const seq_params = &cm->seq_params;
2800
2801 // Special case code to reduce pulsing when key frames are forced at a
2802 // fixed interval. Note the reconstruction error if it is the frame before
2803 // the force key frame
2804 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2805 if (seq_params->use_highbitdepth) {
2806 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
2807 } else {
2808 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2809 }
2810 }
2811
2812 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
2813 cm->cur_frame->buf.transfer_characteristics =
2814 seq_params->transfer_characteristics;
2815 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
2816 cm->cur_frame->buf.monochrome = seq_params->monochrome;
2817 cm->cur_frame->buf.chroma_sample_position =
2818 seq_params->chroma_sample_position;
2819 cm->cur_frame->buf.color_range = seq_params->color_range;
2820 cm->cur_frame->buf.render_width = cm->render_width;
2821 cm->cur_frame->buf.render_height = cm->render_height;
Lester Lu6bc30d62021-12-16 19:13:21 +00002822 cm->cur_frame->buf.bit_depth = (unsigned int)seq_params->bit_depth;
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002823
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002824 // Pick the loop filter level for the frame.
Urvang Joshib6409e92020-03-23 11:23:27 -07002825 if (!cm->features.allow_intrabc) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002826 loopfilter_frame(cpi, cm);
2827 } else {
2828 cm->lf.filter_level[0] = 0;
2829 cm->lf.filter_level[1] = 0;
2830 cm->cdef_info.cdef_bits = 0;
2831 cm->cdef_info.cdef_strengths[0] = 0;
2832 cm->cdef_info.nb_cdef_strengths = 1;
2833 cm->cdef_info.cdef_uv_strengths[0] = 0;
2834 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
2835 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
2836 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
2837 }
2838
2839 // TODO(debargha): Fix mv search range on encoder side
2840 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
2841 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
2842
2843#ifdef OUTPUT_YUV_REC
2844 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
2845#endif
2846
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302847 av1_finalize_encoded_frame(cpi);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002848 // Build the bitstream
2849#if CONFIG_COLLECT_COMPONENT_TIMING
2850 start_timing(cpi, av1_pack_bitstream_final_time);
2851#endif
2852 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
2853 return AOM_CODEC_ERROR;
2854#if CONFIG_COLLECT_COMPONENT_TIMING
2855 end_timing(cpi, av1_pack_bitstream_final_time);
2856#endif
2857
2858 // Compute sse and rate.
2859 if (sse != NULL) {
2860 *sse = (seq_params->use_highbitdepth)
2861 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
2862 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2863 }
2864 if (rate != NULL) {
2865 const int64_t bits = (*size << 3);
2866 *rate = (bits << 5); // To match scale.
2867 }
2868 return AOM_CODEC_OK;
2869}
2870
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002871static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
2872 uint8_t *dest,
2873 int *largest_tile_id) {
2874 const AV1_COMMON *const cm = &cpi->common;
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002875 assert(cm->seq_params.enable_superres);
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +05302876 assert(av1_superres_in_recode_allowed(cpi));
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002877 aom_codec_err_t err = AOM_CODEC_OK;
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302878 av1_save_all_coding_context(cpi);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002879
Urvang Joshibc82d382019-11-01 17:59:20 -07002880 int64_t sse1 = INT64_MAX;
2881 int64_t rate1 = INT64_MAX;
2882 int largest_tile_id1 = 0;
Debargha Mukherjee2f16e852020-10-19 16:57:30 -07002883 int64_t sse2 = INT64_MAX;
2884 int64_t rate2 = INT64_MAX;
2885 int largest_tile_id2;
2886 double proj_rdcost1 = DBL_MAX;
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002887
Debargha Mukherjee2f16e852020-10-19 16:57:30 -07002888 // Encode with superres.
2889 if (cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_ALL) {
2890 SuperResCfg *const superres_cfg = &cpi->oxcf.superres_cfg;
2891 int64_t superres_sses[SCALE_NUMERATOR];
2892 int64_t superres_rates[SCALE_NUMERATOR];
2893 int superres_largest_tile_ids[SCALE_NUMERATOR];
2894 // Use superres for Key-frames and Alt-ref frames only.
2895 const GF_GROUP *const gf_group = &cpi->gf_group;
2896 if (gf_group->update_type[gf_group->index] != OVERLAY_UPDATE &&
2897 gf_group->update_type[gf_group->index] != INTNL_OVERLAY_UPDATE) {
2898 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
2899 ++denom) {
2900 superres_cfg->superres_scale_denominator = denom;
2901 superres_cfg->superres_kf_scale_denominator = denom;
2902 const int this_index = denom - (SCALE_NUMERATOR + 1);
2903
2904 cpi->superres_mode = AOM_SUPERRES_AUTO; // Super-res on for this loop.
2905 err = encode_with_recode_loop_and_filter(
2906 cpi, size, dest, &superres_sses[this_index],
2907 &superres_rates[this_index],
2908 &superres_largest_tile_ids[this_index]);
2909 cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
2910 if (err != AOM_CODEC_OK) return err;
2911 restore_all_coding_context(cpi);
2912 }
2913 // Reset.
2914 superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
2915 superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
2916 } else {
2917 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
2918 ++denom) {
2919 const int this_index = denom - (SCALE_NUMERATOR + 1);
2920 superres_sses[this_index] = INT64_MAX;
2921 superres_rates[this_index] = INT64_MAX;
2922 }
2923 }
2924 // Encode without superres.
2925 assert(cpi->superres_mode == AOM_SUPERRES_NONE);
2926 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
2927 &largest_tile_id2);
2928 if (err != AOM_CODEC_OK) return err;
2929
2930 // Note: Both use common rdmult based on base qindex of fullres.
2931 const int64_t rdmult =
2932 av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
2933
2934 // Find the best rdcost among all superres denoms.
2935 int best_denom = -1;
2936 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
2937 ++denom) {
2938 const int this_index = denom - (SCALE_NUMERATOR + 1);
2939 const int64_t this_sse = superres_sses[this_index];
2940 const int64_t this_rate = superres_rates[this_index];
2941 const int this_largest_tile_id = superres_largest_tile_ids[this_index];
2942 const double this_rdcost = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2943 rdmult, this_rate, this_sse, cm->seq_params.bit_depth);
2944 if (this_rdcost < proj_rdcost1) {
2945 sse1 = this_sse;
2946 rate1 = this_rate;
2947 largest_tile_id1 = this_largest_tile_id;
2948 proj_rdcost1 = this_rdcost;
2949 best_denom = denom;
2950 }
2951 }
2952 const double proj_rdcost2 = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2953 rdmult, rate2, sse2, cm->seq_params.bit_depth);
2954 // Re-encode with superres if it's better.
2955 if (proj_rdcost1 < proj_rdcost2) {
2956 restore_all_coding_context(cpi);
2957 // TODO(urvang): We should avoid rerunning the recode loop by saving
2958 // previous output+state, or running encode only for the selected 'q' in
2959 // previous step.
2960 // Again, temporarily force the best denom.
2961 superres_cfg->superres_scale_denominator = best_denom;
2962 superres_cfg->superres_kf_scale_denominator = best_denom;
2963 int64_t sse3 = INT64_MAX;
2964 int64_t rate3 = INT64_MAX;
2965 cpi->superres_mode =
2966 AOM_SUPERRES_AUTO; // Super-res on for this recode loop.
2967 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
2968 largest_tile_id);
2969 cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
2970 assert(sse1 == sse3);
2971 assert(rate1 == rate3);
2972 assert(largest_tile_id1 == *largest_tile_id);
2973 // Reset.
2974 superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
2975 superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
2976 } else {
2977 *largest_tile_id = largest_tile_id2;
2978 }
2979 } else {
2980 assert(cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_DUAL);
Urvang Joshidf265712020-10-05 23:23:09 -07002981 cpi->superres_mode =
2982 AOM_SUPERRES_AUTO; // Super-res on for this recode loop.
Debargha Mukherjee2f16e852020-10-19 16:57:30 -07002983 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
2984 &largest_tile_id1);
Urvang Joshidf265712020-10-05 23:23:09 -07002985 cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
Debargha Mukherjee2f16e852020-10-19 16:57:30 -07002986 if (err != AOM_CODEC_OK) return err;
2987 restore_all_coding_context(cpi);
2988 // Encode without superres.
2989 assert(cpi->superres_mode == AOM_SUPERRES_NONE);
2990 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
2991 &largest_tile_id2);
2992 if (err != AOM_CODEC_OK) return err;
2993
2994 // Note: Both use common rdmult based on base qindex of fullres.
2995 const int64_t rdmult =
2996 av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
2997 proj_rdcost1 = RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate1, sse1,
2998 cm->seq_params.bit_depth);
2999 const double proj_rdcost2 = RDCOST_DBL_WITH_NATIVE_BD_DIST(
3000 rdmult, rate2, sse2, cm->seq_params.bit_depth);
3001 // Re-encode with superres if it's better.
3002 if (proj_rdcost1 < proj_rdcost2) {
3003 restore_all_coding_context(cpi);
3004 // TODO(urvang): We should avoid rerunning the recode loop by saving
3005 // previous output+state, or running encode only for the selected 'q' in
3006 // previous step.
3007 int64_t sse3 = INT64_MAX;
3008 int64_t rate3 = INT64_MAX;
3009 cpi->superres_mode =
3010 AOM_SUPERRES_AUTO; // Super-res on for this recode loop.
3011 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
3012 largest_tile_id);
3013 cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
3014 assert(sse1 == sse3);
3015 assert(rate1 == rate3);
3016 assert(largest_tile_id1 == *largest_tile_id);
3017 } else {
3018 *largest_tile_id = largest_tile_id2;
3019 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07003020 }
3021
3022 return err;
3023}
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07003024
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08003025extern void av1_print_frame_contexts(const FRAME_CONTEXT *fc,
3026 const char *filename);
3027
Yue Chen2d8405c2020-06-10 14:42:05 -07003028/*!\brief Run the final pass encoding for 1-pass/2-pass encoding mode, and pack
3029 * the bitstream
3030 *
3031 * \ingroup high_level_algo
3032 * \callgraph
3033 * \callergraph
3034 *
3035 * \param[in] cpi Top-level encoder structure
3036 * \param[in] size Bitstream size
3037 * \param[in] dest Bitstream output
3038 *
3039 * \return Returns a value to indicate if the encoding is done successfully.
3040 * \retval #AOM_CODEC_OK
3041 * \retval #AOM_CODEC_ERROR
3042 */
David Turner73245762019-02-11 16:42:34 +00003043static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
3044 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003045 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003046 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00003047 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003048 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003049 struct segmentation *const seg = &cm->seg;
Urvang Joshib6409e92020-03-23 11:23:27 -07003050 FeatureFlags *const features = &cm->features;
Vishesh0481a782020-06-11 14:32:46 +05303051 const TileConfig *const tile_cfg = &oxcf->tile_cfg;
Thomas Davies4822e142017-10-10 11:30:36 +01003052
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003053#if CONFIG_COLLECT_COMPONENT_TIMING
3054 start_timing(cpi, encode_frame_to_data_rate_time);
3055#endif
3056
chiyotsai8b8f8a22020-04-21 11:03:47 -07003057 if (frame_is_intra_only(cm)) {
3058 av1_set_screen_content_options(cpi, features);
3059 cpi->is_screen_content_type = features->allow_screen_content_tools;
3060 }
3061
Fangwen Fu8d164de2016-12-14 13:40:54 -08003062 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00003063 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07003064
Vishesh0481a782020-06-11 14:32:46 +05303065 cm->tiles.large_scale = tile_cfg->enable_large_scale_tile;
3066 cm->tiles.single_tile_decoding = tile_cfg->enable_single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07003067
Urvang Joshib6409e92020-03-23 11:23:27 -07003068 features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
3069 // features->allow_ref_frame_mvs needs to be written into the frame header
Urvang Joshi54ffae72020-03-23 13:37:10 -07003070 // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case
Urvang Joshib6409e92020-03-23 11:23:27 -07003071 // is separated from frame_might_allow_ref_frame_mvs().
Urvang Joshi54ffae72020-03-23 13:37:10 -07003072 features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
Yunqing Wangd48fb162018-06-15 10:55:28 -07003073
Vishesh015b4962020-06-15 14:19:55 +05303074 features->allow_warped_motion = oxcf->motion_mode_cfg.allow_warped_motion &&
3075 frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003076
Urvang Joshi1de67aa2020-03-20 11:21:57 -07003077 cpi->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05303078
Sarah Parker1a72a1c2020-04-24 15:49:47 -07003079 if (frame_is_sframe(cm)) {
3080 GF_GROUP *gf_group = &cpi->gf_group;
Sarah Parker1a72a1c2020-04-24 15:49:47 -07003081 // S frame will wipe out any previously encoded altref so we cannot place
3082 // an overlay frame
3083 gf_group->update_type[gf_group->size] = GF_UPDATE;
Sarah Parker1a72a1c2020-04-24 15:49:47 -07003084 }
3085
Sarah Parker33005522018-07-27 14:46:25 -07003086 if (encode_show_existing_frame(cm)) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +05303087 av1_finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003088 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00003089 int largest_tile_id = 0; // Output from bitstream: unused here
3090 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08003091 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003092
David Turner90311862018-11-29 13:34:36 +00003093 if (seq_params->frame_id_numbers_present_flag &&
3094 current_frame->frame_type == KEY_FRAME) {
3095 // Displaying a forward key-frame, so reset the ref buffer IDs
3096 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
3097 for (int i = 0; i < REF_FRAMES; i++)
3098 cm->ref_frame_id[i] = display_frame_id;
3099 }
3100
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07003101 cpi->seq_params_locked = 1;
3102
David Turner73245762019-02-11 16:42:34 +00003103 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3104 // for the purpose to verify no mismatch between encoder and decoder.
3105 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003106
David Turner73245762019-02-11 16:42:34 +00003107 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003108
Yaowu Xuc27fc142016-08-22 16:08:15 -07003109 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
3110 // to do post-encoding update accordingly.
3111 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003112 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07003113 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003114 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003115
Mufaddal Chakeraea4c6d52020-06-12 15:09:14 +05303116 if (is_psnr_calc_enabled(cpi)) {
3117 cpi->source =
3118 realloc_and_scale_source(cpi, cm->cur_frame->buf.y_crop_width,
3119 cm->cur_frame->buf.y_crop_height);
3120 }
bohanli99852502020-07-14 16:22:45 -07003121
3122 // current_frame->frame_number is incremented already for
3123 // keyframe overlays.
3124 if (!av1_check_keyframe_overlay(cpi->gf_group.index, &cpi->gf_group,
3125 cpi->rc.frames_since_key))
3126 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003127
Tom Finegane4099e32018-01-23 12:01:51 -08003128 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003129 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003130
David Turnerefed6372019-01-11 15:14:11 +00003131 // Work out whether to force_integer_mv this frame
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303132 if (!is_stat_generation_stage(cpi) &&
Urvang Joshib6409e92020-03-23 11:23:27 -07003133 cpi->common.features.allow_screen_content_tools &&
3134 !frame_is_intra_only(cm)) {
David Turnerefed6372019-01-11 15:14:11 +00003135 if (cpi->common.seq_params.force_integer_mv == 2) {
3136 // Adaptive mode: see what previous frame encoded did
3137 if (cpi->unscaled_last_source != NULL) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +05303138 features->cur_frame_force_integer_mv = av1_is_integer_mv(
Vishesh2260a592020-04-06 15:34:51 +05303139 cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info);
David Turnerefed6372019-01-11 15:14:11 +00003140 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07003141 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00003142 }
3143 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07003144 cpi->common.features.cur_frame_force_integer_mv =
David Turnerefed6372019-01-11 15:14:11 +00003145 cpi->common.seq_params.force_integer_mv;
3146 }
3147 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07003148 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00003149 }
Debargha Mukherjeea4c6f692021-03-16 19:45:46 -07003150#if CONFIG_NEW_INTER_MODES
3151 set_max_drl_bits(cpi);
3152#endif // CONFIG_NEW_INTER_MODES
David Turnerefed6372019-01-11 15:14:11 +00003153
Yaowu Xuc27fc142016-08-22 16:08:15 -07003154 // Set default state for segment based loop filter update flags.
3155 cm->lf.mode_ref_delta_update = 0;
3156
Yaowu Xuc27fc142016-08-22 16:08:15 -07003157 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05003158 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003159 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07003160 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003161
3162 // If segmentation is enabled force a map update for key frames.
3163 if (seg->enabled) {
3164 seg->update_map = 1;
3165 seg->update_data = 1;
3166 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003167 }
Vishesh0481a782020-06-11 14:32:46 +05303168 if (tile_cfg->mtu == 0) {
3169 cpi->num_tg = tile_cfg->num_tile_groups;
Thomas Daviesaf6df172016-11-09 14:04:18 +00003170 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08003171 // Use a default value for the purposes of weighting costs in probability
3172 // updates
Urvang Joshi1de67aa2020-03-20 11:21:57 -07003173 cpi->num_tg = DEFAULT_MAX_NUM_TG;
Thomas Daviesaf6df172016-11-09 14:04:18 +00003174 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003175
3176 // For 1 pass CBR, check if we are dropping this frame.
3177 // Never drop on key frame.
Vishesh39e74092020-06-16 17:13:48 +05303178 if (has_no_stats_stage(cpi) && oxcf->rc_cfg.mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00003179 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003180 if (av1_rc_drop_frame(cpi)) {
bohanli1629a4b2020-06-11 16:15:14 -07003181 av1_setup_frame_size(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003182 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00003183 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08003184 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003185 }
3186 }
3187
Jayasanker Jdee179d2020-07-10 23:47:37 +05303188 if (oxcf->tune_cfg.tuning == AOM_TUNE_SSIM)
3189 av1_set_mb_ssim_rdmult_scaling(cpi);
sdengc23c7f12019-06-11 16:56:50 -07003190
sdeng01959162019-12-20 10:46:24 -08003191#if CONFIG_TUNE_VMAF
Vishesh94a65292020-07-01 15:28:53 +05303192 if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
Sai Deng7275a1a2020-08-31 12:06:09 -07003193 oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN ||
3194 oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
sdeng01959162019-12-20 10:46:24 -08003195 av1_set_mb_vmaf_rdmult_scaling(cpi);
sdeng29c62152019-12-09 12:44:18 -08003196 }
sdeng01959162019-12-20 10:46:24 -08003197#endif
sdeng29c62152019-12-09 12:44:18 -08003198
Yaowu Xuf883b422016-08-30 14:01:10 -07003199 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003200
Lester Lu6bc30d62021-12-16 19:13:21 +00003201#if CONFIG_INTERNAL_STATS && !CONFIG_NEW_REF_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003202 memset(cpi->mode_chosen_counts, 0,
3203 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
Lester Lu6bc30d62021-12-16 19:13:21 +00003204#endif // CONFIG_INTERNAL_STATS && !CONFIG_NEW_REF_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003205
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003206 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003207 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08003208 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003209 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00003210 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08003211 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07003212 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
3213 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
3214 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00003215 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07003216 lsb = cpi->source->y_buffer[0] & 0xff;
3217 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00003218 }
David Turner760a2f42018-12-07 15:25:36 +00003219 cm->current_frame_id =
3220 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05003221
3222 // S_frame is meant for stitching different streams of different
3223 // resolutions together, so current_frame_id must be the
3224 // same across different streams of the same content current_frame_id
3225 // should be the same and not random. 0x37 is a chosen number as start
3226 // point
Vishesh7e9873d2020-06-08 15:41:33 +05303227 if (oxcf->kf_cfg.sframe_dist != 0) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003228 } else {
3229 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00003230 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
3231 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003232 }
3233 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003234
Vishesha36c8d02020-06-30 15:42:35 +05303235 switch (oxcf->algo_cfg.cdf_update_mode) {
Hui Su483a8452018-02-26 12:28:48 -08003236 case 0: // No CDF update for any frames(4~6% compression loss).
Urvang Joshib6409e92020-03-23 11:23:27 -07003237 features->disable_cdf_update = 1;
Hui Su483a8452018-02-26 12:28:48 -08003238 break;
3239 case 1: // Enable CDF update for all frames.
Urvang Joshib6409e92020-03-23 11:23:27 -07003240 features->disable_cdf_update = 0;
Hui Su483a8452018-02-26 12:28:48 -08003241 break;
3242 case 2:
3243 // Strategically determine at which frames to do CDF update.
3244 // Currently only enable CDF update for all-intra and no-show frames(1.5%
3245 // compression loss).
3246 // TODO(huisu@google.com): design schemes for various trade-offs between
3247 // compression quality and decoding speed.
Urvang Joshib6409e92020-03-23 11:23:27 -07003248 features->disable_cdf_update =
Hui Sub1b76b32018-02-27 15:24:48 -08003249 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08003250 break;
3251 }
Urvang Joshi450a9a22020-03-31 16:00:22 -07003252 seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08003253
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07003254 int largest_tile_id = 0;
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +05303255 if (av1_superres_in_recode_allowed(cpi)) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07003256 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
3257 AOM_CODEC_OK) {
3258 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003259 }
Hui Su06463e42018-02-23 22:17:36 -08003260 } else {
Urvang Joshi1e86b782020-10-20 11:25:45 -07003261 const aom_superres_mode orig_superres_mode = cpi->superres_mode; // save
Urvang Joshidf265712020-10-05 23:23:09 -07003262 cpi->superres_mode = cpi->oxcf.superres_cfg.superres_mode;
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07003263 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
3264 &largest_tile_id) != AOM_CODEC_OK) {
3265 return AOM_CODEC_ERROR;
3266 }
Urvang Joshi1e86b782020-10-20 11:25:45 -07003267 cpi->superres_mode = orig_superres_mode; // restore
Hui Su06463e42018-02-23 22:17:36 -08003268 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003269
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07003270 cpi->seq_params_locked = 1;
3271
David Turner996b2c12018-12-07 15:52:30 +00003272 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003273 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00003274 for (int i = 0; i < REF_FRAMES; i++) {
3275 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003276 cm->ref_frame_id[i] = cm->current_frame_id;
3277 }
3278 }
3279 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003280
Lester Lu6bc30d62021-12-16 19:13:21 +00003281#if CONFIG_SVC_ENCODER
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07003282 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
3283 cpi->svc.num_encoded_top_layer++;
Lester Lu6bc30d62021-12-16 19:13:21 +00003284#endif // CONFIG_SVC_ENCODER
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07003285
Soo-Chul Han934af352017-10-15 15:21:51 -04003286 if (cm->seg.enabled) {
3287 if (cm->seg.update_map) {
3288 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07003289 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00003290 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Urvang Joshie5fd6462020-10-30 09:58:36 -07003291 cm->cur_frame->mi_cols * cm->cur_frame->mi_rows *
3292 sizeof(*cm->cur_frame->seg_map));
Soo-Chul Han934af352017-10-15 15:21:51 -04003293 }
3294 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003295
3296 if (frame_is_intra_only(cm) == 0) {
3297 release_scaled_references(cpi);
3298 }
3299
David Turner73245762019-02-11 16:42:34 +00003300 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3301 // for the purpose to verify no mismatch between encoder and decoder.
3302 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
3303
3304 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003305
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003306#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07003307 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003308#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003309
Urvang Joshi6237b882020-03-26 15:02:26 -07003310 if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00003311 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07003312 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003313 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07003314 if (!cm->tiles.large_scale) {
David Turnera4c96252019-01-11 16:36:39 +00003315 cm->cur_frame->frame_context = *cm->fc;
3316 }
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08003317
Vishesh0481a782020-06-11 14:32:46 +05303318 if (tile_cfg->enable_ext_tile_debug) {
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08003319 // (yunqing) This test ensures the correctness of large scale tile coding.
Urvang Joshi54ffae72020-03-23 13:37:10 -07003320 if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) {
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08003321 char fn[20] = "./fc";
3322 fn[4] = current_frame->frame_number / 100 + '0';
3323 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
3324 fn[6] = (current_frame->frame_number % 10) + '0';
3325 fn[7] = '\0';
3326 av1_print_frame_contexts(cm->fc, fn);
3327 }
David Turnera4c96252019-01-11 16:36:39 +00003328 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003329
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003330#if CONFIG_COLLECT_COMPONENT_TIMING
3331 end_timing(cpi, encode_frame_to_data_rate_time);
3332
3333 // Print out timing information.
3334 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08003335 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003336 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08003337 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003338 for (i = 0; i < kTimingComponents; i++) {
3339 cpi->component_time[i] += cpi->frame_component_time[i];
3340 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
3341 get_component_name(i), cpi->frame_component_time[i],
3342 cpi->component_time[i]);
3343 cpi->frame_component_time[i] = 0;
3344 }
3345#endif
3346
Urvang Joshi1de67aa2020-03-20 11:21:57 -07003347 cpi->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003348
Yaowu Xuf883b422016-08-30 14:01:10 -07003349 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003350
Yaowu Xuc27fc142016-08-22 16:08:15 -07003351 // Clear the one shot update flags for segmentation map and mode/ref loop
3352 // filter deltas.
3353 cm->seg.update_map = 0;
3354 cm->seg.update_data = 0;
3355 cm->lf.mode_ref_delta_update = 0;
3356
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07003357 // A droppable frame might not be shown but it always
3358 // takes a space in the gf group. Therefore, even when
3359 // it is not shown, we still need update the count down.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003360 if (cm->show_frame) {
bohanli99852502020-07-14 16:22:45 -07003361 // Don't increment frame counters if this is a key frame overlay
3362 if (!av1_check_keyframe_overlay(cpi->gf_group.index, &cpi->gf_group,
3363 cpi->rc.frames_since_key))
3364 ++current_frame->frame_number;
3365 } else if (av1_check_keyframe_arf(cpi->gf_group.index, &cpi->gf_group,
3366 cpi->rc.frames_since_key)) {
3367 // TODO(bohanli) Hack here: increment kf overlay before it is encoded
David Turnerd2a592e2018-11-16 14:59:31 +00003368 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003369 }
3370
Tom Finegane4099e32018-01-23 12:01:51 -08003371 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003372}
3373
David Turner056f7cd2019-01-07 17:48:13 +00003374int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00003375 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00003376 const EncodeFrameParams *const frame_params,
3377 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00003378 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00003379 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00003380
David Turnercb5e36f2019-01-17 17:15:25 +00003381 cpi->unscaled_source = frame_input->source;
3382 cpi->source = frame_input->source;
3383 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00003384
David Turner6e8b4d92019-02-18 15:01:33 +00003385 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
Urvang Joshib6409e92020-03-23 11:23:27 -07003386 cm->features.error_resilient_mode = frame_params->error_resilient_mode;
Urvang Joshi6237b882020-03-26 15:02:26 -07003387 cm->features.primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00003388 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00003389 cm->show_frame = frame_params->show_frame;
Lester Lu6bc30d62021-12-16 19:13:21 +00003390 cm->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00003391 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00003392 cm->show_existing_frame = frame_params->show_existing_frame;
3393 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00003394
David Turner73245762019-02-11 16:42:34 +00003395 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
3396 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
3397
Lester Lu6bc30d62021-12-16 19:13:21 +00003398#if !CONFIG_NEW_REF_SIGNALING
Jayasanker J24cb9bc2020-04-15 13:43:10 +05303399 memcpy(&cpi->refresh_frame, &frame_params->refresh_frame,
3400 sizeof(cpi->refresh_frame));
Lester Lu6bc30d62021-12-16 19:13:21 +00003401#endif // !CONFIG_NEW_REF_SIGNALING
David Turnerfe3aecb2019-02-06 14:42:42 +00003402
Urvang Joshi3fdaf5c2020-07-15 10:17:48 -07003403 if (current_frame->frame_type == KEY_FRAME && !cpi->no_show_fwd_kf) {
liang zhao7aebbd12020-07-03 22:20:34 -07003404 current_frame->key_frame_number += current_frame->frame_number;
David Turnera7f133c2019-01-22 14:47:16 +00003405 current_frame->frame_number = 0;
liang zhao7aebbd12020-07-03 22:20:34 -07003406 }
David Turnera7f133c2019-01-22 14:47:16 +00003407
Remyafc038662020-02-06 11:46:11 +05303408 current_frame->order_hint =
3409 current_frame->frame_number + frame_params->order_offset;
3410 current_frame->display_order_hint = current_frame->order_hint;
Sarah Parkerd9dc3fd2020-08-18 11:11:15 -07003411 current_frame->pyramid_level = get_true_pyr_level(
3412 cpi->gf_group.layer_depth[cpi->gf_group.index],
3413 current_frame->display_order_hint, cpi->gf_group.max_layer_depth);
liang zhao7aebbd12020-07-03 22:20:34 -07003414
3415 current_frame->absolute_poc =
3416 current_frame->key_frame_number + current_frame->display_order_hint;
3417
Remyafc038662020-02-06 11:46:11 +05303418 current_frame->order_hint %=
3419 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
David Turnera7f133c2019-01-22 14:47:16 +00003420
Debargha Mukherjee7118f422020-06-29 15:49:06 -07003421 if (is_stat_generation_stage(cpi)) {
Debargha Mukherjee7118f422020-06-29 15:49:06 -07003422 av1_first_pass(cpi, frame_input->ts_duration);
Debargha Mukherjee7118f422020-06-29 15:49:06 -07003423 } else {
3424 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
3425 AOM_CODEC_OK) {
3426 return AOM_CODEC_ERROR;
3427 }
3428 }
David Turner056f7cd2019-01-07 17:48:13 +00003429 return AOM_CODEC_OK;
3430}
3431
Neil Birkbecka2893ab2018-06-08 14:45:13 -07003432#if CONFIG_DENOISE
3433static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
3434 int block_size, float noise_level,
3435 int64_t time_stamp, int64_t end_time) {
3436 AV1_COMMON *const cm = &cpi->common;
3437 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003438 cpi->denoise_and_model = aom_denoise_and_model_alloc(
3439 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07003440 if (!cpi->denoise_and_model) {
3441 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3442 "Error allocating denoise and model");
3443 return -1;
3444 }
3445 }
3446 if (!cpi->film_grain_table) {
3447 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
3448 if (!cpi->film_grain_table) {
3449 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3450 "Error allocating grain table");
3451 return -1;
3452 }
3453 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
3454 }
3455 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
3456 &cm->film_grain_params)) {
3457 if (cm->film_grain_params.apply_grain) {
3458 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
3459 &cm->film_grain_params);
3460 }
3461 }
3462 return 0;
3463}
3464#endif
3465
James Zern3e2613b2017-03-30 23:14:40 -07003466int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07003467 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3468 int64_t end_time) {
3469 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003470 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003471 int res = 0;
3472 const int subsampling_x = sd->subsampling_x;
3473 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003474 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003475
sdenge63f9fa2019-12-13 09:29:55 -08003476#if CONFIG_TUNE_VMAF
3477 if (!is_stat_generation_stage(cpi) &&
Vishesh94a65292020-07-01 15:28:53 +05303478 cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
sdeng615dc242020-02-04 16:06:14 -08003479 av1_vmaf_frame_preprocessing(cpi, sd);
3480 }
3481 if (!is_stat_generation_stage(cpi) &&
Vishesh94a65292020-07-01 15:28:53 +05303482 cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
sdeng615dc242020-02-04 16:06:14 -08003483 av1_vmaf_blk_preprocessing(cpi, sd);
sdenge63f9fa2019-12-13 09:29:55 -08003484 }
3485#endif
3486
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003487#if CONFIG_INTERNAL_STATS
3488 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07003489 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003490#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07003491#if CONFIG_DENOISE
3492 if (cpi->oxcf.noise_level > 0)
3493 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
3494 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
3495 res = -1;
3496#endif // CONFIG_DENOISE
3497
Yaowu Xuf883b422016-08-30 14:01:10 -07003498 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08003499 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07003500 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003501#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003502 aom_usec_timer_mark(&timer);
3503 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003504#endif
Yunqing Wangaae7a162020-07-09 16:03:01 -07003505
3506 // Note: Regarding profile setting, the following checks are added to help
3507 // choose a proper profile for the input video. The criterion is that all
3508 // bitstreams must be designated as the lowest profile that match its content.
3509 // E.G. A bitstream that contains 4:4:4 video must be designated as High
3510 // Profile in the seq header, and likewise a bitstream that contains 4:2:2
3511 // bitstream must be designated as Professional Profile in the sequence
3512 // header.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003513 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003514 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003515 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003516 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003517 res = -1;
3518 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003519 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003520 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003521 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003522 "Profile 1 requires 4:4:4 color format");
3523 res = -1;
3524 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003525 if ((seq_params->profile == PROFILE_2) &&
3526 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003527 !(subsampling_x == 1 && subsampling_y == 0)) {
3528 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yunqing Wangaae7a162020-07-09 16:03:01 -07003529 "Profile 2 bit-depth <= 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003530 res = -1;
3531 }
3532
3533 return res;
3534}
3535
Yaowu Xuc27fc142016-08-22 16:08:15 -07003536#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003537extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
3538 const unsigned char *img2, int img2_pitch,
3539 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003540
3541static void adjust_image_stat(double y, double u, double v, double all,
3542 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003543 s->stat[STAT_Y] += y;
3544 s->stat[STAT_U] += u;
3545 s->stat[STAT_V] += v;
3546 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07003547 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003548}
3549
Angie Chiang08a22a62017-07-17 17:29:17 -07003550static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003551 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552 double samples = 0.0;
Vishesh4342d7f2020-06-29 18:49:06 +05303553 const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
Yaowu Xue75b58a2020-01-03 12:56:12 -08003554 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003555
Angie Chiang08a22a62017-07-17 17:29:17 -07003556#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00003557 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07003558#endif
3559 cpi->bytes += frame_bytes;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003560 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07003561 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00003562 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003563 double y, u, v, frame_all;
3564
3565 cpi->count++;
3566 if (cpi->b_calculate_psnr) {
3567 PSNR_STATS psnr;
3568 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003569 aom_clear_system_state();
Yaowu Xue75b58a2020-01-03 12:56:12 -08003570 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003571 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
3572 &cpi->psnr);
3573 cpi->total_sq_error += psnr.sse[0];
3574 cpi->total_samples += psnr.samples[0];
3575 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08003576 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003577 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003578 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07003579 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003580 else
Yaowu Xuf883b422016-08-30 14:01:10 -07003581 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003582
Yaowu Xuf883b422016-08-30 14:01:10 -07003583 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003584 cpi->summed_quality += frame_ssim2 * weight;
3585 cpi->summed_weights += weight;
3586
3587#if 0
3588 {
3589 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08003590 double y2 = psnr.psnr[1];
3591 double u2 = psnr.psnr[2];
3592 double v2 = psnr.psnr[3];
3593 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003594 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
David Turnerd2a592e2018-11-16 14:59:31 +00003595 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003596 frame_psnr2, frame_ssim2);
3597 fclose(f);
3598 }
3599#endif
3600 }
3601 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003602 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003603 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07003604 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
3605 recon->y_stride, orig->y_width, orig->y_height);
3606 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003607 cpi->total_blockiness += frame_blockiness;
3608 }
3609
3610 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003611 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003612 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003613 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
3614 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
3615
3616 const double peak = (double)((1 << in_bit_depth) - 1);
3617 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003618 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003619 if (consistency > 0.0)
3620 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003621 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003622 cpi->total_inconsistency += this_inconsistency;
3623 }
3624 }
3625 }
3626
3627 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07003628 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003629 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07003630 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003631 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
3632 }
3633}
3634#endif // CONFIG_INTERNAL_STATS
Wan-Teh Chang1c520052020-06-11 10:17:56 -07003635
Andrey Norkin795ba872018-03-06 13:24:14 -08003636int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
3637 size_t *size, uint8_t *dest, int64_t *time_stamp,
3638 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07003639 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003640 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3641 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003642
3643#if CONFIG_BITSTREAM_DEBUG
Urvang Joshie2b505b2020-06-19 11:39:49 -07003644 assert(cpi->oxcf.max_threads <= 1 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003645 "bitstream debug tool does not support multithreading");
3646 bitstream_queue_record_write();
angiebird1f7174f2020-06-27 16:23:16 -07003647 aom_bitstream_queue_set_frame_write(cm->current_frame.order_hint * 2 +
Yaowu Xu63f2ea32019-04-29 10:47:42 -07003648 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003649#endif
Lester Lu6bc30d62021-12-16 19:13:21 +00003650#if CONFIG_SVC_ENCODER
Marco Paniconi63971322019-08-15 21:32:05 -07003651 if (cpi->use_svc && cm->number_spatial_layers > 1) {
3652 av1_one_pass_cbr_svc_start_layer(cpi);
3653 }
Lester Lu6bc30d62021-12-16 19:13:21 +00003654#endif // CONFIG_SVC_ENCODER
Yaowu Xuc27fc142016-08-22 16:08:15 -07003655
Dominic Symesd4929012018-01-31 17:32:01 +01003656 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00003657 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003658#if CONFIG_INTERNAL_STATS
3659 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07003660 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003661#endif
chiyotsaic666b1f2019-12-20 10:44:58 -08003662 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003663
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07003664 // Normal defaults
Visheshaa6a1702020-06-30 19:27:22 +05303665 cm->features.refresh_frame_context =
3666 oxcf->tool_cfg.frame_parallel_decoding_mode
3667 ? REFRESH_FRAME_CONTEXT_DISABLED
3668 : REFRESH_FRAME_CONTEXT_BACKWARD;
Vishesh0481a782020-06-11 14:32:46 +05303669 if (oxcf->tile_cfg.enable_large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -07003670 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003671
Sarah Parkerb9041612018-05-22 19:06:47 -07003672 // Initialize fields related to forward keyframes
bohanli6f22c7a2020-07-14 10:52:50 -07003673 cpi->no_show_fwd_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08003674
David Turnerdedd8ff2019-01-23 13:59:46 +00003675 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003676
Yue Chen1bc5be62018-08-24 13:57:32 -07003677 const int result =
3678 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
3679 timestamp_ratio, flush);
Wan-Teh Chang1c520052020-06-11 10:17:56 -07003680 if (result == -1) {
David Turnerdedd8ff2019-01-23 13:59:46 +00003681 // Returning -1 indicates no frame encoded; more input is required
3682 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003683 }
Wan-Teh Chang1c520052020-06-11 10:17:56 -07003684 if (result != AOM_CODEC_OK) {
3685 return AOM_CODEC_ERROR;
3686 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003687#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003688 aom_usec_timer_mark(&cmptimer);
3689 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07003690#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjeedce68c52020-07-02 13:06:58 -07003691 // Note *size = 0 indicates a dropeed frame for which psnr is not calculated
3692 if (cpi->b_calculate_psnr && *size > 0) {
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303693 if (cm->show_existing_frame ||
3694 (!is_stat_generation_stage(cpi) && cm->show_frame)) {
David Turnerc4bf8c72019-01-15 13:14:37 +00003695 generate_psnr_packet(cpi);
3696 }
3697 }
Hui Su8ea87322019-03-29 14:44:32 -07003698
Vishesh8ac928b2020-04-01 02:36:35 +05303699 if (cpi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) {
Hui Su8ea87322019-03-29 14:44:32 -07003700 // Initialize level info. at the beginning of each sequence.
bohanli6f22c7a2020-07-14 10:52:50 -07003701 if (cm->current_frame.frame_type == KEY_FRAME && !cpi->no_show_fwd_kf) {
Hui Su58753d62019-05-29 09:56:19 -07003702 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07003703 }
kyslovabeeb7c2019-03-06 18:35:04 -08003704 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07003705 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706
3707#if CONFIG_INTERNAL_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303708 if (!is_stat_generation_stage(cpi)) {
Angie Chiang08a22a62017-07-17 17:29:17 -07003709 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003710 }
3711#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003712#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303713 if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
chiyotsai4c1e5c62020-04-30 17:54:14 -07003714 cpi->tx_search_count += cpi->td.mb.txfm_search_info.tx_search_count;
3715 cpi->td.mb.txfm_search_info.tx_search_count = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003716 }
3717#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003718
Yaowu Xuf883b422016-08-30 14:01:10 -07003719 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003720
Hui Su0d0ee662019-07-29 14:38:36 -07003721 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003722}
3723
Yaowu Xuf883b422016-08-30 14:01:10 -07003724int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
3725 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003726 if (!cm->show_frame) {
3727 return -1;
3728 } else {
3729 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00003730 if (cm->cur_frame != NULL) {
3731 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003732 dest->y_width = cm->width;
3733 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003734 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
3735 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003736 ret = 0;
3737 } else {
3738 ret = -1;
3739 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003740 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003741 return ret;
3742 }
3743}
3744
Yaowu Xuf883b422016-08-30 14:01:10 -07003745int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00003746 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003747
David Turnere7ebf902018-12-04 14:04:55 +00003748 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003749 return 0;
3750}
3751
Yunqing Wang93b18f32018-06-08 21:08:29 -07003752aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
3753 YV12_BUFFER_CONFIG *new_frame,
3754 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07003755 const int num_planes = av1_num_planes(cm);
3756 if (!equal_dimensions_and_border(new_frame, sd))
3757 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
3758 "Incorrect buffer dimensions");
3759 else
3760 aom_yv12_copy_frame(new_frame, sd, num_planes);
3761
3762 return cm->error.error_code;
3763}
3764
Jayasanker J0b1dd292020-04-09 15:47:25 +05303765int av1_set_internal_size(AV1EncoderConfig *const oxcf,
3766 ResizePendingParams *resize_pending_params,
3767 AOM_SCALING horiz_mode, AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003768 int hr = 0, hs = 0, vr = 0, vs = 0;
3769
3770 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
3771
3772 Scale2Ratio(horiz_mode, &hr, &hs);
3773 Scale2Ratio(vert_mode, &vr, &vs);
3774
3775 // always go to the next whole number
Vishesh2b8e4792020-06-12 12:11:19 +05303776 resize_pending_params->width = (hs - 1 + oxcf->frm_dim_cfg.width * hr) / hs;
3777 resize_pending_params->height = (vs - 1 + oxcf->frm_dim_cfg.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003778
Deepa K G260c8262020-07-08 19:13:54 +05303779 if (horiz_mode != NORMAL || vert_mode != NORMAL) {
Marco Paniconi72056922020-06-24 12:31:25 -07003780 oxcf->resize_cfg.resize_mode = RESIZE_FIXED;
Deepa K G260c8262020-07-08 19:13:54 +05303781 oxcf->algo_cfg.enable_tpl_model = 0;
3782 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003783 return 0;
3784}
3785
Urvang Joshi17814622020-03-27 17:26:17 -07003786int av1_get_quantizer(AV1_COMP *cpi) {
3787 return cpi->common.quant_params.base_qindex;
3788}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003789
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04003790int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
3791 size_t output_size = 0;
3792 size_t total_bytes_read = 0;
3793 size_t remaining_size = *frame_size;
3794 uint8_t *buff_ptr = buffer;
3795
3796 // go through each OBUs
3797 while (total_bytes_read < *frame_size) {
3798 uint8_t saved_obu_header[2];
3799 uint64_t obu_payload_size;
3800 size_t length_of_payload_size;
3801 size_t length_of_obu_size;
3802 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
3803 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
3804
3805 // save the obu header (1 or 2 bytes)
3806 memmove(saved_obu_header, buff_ptr, obu_header_size);
3807 // clear the obu_has_size_field
3808 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
3809
3810 // get the payload_size and length of payload_size
3811 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
3812 &obu_payload_size, &length_of_payload_size) != 0) {
3813 return AOM_CODEC_ERROR;
3814 }
3815 obu_bytes_read += length_of_payload_size;
3816
3817 // calculate the length of size of the obu header plus payload
3818 length_of_obu_size =
3819 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
3820
3821 // move the rest of data to new location
3822 memmove(buff_ptr + length_of_obu_size + obu_header_size,
3823 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07003824 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04003825
3826 // write the new obu size
3827 const uint64_t obu_size = obu_header_size + obu_payload_size;
3828 size_t coded_obu_size;
3829 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
3830 &coded_obu_size) != 0) {
3831 return AOM_CODEC_ERROR;
3832 }
3833
3834 // write the saved (modified) obu_header following obu size
3835 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
3836
3837 total_bytes_read += obu_bytes_read;
3838 remaining_size -= obu_bytes_read;
3839 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07003840 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04003841 }
3842
3843 *frame_size = output_size;
3844 return AOM_CODEC_OK;
3845}
3846
Lester Lu6bc30d62021-12-16 19:13:21 +00003847#if CONFIG_SVC_ENCODER
Vishesha195ca32020-04-07 18:46:20 +05303848static void svc_set_updates_external_ref_frame_config(
Vishesh38c05d72020-04-14 12:19:14 +05303849 ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags, SVC *const svc) {
3850 ext_refresh_frame_flags->update_pending = 1;
Lester Lu6bc30d62021-12-16 19:13:21 +00003851#if !CONFIG_NEW_REF_SIGNALING
Vishesh38c05d72020-04-14 12:19:14 +05303852 ext_refresh_frame_flags->last_frame = svc->refresh[svc->ref_idx[0]];
3853 ext_refresh_frame_flags->golden_frame = svc->refresh[svc->ref_idx[3]];
3854 ext_refresh_frame_flags->bwd_ref_frame = svc->refresh[svc->ref_idx[4]];
3855 ext_refresh_frame_flags->alt2_ref_frame = svc->refresh[svc->ref_idx[5]];
3856 ext_refresh_frame_flags->alt_ref_frame = svc->refresh[svc->ref_idx[6]];
Lester Lu6bc30d62021-12-16 19:13:21 +00003857#endif // !CONFIG_NEW_REF_SIGNALING
Vishesha195ca32020-04-07 18:46:20 +05303858 svc->non_reference_frame = 1;
Marco Paniconid8574e32019-08-04 21:30:12 -07003859 for (int i = 0; i < REF_FRAMES; i++) {
Vishesha195ca32020-04-07 18:46:20 +05303860 if (svc->refresh[i] == 1) {
3861 svc->non_reference_frame = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07003862 break;
3863 }
3864 }
3865}
3866
Marco Paniconiad4953a2020-04-02 09:52:58 -07003867static int svc_set_references_external_ref_frame_config(AV1_COMP *cpi) {
3868 // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
3869 // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
3870 int ref = AOM_REFFRAME_ALL;
3871 for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
3872 if (!cpi->svc.reference[i]) ref ^= (1 << i);
3873 }
3874 return ref;
3875}
Lester Lu6bc30d62021-12-16 19:13:21 +00003876#endif // CONFIG_SVC_ENCODER
Marco Paniconiad4953a2020-04-02 09:52:58 -07003877
Yaowu Xuf883b422016-08-30 14:01:10 -07003878void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003879 // TODO(yunqingwang): For what references to use, external encoding flags
3880 // should be consistent with internal reference frame selection. Need to
3881 // ensure that there is not conflict between the two. In AV1 encoder, the
3882 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07003883 // GOLDEN, BWDREF, ALTREF2.
Vishesha195ca32020-04-07 18:46:20 +05303884
3885 ExternalFlags *const ext_flags = &cpi->ext_flags;
Vishesh38c05d72020-04-14 12:19:14 +05303886 ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
3887 &ext_flags->refresh_frame;
Vishesha195ca32020-04-07 18:46:20 +05303888 ext_flags->ref_frame_flags = AOM_REFFRAME_ALL;
Lester Lu6bc30d62021-12-16 19:13:21 +00003889#if CONFIG_NEW_REF_SIGNALING
3890#if CONFIG_SVC_ENCODER
3891 if (cpi->svc.external_ref_frame_config) {
3892 int ref = svc_set_references_external_ref_frame_config(cpi);
3893 av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
3894 svc_set_updates_external_ref_frame_config(ext_refresh_frame_flags,
3895 &cpi->svc);
3896 } else {
3897 ext_refresh_frame_flags->update_pending = 0;
3898 }
3899#else
3900 ext_refresh_frame_flags->update_pending = 0;
3901#endif
3902#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003903 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003904 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
3905 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
3906 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07003907 int ref = AOM_REFFRAME_ALL;
3908
3909 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
3910 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
3911 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
3912
3913 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
3914
3915 if (flags & AOM_EFLAG_NO_REF_ARF) {
3916 ref ^= AOM_ALT_FLAG;
3917 ref ^= AOM_BWD_FLAG;
3918 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003919 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07003920 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
3921 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003922 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07003923
Vishesha195ca32020-04-07 18:46:20 +05303924 av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
Marco Paniconiad4953a2020-04-02 09:52:58 -07003925 } else {
Lester Lu6bc30d62021-12-16 19:13:21 +00003926#if CONFIG_SVC_ENCODER
Marco Paniconiad4953a2020-04-02 09:52:58 -07003927 if (cpi->svc.external_ref_frame_config) {
3928 int ref = svc_set_references_external_ref_frame_config(cpi);
Vishesha195ca32020-04-07 18:46:20 +05303929 av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
Marco Paniconiad4953a2020-04-02 09:52:58 -07003930 }
Lester Lu6bc30d62021-12-16 19:13:21 +00003931#endif // CONFIG_SVC_ENCODER
Yaowu Xuc27fc142016-08-22 16:08:15 -07003932 }
3933
3934 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003935 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003936 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003937
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003938 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
3939 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003940
Yaowu Xuf883b422016-08-30 14:01:10 -07003941 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003942
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003943 if (flags & AOM_EFLAG_NO_UPD_ARF) {
3944 upd ^= AOM_ALT_FLAG;
3945 upd ^= AOM_BWD_FLAG;
3946 upd ^= AOM_ALT2_FLAG;
3947 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003948
Vishesh38c05d72020-04-14 12:19:14 +05303949 ext_refresh_frame_flags->last_frame = (upd & AOM_LAST_FLAG) != 0;
3950 ext_refresh_frame_flags->golden_frame = (upd & AOM_GOLD_FLAG) != 0;
3951 ext_refresh_frame_flags->alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
3952 ext_refresh_frame_flags->bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
3953 ext_refresh_frame_flags->alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
3954 ext_refresh_frame_flags->update_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00003955 } else {
Lester Lu6bc30d62021-12-16 19:13:21 +00003956#if CONFIG_SVC_ENCODER
Marco Paniconid8574e32019-08-04 21:30:12 -07003957 if (cpi->svc.external_ref_frame_config)
Vishesh38c05d72020-04-14 12:19:14 +05303958 svc_set_updates_external_ref_frame_config(ext_refresh_frame_flags,
3959 &cpi->svc);
Marco Paniconid8574e32019-08-04 21:30:12 -07003960 else
Lester Lu6bc30d62021-12-16 19:13:21 +00003961#endif // CONFIG_SVC_ENCODER
Vishesh38c05d72020-04-14 12:19:14 +05303962 ext_refresh_frame_flags->update_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003963 }
Lester Lu6bc30d62021-12-16 19:13:21 +00003964#endif // CONFIG_NEW_REF_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003965
Visheshaa6a1702020-06-30 19:27:22 +05303966 ext_flags->use_ref_frame_mvs = cpi->oxcf.tool_cfg.enable_ref_frame_mvs &
Vishesha195ca32020-04-07 18:46:20 +05303967 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
Visheshaa6a1702020-06-30 19:27:22 +05303968 ext_flags->use_error_resilient = cpi->oxcf.tool_cfg.error_resilient_mode |
Vishesha195ca32020-04-07 18:46:20 +05303969 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
3970 ext_flags->use_s_frame =
Vishesh7e9873d2020-06-08 15:41:33 +05303971 cpi->oxcf.kf_cfg.enable_sframe | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Vishesha195ca32020-04-07 18:46:20 +05303972 ext_flags->use_primary_ref_none =
3973 (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07003974
Yaowu Xuf883b422016-08-30 14:01:10 -07003975 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
Satish Kumar Suman329de4d2020-06-16 10:48:32 +05303976 update_entropy(&ext_flags->refresh_frame_context,
3977 &ext_flags->refresh_frame_context_pending, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003978 }
3979}
Andrey Norkin795ba872018-03-06 13:24:14 -08003980
Tom Fineganf8d6a162018-08-21 10:47:55 -07003981aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
3982 if (!cpi) return NULL;
3983
3984 uint8_t header_buf[512] = { 0 };
3985 const uint32_t sequence_header_size =
Urvang Joshi450a9a22020-03-31 16:00:22 -07003986 av1_write_sequence_header_obu(&cpi->common.seq_params, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07003987 assert(sequence_header_size <= sizeof(header_buf));
3988 if (sequence_header_size == 0) return NULL;
3989
3990 const size_t obu_header_size = 1;
3991 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
3992 const size_t payload_offset = obu_header_size + size_field_size;
3993
3994 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
3995 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
3996
Vishesh8ac928b2020-04-01 02:36:35 +05303997 if (av1_write_obu_header(&cpi->level_params, OBU_SEQUENCE_HEADER, 0,
3998 &header_buf[0]) != obu_header_size) {
Tom Fineganf8d6a162018-08-21 10:47:55 -07003999 return NULL;
4000 }
4001
4002 size_t coded_size_field_size = 0;
4003 if (aom_uleb_encode(sequence_header_size, size_field_size,
4004 &header_buf[obu_header_size],
4005 &coded_size_field_size) != 0) {
4006 return NULL;
4007 }
4008 assert(coded_size_field_size == size_field_size);
4009
4010 aom_fixed_buf_t *global_headers =
4011 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
4012 if (!global_headers) return NULL;
4013
4014 const size_t global_header_buf_size =
4015 obu_header_size + size_field_size + sequence_header_size;
4016
4017 global_headers->buf = malloc(global_header_buf_size);
4018 if (!global_headers->buf) {
4019 free(global_headers);
4020 return NULL;
4021 }
4022
4023 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
4024 global_headers->sz = global_header_buf_size;
4025 return global_headers;
4026}