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