blob: cd51552496398f2da52faa4636fa23139c1cb829 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
Urvang Joshibc82d382019-11-01 17:59:20 -070013#include <float.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070014#include <math.h>
15#include <stdio.h>
Debargha Mukherjeec22e2c52020-07-07 00:06:58 -070016#include <string.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
Tom Finegan60e653d2018-05-22 11:34:58 -070018#include "config/aom_config.h"
chiyotsaic814afb2020-08-04 13:12:35 -070019#include "config/aom_dsp_rtcd.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070020
Yaowu Xufa3721d2018-07-30 14:38:49 -070021#if CONFIG_DENOISE
22#include "aom_dsp/grain_table.h"
23#include "aom_dsp/noise_util.h"
24#include "aom_dsp/noise_model.h"
25#endif
26#include "aom_dsp/psnr.h"
27#if CONFIG_INTERNAL_STATS
28#include "aom_dsp/ssim.h"
29#endif
30#include "aom_ports/aom_timer.h"
31#include "aom_ports/mem.h"
32#include "aom_ports/system_state.h"
33#include "aom_scale/aom_scale.h"
David Turner1539bb02019-01-24 15:28:13 +000034#if CONFIG_BITSTREAM_DEBUG
Yaowu Xufa3721d2018-07-30 14:38:49 -070035#include "aom_util/debug_util.h"
David Turner1539bb02019-01-24 15:28:13 +000036#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070037
38#include "av1/common/alloccommon.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070039#include "av1/common/filter.h"
40#include "av1/common/idct.h"
41#include "av1/common/reconinter.h"
42#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070043#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070044#include "av1/common/tile_common.h"
45
46#include "av1/encoder/aq_complexity.h"
47#include "av1/encoder/aq_cyclicrefresh.h"
48#include "av1/encoder/aq_variance.h"
49#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070050#include "av1/encoder/context_tree.h"
51#include "av1/encoder/encodeframe.h"
52#include "av1/encoder/encodemv.h"
David Turner056f7cd2019-01-07 17:48:13 +000053#include "av1/encoder/encode_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070054#include "av1/encoder/encoder.h"
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +053055#include "av1/encoder/encoder_alloc.h"
Satish Kumar Suman897b7942020-06-11 11:44:29 +053056#include "av1/encoder/encoder_utils.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070057#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070058#include "av1/encoder/ethread.h"
59#include "av1/encoder/firstpass.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070060#include "av1/encoder/hash_motion.h"
chiyotsai73ddf442020-06-01 15:42:23 -070061#include "av1/encoder/intra_mode_search.h"
chiyotsaic666b1f2019-12-20 10:44:58 -080062#include "av1/encoder/mv_prec.h"
David Turner0fa8c492019-02-06 16:38:13 +000063#include "av1/encoder/pass2_strategy.h"
Debargha Mukherjee2d565f42020-06-17 18:07:12 -070064#include "av1/encoder/pickcdef.h"
yixindu368bde52021-06-04 18:30:53 -070065#if CONFIG_CCSO
66#include "av1/encoder/pickccso.h"
67#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070068#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070069#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070070#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070071#include "av1/encoder/ratectrl.h"
Satish Kumar Suman47c06892020-06-10 12:45:25 +053072#include "av1/encoder/rc_utils.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070073#include "av1/encoder/rd.h"
Debargha Mukherjeedf713102018-10-02 12:33:32 -070074#include "av1/encoder/rdopt.h"
Debargha Mukherjeec22e2c52020-07-07 00:06:58 -070075#include "av1/encoder/reconinter_enc.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070076#include "av1/encoder/segmentation.h"
77#include "av1/encoder/speed_features.h"
Debargha Mukherjeec22e2c52020-07-07 00:06:58 -070078#include "av1/encoder/subgop.h"
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +053079#include "av1/encoder/superres_scale.h"
Debargha Mukherjee347c64d2019-05-08 13:53:46 -070080#include "av1/encoder/tpl_model.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070081
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000082#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000083
Debargha Mukherjeea4c6f692021-03-16 19:45:46 -070084#if CONFIG_NEW_INTER_MODES
85#define DEF_MAX_DRL_REFMVS 4
86#endif // CONFIG_NEW_INTER_MODES
87
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080088#if CONFIG_ENTROPY_STATS
89FRAME_COUNTS aggregate_fc;
90#endif // CONFIG_ENTROPY_STATS
91
Yaowu Xuc27fc142016-08-22 16:08:15 -070092// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070093#ifdef OUTPUT_YUV_REC
94FILE *yuv_rec_file;
95#define FILE_NAME_LEN 100
96#endif
97
Yaowu Xuf883b422016-08-30 14:01:10 -070098static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070099 switch (mode) {
100 case NORMAL:
101 *hr = 1;
102 *hs = 1;
103 break;
104 case FOURFIVE:
105 *hr = 4;
106 *hs = 5;
107 break;
108 case THREEFIVE:
109 *hr = 3;
110 *hs = 5;
111 break;
Marco Paniconi4e869372020-06-23 10:22:50 -0700112 case THREEFOUR:
113 *hr = 3;
114 *hs = 4;
115 break;
116 case ONEFOUR:
117 *hr = 1;
118 *hs = 4;
119 break;
120 case ONEEIGHT:
121 *hr = 1;
122 *hs = 8;
123 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700124 case ONETWO:
125 *hr = 1;
126 *hs = 2;
127 break;
128 default:
129 *hr = 1;
130 *hs = 1;
131 assert(0);
132 break;
133 }
134}
135
Yaowu Xuf883b422016-08-30 14:01:10 -0700136int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
137 int cols) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700138 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
139 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140 unsigned char *const active_map_8x8 = cpi->active_map.map;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700141 const int mi_rows = mi_params->mi_rows;
142 const int mi_cols = mi_params->mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700143 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
144 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145 cpi->active_map.update = 1;
146 if (new_map_16x16) {
147 int r, c;
148 for (r = 0; r < mi_rows; ++r) {
149 for (c = 0; c < mi_cols; ++c) {
150 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700151 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152 ? AM_SEGMENT_ID_ACTIVE
153 : AM_SEGMENT_ID_INACTIVE;
154 }
155 }
156 cpi->active_map.enabled = 1;
157 } else {
158 cpi->active_map.enabled = 0;
159 }
160 return 0;
161 } else {
162 return -1;
163 }
164}
165
Yaowu Xuf883b422016-08-30 14:01:10 -0700166int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
167 int cols) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700168 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
169 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols &&
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170 new_map_16x16) {
Vishesh8c90fff2020-04-13 11:32:56 +0530171 unsigned char *const seg_map_8x8 = cpi->enc_seg.map;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700172 const int mi_rows = mi_params->mi_rows;
173 const int mi_cols = mi_params->mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700174 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
175 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
176
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
178 if (cpi->active_map.enabled) {
179 int r, c;
180 for (r = 0; r < mi_rows; ++r) {
181 for (c = 0; c < mi_cols; ++c) {
182 // Cyclic refresh segments are considered active despite not having
183 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700184 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
186 }
187 }
188 }
189 return 0;
190 } else {
191 return -1;
192 }
193}
194
Yaowu Xuf883b422016-08-30 14:01:10 -0700195void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700196 av1_rtcd();
197 aom_dsp_rtcd();
198 aom_scale_rtcd();
199 av1_init_intra_predictors();
200 av1_init_me_luts();
201 av1_rc_init_minq_luts();
202 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203}
204
Yaowu Xuf883b422016-08-30 14:01:10 -0700205static void update_reference_segmentation_map(AV1_COMP *cpi) {
206 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700207 const CommonModeInfoParams *const mi_params = &cm->mi_params;
208 MB_MODE_INFO **mi_4x4_ptr = mi_params->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +0000209 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700211 for (int row = 0; row < mi_params->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700212 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700213 uint8_t *cache = cache_ptr;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700214 for (int col = 0; col < mi_params->mi_cols; col++, mi_4x4++, cache++)
Yushin Choa7f65922018-04-04 16:06:11 -0700215 cache[0] = mi_4x4[0]->segment_id;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700216 mi_4x4_ptr += mi_params->mi_stride;
217 cache_ptr += mi_params->mi_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218 }
219}
220
Yaowu Xuf883b422016-08-30 14:01:10 -0700221void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700222 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700223 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224}
225
Hui Suef139e12019-05-20 15:51:22 -0700226double av1_get_compression_ratio(const AV1_COMMON *const cm,
227 size_t encoded_frame_size) {
228 const int upscaled_width = cm->superres_upscaled_width;
229 const int height = cm->height;
230 const int luma_pic_size = upscaled_width * height;
231 const SequenceHeader *const seq_params = &cm->seq_params;
232 const BITSTREAM_PROFILE profile = seq_params->profile;
233 const int pic_size_profile_factor =
234 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
235 encoded_frame_size =
236 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
237 const size_t uncompressed_frame_size =
238 (luma_pic_size * pic_size_profile_factor) >> 3;
239 return uncompressed_frame_size / (double)encoded_frame_size;
240}
241
Vishesh0481a782020-06-11 14:32:46 +0530242static void set_tile_info(AV1_COMMON *const cm,
243 const TileConfig *const tile_cfg) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700244 const CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi54ffae72020-03-23 13:37:10 -0700245 const SequenceHeader *const seq_params = &cm->seq_params;
246 CommonTileParams *const tiles = &cm->tiles;
Dominic Symesf58f1112017-09-25 12:47:40 +0200247 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200248
249 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200250
251 // configure tile columns
Vishesh0481a782020-06-11 14:32:46 +0530252 if (tile_cfg->tile_width_count == 0 || tile_cfg->tile_height_count == 0) {
Urvang Joshi54ffae72020-03-23 13:37:10 -0700253 tiles->uniform_spacing = 1;
Vishesh0481a782020-06-11 14:32:46 +0530254 tiles->log2_cols = AOMMAX(tile_cfg->tile_columns, tiles->min_log2_cols);
Urvang Joshi54ffae72020-03-23 13:37:10 -0700255 tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200256 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700257 int mi_cols =
258 ALIGN_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -0700259 int sb_cols = mi_cols >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200260 int size_sb, j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -0700261 tiles->uniform_spacing = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200262 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -0700263 tiles->col_start_sb[i] = start_sb;
Vishesh0481a782020-06-11 14:32:46 +0530264 size_sb = tile_cfg->tile_widths[j++];
265 if (j >= tile_cfg->tile_width_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -0700266 start_sb += AOMMIN(size_sb, tiles->max_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200267 }
Urvang Joshi54ffae72020-03-23 13:37:10 -0700268 tiles->cols = i;
269 tiles->col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200270 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700271 av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols,
272 tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200273
274 // configure tile rows
Urvang Joshi54ffae72020-03-23 13:37:10 -0700275 if (tiles->uniform_spacing) {
Vishesh0481a782020-06-11 14:32:46 +0530276 tiles->log2_rows = AOMMAX(tile_cfg->tile_rows, tiles->min_log2_rows);
Urvang Joshi54ffae72020-03-23 13:37:10 -0700277 tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200278 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700279 int mi_rows =
280 ALIGN_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -0700281 int sb_rows = mi_rows >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200282 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200283 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -0700284 tiles->row_start_sb[i] = start_sb;
Vishesh0481a782020-06-11 14:32:46 +0530285 size_sb = tile_cfg->tile_heights[j++];
286 if (j >= tile_cfg->tile_height_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -0700287 start_sb += AOMMIN(size_sb, tiles->max_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200288 }
Urvang Joshi54ffae72020-03-23 13:37:10 -0700289 tiles->rows = i;
290 tiles->row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200291 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700292 av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200293}
294
Yaowu Xuf883b422016-08-30 14:01:10 -0700295static void update_frame_size(AV1_COMP *cpi) {
296 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700297 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
298
chiyotsaia7091f12019-08-09 16:48:27 -0700299 // We need to reallocate the context buffers here in case we need more mis.
300 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
301 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
302 "Failed to allocate context buffers");
303 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700304 av1_init_mi_buffers(&cm->mi_params);
chiyotsaia7091f12019-08-09 16:48:27 -0700305
Urvang Joshi9543ad72020-04-17 16:59:46 -0700306 av1_init_macroblockd(cm, xd);
chiyotsai426c0662019-08-05 16:15:11 -0700307
Visheshd1317912020-04-07 14:39:44 +0530308 if (!is_stat_generation_stage(cpi))
309 alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
Deepa K Gefb2d242020-07-08 17:43:49 +0530310
311 if (!cpi->seq_params_locked)
Jayasanker Jdee179d2020-07-10 23:47:37 +0530312 set_sb_size(&cm->seq_params, av1_select_sb_size(cpi));
Deepa K Gefb2d242020-07-08 17:43:49 +0530313
Vishesh0481a782020-06-11 14:32:46 +0530314 set_tile_info(cm, &cpi->oxcf.tile_cfg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315}
316
Debargha Mukherjee57498692018-05-11 13:29:31 -0700317static INLINE int does_level_match(int width, int height, double fps,
318 int lvl_width, int lvl_height,
319 double lvl_fps, int lvl_dim_mult) {
320 const int64_t lvl_luma_pels = lvl_width * lvl_height;
321 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
322 const int64_t luma_pels = width * height;
323 const double display_sample_rate = luma_pels * fps;
324 return luma_pels <= lvl_luma_pels &&
325 display_sample_rate <= lvl_display_sample_rate &&
326 width <= lvl_width * lvl_dim_mult &&
327 height <= lvl_height * lvl_dim_mult;
328}
329
Andrey Norkin26495512018-06-20 17:13:11 -0700330static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Vishesh2b8e4792020-06-12 12:11:19 +0530331 int width, int height,
332 double init_framerate) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700333 // TODO(any): This is a placeholder function that only addresses dimensions
334 // and max display sample rates.
335 // Need to add checks for max bit rate, max decoded luma sample rate, header
336 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -0700337 AV1_LEVEL level = SEQ_LEVEL_MAX;
Vishesh2b8e4792020-06-12 12:11:19 +0530338 if (does_level_match(width, height, init_framerate, 512, 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700339 level = SEQ_LEVEL_2_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530340 } else if (does_level_match(width, height, init_framerate, 704, 396, 30.0,
341 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700342 level = SEQ_LEVEL_2_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530343 } else if (does_level_match(width, height, init_framerate, 1088, 612, 30.0,
344 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700345 level = SEQ_LEVEL_3_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530346 } else if (does_level_match(width, height, init_framerate, 1376, 774, 30.0,
347 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700348 level = SEQ_LEVEL_3_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530349 } else if (does_level_match(width, height, init_framerate, 2048, 1152, 30.0,
350 3)) {
Hui Su8427ff22019-03-11 10:14:33 -0700351 level = SEQ_LEVEL_4_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530352 } else if (does_level_match(width, height, init_framerate, 2048, 1152, 60.0,
353 3)) {
Hui Su8427ff22019-03-11 10:14:33 -0700354 level = SEQ_LEVEL_4_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530355 } else if (does_level_match(width, height, init_framerate, 4096, 2176, 30.0,
356 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700357 level = SEQ_LEVEL_5_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530358 } else if (does_level_match(width, height, init_framerate, 4096, 2176, 60.0,
359 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700360 level = SEQ_LEVEL_5_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530361 } else if (does_level_match(width, height, init_framerate, 4096, 2176, 120.0,
362 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700363 level = SEQ_LEVEL_5_2;
Vishesh2b8e4792020-06-12 12:11:19 +0530364 } else if (does_level_match(width, height, init_framerate, 8192, 4352, 30.0,
365 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700366 level = SEQ_LEVEL_6_0;
Vishesh2b8e4792020-06-12 12:11:19 +0530367 } else if (does_level_match(width, height, init_framerate, 8192, 4352, 60.0,
368 2)) {
Todd Nguyenff4c8602019-11-25 13:20:21 -0800369 level = SEQ_LEVEL_6_1;
Vishesh2b8e4792020-06-12 12:11:19 +0530370 } else if (does_level_match(width, height, init_framerate, 8192, 4352, 120.0,
371 2)) {
Hui Su8427ff22019-03-11 10:14:33 -0700372 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700373 }
chiyotsaiaa33bbf2019-12-02 12:07:53 -0800374
Urvang Joshi450a9a22020-03-31 16:00:22 -0700375 SequenceHeader *const seq_params = &cm->seq_params;
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700376 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -0700377 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -0700378 // Set the maximum parameters for bitrate and buffer size for this profile,
379 // level, and tier
Urvang Joshi450a9a22020-03-31 16:00:22 -0700380 seq_params->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -0700381 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -0700382 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
383 // check
Urvang Joshi450a9a22020-03-31 16:00:22 -0700384 if (seq_params->op_params[i].bitrate == 0)
Andrey Norkin26495512018-06-20 17:13:11 -0700385 aom_internal_error(
386 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
387 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -0700388 // Buffer size in bits/s is bitrate in bits/s * 1 s
Urvang Joshi450a9a22020-03-31 16:00:22 -0700389 seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700390 }
391}
392
Marco Paniconi2aa13c42020-06-01 11:18:38 -0700393void av1_init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
394 const AV1EncoderConfig *oxcf, int use_svc) {
Vishesh2b8e4792020-06-12 12:11:19 +0530395 const FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg;
Visheshaa6a1702020-06-30 19:27:22 +0530396 const ToolCfg *const tool_cfg = &oxcf->tool_cfg;
Vishesh2b8e4792020-06-12 12:11:19 +0530397
Vishesh4342d7f2020-06-29 18:49:06 +0530398 seq->still_picture =
Visheshaa6a1702020-06-30 19:27:22 +0530399 (tool_cfg->force_video_mode == 0) && (oxcf->input_cfg.limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700400 seq->reduced_still_picture_hdr = seq->still_picture;
Visheshaa6a1702020-06-30 19:27:22 +0530401 seq->reduced_still_picture_hdr &= !tool_cfg->full_still_picture_hdr;
Fyodor Kyslov2b492d82021-07-11 16:37:35 -0700402 seq->force_screen_content_tools = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700403 seq->force_integer_mv = 2;
Visheshaa6a1702020-06-30 19:27:22 +0530404 seq->order_hint_info.enable_order_hint = tool_cfg->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +0000405 seq->frame_id_numbers_present_flag =
406 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Visheshaa6a1702020-06-30 19:27:22 +0530407 !oxcf->tile_cfg.enable_large_scale_tile &&
408 tool_cfg->error_resilient_mode && !use_svc;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700409 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +0000410 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700411 seq->force_screen_content_tools = 2;
412 seq->force_integer_mv = 2;
413 }
David Turnerebf96f42018-11-14 16:57:57 +0000414 seq->order_hint_info.order_hint_bits_minus_1 =
415 seq->order_hint_info.enable_order_hint
416 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
417 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700418
Vishesh2b8e4792020-06-12 12:11:19 +0530419 seq->max_frame_width = frm_dim_cfg->forced_max_frame_width
420 ? frm_dim_cfg->forced_max_frame_width
421 : frm_dim_cfg->width;
422 seq->max_frame_height = frm_dim_cfg->forced_max_frame_height
423 ? frm_dim_cfg->forced_max_frame_height
424 : frm_dim_cfg->height;
David Turner760a2f42018-12-07 15:25:36 +0000425 seq->num_bits_width =
426 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
427 seq->num_bits_height =
428 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
429 assert(seq->num_bits_width <= 16);
430 assert(seq->num_bits_height <= 16);
431
432 seq->frame_id_length = FRAME_ID_LENGTH;
433 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
434
Hui Su93c395b2020-10-05 12:00:20 -0700435#if !CONFIG_REMOVE_DUAL_FILTER
Visheshaa6a1702020-06-30 19:27:22 +0530436 seq->enable_dual_filter = tool_cfg->enable_dual_filter;
Hui Su93c395b2020-10-05 12:00:20 -0700437#endif // !CONFIG_REMOVE_DUAL_FILTER
Debargha Mukherjee9b5a89b2020-10-02 18:28:48 -0700438#if !CONFIG_REMOVE_DIST_WTD_COMP
Vishesh8db8a202020-06-02 17:09:45 +0530439 seq->order_hint_info.enable_dist_wtd_comp =
440 oxcf->comp_type_cfg.enable_dist_wtd_comp;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -0800441 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +0000442 seq->order_hint_info.enable_order_hint;
Debargha Mukherjee9b5a89b2020-10-02 18:28:48 -0700443#endif // !CONFIG_REMOVE_DIST_WTD_COMP
Visheshaa6a1702020-06-30 19:27:22 +0530444 seq->order_hint_info.enable_ref_frame_mvs = tool_cfg->ref_frame_mvs_present;
David Turnerebf96f42018-11-14 16:57:57 +0000445 seq->order_hint_info.enable_ref_frame_mvs &=
446 seq->order_hint_info.enable_order_hint;
Vishesh0d279aa2020-06-03 15:56:56 +0530447 seq->enable_superres = oxcf->superres_cfg.enable_superres;
Visheshaa6a1702020-06-30 19:27:22 +0530448 seq->enable_cdef = tool_cfg->enable_cdef;
449 seq->enable_restoration = tool_cfg->enable_restoration;
yixindu368bde52021-06-04 18:30:53 -0700450#if CONFIG_CCSO
451 seq->enable_ccso = tool_cfg->enable_ccso;
452#endif
Vishesh015b4962020-06-15 14:19:55 +0530453 seq->enable_warped_motion = oxcf->motion_mode_cfg.enable_warped_motion;
Visheshaa6a1702020-06-30 19:27:22 +0530454 seq->enable_interintra_compound = tool_cfg->enable_interintra_comp;
Vishesh8db8a202020-06-02 17:09:45 +0530455 seq->enable_masked_compound = oxcf->comp_type_cfg.enable_masked_comp;
Vishesh29649842020-06-01 18:18:36 +0530456 seq->enable_intra_edge_filter = oxcf->intra_mode_cfg.enable_intra_edge_filter;
457 seq->enable_filter_intra = oxcf->intra_mode_cfg.enable_filter_intra;
leolzhao02141602021-02-16 15:06:35 -0800458#if CONFIG_SDP
459 seq->enable_sdp = oxcf->part_cfg.enable_sdp;
460#endif
leolzhao943a08d2021-03-30 15:32:52 -0700461#if CONFIG_MRLS
462 seq->enable_mrls = oxcf->intra_mode_cfg.enable_mrls;
463#endif
SARWER, Mohammed Golam4241ffe2021-06-08 11:06:06 -0700464#if CONFIG_ORIP
465 seq->enable_orip = oxcf->intra_mode_cfg.enable_orip;
466#endif
Madhu Peringassery Krishnanee0d2f02021-06-14 12:08:32 -0700467#if CONFIG_IST
468 seq->enable_ist = oxcf->txfm_cfg.enable_ist;
469#endif
Vishesh2b8e4792020-06-12 12:11:19 +0530470 set_bitstream_level_tier(seq, cm, frm_dim_cfg->width, frm_dim_cfg->height,
Vishesh4342d7f2020-06-29 18:49:06 +0530471 oxcf->input_cfg.init_framerate);
Adrian Grangec56f6ec2018-05-31 14:19:32 -0700472
473 if (seq->operating_points_cnt_minus_1 == 0) {
474 seq->operating_point_idc[0] = 0;
475 } else {
Marco Paniconi6da983b2019-09-16 20:12:43 -0700476 // Set operating_point_idc[] such that the i=0 point corresponds to the
477 // highest quality operating point (all layers), and subsequent
478 // operarting points (i > 0) are lower quality corresponding to
479 // skip decoding enhancement layers (temporal first).
480 int i = 0;
481 assert(seq->operating_points_cnt_minus_1 ==
482 (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
483 for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
484 for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
485 seq->operating_point_idc[i] =
486 (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
487 ~(~0u << (cm->number_temporal_layers - tl));
488 i++;
489 }
490 }
Adrian Grangec56f6ec2018-05-31 14:19:32 -0700491 }
Ryan Leiccc6ea72021-01-06 11:43:56 -0800492#if CONFIG_EXTQUANT
493 const int is_360p_or_larger =
494 AOMMIN(seq->max_frame_width, seq->max_frame_height) >= 360;
495 const int is_720p_or_larger =
496 AOMMIN(seq->max_frame_width, seq->max_frame_height) >= 720;
497 if (!is_360p_or_larger) {
498 seq->base_y_dc_delta_q = -7;
499 seq->base_uv_dc_delta_q = -6;
500 } else if (!is_720p_or_larger) {
501 seq->base_y_dc_delta_q = -5;
502 seq->base_uv_dc_delta_q = -4;
503 } else {
504 seq->base_y_dc_delta_q = -4;
505 seq->base_uv_dc_delta_q = -3;
506 }
507#endif // CONFIG_EXTQUANT
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700508}
509
Yaowu Xuf883b422016-08-30 14:01:10 -0700510static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
511 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700512 SequenceHeader *const seq_params = &cm->seq_params;
Jayasanker J0b1dd292020-04-09 15:47:25 +0530513 ResizePendingParams *resize_pending_params = &cpi->resize_pending_params;
Vishesha9082242020-06-17 18:26:09 +0530514 const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
Vishesh52fdeae2020-06-24 14:37:00 +0530515 const ColorCfg *const color_cfg = &oxcf->color_cfg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700516 cpi->oxcf = *oxcf;
Vishesh4342d7f2020-06-29 18:49:06 +0530517 cpi->framerate = oxcf->input_cfg.init_framerate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518
Urvang Joshi450a9a22020-03-31 16:00:22 -0700519 seq_params->profile = oxcf->profile;
Visheshaa6a1702020-06-30 19:27:22 +0530520 seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700521 seq_params->use_highbitdepth = oxcf->use_highbitdepth;
Vishesh52fdeae2020-06-24 14:37:00 +0530522 seq_params->color_primaries = color_cfg->color_primaries;
523 seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
524 seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
Visheshaa6a1702020-06-30 19:27:22 +0530525 seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
Vishesh073fc962020-07-01 17:39:16 +0530526 seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
Vishesh52fdeae2020-06-24 14:37:00 +0530527 seq_params->color_range = color_cfg->color_range;
Vishesha9082242020-06-17 18:26:09 +0530528 seq_params->timing_info_present = dec_model_cfg->timing_info_present;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700529 seq_params->timing_info.num_units_in_display_tick =
Vishesha9082242020-06-17 18:26:09 +0530530 dec_model_cfg->timing_info.num_units_in_display_tick;
531 seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700532 seq_params->timing_info.equal_picture_interval =
Vishesha9082242020-06-17 18:26:09 +0530533 dec_model_cfg->timing_info.equal_picture_interval;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700534 seq_params->timing_info.num_ticks_per_picture =
Vishesha9082242020-06-17 18:26:09 +0530535 dec_model_cfg->timing_info.num_ticks_per_picture;
Andrey Norkin795ba872018-03-06 13:24:14 -0800536
Urvang Joshi450a9a22020-03-31 16:00:22 -0700537 seq_params->display_model_info_present_flag =
Vishesha9082242020-06-17 18:26:09 +0530538 dec_model_cfg->display_model_info_present_flag;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700539 seq_params->decoder_model_info_present_flag =
Vishesha9082242020-06-17 18:26:09 +0530540 dec_model_cfg->decoder_model_info_present_flag;
541 if (dec_model_cfg->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -0700542 // set the decoder model parameters in schedule mode
Urvang Joshi450a9a22020-03-31 16:00:22 -0700543 seq_params->decoder_model_info.num_units_in_decoding_tick =
Vishesha9082242020-06-17 18:26:09 +0530544 dec_model_cfg->num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -0700545 cm->buffer_removal_time_present = 1;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700546 av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
547 av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
548 } else if (seq_params->timing_info_present &&
549 seq_params->timing_info.equal_picture_interval &&
550 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -0700551 // set the decoder model parameters in resource availability mode
Urvang Joshi450a9a22020-03-31 16:00:22 -0700552 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -0700553 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700554 seq_params->op_params[0].initial_display_delay =
Andrey Norkinc7511de2018-06-22 12:31:06 -0700555 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -0800556 }
Andrey Norkinc7511de2018-06-22 12:31:06 -0700557
Urvang Joshi450a9a22020-03-31 16:00:22 -0700558 if (seq_params->monochrome) {
559 seq_params->subsampling_x = 1;
560 seq_params->subsampling_y = 1;
561 } else if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
562 seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
563 seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
564 seq_params->subsampling_x = 0;
565 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -0700566 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700567 if (seq_params->profile == 0) {
568 seq_params->subsampling_x = 1;
569 seq_params->subsampling_y = 1;
570 } else if (seq_params->profile == 1) {
571 seq_params->subsampling_x = 0;
572 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -0700573 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700574 if (seq_params->bit_depth == AOM_BITS_12) {
Vishesh4342d7f2020-06-29 18:49:06 +0530575 seq_params->subsampling_x = oxcf->input_cfg.chroma_subsampling_x;
576 seq_params->subsampling_y = oxcf->input_cfg.chroma_subsampling_y;
Tom Fineganf8d6a162018-08-21 10:47:55 -0700577 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700578 seq_params->subsampling_x = 1;
579 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -0700580 }
581 }
Tom Finegan02b2a842018-08-24 13:50:00 -0700582 }
583
Vishesh2b8e4792020-06-12 12:11:19 +0530584 cm->width = oxcf->frm_dim_cfg.width;
585 cm->height = oxcf->frm_dim_cfg.height;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700586 set_sb_size(seq_params,
Satish Kumar Suman36adf352020-06-26 11:20:10 +0530587 av1_select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -0700588 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -0700589
Satish Kumar Suman36adf352020-06-26 11:20:10 +0530590 av1_update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700591
592 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -0700593 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594
Marco Paniconi882c5ad2019-09-30 21:32:29 -0700595 // Set init SVC parameters.
Marco Paniconi67142112019-07-24 15:00:31 -0700596 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -0700597 cpi->svc.external_ref_frame_config = 0;
598 cpi->svc.non_reference_frame = 0;
Marco Paniconi57247a82020-04-13 09:19:07 -0700599 cpi->svc.number_spatial_layers = 1;
600 cpi->svc.number_temporal_layers = 1;
Marco Paniconi67142112019-07-24 15:00:31 -0700601 cm->number_spatial_layers = 1;
602 cm->number_temporal_layers = 1;
Marco Paniconi882c5ad2019-09-30 21:32:29 -0700603 cm->spatial_layer_id = 0;
604 cm->temporal_layer_id = 0;
Marco Paniconi67142112019-07-24 15:00:31 -0700605
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -0700607 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609 cpi->ref_frame_flags = 0;
610
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700611 // Reset resize pending flags
Jayasanker J0b1dd292020-04-09 15:47:25 +0530612 resize_pending_params->width = 0;
613 resize_pending_params->height = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700614
Vishesh2260a592020-04-06 15:34:51 +0530615 init_buffer_indices(&cpi->force_intpel_info, cm->remapped_ref_idx);
Fyodor Kyslov34b591f2020-06-24 20:13:58 -0700616
617 av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700618}
619
Debargha Mukherjee55671702020-08-07 17:31:24 -0700620int aom_strcmp(const char *a, const char *b) {
621 if (a == NULL && b == NULL) return 0;
622 if (a == NULL && b != NULL) return -1;
623 if (a != NULL && b == NULL) return 1;
624 return strcmp(a, b);
625}
626
Debargha Mukherjeea4c6f692021-03-16 19:45:46 -0700627#if CONFIG_NEW_INTER_MODES
628static void set_max_drl_bits(struct AV1_COMP *cpi) {
629 AV1_COMMON *const cm = &cpi->common;
630 // Add logic to choose this in the range [MIN_MAX_DRL_BITS, MAX_MAX_DRL_BITS]
631 if (cpi->oxcf.tool_cfg.max_drl_refmvs == 0) {
632 // TODO(any): Implement an auto mode that potentially adapts the parameter
633 // frame to frame. Currently set at a default value.
634 cm->features.max_drl_bits = DEF_MAX_DRL_REFMVS - 1;
635 } else {
636 cm->features.max_drl_bits = cpi->oxcf.tool_cfg.max_drl_refmvs - 1;
637 }
638 assert(cm->features.max_drl_bits >= MIN_MAX_DRL_BITS &&
639 cm->features.max_drl_bits <= MAX_MAX_DRL_BITS);
640}
641#endif // CONFIG_NEW_INTER_MODES
642
Yaowu Xuf883b422016-08-30 14:01:10 -0700643void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
644 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700645 SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700646 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -0700647 MACROBLOCK *const x = &cpi->td.mb;
Vishesh8ac928b2020-04-01 02:36:35 +0530648 AV1LevelParams *const level_params = &cpi->level_params;
Jayasanker J44fdab72020-04-13 20:34:38 +0530649 InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
Jayasanker J24cb9bc2020-04-15 13:43:10 +0530650 RefreshFrameFlagsInfo *const refresh_frame_flags = &cpi->refresh_frame;
Vishesh2b8e4792020-06-12 12:11:19 +0530651 const FrameDimensionCfg *const frm_dim_cfg = &cpi->oxcf.frm_dim_cfg;
Vishesha9082242020-06-17 18:26:09 +0530652 const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
Vishesh52fdeae2020-06-24 14:37:00 +0530653 const ColorCfg *const color_cfg = &oxcf->color_cfg;
Vishesh073fc962020-07-01 17:39:16 +0530654 const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
Mufaddal Chakera48e16492020-05-04 16:07:18 +0530655 // in case of LAP, lag in frames is set according to number of lap buffers
656 // calculated at init time. This stores and restores LAP's lag in frames to
657 // prevent override by new cfg.
658 int lap_lag_in_frames = -1;
659 if (cpi->lap_enabled && cpi->compressor_stage == LAP_STAGE) {
Vishesh5b50e6d2020-06-10 19:20:07 +0530660 lap_lag_in_frames = cpi->oxcf.gf_cfg.lag_in_frames;
Mufaddal Chakera48e16492020-05-04 16:07:18 +0530661 }
662
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700663 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
Visheshaa6a1702020-06-30 19:27:22 +0530664 seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
Vishesh52fdeae2020-06-24 14:37:00 +0530665 seq_params->color_primaries = color_cfg->color_primaries;
666 seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
667 seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
Visheshaa6a1702020-06-30 19:27:22 +0530668 seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
Vishesh073fc962020-07-01 17:39:16 +0530669 seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
Vishesh52fdeae2020-06-24 14:37:00 +0530670 seq_params->color_range = color_cfg->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700671
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700672 assert(IMPLIES(seq_params->profile <= PROFILE_1,
673 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700674
Vishesha9082242020-06-17 18:26:09 +0530675 seq_params->timing_info_present = dec_model_cfg->timing_info_present;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700676 seq_params->timing_info.num_units_in_display_tick =
Vishesha9082242020-06-17 18:26:09 +0530677 dec_model_cfg->timing_info.num_units_in_display_tick;
678 seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700679 seq_params->timing_info.equal_picture_interval =
Vishesha9082242020-06-17 18:26:09 +0530680 dec_model_cfg->timing_info.equal_picture_interval;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700681 seq_params->timing_info.num_ticks_per_picture =
Vishesha9082242020-06-17 18:26:09 +0530682 dec_model_cfg->timing_info.num_ticks_per_picture;
Andrey Norkin795ba872018-03-06 13:24:14 -0800683
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700684 seq_params->display_model_info_present_flag =
Vishesha9082242020-06-17 18:26:09 +0530685 dec_model_cfg->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700686 seq_params->decoder_model_info_present_flag =
Vishesha9082242020-06-17 18:26:09 +0530687 dec_model_cfg->decoder_model_info_present_flag;
688 if (dec_model_cfg->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -0700689 // set the decoder model parameters in schedule mode
Urvang Joshi450a9a22020-03-31 16:00:22 -0700690 seq_params->decoder_model_info.num_units_in_decoding_tick =
Vishesha9082242020-06-17 18:26:09 +0530691 dec_model_cfg->num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -0700692 cm->buffer_removal_time_present = 1;
Urvang Joshi450a9a22020-03-31 16:00:22 -0700693 av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
694 av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
695 } else if (seq_params->timing_info_present &&
696 seq_params->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700697 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -0700698 // set the decoder model parameters in resource availability mode
Urvang Joshi450a9a22020-03-31 16:00:22 -0700699 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -0700700 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -0700701 seq_params->op_params[0].initial_display_delay =
Andrey Norkinc7511de2018-06-22 12:31:06 -0700702 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -0800703 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800704
Satish Kumar Suman36adf352020-06-26 11:20:10 +0530705 av1_update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800706
Yaowu Xuc27fc142016-08-22 16:08:15 -0700707 cpi->oxcf = *oxcf;
Urvang Joshidf265712020-10-05 23:23:09 -0700708 // When user provides superres_mode = AOM_SUPERRES_AUTO, we still initialize
709 // superres mode for current encoding = AOM_SUPERRES_NONE. This is to ensure
710 // that any analysis (e.g. TPL) happening outside the main encoding loop still
711 // happens at full resolution.
712 // This value will later be set appropriately just before main encoding loop.
713 cpi->superres_mode = oxcf->superres_cfg.superres_mode == AOM_SUPERRES_AUTO
714 ? AOM_SUPERRES_NONE
715 : oxcf->superres_cfg.superres_mode; // default
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700716 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -0700717 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718
Vishesh8ac928b2020-04-01 02:36:35 +0530719 memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
720 sizeof(level_params->target_seq_level_idx));
721 level_params->keep_level_stats = 0;
Wan-Teh Changbcacb322019-09-11 14:50:20 -0700722 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Vishesh8ac928b2020-04-01 02:36:35 +0530723 if (level_params->target_seq_level_idx[i] <= SEQ_LEVELS) {
724 level_params->keep_level_stats |= 1u << i;
725 if (!level_params->level_info[i]) {
726 CHECK_MEM_ERROR(cm, level_params->level_info[i],
727 aom_calloc(1, sizeof(*level_params->level_info[i])));
Wan-Teh Changbcacb322019-09-11 14:50:20 -0700728 }
729 }
730 }
731
732 // TODO(huisu@): level targeting currently only works for the 0th operating
733 // point, so scalable coding is not supported yet.
Vishesh8ac928b2020-04-01 02:36:35 +0530734 if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) {
Wan-Teh Changbcacb322019-09-11 14:50:20 -0700735 // Adjust encoder config in order to meet target level.
Vishesh8ac928b2020-04-01 02:36:35 +0530736 config_target_level(cpi, level_params->target_seq_level_idx[0],
737 seq_params->tier[0]);
Wan-Teh Changbcacb322019-09-11 14:50:20 -0700738 }
739
Urvang Joshiade2d322020-09-01 13:26:46 -0700740 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741
Jayasanker J24cb9bc2020-04-15 13:43:10 +0530742 refresh_frame_flags->golden_frame = false;
743 refresh_frame_flags->bwd_ref_frame = false;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744
Visheshaa6a1702020-06-30 19:27:22 +0530745 cm->features.refresh_frame_context =
746 (oxcf->tool_cfg.frame_parallel_decoding_mode)
747 ? REFRESH_FRAME_CONTEXT_DISABLED
748 : REFRESH_FRAME_CONTEXT_BACKWARD;
Vishesh0481a782020-06-11 14:32:46 +0530749 if (oxcf->tile_cfg.enable_large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -0700750 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +0100751
Alex Converse74ad0912017-07-18 10:22:58 -0700752 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -0700753 CHECK_MEM_ERROR(cm, x->palette_buffer,
754 aom_memalign(16, sizeof(*x->palette_buffer)));
755 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700756
Hui Su38711e72019-06-11 10:49:47 -0700757 if (x->comp_rd_buffer.pred0 == NULL) {
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +0530758 alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700759 }
760
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700761 if (x->tmp_conv_dst == NULL) {
762 CHECK_MEM_ERROR(
763 cm, x->tmp_conv_dst,
764 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -0700765 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700766 }
767 for (int i = 0; i < 2; ++i) {
chiyotsai2a897eb2020-04-28 19:22:13 -0700768 if (x->tmp_pred_bufs[i] == NULL) {
769 CHECK_MEM_ERROR(cm, x->tmp_pred_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +0800770 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
chiyotsai2a897eb2020-04-28 19:22:13 -0700771 sizeof(*x->tmp_pred_bufs[i])));
772 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_pred_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700773 }
774 }
775
Marco Paniconi177b8c32020-01-26 20:37:29 -0800776 av1_reset_segment_features(cm);
Fyodor Kyslov64aaa092020-01-24 11:44:24 -0800777
Debargha Mukherjeea4c6f692021-03-16 19:45:46 -0700778#if CONFIG_NEW_INTER_MODES
779 // Add logic to choose this in the range [MIN_MAX_DRL_BITS, MAX_MAX_DRL_BITS]
780 set_max_drl_bits(cpi);
781#endif // CONFIG_NEW_INTER_MODES
782
chiyotsaic666b1f2019-12-20 10:44:58 -0800783 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784
Vishesh073fc962020-07-01 17:39:16 +0530785 set_rc_buffer_sizes(rc, rc_cfg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786
787 // Under a configuration change, where maximum_buffer_size may change,
788 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
790 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791
792 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -0700793 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794
795 // Set absolute upper and lower quality limits
Vishesh073fc962020-07-01 17:39:16 +0530796 rc->worst_quality = rc_cfg->worst_allowed_q;
797 rc->best_quality = rc_cfg->best_allowed_q;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700798
Urvang Joshi6237b882020-03-26 15:02:26 -0700799 cm->features.interp_filter =
Vishesh0481a782020-06-11 14:32:46 +0530800 oxcf->tile_cfg.enable_large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Urvang Joshi6237b882020-03-26 15:02:26 -0700801 cm->features.switchable_motion_mode = 1;
Yue Chen5380cb52018-02-23 15:33:21 -0800802
Vishesh2b8e4792020-06-12 12:11:19 +0530803 if (frm_dim_cfg->render_width > 0 && frm_dim_cfg->render_height > 0) {
804 cm->render_width = frm_dim_cfg->render_width;
805 cm->render_height = frm_dim_cfg->render_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806 } else {
Vishesh2b8e4792020-06-12 12:11:19 +0530807 cm->render_width = frm_dim_cfg->width;
808 cm->render_height = frm_dim_cfg->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700809 }
Vishesh2b8e4792020-06-12 12:11:19 +0530810 cm->width = frm_dim_cfg->width;
811 cm->height = frm_dim_cfg->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700813 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -0800814 // Superblock size should not be updated after the first key frame.
815 if (!cpi->seq_params_locked) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +0530816 set_sb_size(&cm->seq_params, av1_select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -0800817 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
818 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -0800819 }
Dominic Symes917d6c02017-10-11 18:00:52 +0200820
Jayasanker J44fdab72020-04-13 20:34:38 +0530821 if (initial_dimensions->width || sb_size != seq_params->sb_size) {
822 if (cm->width > initial_dimensions->width ||
823 cm->height > initial_dimensions->height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700824 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700825 av1_free_context_buffers(cm);
Yue Cheneb628982019-08-29 15:17:13 -0700826 av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
827 av1_free_sms_tree(&cpi->td);
Mufaddal Chakera6c3b6de2020-04-29 12:27:42 +0530828 av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
829 cpi->td.firstpass_ctx = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -0700830 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700831 realloc_segmentation_maps(cpi);
Jayasanker J44fdab72020-04-13 20:34:38 +0530832 initial_dimensions->width = initial_dimensions->height = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833 }
834 }
835 update_frame_size(cpi);
836
Yaowu Xuc27fc142016-08-22 16:08:15 -0700837 rc->is_src_frame_alt_ref = 0;
838
Vishesh0481a782020-06-11 14:32:46 +0530839 set_tile_info(cm, &cpi->oxcf.tile_cfg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700840
Marco Paniconid8574e32019-08-04 21:30:12 -0700841 if (!cpi->svc.external_ref_frame_config)
Vishesh38c05d72020-04-14 12:19:14 +0530842 cpi->ext_flags.refresh_frame.update_pending = 0;
Vishesha195ca32020-04-07 18:46:20 +0530843 cpi->ext_flags.refresh_frame_context_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700844
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +0000846
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700847 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -0700848 // This should not be called after the first key frame.
849 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700850 seq_params->operating_points_cnt_minus_1 =
Marco Paniconi6da983b2019-09-16 20:12:43 -0700851 (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
852 ? cm->number_spatial_layers * cm->number_temporal_layers - 1
853 : 0;
Marco Paniconi2aa13c42020-06-01 11:18:38 -0700854 av1_init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -0700855 }
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700856
857 if (cpi->use_svc)
Vishesh073fc962020-07-01 17:39:16 +0530858 av1_update_layer_context_change_config(cpi, rc_cfg->target_bandwidth);
Mufaddal Chakera48e16492020-05-04 16:07:18 +0530859
860 // restore the value of lag_in_frame for LAP stage.
861 if (lap_lag_in_frames != -1) {
Vishesh5b50e6d2020-06-10 19:20:07 +0530862 cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
Mufaddal Chakera48e16492020-05-04 16:07:18 +0530863 }
Debargha Mukherjeec22e2c52020-07-07 00:06:58 -0700864
Debargha Mukherjee55671702020-08-07 17:31:24 -0700865 bool subgop_config_changed = false;
866 if (aom_strcmp(cpi->subgop_config_path, oxcf->subgop_config_path)) {
867 aom_free(cpi->subgop_config_path);
Cherma Rajan Ac89a2512020-11-20 12:18:20 +0530868 cpi->subgop_config_path = NULL;
Debargha Mukherjee55671702020-08-07 17:31:24 -0700869 if (oxcf->subgop_config_path != NULL) {
870 cpi->subgop_config_path =
871 (char *)aom_malloc((strlen(oxcf->subgop_config_path) + 1) *
872 sizeof(*oxcf->subgop_config_path));
873 strcpy(cpi->subgop_config_path, oxcf->subgop_config_path);
874 }
875 subgop_config_changed = true;
876 }
877 if (aom_strcmp(cpi->subgop_config_str, oxcf->subgop_config_str)) {
Debargha Mukherjeeda898442020-07-10 14:45:20 -0700878 aom_free(cpi->subgop_config_str);
Cherma Rajan Ac89a2512020-11-20 12:18:20 +0530879 cpi->subgop_config_str = NULL;
Debargha Mukherjeeda898442020-07-10 14:45:20 -0700880 if (oxcf->subgop_config_str != NULL) {
881 cpi->subgop_config_str =
882 (char *)aom_malloc((strlen(oxcf->subgop_config_str) + 1) *
883 sizeof(*oxcf->subgop_config_str));
884 strcpy(cpi->subgop_config_str, oxcf->subgop_config_str);
Debargha Mukherjeeda898442020-07-10 14:45:20 -0700885 }
Debargha Mukherjee55671702020-08-07 17:31:24 -0700886 subgop_config_changed = true;
887 }
888 if (subgop_config_changed && cpi->compressor_stage == ENCODE_STAGE) {
889 av1_init_subgop_config_set(&cpi->subgop_config_set);
890 // Parse config file first
891 av1_process_subgop_config_set_fromfile(cpi->subgop_config_path,
892 &cpi->subgop_config_set);
893 // Parse config string next, which may override config file configs
894 // or append to it.
895 av1_process_subgop_config_set(cpi->subgop_config_str,
896 &cpi->subgop_config_set);
Urvang Joshieca7e6b2021-06-29 15:30:16 -0700897 if (cpi->print_per_frame_stats) {
898 printf("Successfully processed %d subgop configs.\n",
899 cpi->subgop_config_set.num_configs);
900 // Print out the configuration. Note the printed configuration
901 // is in fact in the config file format that can be parsed back.
902 av1_print_subgop_config_set(&cpi->subgop_config_set);
903 }
Debargha Mukherjeeda898442020-07-10 14:45:20 -0700904 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700905}
906
Urvang Joshi31a7aa52020-03-31 13:09:13 -0700907static INLINE void init_frame_info(FRAME_INFO *frame_info,
908 const AV1_COMMON *const cm) {
909 const CommonModeInfoParams *const mi_params = &cm->mi_params;
910 const SequenceHeader *const seq_params = &cm->seq_params;
911 frame_info->frame_width = cm->width;
912 frame_info->frame_height = cm->height;
913 frame_info->mi_cols = mi_params->mi_cols;
914 frame_info->mi_rows = mi_params->mi_rows;
915 frame_info->mb_cols = mi_params->mb_cols;
916 frame_info->mb_rows = mi_params->mb_rows;
917 frame_info->num_mbs = mi_params->MBs;
918 frame_info->bit_depth = seq_params->bit_depth;
919 frame_info->subsampling_x = seq_params->subsampling_x;
920 frame_info->subsampling_y = seq_params->subsampling_y;
921}
922
Aasaipriyabd659e42019-12-13 16:12:13 +0530923AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
Mufaddal Chakera5517b282019-12-13 16:39:56 +0530924 FIRSTPASS_STATS *frame_stats_buf,
Akshata Jadhava49be172019-12-18 00:03:53 +0530925 COMPRESSOR_STAGE stage, int num_lap_buffers,
Mufaddal Chakera412efe22020-01-17 14:35:01 +0530926 int lap_lag_in_frames,
Akshata Jadhava49be172019-12-18 00:03:53 +0530927 STATS_BUFFER_CTX *stats_buf_context) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700928 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
929 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700930
931 if (!cm) return NULL;
932
Yaowu Xuf883b422016-08-30 14:01:10 -0700933 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -0700935 // The jmp_buf is valid only for the duration of the function that calls
936 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
937 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700938 if (setjmp(cm->error.jmp)) {
939 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700940 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941 return 0;
942 }
943
Ryan Leiccc6ea72021-01-06 11:43:56 -0800944#if DEBUG_EXTQUANT
945 cm->fEncCoeffLog = fopen("EncCoeffLog.txt", "wt");
946#endif
947
Yaowu Xuc27fc142016-08-22 16:08:15 -0700948 cm->error.setjmp = 1;
Mufaddal Chakeraed7b1622020-04-06 12:30:23 +0530949 cpi->lap_enabled = num_lap_buffers > 0;
950 cpi->compressor_stage = stage;
chiyotsaia7091f12019-08-09 16:48:27 -0700951
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700952 CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700953 mi_params->free_mi = enc_free_mi;
954 mi_params->setup_mi = enc_setup_mi;
Debargha Mukherjee7118f422020-06-29 15:49:06 -0700955 mi_params->set_mb_mi = (cpi->compressor_stage == LAP_STAGE)
956 ? stat_stage_set_mb_mi
957 : enc_set_mb_mi;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700958
959 mi_params->mi_alloc_bsize = BLOCK_4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960
Angie Chianga5d96c42016-10-21 16:16:56 -0700961 CHECK_MEM_ERROR(cm, cm->fc,
962 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +0000963 CHECK_MEM_ERROR(
964 cm, cm->default_frame_context,
965 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -0700966 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +0000967 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700968
Yaowu Xuc27fc142016-08-22 16:08:15 -0700969 cpi->common.buffer_pool = pool;
970
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +0530971 init_config(cpi, oxcf);
Mufaddal Chakera11f284a2019-12-19 11:38:46 +0530972 if (cpi->compressor_stage == LAP_STAGE) {
Vishesh5b50e6d2020-06-10 19:20:07 +0530973 cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
Mufaddal Chakera11f284a2019-12-19 11:38:46 +0530974 }
Mufaddal Chakeraac828682019-12-13 16:31:42 +0530975
Mufaddal Chakera7a090f22020-07-09 15:09:51 +0530976 cpi->frames_left = cpi->oxcf.input_cfg.limit;
977
Debargha Mukherjee7118f422020-06-29 15:49:06 -0700978 av1_rc_init(&cpi->oxcf, 0, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700979
Aasaipriya196c58a2020-04-23 19:05:45 +0530980 // For two pass and lag_in_frames > 33 in LAP.
981 cpi->rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_2;
982 if (cpi->lap_enabled) {
983 if ((num_lap_buffers <
984 (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)) &&
985 num_lap_buffers >= (MAX_GF_LENGTH_LAP + 3)) {
986 /*
987 * For lag in frames >= 19 and <33, enable scenecut
988 * with limited future frame prediction.
989 */
990 cpi->rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_1;
991 } else if (num_lap_buffers < (MAX_GF_LENGTH_LAP + 3)) {
992 // Disable scenecut when lag_in_frames < 19.
993 cpi->rc.enable_scenecut_detection = DISABLE_SCENECUT;
994 }
995 }
Jingning Han17af7742019-09-17 16:58:03 -0700996 init_frame_info(&cpi->frame_info, cm);
997
David Turnerd2a592e2018-11-16 14:59:31 +0000998 cm->current_frame.frame_number = 0;
liang zhao7aebbd12020-07-03 22:20:34 -0700999 cm->current_frame.key_frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00001000 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07001001 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001002 cpi->partition_search_skippable_frame = 0;
1003 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00001004 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001005 realloc_segmentation_maps(cpi);
1006
Jayasanker J24cb9bc2020-04-15 13:43:10 +05301007 cpi->refresh_frame.alt_ref_frame = false;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001008
1009 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1010#if CONFIG_INTERNAL_STATS
1011 cpi->b_calculate_blockiness = 1;
1012 cpi->b_calculate_consistency = 1;
1013 cpi->total_inconsistency = 0;
1014 cpi->psnr.worst = 100.0;
1015 cpi->worst_ssim = 100.0;
1016
1017 cpi->count = 0;
1018 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08001019#if CONFIG_SPEED_STATS
1020 cpi->tx_search_count = 0;
1021#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001022
1023 if (cpi->b_calculate_psnr) {
1024 cpi->total_sq_error = 0;
1025 cpi->total_samples = 0;
1026 cpi->tot_recode_hits = 0;
1027 cpi->summed_quality = 0;
1028 cpi->summed_weights = 0;
1029 }
1030
1031 cpi->fastssim.worst = 100.0;
1032 cpi->psnrhvs.worst = 100.0;
1033
1034 if (cpi->b_calculate_blockiness) {
1035 cpi->total_blockiness = 0;
1036 cpi->worst_blockiness = 0.0;
1037 }
1038
1039 if (cpi->b_calculate_consistency) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001040 CHECK_MEM_ERROR(
1041 cm, cpi->ssim_vars,
1042 aom_malloc(sizeof(*cpi->ssim_vars) * 4 * cpi->common.mi_params.mi_rows *
1043 cpi->common.mi_params.mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001044 cpi->worst_consistency = 100.0;
1045 }
1046#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08001047#if CONFIG_ENTROPY_STATS
1048 av1_zero(aggregate_fc);
1049#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001050
Deepa K Gfb89ce02020-04-06 13:34:42 +05301051 cpi->time_stamps.first_ever = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001052
Yaowu Xuc27fc142016-08-22 16:08:15 -07001053#ifdef OUTPUT_YUV_REC
1054 yuv_rec_file = fopen("rec.yuv", "wb");
1055#endif
1056
Mufaddal Chakera5517b282019-12-13 16:39:56 +05301057 assert(MAX_LAP_BUFFERS >= MAX_LAG_BUFFERS);
Akshata Jadhav4be65112019-12-18 00:26:25 +05301058 int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
1059 for (int i = 0; i < size; i++)
Aasaipriyabd659e42019-12-13 16:12:13 +05301060 cpi->twopass.frame_stats_arr[i] = &frame_stats_buf[i];
1061
Akshata Jadhava49be172019-12-18 00:03:53 +05301062 cpi->twopass.stats_buf_ctx = stats_buf_context;
1063 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
1064
Aasaipriyaeb417c12020-04-07 12:08:24 +05301065 if (is_stat_consumption_stage(cpi)) {
Debargha Mukherjee0d0aaed2020-07-17 17:47:06 -07001066 av1_init_single_pass_lap(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001067 }
1068
Remya0cce44c2019-08-16 11:57:24 +05301069 int sb_mi_size = av1_get_sb_mi_size(cm);
1070
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +05301071 alloc_obmc_buffers(&cpi->td.mb.obmc_buffer, cm);
Jingning Hand064cf02017-06-01 10:00:39 -07001072
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05301073 CHECK_MEM_ERROR(
1074 cm, cpi->td.mb.inter_modes_info,
1075 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05301076
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301077 for (int x = 0; x < 2; x++)
1078 for (int y = 0; y < 2; y++)
1079 CHECK_MEM_ERROR(
chiyotsai82f36c92020-04-09 16:18:02 -07001080 cm, cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
1081 (uint32_t *)aom_malloc(
1082 AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
1083 sizeof(*cpi->td.mb.intrabc_hash_info.hash_value_buffer[0][0])));
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301084
chiyotsai82f36c92020-04-09 16:18:02 -07001085 cpi->td.mb.intrabc_hash_info.g_crc_initialized = 0;
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301086
Remya0cce44c2019-08-16 11:57:24 +05301087 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
1088 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
1089
David Turner04b70d82019-01-24 15:39:19 +00001090 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
1091 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001092
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07001093 CHECK_MEM_ERROR(cm, cpi->consec_zero_mv,
1094 aom_calloc((mi_params->mi_rows * mi_params->mi_cols) >> 2,
1095 sizeof(*cpi->consec_zero_mv)));
1096
sdengc23c7f12019-06-11 16:56:50 -07001097 {
1098 const int bsize = BLOCK_16X16;
1099 const int w = mi_size_wide[bsize];
1100 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001101 const int num_cols = (mi_params->mi_cols + w - 1) / w;
1102 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengf46a1062019-08-04 18:43:50 -07001103 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
1104 aom_calloc(num_rows * num_cols,
1105 sizeof(*cpi->tpl_rdmult_scaling_factors)));
1106 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
1107 aom_calloc(num_rows * num_cols,
1108 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
1109 }
1110
1111 {
1112 const int bsize = BLOCK_16X16;
1113 const int w = mi_size_wide[bsize];
1114 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001115 const int num_cols = (mi_params->mi_cols + w - 1) / w;
1116 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengc23c7f12019-06-11 16:56:50 -07001117 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
1118 aom_calloc(num_rows * num_cols,
1119 sizeof(*cpi->ssim_rdmult_scaling_factors)));
1120 }
1121
sdeng01959162019-12-20 10:46:24 -08001122#if CONFIG_TUNE_VMAF
1123 {
1124 const int bsize = BLOCK_64X64;
1125 const int w = mi_size_wide[bsize];
1126 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001127 const int num_cols = (mi_params->mi_cols + w - 1) / w;
1128 const int num_rows = (mi_params->mi_rows + h - 1) / h;
Visheshf18766f2020-04-17 10:22:48 +05301129 CHECK_MEM_ERROR(cm, cpi->vmaf_info.rdmult_scaling_factors,
sdeng01959162019-12-20 10:46:24 -08001130 aom_calloc(num_rows * num_cols,
Visheshf18766f2020-04-17 10:22:48 +05301131 sizeof(*cpi->vmaf_info.rdmult_scaling_factors)));
Sai Deng0cbf0282020-09-17 16:25:52 -07001132 for (int i = 0; i < MAX_ARF_LAYERS; i++) {
1133 cpi->vmaf_info.last_frame_unsharp_amount[i] = -1.0;
1134 cpi->vmaf_info.last_frame_ysse[i] = -1.0;
1135 cpi->vmaf_info.last_frame_vmaf[i] = -1.0;
1136 cpi->vmaf_info.best_unsharp_amount[i] = -1.0;
1137 }
Sai Deng48ebb282020-08-13 15:30:50 -07001138 cpi->vmaf_info.original_qindex = -1;
Sai Deng7275a1a2020-08-31 12:06:09 -07001139
1140#if CONFIG_USE_VMAF_RC
1141 cpi->vmaf_info.vmaf_model = NULL;
1142#endif
sdeng01959162019-12-20 10:46:24 -08001143 }
1144#endif
1145
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05301146 if (!is_stat_generation_stage(cpi)) {
Vishesh39d03622020-03-31 15:18:16 +05301147 setup_tpl_buffers(cm, &cpi->tpl_data);
Jingning Hanf83d6812020-02-27 13:08:19 -08001148 }
Yue Chen7cae98f2018-08-24 10:43:16 -07001149
chiyotsai9c484b32019-03-07 16:01:50 -08001150#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08001151 av1_zero(cpi->partition_stats);
1152#endif
1153
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001154#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1155 cpi->fn_ptr[BT].sdf = SDF; \
1156 cpi->fn_ptr[BT].sdaf = SDAF; \
1157 cpi->fn_ptr[BT].vf = VF; \
1158 cpi->fn_ptr[BT].svf = SVF; \
1159 cpi->fn_ptr[BT].svaf = SVAF; \
1160 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1161 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07001162 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001163
Cheng Chenf78632e2017-10-20 15:30:51 -07001164 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001165 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001166 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
1167 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001168
1169 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001170 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001171 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
1172 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07001173
1174 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001175 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001176 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
1177 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07001178
1179 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001180 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001181 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
1182 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07001183
1184 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001185 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001186 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
1187 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07001188
1189 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001190 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001191 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
1192 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001193
Cheng Chenf78632e2017-10-20 15:30:51 -07001194 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
1195 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001196 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
1197 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07001198
1199 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001200 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001201 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
1202 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07001203
1204 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001205 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001206 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
1207 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07001208
1209 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001210 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001211 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
1212 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001213
1214 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001215 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001216 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
1217 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07001218
1219 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001220 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001221 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
1222 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07001223
1224 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001225 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001226 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
1227 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07001228
1229 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
1230 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001231 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
1232 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07001233
1234 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
1235 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001236 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
1237 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07001238
1239 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
1240 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001241 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
1242 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001243
1244 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001245 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001246 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
1247 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07001248
1249 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001250 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001251 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
1252 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07001253
1254 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001255 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001256 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07001257
1258 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001259 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001260 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07001261
1262 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001263 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001264 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07001265
1266 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001267 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001268 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07001269
Yaowu Xuc27fc142016-08-22 16:08:15 -07001270#define OBFP(BT, OSDF, OVF, OSVF) \
1271 cpi->fn_ptr[BT].osdf = OSDF; \
1272 cpi->fn_ptr[BT].ovf = OVF; \
1273 cpi->fn_ptr[BT].osvf = OSVF;
1274
Yaowu Xuf883b422016-08-30 14:01:10 -07001275 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
1276 aom_obmc_sub_pixel_variance128x128)
1277 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
1278 aom_obmc_sub_pixel_variance128x64)
1279 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
1280 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001281 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
1282 aom_obmc_sub_pixel_variance64x64)
1283 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
1284 aom_obmc_sub_pixel_variance64x32)
1285 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
1286 aom_obmc_sub_pixel_variance32x64)
1287 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
1288 aom_obmc_sub_pixel_variance32x32)
1289 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
1290 aom_obmc_sub_pixel_variance32x16)
1291 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
1292 aom_obmc_sub_pixel_variance16x32)
1293 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
1294 aom_obmc_sub_pixel_variance16x16)
1295 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
1296 aom_obmc_sub_pixel_variance16x8)
1297 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
1298 aom_obmc_sub_pixel_variance8x16)
1299 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
1300 aom_obmc_sub_pixel_variance8x8)
1301 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
1302 aom_obmc_sub_pixel_variance4x8)
1303 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
1304 aom_obmc_sub_pixel_variance8x4)
1305 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
1306 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001307 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
1308 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001309 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
1310 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001311 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
1312 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001313 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
1314 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001315 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
1316 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001317 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
1318 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001319
David Barkerf19f35f2017-05-22 16:33:22 +01001320#define MBFP(BT, MCSDF, MCSVF) \
1321 cpi->fn_ptr[BT].msdf = MCSDF; \
1322 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001323
David Barkerf19f35f2017-05-22 16:33:22 +01001324 MBFP(BLOCK_128X128, aom_masked_sad128x128,
1325 aom_masked_sub_pixel_variance128x128)
1326 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
1327 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001328 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
1329 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
1330 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
1331 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
1332 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
1333 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
1334 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
1335 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
1336 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
1337 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
1338 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
1339 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
1340 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001341
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001342 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
1343
1344 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
1345
1346 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
1347
1348 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001349
1350 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
1351
1352 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001353
chiyotsaic814afb2020-08-04 13:12:35 -07001354#define SDSFP(BT, SDSF, SDSX4DF) \
1355 cpi->fn_ptr[BT].sdsf = SDSF; \
1356 cpi->fn_ptr[BT].sdsx4df = SDSX4DF;
1357
1358 SDSFP(BLOCK_128X128, aom_sad_skip_128x128, aom_sad_skip_128x128x4d);
1359 SDSFP(BLOCK_128X64, aom_sad_skip_128x64, aom_sad_skip_128x64x4d);
1360 SDSFP(BLOCK_64X128, aom_sad_skip_64x128, aom_sad_skip_64x128x4d);
1361 SDSFP(BLOCK_64X64, aom_sad_skip_64x64, aom_sad_skip_64x64x4d);
1362 SDSFP(BLOCK_64X32, aom_sad_skip_64x32, aom_sad_skip_64x32x4d);
1363 SDSFP(BLOCK_64X16, aom_sad_skip_64x16, aom_sad_skip_64x16x4d);
1364 SDSFP(BLOCK_32X64, aom_sad_skip_32x64, aom_sad_skip_32x64x4d);
1365 SDSFP(BLOCK_32X32, aom_sad_skip_32x32, aom_sad_skip_32x32x4d);
1366 SDSFP(BLOCK_32X16, aom_sad_skip_32x16, aom_sad_skip_32x16x4d);
1367 SDSFP(BLOCK_32X8, aom_sad_skip_32x8, aom_sad_skip_32x8x4d);
1368
1369 SDSFP(BLOCK_16X64, aom_sad_skip_16x64, aom_sad_skip_16x64x4d);
1370 SDSFP(BLOCK_16X32, aom_sad_skip_16x32, aom_sad_skip_16x32x4d);
1371 SDSFP(BLOCK_16X16, aom_sad_skip_16x16, aom_sad_skip_16x16x4d);
1372 SDSFP(BLOCK_16X8, aom_sad_skip_16x8, aom_sad_skip_16x8x4d);
1373 SDSFP(BLOCK_8X16, aom_sad_skip_8x16, aom_sad_skip_8x16x4d);
1374 SDSFP(BLOCK_8X8, aom_sad_skip_8x8, aom_sad_skip_8x8x4d);
1375 SDSFP(BLOCK_4X16, aom_sad_skip_4x16, aom_sad_skip_4x16x4d);
1376 SDSFP(BLOCK_4X8, aom_sad_skip_4x8, aom_sad_skip_4x8x4d);
chiyotsaic814afb2020-08-04 13:12:35 -07001377 SDSFP(BLOCK_8X32, aom_sad_skip_8x32, aom_sad_skip_8x32x4d);
chiyotsaic814afb2020-08-04 13:12:35 -07001378#undef SDSFP
1379
Yaowu Xuc27fc142016-08-22 16:08:15 -07001380 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001381
Yaowu Xuf883b422016-08-30 14:01:10 -07001382 /* av1_init_quantizer() is first called here. Add check in
1383 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07001384 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07001385 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386 */
Ryan Leiccc6ea72021-01-06 11:43:56 -08001387 av1_init_quantizer(&cm->seq_params, &cpi->enc_quant_dequant_params,
1388 &cm->quant_params);
Urvang Joshi17814622020-03-27 17:26:17 -07001389 av1_qm_init(&cm->quant_params, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001390
Yaowu Xuf883b422016-08-30 14:01:10 -07001391 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07001392 cm->superres_scale_denominator = SCALE_NUMERATOR;
Vishesh2b8e4792020-06-12 12:11:19 +05301393 cm->superres_upscaled_width = oxcf->frm_dim_cfg.width;
1394 cm->superres_upscaled_height = oxcf->frm_dim_cfg.height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001395 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07001396
1397 cm->error.setjmp = 0;
1398
1399 return cpi;
1400}
1401
Urvang Joshiee2c8112018-05-04 14:53:15 -07001402#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001403#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
1404
1405#define SNPRINT2(H, T, V) \
1406 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07001407#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001408
Sachin Kumar Garg700af532020-05-15 10:47:41 +05301409// This function will change the state and free the mutex of corresponding
1410// workers and terminate the object. The object can not be re-used unless a call
1411// to reset() is made.
1412static AOM_INLINE void terminate_worker_data(AV1_COMP *cpi) {
1413 MultiThreadInfo *const mt_info = &cpi->mt_info;
1414 for (int t = mt_info->num_workers - 1; t >= 0; --t) {
1415 AVxWorker *const worker = &mt_info->workers[t];
1416 aom_get_worker_interface()->end(worker);
1417 }
1418}
1419
1420// Deallocate allocated thread_data.
1421static AOM_INLINE void free_thread_data(AV1_COMP *cpi) {
1422 MultiThreadInfo *const mt_info = &cpi->mt_info;
1423 AV1_COMMON *cm = &cpi->common;
1424 for (int t = 0; t < mt_info->num_workers; ++t) {
1425 EncWorkerData *const thread_data = &mt_info->tile_thr_data[t];
1426 aom_free(thread_data->td->tctx);
1427 if (t == 0) continue;
1428 aom_free(thread_data->td->palette_buffer);
1429 aom_free(thread_data->td->tmp_conv_dst);
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +05301430 release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Sachin Kumar Garg700af532020-05-15 10:47:41 +05301431 for (int j = 0; j < 2; ++j) {
1432 aom_free(thread_data->td->tmp_pred_bufs[j]);
1433 }
Satish Kumar Sumand3caa0d2020-06-16 14:02:50 +05301434 release_obmc_buffers(&thread_data->td->obmc_buffer);
Sachin Kumar Garg700af532020-05-15 10:47:41 +05301435 aom_free(thread_data->td->vt64x64);
1436
1437 aom_free(thread_data->td->inter_modes_info);
1438 for (int x = 0; x < 2; x++) {
1439 for (int y = 0; y < 2; y++) {
1440 aom_free(thread_data->td->hash_value_buffer[x][y]);
1441 thread_data->td->hash_value_buffer[x][y] = NULL;
1442 }
1443 }
1444 aom_free(thread_data->td->counts);
1445 aom_free(thread_data->td->mbmi_ext);
1446 av1_free_pmc(thread_data->td->firstpass_ctx, av1_num_planes(cm));
1447 thread_data->td->firstpass_ctx = NULL;
1448 av1_free_shared_coeff_buffer(&thread_data->td->shared_coeff_buf);
1449 av1_free_sms_tree(thread_data->td);
1450 aom_free(thread_data->td);
1451 }
1452}
1453
Yaowu Xuf883b422016-08-30 14:01:10 -07001454void av1_remove_compressor(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001455 if (!cpi) return;
1456
Yunqing Wang82f140a2020-06-03 18:40:50 -07001457 AV1_COMMON *cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00001458 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08001459#if CONFIG_ENTROPY_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05301460 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08001461 fprintf(stderr, "Writing counts.stt\n");
1462 FILE *f = fopen("counts.stt", "wb");
1463 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
1464 fclose(f);
1465 }
1466#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001467#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001468 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07001469
Mufaddal Chakerae7326122019-12-04 14:49:09 +05301470 if (!is_stat_generation_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001471 char headings[512] = { 0 };
1472 char results[512] = { 0 };
1473 FILE *f = fopen("opsnr.stt", "a");
1474 double time_encoded =
Deepa K Gfb89ce02020-04-06 13:34:42 +05301475 (cpi->time_stamps.prev_end_seen - cpi->time_stamps.first_ever) /
Yaowu Xuc27fc142016-08-22 16:08:15 -07001476 10000000.000;
1477 double total_encode_time =
1478 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
1479 const double dr =
1480 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
Vishesh4342d7f2020-06-29 18:49:06 +05301481 const double peak =
1482 (double)((1 << cpi->oxcf.input_cfg.input_bit_depth) - 1);
Vishesh073fc962020-07-01 17:39:16 +05301483 const double target_rate =
1484 (double)cpi->oxcf.rc_cfg.target_bandwidth / 1000;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001485 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
1486
1487 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001488 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001489 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
1490 const double total_ssim =
1491 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
1492 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08001493 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07001494 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08001495 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07001496 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07001497 snprintf(results, sizeof(results),
1498 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
1499 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08001500 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07001501 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07001502 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
1503 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
1504 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
1505 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08001506 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07001507 cpi->psnr.stat[STAT_Y] / cpi->count,
1508 cpi->psnr.stat[STAT_U] / cpi->count,
Yue Chenf0652ed2019-08-13 16:09:25 -07001509 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001510
1511 if (cpi->b_calculate_blockiness) {
1512 SNPRINT(headings, "\t Block\tWstBlck");
1513 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
1514 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
1515 }
1516
1517 if (cpi->b_calculate_consistency) {
1518 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07001519 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001520 (double)cpi->total_inconsistency);
1521
1522 SNPRINT(headings, "\tConsist\tWstCons");
1523 SNPRINT2(results, "\t%7.3f", consistency);
1524 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
1525 }
Yue Chenf0652ed2019-08-13 16:09:25 -07001526
1527 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
1528 SNPRINT2(results, "\t%8.0f", total_encode_time);
1529 SNPRINT2(results, "\t%7.2f", rate_err);
1530 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
1531
1532 fprintf(f, "%s\tAPsnr611\n", headings);
1533 fprintf(f, "%s\t%7.3f\n", results,
1534 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
1535 cpi->psnr.stat[STAT_V]) /
1536 (cpi->count * 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001537 }
1538
1539 fclose(f);
1540 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07001541#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08001542#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05301543 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08001544 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
1545 }
1546#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08001547
chiyotsai9c484b32019-03-07 16:01:50 -08001548#if CONFIG_COLLECT_PARTITION_STATS == 2
Mufaddal Chakerae7326122019-12-04 14:49:09 +05301549 if (!is_stat_generation_stage(cpi)) {
chiyotsai92ed0dd2019-01-25 14:50:14 -08001550 av1_print_partition_stats(&cpi->partition_stats);
1551 }
1552#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001553 }
1554
Yunqing Wang82f140a2020-06-03 18:40:50 -07001555 TplParams *const tpl_data = &cpi->tpl_data;
Jingning Hanf83d6812020-02-27 13:08:19 -08001556 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
Vishesh39d03622020-03-31 15:18:16 +05301557 aom_free(tpl_data->tpl_stats_pool[frame]);
1558 aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]);
Yue Chen7cae98f2018-08-24 10:43:16 -07001559 }
1560
Mufaddal Chakera91579252020-05-18 17:41:46 +05301561 if (cpi->compressor_stage != LAP_STAGE) {
1562 terminate_worker_data(cpi);
1563 free_thread_data(cpi);
1564 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001565
Yunqing Wang82f140a2020-06-03 18:40:50 -07001566 MultiThreadInfo *const mt_info = &cpi->mt_info;
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05301567#if CONFIG_MULTITHREAD
Yunqing Wang82f140a2020-06-03 18:40:50 -07001568 pthread_mutex_t *const enc_row_mt_mutex_ = mt_info->enc_row_mt.mutex_;
1569 pthread_mutex_t *const gm_mt_mutex_ = mt_info->gm_sync.mutex_;
Deepa K G74de2a02020-04-11 13:09:11 +05301570 if (enc_row_mt_mutex_ != NULL) {
1571 pthread_mutex_destroy(enc_row_mt_mutex_);
1572 aom_free(enc_row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05301573 }
Remya1a090d52020-05-04 11:52:10 +05301574 if (gm_mt_mutex_ != NULL) {
1575 pthread_mutex_destroy(gm_mt_mutex_);
1576 aom_free(gm_mt_mutex_);
1577 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05301578#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05301579 av1_row_mt_mem_dealloc(cpi);
Mufaddal Chakera91579252020-05-18 17:41:46 +05301580 if (cpi->compressor_stage != LAP_STAGE) {
1581 aom_free(mt_info->tile_thr_data);
1582 aom_free(mt_info->workers);
1583 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001584
Sachin Kumar Garg194c57b2020-05-06 08:50:13 +05301585 av1_tpl_dealloc(&tpl_data->tpl_mt_sync);
Deepa K G74de2a02020-04-11 13:09:11 +05301586 if (mt_info->num_workers > 1) {
1587 av1_loop_filter_dealloc(&mt_info->lf_row_sync);
1588 av1_loop_restoration_dealloc(&mt_info->lr_row_sync, mt_info->num_workers);
Remya1a090d52020-05-04 11:52:10 +05301589 av1_gm_dealloc(&mt_info->gm_sync);
Deepa K G964e72e2018-05-16 16:56:01 +05301590 }
1591
Yaowu Xuc27fc142016-08-22 16:08:15 -07001592 dealloc_compressor_data(cpi);
1593
Debargha Mukherjee5d157212017-01-10 14:44:47 -08001594#if CONFIG_INTERNAL_STATS
1595 aom_free(cpi->ssim_vars);
1596 cpi->ssim_vars = NULL;
1597#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001598
Yaowu Xuf883b422016-08-30 14:01:10 -07001599 av1_remove_common(cm);
1600 av1_free_ref_frame_buffers(cm->buffer_pool);
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05301601
Ryan Leiccc6ea72021-01-06 11:43:56 -08001602#if DEBUG_EXTQUANT
1603 if (cpi->common.fEncCoeffLog != NULL) {
1604 fclose(cpi->common.fEncCoeffLog);
1605 }
1606#endif
1607
Debargha Mukherjeeda898442020-07-10 14:45:20 -07001608 aom_free(cpi->subgop_config_str);
Debargha Mukherjee55671702020-08-07 17:31:24 -07001609 aom_free(cpi->subgop_config_path);
Yaowu Xuf883b422016-08-30 14:01:10 -07001610 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001611
Yaowu Xuc27fc142016-08-22 16:08:15 -07001612#ifdef OUTPUT_YUV_REC
1613 fclose(yuv_rec_file);
1614#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001615}
1616
Yaowu Xuf883b422016-08-30 14:01:10 -07001617static void generate_psnr_packet(AV1_COMP *cpi) {
1618 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001619 int i;
1620 PSNR_STATS psnr;
Vishesh4342d7f2020-06-29 18:49:06 +05301621 const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
Yaowu Xue75b58a2020-01-03 12:56:12 -08001622 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
1623 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
1624 bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001625
1626 for (i = 0; i < 4; ++i) {
1627 pkt.data.psnr.samples[i] = psnr.samples[i];
1628 pkt.data.psnr.sse[i] = psnr.sse[i];
1629 pkt.data.psnr.psnr[i] = psnr.psnr[i];
1630 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001631 pkt.kind = AOM_CODEC_PSNR_PKT;
1632 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001633}
1634
Vishesha195ca32020-04-07 18:46:20 +05301635int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001636 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
1637
Vishesha195ca32020-04-07 18:46:20 +05301638 *ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001639 return 0;
1640}
1641
Thomas Daede497d1952017-08-08 17:33:06 -07001642int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1643 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001644 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07001645 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001646 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001647 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001648 return 0;
1649 } else {
1650 return -1;
1651 }
1652}
1653
Thomas Daede497d1952017-08-08 17:33:06 -07001654int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1655 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001656 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07001657 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07001658 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001659 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07001660 return 0;
1661 } else {
1662 return -1;
1663 }
1664}
1665
Yaowu Xuc27fc142016-08-22 16:08:15 -07001666#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07001667void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001668 uint8_t *src = s->y_buffer;
1669 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07001670 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001671 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
1672 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
1673
1674 do {
1675 fwrite(src16, s->y_width, 2, yuv_rec_file);
1676 src16 += s->y_stride;
1677 } while (--h);
1678
1679 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
1680 h = s->uv_height;
1681
1682 do {
1683 fwrite(src16, s->uv_width, 2, yuv_rec_file);
1684 src16 += s->uv_stride;
1685 } while (--h);
1686
1687 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
1688 h = s->uv_height;
1689
1690 do {
1691 fwrite(src16, s->uv_width, 2, yuv_rec_file);
1692 src16 += s->uv_stride;
1693 } while (--h);
1694
1695 fflush(yuv_rec_file);
1696 return;
1697 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001698
1699 do {
1700 fwrite(src, s->y_width, 1, yuv_rec_file);
1701 src += s->y_stride;
1702 } while (--h);
1703
1704 src = s->u_buffer;
1705 h = s->uv_height;
1706
1707 do {
1708 fwrite(src, s->uv_width, 1, yuv_rec_file);
1709 src += s->uv_stride;
1710 } while (--h);
1711
1712 src = s->v_buffer;
1713 h = s->uv_height;
1714
1715 do {
1716 fwrite(src, s->uv_width, 1, yuv_rec_file);
1717 src += s->uv_stride;
1718 } while (--h);
1719
1720 fflush(yuv_rec_file);
1721}
1722#endif // OUTPUT_YUV_REC
1723
Yaowu Xuf883b422016-08-30 14:01:10 -07001724static void set_mv_search_params(AV1_COMP *cpi) {
1725 const AV1_COMMON *const cm = &cpi->common;
Nithya V Sace29f32020-04-07 16:15:12 +05301726 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
Yunqing Wang4b7bf402020-01-28 15:20:53 -08001727 const int max_mv_def = AOMMAX(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001728
1729 // Default based on max resolution.
Nithya V Sace29f32020-04-07 16:15:12 +05301730 mv_search_params->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001731
chiyotsai8cc054a2019-12-12 14:57:43 -08001732 if (cpi->sf.mv_sf.auto_mv_step_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001733 if (frame_is_intra_only(cm)) {
1734 // Initialize max_mv_magnitude for use in the first INTER frame
1735 // after a key/intra-only frame.
Nithya V Sace29f32020-04-07 16:15:12 +05301736 mv_search_params->max_mv_magnitude = max_mv_def;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001737 } else {
Yunqing Wang4b7bf402020-01-28 15:20:53 -08001738 // Use cpi->max_mv_magnitude == -1 to exclude first pass case.
Nithya V Sace29f32020-04-07 16:15:12 +05301739 if (cm->show_frame && mv_search_params->max_mv_magnitude != -1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001740 // Allow mv_steps to correspond to twice the max mv magnitude found
1741 // in the previous frame, capped by the default max_mv_magnitude based
1742 // on resolution.
Nithya V Sace29f32020-04-07 16:15:12 +05301743 mv_search_params->mv_step_param = av1_init_search_range(
1744 AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001745 }
Nithya V Sace29f32020-04-07 16:15:12 +05301746 mv_search_params->max_mv_magnitude = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001747 }
1748 }
1749}
1750
Yaowu Xubca61902020-04-08 08:41:47 -07001751void av1_set_screen_content_options(const AV1_COMP *cpi,
1752 FeatureFlags *features) {
Urvang Joshib6409e92020-03-23 11:23:27 -07001753 const AV1_COMMON *const cm = &cpi->common;
Hui Subdf0c992019-02-14 14:52:41 -08001754
1755 if (cm->seq_params.force_screen_content_tools != 2) {
Urvang Joshib6409e92020-03-23 11:23:27 -07001756 features->allow_screen_content_tools = features->allow_intrabc =
Hui Subdf0c992019-02-14 14:52:41 -08001757 cm->seq_params.force_screen_content_tools;
1758 return;
1759 }
1760
Vishesh94a65292020-07-01 15:28:53 +05301761 if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
Urvang Joshib6409e92020-03-23 11:23:27 -07001762 features->allow_screen_content_tools = features->allow_intrabc = 1;
Hui Subdf0c992019-02-14 14:52:41 -08001763 return;
1764 }
1765
1766 // Estimate if the source frame is screen content, based on the portion of
1767 // blocks that have few luma colors.
Cheng Chen4f666ca2019-11-18 17:05:03 -08001768 const uint8_t *src = cpi->unfiltered_source->y_buffer;
Hui Subdf0c992019-02-14 14:52:41 -08001769 assert(src != NULL);
Cheng Chen4f666ca2019-11-18 17:05:03 -08001770 const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
1771 const int stride = cpi->unfiltered_source->y_stride;
1772 const int width = cpi->unfiltered_source->y_width;
1773 const int height = cpi->unfiltered_source->y_height;
Hui Subdf0c992019-02-14 14:52:41 -08001774 const int bd = cm->seq_params.bit_depth;
1775 const int blk_w = 16;
1776 const int blk_h = 16;
1777 // These threshold values are selected experimentally.
1778 const int color_thresh = 4;
1779 const unsigned int var_thresh = 0;
1780 // Counts of blocks with no more than color_thresh colors.
1781 int counts_1 = 0;
1782 // Counts of blocks with no more than color_thresh colors and variance larger
1783 // than var_thresh.
1784 int counts_2 = 0;
1785
1786 for (int r = 0; r + blk_h <= height; r += blk_h) {
1787 for (int c = 0; c + blk_w <= width; c += blk_w) {
Aasaipriya Chandranfa3ecee2020-09-22 12:46:17 +05301788 int count_buf[1 << 8]; // Maximum (1 << 8) bins for hbd path.
Hui Subdf0c992019-02-14 14:52:41 -08001789 const uint8_t *const this_src = src + r * stride + c;
Aasaipriya chandran7c962c22020-10-08 12:37:42 +05301790 int n_colors;
1791 if (use_hbd)
1792 av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd, NULL,
1793 count_buf, &n_colors, NULL);
1794 else
1795 av1_count_colors(this_src, stride, blk_w, blk_h, count_buf, &n_colors);
Hui Subdf0c992019-02-14 14:52:41 -08001796 if (n_colors > 1 && n_colors <= color_thresh) {
1797 ++counts_1;
1798 struct buf_2d buf;
1799 buf.stride = stride;
1800 buf.buf = (uint8_t *)this_src;
1801 const unsigned int var =
1802 use_hbd
1803 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
1804 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
1805 if (var > var_thresh) ++counts_2;
1806 }
1807 }
1808 }
1809
1810 // The threshold values are selected experimentally.
Urvang Joshib6409e92020-03-23 11:23:27 -07001811 features->allow_screen_content_tools =
Hui Subdf0c992019-02-14 14:52:41 -08001812 counts_1 * blk_h * blk_w * 10 > width * height;
1813 // IntraBC would force loop filters off, so we use more strict rules that also
1814 // requires that the block has high variance.
Urvang Joshib6409e92020-03-23 11:23:27 -07001815 features->allow_intrabc = features->allow_screen_content_tools &&
1816 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08001817}
1818
venkat sanampudi14affd02020-07-23 07:41:05 +05301819// Function pointer to search site config initialization
1820// of different search method functions.
1821typedef void (*av1_init_search_site_config)(search_site_config *cfg,
1822 int stride);
1823
1824av1_init_search_site_config
1825 av1_init_motion_compensation[NUM_DISTINCT_SEARCH_METHODS] = {
1826 av1_init_dsmotion_compensation, av1_init_motion_compensation_nstep,
1827 av1_init_motion_compensation_hex, av1_init_motion_compensation_bigdia,
1828 av1_init_motion_compensation_square
1829 };
1830
Yaowu Xuf883b422016-08-30 14:01:10 -07001831static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08001832 AV1_COMMON *const cm = &cpi->common;
Nithya V Sace29f32020-04-07 16:15:12 +05301833 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
chiyotsai836b69b2019-04-09 13:41:24 -07001834 const int y_stride = cpi->scaled_source.y_stride;
Vishesh2b8e4792020-06-12 12:11:19 +05301835 const int y_stride_src = ((cpi->oxcf.frm_dim_cfg.width != cm->width ||
1836 cpi->oxcf.frm_dim_cfg.height != cm->height) ||
1837 av1_superres_scaled(cm))
1838 ? y_stride
1839 : cpi->lookahead->buf->img.y_stride;
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05301840 int fpf_y_stride = cm->cur_frame != NULL ? cm->cur_frame->buf.y_stride
1841 : cpi->scaled_source.y_stride;
1842
Cheng Chencaf33512020-04-21 14:54:57 -07001843 // Update if search_site_cfg is uninitialized or the current frame has a new
1844 // stride
Nithya V Sace29f32020-04-07 16:15:12 +05301845 const int should_update =
venkat sanampudi14affd02020-07-23 07:41:05 +05301846 !mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride ||
1847 !mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][DIAMOND].stride ||
1848 (y_stride !=
1849 mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001850
chiyotsai836b69b2019-04-09 13:41:24 -07001851 if (!should_update) {
1852 return;
1853 }
1854
venkat sanampudi14affd02020-07-23 07:41:05 +05301855 // Initialization of search_site_cfg for NUM_DISTINCT_SEARCH_METHODS.
1856 for (SEARCH_METHODS i = DIAMOND; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
1857 av1_init_motion_compensation[i](
1858 &mv_search_params->search_site_cfg[SS_CFG_SRC][i], y_stride);
1859 av1_init_motion_compensation[i](
1860 &mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][i], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001861 }
venkat sanampudidc5371c2020-07-10 09:36:01 +05301862
venkat sanampudi14affd02020-07-23 07:41:05 +05301863 // First pass search site config initialization.
1864 av1_init_motion_fpf(&mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
Cheng Chencaf33512020-04-21 14:54:57 -07001865 fpf_y_stride);
venkat sanampudi14affd02020-07-23 07:41:05 +05301866 for (SEARCH_METHODS i = NSTEP; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
1867 memcpy(&mv_search_params->search_site_cfg[SS_CFG_FPF][i],
1868 &mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
1869 sizeof(search_site_config));
venkat sanampudidc5371c2020-07-10 09:36:01 +05301870 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001871}
1872
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07001873#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001874static void set_restoration_unit_size(int width, int height, int sx, int sy,
1875 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001876 (void)width;
1877 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07001878 (void)sx;
1879 (void)sy;
1880#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
1881 int s = AOMMIN(sx, sy);
1882#else
1883 int s = 0;
1884#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
1885
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07001886 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08001887 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07001888 else
Urvang Joshi813186b2018-03-08 15:38:46 -08001889 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001890 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
1891 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001892}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001893
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301894static void init_ref_frame_bufs(AV1_COMP *cpi) {
1895 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07001896 int i;
1897 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00001898 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07001899 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00001900 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07001901 }
1902 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07001903 pool->frame_bufs[i].ref_count = 0;
1904 }
Cheng Chen46f30c72017-09-07 11:13:33 -07001905}
1906
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05301907void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
1908 int subsampling_x, int subsampling_y) {
Cheng Chen46f30c72017-09-07 11:13:33 -07001909 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001910 SequenceHeader *const seq_params = &cm->seq_params;
Jayasanker J44fdab72020-04-13 20:34:38 +05301911 InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
Cheng Chen46f30c72017-09-07 11:13:33 -07001912
Jayasanker J44fdab72020-04-13 20:34:38 +05301913 if (!initial_dimensions->width ||
1914 seq_params->use_highbitdepth != use_highbitdepth ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001915 seq_params->subsampling_x != subsampling_x ||
1916 seq_params->subsampling_y != subsampling_y) {
1917 seq_params->subsampling_x = subsampling_x;
1918 seq_params->subsampling_y = subsampling_y;
1919 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07001920
Jingning Han183b2a82019-12-18 16:03:41 -08001921 av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
1922 av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
1923
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05301924 if (!is_stat_generation_stage(cpi)) {
1925 alloc_altref_frame_buffer(cpi);
1926 alloc_util_frame_buffers(cpi);
1927 }
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05301928 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07001929
1930 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
1931
Jayasanker J44fdab72020-04-13 20:34:38 +05301932 initial_dimensions->width = cm->width;
1933 initial_dimensions->height = cm->height;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001934 cpi->initial_mbs = cm->mi_params.MBs;
Cheng Chen46f30c72017-09-07 11:13:33 -07001935 }
1936}
1937
1938// Returns 1 if the assigned width or height was <= 0.
Marco Paniconi63971322019-08-15 21:32:05 -07001939int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Cheng Chen46f30c72017-09-07 11:13:33 -07001940 AV1_COMMON *cm = &cpi->common;
Jayasanker J44fdab72020-04-13 20:34:38 +05301941 InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05301942 av1_check_initial_width(cpi, cm->seq_params.use_highbitdepth,
1943 cm->seq_params.subsampling_x,
1944 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07001945
1946 if (width <= 0 || height <= 0) return 1;
1947
1948 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07001949 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001950
Jayasanker J44fdab72020-04-13 20:34:38 +05301951 if (initial_dimensions->width && initial_dimensions->height &&
1952 (cm->width > initial_dimensions->width ||
1953 cm->height > initial_dimensions->height)) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001954 av1_free_context_buffers(cm);
Yue Cheneb628982019-08-29 15:17:13 -07001955 av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
1956 av1_free_sms_tree(&cpi->td);
Mufaddal Chakera6c3b6de2020-04-29 12:27:42 +05301957 av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
1958 cpi->td.firstpass_ctx = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001959 alloc_compressor_data(cpi);
1960 realloc_segmentation_maps(cpi);
Jayasanker J44fdab72020-04-13 20:34:38 +05301961 initial_dimensions->width = initial_dimensions->height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07001962 }
Cheng Chen46f30c72017-09-07 11:13:33 -07001963 update_frame_size(cpi);
1964
1965 return 0;
1966}
1967
David Turner475a3132019-01-18 15:17:17 +00001968void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07001969 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001970 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001971 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07001972 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001973 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001974
Fergus Simpsonbc189932017-05-16 17:02:39 -07001975 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07001976 // There has been a change in the encoded frame size
Marco Paniconi63971322019-08-15 21:32:05 -07001977 av1_set_size_literal(cpi, width, height);
Urvang Joshic8b52d52018-03-23 13:16:51 -07001978 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Urvang Joshib6409e92020-03-23 11:23:27 -07001979 cm->features.all_lossless =
1980 cm->features.coded_lossless && !av1_superres_scaled(cm);
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07001981
1982 av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001983 }
Yunqing Wang4b7bf402020-01-28 15:20:53 -08001984 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001985
Hamsalekha S37cc1d12019-12-12 19:27:41 +05301986 if (is_stat_consumption_stage(cpi)) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001987 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988 }
1989
David Turnere7ebf902018-12-04 14:04:55 +00001990 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001991
Cherma Rajan A71d20db2018-04-27 11:15:32 +05301992 // Allocate above context buffers
Urvang Joshi5c8625a2020-03-30 13:16:37 -07001993 CommonContexts *const above_contexts = &cm->above_contexts;
1994 if (above_contexts->num_planes < av1_num_planes(cm) ||
1995 above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
1996 above_contexts->num_tile_rows < cm->tiles.rows) {
1997 av1_free_above_context_buffers(above_contexts);
1998 if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
1999 cm->mi_params.mi_cols,
2000 av1_num_planes(cm)))
Cherma Rajan A71d20db2018-04-27 11:15:32 +05302001 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
2002 "Failed to allocate context buffers");
2003 }
2004
Yaowu Xuc27fc142016-08-22 16:08:15 -07002005 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002006 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00002007 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
2008 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07002009 cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
2010 NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07002011 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002012 "Failed to allocate frame buffer");
2013
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01002014 const int frame_width = cm->superres_upscaled_width;
2015 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002016 set_restoration_unit_size(frame_width, frame_height,
2017 seq_params->subsampling_x,
2018 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002019 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01002020 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01002021
2022 av1_alloc_restoration_buffers(cm);
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05302023 if (!is_stat_generation_stage(cpi)) alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002024 init_motion_estimation(cpi);
2025
2026 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00002027 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00002028 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00002029 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
2030 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00002031 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08002032 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00002033 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002034 }
2035 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07002036
Hui Su5ebd8702018-01-08 18:09:20 -08002037 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08002038 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002039
2040 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2041}
2042
Yue Chen2d8405c2020-06-10 14:42:05 -07002043/*!\brief Select and apply cdef filters and switchable restoration filters
2044 *
2045 * \ingroup high_level_algo
Yue Chen2d8405c2020-06-10 14:42:05 -07002046 */
Yue Chen92271e12019-07-09 16:28:02 -07002047static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
2048 MACROBLOCKD *xd, int use_restoration,
2049 int use_cdef) {
yixindu368bde52021-06-04 18:30:53 -07002050#if CONFIG_CCSO
2051 uint16_t *rec_uv[2];
2052 uint16_t *org_uv[2];
2053 uint16_t *ext_rec_y;
2054 uint8_t *ref_buffer;
2055 const YV12_BUFFER_CONFIG *ref = cpi->source;
2056 int ref_stride;
2057 const int use_ccso = !cm->features.coded_lossless && !cm->tiles.large_scale &&
2058 cm->seq_params.enable_ccso;
2059 const int num_planes = av1_num_planes(cm);
2060 av1_setup_dst_planes(xd->plane, cm->seq_params.sb_size, &cm->cur_frame->buf,
2061 0, 0, 0, num_planes);
2062 const int ccso_stride = xd->plane[0].dst.width;
2063 const int ccso_stride_ext = xd->plane[0].dst.width + (CCSO_PADDING_SIZE << 1);
2064 for (int pli = 0; pli < 2; pli++) {
2065 rec_uv[pli] =
2066 aom_malloc(sizeof(*rec_uv) * xd->plane[0].dst.height * ccso_stride);
2067 org_uv[pli] =
2068 aom_malloc(sizeof(*org_uv) * xd->plane[0].dst.height * ccso_stride);
2069 }
2070 if (use_ccso) {
2071 ext_rec_y =
2072 aom_malloc(sizeof(*ext_rec_y) *
2073 (xd->plane[0].dst.height + (CCSO_PADDING_SIZE << 1)) *
2074 (xd->plane[0].dst.width + (CCSO_PADDING_SIZE << 1)));
2075 for (int pli = 0; pli < 1; pli++) {
2076 const int pic_height = xd->plane[pli].dst.height;
2077 const int pic_width = xd->plane[pli].dst.width;
2078 const int dst_stride = xd->plane[pli].dst.stride;
2079 for (int r = 0; r < pic_height; ++r) {
2080 for (int c = 0; c < pic_width; ++c) {
2081 if (cm->seq_params.use_highbitdepth) {
2082 if (pli == 0)
2083 ext_rec_y[(r + CCSO_PADDING_SIZE) * ccso_stride_ext + c +
2084 CCSO_PADDING_SIZE] =
2085 CONVERT_TO_SHORTPTR(
2086 xd->plane[pli].dst.buf)[r * dst_stride + c];
2087 } else {
2088 if (pli == 0)
2089 ext_rec_y[(r + CCSO_PADDING_SIZE) * ccso_stride_ext + c +
2090 CCSO_PADDING_SIZE] =
2091 xd->plane[pli].dst.buf[r * dst_stride + c];
2092 }
2093 }
2094 }
2095 }
2096 extend_ccso_border(ext_rec_y, CCSO_PADDING_SIZE, xd);
2097 }
2098#endif
2099
Deepa K G74de2a02020-04-11 13:09:11 +05302100 MultiThreadInfo *const mt_info = &cpi->mt_info;
2101 const int num_workers = mt_info->num_workers;
logangwf95c9162019-02-20 12:02:32 -08002102 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00002103 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02002104
logangwf95c9162019-02-20 12:02:32 -08002105 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002106#if CONFIG_COLLECT_COMPONENT_TIMING
2107 start_timing(cpi, cdef_time);
2108#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02002109 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00002110 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
chiyotsaid2f7b412019-12-18 10:16:42 -08002111 cpi->sf.lpf_sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002112
2113 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00002114 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002115#if CONFIG_COLLECT_COMPONENT_TIMING
2116 end_timing(cpi, cdef_time);
2117#endif
logangwf95c9162019-02-20 12:02:32 -08002118 } else {
2119 cm->cdef_info.cdef_bits = 0;
2120 cm->cdef_info.cdef_strengths[0] = 0;
2121 cm->cdef_info.nb_cdef_strengths = 1;
2122 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002123 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002124
yixindu368bde52021-06-04 18:30:53 -07002125#if CONFIG_CCSO
2126 if (use_ccso) {
2127 av1_setup_dst_planes(xd->plane, cm->seq_params.sb_size, &cm->cur_frame->buf,
2128 0, 0, 0, num_planes);
2129 // Reading original and reconstructed chroma samples as input
2130 for (int pli = 1; pli < 3; pli++) {
2131 const int pic_height = xd->plane[pli].dst.height;
2132 const int pic_width = xd->plane[pli].dst.width;
2133 const int dst_stride = xd->plane[pli].dst.stride;
2134 switch (pli) {
2135 case 1:
2136 ref_buffer = ref->u_buffer;
2137 ref_stride = ref->uv_stride;
2138 break;
2139 case 2:
2140 ref_buffer = ref->v_buffer;
2141 ref_stride = ref->uv_stride;
2142 break;
2143 default: ref_stride = 0;
2144 }
2145 for (int r = 0; r < pic_height; ++r) {
2146 for (int c = 0; c < pic_width; ++c) {
2147 if (cm->seq_params.use_highbitdepth) {
2148 rec_uv[pli - 1][r * ccso_stride + c] =
2149 CONVERT_TO_SHORTPTR(xd->plane[pli].dst.buf)[r * dst_stride + c];
2150 org_uv[pli - 1][r * ccso_stride + c] =
2151 CONVERT_TO_SHORTPTR(ref_buffer)[r * ref_stride + c];
2152 } else {
2153 rec_uv[pli - 1][r * ccso_stride + c] =
2154 xd->plane[pli].dst.buf[r * dst_stride + c];
2155 org_uv[pli - 1][r * ccso_stride + c] =
2156 ref_buffer[r * ref_stride + c];
2157 }
2158 }
2159 }
2160 }
2161 ccso_search(cm, xd, cpi->td.mb.rdmult, ext_rec_y, rec_uv, org_uv);
2162 ccso_frame(&cm->cur_frame->buf, cm, xd, ext_rec_y);
2163 aom_free(ext_rec_y);
2164 }
2165 for (int pli = 0; pli < 2; pli++) {
2166 aom_free(rec_uv[pli]);
2167 aom_free(org_uv[pli]);
2168 }
2169#endif
2170
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +05302171 av1_superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002172
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002173#if CONFIG_COLLECT_COMPONENT_TIMING
2174 start_timing(cpi, loop_restoration_time);
2175#endif
yixindu368bde52021-06-04 18:30:53 -07002176
logangwf95c9162019-02-20 12:02:32 -08002177 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00002178 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08002179 av1_pick_filter_restoration(cpi->source, cpi);
2180 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2181 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2182 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Deepa K G74de2a02020-04-11 13:09:11 +05302183 if (num_workers > 1)
2184 av1_loop_restoration_filter_frame_mt(
2185 &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
2186 &mt_info->lr_row_sync, &cpi->lr_ctxt);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05302187 else
David Turnerc29e1a92018-12-06 14:10:14 +00002188 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05302189 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08002190 }
logangwf95c9162019-02-20 12:02:32 -08002191 } else {
2192 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
2193 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
2194 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002195 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002196#if CONFIG_COLLECT_COMPONENT_TIMING
2197 end_timing(cpi, loop_restoration_time);
2198#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07002199}
2200
Yue Chen2d8405c2020-06-10 14:42:05 -07002201/*!\brief Select and apply in-loop deblocking filters, cdef filters, and
2202 * restoration filters
2203 *
2204 * \ingroup high_level_algo
Yue Chen2d8405c2020-06-10 14:42:05 -07002205 */
Yue Chen92271e12019-07-09 16:28:02 -07002206static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Deepa K G74de2a02020-04-11 13:09:11 +05302207 MultiThreadInfo *const mt_info = &cpi->mt_info;
2208 const int num_workers = mt_info->num_workers;
Yue Chen92271e12019-07-09 16:28:02 -07002209 const int num_planes = av1_num_planes(cm);
2210 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2211
Vishesh39e74092020-06-16 17:13:48 +05302212 assert(IMPLIES(is_lossless_requested(&cpi->oxcf.rc_cfg),
Urvang Joshib6409e92020-03-23 11:23:27 -07002213 cm->features.coded_lossless && cm->features.all_lossless));
Yue Chen92271e12019-07-09 16:28:02 -07002214
Ryan Leiefc91662020-10-09 14:36:14 -07002215 const int use_loopfilter = !cm->features.coded_lossless &&
2216 !cm->tiles.large_scale &&
2217 cpi->oxcf.tool_cfg.enable_deblocking;
Urvang Joshib6409e92020-03-23 11:23:27 -07002218 const int use_cdef = cm->seq_params.enable_cdef &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07002219 !cm->features.coded_lossless && !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07002220 const int use_restoration = cm->seq_params.enable_restoration &&
Urvang Joshib6409e92020-03-23 11:23:27 -07002221 !cm->features.all_lossless &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07002222 !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07002223
2224 struct loopfilter *lf = &cm->lf;
2225
2226#if CONFIG_COLLECT_COMPONENT_TIMING
2227 start_timing(cpi, loop_filter_time);
2228#endif
2229 if (use_loopfilter) {
2230 aom_clear_system_state();
chiyotsaid2f7b412019-12-18 10:16:42 -08002231 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
Yue Chen92271e12019-07-09 16:28:02 -07002232 } else {
2233 lf->filter_level[0] = 0;
2234 lf->filter_level[1] = 0;
2235 }
2236
2237 if (lf->filter_level[0] || lf->filter_level[1]) {
Deepa K G74de2a02020-04-11 13:09:11 +05302238 if (num_workers > 1)
Yue Chen92271e12019-07-09 16:28:02 -07002239 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
2240#if CONFIG_LPF_MASK
2241 0,
2242#endif
Deepa K G74de2a02020-04-11 13:09:11 +05302243 mt_info->workers, num_workers,
2244 &mt_info->lf_row_sync);
Yue Chen92271e12019-07-09 16:28:02 -07002245 else
2246 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
2247#if CONFIG_LPF_MASK
2248 0,
2249#endif
2250 0, num_planes, 0);
2251 }
2252#if CONFIG_COLLECT_COMPONENT_TIMING
2253 end_timing(cpi, loop_filter_time);
2254#endif
2255
2256 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
2257}
2258
Yue Chen2d8405c2020-06-10 14:42:05 -07002259/*!\brief Encode a frame without the recode loop, usually used in one-pass
Fyodor Kyslovf2a2eee2021-07-12 13:07:54 -07002260 * encoding.
Yue Chen2d8405c2020-06-10 14:42:05 -07002261 *
2262 * \ingroup high_level_algo
Yue Chen2d8405c2020-06-10 14:42:05 -07002263 *
2264 * \param[in] cpi Top-level encoder structure
2265 *
2266 * \return Returns a value to indicate if the encoding is done successfully.
2267 * \retval #AOM_CODEC_OK
2268 * \retval #AOM_CODEC_ERROR
2269 */
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002270static int encode_without_recode(AV1_COMP *cpi) {
2271 AV1_COMMON *const cm = &cpi->common;
Vishesh734eff92020-06-20 21:46:36 +05302272 const QuantizationCfg *const q_cfg = &cpi->oxcf.q_cfg;
Jerome Jiang790190e2020-06-12 16:47:45 -07002273 SVC *const svc = &cpi->svc;
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07002274 ResizePendingParams *const resize_pending_params =
2275 &cpi->resize_pending_params;
2276 const int resize_pending =
2277 (resize_pending_params->width && resize_pending_params->height &&
2278 (cpi->common.width != resize_pending_params->width ||
2279 cpi->common.height != resize_pending_params->height));
2280
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002281 int top_index = 0, bottom_index = 0, q = 0;
Jerome Jiang790190e2020-06-12 16:47:45 -07002282 YV12_BUFFER_CONFIG *unscaled = cpi->unscaled_source;
Jerome Jiangdede2932020-06-23 16:49:41 -07002283 InterpFilter filter_scaler =
Jerome Jiang790190e2020-06-12 16:47:45 -07002284 cpi->use_svc ? svc->downsample_filter_type[svc->spatial_layer_id]
Marco Paniconi327528a2020-06-30 21:19:09 -07002285 : EIGHTTAP_SMOOTH;
Jerome Jiang790190e2020-06-12 16:47:45 -07002286 int phase_scaler =
2287 cpi->use_svc ? svc->downsample_filter_phase[svc->spatial_layer_id] : 0;
Jerome Jiangdede2932020-06-23 16:49:41 -07002288
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002289 set_size_independent_vars(cpi);
2290 av1_setup_frame_size(cpi);
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302291 av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002292
Jerome Jiangc1b11262020-06-25 13:53:55 -07002293 if (!cpi->use_svc) {
Jerome Jiangdede2932020-06-23 16:49:41 -07002294 phase_scaler = 8;
Marco Paniconi5fffb442020-07-01 10:49:30 -07002295 // 2:1 scaling.
Jerome Jiangc1b11262020-06-25 13:53:55 -07002296 if ((cm->width << 1) == unscaled->y_crop_width &&
Marco Paniconi5fffb442020-07-01 10:49:30 -07002297 (cm->height << 1) == unscaled->y_crop_height) {
Jerome Jiangc1b11262020-06-25 13:53:55 -07002298 filter_scaler = BILINEAR;
Marco Paniconi5fffb442020-07-01 10:49:30 -07002299 // For lower resolutions use eighttap_smooth.
2300 if (cm->width * cm->height <= 320 * 180) filter_scaler = EIGHTTAP_SMOOTH;
2301 } else if ((cm->width << 2) == unscaled->y_crop_width &&
2302 (cm->height << 2) == unscaled->y_crop_height) {
2303 // 4:1 scaling.
Jerome Jiangc1b11262020-06-25 13:53:55 -07002304 filter_scaler = EIGHTTAP_SMOOTH;
Marco Paniconi5fffb442020-07-01 10:49:30 -07002305 } else if ((cm->width << 2) == 3 * unscaled->y_crop_width &&
2306 (cm->height << 2) == 3 * unscaled->y_crop_height) {
2307 // 4:3 scaling.
Jerome Jiang0da8c242020-07-24 14:24:41 -07002308 // TODO(jianj): Neon optimization for 4:3 scaling for EIGHTTAP has issues.
2309 // See aomedia:2766.
2310 filter_scaler = BILINEAR;
Marco Paniconi5fffb442020-07-01 10:49:30 -07002311 }
Jerome Jiangdede2932020-06-23 16:49:41 -07002312 }
2313
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002314 if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi);
2315
2316#if CONFIG_COLLECT_COMPONENT_TIMING
2317 printf("\n Encoding a frame:");
2318#endif
Marco Paniconid13853d2020-05-07 11:42:16 -07002319
2320 aom_clear_system_state();
2321
Jerome Jiang790190e2020-06-12 16:47:45 -07002322 cpi->source = av1_scale_if_required(cm, unscaled, &cpi->scaled_source,
Urvang Joshibd955852020-10-06 16:05:26 -07002323 filter_scaler, phase_scaler, true, false);
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07002324 if (frame_is_intra_only(cm) || resize_pending != 0) {
2325 memset(cpi->consec_zero_mv, 0,
2326 ((cm->mi_params.mi_rows * cm->mi_params.mi_cols) >> 2) *
2327 sizeof(*cpi->consec_zero_mv));
2328 }
2329
Marco Paniconid13853d2020-05-07 11:42:16 -07002330 if (cpi->unscaled_last_source != NULL) {
Urvang Joshibd955852020-10-06 16:05:26 -07002331 cpi->last_source = av1_scale_if_required(
2332 cm, cpi->unscaled_last_source, &cpi->scaled_last_source, filter_scaler,
2333 phase_scaler, true, false);
Marco Paniconid13853d2020-05-07 11:42:16 -07002334 }
Marco Paniconi2b22a1f2020-06-22 21:55:02 -07002335
Marco Paniconi69e6dcc2020-07-20 14:19:20 -07002336 // For 1 spatial layer encoding: if the (non-LAST) reference has different
2337 // resolution from the source then disable that reference. This is to avoid
2338 // significant increase in encode time from scaling the references in
2339 // av1_scale_references. Note GOLDEN is forced to update on the (first/tigger)
2340 // resized frame and ALTREF will be refreshed ~4 frames later, so both
2341 // references become available again after few frames.
2342 if (svc->number_spatial_layers == 1) {
2343 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[GOLDEN_FRAME]) {
2344 const YV12_BUFFER_CONFIG *const ref =
2345 get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
2346 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
2347 cpi->ref_frame_flags ^= AOM_GOLD_FLAG;
2348 }
2349 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ALTREF_FRAME]) {
2350 const YV12_BUFFER_CONFIG *const ref =
2351 get_ref_frame_yv12_buf(cm, ALTREF_FRAME);
2352 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
2353 cpi->ref_frame_flags ^= AOM_ALT_FLAG;
2354 }
2355 }
2356
Marco Paniconi2b22a1f2020-06-22 21:55:02 -07002357 // For SVC the inter-layer/spatial prediction is not done for newmv
2358 // (zero_mode is forced), and since the scaled references are only
2359 // use for newmv search, we can avoid scaling here.
2360 if (!frame_is_intra_only(cm) &&
2361 !(cpi->use_svc && cpi->svc.force_zero_mode_spatial_ref))
Jerome Jiang3dddc322020-06-29 16:59:58 -07002362 av1_scale_references(cpi, filter_scaler, phase_scaler, 1);
Marco Paniconid13853d2020-05-07 11:42:16 -07002363
Vishesh734eff92020-06-20 21:46:36 +05302364 av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2365 q_cfg->enable_chroma_deltaq);
Marco Paniconid13853d2020-05-07 11:42:16 -07002366 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
Ryan Leiccc6ea72021-01-06 11:43:56 -08002367#if !CONFIG_EXTQUANT
2368 if (q_cfg->deltaq_mode != NO_DELTA_Q || q_cfg->enable_chroma_deltaq)
2369#endif
2370 av1_init_quantizer(&cm->seq_params, &cpi->enc_quant_dequant_params,
2371 &cm->quant_params);
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302372 av1_setup_frame(cpi);
Marco Paniconid13853d2020-05-07 11:42:16 -07002373
Vishesh734eff92020-06-20 21:46:36 +05302374 if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ) {
Marco Paniconid13853d2020-05-07 11:42:16 -07002375 suppress_active_map(cpi);
2376 av1_cyclic_refresh_setup(cpi);
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302377 av1_apply_active_map(cpi);
Marco Paniconid13853d2020-05-07 11:42:16 -07002378 }
2379 if (cm->seg.enabled) {
2380 if (!cm->seg.update_data && cm->prev_frame) {
2381 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
2382 cm->seg.enabled = cm->prev_frame->seg.enabled;
2383 } else {
2384 av1_calculate_segdata(&cm->seg);
2385 }
2386 } else {
2387 memset(&cm->seg, 0, sizeof(cm->seg));
2388 }
2389 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
2390 cm->cur_frame->seg.enabled = cm->seg.enabled;
2391
2392#if CONFIG_COLLECT_COMPONENT_TIMING
2393 start_timing(cpi, av1_encode_frame_time);
2394#endif
2395
2396 // Set the motion vector precision based on mv stats from the last coded
2397 // frame.
2398 if (!frame_is_intra_only(cm)) av1_pick_and_set_high_precision_mv(cpi, q);
2399
2400 // transform / motion compensation build reconstruction frame
2401 av1_encode_frame(cpi);
2402
2403 // Update some stats from cyclic refresh.
Vishesh734eff92020-06-20 21:46:36 +05302404 if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ && !frame_is_intra_only(cm))
Marco Paniconid13853d2020-05-07 11:42:16 -07002405 av1_cyclic_refresh_postencode(cpi);
2406
2407#if CONFIG_COLLECT_COMPONENT_TIMING
2408 end_timing(cpi, av1_encode_frame_time);
2409#endif
2410#if CONFIG_INTERNAL_STATS
2411 ++cpi->tot_recode_hits;
2412#endif
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002413
Marco Paniconie635eb62020-05-14 14:01:02 -07002414 aom_clear_system_state();
2415
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002416 return AOM_CODEC_OK;
Marco Paniconid13853d2020-05-07 11:42:16 -07002417}
2418
Yue Chen2d8405c2020-06-10 14:42:05 -07002419/*!\brief Recode loop for encoding one frame. the purpose of encoding one frame
2420 * for multiple times can be approaching a target bitrate or adjusting the usage
2421 * of global motions.
2422 *
2423 * \ingroup high_level_algo
Yue Chen2d8405c2020-06-10 14:42:05 -07002424 *
2425 * \param[in] cpi Top-level encoder structure
2426 * \param[in] size Bitstream size
2427 * \param[in] dest Bitstream output
2428 *
2429 * \return Returns a value to indicate if the encoding is done successfully.
2430 * \retval #AOM_CODEC_OK
2431 * \retval -1
2432 * \retval #AOM_CODEC_ERROR
2433 */
Tom Finegane4099e32018-01-23 12:01:51 -08002434static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002435 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002436 RATE_CONTROL *const rc = &cpi->rc;
Vishesh8ba8bfe2020-04-03 14:20:29 +05302437 GlobalMotionInfo *const gm_info = &cpi->gm_info;
Vishesh734eff92020-06-20 21:46:36 +05302438 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2439 const QuantizationCfg *const q_cfg = &oxcf->q_cfg;
chiyotsai6b430132019-12-18 10:33:51 -08002440 const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07002441 // Must allow recode if minimum compression ratio is set.
Vishesh734eff92020-06-20 21:46:36 +05302442 assert(IMPLIES(oxcf->rc_cfg.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443
2444 set_size_independent_vars(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07002445 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07002446
David Turnerdedd8ff2019-01-23 13:59:46 +00002447 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07002448
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +05302449 if (av1_superres_in_recode_allowed(cpi) &&
Urvang Joshi9d2606c2020-04-14 15:58:01 -07002450 cpi->superres_mode != AOM_SUPERRES_NONE &&
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002451 cm->superres_scale_denominator == SCALE_NUMERATOR) {
Urvang Joshidb4551e2020-04-08 12:30:21 -07002452 // Superres mode is currently enabled, but the denominator selected will
Urvang Joshif9c2bd72020-04-07 15:11:51 -07002453 // disable superres. So no need to continue, as we will go through another
2454 // recode loop for full-resolution after this anyway.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002455 return -1;
2456 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002457
David Turner2f3b5df2019-01-02 14:30:50 +00002458 int top_index = 0, bottom_index = 0;
2459 int q = 0, q_low = 0, q_high = 0;
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302460 av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
David Turner2f3b5df2019-01-02 14:30:50 +00002461 q_low = bottom_index;
2462 q_high = top_index;
2463
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002464 if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi);
Cheng Chen92a926a2020-01-30 17:57:33 -08002465
2466#if CONFIG_COLLECT_COMPONENT_TIMING
2467 printf("\n Encoding a frame:");
2468#endif
2469
Marco Paniconid13853d2020-05-07 11:42:16 -07002470 // Determine whether to use screen content tools using two fast encoding.
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302471 av1_determine_sc_tools_with_encoding(cpi, q);
Marco Paniconid13853d2020-05-07 11:42:16 -07002472
Sai Deng7275a1a2020-08-31 12:06:09 -07002473#if CONFIG_USE_VMAF_RC
2474 if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2475 av1_vmaf_neg_preprocessing(cpi, cpi->unscaled_source);
2476 }
2477#endif
2478
David Turner2f3b5df2019-01-02 14:30:50 +00002479 // Loop variables
Cheng Chen92a926a2020-01-30 17:57:33 -08002480 int loop = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00002481 int loop_count = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00002482 int overshoot_seen = 0;
2483 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07002484 int low_cr_seen = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08002485 int last_loop_allow_hp = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002486
Yaowu Xuc27fc142016-08-22 16:08:15 -07002487 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07002488 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002489 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002490
Urvang Joshif1fa6862018-01-08 16:39:33 -08002491 // if frame was scaled calculate global_motion_search again if already
2492 // done
Vishesh8ba8bfe2020-04-03 14:20:29 +05302493 if (loop_count > 0 && cpi->source && gm_info->search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07002494 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00002495 cpi->source->y_crop_height != cm->height) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05302496 gm_info->search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00002497 }
2498 }
Urvang Joshibd955852020-10-06 16:05:26 -07002499 cpi->source =
2500 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source,
2501 EIGHTTAP_REGULAR, 0, false, false);
Jerome Jiangdede2932020-06-23 16:49:41 -07002502
David Turner2f3b5df2019-01-02 14:30:50 +00002503 if (cpi->unscaled_last_source != NULL) {
Urvang Joshibd955852020-10-06 16:05:26 -07002504 cpi->last_source = av1_scale_if_required(
2505 cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
2506 EIGHTTAP_REGULAR, 0, false, false);
David Turner2f3b5df2019-01-02 14:30:50 +00002507 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07002508
David Turner2f3b5df2019-01-02 14:30:50 +00002509 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002510 if (loop_count > 0) {
2511 release_scaled_references(cpi);
2512 }
Jerome Jiang3dddc322020-06-29 16:59:58 -07002513 av1_scale_references(cpi, EIGHTTAP_REGULAR, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002514 }
sdeng3cd9eec2020-01-23 15:49:50 -08002515#if CONFIG_TUNE_VMAF
Sai Deng7275a1a2020-08-31 12:06:09 -07002516 if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
2517 oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
Sai Deng48ebb282020-08-13 15:30:50 -07002518 cpi->vmaf_info.original_qindex = q;
2519 q = av1_get_vmaf_base_qindex(cpi, q);
sdeng3cd9eec2020-01-23 15:49:50 -08002520 }
2521#endif
Sai Deng48ebb282020-08-13 15:30:50 -07002522 av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2523 q_cfg->enable_chroma_deltaq);
Vishesh734eff92020-06-20 21:46:36 +05302524 av1_set_speed_features_qindex_dependent(cpi, oxcf->speed);
sdeng3cd9eec2020-01-23 15:49:50 -08002525
Ryan Leiccc6ea72021-01-06 11:43:56 -08002526#if !CONFIG_EXTQUANT
Madhu Peringassery Krishnan5fa4fcf2021-02-19 15:48:34 -08002527 if (q_cfg->deltaq_mode != NO_DELTA_Q || q_cfg->enable_chroma_deltaq)
Ryan Leiccc6ea72021-01-06 11:43:56 -08002528#endif
2529 av1_init_quantizer(&cm->seq_params, &cpi->enc_quant_dequant_params,
2530 &cm->quant_params);
kyslov7b9d0d62018-12-21 11:12:26 -08002531
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08002532 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
2533 // cm->current_frame.frame_number, cm->show_frame, q,
2534 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002535
David Turner2f3b5df2019-01-02 14:30:50 +00002536 if (loop_count == 0) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302537 av1_setup_frame(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00002538 } else if (get_primary_ref_frame_buf(cm) == NULL) {
2539 // Base q-index may have changed, so we need to assign proper default coef
2540 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07002541 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08002542 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00002543 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002544
Vishesh734eff92020-06-20 21:46:36 +05302545 if (q_cfg->aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002546 av1_vaq_frame_setup(cpi);
Vishesh734eff92020-06-20 21:46:36 +05302547 } else if (q_cfg->aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002548 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002549 }
David Turner2f3b5df2019-01-02 14:30:50 +00002550
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00002551 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00002552 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00002553 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07002554 cm->seg.enabled = cm->prev_frame->seg.enabled;
David Barker11c93562018-06-05 12:00:07 +01002555 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07002556 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07002557 }
David Barkercab37552018-03-21 11:56:24 +00002558 } else {
2559 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00002560 }
David Barkercab37552018-03-21 11:56:24 +00002561 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07002562 cm->cur_frame->seg.enabled = cm->seg.enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002563
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002564#if CONFIG_COLLECT_COMPONENT_TIMING
2565 start_timing(cpi, av1_encode_frame_time);
2566#endif
chiyotsai8b7cef82020-01-21 16:52:54 -08002567 // Set the motion vector precision based on mv stats from the last coded
2568 // frame.
2569 if (!frame_is_intra_only(cm)) {
2570 av1_pick_and_set_high_precision_mv(cpi, q);
2571
2572 // If the precision has changed during different iteration of the loop,
2573 // then we need to reset the global motion vectors
Urvang Joshib6409e92020-03-23 11:23:27 -07002574 if (loop_count > 0 &&
2575 cm->features.allow_high_precision_mv != last_loop_allow_hp) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05302576 gm_info->search_done = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08002577 }
Urvang Joshib6409e92020-03-23 11:23:27 -07002578 last_loop_allow_hp = cm->features.allow_high_precision_mv;
chiyotsai8b7cef82020-01-21 16:52:54 -08002579 }
chiyotsaic666b1f2019-12-20 10:44:58 -08002580
Yaowu Xuc27fc142016-08-22 16:08:15 -07002581 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07002582 av1_encode_frame(cpi);
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002583
chiyotsai51bd6482019-12-20 10:49:34 -08002584 // Reset the mv_stats in case we are interrupted by an intraframe or an
2585 // overlay frame.
2586 if (cpi->mv_stats.valid) {
2587 av1_zero(cpi->mv_stats);
2588 }
2589 // Gather the mv_stats for the next frame
2590 if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
2591 av1_frame_allows_smart_mv(cpi)) {
2592 av1_collect_mv_stats(cpi, q);
2593 }
chiyotsai51bd6482019-12-20 10:49:34 -08002594
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002595#if CONFIG_COLLECT_COMPONENT_TIMING
2596 end_timing(cpi, av1_encode_frame_time);
2597#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002598
Yaowu Xuf883b422016-08-30 14:01:10 -07002599 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002600
2601 // Dummy pack of the bitstream using up to date stats to get an
2602 // accurate estimate of output frame size to determine if we need
2603 // to recode.
chiyotsai6b430132019-12-18 10:33:51 -08002604 const int do_dummy_pack =
2605 (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
Vishesh734eff92020-06-20 21:46:36 +05302606 oxcf->rc_cfg.mode != AOM_Q) ||
2607 oxcf->rc_cfg.min_cr > 0;
Hui Sua1d71842019-07-31 12:02:24 -07002608 if (do_dummy_pack) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302609 av1_finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00002610 int largest_tile_id = 0; // Output from bitstream: unused here
chiyotsaic666b1f2019-12-20 10:44:58 -08002611 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) !=
2612 AOM_CODEC_OK) {
Tom Finegane4099e32018-01-23 12:01:51 -08002613 return AOM_CODEC_ERROR;
chiyotsaic666b1f2019-12-20 10:44:58 -08002614 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002615
2616 rc->projected_frame_size = (int)(*size) << 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617 }
2618
Sai Deng48ebb282020-08-13 15:30:50 -07002619#if CONFIG_TUNE_VMAF
Sai Deng7275a1a2020-08-31 12:06:09 -07002620 if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
2621 oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
Sai Deng48ebb282020-08-13 15:30:50 -07002622 q = cpi->vmaf_info.original_qindex;
2623 }
2624#endif
Hui Suef139e12019-05-20 15:51:22 -07002625 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00002626 // Update q and decide whether to do a recode loop
2627 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
2628 bottom_index, &undershoot_seen, &overshoot_seen,
Paul Wilkinsc8d3f112020-07-08 17:58:14 +01002629 &low_cr_seen, loop_count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002630 }
2631
2632 // Special case for overlay frame.
Hui Su7e3eeaa2019-09-11 15:50:41 -07002633 if (loop && rc->is_src_frame_alt_ref &&
2634 rc->projected_frame_size < rc->max_frame_bandwidth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002635 loop = 0;
Hui Su7e3eeaa2019-09-11 15:50:41 -07002636 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002637
chiyotsai76cd2652019-12-18 10:26:36 -08002638 if (allow_recode && !cpi->sf.gm_sf.gm_disable_recode &&
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302639 av1_recode_loop_test_global_motion(cm->global_motion,
2640 cpi->td.rd_counts.global_motion_used,
2641 gm_info->params_cost)) {
David Turner2f3b5df2019-01-02 14:30:50 +00002642 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08002643 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08002644
Yaowu Xuc27fc142016-08-22 16:08:15 -07002645 if (loop) {
2646 ++loop_count;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647
2648#if CONFIG_INTERNAL_STATS
2649 ++cpi->tot_recode_hits;
2650#endif
2651 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002652#if CONFIG_COLLECT_COMPONENT_TIMING
2653 if (loop) printf("\n Recoding:");
2654#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08002656
2657 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002658}
2659
Yue Chen2d8405c2020-06-10 14:42:05 -07002660/*!\brief Recode loop or a single loop for encoding one frame, followed by
2661 * in-loop deblocking filters, CDEF filters, and restoration filters.
2662 *
2663 * \ingroup high_level_algo
2664 * \callgraph
2665 * \callergraph
2666 *
2667 * \param[in] cpi Top-level encoder structure
2668 * \param[in] size Bitstream size
2669 * \param[in] dest Bitstream output
2670 * \param[in] sse Total distortion of the frame
2671 * \param[in] rate Total rate of the frame
2672 * \param[in] largest_tile_id Tile id of the last tile
2673 *
2674 * \return Returns a value to indicate if the encoding is done successfully.
2675 * \retval #AOM_CODEC_OK
2676 * \retval #AOM_CODEC_ERROR
2677 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002678static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
2679 uint8_t *dest, int64_t *sse,
2680 int64_t *rate,
2681 int *largest_tile_id) {
2682#if CONFIG_COLLECT_COMPONENT_TIMING
2683 start_timing(cpi, encode_with_recode_loop_time);
2684#endif
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002685 int err;
Jingning Han37af76d2020-07-27 23:17:26 -07002686 if (cpi->sf.hl_sf.recode_loop == DISALLOW_RECODE)
Marco Paniconic6cd72e2020-05-08 11:00:50 -07002687 err = encode_without_recode(cpi);
2688 else
2689 err = encode_with_recode_loop(cpi, size, dest);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002690#if CONFIG_COLLECT_COMPONENT_TIMING
2691 end_timing(cpi, encode_with_recode_loop_time);
2692#endif
2693 if (err != AOM_CODEC_OK) {
2694 if (err == -1) {
2695 // special case as described in encode_with_recode_loop().
2696 // Encoding was skipped.
2697 err = AOM_CODEC_OK;
2698 if (sse != NULL) *sse = INT64_MAX;
2699 if (rate != NULL) *rate = INT64_MAX;
2700 *largest_tile_id = 0;
2701 }
2702 return err;
2703 }
2704
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002705 AV1_COMMON *const cm = &cpi->common;
2706 SequenceHeader *const seq_params = &cm->seq_params;
2707
2708 // Special case code to reduce pulsing when key frames are forced at a
2709 // fixed interval. Note the reconstruction error if it is the frame before
2710 // the force key frame
2711 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2712 if (seq_params->use_highbitdepth) {
2713 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
2714 } else {
2715 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2716 }
2717 }
2718
2719 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
2720 cm->cur_frame->buf.transfer_characteristics =
2721 seq_params->transfer_characteristics;
2722 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
2723 cm->cur_frame->buf.monochrome = seq_params->monochrome;
2724 cm->cur_frame->buf.chroma_sample_position =
2725 seq_params->chroma_sample_position;
2726 cm->cur_frame->buf.color_range = seq_params->color_range;
2727 cm->cur_frame->buf.render_width = cm->render_width;
2728 cm->cur_frame->buf.render_height = cm->render_height;
2729
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002730 // Pick the loop filter level for the frame.
Urvang Joshib6409e92020-03-23 11:23:27 -07002731 if (!cm->features.allow_intrabc) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002732 loopfilter_frame(cpi, cm);
2733 } else {
2734 cm->lf.filter_level[0] = 0;
2735 cm->lf.filter_level[1] = 0;
2736 cm->cdef_info.cdef_bits = 0;
2737 cm->cdef_info.cdef_strengths[0] = 0;
2738 cm->cdef_info.nb_cdef_strengths = 1;
2739 cm->cdef_info.cdef_uv_strengths[0] = 0;
2740 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
2741 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
2742 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
2743 }
2744
2745 // TODO(debargha): Fix mv search range on encoder side
2746 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
2747 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
2748
2749#ifdef OUTPUT_YUV_REC
2750 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
2751#endif
2752
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302753 av1_finalize_encoded_frame(cpi);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002754 // Build the bitstream
2755#if CONFIG_COLLECT_COMPONENT_TIMING
2756 start_timing(cpi, av1_pack_bitstream_final_time);
2757#endif
2758 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
2759 return AOM_CODEC_ERROR;
2760#if CONFIG_COLLECT_COMPONENT_TIMING
2761 end_timing(cpi, av1_pack_bitstream_final_time);
2762#endif
2763
2764 // Compute sse and rate.
2765 if (sse != NULL) {
2766 *sse = (seq_params->use_highbitdepth)
2767 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
2768 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2769 }
2770 if (rate != NULL) {
2771 const int64_t bits = (*size << 3);
2772 *rate = (bits << 5); // To match scale.
2773 }
2774 return AOM_CODEC_OK;
2775}
2776
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002777static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
2778 uint8_t *dest,
2779 int *largest_tile_id) {
2780 const AV1_COMMON *const cm = &cpi->common;
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002781 assert(cm->seq_params.enable_superres);
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +05302782 assert(av1_superres_in_recode_allowed(cpi));
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002783 aom_codec_err_t err = AOM_CODEC_OK;
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302784 av1_save_all_coding_context(cpi);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002785
Urvang Joshibc82d382019-11-01 17:59:20 -07002786 int64_t sse1 = INT64_MAX;
2787 int64_t rate1 = INT64_MAX;
2788 int largest_tile_id1 = 0;
Debargha Mukherjee2f16e852020-10-19 16:57:30 -07002789 int64_t sse2 = INT64_MAX;
2790 int64_t rate2 = INT64_MAX;
2791 int largest_tile_id2;
2792 double proj_rdcost1 = DBL_MAX;
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002793
Debargha Mukherjee2f16e852020-10-19 16:57:30 -07002794 // Encode with superres.
2795 if (cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_ALL) {
2796 SuperResCfg *const superres_cfg = &cpi->oxcf.superres_cfg;
2797 int64_t superres_sses[SCALE_NUMERATOR];
2798 int64_t superres_rates[SCALE_NUMERATOR];
2799 int superres_largest_tile_ids[SCALE_NUMERATOR];
2800 // Use superres for Key-frames and Alt-ref frames only.
2801 const GF_GROUP *const gf_group = &cpi->gf_group;
2802 if (gf_group->update_type[gf_group->index] != OVERLAY_UPDATE &&
2803 gf_group->update_type[gf_group->index] != INTNL_OVERLAY_UPDATE) {
2804 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
2805 ++denom) {
2806 superres_cfg->superres_scale_denominator = denom;
2807 superres_cfg->superres_kf_scale_denominator = denom;
2808 const int this_index = denom - (SCALE_NUMERATOR + 1);
2809
2810 cpi->superres_mode = AOM_SUPERRES_AUTO; // Super-res on for this loop.
2811 err = encode_with_recode_loop_and_filter(
2812 cpi, size, dest, &superres_sses[this_index],
2813 &superres_rates[this_index],
2814 &superres_largest_tile_ids[this_index]);
2815 cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
2816 if (err != AOM_CODEC_OK) return err;
2817 restore_all_coding_context(cpi);
2818 }
2819 // Reset.
2820 superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
2821 superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
2822 } else {
2823 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
2824 ++denom) {
2825 const int this_index = denom - (SCALE_NUMERATOR + 1);
2826 superres_sses[this_index] = INT64_MAX;
2827 superres_rates[this_index] = INT64_MAX;
2828 }
2829 }
2830 // Encode without superres.
2831 assert(cpi->superres_mode == AOM_SUPERRES_NONE);
2832 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
2833 &largest_tile_id2);
2834 if (err != AOM_CODEC_OK) return err;
2835
2836 // Note: Both use common rdmult based on base qindex of fullres.
2837 const int64_t rdmult =
2838 av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
2839
2840 // Find the best rdcost among all superres denoms.
2841 int best_denom = -1;
2842 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
2843 ++denom) {
2844 const int this_index = denom - (SCALE_NUMERATOR + 1);
2845 const int64_t this_sse = superres_sses[this_index];
2846 const int64_t this_rate = superres_rates[this_index];
2847 const int this_largest_tile_id = superres_largest_tile_ids[this_index];
2848 const double this_rdcost = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2849 rdmult, this_rate, this_sse, cm->seq_params.bit_depth);
2850 if (this_rdcost < proj_rdcost1) {
2851 sse1 = this_sse;
2852 rate1 = this_rate;
2853 largest_tile_id1 = this_largest_tile_id;
2854 proj_rdcost1 = this_rdcost;
2855 best_denom = denom;
2856 }
2857 }
2858 const double proj_rdcost2 = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2859 rdmult, rate2, sse2, cm->seq_params.bit_depth);
2860 // Re-encode with superres if it's better.
2861 if (proj_rdcost1 < proj_rdcost2) {
2862 restore_all_coding_context(cpi);
2863 // TODO(urvang): We should avoid rerunning the recode loop by saving
2864 // previous output+state, or running encode only for the selected 'q' in
2865 // previous step.
2866 // Again, temporarily force the best denom.
2867 superres_cfg->superres_scale_denominator = best_denom;
2868 superres_cfg->superres_kf_scale_denominator = best_denom;
2869 int64_t sse3 = INT64_MAX;
2870 int64_t rate3 = INT64_MAX;
2871 cpi->superres_mode =
2872 AOM_SUPERRES_AUTO; // Super-res on for this recode loop.
2873 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
2874 largest_tile_id);
2875 cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
2876 assert(sse1 == sse3);
2877 assert(rate1 == rate3);
2878 assert(largest_tile_id1 == *largest_tile_id);
2879 // Reset.
2880 superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
2881 superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
2882 } else {
2883 *largest_tile_id = largest_tile_id2;
2884 }
2885 } else {
2886 assert(cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_DUAL);
Urvang Joshidf265712020-10-05 23:23:09 -07002887 cpi->superres_mode =
2888 AOM_SUPERRES_AUTO; // Super-res on for this recode loop.
Debargha Mukherjee2f16e852020-10-19 16:57:30 -07002889 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
2890 &largest_tile_id1);
Urvang Joshidf265712020-10-05 23:23:09 -07002891 cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
Debargha Mukherjee2f16e852020-10-19 16:57:30 -07002892 if (err != AOM_CODEC_OK) return err;
2893 restore_all_coding_context(cpi);
2894 // Encode without superres.
2895 assert(cpi->superres_mode == AOM_SUPERRES_NONE);
2896 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
2897 &largest_tile_id2);
2898 if (err != AOM_CODEC_OK) return err;
2899
2900 // Note: Both use common rdmult based on base qindex of fullres.
2901 const int64_t rdmult =
2902 av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
2903 proj_rdcost1 = RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate1, sse1,
2904 cm->seq_params.bit_depth);
2905 const double proj_rdcost2 = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2906 rdmult, rate2, sse2, cm->seq_params.bit_depth);
2907 // Re-encode with superres if it's better.
2908 if (proj_rdcost1 < proj_rdcost2) {
2909 restore_all_coding_context(cpi);
2910 // TODO(urvang): We should avoid rerunning the recode loop by saving
2911 // previous output+state, or running encode only for the selected 'q' in
2912 // previous step.
2913 int64_t sse3 = INT64_MAX;
2914 int64_t rate3 = INT64_MAX;
2915 cpi->superres_mode =
2916 AOM_SUPERRES_AUTO; // Super-res on for this recode loop.
2917 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
2918 largest_tile_id);
2919 cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
2920 assert(sse1 == sse3);
2921 assert(rate1 == rate3);
2922 assert(largest_tile_id1 == *largest_tile_id);
2923 } else {
2924 *largest_tile_id = largest_tile_id2;
2925 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002926 }
2927
2928 return err;
2929}
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07002930
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08002931extern void av1_print_frame_contexts(const FRAME_CONTEXT *fc,
2932 const char *filename);
2933
Yue Chen2d8405c2020-06-10 14:42:05 -07002934/*!\brief Run the final pass encoding for 1-pass/2-pass encoding mode, and pack
2935 * the bitstream
2936 *
2937 * \ingroup high_level_algo
2938 * \callgraph
2939 * \callergraph
2940 *
2941 * \param[in] cpi Top-level encoder structure
2942 * \param[in] size Bitstream size
2943 * \param[in] dest Bitstream output
2944 *
2945 * \return Returns a value to indicate if the encoding is done successfully.
2946 * \retval #AOM_CODEC_OK
2947 * \retval #AOM_CODEC_ERROR
2948 */
David Turner73245762019-02-11 16:42:34 +00002949static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
2950 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002951 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002952 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00002953 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07002954 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955 struct segmentation *const seg = &cm->seg;
Urvang Joshib6409e92020-03-23 11:23:27 -07002956 FeatureFlags *const features = &cm->features;
Vishesh0481a782020-06-11 14:32:46 +05302957 const TileConfig *const tile_cfg = &oxcf->tile_cfg;
Thomas Davies4822e142017-10-10 11:30:36 +01002958
Yunqing Wangd1f32e62019-02-20 10:37:51 -08002959#if CONFIG_COLLECT_COMPONENT_TIMING
2960 start_timing(cpi, encode_frame_to_data_rate_time);
2961#endif
2962
chiyotsai8b8f8a22020-04-21 11:03:47 -07002963 if (frame_is_intra_only(cm)) {
2964 av1_set_screen_content_options(cpi, features);
2965 cpi->is_screen_content_type = features->allow_screen_content_tools;
2966 }
2967
Fangwen Fu8d164de2016-12-14 13:40:54 -08002968 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00002969 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07002970
Vishesh0481a782020-06-11 14:32:46 +05302971 cm->tiles.large_scale = tile_cfg->enable_large_scale_tile;
2972 cm->tiles.single_tile_decoding = tile_cfg->enable_single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07002973
Urvang Joshib6409e92020-03-23 11:23:27 -07002974 features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
2975 // features->allow_ref_frame_mvs needs to be written into the frame header
Urvang Joshi54ffae72020-03-23 13:37:10 -07002976 // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case
Urvang Joshib6409e92020-03-23 11:23:27 -07002977 // is separated from frame_might_allow_ref_frame_mvs().
Urvang Joshi54ffae72020-03-23 13:37:10 -07002978 features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
Yunqing Wangd48fb162018-06-15 10:55:28 -07002979
Vishesh015b4962020-06-15 14:19:55 +05302980 features->allow_warped_motion = oxcf->motion_mode_cfg.allow_warped_motion &&
2981 frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002982
Urvang Joshi1de67aa2020-03-20 11:21:57 -07002983 cpi->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05302984
Sarah Parker1a72a1c2020-04-24 15:49:47 -07002985 if (frame_is_sframe(cm)) {
2986 GF_GROUP *gf_group = &cpi->gf_group;
Sarah Parker1a72a1c2020-04-24 15:49:47 -07002987 // S frame will wipe out any previously encoded altref so we cannot place
2988 // an overlay frame
2989 gf_group->update_type[gf_group->size] = GF_UPDATE;
Sarah Parker1a72a1c2020-04-24 15:49:47 -07002990 }
2991
Sarah Parker33005522018-07-27 14:46:25 -07002992 if (encode_show_existing_frame(cm)) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +05302993 av1_finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002994 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00002995 int largest_tile_id = 0; // Output from bitstream: unused here
2996 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08002997 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002998
David Turner90311862018-11-29 13:34:36 +00002999 if (seq_params->frame_id_numbers_present_flag &&
3000 current_frame->frame_type == KEY_FRAME) {
3001 // Displaying a forward key-frame, so reset the ref buffer IDs
3002 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
3003 for (int i = 0; i < REF_FRAMES; i++)
3004 cm->ref_frame_id[i] = display_frame_id;
3005 }
3006
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07003007 cpi->seq_params_locked = 1;
3008
David Turner73245762019-02-11 16:42:34 +00003009 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3010 // for the purpose to verify no mismatch between encoder and decoder.
3011 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003012
David Turner73245762019-02-11 16:42:34 +00003013 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003014
Yaowu Xuc27fc142016-08-22 16:08:15 -07003015 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
3016 // to do post-encoding update accordingly.
3017 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003018 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07003019 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003020 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003021
Mufaddal Chakeraea4c6d52020-06-12 15:09:14 +05303022 if (is_psnr_calc_enabled(cpi)) {
3023 cpi->source =
3024 realloc_and_scale_source(cpi, cm->cur_frame->buf.y_crop_width,
3025 cm->cur_frame->buf.y_crop_height);
3026 }
bohanli99852502020-07-14 16:22:45 -07003027
3028 // current_frame->frame_number is incremented already for
3029 // keyframe overlays.
3030 if (!av1_check_keyframe_overlay(cpi->gf_group.index, &cpi->gf_group,
3031 cpi->rc.frames_since_key))
3032 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003033
Tom Finegane4099e32018-01-23 12:01:51 -08003034 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003035 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003036
David Turnerefed6372019-01-11 15:14:11 +00003037 // Work out whether to force_integer_mv this frame
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303038 if (!is_stat_generation_stage(cpi) &&
Urvang Joshib6409e92020-03-23 11:23:27 -07003039 cpi->common.features.allow_screen_content_tools &&
3040 !frame_is_intra_only(cm)) {
David Turnerefed6372019-01-11 15:14:11 +00003041 if (cpi->common.seq_params.force_integer_mv == 2) {
3042 // Adaptive mode: see what previous frame encoded did
3043 if (cpi->unscaled_last_source != NULL) {
Satish Kumar Suman36adf352020-06-26 11:20:10 +05303044 features->cur_frame_force_integer_mv = av1_is_integer_mv(
Vishesh2260a592020-04-06 15:34:51 +05303045 cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info);
David Turnerefed6372019-01-11 15:14:11 +00003046 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07003047 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00003048 }
3049 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07003050 cpi->common.features.cur_frame_force_integer_mv =
David Turnerefed6372019-01-11 15:14:11 +00003051 cpi->common.seq_params.force_integer_mv;
3052 }
3053 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07003054 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00003055 }
Debargha Mukherjeea4c6f692021-03-16 19:45:46 -07003056#if CONFIG_NEW_INTER_MODES
3057 set_max_drl_bits(cpi);
3058#endif // CONFIG_NEW_INTER_MODES
David Turnerefed6372019-01-11 15:14:11 +00003059
Yaowu Xuc27fc142016-08-22 16:08:15 -07003060 // Set default state for segment based loop filter update flags.
3061 cm->lf.mode_ref_delta_update = 0;
3062
Yaowu Xuc27fc142016-08-22 16:08:15 -07003063 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05003064 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003065 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07003066 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003067
3068 // If segmentation is enabled force a map update for key frames.
3069 if (seg->enabled) {
3070 seg->update_map = 1;
3071 seg->update_data = 1;
3072 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003073 }
Vishesh0481a782020-06-11 14:32:46 +05303074 if (tile_cfg->mtu == 0) {
3075 cpi->num_tg = tile_cfg->num_tile_groups;
Thomas Daviesaf6df172016-11-09 14:04:18 +00003076 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08003077 // Use a default value for the purposes of weighting costs in probability
3078 // updates
Urvang Joshi1de67aa2020-03-20 11:21:57 -07003079 cpi->num_tg = DEFAULT_MAX_NUM_TG;
Thomas Daviesaf6df172016-11-09 14:04:18 +00003080 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003081
3082 // For 1 pass CBR, check if we are dropping this frame.
3083 // Never drop on key frame.
Vishesh39e74092020-06-16 17:13:48 +05303084 if (has_no_stats_stage(cpi) && oxcf->rc_cfg.mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00003085 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003086 if (av1_rc_drop_frame(cpi)) {
bohanli1629a4b2020-06-11 16:15:14 -07003087 av1_setup_frame_size(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003088 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00003089 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08003090 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003091 }
3092 }
3093
Jayasanker Jdee179d2020-07-10 23:47:37 +05303094 if (oxcf->tune_cfg.tuning == AOM_TUNE_SSIM)
3095 av1_set_mb_ssim_rdmult_scaling(cpi);
sdengc23c7f12019-06-11 16:56:50 -07003096
sdeng01959162019-12-20 10:46:24 -08003097#if CONFIG_TUNE_VMAF
Vishesh94a65292020-07-01 15:28:53 +05303098 if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
Sai Deng7275a1a2020-08-31 12:06:09 -07003099 oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN ||
3100 oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
sdeng01959162019-12-20 10:46:24 -08003101 av1_set_mb_vmaf_rdmult_scaling(cpi);
sdeng29c62152019-12-09 12:44:18 -08003102 }
sdeng01959162019-12-20 10:46:24 -08003103#endif
sdeng29c62152019-12-09 12:44:18 -08003104
Yaowu Xuf883b422016-08-30 14:01:10 -07003105 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003106
3107#if CONFIG_INTERNAL_STATS
3108 memset(cpi->mode_chosen_counts, 0,
3109 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
3110#endif
3111
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003112 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003113 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08003114 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003115 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00003116 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08003117 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07003118 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
3119 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
3120 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00003121 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07003122 lsb = cpi->source->y_buffer[0] & 0xff;
3123 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00003124 }
David Turner760a2f42018-12-07 15:25:36 +00003125 cm->current_frame_id =
3126 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05003127
3128 // S_frame is meant for stitching different streams of different
3129 // resolutions together, so current_frame_id must be the
3130 // same across different streams of the same content current_frame_id
3131 // should be the same and not random. 0x37 is a chosen number as start
3132 // point
Vishesh7e9873d2020-06-08 15:41:33 +05303133 if (oxcf->kf_cfg.sframe_dist != 0) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003134 } else {
3135 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00003136 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
3137 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003138 }
3139 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003140
Vishesha36c8d02020-06-30 15:42:35 +05303141 switch (oxcf->algo_cfg.cdf_update_mode) {
Hui Su483a8452018-02-26 12:28:48 -08003142 case 0: // No CDF update for any frames(4~6% compression loss).
Urvang Joshib6409e92020-03-23 11:23:27 -07003143 features->disable_cdf_update = 1;
Hui Su483a8452018-02-26 12:28:48 -08003144 break;
3145 case 1: // Enable CDF update for all frames.
Urvang Joshib6409e92020-03-23 11:23:27 -07003146 features->disable_cdf_update = 0;
Hui Su483a8452018-02-26 12:28:48 -08003147 break;
3148 case 2:
3149 // Strategically determine at which frames to do CDF update.
3150 // Currently only enable CDF update for all-intra and no-show frames(1.5%
3151 // compression loss).
3152 // TODO(huisu@google.com): design schemes for various trade-offs between
3153 // compression quality and decoding speed.
Urvang Joshib6409e92020-03-23 11:23:27 -07003154 features->disable_cdf_update =
Hui Sub1b76b32018-02-27 15:24:48 -08003155 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08003156 break;
3157 }
Urvang Joshi450a9a22020-03-31 16:00:22 -07003158 seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08003159
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07003160 int largest_tile_id = 0;
Satish Kumar Suman7a5f5f42020-06-09 14:20:47 +05303161 if (av1_superres_in_recode_allowed(cpi)) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07003162 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
3163 AOM_CODEC_OK) {
3164 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003165 }
Hui Su06463e42018-02-23 22:17:36 -08003166 } else {
Urvang Joshi1e86b782020-10-20 11:25:45 -07003167 const aom_superres_mode orig_superres_mode = cpi->superres_mode; // save
Urvang Joshidf265712020-10-05 23:23:09 -07003168 cpi->superres_mode = cpi->oxcf.superres_cfg.superres_mode;
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07003169 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
3170 &largest_tile_id) != AOM_CODEC_OK) {
3171 return AOM_CODEC_ERROR;
3172 }
Urvang Joshi1e86b782020-10-20 11:25:45 -07003173 cpi->superres_mode = orig_superres_mode; // restore
Hui Su06463e42018-02-23 22:17:36 -08003174 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003175
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07003176 cpi->seq_params_locked = 1;
3177
David Turner996b2c12018-12-07 15:52:30 +00003178 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003179 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00003180 for (int i = 0; i < REF_FRAMES; i++) {
3181 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003182 cm->ref_frame_id[i] = cm->current_frame_id;
3183 }
3184 }
3185 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003186
Fyodor Kyslov34b591f2020-06-24 20:13:58 -07003187 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
3188 cpi->svc.num_encoded_top_layer++;
3189
Soo-Chul Han934af352017-10-15 15:21:51 -04003190 if (cm->seg.enabled) {
3191 if (cm->seg.update_map) {
3192 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07003193 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00003194 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Urvang Joshie5fd6462020-10-30 09:58:36 -07003195 cm->cur_frame->mi_cols * cm->cur_frame->mi_rows *
3196 sizeof(*cm->cur_frame->seg_map));
Soo-Chul Han934af352017-10-15 15:21:51 -04003197 }
3198 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003199
3200 if (frame_is_intra_only(cm) == 0) {
3201 release_scaled_references(cpi);
3202 }
3203
David Turner73245762019-02-11 16:42:34 +00003204 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3205 // for the purpose to verify no mismatch between encoder and decoder.
3206 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
3207
3208 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003209
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003210#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07003211 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003212#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003213
Urvang Joshi6237b882020-03-26 15:02:26 -07003214 if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00003215 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07003216 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003217 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07003218 if (!cm->tiles.large_scale) {
David Turnera4c96252019-01-11 16:36:39 +00003219 cm->cur_frame->frame_context = *cm->fc;
3220 }
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08003221
Vishesh0481a782020-06-11 14:32:46 +05303222 if (tile_cfg->enable_ext_tile_debug) {
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08003223 // (yunqing) This test ensures the correctness of large scale tile coding.
Urvang Joshi54ffae72020-03-23 13:37:10 -07003224 if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) {
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08003225 char fn[20] = "./fc";
3226 fn[4] = current_frame->frame_number / 100 + '0';
3227 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
3228 fn[6] = (current_frame->frame_number % 10) + '0';
3229 fn[7] = '\0';
3230 av1_print_frame_contexts(cm->fc, fn);
3231 }
David Turnera4c96252019-01-11 16:36:39 +00003232 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003233
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003234#if CONFIG_COLLECT_COMPONENT_TIMING
3235 end_timing(cpi, encode_frame_to_data_rate_time);
3236
3237 // Print out timing information.
3238 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08003239 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003240 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08003241 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003242 for (i = 0; i < kTimingComponents; i++) {
3243 cpi->component_time[i] += cpi->frame_component_time[i];
3244 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
3245 get_component_name(i), cpi->frame_component_time[i],
3246 cpi->component_time[i]);
3247 cpi->frame_component_time[i] = 0;
3248 }
3249#endif
3250
Urvang Joshi1de67aa2020-03-20 11:21:57 -07003251 cpi->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003252
Yaowu Xuf883b422016-08-30 14:01:10 -07003253 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003254
Yaowu Xuc27fc142016-08-22 16:08:15 -07003255 // Clear the one shot update flags for segmentation map and mode/ref loop
3256 // filter deltas.
3257 cm->seg.update_map = 0;
3258 cm->seg.update_data = 0;
3259 cm->lf.mode_ref_delta_update = 0;
3260
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07003261 // A droppable frame might not be shown but it always
3262 // takes a space in the gf group. Therefore, even when
3263 // it is not shown, we still need update the count down.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003264 if (cm->show_frame) {
bohanli99852502020-07-14 16:22:45 -07003265 // Don't increment frame counters if this is a key frame overlay
3266 if (!av1_check_keyframe_overlay(cpi->gf_group.index, &cpi->gf_group,
3267 cpi->rc.frames_since_key))
3268 ++current_frame->frame_number;
3269 } else if (av1_check_keyframe_arf(cpi->gf_group.index, &cpi->gf_group,
3270 cpi->rc.frames_since_key)) {
3271 // TODO(bohanli) Hack here: increment kf overlay before it is encoded
David Turnerd2a592e2018-11-16 14:59:31 +00003272 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003273 }
3274
Tom Finegane4099e32018-01-23 12:01:51 -08003275 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003276}
3277
David Turner056f7cd2019-01-07 17:48:13 +00003278int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00003279 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00003280 const EncodeFrameParams *const frame_params,
3281 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00003282 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00003283 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00003284
David Turnercb5e36f2019-01-17 17:15:25 +00003285 cpi->unscaled_source = frame_input->source;
3286 cpi->source = frame_input->source;
3287 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00003288
David Turner6e8b4d92019-02-18 15:01:33 +00003289 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
Urvang Joshib6409e92020-03-23 11:23:27 -07003290 cm->features.error_resilient_mode = frame_params->error_resilient_mode;
Urvang Joshi6237b882020-03-26 15:02:26 -07003291 cm->features.primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00003292 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00003293 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00003294 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00003295 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00003296 cm->show_existing_frame = frame_params->show_existing_frame;
3297 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00003298
David Turner73245762019-02-11 16:42:34 +00003299 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
3300 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
3301
Jayasanker J24cb9bc2020-04-15 13:43:10 +05303302 memcpy(&cpi->refresh_frame, &frame_params->refresh_frame,
3303 sizeof(cpi->refresh_frame));
David Turnerfe3aecb2019-02-06 14:42:42 +00003304
Urvang Joshi3fdaf5c2020-07-15 10:17:48 -07003305 if (current_frame->frame_type == KEY_FRAME && !cpi->no_show_fwd_kf) {
liang zhao7aebbd12020-07-03 22:20:34 -07003306 current_frame->key_frame_number += current_frame->frame_number;
David Turnera7f133c2019-01-22 14:47:16 +00003307 current_frame->frame_number = 0;
liang zhao7aebbd12020-07-03 22:20:34 -07003308 }
David Turnera7f133c2019-01-22 14:47:16 +00003309
Remyafc038662020-02-06 11:46:11 +05303310 current_frame->order_hint =
3311 current_frame->frame_number + frame_params->order_offset;
3312 current_frame->display_order_hint = current_frame->order_hint;
Sarah Parkerd9dc3fd2020-08-18 11:11:15 -07003313 current_frame->pyramid_level = get_true_pyr_level(
3314 cpi->gf_group.layer_depth[cpi->gf_group.index],
3315 current_frame->display_order_hint, cpi->gf_group.max_layer_depth);
liang zhao7aebbd12020-07-03 22:20:34 -07003316
3317 current_frame->absolute_poc =
3318 current_frame->key_frame_number + current_frame->display_order_hint;
3319
Remyafc038662020-02-06 11:46:11 +05303320 current_frame->order_hint %=
3321 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
David Turnera7f133c2019-01-22 14:47:16 +00003322
Debargha Mukherjee7118f422020-06-29 15:49:06 -07003323 if (is_stat_generation_stage(cpi)) {
Debargha Mukherjee7118f422020-06-29 15:49:06 -07003324 av1_first_pass(cpi, frame_input->ts_duration);
Debargha Mukherjee7118f422020-06-29 15:49:06 -07003325 } else {
3326 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
3327 AOM_CODEC_OK) {
3328 return AOM_CODEC_ERROR;
3329 }
3330 }
David Turner056f7cd2019-01-07 17:48:13 +00003331 return AOM_CODEC_OK;
3332}
3333
Neil Birkbecka2893ab2018-06-08 14:45:13 -07003334#if CONFIG_DENOISE
3335static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
3336 int block_size, float noise_level,
3337 int64_t time_stamp, int64_t end_time) {
3338 AV1_COMMON *const cm = &cpi->common;
3339 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003340 cpi->denoise_and_model = aom_denoise_and_model_alloc(
3341 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07003342 if (!cpi->denoise_and_model) {
3343 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3344 "Error allocating denoise and model");
3345 return -1;
3346 }
3347 }
3348 if (!cpi->film_grain_table) {
3349 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
3350 if (!cpi->film_grain_table) {
3351 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3352 "Error allocating grain table");
3353 return -1;
3354 }
3355 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
3356 }
3357 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
3358 &cm->film_grain_params)) {
3359 if (cm->film_grain_params.apply_grain) {
3360 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
3361 &cm->film_grain_params);
3362 }
3363 }
3364 return 0;
3365}
3366#endif
3367
James Zern3e2613b2017-03-30 23:14:40 -07003368int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07003369 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3370 int64_t end_time) {
3371 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003372 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003373 int res = 0;
3374 const int subsampling_x = sd->subsampling_x;
3375 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003376 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003377
sdenge63f9fa2019-12-13 09:29:55 -08003378#if CONFIG_TUNE_VMAF
3379 if (!is_stat_generation_stage(cpi) &&
Vishesh94a65292020-07-01 15:28:53 +05303380 cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
sdeng615dc242020-02-04 16:06:14 -08003381 av1_vmaf_frame_preprocessing(cpi, sd);
3382 }
3383 if (!is_stat_generation_stage(cpi) &&
Vishesh94a65292020-07-01 15:28:53 +05303384 cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
sdeng615dc242020-02-04 16:06:14 -08003385 av1_vmaf_blk_preprocessing(cpi, sd);
sdenge63f9fa2019-12-13 09:29:55 -08003386 }
3387#endif
3388
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003389#if CONFIG_INTERNAL_STATS
3390 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07003391 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003392#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07003393#if CONFIG_DENOISE
3394 if (cpi->oxcf.noise_level > 0)
3395 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
3396 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
3397 res = -1;
3398#endif // CONFIG_DENOISE
3399
Yaowu Xuf883b422016-08-30 14:01:10 -07003400 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08003401 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07003402 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003403#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003404 aom_usec_timer_mark(&timer);
3405 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003406#endif
Yunqing Wangaae7a162020-07-09 16:03:01 -07003407
3408 // Note: Regarding profile setting, the following checks are added to help
3409 // choose a proper profile for the input video. The criterion is that all
3410 // bitstreams must be designated as the lowest profile that match its content.
3411 // E.G. A bitstream that contains 4:4:4 video must be designated as High
3412 // Profile in the seq header, and likewise a bitstream that contains 4:2:2
3413 // bitstream must be designated as Professional Profile in the sequence
3414 // header.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003415 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003417 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003418 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003419 res = -1;
3420 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003421 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003422 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003423 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003424 "Profile 1 requires 4:4:4 color format");
3425 res = -1;
3426 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003427 if ((seq_params->profile == PROFILE_2) &&
3428 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003429 !(subsampling_x == 1 && subsampling_y == 0)) {
3430 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yunqing Wangaae7a162020-07-09 16:03:01 -07003431 "Profile 2 bit-depth <= 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003432 res = -1;
3433 }
3434
3435 return res;
3436}
3437
Yaowu Xuc27fc142016-08-22 16:08:15 -07003438#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003439extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
3440 const unsigned char *img2, int img2_pitch,
3441 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003442
3443static void adjust_image_stat(double y, double u, double v, double all,
3444 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003445 s->stat[STAT_Y] += y;
3446 s->stat[STAT_U] += u;
3447 s->stat[STAT_V] += v;
3448 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07003449 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003450}
3451
Angie Chiang08a22a62017-07-17 17:29:17 -07003452static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003453 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003454 double samples = 0.0;
Vishesh4342d7f2020-06-29 18:49:06 +05303455 const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
Yaowu Xue75b58a2020-01-03 12:56:12 -08003456 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003457
Angie Chiang08a22a62017-07-17 17:29:17 -07003458#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00003459 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07003460#endif
3461 cpi->bytes += frame_bytes;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003462 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07003463 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00003464 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003465 double y, u, v, frame_all;
3466
3467 cpi->count++;
3468 if (cpi->b_calculate_psnr) {
3469 PSNR_STATS psnr;
3470 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003471 aom_clear_system_state();
Yaowu Xue75b58a2020-01-03 12:56:12 -08003472 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003473 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
3474 &cpi->psnr);
3475 cpi->total_sq_error += psnr.sse[0];
3476 cpi->total_samples += psnr.samples[0];
3477 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08003478 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003479 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003480 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07003481 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482 else
Yaowu Xuf883b422016-08-30 14:01:10 -07003483 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003484
Yaowu Xuf883b422016-08-30 14:01:10 -07003485 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003486 cpi->summed_quality += frame_ssim2 * weight;
3487 cpi->summed_weights += weight;
3488
3489#if 0
3490 {
3491 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08003492 double y2 = psnr.psnr[1];
3493 double u2 = psnr.psnr[2];
3494 double v2 = psnr.psnr[3];
3495 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003496 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 +00003497 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003498 frame_psnr2, frame_ssim2);
3499 fclose(f);
3500 }
3501#endif
3502 }
3503 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003504 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003505 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07003506 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
3507 recon->y_stride, orig->y_width, orig->y_height);
3508 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003509 cpi->total_blockiness += frame_blockiness;
3510 }
3511
3512 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003513 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003514 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003515 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
3516 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
3517
3518 const double peak = (double)((1 << in_bit_depth) - 1);
3519 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003520 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003521 if (consistency > 0.0)
3522 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003523 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003524 cpi->total_inconsistency += this_inconsistency;
3525 }
3526 }
3527 }
3528
3529 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07003530 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003531 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07003532 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003533 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
3534 }
3535}
3536#endif // CONFIG_INTERNAL_STATS
Wan-Teh Chang1c520052020-06-11 10:17:56 -07003537
Andrey Norkin795ba872018-03-06 13:24:14 -08003538int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
3539 size_t *size, uint8_t *dest, int64_t *time_stamp,
3540 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07003541 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003542 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3543 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003544
3545#if CONFIG_BITSTREAM_DEBUG
Urvang Joshie2b505b2020-06-19 11:39:49 -07003546 assert(cpi->oxcf.max_threads <= 1 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003547 "bitstream debug tool does not support multithreading");
3548 bitstream_queue_record_write();
angiebird1f7174f2020-06-27 16:23:16 -07003549 aom_bitstream_queue_set_frame_write(cm->current_frame.order_hint * 2 +
Yaowu Xu63f2ea32019-04-29 10:47:42 -07003550 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003551#endif
Marco Paniconi63971322019-08-15 21:32:05 -07003552 if (cpi->use_svc && cm->number_spatial_layers > 1) {
3553 av1_one_pass_cbr_svc_start_layer(cpi);
3554 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003555
Dominic Symesd4929012018-01-31 17:32:01 +01003556 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00003557 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003558#if CONFIG_INTERNAL_STATS
3559 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07003560 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003561#endif
chiyotsaic666b1f2019-12-20 10:44:58 -08003562 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003563
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07003564 // Normal defaults
Visheshaa6a1702020-06-30 19:27:22 +05303565 cm->features.refresh_frame_context =
3566 oxcf->tool_cfg.frame_parallel_decoding_mode
3567 ? REFRESH_FRAME_CONTEXT_DISABLED
3568 : REFRESH_FRAME_CONTEXT_BACKWARD;
Vishesh0481a782020-06-11 14:32:46 +05303569 if (oxcf->tile_cfg.enable_large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -07003570 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003571
Sarah Parkerb9041612018-05-22 19:06:47 -07003572 // Initialize fields related to forward keyframes
bohanli6f22c7a2020-07-14 10:52:50 -07003573 cpi->no_show_fwd_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08003574
David Turnerdedd8ff2019-01-23 13:59:46 +00003575 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003576
Yue Chen1bc5be62018-08-24 13:57:32 -07003577 const int result =
3578 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
3579 timestamp_ratio, flush);
Wan-Teh Chang1c520052020-06-11 10:17:56 -07003580 if (result == -1) {
David Turnerdedd8ff2019-01-23 13:59:46 +00003581 // Returning -1 indicates no frame encoded; more input is required
3582 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003583 }
Wan-Teh Chang1c520052020-06-11 10:17:56 -07003584 if (result != AOM_CODEC_OK) {
3585 return AOM_CODEC_ERROR;
3586 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08003587#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003588 aom_usec_timer_mark(&cmptimer);
3589 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07003590#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjeedce68c52020-07-02 13:06:58 -07003591 // Note *size = 0 indicates a dropeed frame for which psnr is not calculated
3592 if (cpi->b_calculate_psnr && *size > 0) {
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303593 if (cm->show_existing_frame ||
3594 (!is_stat_generation_stage(cpi) && cm->show_frame)) {
David Turnerc4bf8c72019-01-15 13:14:37 +00003595 generate_psnr_packet(cpi);
3596 }
3597 }
Hui Su8ea87322019-03-29 14:44:32 -07003598
Vishesh8ac928b2020-04-01 02:36:35 +05303599 if (cpi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) {
Hui Su8ea87322019-03-29 14:44:32 -07003600 // Initialize level info. at the beginning of each sequence.
bohanli6f22c7a2020-07-14 10:52:50 -07003601 if (cm->current_frame.frame_type == KEY_FRAME && !cpi->no_show_fwd_kf) {
Hui Su58753d62019-05-29 09:56:19 -07003602 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07003603 }
kyslovabeeb7c2019-03-06 18:35:04 -08003604 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07003605 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003606
3607#if CONFIG_INTERNAL_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303608 if (!is_stat_generation_stage(cpi)) {
Angie Chiang08a22a62017-07-17 17:29:17 -07003609 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003610 }
3611#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003612#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303613 if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
chiyotsai4c1e5c62020-04-30 17:54:14 -07003614 cpi->tx_search_count += cpi->td.mb.txfm_search_info.tx_search_count;
3615 cpi->td.mb.txfm_search_info.tx_search_count = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003616 }
3617#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003618
Yaowu Xuf883b422016-08-30 14:01:10 -07003619 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003620
Hui Su0d0ee662019-07-29 14:38:36 -07003621 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003622}
3623
Yaowu Xuf883b422016-08-30 14:01:10 -07003624int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
3625 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003626 if (!cm->show_frame) {
3627 return -1;
3628 } else {
3629 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00003630 if (cm->cur_frame != NULL) {
3631 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003632 dest->y_width = cm->width;
3633 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003634 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
3635 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003636 ret = 0;
3637 } else {
3638 ret = -1;
3639 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003640 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003641 return ret;
3642 }
3643}
3644
Yaowu Xuf883b422016-08-30 14:01:10 -07003645int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00003646 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003647
David Turnere7ebf902018-12-04 14:04:55 +00003648 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003649 return 0;
3650}
3651
Yunqing Wang93b18f32018-06-08 21:08:29 -07003652aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
3653 YV12_BUFFER_CONFIG *new_frame,
3654 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07003655 const int num_planes = av1_num_planes(cm);
3656 if (!equal_dimensions_and_border(new_frame, sd))
3657 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
3658 "Incorrect buffer dimensions");
3659 else
3660 aom_yv12_copy_frame(new_frame, sd, num_planes);
3661
3662 return cm->error.error_code;
3663}
3664
Jayasanker J0b1dd292020-04-09 15:47:25 +05303665int av1_set_internal_size(AV1EncoderConfig *const oxcf,
3666 ResizePendingParams *resize_pending_params,
3667 AOM_SCALING horiz_mode, AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003668 int hr = 0, hs = 0, vr = 0, vs = 0;
3669
3670 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
3671
3672 Scale2Ratio(horiz_mode, &hr, &hs);
3673 Scale2Ratio(vert_mode, &vr, &vs);
3674
3675 // always go to the next whole number
Vishesh2b8e4792020-06-12 12:11:19 +05303676 resize_pending_params->width = (hs - 1 + oxcf->frm_dim_cfg.width * hr) / hs;
3677 resize_pending_params->height = (vs - 1 + oxcf->frm_dim_cfg.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003678
Deepa K G260c8262020-07-08 19:13:54 +05303679 if (horiz_mode != NORMAL || vert_mode != NORMAL) {
Marco Paniconi72056922020-06-24 12:31:25 -07003680 oxcf->resize_cfg.resize_mode = RESIZE_FIXED;
Deepa K G260c8262020-07-08 19:13:54 +05303681 oxcf->algo_cfg.enable_tpl_model = 0;
3682 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003683 return 0;
3684}
3685
Urvang Joshi17814622020-03-27 17:26:17 -07003686int av1_get_quantizer(AV1_COMP *cpi) {
3687 return cpi->common.quant_params.base_qindex;
3688}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003689
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04003690int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
3691 size_t output_size = 0;
3692 size_t total_bytes_read = 0;
3693 size_t remaining_size = *frame_size;
3694 uint8_t *buff_ptr = buffer;
3695
3696 // go through each OBUs
3697 while (total_bytes_read < *frame_size) {
3698 uint8_t saved_obu_header[2];
3699 uint64_t obu_payload_size;
3700 size_t length_of_payload_size;
3701 size_t length_of_obu_size;
3702 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
3703 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
3704
3705 // save the obu header (1 or 2 bytes)
3706 memmove(saved_obu_header, buff_ptr, obu_header_size);
3707 // clear the obu_has_size_field
3708 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
3709
3710 // get the payload_size and length of payload_size
3711 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
3712 &obu_payload_size, &length_of_payload_size) != 0) {
3713 return AOM_CODEC_ERROR;
3714 }
3715 obu_bytes_read += length_of_payload_size;
3716
3717 // calculate the length of size of the obu header plus payload
3718 length_of_obu_size =
3719 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
3720
3721 // move the rest of data to new location
3722 memmove(buff_ptr + length_of_obu_size + obu_header_size,
3723 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07003724 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04003725
3726 // write the new obu size
3727 const uint64_t obu_size = obu_header_size + obu_payload_size;
3728 size_t coded_obu_size;
3729 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
3730 &coded_obu_size) != 0) {
3731 return AOM_CODEC_ERROR;
3732 }
3733
3734 // write the saved (modified) obu_header following obu size
3735 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
3736
3737 total_bytes_read += obu_bytes_read;
3738 remaining_size -= obu_bytes_read;
3739 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07003740 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04003741 }
3742
3743 *frame_size = output_size;
3744 return AOM_CODEC_OK;
3745}
3746
Vishesha195ca32020-04-07 18:46:20 +05303747static void svc_set_updates_external_ref_frame_config(
Vishesh38c05d72020-04-14 12:19:14 +05303748 ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags, SVC *const svc) {
3749 ext_refresh_frame_flags->update_pending = 1;
3750 ext_refresh_frame_flags->last_frame = svc->refresh[svc->ref_idx[0]];
3751 ext_refresh_frame_flags->golden_frame = svc->refresh[svc->ref_idx[3]];
3752 ext_refresh_frame_flags->bwd_ref_frame = svc->refresh[svc->ref_idx[4]];
3753 ext_refresh_frame_flags->alt2_ref_frame = svc->refresh[svc->ref_idx[5]];
3754 ext_refresh_frame_flags->alt_ref_frame = svc->refresh[svc->ref_idx[6]];
Vishesha195ca32020-04-07 18:46:20 +05303755 svc->non_reference_frame = 1;
Marco Paniconid8574e32019-08-04 21:30:12 -07003756 for (int i = 0; i < REF_FRAMES; i++) {
Vishesha195ca32020-04-07 18:46:20 +05303757 if (svc->refresh[i] == 1) {
3758 svc->non_reference_frame = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07003759 break;
3760 }
3761 }
3762}
3763
Marco Paniconiad4953a2020-04-02 09:52:58 -07003764static int svc_set_references_external_ref_frame_config(AV1_COMP *cpi) {
3765 // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
3766 // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
3767 int ref = AOM_REFFRAME_ALL;
3768 for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
3769 if (!cpi->svc.reference[i]) ref ^= (1 << i);
3770 }
3771 return ref;
3772}
3773
Yaowu Xuf883b422016-08-30 14:01:10 -07003774void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003775 // TODO(yunqingwang): For what references to use, external encoding flags
3776 // should be consistent with internal reference frame selection. Need to
3777 // ensure that there is not conflict between the two. In AV1 encoder, the
3778 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07003779 // GOLDEN, BWDREF, ALTREF2.
Vishesha195ca32020-04-07 18:46:20 +05303780
3781 ExternalFlags *const ext_flags = &cpi->ext_flags;
Vishesh38c05d72020-04-14 12:19:14 +05303782 ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
3783 &ext_flags->refresh_frame;
Vishesha195ca32020-04-07 18:46:20 +05303784 ext_flags->ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003785 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003786 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
3787 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
3788 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07003789 int ref = AOM_REFFRAME_ALL;
3790
3791 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
3792 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
3793 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
3794
3795 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
3796
3797 if (flags & AOM_EFLAG_NO_REF_ARF) {
3798 ref ^= AOM_ALT_FLAG;
3799 ref ^= AOM_BWD_FLAG;
3800 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003801 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07003802 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
3803 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003804 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07003805
Vishesha195ca32020-04-07 18:46:20 +05303806 av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
Marco Paniconiad4953a2020-04-02 09:52:58 -07003807 } else {
3808 if (cpi->svc.external_ref_frame_config) {
3809 int ref = svc_set_references_external_ref_frame_config(cpi);
Vishesha195ca32020-04-07 18:46:20 +05303810 av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
Marco Paniconiad4953a2020-04-02 09:52:58 -07003811 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003812 }
3813
3814 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003815 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003816 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003817
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003818 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
3819 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003820
Yaowu Xuf883b422016-08-30 14:01:10 -07003821 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003822
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003823 if (flags & AOM_EFLAG_NO_UPD_ARF) {
3824 upd ^= AOM_ALT_FLAG;
3825 upd ^= AOM_BWD_FLAG;
3826 upd ^= AOM_ALT2_FLAG;
3827 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003828
Vishesh38c05d72020-04-14 12:19:14 +05303829 ext_refresh_frame_flags->last_frame = (upd & AOM_LAST_FLAG) != 0;
3830 ext_refresh_frame_flags->golden_frame = (upd & AOM_GOLD_FLAG) != 0;
3831 ext_refresh_frame_flags->alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
3832 ext_refresh_frame_flags->bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
3833 ext_refresh_frame_flags->alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
3834 ext_refresh_frame_flags->update_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00003835 } else {
Marco Paniconid8574e32019-08-04 21:30:12 -07003836 if (cpi->svc.external_ref_frame_config)
Vishesh38c05d72020-04-14 12:19:14 +05303837 svc_set_updates_external_ref_frame_config(ext_refresh_frame_flags,
3838 &cpi->svc);
Marco Paniconid8574e32019-08-04 21:30:12 -07003839 else
Vishesh38c05d72020-04-14 12:19:14 +05303840 ext_refresh_frame_flags->update_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003841 }
3842
Visheshaa6a1702020-06-30 19:27:22 +05303843 ext_flags->use_ref_frame_mvs = cpi->oxcf.tool_cfg.enable_ref_frame_mvs &
Vishesha195ca32020-04-07 18:46:20 +05303844 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
Visheshaa6a1702020-06-30 19:27:22 +05303845 ext_flags->use_error_resilient = cpi->oxcf.tool_cfg.error_resilient_mode |
Vishesha195ca32020-04-07 18:46:20 +05303846 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
3847 ext_flags->use_s_frame =
Vishesh7e9873d2020-06-08 15:41:33 +05303848 cpi->oxcf.kf_cfg.enable_sframe | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Vishesha195ca32020-04-07 18:46:20 +05303849 ext_flags->use_primary_ref_none =
3850 (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07003851
Yaowu Xuf883b422016-08-30 14:01:10 -07003852 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
Satish Kumar Suman329de4d2020-06-16 10:48:32 +05303853 update_entropy(&ext_flags->refresh_frame_context,
3854 &ext_flags->refresh_frame_context_pending, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003855 }
3856}
Andrey Norkin795ba872018-03-06 13:24:14 -08003857
Tom Fineganf8d6a162018-08-21 10:47:55 -07003858aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
3859 if (!cpi) return NULL;
3860
3861 uint8_t header_buf[512] = { 0 };
3862 const uint32_t sequence_header_size =
Urvang Joshi450a9a22020-03-31 16:00:22 -07003863 av1_write_sequence_header_obu(&cpi->common.seq_params, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07003864 assert(sequence_header_size <= sizeof(header_buf));
3865 if (sequence_header_size == 0) return NULL;
3866
3867 const size_t obu_header_size = 1;
3868 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
3869 const size_t payload_offset = obu_header_size + size_field_size;
3870
3871 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
3872 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
3873
Vishesh8ac928b2020-04-01 02:36:35 +05303874 if (av1_write_obu_header(&cpi->level_params, OBU_SEQUENCE_HEADER, 0,
3875 &header_buf[0]) != obu_header_size) {
Tom Fineganf8d6a162018-08-21 10:47:55 -07003876 return NULL;
3877 }
3878
3879 size_t coded_size_field_size = 0;
3880 if (aom_uleb_encode(sequence_header_size, size_field_size,
3881 &header_buf[obu_header_size],
3882 &coded_size_field_size) != 0) {
3883 return NULL;
3884 }
3885 assert(coded_size_field_size == size_field_size);
3886
3887 aom_fixed_buf_t *global_headers =
3888 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
3889 if (!global_headers) return NULL;
3890
3891 const size_t global_header_buf_size =
3892 obu_header_size + size_field_size + sequence_header_size;
3893
3894 global_headers->buf = malloc(global_header_buf_size);
3895 if (!global_headers->buf) {
3896 free(global_headers);
3897 return NULL;
3898 }
3899
3900 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
3901 global_headers->sz = global_header_buf_size;
3902 return global_headers;
3903}