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