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