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 "config/aom_config.h"
20 #include "config/aom_dsp_rtcd.h"
21 
22 #include "aom/aomcx.h"
23 
24 #if CONFIG_DENOISE
25 #include "aom_dsp/grain_table.h"
26 #include "aom_dsp/noise_util.h"
27 #include "aom_dsp/noise_model.h"
28 #endif
29 #include "aom_dsp/psnr.h"
30 #if CONFIG_INTERNAL_STATS
31 #include "aom_dsp/ssim.h"
32 #endif
33 #include "aom_ports/aom_timer.h"
34 #include "aom_ports/mem.h"
35 #include "aom_ports/system_state.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/aq_complexity.h"
50 #include "av1/encoder/aq_cyclicrefresh.h"
51 #include "av1/encoder/aq_variance.h"
52 #include "av1/encoder/bitstream.h"
53 #include "av1/encoder/context_tree.h"
54 #include "av1/encoder/encodeframe.h"
55 #include "av1/encoder/encodemv.h"
56 #include "av1/encoder/encode_strategy.h"
57 #include "av1/encoder/encoder.h"
58 #include "av1/encoder/encoder_alloc.h"
59 #include "av1/encoder/encoder_utils.h"
60 #include "av1/encoder/encodetxb.h"
61 #include "av1/encoder/ethread.h"
62 #include "av1/encoder/firstpass.h"
63 #include "av1/encoder/hash_motion.h"
64 #include "av1/encoder/intra_mode_search.h"
65 #include "av1/encoder/mv_prec.h"
66 #include "av1/encoder/pass2_strategy.h"
67 #include "av1/encoder/pickcdef.h"
68 #include "av1/encoder/picklpf.h"
69 #include "av1/encoder/pickrst.h"
70 #include "av1/encoder/random.h"
71 #include "av1/encoder/ratectrl.h"
72 #include "av1/encoder/rc_utils.h"
73 #include "av1/encoder/rd.h"
74 #include "av1/encoder/rdopt.h"
75 #include "av1/encoder/segmentation.h"
76 #include "av1/encoder/speed_features.h"
77 #include "av1/encoder/superres_scale.h"
78 #include "av1/encoder/tpl_model.h"
79 #include "av1/encoder/reconinter_enc.h"
80 #include "av1/encoder/var_based_part.h"
81 
82 #define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
83 
84 #if CONFIG_ENTROPY_STATS
85 FRAME_COUNTS aggregate_fc;
86 #endif  // CONFIG_ENTROPY_STATS
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,int * hr,int * hs)98 static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
99   switch (mode) {
100     case NORMAL:
101       *hr = 1;
102       *hs = 1;
103       break;
104     case FOURFIVE:
105       *hr = 4;
106       *hs = 5;
107       break;
108     case THREEFIVE:
109       *hr = 3;
110       *hs = 5;
111       break;
112     case THREEFOUR:
113       *hr = 3;
114       *hs = 4;
115       break;
116     case ONEFOUR:
117       *hr = 1;
118       *hs = 4;
119       break;
120     case ONEEIGHT:
121       *hr = 1;
122       *hs = 8;
123       break;
124     case ONETWO:
125       *hr = 1;
126       *hs = 2;
127       break;
128     default:
129       *hr = 1;
130       *hs = 1;
131       assert(0);
132       break;
133   }
134 }
135 
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_8x8 = 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 = 1;
146     if (new_map_16x16) {
147       int r, c;
148       for (r = 0; r < mi_rows; ++r) {
149         for (c = 0; c < mi_cols; ++c) {
150           active_map_8x8[r * mi_cols + c] =
151               new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
152                   ? AM_SEGMENT_ID_ACTIVE
153                   : AM_SEGMENT_ID_INACTIVE;
154         }
155       }
156       cpi->active_map.enabled = 1;
157     } else {
158       cpi->active_map.enabled = 0;
159     }
160     return 0;
161   } else {
162     return -1;
163   }
164 }
165 
av1_get_active_map(AV1_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)166 int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
167                        int cols) {
168   const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
169   if (rows == mi_params->mb_rows && cols == mi_params->mb_cols &&
170       new_map_16x16) {
171     unsigned char *const seg_map_8x8 = cpi->enc_seg.map;
172     const int mi_rows = mi_params->mi_rows;
173     const int mi_cols = mi_params->mi_cols;
174     const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
175     const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
176 
177     memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
178     if (cpi->active_map.enabled) {
179       int r, c;
180       for (r = 0; r < mi_rows; ++r) {
181         for (c = 0; c < mi_cols; ++c) {
182           // Cyclic refresh segments are considered active despite not having
183           // AM_SEGMENT_ID_ACTIVE
184           new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
185               seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
186         }
187       }
188     }
189     return 0;
190   } else {
191     return -1;
192   }
193 }
194 
av1_initialize_enc(void)195 void av1_initialize_enc(void) {
196   av1_rtcd();
197   aom_dsp_rtcd();
198   aom_scale_rtcd();
199   av1_init_intra_predictors();
200   av1_init_me_luts();
201   av1_rc_init_minq_luts();
202   av1_init_wedge_masks();
203 }
204 
update_reference_segmentation_map(AV1_COMP * cpi)205 static void update_reference_segmentation_map(AV1_COMP *cpi) {
206   AV1_COMMON *const cm = &cpi->common;
207   const CommonModeInfoParams *const mi_params = &cm->mi_params;
208   MB_MODE_INFO **mi_4x4_ptr = mi_params->mi_grid_base;
209   uint8_t *cache_ptr = cm->cur_frame->seg_map;
210 
211   for (int row = 0; row < mi_params->mi_rows; row++) {
212     MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
213     uint8_t *cache = cache_ptr;
214     for (int col = 0; col < mi_params->mi_cols; col++, mi_4x4++, cache++)
215       cache[0] = mi_4x4[0]->segment_id;
216     mi_4x4_ptr += mi_params->mi_stride;
217     cache_ptr += mi_params->mi_cols;
218   }
219 }
220 
av1_new_framerate(AV1_COMP * cpi,double framerate)221 void av1_new_framerate(AV1_COMP *cpi, double framerate) {
222   cpi->framerate = framerate < 0.1 ? 30 : framerate;
223   av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
224 }
225 
av1_get_compression_ratio(const AV1_COMMON * const cm,size_t encoded_frame_size)226 double av1_get_compression_ratio(const AV1_COMMON *const cm,
227                                  size_t encoded_frame_size) {
228   const int upscaled_width = cm->superres_upscaled_width;
229   const int height = cm->height;
230   const int luma_pic_size = upscaled_width * height;
231   const SequenceHeader *const seq_params = &cm->seq_params;
232   const BITSTREAM_PROFILE profile = seq_params->profile;
233   const int pic_size_profile_factor =
234       profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
235   encoded_frame_size =
236       (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
237   const size_t uncompressed_frame_size =
238       (luma_pic_size * pic_size_profile_factor) >> 3;
239   return uncompressed_frame_size / (double)encoded_frame_size;
240 }
241 
set_tile_info(AV1_COMMON * const cm,const TileConfig * const tile_cfg)242 static void set_tile_info(AV1_COMMON *const cm,
243                           const TileConfig *const tile_cfg) {
244   const CommonModeInfoParams *const mi_params = &cm->mi_params;
245   const SequenceHeader *const seq_params = &cm->seq_params;
246   CommonTileParams *const tiles = &cm->tiles;
247   int i, start_sb;
248 
249   av1_get_tile_limits(cm);
250 
251   // configure tile columns
252   if (tile_cfg->tile_width_count == 0 || tile_cfg->tile_height_count == 0) {
253     tiles->uniform_spacing = 1;
254     tiles->log2_cols = AOMMAX(tile_cfg->tile_columns, tiles->min_log2_cols);
255     tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
256   } else {
257     int mi_cols =
258         ALIGN_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
259     int sb_cols = mi_cols >> seq_params->mib_size_log2;
260     int size_sb, j = 0;
261     tiles->uniform_spacing = 0;
262     for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
263       tiles->col_start_sb[i] = start_sb;
264       size_sb = tile_cfg->tile_widths[j++];
265       if (j >= tile_cfg->tile_width_count) j = 0;
266       start_sb += AOMMIN(size_sb, tiles->max_width_sb);
267     }
268     tiles->cols = i;
269     tiles->col_start_sb[i] = sb_cols;
270   }
271   av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols,
272                           tiles);
273 
274   // configure tile rows
275   if (tiles->uniform_spacing) {
276     tiles->log2_rows = AOMMAX(tile_cfg->tile_rows, tiles->min_log2_rows);
277     tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
278   } else {
279     int mi_rows =
280         ALIGN_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
281     int sb_rows = mi_rows >> seq_params->mib_size_log2;
282     int size_sb, j = 0;
283     for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
284       tiles->row_start_sb[i] = start_sb;
285       size_sb = tile_cfg->tile_heights[j++];
286       if (j >= tile_cfg->tile_height_count) j = 0;
287       start_sb += AOMMIN(size_sb, tiles->max_height_sb);
288     }
289     tiles->rows = i;
290     tiles->row_start_sb[i] = sb_rows;
291   }
292   av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles);
293 }
294 
av1_update_frame_size(AV1_COMP * cpi)295 void av1_update_frame_size(AV1_COMP *cpi) {
296   AV1_COMMON *const cm = &cpi->common;
297   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
298 
299   // We need to reallocate the context buffers here in case we need more mis.
300   if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
301     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
302                        "Failed to allocate context buffers");
303   }
304   av1_init_mi_buffers(&cm->mi_params);
305 
306   av1_init_macroblockd(cm, xd);
307 
308   if (!is_stat_generation_stage(cpi))
309     alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
310 
311   if (!cpi->seq_params_locked)
312     set_sb_size(&cm->seq_params, av1_select_sb_size(cpi));
313 
314   set_tile_info(cm, &cpi->oxcf.tile_cfg);
315 }
316 
does_level_match(int width,int height,double fps,int lvl_width,int lvl_height,double lvl_fps,int lvl_dim_mult)317 static INLINE int does_level_match(int width, int height, double fps,
318                                    int lvl_width, int lvl_height,
319                                    double lvl_fps, int lvl_dim_mult) {
320   const int64_t lvl_luma_pels = lvl_width * lvl_height;
321   const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
322   const int64_t luma_pels = width * height;
323   const double display_sample_rate = luma_pels * fps;
324   return luma_pels <= lvl_luma_pels &&
325          display_sample_rate <= lvl_display_sample_rate &&
326          width <= lvl_width * lvl_dim_mult &&
327          height <= lvl_height * lvl_dim_mult;
328 }
329 
set_bitstream_level_tier(SequenceHeader * seq,AV1_COMMON * cm,int width,int height,double init_framerate)330 static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
331                                      int width, int height,
332                                      double init_framerate) {
333   // TODO(any): This is a placeholder function that only addresses dimensions
334   // and max display sample rates.
335   // Need to add checks for max bit rate, max decoded luma sample rate, header
336   // rate, etc. that are not covered by this function.
337   AV1_LEVEL level = SEQ_LEVEL_MAX;
338   if (does_level_match(width, height, init_framerate, 512, 288, 30.0, 4)) {
339     level = SEQ_LEVEL_2_0;
340   } else if (does_level_match(width, height, init_framerate, 704, 396, 30.0,
341                               4)) {
342     level = SEQ_LEVEL_2_1;
343   } else if (does_level_match(width, height, init_framerate, 1088, 612, 30.0,
344                               4)) {
345     level = SEQ_LEVEL_3_0;
346   } else if (does_level_match(width, height, init_framerate, 1376, 774, 30.0,
347                               4)) {
348     level = SEQ_LEVEL_3_1;
349   } else if (does_level_match(width, height, init_framerate, 2048, 1152, 30.0,
350                               3)) {
351     level = SEQ_LEVEL_4_0;
352   } else if (does_level_match(width, height, init_framerate, 2048, 1152, 60.0,
353                               3)) {
354     level = SEQ_LEVEL_4_1;
355   } else if (does_level_match(width, height, init_framerate, 4096, 2176, 30.0,
356                               2)) {
357     level = SEQ_LEVEL_5_0;
358   } else if (does_level_match(width, height, init_framerate, 4096, 2176, 60.0,
359                               2)) {
360     level = SEQ_LEVEL_5_1;
361   } else if (does_level_match(width, height, init_framerate, 4096, 2176, 120.0,
362                               2)) {
363     level = SEQ_LEVEL_5_2;
364   } else if (does_level_match(width, height, init_framerate, 8192, 4352, 30.0,
365                               2)) {
366     level = SEQ_LEVEL_6_0;
367   } else if (does_level_match(width, height, init_framerate, 8192, 4352, 60.0,
368                               2)) {
369     level = SEQ_LEVEL_6_1;
370   } else if (does_level_match(width, height, init_framerate, 8192, 4352, 120.0,
371                               2)) {
372     level = SEQ_LEVEL_6_2;
373   }
374 
375   SequenceHeader *const seq_params = &cm->seq_params;
376   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
377     seq->seq_level_idx[i] = level;
378     // Set the maximum parameters for bitrate and buffer size for this profile,
379     // level, and tier
380     seq_params->op_params[i].bitrate = av1_max_level_bitrate(
381         cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
382     // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
383     // check
384     if (seq_params->op_params[i].bitrate == 0)
385       aom_internal_error(
386           &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
387           "AV1 does not support this combination of profile, level, and tier.");
388     // Buffer size in bits/s is bitrate in bits/s * 1 s
389     seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
390   }
391 }
392 
av1_init_seq_coding_tools(SequenceHeader * seq,AV1_COMMON * cm,const AV1EncoderConfig * oxcf,int use_svc)393 void av1_init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
394                                const AV1EncoderConfig *oxcf, int use_svc) {
395   const FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg;
396   const ToolCfg *const tool_cfg = &oxcf->tool_cfg;
397 
398   seq->still_picture =
399       !tool_cfg->force_video_mode && (oxcf->input_cfg.limit == 1);
400   seq->reduced_still_picture_hdr =
401       seq->still_picture && !tool_cfg->full_still_picture_hdr;
402   seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
403   seq->force_integer_mv = 2;
404   seq->order_hint_info.enable_order_hint = tool_cfg->enable_order_hint;
405   seq->frame_id_numbers_present_flag =
406       !seq->reduced_still_picture_hdr &&
407       !oxcf->tile_cfg.enable_large_scale_tile &&
408       tool_cfg->error_resilient_mode && !use_svc;
409   if (seq->reduced_still_picture_hdr) {
410     seq->order_hint_info.enable_order_hint = 0;
411     seq->force_screen_content_tools = 2;
412     seq->force_integer_mv = 2;
413   }
414   seq->order_hint_info.order_hint_bits_minus_1 =
415       seq->order_hint_info.enable_order_hint
416           ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
417           : -1;
418 
419   seq->max_frame_width = frm_dim_cfg->forced_max_frame_width
420                              ? frm_dim_cfg->forced_max_frame_width
421                              : frm_dim_cfg->width;
422   seq->max_frame_height = frm_dim_cfg->forced_max_frame_height
423                               ? frm_dim_cfg->forced_max_frame_height
424                               : frm_dim_cfg->height;
425   seq->num_bits_width =
426       (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
427   seq->num_bits_height =
428       (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
429   assert(seq->num_bits_width <= 16);
430   assert(seq->num_bits_height <= 16);
431 
432   seq->frame_id_length = FRAME_ID_LENGTH;
433   seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
434 
435   seq->enable_dual_filter = tool_cfg->enable_dual_filter;
436   seq->order_hint_info.enable_dist_wtd_comp =
437       oxcf->comp_type_cfg.enable_dist_wtd_comp;
438   seq->order_hint_info.enable_dist_wtd_comp &=
439       seq->order_hint_info.enable_order_hint;
440   seq->order_hint_info.enable_ref_frame_mvs = tool_cfg->ref_frame_mvs_present;
441   seq->order_hint_info.enable_ref_frame_mvs &=
442       seq->order_hint_info.enable_order_hint;
443   seq->enable_superres = oxcf->superres_cfg.enable_superres;
444   seq->enable_cdef = tool_cfg->enable_cdef;
445   seq->enable_restoration = tool_cfg->enable_restoration;
446   seq->enable_warped_motion = oxcf->motion_mode_cfg.enable_warped_motion;
447   seq->enable_interintra_compound = tool_cfg->enable_interintra_comp;
448   seq->enable_masked_compound = oxcf->comp_type_cfg.enable_masked_comp;
449   seq->enable_intra_edge_filter = oxcf->intra_mode_cfg.enable_intra_edge_filter;
450   seq->enable_filter_intra = oxcf->intra_mode_cfg.enable_filter_intra;
451 
452   set_bitstream_level_tier(seq, cm, frm_dim_cfg->width, frm_dim_cfg->height,
453                            oxcf->input_cfg.init_framerate);
454 
455   if (seq->operating_points_cnt_minus_1 == 0) {
456     seq->operating_point_idc[0] = 0;
457   } else {
458     // Set operating_point_idc[] such that the i=0 point corresponds to the
459     // highest quality operating point (all layers), and subsequent
460     // operarting points (i > 0) are lower quality corresponding to
461     // skip decoding enhancement  layers (temporal first).
462     int i = 0;
463     assert(seq->operating_points_cnt_minus_1 ==
464            (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
465     for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
466       for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
467         seq->operating_point_idc[i] =
468             (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
469             ~(~0u << (cm->number_temporal_layers - tl));
470         i++;
471       }
472     }
473   }
474 }
475 
init_config(struct AV1_COMP * cpi,AV1EncoderConfig * oxcf)476 static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
477   AV1_COMMON *const cm = &cpi->common;
478   SequenceHeader *const seq_params = &cm->seq_params;
479   ResizePendingParams *resize_pending_params = &cpi->resize_pending_params;
480   const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
481   const ColorCfg *const color_cfg = &oxcf->color_cfg;
482   cpi->oxcf = *oxcf;
483   cpi->framerate = oxcf->input_cfg.init_framerate;
484 
485   seq_params->profile = oxcf->profile;
486   seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
487   seq_params->use_highbitdepth = oxcf->use_highbitdepth;
488   seq_params->color_primaries = color_cfg->color_primaries;
489   seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
490   seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
491   seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
492   seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
493   seq_params->color_range = color_cfg->color_range;
494   seq_params->timing_info_present = dec_model_cfg->timing_info_present;
495   seq_params->timing_info.num_units_in_display_tick =
496       dec_model_cfg->timing_info.num_units_in_display_tick;
497   seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
498   seq_params->timing_info.equal_picture_interval =
499       dec_model_cfg->timing_info.equal_picture_interval;
500   seq_params->timing_info.num_ticks_per_picture =
501       dec_model_cfg->timing_info.num_ticks_per_picture;
502 
503   seq_params->display_model_info_present_flag =
504       dec_model_cfg->display_model_info_present_flag;
505   seq_params->decoder_model_info_present_flag =
506       dec_model_cfg->decoder_model_info_present_flag;
507   if (dec_model_cfg->decoder_model_info_present_flag) {
508     // set the decoder model parameters in schedule mode
509     seq_params->decoder_model_info.num_units_in_decoding_tick =
510         dec_model_cfg->num_units_in_decoding_tick;
511     cm->buffer_removal_time_present = 1;
512     av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
513     av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
514   } else if (seq_params->timing_info_present &&
515              seq_params->timing_info.equal_picture_interval &&
516              !seq_params->decoder_model_info_present_flag) {
517     // set the decoder model parameters in resource availability mode
518     av1_set_resource_availability_parameters(&seq_params->op_params[0]);
519   } else {
520     seq_params->op_params[0].initial_display_delay =
521         10;  // Default value (not signaled)
522   }
523 
524   if (seq_params->monochrome) {
525     seq_params->subsampling_x = 1;
526     seq_params->subsampling_y = 1;
527   } else if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
528              seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
529              seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
530     seq_params->subsampling_x = 0;
531     seq_params->subsampling_y = 0;
532   } else {
533     if (seq_params->profile == 0) {
534       seq_params->subsampling_x = 1;
535       seq_params->subsampling_y = 1;
536     } else if (seq_params->profile == 1) {
537       seq_params->subsampling_x = 0;
538       seq_params->subsampling_y = 0;
539     } else {
540       if (seq_params->bit_depth == AOM_BITS_12) {
541         seq_params->subsampling_x = oxcf->input_cfg.chroma_subsampling_x;
542         seq_params->subsampling_y = oxcf->input_cfg.chroma_subsampling_y;
543       } else {
544         seq_params->subsampling_x = 1;
545         seq_params->subsampling_y = 0;
546       }
547     }
548   }
549 
550   cm->width = oxcf->frm_dim_cfg.width;
551   cm->height = oxcf->frm_dim_cfg.height;
552   set_sb_size(seq_params,
553               av1_select_sb_size(cpi));  // set sb size before allocations
554   alloc_compressor_data(cpi);
555 
556   av1_update_film_grain_parameters(cpi, oxcf);
557 
558   // Single thread case: use counts in common.
559   cpi->td.counts = &cpi->counts;
560 
561   // Set init SVC parameters.
562   cpi->use_svc = 0;
563   cpi->svc.external_ref_frame_config = 0;
564   cpi->svc.non_reference_frame = 0;
565   cpi->svc.number_spatial_layers = 1;
566   cpi->svc.number_temporal_layers = 1;
567   cm->number_spatial_layers = 1;
568   cm->number_temporal_layers = 1;
569   cm->spatial_layer_id = 0;
570   cm->temporal_layer_id = 0;
571 
572   // change includes all joint functionality
573   av1_change_config(cpi, oxcf);
574 
575   cpi->ref_frame_flags = 0;
576 
577   // Reset resize pending flags
578   resize_pending_params->width = 0;
579   resize_pending_params->height = 0;
580 
581   init_buffer_indices(&cpi->force_intpel_info, cm->remapped_ref_idx);
582 
583   av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
584 }
585 
av1_change_config(struct AV1_COMP * cpi,const AV1EncoderConfig * oxcf)586 void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
587   AV1_COMMON *const cm = &cpi->common;
588   SequenceHeader *const seq_params = &cm->seq_params;
589   RATE_CONTROL *const rc = &cpi->rc;
590   MACROBLOCK *const x = &cpi->td.mb;
591   AV1LevelParams *const level_params = &cpi->level_params;
592   InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
593   RefreshFrameFlagsInfo *const refresh_frame_flags = &cpi->refresh_frame;
594   const FrameDimensionCfg *const frm_dim_cfg = &cpi->oxcf.frm_dim_cfg;
595   const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
596   const ColorCfg *const color_cfg = &oxcf->color_cfg;
597   const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
598   // in case of LAP, lag in frames is set according to number of lap buffers
599   // calculated at init time. This stores and restores LAP's lag in frames to
600   // prevent override by new cfg.
601   int lap_lag_in_frames = -1;
602   if (cpi->lap_enabled && cpi->compressor_stage == LAP_STAGE) {
603     lap_lag_in_frames = cpi->oxcf.gf_cfg.lag_in_frames;
604   }
605 
606   if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
607   seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
608   seq_params->color_primaries = color_cfg->color_primaries;
609   seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
610   seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
611   seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
612   seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
613   seq_params->color_range = color_cfg->color_range;
614 
615   assert(IMPLIES(seq_params->profile <= PROFILE_1,
616                  seq_params->bit_depth <= AOM_BITS_10));
617 
618   seq_params->timing_info_present = dec_model_cfg->timing_info_present;
619   seq_params->timing_info.num_units_in_display_tick =
620       dec_model_cfg->timing_info.num_units_in_display_tick;
621   seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
622   seq_params->timing_info.equal_picture_interval =
623       dec_model_cfg->timing_info.equal_picture_interval;
624   seq_params->timing_info.num_ticks_per_picture =
625       dec_model_cfg->timing_info.num_ticks_per_picture;
626 
627   seq_params->display_model_info_present_flag =
628       dec_model_cfg->display_model_info_present_flag;
629   seq_params->decoder_model_info_present_flag =
630       dec_model_cfg->decoder_model_info_present_flag;
631   if (dec_model_cfg->decoder_model_info_present_flag) {
632     // set the decoder model parameters in schedule mode
633     seq_params->decoder_model_info.num_units_in_decoding_tick =
634         dec_model_cfg->num_units_in_decoding_tick;
635     cm->buffer_removal_time_present = 1;
636     av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
637     av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
638   } else if (seq_params->timing_info_present &&
639              seq_params->timing_info.equal_picture_interval &&
640              !seq_params->decoder_model_info_present_flag) {
641     // set the decoder model parameters in resource availability mode
642     av1_set_resource_availability_parameters(&seq_params->op_params[0]);
643   } else {
644     seq_params->op_params[0].initial_display_delay =
645         10;  // Default value (not signaled)
646   }
647 
648   av1_update_film_grain_parameters(cpi, oxcf);
649 
650   cpi->oxcf = *oxcf;
651   // When user provides superres_mode = AOM_SUPERRES_AUTO, we still initialize
652   // superres mode for current encoding = AOM_SUPERRES_NONE. This is to ensure
653   // that any analysis (e.g. TPL) happening outside the main encoding loop still
654   // happens at full resolution.
655   // This value will later be set appropriately just before main encoding loop.
656   cpi->superres_mode = oxcf->superres_cfg.superres_mode == AOM_SUPERRES_AUTO
657                            ? AOM_SUPERRES_NONE
658                            : oxcf->superres_cfg.superres_mode;  // default
659   x->e_mbd.bd = (int)seq_params->bit_depth;
660   x->e_mbd.global_motion = cm->global_motion;
661 
662   memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
663          sizeof(level_params->target_seq_level_idx));
664   level_params->keep_level_stats = 0;
665   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
666     if (level_params->target_seq_level_idx[i] <= SEQ_LEVELS) {
667       level_params->keep_level_stats |= 1u << i;
668       if (!level_params->level_info[i]) {
669         CHECK_MEM_ERROR(cm, level_params->level_info[i],
670                         aom_calloc(1, sizeof(*level_params->level_info[i])));
671       }
672     }
673   }
674 
675   // TODO(huisu@): level targeting currently only works for the 0th operating
676   // point, so scalable coding is not supported yet.
677   if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) {
678     // Adjust encoder config in order to meet target level.
679     config_target_level(cpi, level_params->target_seq_level_idx[0],
680                         seq_params->tier[0]);
681   }
682 
683   if ((has_no_stats_stage(cpi)) && (rc_cfg->mode == AOM_Q)) {
684     rc->baseline_gf_interval = FIXED_GF_INTERVAL;
685   } else {
686     rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
687   }
688 
689   refresh_frame_flags->golden_frame = false;
690   refresh_frame_flags->bwd_ref_frame = false;
691 
692   cm->features.refresh_frame_context =
693       (oxcf->tool_cfg.frame_parallel_decoding_mode)
694           ? REFRESH_FRAME_CONTEXT_DISABLED
695           : REFRESH_FRAME_CONTEXT_BACKWARD;
696   if (oxcf->tile_cfg.enable_large_scale_tile)
697     cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
698 
699   if (x->palette_buffer == NULL) {
700     CHECK_MEM_ERROR(cm, x->palette_buffer,
701                     aom_memalign(16, sizeof(*x->palette_buffer)));
702   }
703 
704   if (x->comp_rd_buffer.pred0 == NULL) {
705     alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
706   }
707 
708   if (x->tmp_conv_dst == NULL) {
709     CHECK_MEM_ERROR(
710         cm, x->tmp_conv_dst,
711         aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
712     x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
713   }
714   for (int i = 0; i < 2; ++i) {
715     if (x->tmp_pred_bufs[i] == NULL) {
716       CHECK_MEM_ERROR(cm, x->tmp_pred_bufs[i],
717                       aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
718                                            sizeof(*x->tmp_pred_bufs[i])));
719       x->e_mbd.tmp_obmc_bufs[i] = x->tmp_pred_bufs[i];
720     }
721   }
722 
723   av1_reset_segment_features(cm);
724 
725   av1_set_high_precision_mv(cpi, 1, 0);
726 
727   set_rc_buffer_sizes(rc, rc_cfg);
728 
729   // Under a configuration change, where maximum_buffer_size may change,
730   // keep buffer level clipped to the maximum allowed buffer size.
731   rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
732   rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
733 
734   // Set up frame rate and related parameters rate control values.
735   av1_new_framerate(cpi, cpi->framerate);
736 
737   // Set absolute upper and lower quality limits
738   rc->worst_quality = rc_cfg->worst_allowed_q;
739   rc->best_quality = rc_cfg->best_allowed_q;
740 
741   cm->features.interp_filter =
742       oxcf->tile_cfg.enable_large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
743   cm->features.switchable_motion_mode = 1;
744 
745   if (frm_dim_cfg->render_width > 0 && frm_dim_cfg->render_height > 0) {
746     cm->render_width = frm_dim_cfg->render_width;
747     cm->render_height = frm_dim_cfg->render_height;
748   } else {
749     cm->render_width = frm_dim_cfg->width;
750     cm->render_height = frm_dim_cfg->height;
751   }
752   cm->width = frm_dim_cfg->width;
753   cm->height = frm_dim_cfg->height;
754 
755   int sb_size = seq_params->sb_size;
756   // Superblock size should not be updated after the first key frame.
757   if (!cpi->seq_params_locked) {
758     set_sb_size(&cm->seq_params, av1_select_sb_size(cpi));
759     for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
760       seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
761   }
762 
763   if (initial_dimensions->width || sb_size != seq_params->sb_size) {
764     if (cm->width > initial_dimensions->width ||
765         cm->height > initial_dimensions->height ||
766         seq_params->sb_size != sb_size) {
767       av1_free_context_buffers(cm);
768       av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
769       av1_free_sms_tree(&cpi->td);
770       av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
771       cpi->td.firstpass_ctx = NULL;
772       alloc_compressor_data(cpi);
773       realloc_segmentation_maps(cpi);
774       initial_dimensions->width = initial_dimensions->height = 0;
775     }
776   }
777   av1_update_frame_size(cpi);
778 
779   rc->is_src_frame_alt_ref = 0;
780 
781   set_tile_info(cm, &cpi->oxcf.tile_cfg);
782 
783   if (!cpi->svc.external_ref_frame_config)
784     cpi->ext_flags.refresh_frame.update_pending = 0;
785   cpi->ext_flags.refresh_frame_context_pending = 0;
786 
787 #if CONFIG_AV1_HIGHBITDEPTH
788   highbd_set_var_fns(cpi);
789 #endif
790 
791   // Init sequence level coding tools
792   // This should not be called after the first key frame.
793   if (!cpi->seq_params_locked) {
794     seq_params->operating_points_cnt_minus_1 =
795         (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
796             ? cm->number_spatial_layers * cm->number_temporal_layers - 1
797             : 0;
798     av1_init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
799   }
800 
801   if (cpi->use_svc)
802     av1_update_layer_context_change_config(cpi, rc_cfg->target_bandwidth);
803 
804   // restore the value of lag_in_frame for LAP stage.
805   if (lap_lag_in_frames != -1) {
806     cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
807   }
808 }
809 
init_frame_info(FRAME_INFO * frame_info,const AV1_COMMON * const cm)810 static INLINE void init_frame_info(FRAME_INFO *frame_info,
811                                    const AV1_COMMON *const cm) {
812   const CommonModeInfoParams *const mi_params = &cm->mi_params;
813   const SequenceHeader *const seq_params = &cm->seq_params;
814   frame_info->frame_width = cm->width;
815   frame_info->frame_height = cm->height;
816   frame_info->mi_cols = mi_params->mi_cols;
817   frame_info->mi_rows = mi_params->mi_rows;
818   frame_info->mb_cols = mi_params->mb_cols;
819   frame_info->mb_rows = mi_params->mb_rows;
820   frame_info->num_mbs = mi_params->MBs;
821   frame_info->bit_depth = seq_params->bit_depth;
822   frame_info->subsampling_x = seq_params->subsampling_x;
823   frame_info->subsampling_y = seq_params->subsampling_y;
824 }
825 
init_frame_index_set(FRAME_INDEX_SET * frame_index_set)826 static INLINE void init_frame_index_set(FRAME_INDEX_SET *frame_index_set) {
827   frame_index_set->show_frame_count = 0;
828 }
829 
update_frame_index_set(FRAME_INDEX_SET * frame_index_set,int is_show_frame)830 static INLINE void update_frame_index_set(FRAME_INDEX_SET *frame_index_set,
831                                           int is_show_frame) {
832   if (is_show_frame) {
833     frame_index_set->show_frame_count++;
834   }
835 }
836 
av1_create_compressor(AV1EncoderConfig * oxcf,BufferPool * const pool,FIRSTPASS_STATS * frame_stats_buf,COMPRESSOR_STAGE stage,int num_lap_buffers,int lap_lag_in_frames,STATS_BUFFER_CTX * stats_buf_context)837 AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
838                                 FIRSTPASS_STATS *frame_stats_buf,
839                                 COMPRESSOR_STAGE stage, int num_lap_buffers,
840                                 int lap_lag_in_frames,
841                                 STATS_BUFFER_CTX *stats_buf_context) {
842   AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
843   AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
844 
845   if (!cm) return NULL;
846 
847   av1_zero(*cpi);
848 
849   // The jmp_buf is valid only for the duration of the function that calls
850   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
851   // before it returns.
852   if (setjmp(cm->error.jmp)) {
853     cm->error.setjmp = 0;
854     av1_remove_compressor(cpi);
855     return 0;
856   }
857 
858   cm->error.setjmp = 1;
859   cpi->lap_enabled = num_lap_buffers > 0;
860   cpi->compressor_stage = stage;
861 
862   CommonModeInfoParams *const mi_params = &cm->mi_params;
863   mi_params->free_mi = enc_free_mi;
864   mi_params->setup_mi = enc_setup_mi;
865   mi_params->set_mb_mi = (oxcf->pass == 1 || cpi->compressor_stage == LAP_STAGE)
866                              ? stat_stage_set_mb_mi
867                              : enc_set_mb_mi;
868 
869   mi_params->mi_alloc_bsize = BLOCK_4X4;
870 
871   CHECK_MEM_ERROR(cm, cm->fc,
872                   (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
873   CHECK_MEM_ERROR(
874       cm, cm->default_frame_context,
875       (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
876   memset(cm->fc, 0, sizeof(*cm->fc));
877   memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
878 
879   cpi->common.buffer_pool = pool;
880 
881   init_config(cpi, oxcf);
882   if (cpi->compressor_stage == LAP_STAGE) {
883     cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
884   }
885 
886   cpi->frames_left = cpi->oxcf.input_cfg.limit;
887 
888   av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
889 
890   // For two pass and lag_in_frames > 33 in LAP.
891   cpi->rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_2;
892   if (cpi->lap_enabled) {
893     if ((num_lap_buffers <
894          (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)) &&
895         num_lap_buffers >= (MAX_GF_LENGTH_LAP + 3)) {
896       /*
897        * For lag in frames >= 19 and <33, enable scenecut
898        * with limited future frame prediction.
899        */
900       cpi->rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_1;
901     } else if (num_lap_buffers < (MAX_GF_LENGTH_LAP + 3)) {
902       // Disable scenecut when lag_in_frames < 19.
903       cpi->rc.enable_scenecut_detection = DISABLE_SCENECUT;
904     }
905   }
906   init_frame_info(&cpi->frame_info, cm);
907   init_frame_index_set(&cpi->frame_index_set);
908 
909   cm->current_frame.frame_number = 0;
910   cm->current_frame_id = -1;
911   cpi->seq_params_locked = 0;
912   cpi->partition_search_skippable_frame = 0;
913   cpi->tile_data = NULL;
914   cpi->last_show_frame_buf = NULL;
915   realloc_segmentation_maps(cpi);
916 
917   cpi->refresh_frame.alt_ref_frame = false;
918 
919   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
920 #if CONFIG_INTERNAL_STATS
921   cpi->b_calculate_blockiness = 1;
922   cpi->b_calculate_consistency = 1;
923   cpi->total_inconsistency = 0;
924   cpi->psnr[0].worst = 100.0;
925   cpi->psnr[1].worst = 100.0;
926   cpi->worst_ssim = 100.0;
927   cpi->worst_ssim_hbd = 100.0;
928 
929   cpi->count[0] = 0;
930   cpi->count[1] = 0;
931   cpi->bytes = 0;
932 #if CONFIG_SPEED_STATS
933   cpi->tx_search_count = 0;
934 #endif  // CONFIG_SPEED_STATS
935 
936   if (cpi->b_calculate_psnr) {
937     cpi->total_sq_error[0] = 0;
938     cpi->total_samples[0] = 0;
939     cpi->total_sq_error[1] = 0;
940     cpi->total_samples[1] = 0;
941     cpi->tot_recode_hits = 0;
942     cpi->summed_quality = 0;
943     cpi->summed_weights = 0;
944     cpi->summed_quality_hbd = 0;
945     cpi->summed_weights_hbd = 0;
946   }
947 
948   cpi->fastssim.worst = 100.0;
949   cpi->psnrhvs.worst = 100.0;
950 
951   if (cpi->b_calculate_blockiness) {
952     cpi->total_blockiness = 0;
953     cpi->worst_blockiness = 0.0;
954   }
955 
956   if (cpi->b_calculate_consistency) {
957     CHECK_MEM_ERROR(
958         cm, cpi->ssim_vars,
959         aom_malloc(sizeof(*cpi->ssim_vars) * 4 * cpi->common.mi_params.mi_rows *
960                    cpi->common.mi_params.mi_cols));
961     cpi->worst_consistency = 100.0;
962   }
963 #endif
964 #if CONFIG_ENTROPY_STATS
965   av1_zero(aggregate_fc);
966 #endif  // CONFIG_ENTROPY_STATS
967 
968   cpi->time_stamps.first_ts_start = INT64_MAX;
969 
970 #ifdef OUTPUT_YUV_REC
971   yuv_rec_file = fopen("rec.yuv", "wb");
972 #endif
973 #ifdef OUTPUT_YUV_DENOISED
974   yuv_denoised_file = fopen("denoised.yuv", "wb");
975 #endif
976 
977   assert(MAX_LAP_BUFFERS >= MAX_LAG_BUFFERS);
978   int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
979   for (int i = 0; i < size; i++)
980     cpi->twopass.frame_stats_arr[i] = &frame_stats_buf[i];
981 
982   cpi->twopass.stats_buf_ctx = stats_buf_context;
983   cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
984 
985 #if !CONFIG_REALTIME_ONLY
986   if (is_stat_consumption_stage(cpi)) {
987     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
988     const int packets = (int)(oxcf->twopass_stats_in.sz / packet_sz);
989 
990     if (!cpi->lap_enabled) {
991       /*Re-initialize to stats buffer, populated by application in the case of
992        * two pass*/
993       cpi->twopass.stats_buf_ctx->stats_in_start = oxcf->twopass_stats_in.buf;
994       cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
995       cpi->twopass.stats_buf_ctx->stats_in_end =
996           &cpi->twopass.stats_buf_ctx->stats_in_start[packets - 1];
997 
998       av1_init_second_pass(cpi);
999     } else {
1000       av1_init_single_pass_lap(cpi);
1001     }
1002   }
1003 #endif
1004 
1005   alloc_obmc_buffers(&cpi->td.mb.obmc_buffer, cm);
1006 
1007   CHECK_MEM_ERROR(
1008       cm, cpi->td.mb.inter_modes_info,
1009       (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
1010 
1011   for (int x = 0; x < 2; x++)
1012     for (int y = 0; y < 2; y++)
1013       CHECK_MEM_ERROR(
1014           cm, cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
1015           (uint32_t *)aom_malloc(
1016               AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
1017               sizeof(*cpi->td.mb.intrabc_hash_info.hash_value_buffer[0][0])));
1018 
1019   cpi->td.mb.intrabc_hash_info.g_crc_initialized = 0;
1020 
1021   av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
1022   av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
1023 
1024   CHECK_MEM_ERROR(cm, cpi->consec_zero_mv,
1025                   aom_calloc((mi_params->mi_rows * mi_params->mi_cols) >> 2,
1026                              sizeof(*cpi->consec_zero_mv)));
1027 
1028   {
1029     const int bsize = BLOCK_16X16;
1030     const int w = mi_size_wide[bsize];
1031     const int h = mi_size_high[bsize];
1032     const int num_cols = (mi_params->mi_cols + w - 1) / w;
1033     const int num_rows = (mi_params->mi_rows + h - 1) / h;
1034     CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
1035                     aom_calloc(num_rows * num_cols,
1036                                sizeof(*cpi->tpl_rdmult_scaling_factors)));
1037     CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
1038                     aom_calloc(num_rows * num_cols,
1039                                sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
1040   }
1041 
1042   {
1043     const int bsize = BLOCK_16X16;
1044     const int w = mi_size_wide[bsize];
1045     const int h = mi_size_high[bsize];
1046     const int num_cols = (mi_params->mi_cols + w - 1) / w;
1047     const int num_rows = (mi_params->mi_rows + h - 1) / h;
1048     CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
1049                     aom_calloc(num_rows * num_cols,
1050                                sizeof(*cpi->ssim_rdmult_scaling_factors)));
1051   }
1052 
1053 #if CONFIG_TUNE_VMAF
1054   {
1055     const int bsize = BLOCK_64X64;
1056     const int w = mi_size_wide[bsize];
1057     const int h = mi_size_high[bsize];
1058     const int num_cols = (mi_params->mi_cols + w - 1) / w;
1059     const int num_rows = (mi_params->mi_rows + h - 1) / h;
1060     CHECK_MEM_ERROR(cm, cpi->vmaf_info.rdmult_scaling_factors,
1061                     aom_calloc(num_rows * num_cols,
1062                                sizeof(*cpi->vmaf_info.rdmult_scaling_factors)));
1063     for (int i = 0; i < MAX_ARF_LAYERS; i++) {
1064       cpi->vmaf_info.last_frame_unsharp_amount[i] = -1.0;
1065       cpi->vmaf_info.last_frame_ysse[i] = -1.0;
1066       cpi->vmaf_info.last_frame_vmaf[i] = -1.0;
1067     }
1068     cpi->vmaf_info.original_qindex = -1;
1069 
1070 #if CONFIG_USE_VMAF_RC
1071     cpi->vmaf_info.vmaf_model = NULL;
1072 #endif
1073   }
1074 #endif
1075 
1076   if (!is_stat_generation_stage(cpi)) {
1077     setup_tpl_buffers(cm, &cpi->tpl_data, cpi->oxcf.gf_cfg.lag_in_frames);
1078   }
1079 
1080 #if CONFIG_COLLECT_PARTITION_STATS
1081   av1_zero(cpi->partition_stats);
1082 #endif  // CONFIG_COLLECT_PARTITION_STATS
1083 
1084 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1085   cpi->fn_ptr[BT].sdf = SDF;                                    \
1086   cpi->fn_ptr[BT].sdaf = SDAF;                                  \
1087   cpi->fn_ptr[BT].vf = VF;                                      \
1088   cpi->fn_ptr[BT].svf = SVF;                                    \
1089   cpi->fn_ptr[BT].svaf = SVAF;                                  \
1090   cpi->fn_ptr[BT].sdx4df = SDX4DF;                              \
1091   cpi->fn_ptr[BT].jsdaf = JSDAF;                                \
1092   cpi->fn_ptr[BT].jsvaf = JSVAF;
1093 
1094 // Realtime mode doesn't use 4x rectangular blocks.
1095 #if !CONFIG_REALTIME_ONLY
1096   BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
1097       aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
1098       aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
1099       aom_dist_wtd_sub_pixel_avg_variance4x16)
1100 
1101   BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
1102       aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
1103       aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
1104       aom_dist_wtd_sub_pixel_avg_variance16x4)
1105 
1106   BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
1107       aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
1108       aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
1109       aom_dist_wtd_sub_pixel_avg_variance8x32)
1110 
1111   BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
1112       aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
1113       aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
1114       aom_dist_wtd_sub_pixel_avg_variance32x8)
1115 
1116   BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
1117       aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
1118       aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
1119       aom_dist_wtd_sub_pixel_avg_variance16x64)
1120 
1121   BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
1122       aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
1123       aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
1124       aom_dist_wtd_sub_pixel_avg_variance64x16)
1125 #endif  // !CONFIG_REALTIME_ONLY
1126 
1127   BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
1128       aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
1129       aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
1130       aom_dist_wtd_sub_pixel_avg_variance128x128)
1131 
1132   BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
1133       aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
1134       aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
1135       aom_dist_wtd_sub_pixel_avg_variance128x64)
1136 
1137   BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
1138       aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
1139       aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
1140       aom_dist_wtd_sub_pixel_avg_variance64x128)
1141 
1142   BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
1143       aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
1144       aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
1145       aom_dist_wtd_sub_pixel_avg_variance32x16)
1146 
1147   BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
1148       aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
1149       aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
1150       aom_dist_wtd_sub_pixel_avg_variance16x32)
1151 
1152   BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
1153       aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
1154       aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
1155       aom_dist_wtd_sub_pixel_avg_variance64x32)
1156 
1157   BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
1158       aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
1159       aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
1160       aom_dist_wtd_sub_pixel_avg_variance32x64)
1161 
1162   BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
1163       aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
1164       aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
1165       aom_dist_wtd_sub_pixel_avg_variance32x32)
1166 
1167   BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
1168       aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
1169       aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
1170       aom_dist_wtd_sub_pixel_avg_variance64x64)
1171 
1172   BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
1173       aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
1174       aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
1175       aom_dist_wtd_sub_pixel_avg_variance16x16)
1176 
1177   BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
1178       aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
1179       aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
1180       aom_dist_wtd_sub_pixel_avg_variance16x8)
1181 
1182   BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
1183       aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
1184       aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
1185       aom_dist_wtd_sub_pixel_avg_variance8x16)
1186 
1187   BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
1188       aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
1189       aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
1190 
1191   BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
1192       aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
1193       aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
1194 
1195   BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
1196       aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
1197       aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
1198 
1199   BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
1200       aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
1201       aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
1202 
1203 #if !CONFIG_REALTIME_ONLY
1204 #define OBFP(BT, OSDF, OVF, OSVF) \
1205   cpi->fn_ptr[BT].osdf = OSDF;    \
1206   cpi->fn_ptr[BT].ovf = OVF;      \
1207   cpi->fn_ptr[BT].osvf = OSVF;
1208 
1209   OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
1210        aom_obmc_sub_pixel_variance128x128)
1211   OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
1212        aom_obmc_sub_pixel_variance128x64)
1213   OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
1214        aom_obmc_sub_pixel_variance64x128)
1215   OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
1216        aom_obmc_sub_pixel_variance64x64)
1217   OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
1218        aom_obmc_sub_pixel_variance64x32)
1219   OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
1220        aom_obmc_sub_pixel_variance32x64)
1221   OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
1222        aom_obmc_sub_pixel_variance32x32)
1223   OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
1224        aom_obmc_sub_pixel_variance32x16)
1225   OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
1226        aom_obmc_sub_pixel_variance16x32)
1227   OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
1228        aom_obmc_sub_pixel_variance16x16)
1229   OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
1230        aom_obmc_sub_pixel_variance16x8)
1231   OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
1232        aom_obmc_sub_pixel_variance8x16)
1233   OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
1234        aom_obmc_sub_pixel_variance8x8)
1235   OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
1236        aom_obmc_sub_pixel_variance4x8)
1237   OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
1238        aom_obmc_sub_pixel_variance8x4)
1239   OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
1240        aom_obmc_sub_pixel_variance4x4)
1241   OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
1242        aom_obmc_sub_pixel_variance4x16)
1243   OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
1244        aom_obmc_sub_pixel_variance16x4)
1245   OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
1246        aom_obmc_sub_pixel_variance8x32)
1247   OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
1248        aom_obmc_sub_pixel_variance32x8)
1249   OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
1250        aom_obmc_sub_pixel_variance16x64)
1251   OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
1252        aom_obmc_sub_pixel_variance64x16)
1253 #endif  // !CONFIG_REALTIME_ONLY
1254 
1255 #define MBFP(BT, MCSDF, MCSVF)  \
1256   cpi->fn_ptr[BT].msdf = MCSDF; \
1257   cpi->fn_ptr[BT].msvf = MCSVF;
1258 
1259   MBFP(BLOCK_128X128, aom_masked_sad128x128,
1260        aom_masked_sub_pixel_variance128x128)
1261   MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
1262   MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
1263   MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
1264   MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
1265   MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
1266   MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
1267   MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
1268   MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
1269   MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
1270   MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
1271   MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
1272   MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
1273   MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
1274   MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
1275   MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
1276 
1277 #if !CONFIG_REALTIME_ONLY
1278   MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
1279   MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
1280   MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
1281   MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
1282   MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
1283   MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
1284 #endif
1285 
1286 #define SDSFP(BT, SDSF, SDSX4DF) \
1287   cpi->fn_ptr[BT].sdsf = SDSF;   \
1288   cpi->fn_ptr[BT].sdsx4df = SDSX4DF;
1289 
1290   SDSFP(BLOCK_128X128, aom_sad_skip_128x128, aom_sad_skip_128x128x4d);
1291   SDSFP(BLOCK_128X64, aom_sad_skip_128x64, aom_sad_skip_128x64x4d);
1292   SDSFP(BLOCK_64X128, aom_sad_skip_64x128, aom_sad_skip_64x128x4d);
1293   SDSFP(BLOCK_64X64, aom_sad_skip_64x64, aom_sad_skip_64x64x4d);
1294   SDSFP(BLOCK_64X32, aom_sad_skip_64x32, aom_sad_skip_64x32x4d);
1295 
1296   SDSFP(BLOCK_32X64, aom_sad_skip_32x64, aom_sad_skip_32x64x4d);
1297   SDSFP(BLOCK_32X32, aom_sad_skip_32x32, aom_sad_skip_32x32x4d);
1298   SDSFP(BLOCK_32X16, aom_sad_skip_32x16, aom_sad_skip_32x16x4d);
1299 
1300   SDSFP(BLOCK_16X32, aom_sad_skip_16x32, aom_sad_skip_16x32x4d);
1301   SDSFP(BLOCK_16X16, aom_sad_skip_16x16, aom_sad_skip_16x16x4d);
1302   SDSFP(BLOCK_16X8, aom_sad_skip_16x8, aom_sad_skip_16x8x4d);
1303   SDSFP(BLOCK_8X16, aom_sad_skip_8x16, aom_sad_skip_8x16x4d);
1304   SDSFP(BLOCK_8X8, aom_sad_skip_8x8, aom_sad_skip_8x8x4d);
1305 
1306   SDSFP(BLOCK_4X8, aom_sad_skip_4x8, aom_sad_skip_4x8x4d);
1307 
1308 #if !CONFIG_REALTIME_ONLY
1309   SDSFP(BLOCK_64X16, aom_sad_skip_64x16, aom_sad_skip_64x16x4d);
1310   SDSFP(BLOCK_16X64, aom_sad_skip_16x64, aom_sad_skip_16x64x4d);
1311   SDSFP(BLOCK_32X8, aom_sad_skip_32x8, aom_sad_skip_32x8x4d);
1312   SDSFP(BLOCK_8X32, aom_sad_skip_8x32, aom_sad_skip_8x32x4d);
1313   SDSFP(BLOCK_4X16, aom_sad_skip_4x16, aom_sad_skip_4x16x4d);
1314 #endif
1315 #undef SDSFP
1316 
1317 #if CONFIG_AV1_HIGHBITDEPTH
1318   highbd_set_var_fns(cpi);
1319 #endif
1320 
1321   /* av1_init_quantizer() is first called here. Add check in
1322    * av1_frame_init_quantizer() so that av1_init_quantizer is only
1323    * called later when needed. This will avoid unnecessary calls of
1324    * av1_init_quantizer() for every frame.
1325    */
1326   av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
1327                      cm->seq_params.bit_depth);
1328   av1_qm_init(&cm->quant_params, av1_num_planes(cm));
1329 
1330   av1_loop_filter_init(cm);
1331   cm->superres_scale_denominator = SCALE_NUMERATOR;
1332   cm->superres_upscaled_width = oxcf->frm_dim_cfg.width;
1333   cm->superres_upscaled_height = oxcf->frm_dim_cfg.height;
1334 #if !CONFIG_REALTIME_ONLY
1335   av1_loop_restoration_precal();
1336 #endif
1337   cm->error.setjmp = 0;
1338 
1339   return cpi;
1340 }
1341 
1342 #if CONFIG_INTERNAL_STATS
1343 #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
1344 
1345 #define SNPRINT2(H, T, V) \
1346   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
1347 #endif  // CONFIG_INTERNAL_STATS
1348 
1349 // This function will change the state and free the mutex of corresponding
1350 // workers and terminate the object. The object can not be re-used unless a call
1351 // to reset() is made.
terminate_worker_data(AV1_COMP * cpi)1352 static AOM_INLINE void terminate_worker_data(AV1_COMP *cpi) {
1353   MultiThreadInfo *const mt_info = &cpi->mt_info;
1354   for (int t = mt_info->num_workers - 1; t >= 0; --t) {
1355     AVxWorker *const worker = &mt_info->workers[t];
1356     aom_get_worker_interface()->end(worker);
1357   }
1358 }
1359 
1360 // Deallocate allocated thread_data.
free_thread_data(AV1_COMP * cpi)1361 static AOM_INLINE void free_thread_data(AV1_COMP *cpi) {
1362   MultiThreadInfo *const mt_info = &cpi->mt_info;
1363   AV1_COMMON *cm = &cpi->common;
1364   for (int t = 0; t < mt_info->num_workers; ++t) {
1365     EncWorkerData *const thread_data = &mt_info->tile_thr_data[t];
1366     aom_free(thread_data->td->tctx);
1367     if (t == 0) continue;
1368     aom_free(thread_data->td->palette_buffer);
1369     aom_free(thread_data->td->tmp_conv_dst);
1370     release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
1371     for (int j = 0; j < 2; ++j) {
1372       aom_free(thread_data->td->tmp_pred_bufs[j]);
1373     }
1374     release_obmc_buffers(&thread_data->td->obmc_buffer);
1375     aom_free(thread_data->td->vt64x64);
1376 
1377     aom_free(thread_data->td->inter_modes_info);
1378     for (int x = 0; x < 2; x++) {
1379       for (int y = 0; y < 2; y++) {
1380         aom_free(thread_data->td->hash_value_buffer[x][y]);
1381         thread_data->td->hash_value_buffer[x][y] = NULL;
1382       }
1383     }
1384     aom_free(thread_data->td->counts);
1385     av1_free_pmc(thread_data->td->firstpass_ctx, av1_num_planes(cm));
1386     thread_data->td->firstpass_ctx = NULL;
1387     av1_free_shared_coeff_buffer(&thread_data->td->shared_coeff_buf);
1388     av1_free_sms_tree(thread_data->td);
1389     aom_free(thread_data->td);
1390   }
1391 }
1392 
av1_remove_compressor(AV1_COMP * cpi)1393 void av1_remove_compressor(AV1_COMP *cpi) {
1394   if (!cpi) return;
1395 
1396   AV1_COMMON *cm = &cpi->common;
1397   if (cm->current_frame.frame_number > 0) {
1398 #if CONFIG_ENTROPY_STATS
1399     if (!is_stat_generation_stage(cpi)) {
1400       fprintf(stderr, "Writing counts.stt\n");
1401       FILE *f = fopen("counts.stt", "wb");
1402       fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
1403       fclose(f);
1404     }
1405 #endif  // CONFIG_ENTROPY_STATS
1406 #if CONFIG_INTERNAL_STATS
1407     aom_clear_system_state();
1408 
1409     if (!is_stat_generation_stage(cpi)) {
1410       char headings[512] = { 0 };
1411       char results[512] = { 0 };
1412       FILE *f = fopen("opsnr.stt", "a");
1413       double time_encoded =
1414           (cpi->time_stamps.prev_ts_end - cpi->time_stamps.first_ts_start) /
1415           10000000.000;
1416       double total_encode_time =
1417           (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
1418       const double dr =
1419           (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
1420       const double peak =
1421           (double)((1 << cpi->oxcf.input_cfg.input_bit_depth) - 1);
1422       const double target_rate =
1423           (double)cpi->oxcf.rc_cfg.target_bandwidth / 1000;
1424       const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
1425 
1426       if (cpi->b_calculate_psnr) {
1427         const double total_psnr =
1428             aom_sse_to_psnr((double)cpi->total_samples[0], peak,
1429                             (double)cpi->total_sq_error[0]);
1430         const double total_ssim =
1431             100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
1432         snprintf(headings, sizeof(headings),
1433                  "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1434                  "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
1435                  "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
1436                  "AVPsrnY\tAPsnrCb\tAPsnrCr");
1437         snprintf(results, sizeof(results),
1438                  "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
1439                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
1440                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
1441                  "%7.3f\t%7.3f\t%7.3f",
1442                  dr, cpi->psnr[0].stat[STAT_ALL] / cpi->count[0], total_psnr,
1443                  cpi->psnr[0].stat[STAT_ALL] / cpi->count[0], total_psnr,
1444                  total_ssim, total_ssim,
1445                  cpi->fastssim.stat[STAT_ALL] / cpi->count[0],
1446                  cpi->psnrhvs.stat[STAT_ALL] / cpi->count[0],
1447                  cpi->psnr[0].worst, cpi->worst_ssim, cpi->fastssim.worst,
1448                  cpi->psnrhvs.worst, cpi->psnr[0].stat[STAT_Y] / cpi->count[0],
1449                  cpi->psnr[0].stat[STAT_U] / cpi->count[0],
1450                  cpi->psnr[0].stat[STAT_V] / cpi->count[0]);
1451 
1452         if (cpi->b_calculate_blockiness) {
1453           SNPRINT(headings, "\t  Block\tWstBlck");
1454           SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count[0]);
1455           SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
1456         }
1457 
1458         if (cpi->b_calculate_consistency) {
1459           double consistency =
1460               aom_sse_to_psnr((double)cpi->total_samples[0], peak,
1461                               (double)cpi->total_inconsistency);
1462 
1463           SNPRINT(headings, "\tConsist\tWstCons");
1464           SNPRINT2(results, "\t%7.3f", consistency);
1465           SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
1466         }
1467 
1468         SNPRINT(headings, "\t   Time\tRcErr\tAbsErr");
1469         SNPRINT2(results, "\t%8.0f", total_encode_time);
1470         SNPRINT2(results, " %7.2f", rate_err);
1471         SNPRINT2(results, " %7.2f", fabs(rate_err));
1472 
1473         SNPRINT(headings, "\tAPsnr611");
1474         SNPRINT2(results, " %7.3f",
1475                  (6 * cpi->psnr[0].stat[STAT_Y] + cpi->psnr[0].stat[STAT_U] +
1476                   cpi->psnr[0].stat[STAT_V]) /
1477                      (cpi->count[0] * 8));
1478 
1479 #if CONFIG_AV1_HIGHBITDEPTH
1480         const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
1481         const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
1482         if ((cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) &&
1483             (in_bit_depth < bit_depth)) {
1484           const double peak_hbd = (double)((1 << bit_depth) - 1);
1485           const double total_psnr_hbd =
1486               aom_sse_to_psnr((double)cpi->total_samples[1], peak_hbd,
1487                               (double)cpi->total_sq_error[1]);
1488           const double total_ssim_hbd =
1489               100 * pow(cpi->summed_quality_hbd / cpi->summed_weights_hbd, 8.0);
1490           SNPRINT(headings,
1491                   "\t AVGPsnrH GLBPsnrH AVPsnrPH GLPsnrPH"
1492                   " AVPsnrYH APsnrCbH APsnrCrH WstPsnrH"
1493                   " AOMSSIMH VPSSIMPH WstSsimH");
1494           SNPRINT2(results, "\t%7.3f",
1495                    cpi->psnr[1].stat[STAT_ALL] / cpi->count[1]);
1496           SNPRINT2(results, "  %7.3f", total_psnr_hbd);
1497           SNPRINT2(results, "  %7.3f",
1498                    cpi->psnr[1].stat[STAT_ALL] / cpi->count[1]);
1499           SNPRINT2(results, "  %7.3f", total_psnr_hbd);
1500           SNPRINT2(results, "  %7.3f",
1501                    cpi->psnr[1].stat[STAT_Y] / cpi->count[1]);
1502           SNPRINT2(results, "  %7.3f",
1503                    cpi->psnr[1].stat[STAT_U] / cpi->count[1]);
1504           SNPRINT2(results, "  %7.3f",
1505                    cpi->psnr[1].stat[STAT_V] / cpi->count[1]);
1506           SNPRINT2(results, "  %7.3f", cpi->psnr[1].worst);
1507           SNPRINT2(results, "  %7.3f", total_ssim_hbd);
1508           SNPRINT2(results, "  %7.3f", total_ssim_hbd);
1509           SNPRINT2(results, "  %7.3f", cpi->worst_ssim_hbd);
1510         }
1511 #endif
1512         fprintf(f, "%s\n", headings);
1513         fprintf(f, "%s\n", results);
1514       }
1515 
1516       fclose(f);
1517     }
1518 #endif  // CONFIG_INTERNAL_STATS
1519 #if CONFIG_SPEED_STATS
1520     if (!is_stat_generation_stage(cpi)) {
1521       fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
1522     }
1523 #endif  // CONFIG_SPEED_STATS
1524 
1525 #if CONFIG_COLLECT_PARTITION_STATS == 2
1526     if (!is_stat_generation_stage(cpi)) {
1527       av1_print_fr_partition_timing_stats(&cpi->partition_stats,
1528                                           "fr_part_timing_data.csv");
1529     }
1530 #endif
1531   }
1532 
1533 #if CONFIG_AV1_TEMPORAL_DENOISING
1534   av1_denoiser_free(&(cpi->denoiser));
1535 #endif
1536 
1537   TplParams *const tpl_data = &cpi->tpl_data;
1538   for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
1539     aom_free(tpl_data->tpl_stats_pool[frame]);
1540     aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]);
1541   }
1542 
1543   if (cpi->compressor_stage != LAP_STAGE) {
1544     terminate_worker_data(cpi);
1545     free_thread_data(cpi);
1546   }
1547 
1548   MultiThreadInfo *const mt_info = &cpi->mt_info;
1549 #if CONFIG_MULTITHREAD
1550   pthread_mutex_t *const enc_row_mt_mutex_ = mt_info->enc_row_mt.mutex_;
1551   pthread_mutex_t *const gm_mt_mutex_ = mt_info->gm_sync.mutex_;
1552   if (enc_row_mt_mutex_ != NULL) {
1553     pthread_mutex_destroy(enc_row_mt_mutex_);
1554     aom_free(enc_row_mt_mutex_);
1555   }
1556   if (gm_mt_mutex_ != NULL) {
1557     pthread_mutex_destroy(gm_mt_mutex_);
1558     aom_free(gm_mt_mutex_);
1559   }
1560 #endif
1561   av1_row_mt_mem_dealloc(cpi);
1562   if (cpi->compressor_stage != LAP_STAGE) {
1563     aom_free(mt_info->tile_thr_data);
1564     aom_free(mt_info->workers);
1565   }
1566 
1567 #if !CONFIG_REALTIME_ONLY
1568   av1_tpl_dealloc(&tpl_data->tpl_mt_sync);
1569 #endif
1570   if (mt_info->num_workers > 1) {
1571     av1_loop_filter_dealloc(&mt_info->lf_row_sync);
1572     av1_cdef_mt_dealloc(&mt_info->cdef_sync);
1573 #if !CONFIG_REALTIME_ONLY
1574     av1_loop_restoration_dealloc(&mt_info->lr_row_sync,
1575                                  mt_info->num_mod_workers[MOD_LR]);
1576     av1_gm_dealloc(&mt_info->gm_sync);
1577     av1_tf_mt_dealloc(&mt_info->tf_sync);
1578 #endif
1579   }
1580 
1581   dealloc_compressor_data(cpi);
1582 
1583 #if CONFIG_INTERNAL_STATS
1584   aom_free(cpi->ssim_vars);
1585   cpi->ssim_vars = NULL;
1586 #endif  // CONFIG_INTERNAL_STATS
1587 
1588   av1_remove_common(cm);
1589   av1_free_ref_frame_buffers(cm->buffer_pool);
1590 
1591   aom_free(cpi);
1592 
1593 #ifdef OUTPUT_YUV_REC
1594   fclose(yuv_rec_file);
1595 #endif
1596 
1597 #ifdef OUTPUT_YUV_DENOISED
1598   fclose(yuv_denoised_file);
1599 #endif
1600 }
1601 
generate_psnr_packet(AV1_COMP * cpi)1602 static void generate_psnr_packet(AV1_COMP *cpi) {
1603   struct aom_codec_cx_pkt pkt;
1604   int i;
1605   PSNR_STATS psnr;
1606 #if CONFIG_AV1_HIGHBITDEPTH
1607   const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
1608   const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
1609   aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
1610                        bit_depth, in_bit_depth);
1611 #else
1612   aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
1613 #endif
1614 
1615   for (i = 0; i < 4; ++i) {
1616     pkt.data.psnr.samples[i] = psnr.samples[i];
1617     pkt.data.psnr.sse[i] = psnr.sse[i];
1618     pkt.data.psnr.psnr[i] = psnr.psnr[i];
1619   }
1620 
1621 #if CONFIG_AV1_HIGHBITDEPTH
1622   if ((cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) &&
1623       (in_bit_depth < bit_depth)) {
1624     for (i = 0; i < 4; ++i) {
1625       pkt.data.psnr.samples_hbd[i] = psnr.samples_hbd[i];
1626       pkt.data.psnr.sse_hbd[i] = psnr.sse_hbd[i];
1627       pkt.data.psnr.psnr_hbd[i] = psnr.psnr_hbd[i];
1628     }
1629   }
1630 #endif
1631 
1632   pkt.kind = AOM_CODEC_PSNR_PKT;
1633   aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
1634 }
1635 
av1_use_as_reference(int * ext_ref_frame_flags,int ref_frame_flags)1636 int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags) {
1637   if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
1638 
1639   *ext_ref_frame_flags = ref_frame_flags;
1640   return 0;
1641 }
1642 
av1_copy_reference_enc(AV1_COMP * cpi,int idx,YV12_BUFFER_CONFIG * sd)1643 int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1644   AV1_COMMON *const cm = &cpi->common;
1645   const int num_planes = av1_num_planes(cm);
1646   YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
1647   if (cfg) {
1648     aom_yv12_copy_frame(cfg, sd, num_planes);
1649     return 0;
1650   } else {
1651     return -1;
1652   }
1653 }
1654 
av1_set_reference_enc(AV1_COMP * cpi,int idx,YV12_BUFFER_CONFIG * sd)1655 int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1656   AV1_COMMON *const cm = &cpi->common;
1657   const int num_planes = av1_num_planes(cm);
1658   YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
1659   if (cfg) {
1660     aom_yv12_copy_frame(sd, cfg, num_planes);
1661     return 0;
1662   } else {
1663     return -1;
1664   }
1665 }
1666 
1667 #ifdef OUTPUT_YUV_REC
aom_write_one_yuv_frame(AV1_COMMON * cm,YV12_BUFFER_CONFIG * s)1668 void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
1669   uint8_t *src = s->y_buffer;
1670   int h = cm->height;
1671   if (yuv_rec_file == NULL) return;
1672   if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
1673     uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
1674 
1675     do {
1676       fwrite(src16, s->y_width, 2, yuv_rec_file);
1677       src16 += s->y_stride;
1678     } while (--h);
1679 
1680     src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
1681     h = s->uv_height;
1682 
1683     do {
1684       fwrite(src16, s->uv_width, 2, yuv_rec_file);
1685       src16 += s->uv_stride;
1686     } while (--h);
1687 
1688     src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
1689     h = s->uv_height;
1690 
1691     do {
1692       fwrite(src16, s->uv_width, 2, yuv_rec_file);
1693       src16 += s->uv_stride;
1694     } while (--h);
1695 
1696     fflush(yuv_rec_file);
1697     return;
1698   }
1699 
1700   do {
1701     fwrite(src, s->y_width, 1, yuv_rec_file);
1702     src += s->y_stride;
1703   } while (--h);
1704 
1705   src = s->u_buffer;
1706   h = s->uv_height;
1707 
1708   do {
1709     fwrite(src, s->uv_width, 1, yuv_rec_file);
1710     src += s->uv_stride;
1711   } while (--h);
1712 
1713   src = s->v_buffer;
1714   h = s->uv_height;
1715 
1716   do {
1717     fwrite(src, s->uv_width, 1, yuv_rec_file);
1718     src += s->uv_stride;
1719   } while (--h);
1720 
1721   fflush(yuv_rec_file);
1722 }
1723 #endif  // OUTPUT_YUV_REC
1724 
set_mv_search_params(AV1_COMP * cpi)1725 static void set_mv_search_params(AV1_COMP *cpi) {
1726   const AV1_COMMON *const cm = &cpi->common;
1727   MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
1728   const int max_mv_def = AOMMAX(cm->width, cm->height);
1729 
1730   // Default based on max resolution.
1731   mv_search_params->mv_step_param = av1_init_search_range(max_mv_def);
1732 
1733   if (cpi->sf.mv_sf.auto_mv_step_size) {
1734     if (frame_is_intra_only(cm)) {
1735       // Initialize max_mv_magnitude for use in the first INTER frame
1736       // after a key/intra-only frame.
1737       mv_search_params->max_mv_magnitude = max_mv_def;
1738     } else {
1739       // Use cpi->max_mv_magnitude == -1 to exclude first pass case.
1740       if (cm->show_frame && mv_search_params->max_mv_magnitude != -1) {
1741         // Allow mv_steps to correspond to twice the max mv magnitude found
1742         // in the previous frame, capped by the default max_mv_magnitude based
1743         // on resolution.
1744         mv_search_params->mv_step_param = av1_init_search_range(
1745             AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude));
1746       }
1747       mv_search_params->max_mv_magnitude = -1;
1748     }
1749   }
1750 }
1751 
av1_set_screen_content_options(AV1_COMP * cpi,FeatureFlags * features)1752 void av1_set_screen_content_options(AV1_COMP *cpi, FeatureFlags *features) {
1753   const AV1_COMMON *const cm = &cpi->common;
1754 
1755   if (cm->seq_params.force_screen_content_tools != 2) {
1756     features->allow_screen_content_tools = features->allow_intrabc =
1757         cm->seq_params.force_screen_content_tools;
1758     return;
1759   }
1760 
1761   if (cpi->oxcf.mode == REALTIME) {
1762     assert(cm->seq_params.reduced_still_picture_hdr);
1763     features->allow_screen_content_tools = features->allow_intrabc = 0;
1764     return;
1765   }
1766 
1767   if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
1768     features->allow_screen_content_tools = features->allow_intrabc = 1;
1769     return;
1770   }
1771 
1772   // Estimate if the source frame is screen content, based on the portion of
1773   // blocks that have few luma colors.
1774   const uint8_t *src = cpi->unfiltered_source->y_buffer;
1775   assert(src != NULL);
1776   const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
1777   const int stride = cpi->unfiltered_source->y_stride;
1778   const int width = cpi->unfiltered_source->y_width;
1779   const int height = cpi->unfiltered_source->y_height;
1780   const int bd = cm->seq_params.bit_depth;
1781   const int blk_w = 16;
1782   const int blk_h = 16;
1783   // These threshold values are selected experimentally.
1784   const int color_thresh = 4;
1785   const unsigned int var_thresh = 0;
1786   // Counts of blocks with no more than color_thresh colors.
1787   int counts_1 = 0;
1788   // Counts of blocks with no more than color_thresh colors and variance larger
1789   // than var_thresh.
1790   int counts_2 = 0;
1791 
1792   for (int r = 0; r + blk_h <= height; r += blk_h) {
1793     for (int c = 0; c + blk_w <= width; c += blk_w) {
1794       int count_buf[1 << 8];  // Maximum (1 << 8) bins for hbd path.
1795       const uint8_t *const this_src = src + r * stride + c;
1796       int n_colors;
1797       if (use_hbd)
1798         av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd, NULL,
1799                                 count_buf, &n_colors, NULL);
1800       else
1801         av1_count_colors(this_src, stride, blk_w, blk_h, count_buf, &n_colors);
1802       if (n_colors > 1 && n_colors <= color_thresh) {
1803         ++counts_1;
1804         struct buf_2d buf;
1805         buf.stride = stride;
1806         buf.buf = (uint8_t *)this_src;
1807         const unsigned int var =
1808             use_hbd
1809                 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
1810                 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
1811         if (var > var_thresh) ++counts_2;
1812       }
1813     }
1814   }
1815 
1816   // The threshold values are selected experimentally.
1817   features->allow_screen_content_tools =
1818       counts_1 * blk_h * blk_w * 10 > width * height;
1819   // IntraBC would force loop filters off, so we use more strict rules that also
1820   // requires that the block has high variance.
1821   features->allow_intrabc = features->allow_screen_content_tools &&
1822                             counts_2 * blk_h * blk_w * 12 > width * height;
1823   cpi->use_screen_content_tools = features->allow_screen_content_tools;
1824   cpi->is_screen_content_type =
1825       features->allow_intrabc ||
1826       (counts_1 * blk_h * blk_w * 10 > width * height * 4 &&
1827        counts_2 * blk_h * blk_w * 30 > width * height);
1828 }
1829 
1830 // Function pointer to search site config initialization
1831 // of different search method functions.
1832 typedef void (*av1_init_search_site_config)(search_site_config *cfg, int stride,
1833                                             int level);
1834 
1835 av1_init_search_site_config
1836     av1_init_motion_compensation[NUM_DISTINCT_SEARCH_METHODS] = {
1837       av1_init_dsmotion_compensation,     av1_init_motion_compensation_nstep,
1838       av1_init_motion_compensation_nstep, av1_init_dsmotion_compensation,
1839       av1_init_motion_compensation_hex,   av1_init_motion_compensation_bigdia,
1840       av1_init_motion_compensation_square
1841     };
1842 
init_motion_estimation(AV1_COMP * cpi)1843 static void init_motion_estimation(AV1_COMP *cpi) {
1844   AV1_COMMON *const cm = &cpi->common;
1845   MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
1846   const int y_stride = cpi->scaled_source.y_stride;
1847   const int y_stride_src = ((cpi->oxcf.frm_dim_cfg.width != cm->width ||
1848                              cpi->oxcf.frm_dim_cfg.height != cm->height) ||
1849                             av1_superres_scaled(cm))
1850                                ? y_stride
1851                                : cpi->lookahead->buf->img.y_stride;
1852   int fpf_y_stride = cm->cur_frame != NULL ? cm->cur_frame->buf.y_stride
1853                                            : cpi->scaled_source.y_stride;
1854 
1855   // Update if search_site_cfg is uninitialized or the current frame has a new
1856   // stride
1857   const int should_update =
1858       !mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride ||
1859       !mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][DIAMOND].stride ||
1860       (y_stride !=
1861        mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride);
1862 
1863   if (!should_update) {
1864     return;
1865   }
1866 
1867   // Initialization of search_site_cfg for NUM_DISTINCT_SEARCH_METHODS.
1868   for (SEARCH_METHODS i = DIAMOND; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
1869     const int level = ((i == NSTEP_8PT) || (i == CLAMPED_DIAMOND)) ? 1 : 0;
1870     av1_init_motion_compensation[i](
1871         &mv_search_params->search_site_cfg[SS_CFG_SRC][i], y_stride, level);
1872     av1_init_motion_compensation[i](
1873         &mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][i], y_stride_src,
1874         level);
1875   }
1876 
1877   // First pass search site config initialization.
1878   av1_init_motion_fpf(&mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
1879                       fpf_y_stride);
1880   for (SEARCH_METHODS i = NSTEP; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
1881     memcpy(&mv_search_params->search_site_cfg[SS_CFG_FPF][i],
1882            &mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
1883            sizeof(search_site_config));
1884   }
1885 }
1886 
1887 #if !CONFIG_REALTIME_ONLY
1888 #define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
set_restoration_unit_size(int width,int height,int sx,int sy,RestorationInfo * rst)1889 static void set_restoration_unit_size(int width, int height, int sx, int sy,
1890                                       RestorationInfo *rst) {
1891   (void)width;
1892   (void)height;
1893   (void)sx;
1894   (void)sy;
1895 #if COUPLED_CHROMA_FROM_LUMA_RESTORATION
1896   int s = AOMMIN(sx, sy);
1897 #else
1898   int s = 0;
1899 #endif  // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
1900 
1901   if (width * height > 352 * 288)
1902     rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
1903   else
1904     rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
1905   rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
1906   rst[2].restoration_unit_size = rst[1].restoration_unit_size;
1907 }
1908 #endif
1909 
init_ref_frame_bufs(AV1_COMP * cpi)1910 static void init_ref_frame_bufs(AV1_COMP *cpi) {
1911   AV1_COMMON *const cm = &cpi->common;
1912   int i;
1913   BufferPool *const pool = cm->buffer_pool;
1914   cm->cur_frame = NULL;
1915   for (i = 0; i < REF_FRAMES; ++i) {
1916     cm->ref_frame_map[i] = NULL;
1917   }
1918   for (i = 0; i < FRAME_BUFFERS; ++i) {
1919     pool->frame_bufs[i].ref_count = 0;
1920   }
1921 }
1922 
av1_check_initial_width(AV1_COMP * cpi,int use_highbitdepth,int subsampling_x,int subsampling_y)1923 void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
1924                              int subsampling_x, int subsampling_y) {
1925   AV1_COMMON *const cm = &cpi->common;
1926   SequenceHeader *const seq_params = &cm->seq_params;
1927   InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
1928 
1929   if (!initial_dimensions->width ||
1930       seq_params->use_highbitdepth != use_highbitdepth ||
1931       seq_params->subsampling_x != subsampling_x ||
1932       seq_params->subsampling_y != subsampling_y) {
1933     seq_params->subsampling_x = subsampling_x;
1934     seq_params->subsampling_y = subsampling_y;
1935     seq_params->use_highbitdepth = use_highbitdepth;
1936 
1937     av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
1938     av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
1939 
1940     if (!is_stat_generation_stage(cpi)) {
1941       alloc_altref_frame_buffer(cpi);
1942       alloc_util_frame_buffers(cpi);
1943     }
1944     init_ref_frame_bufs(cpi);
1945 
1946     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
1947 
1948     initial_dimensions->width = cm->width;
1949     initial_dimensions->height = cm->height;
1950     cpi->initial_mbs = cm->mi_params.MBs;
1951   }
1952 }
1953 
1954 #if CONFIG_AV1_TEMPORAL_DENOISING
setup_denoiser_buffer(AV1_COMP * cpi)1955 static void setup_denoiser_buffer(AV1_COMP *cpi) {
1956   AV1_COMMON *const cm = &cpi->common;
1957   if (cpi->oxcf.noise_sensitivity > 0 &&
1958       !cpi->denoiser.frame_buffer_initialized) {
1959     if (av1_denoiser_alloc(
1960             cm, &cpi->svc, &cpi->denoiser, cpi->use_svc,
1961             cpi->oxcf.noise_sensitivity, cm->width, cm->height,
1962             cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
1963             cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS))
1964       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1965                          "Failed to allocate denoiser");
1966   }
1967 }
1968 #endif
1969 
1970 // Returns 1 if the assigned width or height was <= 0.
av1_set_size_literal(AV1_COMP * cpi,int width,int height)1971 int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
1972   AV1_COMMON *cm = &cpi->common;
1973   InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
1974   av1_check_initial_width(cpi, cm->seq_params.use_highbitdepth,
1975                           cm->seq_params.subsampling_x,
1976                           cm->seq_params.subsampling_y);
1977 
1978   if (width <= 0 || height <= 0) return 1;
1979 
1980   cm->width = width;
1981   cm->height = height;
1982 
1983 #if CONFIG_AV1_TEMPORAL_DENOISING
1984   setup_denoiser_buffer(cpi);
1985 #endif
1986 
1987   if (initial_dimensions->width && initial_dimensions->height &&
1988       (cm->width > initial_dimensions->width ||
1989        cm->height > initial_dimensions->height)) {
1990     av1_free_context_buffers(cm);
1991     av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
1992     av1_free_sms_tree(&cpi->td);
1993     av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
1994     cpi->td.firstpass_ctx = NULL;
1995     alloc_compressor_data(cpi);
1996     realloc_segmentation_maps(cpi);
1997     initial_dimensions->width = initial_dimensions->height = 0;
1998   }
1999   av1_update_frame_size(cpi);
2000 
2001   return 0;
2002 }
2003 
av1_set_frame_size(AV1_COMP * cpi,int width,int height)2004 void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
2005   AV1_COMMON *const cm = &cpi->common;
2006   const SequenceHeader *const seq_params = &cm->seq_params;
2007   const int num_planes = av1_num_planes(cm);
2008   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
2009   int ref_frame;
2010 
2011   if (width != cm->width || height != cm->height) {
2012     // There has been a change in the encoded frame size
2013     av1_set_size_literal(cpi, width, height);
2014     // Recalculate 'all_lossless' in case super-resolution was (un)selected.
2015     cm->features.all_lossless =
2016         cm->features.coded_lossless && !av1_superres_scaled(cm);
2017 
2018     av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
2019 #if CONFIG_AV1_TEMPORAL_DENOISING
2020     // Reset the denoiser on the resized frame.
2021     if (cpi->oxcf.noise_sensitivity > 0) {
2022       av1_denoiser_free(&(cpi->denoiser));
2023       setup_denoiser_buffer(cpi);
2024     }
2025 #endif
2026   }
2027   set_mv_search_params(cpi);
2028 
2029   if (is_stat_consumption_stage(cpi)) {
2030     av1_set_target_rate(cpi, cm->width, cm->height);
2031   }
2032 
2033   alloc_frame_mvs(cm, cm->cur_frame);
2034 
2035   // Allocate above context buffers
2036   CommonContexts *const above_contexts = &cm->above_contexts;
2037   if (above_contexts->num_planes < av1_num_planes(cm) ||
2038       above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
2039       above_contexts->num_tile_rows < cm->tiles.rows) {
2040     av1_free_above_context_buffers(above_contexts);
2041     if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
2042                                         cm->mi_params.mi_cols,
2043                                         av1_num_planes(cm)))
2044       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
2045                          "Failed to allocate context buffers");
2046   }
2047 
2048   // Reset the frame pointers to the current frame size.
2049   if (aom_realloc_frame_buffer(
2050           &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
2051           seq_params->subsampling_y, seq_params->use_highbitdepth,
2052           cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
2053           NULL))
2054     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
2055                        "Failed to allocate frame buffer");
2056 
2057 #if !CONFIG_REALTIME_ONLY
2058   const int use_restoration = cm->seq_params.enable_restoration &&
2059                               !cm->features.all_lossless &&
2060                               !cm->tiles.large_scale;
2061   if (use_restoration) {
2062     const int frame_width = cm->superres_upscaled_width;
2063     const int frame_height = cm->superres_upscaled_height;
2064     set_restoration_unit_size(frame_width, frame_height,
2065                               seq_params->subsampling_x,
2066                               seq_params->subsampling_y, cm->rst_info);
2067     for (int i = 0; i < num_planes; ++i)
2068       cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
2069 
2070     av1_alloc_restoration_buffers(cm);
2071   }
2072 #endif
2073   if (!is_stat_generation_stage(cpi)) alloc_util_frame_buffers(cpi);
2074   init_motion_estimation(cpi);
2075 
2076   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2077     RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
2078     if (buf != NULL) {
2079       struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
2080       av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
2081                                         buf->buf.y_crop_height, cm->width,
2082                                         cm->height);
2083       if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
2084     }
2085   }
2086 
2087   av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
2088                                     cm->width, cm->height);
2089 
2090   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2091 }
2092 
2093 /*!\brief Select and apply cdef filters and switchable restoration filters
2094  *
2095  * \ingroup high_level_algo
2096  */
cdef_restoration_frame(AV1_COMP * cpi,AV1_COMMON * cm,MACROBLOCKD * xd,int use_restoration,int use_cdef)2097 static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
2098                                    MACROBLOCKD *xd, int use_restoration,
2099                                    int use_cdef) {
2100 #if !CONFIG_REALTIME_ONLY
2101   if (use_restoration)
2102     av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
2103 #else
2104   (void)use_restoration;
2105 #endif
2106 
2107   if (use_cdef) {
2108 #if CONFIG_COLLECT_COMPONENT_TIMING
2109     start_timing(cpi, cdef_time);
2110 #endif
2111     // Find CDEF parameters
2112     av1_cdef_search(&cpi->mt_info, &cm->cur_frame->buf, cpi->source, cm, xd,
2113                     cpi->sf.lpf_sf.cdef_pick_method, cpi->td.mb.rdmult);
2114 
2115     // Apply the filter
2116     if (!cpi->sf.rt_sf.skip_loopfilter_non_reference)
2117       av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
2118 #if CONFIG_COLLECT_COMPONENT_TIMING
2119     end_timing(cpi, cdef_time);
2120 #endif
2121   } else {
2122     cm->cdef_info.cdef_bits = 0;
2123     cm->cdef_info.cdef_strengths[0] = 0;
2124     cm->cdef_info.nb_cdef_strengths = 1;
2125     cm->cdef_info.cdef_uv_strengths[0] = 0;
2126   }
2127 
2128   av1_superres_post_encode(cpi);
2129 
2130 #if !CONFIG_REALTIME_ONLY
2131 #if CONFIG_COLLECT_COMPONENT_TIMING
2132   start_timing(cpi, loop_restoration_time);
2133 #endif
2134   if (use_restoration) {
2135     MultiThreadInfo *const mt_info = &cpi->mt_info;
2136     const int num_workers = mt_info->num_mod_workers[MOD_LR];
2137     av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
2138     av1_pick_filter_restoration(cpi->source, cpi);
2139     if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2140         cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2141         cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2142       if (num_workers > 1)
2143         av1_loop_restoration_filter_frame_mt(
2144             &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
2145             &mt_info->lr_row_sync, &cpi->lr_ctxt);
2146       else
2147         av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
2148                                           &cpi->lr_ctxt);
2149     }
2150   } else {
2151     cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
2152     cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
2153     cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
2154   }
2155 #if CONFIG_COLLECT_COMPONENT_TIMING
2156   end_timing(cpi, loop_restoration_time);
2157 #endif
2158 #endif  // !CONFIG_REALTIME_ONLY
2159 }
2160 
2161 /*!\brief Select and apply in-loop deblocking filters, cdef filters, and
2162  * restoration filters
2163  *
2164  * \ingroup high_level_algo
2165  */
loopfilter_frame(AV1_COMP * cpi,AV1_COMMON * cm)2166 static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
2167   MultiThreadInfo *const mt_info = &cpi->mt_info;
2168   const int num_workers = mt_info->num_mod_workers[MOD_LPF];
2169   const int num_planes = av1_num_planes(cm);
2170   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2171 
2172   assert(IMPLIES(is_lossless_requested(&cpi->oxcf.rc_cfg),
2173                  cm->features.coded_lossless && cm->features.all_lossless));
2174 
2175   const int use_loopfilter =
2176       !cm->features.coded_lossless && !cm->tiles.large_scale;
2177   const int use_cdef = cm->seq_params.enable_cdef &&
2178                        !cm->features.coded_lossless && !cm->tiles.large_scale;
2179   const int use_restoration = cm->seq_params.enable_restoration &&
2180                               !cm->features.all_lossless &&
2181                               !cm->tiles.large_scale;
2182 
2183   struct loopfilter *lf = &cm->lf;
2184 
2185 #if CONFIG_COLLECT_COMPONENT_TIMING
2186   start_timing(cpi, loop_filter_time);
2187 #endif
2188   if (use_loopfilter) {
2189     aom_clear_system_state();
2190     av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
2191   } else {
2192     lf->filter_level[0] = 0;
2193     lf->filter_level[1] = 0;
2194   }
2195 
2196   if ((lf->filter_level[0] || lf->filter_level[1]) &&
2197       !cpi->sf.rt_sf.skip_loopfilter_non_reference) {
2198     if (num_workers > 1)
2199       av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
2200 #if CONFIG_LPF_MASK
2201                                0,
2202 #endif
2203                                mt_info->workers, num_workers,
2204                                &mt_info->lf_row_sync);
2205     else
2206       av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
2207 #if CONFIG_LPF_MASK
2208                             0,
2209 #endif
2210                             0, num_planes, 0);
2211   }
2212 #if CONFIG_COLLECT_COMPONENT_TIMING
2213   end_timing(cpi, loop_filter_time);
2214 #endif
2215 
2216   cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
2217 }
2218 
2219 /*!\brief Encode a frame without the recode loop, usually used in one-pass
2220  * encoding and realtime coding.
2221  *
2222  * \ingroup high_level_algo
2223  *
2224  * \param[in]    cpi             Top-level encoder structure
2225  *
2226  * \return Returns a value to indicate if the encoding is done successfully.
2227  * \retval #AOM_CODEC_OK
2228  * \retval #AOM_CODEC_ERROR
2229  */
encode_without_recode(AV1_COMP * cpi)2230 static int encode_without_recode(AV1_COMP *cpi) {
2231   AV1_COMMON *const cm = &cpi->common;
2232   const QuantizationCfg *const q_cfg = &cpi->oxcf.q_cfg;
2233   SVC *const svc = &cpi->svc;
2234   ResizePendingParams *const resize_pending_params =
2235       &cpi->resize_pending_params;
2236   const int resize_pending =
2237       (resize_pending_params->width && resize_pending_params->height &&
2238        (cpi->common.width != resize_pending_params->width ||
2239         cpi->common.height != resize_pending_params->height));
2240 
2241   int top_index = 0, bottom_index = 0, q = 0;
2242   YV12_BUFFER_CONFIG *unscaled = cpi->unscaled_source;
2243   InterpFilter filter_scaler =
2244       cpi->use_svc ? svc->downsample_filter_type[svc->spatial_layer_id]
2245                    : EIGHTTAP_SMOOTH;
2246   int phase_scaler =
2247       cpi->use_svc ? svc->downsample_filter_phase[svc->spatial_layer_id] : 0;
2248 
2249   set_size_independent_vars(cpi);
2250   av1_setup_frame_size(cpi);
2251   av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2252 
2253   if (!cpi->use_svc) {
2254     phase_scaler = 8;
2255     // 2:1 scaling.
2256     if ((cm->width << 1) == unscaled->y_crop_width &&
2257         (cm->height << 1) == unscaled->y_crop_height) {
2258       filter_scaler = BILINEAR;
2259       // For lower resolutions use eighttap_smooth.
2260       if (cm->width * cm->height <= 320 * 180) filter_scaler = EIGHTTAP_SMOOTH;
2261     } else if ((cm->width << 2) == unscaled->y_crop_width &&
2262                (cm->height << 2) == unscaled->y_crop_height) {
2263       // 4:1 scaling.
2264       filter_scaler = EIGHTTAP_SMOOTH;
2265     } else if ((cm->width << 2) == 3 * unscaled->y_crop_width &&
2266                (cm->height << 2) == 3 * unscaled->y_crop_height) {
2267       // 4:3 scaling.
2268       filter_scaler = EIGHTTAP_REGULAR;
2269     }
2270   }
2271 
2272   if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION)
2273     variance_partition_alloc(cpi);
2274 
2275   if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi);
2276 
2277 #if CONFIG_COLLECT_COMPONENT_TIMING
2278   printf("\n Encoding a frame:");
2279 #endif
2280 
2281   aom_clear_system_state();
2282 
2283   cpi->source = av1_scale_if_required(cm, unscaled, &cpi->scaled_source,
2284                                       filter_scaler, phase_scaler, true, false);
2285   if (frame_is_intra_only(cm) || resize_pending != 0) {
2286     memset(cpi->consec_zero_mv, 0,
2287            ((cm->mi_params.mi_rows * cm->mi_params.mi_cols) >> 2) *
2288                sizeof(*cpi->consec_zero_mv));
2289   }
2290 
2291   if (cpi->unscaled_last_source != NULL) {
2292     cpi->last_source = av1_scale_if_required(
2293         cm, cpi->unscaled_last_source, &cpi->scaled_last_source, filter_scaler,
2294         phase_scaler, true, false);
2295   }
2296 
2297   if (cpi->sf.rt_sf.use_temporal_noise_estimate) {
2298     av1_update_noise_estimate(cpi);
2299   }
2300 
2301 #if CONFIG_AV1_TEMPORAL_DENOISING
2302   if (cpi->oxcf.noise_sensitivity > 0 && cpi->use_svc)
2303     av1_denoiser_reset_on_first_frame(cpi);
2304 #endif
2305 
2306   // For 1 spatial layer encoding: if the (non-LAST) reference has different
2307   // resolution from the source then disable that reference. This is to avoid
2308   // significant increase in encode time from scaling the references in
2309   // av1_scale_references. Note GOLDEN is forced to update on the (first/tigger)
2310   // resized frame and ALTREF will be refreshed ~4 frames later, so both
2311   // references become available again after few frames.
2312   if (svc->number_spatial_layers == 1) {
2313     if (cpi->ref_frame_flags & av1_ref_frame_flag_list[GOLDEN_FRAME]) {
2314       const YV12_BUFFER_CONFIG *const ref =
2315           get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
2316       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
2317         cpi->ref_frame_flags ^= AOM_GOLD_FLAG;
2318     }
2319     if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ALTREF_FRAME]) {
2320       const YV12_BUFFER_CONFIG *const ref =
2321           get_ref_frame_yv12_buf(cm, ALTREF_FRAME);
2322       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
2323         cpi->ref_frame_flags ^= AOM_ALT_FLAG;
2324     }
2325   }
2326 
2327   // For SVC the inter-layer/spatial prediction is not done for newmv
2328   // (zero_mode is forced), and since the scaled references are only
2329   // use for newmv search, we can avoid scaling here.
2330   if (!frame_is_intra_only(cm) &&
2331       !(cpi->use_svc && cpi->svc.force_zero_mode_spatial_ref))
2332     av1_scale_references(cpi, filter_scaler, phase_scaler, 1);
2333 
2334   av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2335                     q_cfg->enable_chroma_deltaq);
2336   av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
2337   if ((q_cfg->deltaq_mode != NO_DELTA_Q) || q_cfg->enable_chroma_deltaq)
2338     av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
2339                        cm->seq_params.bit_depth);
2340   av1_set_variance_partition_thresholds(cpi, q, 0);
2341   av1_setup_frame(cpi);
2342 
2343   // Check if this high_source_sad (scene/slide change) frame should be
2344   // encoded at high/max QP, and if so, set the q and adjust some rate
2345   // control parameters.
2346   if (cpi->sf.rt_sf.overshoot_detection_cbr == FAST_DETECTION_MAXQ &&
2347       cpi->rc.high_source_sad) {
2348     if (av1_encodedframe_overshoot_cbr(cpi, &q)) {
2349       av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2350                         q_cfg->enable_chroma_deltaq);
2351       av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
2352       if (q_cfg->deltaq_mode != NO_DELTA_Q || q_cfg->enable_chroma_deltaq)
2353         av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
2354                            cm->seq_params.bit_depth);
2355       av1_set_variance_partition_thresholds(cpi, q, 0);
2356       if (frame_is_intra_only(cm) || cm->features.error_resilient_mode)
2357         av1_setup_frame(cpi);
2358     }
2359   }
2360 
2361   if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ) {
2362     suppress_active_map(cpi);
2363     av1_cyclic_refresh_setup(cpi);
2364     av1_apply_active_map(cpi);
2365   }
2366   if (cm->seg.enabled) {
2367     if (!cm->seg.update_data && cm->prev_frame) {
2368       segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
2369       cm->seg.enabled = cm->prev_frame->seg.enabled;
2370     } else {
2371       av1_calculate_segdata(&cm->seg);
2372     }
2373   } else {
2374     memset(&cm->seg, 0, sizeof(cm->seg));
2375   }
2376   segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
2377   cm->cur_frame->seg.enabled = cm->seg.enabled;
2378 
2379 #if CONFIG_COLLECT_COMPONENT_TIMING
2380   start_timing(cpi, av1_encode_frame_time);
2381 #endif
2382 
2383   // Set the motion vector precision based on mv stats from the last coded
2384   // frame.
2385   if (!frame_is_intra_only(cm)) av1_pick_and_set_high_precision_mv(cpi, q);
2386 
2387   // transform / motion compensation build reconstruction frame
2388   av1_encode_frame(cpi);
2389 
2390   // Update some stats from cyclic refresh.
2391   if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ && !frame_is_intra_only(cm))
2392     av1_cyclic_refresh_postencode(cpi);
2393 
2394 #if CONFIG_COLLECT_COMPONENT_TIMING
2395   end_timing(cpi, av1_encode_frame_time);
2396 #endif
2397 #if CONFIG_INTERNAL_STATS
2398   ++cpi->tot_recode_hits;
2399 #endif
2400 
2401   aom_clear_system_state();
2402 
2403   return AOM_CODEC_OK;
2404 }
2405 
2406 #if !CONFIG_REALTIME_ONLY
2407 
2408 /*!\brief Recode loop for encoding one frame. the purpose of encoding one frame
2409  * for multiple times can be approaching a target bitrate or adjusting the usage
2410  * of global motions.
2411  *
2412  * \ingroup high_level_algo
2413  *
2414  * \param[in]    cpi             Top-level encoder structure
2415  * \param[in]    size            Bitstream size
2416  * \param[in]    dest            Bitstream output
2417  *
2418  * \return Returns a value to indicate if the encoding is done successfully.
2419  * \retval #AOM_CODEC_OK
2420  * \retval -1
2421  * \retval #AOM_CODEC_ERROR
2422  */
encode_with_recode_loop(AV1_COMP * cpi,size_t * size,uint8_t * dest)2423 static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
2424   AV1_COMMON *const cm = &cpi->common;
2425   RATE_CONTROL *const rc = &cpi->rc;
2426   GlobalMotionInfo *const gm_info = &cpi->gm_info;
2427   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2428   const QuantizationCfg *const q_cfg = &oxcf->q_cfg;
2429   const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
2430   // Must allow recode if minimum compression ratio is set.
2431   assert(IMPLIES(oxcf->rc_cfg.min_cr > 0, allow_recode));
2432 
2433   set_size_independent_vars(cpi);
2434   if (is_stat_consumption_stage_twopass(cpi) &&
2435       cpi->sf.interp_sf.adaptive_interp_filter_search)
2436     cpi->interp_search_flags.interp_filter_search_mask =
2437         av1_setup_interp_filter_search_mask(cpi);
2438   cpi->source->buf_8bit_valid = 0;
2439 
2440   av1_setup_frame_size(cpi);
2441 
2442   if (av1_superres_in_recode_allowed(cpi) &&
2443       cpi->superres_mode != AOM_SUPERRES_NONE &&
2444       cm->superres_scale_denominator == SCALE_NUMERATOR) {
2445     // Superres mode is currently enabled, but the denominator selected will
2446     // disable superres. So no need to continue, as we will go through another
2447     // recode loop for full-resolution after this anyway.
2448     return -1;
2449   }
2450 
2451   int top_index = 0, bottom_index = 0;
2452   int q = 0, q_low = 0, q_high = 0;
2453   av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2454   q_low = bottom_index;
2455   q_high = top_index;
2456 
2457   if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION)
2458     variance_partition_alloc(cpi);
2459 
2460   if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi);
2461 
2462 #if CONFIG_COLLECT_COMPONENT_TIMING
2463   printf("\n Encoding a frame:");
2464 #endif
2465 
2466   // Determine whether to use screen content tools using two fast encoding.
2467   if (!cpi->sf.hl_sf.disable_extra_sc_testing)
2468     av1_determine_sc_tools_with_encoding(cpi, q);
2469 
2470 #if CONFIG_USE_VMAF_RC
2471   if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2472     av1_vmaf_neg_preprocessing(cpi, cpi->unscaled_source);
2473   }
2474 #endif
2475 
2476   // Loop variables
2477   int loop = 0;
2478   int loop_count = 0;
2479   int overshoot_seen = 0;
2480   int undershoot_seen = 0;
2481   int low_cr_seen = 0;
2482   int last_loop_allow_hp = 0;
2483 
2484   do {
2485     loop = 0;
2486     aom_clear_system_state();
2487 
2488     // if frame was scaled calculate global_motion_search again if already
2489     // done
2490     if (loop_count > 0 && cpi->source && gm_info->search_done) {
2491       if (cpi->source->y_crop_width != cm->width ||
2492           cpi->source->y_crop_height != cm->height) {
2493         gm_info->search_done = 0;
2494       }
2495     }
2496     cpi->source =
2497         av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source,
2498                               EIGHTTAP_REGULAR, 0, false, false);
2499 
2500     if (cpi->unscaled_last_source != NULL) {
2501       cpi->last_source = av1_scale_if_required(
2502           cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
2503           EIGHTTAP_REGULAR, 0, false, false);
2504     }
2505 
2506     if (!frame_is_intra_only(cm)) {
2507       if (loop_count > 0) {
2508         release_scaled_references(cpi);
2509       }
2510       av1_scale_references(cpi, EIGHTTAP_REGULAR, 0, 0);
2511     }
2512 #if CONFIG_TUNE_VMAF
2513     if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
2514         oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2515       cpi->vmaf_info.original_qindex = q;
2516       q = av1_get_vmaf_base_qindex(cpi, q);
2517     }
2518 #endif
2519     av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2520                       q_cfg->enable_chroma_deltaq);
2521     av1_set_speed_features_qindex_dependent(cpi, oxcf->speed);
2522 
2523     if (q_cfg->deltaq_mode != NO_DELTA_Q || q_cfg->enable_chroma_deltaq)
2524       av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
2525                          cm->seq_params.bit_depth);
2526 
2527     av1_set_variance_partition_thresholds(cpi, q, 0);
2528 
2529     // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
2530     //        cm->current_frame.frame_number, cm->show_frame, q,
2531     //        cm->current_frame.frame_type, cm->superres_scale_denominator);
2532 
2533     if (loop_count == 0) {
2534       av1_setup_frame(cpi);
2535     } else if (get_primary_ref_frame_buf(cm) == NULL) {
2536       // Base q-index may have changed, so we need to assign proper default coef
2537       // probs before every iteration.
2538       av1_default_coef_probs(cm);
2539       av1_setup_frame_contexts(cm);
2540     }
2541 
2542     if (q_cfg->aq_mode == VARIANCE_AQ) {
2543       av1_vaq_frame_setup(cpi);
2544     } else if (q_cfg->aq_mode == COMPLEXITY_AQ) {
2545       av1_setup_in_frame_q_adj(cpi);
2546     }
2547 
2548     if (cm->seg.enabled) {
2549       if (!cm->seg.update_data && cm->prev_frame) {
2550         segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
2551         cm->seg.enabled = cm->prev_frame->seg.enabled;
2552       } else {
2553         av1_calculate_segdata(&cm->seg);
2554       }
2555     } else {
2556       memset(&cm->seg, 0, sizeof(cm->seg));
2557     }
2558     segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
2559     cm->cur_frame->seg.enabled = cm->seg.enabled;
2560 
2561 #if CONFIG_COLLECT_COMPONENT_TIMING
2562     start_timing(cpi, av1_encode_frame_time);
2563 #endif
2564     // Set the motion vector precision based on mv stats from the last coded
2565     // frame.
2566     if (!frame_is_intra_only(cm)) {
2567       av1_pick_and_set_high_precision_mv(cpi, q);
2568 
2569       // If the precision has changed during different iteration of the loop,
2570       // then we need to reset the global motion vectors
2571       if (loop_count > 0 &&
2572           cm->features.allow_high_precision_mv != last_loop_allow_hp) {
2573         gm_info->search_done = 0;
2574       }
2575       last_loop_allow_hp = cm->features.allow_high_precision_mv;
2576     }
2577 
2578     // transform / motion compensation build reconstruction frame
2579     av1_encode_frame(cpi);
2580 
2581     // Reset the mv_stats in case we are interrupted by an intraframe or an
2582     // overlay frame.
2583     if (cpi->mv_stats.valid) {
2584       av1_zero(cpi->mv_stats);
2585     }
2586     // Gather the mv_stats for the next frame
2587     if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
2588         av1_frame_allows_smart_mv(cpi)) {
2589       av1_collect_mv_stats(cpi, q);
2590     }
2591 
2592 #if CONFIG_COLLECT_COMPONENT_TIMING
2593     end_timing(cpi, av1_encode_frame_time);
2594 #endif
2595 
2596     aom_clear_system_state();
2597 
2598     // Dummy pack of the bitstream using up to date stats to get an
2599     // accurate estimate of output frame size to determine if we need
2600     // to recode.
2601     const int do_dummy_pack =
2602         (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
2603          oxcf->rc_cfg.mode != AOM_Q) ||
2604         oxcf->rc_cfg.min_cr > 0;
2605     if (do_dummy_pack) {
2606       av1_finalize_encoded_frame(cpi);
2607       int largest_tile_id = 0;  // Output from bitstream: unused here
2608       rc->coefficient_size = 0;
2609       if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) !=
2610           AOM_CODEC_OK) {
2611         return AOM_CODEC_ERROR;
2612       }
2613 
2614       rc->projected_frame_size = (int)(*size) << 3;
2615     }
2616 
2617 #if CONFIG_TUNE_VMAF
2618     if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
2619         oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2620       q = cpi->vmaf_info.original_qindex;
2621     }
2622 #endif
2623     if (allow_recode) {
2624       // Update q and decide whether to do a recode loop
2625       recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
2626                            bottom_index, &undershoot_seen, &overshoot_seen,
2627                            &low_cr_seen, loop_count);
2628     }
2629 
2630     if (loop) {
2631       ++loop_count;
2632 
2633 #if CONFIG_INTERNAL_STATS
2634       ++cpi->tot_recode_hits;
2635 #endif
2636     }
2637 #if CONFIG_COLLECT_COMPONENT_TIMING
2638     if (loop) printf("\n Recoding:");
2639 #endif
2640   } while (loop);
2641 
2642   return AOM_CODEC_OK;
2643 }
2644 #endif  // !CONFIG_REALTIME_ONLY
2645 
2646 // TODO(jingning, paulwilkins): Set up high grain level to test
2647 // hardware decoders. Need to adapt the actual noise variance
2648 // according to the difference between reconstructed frame and the
2649 // source signal.
set_grain_syn_params(AV1_COMMON * cm)2650 static void set_grain_syn_params(AV1_COMMON *cm) {
2651   aom_film_grain_t *film_grain_params = &cm->film_grain_params;
2652   film_grain_params->apply_grain = 1;
2653   film_grain_params->update_parameters = 1;
2654   film_grain_params->random_seed = rand() & 0xffff;
2655 
2656   film_grain_params->num_y_points = 1;
2657   film_grain_params->scaling_points_y[0][0] = 128;
2658   film_grain_params->scaling_points_y[0][1] = 100;
2659 
2660   film_grain_params->num_cb_points = 1;
2661   film_grain_params->scaling_points_cb[0][0] = 128;
2662   film_grain_params->scaling_points_cb[0][1] = 100;
2663 
2664   film_grain_params->num_cr_points = 1;
2665   film_grain_params->scaling_points_cr[0][0] = 128;
2666   film_grain_params->scaling_points_cr[0][1] = 100;
2667 
2668   film_grain_params->chroma_scaling_from_luma = 0;
2669   film_grain_params->scaling_shift = 1;
2670   film_grain_params->ar_coeff_lag = 0;
2671   film_grain_params->ar_coeff_shift = 1;
2672   film_grain_params->overlap_flag = 1;
2673   film_grain_params->grain_scale_shift = 0;
2674 }
2675 
2676 /*!\brief Recode loop or a single loop for encoding one frame, followed by
2677  * in-loop deblocking filters, CDEF filters, and restoration filters.
2678  *
2679  * \ingroup high_level_algo
2680  * \callgraph
2681  * \callergraph
2682  *
2683  * \param[in]    cpi             Top-level encoder structure
2684  * \param[in]    size            Bitstream size
2685  * \param[in]    dest            Bitstream output
2686  * \param[in]    sse             Total distortion of the frame
2687  * \param[in]    rate            Total rate of the frame
2688  * \param[in]    largest_tile_id Tile id of the last tile
2689  *
2690  * \return Returns a value to indicate if the encoding is done successfully.
2691  * \retval #AOM_CODEC_OK
2692  * \retval #AOM_CODEC_ERROR
2693  */
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)2694 static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
2695                                               uint8_t *dest, int64_t *sse,
2696                                               int64_t *rate,
2697                                               int *largest_tile_id) {
2698 #if CONFIG_COLLECT_COMPONENT_TIMING
2699   start_timing(cpi, encode_with_recode_loop_time);
2700 #endif
2701   int err;
2702 #if CONFIG_REALTIME_ONLY
2703   err = encode_without_recode(cpi);
2704 #else
2705   if (cpi->sf.hl_sf.recode_loop == DISALLOW_RECODE)
2706     err = encode_without_recode(cpi);
2707   else
2708     err = encode_with_recode_loop(cpi, size, dest);
2709 #endif
2710 #if CONFIG_COLLECT_COMPONENT_TIMING
2711   end_timing(cpi, encode_with_recode_loop_time);
2712 #endif
2713   if (err != AOM_CODEC_OK) {
2714     if (err == -1) {
2715       // special case as described in encode_with_recode_loop().
2716       // Encoding was skipped.
2717       err = AOM_CODEC_OK;
2718       if (sse != NULL) *sse = INT64_MAX;
2719       if (rate != NULL) *rate = INT64_MAX;
2720       *largest_tile_id = 0;
2721     }
2722     return err;
2723   }
2724 
2725 #ifdef OUTPUT_YUV_DENOISED
2726   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2727   if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
2728     aom_write_yuv_frame(yuv_denoised_file,
2729                         &cpi->denoiser.running_avg_y[INTRA_FRAME]);
2730   }
2731 #endif
2732 
2733   AV1_COMMON *const cm = &cpi->common;
2734   SequenceHeader *const seq_params = &cm->seq_params;
2735 
2736   // Special case code to reduce pulsing when key frames are forced at a
2737   // fixed interval. Note the reconstruction error if it is the frame before
2738   // the force key frame
2739   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2740 #if CONFIG_AV1_HIGHBITDEPTH
2741     if (seq_params->use_highbitdepth) {
2742       cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
2743     } else {
2744       cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2745     }
2746 #else
2747     cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2748 #endif
2749   }
2750 
2751   cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
2752   cm->cur_frame->buf.transfer_characteristics =
2753       seq_params->transfer_characteristics;
2754   cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
2755   cm->cur_frame->buf.monochrome = seq_params->monochrome;
2756   cm->cur_frame->buf.chroma_sample_position =
2757       seq_params->chroma_sample_position;
2758   cm->cur_frame->buf.color_range = seq_params->color_range;
2759   cm->cur_frame->buf.render_width = cm->render_width;
2760   cm->cur_frame->buf.render_height = cm->render_height;
2761 
2762   // Pick the loop filter level for the frame.
2763   if (!cm->features.allow_intrabc) {
2764     loopfilter_frame(cpi, cm);
2765   } else {
2766     cm->lf.filter_level[0] = 0;
2767     cm->lf.filter_level[1] = 0;
2768     cm->cdef_info.cdef_bits = 0;
2769     cm->cdef_info.cdef_strengths[0] = 0;
2770     cm->cdef_info.nb_cdef_strengths = 1;
2771     cm->cdef_info.cdef_uv_strengths[0] = 0;
2772     cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
2773     cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
2774     cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
2775   }
2776 
2777   // TODO(debargha): Fix mv search range on encoder side
2778   // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
2779   aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
2780 
2781 #ifdef OUTPUT_YUV_REC
2782   aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
2783 #endif
2784 
2785   if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_FILM) {
2786     set_grain_syn_params(cm);
2787   }
2788 
2789   av1_finalize_encoded_frame(cpi);
2790   // Build the bitstream
2791 #if CONFIG_COLLECT_COMPONENT_TIMING
2792   start_timing(cpi, av1_pack_bitstream_final_time);
2793 #endif
2794   cpi->rc.coefficient_size = 0;
2795   if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
2796     return AOM_CODEC_ERROR;
2797 #if CONFIG_COLLECT_COMPONENT_TIMING
2798   end_timing(cpi, av1_pack_bitstream_final_time);
2799 #endif
2800 
2801   // Compute sse and rate.
2802   if (sse != NULL) {
2803 #if CONFIG_AV1_HIGHBITDEPTH
2804     *sse = (seq_params->use_highbitdepth)
2805                ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
2806                : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2807 #else
2808     *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2809 #endif
2810   }
2811   if (rate != NULL) {
2812     const int64_t bits = (*size << 3);
2813     *rate = (bits << 5);  // To match scale.
2814   }
2815   return AOM_CODEC_OK;
2816 }
2817 
encode_with_and_without_superres(AV1_COMP * cpi,size_t * size,uint8_t * dest,int * largest_tile_id)2818 static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
2819                                             uint8_t *dest,
2820                                             int *largest_tile_id) {
2821   const AV1_COMMON *const cm = &cpi->common;
2822   assert(cm->seq_params.enable_superres);
2823   assert(av1_superres_in_recode_allowed(cpi));
2824   aom_codec_err_t err = AOM_CODEC_OK;
2825   av1_save_all_coding_context(cpi);
2826 
2827   int64_t sse1 = INT64_MAX;
2828   int64_t rate1 = INT64_MAX;
2829   int largest_tile_id1 = 0;
2830   int64_t sse2 = INT64_MAX;
2831   int64_t rate2 = INT64_MAX;
2832   int largest_tile_id2;
2833   double proj_rdcost1 = DBL_MAX;
2834 
2835   // Encode with superres.
2836   if (cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_ALL) {
2837     SuperResCfg *const superres_cfg = &cpi->oxcf.superres_cfg;
2838     int64_t superres_sses[SCALE_NUMERATOR];
2839     int64_t superres_rates[SCALE_NUMERATOR];
2840     int superres_largest_tile_ids[SCALE_NUMERATOR];
2841     // Use superres for Key-frames and Alt-ref frames only.
2842     const GF_GROUP *const gf_group = &cpi->gf_group;
2843     if (gf_group->update_type[gf_group->index] != OVERLAY_UPDATE &&
2844         gf_group->update_type[gf_group->index] != INTNL_OVERLAY_UPDATE) {
2845       for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
2846            ++denom) {
2847         superres_cfg->superres_scale_denominator = denom;
2848         superres_cfg->superres_kf_scale_denominator = denom;
2849         const int this_index = denom - (SCALE_NUMERATOR + 1);
2850 
2851         cpi->superres_mode = AOM_SUPERRES_AUTO;  // Super-res on for this loop.
2852         err = encode_with_recode_loop_and_filter(
2853             cpi, size, dest, &superres_sses[this_index],
2854             &superres_rates[this_index],
2855             &superres_largest_tile_ids[this_index]);
2856         cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
2857         if (err != AOM_CODEC_OK) return err;
2858         restore_all_coding_context(cpi);
2859       }
2860       // Reset.
2861       superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
2862       superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
2863     } else {
2864       for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
2865            ++denom) {
2866         const int this_index = denom - (SCALE_NUMERATOR + 1);
2867         superres_sses[this_index] = INT64_MAX;
2868         superres_rates[this_index] = INT64_MAX;
2869       }
2870     }
2871     // Encode without superres.
2872     assert(cpi->superres_mode == AOM_SUPERRES_NONE);
2873     err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
2874                                              &largest_tile_id2);
2875     if (err != AOM_CODEC_OK) return err;
2876 
2877     // Note: Both use common rdmult based on base qindex of fullres.
2878     const int64_t rdmult =
2879         av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
2880 
2881     // Find the best rdcost among all superres denoms.
2882     int best_denom = -1;
2883     for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
2884          ++denom) {
2885       const int this_index = denom - (SCALE_NUMERATOR + 1);
2886       const int64_t this_sse = superres_sses[this_index];
2887       const int64_t this_rate = superres_rates[this_index];
2888       const int this_largest_tile_id = superres_largest_tile_ids[this_index];
2889       const double this_rdcost = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2890           rdmult, this_rate, this_sse, cm->seq_params.bit_depth);
2891       if (this_rdcost < proj_rdcost1) {
2892         sse1 = this_sse;
2893         rate1 = this_rate;
2894         largest_tile_id1 = this_largest_tile_id;
2895         proj_rdcost1 = this_rdcost;
2896         best_denom = denom;
2897       }
2898     }
2899     const double proj_rdcost2 = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2900         rdmult, rate2, sse2, cm->seq_params.bit_depth);
2901     // Re-encode with superres if it's better.
2902     if (proj_rdcost1 < proj_rdcost2) {
2903       restore_all_coding_context(cpi);
2904       // TODO(urvang): We should avoid rerunning the recode loop by saving
2905       // previous output+state, or running encode only for the selected 'q' in
2906       // previous step.
2907       // Again, temporarily force the best denom.
2908       superres_cfg->superres_scale_denominator = best_denom;
2909       superres_cfg->superres_kf_scale_denominator = best_denom;
2910       int64_t sse3 = INT64_MAX;
2911       int64_t rate3 = INT64_MAX;
2912       cpi->superres_mode =
2913           AOM_SUPERRES_AUTO;  // Super-res on for this recode loop.
2914       err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
2915                                                largest_tile_id);
2916       cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
2917       assert(sse1 == sse3);
2918       assert(rate1 == rate3);
2919       assert(largest_tile_id1 == *largest_tile_id);
2920       // Reset.
2921       superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
2922       superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
2923     } else {
2924       *largest_tile_id = largest_tile_id2;
2925     }
2926   } else {
2927     assert(cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_DUAL);
2928     cpi->superres_mode =
2929         AOM_SUPERRES_AUTO;  // Super-res on for this recode loop.
2930     err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
2931                                              &largest_tile_id1);
2932     cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
2933     if (err != AOM_CODEC_OK) return err;
2934     restore_all_coding_context(cpi);
2935     // Encode without superres.
2936     assert(cpi->superres_mode == AOM_SUPERRES_NONE);
2937     err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
2938                                              &largest_tile_id2);
2939     if (err != AOM_CODEC_OK) return err;
2940 
2941     // Note: Both use common rdmult based on base qindex of fullres.
2942     const int64_t rdmult =
2943         av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
2944     proj_rdcost1 = RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate1, sse1,
2945                                                   cm->seq_params.bit_depth);
2946     const double proj_rdcost2 = RDCOST_DBL_WITH_NATIVE_BD_DIST(
2947         rdmult, rate2, sse2, cm->seq_params.bit_depth);
2948     // Re-encode with superres if it's better.
2949     if (proj_rdcost1 < proj_rdcost2) {
2950       restore_all_coding_context(cpi);
2951       // TODO(urvang): We should avoid rerunning the recode loop by saving
2952       // previous output+state, or running encode only for the selected 'q' in
2953       // previous step.
2954       int64_t sse3 = INT64_MAX;
2955       int64_t rate3 = INT64_MAX;
2956       cpi->superres_mode =
2957           AOM_SUPERRES_AUTO;  // Super-res on for this recode loop.
2958       err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
2959                                                largest_tile_id);
2960       cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
2961       assert(sse1 == sse3);
2962       assert(rate1 == rate3);
2963       assert(largest_tile_id1 == *largest_tile_id);
2964     } else {
2965       *largest_tile_id = largest_tile_id2;
2966     }
2967   }
2968 
2969   return err;
2970 }
2971 
2972 extern void av1_print_frame_contexts(const FRAME_CONTEXT *fc,
2973                                      const char *filename);
2974 
2975 /*!\brief Run the final pass encoding for 1-pass/2-pass encoding mode, and pack
2976  * the bitstream
2977  *
2978  * \ingroup high_level_algo
2979  * \callgraph
2980  * \callergraph
2981  *
2982  * \param[in]    cpi             Top-level encoder structure
2983  * \param[in]    size            Bitstream size
2984  * \param[in]    dest            Bitstream output
2985  *
2986  * \return Returns a value to indicate if the encoding is done successfully.
2987  * \retval #AOM_CODEC_OK
2988  * \retval #AOM_CODEC_ERROR
2989  */
encode_frame_to_data_rate(AV1_COMP * cpi,size_t * size,uint8_t * dest)2990 static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
2991                                      uint8_t *dest) {
2992   AV1_COMMON *const cm = &cpi->common;
2993   SequenceHeader *const seq_params = &cm->seq_params;
2994   CurrentFrame *const current_frame = &cm->current_frame;
2995   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2996   struct segmentation *const seg = &cm->seg;
2997   FeatureFlags *const features = &cm->features;
2998   const TileConfig *const tile_cfg = &oxcf->tile_cfg;
2999 
3000 #if CONFIG_COLLECT_COMPONENT_TIMING
3001   start_timing(cpi, encode_frame_to_data_rate_time);
3002 #endif
3003 
3004   if (frame_is_intra_only(cm)) {
3005     av1_set_screen_content_options(cpi, features);
3006   }
3007 
3008   // frame type has been decided outside of this function call
3009   cm->cur_frame->frame_type = current_frame->frame_type;
3010 
3011   cm->tiles.large_scale = tile_cfg->enable_large_scale_tile;
3012   cm->tiles.single_tile_decoding = tile_cfg->enable_single_tile_decoding;
3013 
3014   features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
3015   // features->allow_ref_frame_mvs needs to be written into the frame header
3016   // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case
3017   // is separated from frame_might_allow_ref_frame_mvs().
3018   features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
3019 
3020   features->allow_warped_motion = oxcf->motion_mode_cfg.allow_warped_motion &&
3021                                   frame_might_allow_warped_motion(cm);
3022 
3023   cpi->last_frame_type = current_frame->frame_type;
3024 
3025   if (frame_is_sframe(cm)) {
3026     GF_GROUP *gf_group = &cpi->gf_group;
3027     // S frame will wipe out any previously encoded altref so we cannot place
3028     // an overlay frame
3029     gf_group->update_type[gf_group->size] = GF_UPDATE;
3030   }
3031 
3032   if (encode_show_existing_frame(cm)) {
3033     av1_finalize_encoded_frame(cpi);
3034     // Build the bitstream
3035     int largest_tile_id = 0;  // Output from bitstream: unused here
3036     cpi->rc.coefficient_size = 0;
3037     if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
3038       return AOM_CODEC_ERROR;
3039 
3040     if (seq_params->frame_id_numbers_present_flag &&
3041         current_frame->frame_type == KEY_FRAME) {
3042       // Displaying a forward key-frame, so reset the ref buffer IDs
3043       int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
3044       for (int i = 0; i < REF_FRAMES; i++)
3045         cm->ref_frame_id[i] = display_frame_id;
3046     }
3047 
3048     cpi->seq_params_locked = 1;
3049 
3050 #if DUMP_RECON_FRAMES == 1
3051     // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
3052     av1_dump_filtered_recon_frames(cpi);
3053 #endif  // DUMP_RECON_FRAMES
3054 
3055     // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3056     //       for the purpose to verify no mismatch between encoder and decoder.
3057     if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
3058 
3059 #if CONFIG_AV1_TEMPORAL_DENOISING
3060     av1_denoiser_update_ref_frame(cpi);
3061 #endif
3062 
3063     refresh_reference_frames(cpi);
3064 
3065     // Since we allocate a spot for the OVERLAY frame in the gf group, we need
3066     // to do post-encoding update accordingly.
3067     av1_set_target_rate(cpi, cm->width, cm->height);
3068     av1_rc_postencode_update(cpi, *size);
3069 
3070     if (is_psnr_calc_enabled(cpi)) {
3071       cpi->source =
3072           realloc_and_scale_source(cpi, cm->cur_frame->buf.y_crop_width,
3073                                    cm->cur_frame->buf.y_crop_height);
3074     }
3075 
3076     ++current_frame->frame_number;
3077     update_frame_index_set(&cpi->frame_index_set, cm->show_frame);
3078     return AOM_CODEC_OK;
3079   }
3080 
3081   // Work out whether to force_integer_mv this frame
3082   if (!is_stat_generation_stage(cpi) &&
3083       cpi->common.features.allow_screen_content_tools &&
3084       !frame_is_intra_only(cm)) {
3085     if (cpi->common.seq_params.force_integer_mv == 2) {
3086       // Adaptive mode: see what previous frame encoded did
3087       if (cpi->unscaled_last_source != NULL) {
3088         features->cur_frame_force_integer_mv = av1_is_integer_mv(
3089             cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info);
3090       } else {
3091         cpi->common.features.cur_frame_force_integer_mv = 0;
3092       }
3093     } else {
3094       cpi->common.features.cur_frame_force_integer_mv =
3095           cpi->common.seq_params.force_integer_mv;
3096     }
3097   } else {
3098     cpi->common.features.cur_frame_force_integer_mv = 0;
3099   }
3100 
3101   // Set default state for segment based loop filter update flags.
3102   cm->lf.mode_ref_delta_update = 0;
3103 
3104   // Set various flags etc to special state if it is a key frame.
3105   if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
3106     // Reset the loop filter deltas and segmentation map.
3107     av1_reset_segment_features(cm);
3108 
3109     // If segmentation is enabled force a map update for key frames.
3110     if (seg->enabled) {
3111       seg->update_map = 1;
3112       seg->update_data = 1;
3113     }
3114   }
3115   if (tile_cfg->mtu == 0) {
3116     cpi->num_tg = tile_cfg->num_tile_groups;
3117   } else {
3118     // Use a default value for the purposes of weighting costs in probability
3119     // updates
3120     cpi->num_tg = DEFAULT_MAX_NUM_TG;
3121   }
3122 
3123   // For 1 pass CBR, check if we are dropping this frame.
3124   // Never drop on key frame.
3125   if (has_no_stats_stage(cpi) && oxcf->rc_cfg.mode == AOM_CBR &&
3126       current_frame->frame_type != KEY_FRAME) {
3127     if (av1_rc_drop_frame(cpi)) {
3128       av1_setup_frame_size(cpi);
3129       av1_rc_postencode_update_drop_frame(cpi);
3130       release_scaled_references(cpi);
3131       return AOM_CODEC_OK;
3132     }
3133   }
3134 
3135   if (oxcf->tune_cfg.tuning == AOM_TUNE_SSIM)
3136     av1_set_mb_ssim_rdmult_scaling(cpi);
3137 
3138 #if CONFIG_TUNE_VMAF
3139   if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
3140       oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN ||
3141       oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
3142     av1_set_mb_vmaf_rdmult_scaling(cpi);
3143   }
3144 #endif
3145 
3146   aom_clear_system_state();
3147 
3148 #if CONFIG_INTERNAL_STATS
3149   memset(cpi->mode_chosen_counts, 0,
3150          MAX_MODES * sizeof(*cpi->mode_chosen_counts));
3151 #endif
3152 
3153   if (seq_params->frame_id_numbers_present_flag) {
3154     /* Non-normative definition of current_frame_id ("frame counter" with
3155      * wraparound) */
3156     if (cm->current_frame_id == -1) {
3157       int lsb, msb;
3158       /* quasi-random initialization of current_frame_id for a key frame */
3159       if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
3160         lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
3161         msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
3162       } else {
3163         lsb = cpi->source->y_buffer[0] & 0xff;
3164         msb = cpi->source->y_buffer[1] & 0xff;
3165       }
3166       cm->current_frame_id =
3167           ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
3168 
3169       // S_frame is meant for stitching different streams of different
3170       // resolutions together, so current_frame_id must be the
3171       // same across different streams of the same content current_frame_id
3172       // should be the same and not random. 0x37 is a chosen number as start
3173       // point
3174       if (oxcf->kf_cfg.sframe_dist != 0) cm->current_frame_id = 0x37;
3175     } else {
3176       cm->current_frame_id =
3177           (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
3178           (1 << seq_params->frame_id_length);
3179     }
3180   }
3181 
3182   switch (oxcf->algo_cfg.cdf_update_mode) {
3183     case 0:  // No CDF update for any frames(4~6% compression loss).
3184       features->disable_cdf_update = 1;
3185       break;
3186     case 1:  // Enable CDF update for all frames.
3187       features->disable_cdf_update = 0;
3188       break;
3189     case 2:
3190       // Strategically determine at which frames to do CDF update.
3191       // Currently only enable CDF update for all-intra and no-show frames(1.5%
3192       // compression loss).
3193       // TODO(huisu@google.com): design schemes for various trade-offs between
3194       // compression quality and decoding speed.
3195       if (oxcf->mode == GOOD) {
3196         features->disable_cdf_update =
3197             (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
3198       } else {
3199         if (cpi->svc.number_spatial_layers == 1 &&
3200             cpi->svc.number_temporal_layers == 1)
3201           features->disable_cdf_update = cm->current_frame.frame_number & 1;
3202         else if (cpi->svc.number_temporal_layers > 1)
3203           // Disable only on top temporal enhancement layer for now.
3204           features->disable_cdf_update = (cpi->svc.temporal_layer_id ==
3205                                           cpi->svc.number_temporal_layers - 1);
3206       }
3207       break;
3208   }
3209   seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr;
3210 
3211   int largest_tile_id = 0;
3212   if (av1_superres_in_recode_allowed(cpi)) {
3213     if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
3214         AOM_CODEC_OK) {
3215       return AOM_CODEC_ERROR;
3216     }
3217   } else {
3218     const aom_superres_mode orig_superres_mode = cpi->superres_mode;  // save
3219     cpi->superres_mode = cpi->oxcf.superres_cfg.superres_mode;
3220     if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
3221                                            &largest_tile_id) != AOM_CODEC_OK) {
3222       return AOM_CODEC_ERROR;
3223     }
3224     cpi->superres_mode = orig_superres_mode;  // restore
3225   }
3226 
3227   cpi->seq_params_locked = 1;
3228 
3229   // Update reference frame ids for reference frames this frame will overwrite
3230   if (seq_params->frame_id_numbers_present_flag) {
3231     for (int i = 0; i < REF_FRAMES; i++) {
3232       if ((current_frame->refresh_frame_flags >> i) & 1) {
3233         cm->ref_frame_id[i] = cm->current_frame_id;
3234       }
3235     }
3236   }
3237 
3238   if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
3239     cpi->svc.num_encoded_top_layer++;
3240 
3241 #if DUMP_RECON_FRAMES == 1
3242   // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
3243   av1_dump_filtered_recon_frames(cpi);
3244 #endif  // DUMP_RECON_FRAMES
3245 
3246   if (cm->seg.enabled) {
3247     if (cm->seg.update_map) {
3248       update_reference_segmentation_map(cpi);
3249     } else if (cm->last_frame_seg_map) {
3250       memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
3251              cm->cur_frame->mi_cols * cm->cur_frame->mi_rows *
3252                  sizeof(*cm->cur_frame->seg_map));
3253     }
3254   }
3255 
3256   if (frame_is_intra_only(cm) == 0) {
3257     release_scaled_references(cpi);
3258   }
3259 #if CONFIG_AV1_TEMPORAL_DENOISING
3260   av1_denoiser_update_ref_frame(cpi);
3261 #endif
3262 
3263   // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3264   //       for the purpose to verify no mismatch between encoder and decoder.
3265   if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
3266 
3267   refresh_reference_frames(cpi);
3268 
3269 #if CONFIG_ENTROPY_STATS
3270   av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
3271 #endif  // CONFIG_ENTROPY_STATS
3272 
3273   if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3274     *cm->fc = cpi->tile_data[largest_tile_id].tctx;
3275     av1_reset_cdf_symbol_counters(cm->fc);
3276   }
3277   if (!cm->tiles.large_scale) {
3278     cm->cur_frame->frame_context = *cm->fc;
3279   }
3280 
3281   if (tile_cfg->enable_ext_tile_debug) {
3282     // (yunqing) This test ensures the correctness of large scale tile coding.
3283     if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) {
3284       char fn[20] = "./fc";
3285       fn[4] = current_frame->frame_number / 100 + '0';
3286       fn[5] = (current_frame->frame_number % 100) / 10 + '0';
3287       fn[6] = (current_frame->frame_number % 10) + '0';
3288       fn[7] = '\0';
3289       av1_print_frame_contexts(cm->fc, fn);
3290     }
3291   }
3292 
3293   cpi->last_frame_type = current_frame->frame_type;
3294 
3295   av1_rc_postencode_update(cpi, *size);
3296 
3297   // Clear the one shot update flags for segmentation map and mode/ref loop
3298   // filter deltas.
3299   cm->seg.update_map = 0;
3300   cm->seg.update_data = 0;
3301   cm->lf.mode_ref_delta_update = 0;
3302 
3303   // A droppable frame might not be shown but it always
3304   // takes a space in the gf group. Therefore, even when
3305   // it is not shown, we still need update the count down.
3306   if (cm->show_frame) {
3307     update_frame_index_set(&cpi->frame_index_set, cm->show_frame);
3308     ++current_frame->frame_number;
3309   }
3310 
3311 #if CONFIG_COLLECT_COMPONENT_TIMING
3312   end_timing(cpi, encode_frame_to_data_rate_time);
3313 #endif
3314 
3315   return AOM_CODEC_OK;
3316 }
3317 
av1_encode(AV1_COMP * const cpi,uint8_t * const dest,const EncodeFrameInput * const frame_input,const EncodeFrameParams * const frame_params,EncodeFrameResults * const frame_results)3318 int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
3319                const EncodeFrameInput *const frame_input,
3320                const EncodeFrameParams *const frame_params,
3321                EncodeFrameResults *const frame_results) {
3322   AV1_COMMON *const cm = &cpi->common;
3323   CurrentFrame *const current_frame = &cm->current_frame;
3324 
3325   cpi->unscaled_source = frame_input->source;
3326   cpi->source = frame_input->source;
3327   cpi->unscaled_last_source = frame_input->last_source;
3328 
3329   current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
3330   cm->features.error_resilient_mode = frame_params->error_resilient_mode;
3331   cm->features.primary_ref_frame = frame_params->primary_ref_frame;
3332   cm->current_frame.frame_type = frame_params->frame_type;
3333   cm->show_frame = frame_params->show_frame;
3334   cpi->ref_frame_flags = frame_params->ref_frame_flags;
3335   cpi->speed = frame_params->speed;
3336   cm->show_existing_frame = frame_params->show_existing_frame;
3337   cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
3338 
3339   memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
3340          REF_FRAMES * sizeof(*cm->remapped_ref_idx));
3341 
3342   memcpy(&cpi->refresh_frame, &frame_params->refresh_frame,
3343          sizeof(cpi->refresh_frame));
3344 
3345   if (current_frame->frame_type == KEY_FRAME && !cpi->no_show_fwd_kf) {
3346     current_frame->frame_number = 0;
3347   }
3348 
3349   current_frame->order_hint =
3350       current_frame->frame_number + frame_params->order_offset;
3351 
3352   current_frame->display_order_hint = current_frame->order_hint;
3353   current_frame->order_hint %=
3354       (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
3355 
3356   if (is_stat_generation_stage(cpi)) {
3357 #if !CONFIG_REALTIME_ONLY
3358     av1_first_pass(cpi, frame_input->ts_duration);
3359 #endif
3360   } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
3361     if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
3362         AOM_CODEC_OK) {
3363       return AOM_CODEC_ERROR;
3364     }
3365   } else {
3366     return AOM_CODEC_ERROR;
3367   }
3368 
3369   return AOM_CODEC_OK;
3370 }
3371 
3372 #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)3373 static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
3374                             int block_size, float noise_level,
3375                             int64_t time_stamp, int64_t end_time) {
3376   AV1_COMMON *const cm = &cpi->common;
3377   if (!cpi->denoise_and_model) {
3378     cpi->denoise_and_model = aom_denoise_and_model_alloc(
3379         cm->seq_params.bit_depth, block_size, noise_level);
3380     if (!cpi->denoise_and_model) {
3381       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3382                          "Error allocating denoise and model");
3383       return -1;
3384     }
3385   }
3386   if (!cpi->film_grain_table) {
3387     cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
3388     if (!cpi->film_grain_table) {
3389       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3390                          "Error allocating grain table");
3391       return -1;
3392     }
3393     memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
3394   }
3395   if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
3396                                 &cm->film_grain_params,
3397                                 cpi->oxcf.enable_dnl_denoising)) {
3398     if (cm->film_grain_params.apply_grain) {
3399       aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
3400                                   &cm->film_grain_params);
3401     }
3402   }
3403   return 0;
3404 }
3405 #endif
3406 
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)3407 int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
3408                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3409                           int64_t end_time) {
3410   AV1_COMMON *const cm = &cpi->common;
3411   const SequenceHeader *const seq_params = &cm->seq_params;
3412   int res = 0;
3413   const int subsampling_x = sd->subsampling_x;
3414   const int subsampling_y = sd->subsampling_y;
3415   const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
3416 
3417 #if CONFIG_TUNE_VMAF
3418   if (!is_stat_generation_stage(cpi) &&
3419       cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
3420     av1_vmaf_frame_preprocessing(cpi, sd);
3421   }
3422   if (!is_stat_generation_stage(cpi) &&
3423       cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
3424     av1_vmaf_blk_preprocessing(cpi, sd);
3425   }
3426 #endif
3427 
3428 #if CONFIG_INTERNAL_STATS
3429   struct aom_usec_timer timer;
3430   aom_usec_timer_start(&timer);
3431 #endif
3432 
3433 #if CONFIG_AV1_TEMPORAL_DENOISING
3434   setup_denoiser_buffer(cpi);
3435 #endif
3436 
3437 #if CONFIG_DENOISE
3438   // even if denoise_noise_level is > 0, we don't need need to denoise on pass
3439   // 1 of 2 if enable_dnl_denoising is disabled since the 2nd pass will be
3440   // encoding the original (non-denoised) frame
3441   if (cpi->oxcf.noise_level > 0 &&
3442       !(cpi->oxcf.pass == AOM_RC_FIRST_PASS && !cpi->oxcf.enable_dnl_denoising))
3443     if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
3444                          cpi->oxcf.noise_level, time_stamp, end_time) < 0)
3445       res = -1;
3446 #endif  //  CONFIG_DENOISE
3447 
3448   if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
3449                          use_highbitdepth, frame_flags))
3450     res = -1;
3451 #if CONFIG_INTERNAL_STATS
3452   aom_usec_timer_mark(&timer);
3453   cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
3454 #endif
3455 
3456   // Note: Regarding profile setting, the following checks are added to help
3457   // choose a proper profile for the input video. The criterion is that all
3458   // bitstreams must be designated as the lowest profile that match its content.
3459   // E.G. A bitstream that contains 4:4:4 video must be designated as High
3460   // Profile in the seq header, and likewise a bitstream that contains 4:2:2
3461   // bitstream must be designated as Professional Profile in the sequence
3462   // header.
3463   if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
3464       (subsampling_x != 1 || subsampling_y != 1)) {
3465     aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
3466                        "Non-4:2:0 color format requires profile 1 or 2");
3467     res = -1;
3468   }
3469   if ((seq_params->profile == PROFILE_1) &&
3470       !(subsampling_x == 0 && subsampling_y == 0)) {
3471     aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
3472                        "Profile 1 requires 4:4:4 color format");
3473     res = -1;
3474   }
3475   if ((seq_params->profile == PROFILE_2) &&
3476       (seq_params->bit_depth <= AOM_BITS_10) &&
3477       !(subsampling_x == 1 && subsampling_y == 0)) {
3478     aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
3479                        "Profile 2 bit-depth <= 10 requires 4:2:2 color format");
3480     res = -1;
3481   }
3482 
3483   return res;
3484 }
3485 
3486 #if CONFIG_INTERNAL_STATS
3487 extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
3488                                  const unsigned char *img2, int img2_pitch,
3489                                  int width, int height);
3490 
adjust_image_stat(double y,double u,double v,double all,ImageStat * s)3491 static void adjust_image_stat(double y, double u, double v, double all,
3492                               ImageStat *s) {
3493   s->stat[STAT_Y] += y;
3494   s->stat[STAT_U] += u;
3495   s->stat[STAT_V] += v;
3496   s->stat[STAT_ALL] += all;
3497   s->worst = AOMMIN(s->worst, all);
3498 }
3499 
compute_internal_stats(AV1_COMP * cpi,int frame_bytes)3500 static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
3501   AV1_COMMON *const cm = &cpi->common;
3502   double samples = 0.0;
3503   const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
3504   const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
3505 
3506 #if CONFIG_INTER_STATS_ONLY
3507   if (cm->current_frame.frame_type == KEY_FRAME) return;  // skip key frame
3508 #endif
3509   cpi->bytes += frame_bytes;
3510   if (cm->show_frame) {
3511     const YV12_BUFFER_CONFIG *orig = cpi->source;
3512     const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
3513     double y, u, v, frame_all;
3514 
3515     cpi->count[0]++;
3516     cpi->count[1]++;
3517     if (cpi->b_calculate_psnr) {
3518       PSNR_STATS psnr;
3519       double weight[2] = { 0.0, 0.0 };
3520       double frame_ssim2[2] = { 0.0, 0.0 };
3521       aom_clear_system_state();
3522 #if CONFIG_AV1_HIGHBITDEPTH
3523       aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
3524 #else
3525       aom_calc_psnr(orig, recon, &psnr);
3526 #endif
3527       adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
3528                         &(cpi->psnr[0]));
3529       cpi->total_sq_error[0] += psnr.sse[0];
3530       cpi->total_samples[0] += psnr.samples[0];
3531       samples = psnr.samples[0];
3532 
3533       // TODO(yaowu): unify these two versions into one.
3534       if (cm->seq_params.use_highbitdepth)
3535         aom_highbd_calc_ssim(orig, recon, weight, bit_depth, in_bit_depth,
3536                              frame_ssim2);
3537       else
3538         aom_calc_ssim(orig, recon, &weight[0], &frame_ssim2[0]);
3539 
3540       cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2[0]);
3541       cpi->summed_quality += frame_ssim2[0] * weight[0];
3542       cpi->summed_weights += weight[0];
3543 
3544 #if CONFIG_AV1_HIGHBITDEPTH
3545       // Compute PSNR based on stream bit depth
3546       if ((cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) &&
3547           (in_bit_depth < bit_depth)) {
3548         adjust_image_stat(psnr.psnr_hbd[1], psnr.psnr_hbd[2], psnr.psnr_hbd[3],
3549                           psnr.psnr_hbd[0], &cpi->psnr[1]);
3550         cpi->total_sq_error[1] += psnr.sse_hbd[0];
3551         cpi->total_samples[1] += psnr.samples_hbd[0];
3552 
3553         cpi->worst_ssim_hbd = AOMMIN(cpi->worst_ssim_hbd, frame_ssim2[1]);
3554         cpi->summed_quality_hbd += frame_ssim2[1] * weight[1];
3555         cpi->summed_weights_hbd += weight[1];
3556       }
3557 #endif
3558 
3559 #if 0
3560       {
3561         FILE *f = fopen("q_used.stt", "a");
3562         double y2 = psnr.psnr[1];
3563         double u2 = psnr.psnr[2];
3564         double v2 = psnr.psnr[3];
3565         double frame_psnr2 = psnr.psnr[0];
3566         fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3567                 cm->current_frame.frame_number, y2, u2, v2,
3568                 frame_psnr2, frame_ssim2);
3569         fclose(f);
3570       }
3571 #endif
3572     }
3573     if (cpi->b_calculate_blockiness) {
3574       if (!cm->seq_params.use_highbitdepth) {
3575         const double frame_blockiness =
3576             av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
3577                                recon->y_stride, orig->y_width, orig->y_height);
3578         cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
3579         cpi->total_blockiness += frame_blockiness;
3580       }
3581 
3582       if (cpi->b_calculate_consistency) {
3583         if (!cm->seq_params.use_highbitdepth) {
3584           const double this_inconsistency = aom_get_ssim_metrics(
3585               orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
3586               orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
3587 
3588           const double peak = (double)((1 << in_bit_depth) - 1);
3589           const double consistency =
3590               aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
3591           if (consistency > 0.0)
3592             cpi->worst_consistency =
3593                 AOMMIN(cpi->worst_consistency, consistency);
3594           cpi->total_inconsistency += this_inconsistency;
3595         }
3596       }
3597     }
3598 
3599     frame_all =
3600         aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
3601     adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
3602     frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
3603     adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
3604   }
3605 }
3606 #endif  // CONFIG_INTERNAL_STATS
3607 
av1_get_compressed_data(AV1_COMP * cpi,unsigned int * frame_flags,size_t * size,uint8_t * dest,int64_t * time_stamp,int64_t * time_end,int flush,const aom_rational64_t * timestamp_ratio)3608 int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
3609                             size_t *size, uint8_t *dest, int64_t *time_stamp,
3610                             int64_t *time_end, int flush,
3611                             const aom_rational64_t *timestamp_ratio) {
3612   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3613   AV1_COMMON *const cm = &cpi->common;
3614 
3615 #if CONFIG_BITSTREAM_DEBUG
3616   assert(cpi->oxcf.max_threads <= 1 &&
3617          "bitstream debug tool does not support multithreading");
3618   bitstream_queue_record_write();
3619   aom_bitstream_queue_set_frame_write(cm->current_frame.order_hint * 2 +
3620                                       cm->show_frame);
3621 #endif
3622   if (cpi->use_svc && cm->number_spatial_layers > 1) {
3623     av1_one_pass_cbr_svc_start_layer(cpi);
3624   }
3625 
3626   cm->showable_frame = 0;
3627   *size = 0;
3628 #if CONFIG_INTERNAL_STATS
3629   struct aom_usec_timer cmptimer;
3630   aom_usec_timer_start(&cmptimer);
3631 #endif
3632   av1_set_high_precision_mv(cpi, 1, 0);
3633 
3634   // Normal defaults
3635   cm->features.refresh_frame_context =
3636       oxcf->tool_cfg.frame_parallel_decoding_mode
3637           ? REFRESH_FRAME_CONTEXT_DISABLED
3638           : REFRESH_FRAME_CONTEXT_BACKWARD;
3639   if (oxcf->tile_cfg.enable_large_scale_tile)
3640     cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
3641 
3642   // Initialize fields related to forward keyframes
3643   cpi->no_show_fwd_kf = 0;
3644 
3645   if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
3646 
3647 #if CONFIG_COLLECT_COMPONENT_TIMING
3648   // Only accumulate 2nd pass time.
3649   if (cpi->oxcf.pass == 2) start_timing(cpi, av1_encode_strategy_time);
3650 #endif
3651 
3652   const int result =
3653       av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
3654                           timestamp_ratio, flush);
3655 
3656 #if CONFIG_COLLECT_COMPONENT_TIMING
3657   if (cpi->oxcf.pass == 2) end_timing(cpi, av1_encode_strategy_time);
3658 
3659   // Print out timing information.
3660   // Note: Use "cpi->frame_component_time[0] > 100 us" to avoid showing of
3661   // show_existing_frame and lag-in-frames.
3662   if (cpi->oxcf.pass == 2 && cpi->frame_component_time[0] > 100) {
3663     int i;
3664     uint64_t frame_total = 0, total = 0;
3665 
3666     fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
3667             cm->current_frame.frame_number,
3668             get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
3669     for (i = 0; i < kTimingComponents; i++) {
3670       cpi->component_time[i] += cpi->frame_component_time[i];
3671       // Use av1_encode_strategy_time (i = 0) as the total time.
3672       if (i == 0) {
3673         frame_total = cpi->frame_component_time[0];
3674         total = cpi->component_time[0];
3675       }
3676       fprintf(stderr,
3677               " %50s:  %15" PRId64 " us [%6.2f%%] (total: %15" PRId64
3678               " us [%6.2f%%])\n",
3679               get_component_name(i), cpi->frame_component_time[i],
3680               (float)((float)cpi->frame_component_time[i] * 100.0 /
3681                       (float)frame_total),
3682               cpi->component_time[i],
3683               (float)((float)cpi->component_time[i] * 100.0 / (float)total));
3684       cpi->frame_component_time[i] = 0;
3685     }
3686   }
3687 #endif
3688 
3689   if (result == -1) {
3690     // Returning -1 indicates no frame encoded; more input is required
3691     return -1;
3692   }
3693   if (result != AOM_CODEC_OK) {
3694     return AOM_CODEC_ERROR;
3695   }
3696 #if CONFIG_INTERNAL_STATS
3697   aom_usec_timer_mark(&cmptimer);
3698   cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
3699 #endif  // CONFIG_INTERNAL_STATS
3700   // Note *size = 0 indicates a dropped frame for which psnr is not calculated
3701   if (cpi->b_calculate_psnr && *size > 0) {
3702     if (cm->show_existing_frame ||
3703         (!is_stat_generation_stage(cpi) && cm->show_frame)) {
3704       generate_psnr_packet(cpi);
3705     }
3706   }
3707 
3708   if (cpi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) {
3709     // Initialize level info. at the beginning of each sequence.
3710     if (cm->current_frame.frame_type == KEY_FRAME && !cpi->no_show_fwd_kf) {
3711       av1_init_level_info(cpi);
3712     }
3713     av1_update_level_info(cpi, *size, *time_stamp, *time_end);
3714   }
3715 
3716 #if CONFIG_INTERNAL_STATS
3717   if (!is_stat_generation_stage(cpi)) {
3718     compute_internal_stats(cpi, (int)(*size));
3719   }
3720 #endif  // CONFIG_INTERNAL_STATS
3721 #if CONFIG_SPEED_STATS
3722   if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
3723     cpi->tx_search_count += cpi->td.mb.txfm_search_info.tx_search_count;
3724     cpi->td.mb.txfm_search_info.tx_search_count = 0;
3725   }
3726 #endif  // CONFIG_SPEED_STATS
3727 
3728   aom_clear_system_state();
3729 
3730   return AOM_CODEC_OK;
3731 }
3732 
av1_get_preview_raw_frame(AV1_COMP * cpi,YV12_BUFFER_CONFIG * dest)3733 int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
3734   AV1_COMMON *cm = &cpi->common;
3735   if (!cm->show_frame) {
3736     return -1;
3737   } else {
3738     int ret;
3739     if (cm->cur_frame != NULL) {
3740       *dest = cm->cur_frame->buf;
3741       dest->y_width = cm->width;
3742       dest->y_height = cm->height;
3743       dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
3744       dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
3745       ret = 0;
3746     } else {
3747       ret = -1;
3748     }
3749     aom_clear_system_state();
3750     return ret;
3751   }
3752 }
3753 
av1_get_last_show_frame(AV1_COMP * cpi,YV12_BUFFER_CONFIG * frame)3754 int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
3755   if (cpi->last_show_frame_buf == NULL) return -1;
3756 
3757   *frame = cpi->last_show_frame_buf->buf;
3758   return 0;
3759 }
3760 
av1_copy_new_frame_enc(AV1_COMMON * cm,YV12_BUFFER_CONFIG * new_frame,YV12_BUFFER_CONFIG * sd)3761 aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
3762                                        YV12_BUFFER_CONFIG *new_frame,
3763                                        YV12_BUFFER_CONFIG *sd) {
3764   const int num_planes = av1_num_planes(cm);
3765   if (!equal_dimensions_and_border(new_frame, sd))
3766     aom_internal_error(&cm->error, AOM_CODEC_ERROR,
3767                        "Incorrect buffer dimensions");
3768   else
3769     aom_yv12_copy_frame(new_frame, sd, num_planes);
3770 
3771   return cm->error.error_code;
3772 }
3773 
av1_set_internal_size(AV1EncoderConfig * const oxcf,ResizePendingParams * resize_pending_params,AOM_SCALING horiz_mode,AOM_SCALING vert_mode)3774 int av1_set_internal_size(AV1EncoderConfig *const oxcf,
3775                           ResizePendingParams *resize_pending_params,
3776                           AOM_SCALING horiz_mode, AOM_SCALING vert_mode) {
3777   int hr = 0, hs = 0, vr = 0, vs = 0;
3778 
3779   if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
3780 
3781   Scale2Ratio(horiz_mode, &hr, &hs);
3782   Scale2Ratio(vert_mode, &vr, &vs);
3783 
3784   // always go to the next whole number
3785   resize_pending_params->width = (hs - 1 + oxcf->frm_dim_cfg.width * hr) / hs;
3786   resize_pending_params->height = (vs - 1 + oxcf->frm_dim_cfg.height * vr) / vs;
3787 
3788   if (horiz_mode != NORMAL || vert_mode != NORMAL) {
3789     oxcf->resize_cfg.resize_mode = RESIZE_FIXED;
3790     oxcf->algo_cfg.enable_tpl_model = 0;
3791   }
3792   return 0;
3793 }
3794 
av1_get_quantizer(AV1_COMP * cpi)3795 int av1_get_quantizer(AV1_COMP *cpi) {
3796   return cpi->common.quant_params.base_qindex;
3797 }
3798 
av1_convert_sect5obus_to_annexb(uint8_t * buffer,size_t * frame_size)3799 int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
3800   size_t output_size = 0;
3801   size_t total_bytes_read = 0;
3802   size_t remaining_size = *frame_size;
3803   uint8_t *buff_ptr = buffer;
3804 
3805   // go through each OBUs
3806   while (total_bytes_read < *frame_size) {
3807     uint8_t saved_obu_header[2];
3808     uint64_t obu_payload_size;
3809     size_t length_of_payload_size;
3810     size_t length_of_obu_size;
3811     uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
3812     size_t obu_bytes_read = obu_header_size;  // bytes read for current obu
3813 
3814     // save the obu header (1 or 2 bytes)
3815     memmove(saved_obu_header, buff_ptr, obu_header_size);
3816     // clear the obu_has_size_field
3817     saved_obu_header[0] = saved_obu_header[0] & (~0x2);
3818 
3819     // get the payload_size and length of payload_size
3820     if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
3821                         &obu_payload_size, &length_of_payload_size) != 0) {
3822       return AOM_CODEC_ERROR;
3823     }
3824     obu_bytes_read += length_of_payload_size;
3825 
3826     // calculate the length of size of the obu header plus payload
3827     length_of_obu_size =
3828         aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
3829 
3830     // move the rest of data to new location
3831     memmove(buff_ptr + length_of_obu_size + obu_header_size,
3832             buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
3833     obu_bytes_read += (size_t)obu_payload_size;
3834 
3835     // write the new obu size
3836     const uint64_t obu_size = obu_header_size + obu_payload_size;
3837     size_t coded_obu_size;
3838     if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
3839                         &coded_obu_size) != 0) {
3840       return AOM_CODEC_ERROR;
3841     }
3842 
3843     // write the saved (modified) obu_header following obu size
3844     memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
3845 
3846     total_bytes_read += obu_bytes_read;
3847     remaining_size -= obu_bytes_read;
3848     buff_ptr += length_of_obu_size + obu_size;
3849     output_size += length_of_obu_size + (size_t)obu_size;
3850   }
3851 
3852   *frame_size = output_size;
3853   return AOM_CODEC_OK;
3854 }
3855 
svc_set_updates_external_ref_frame_config(ExtRefreshFrameFlagsInfo * const ext_refresh_frame_flags,SVC * const svc)3856 static void svc_set_updates_external_ref_frame_config(
3857     ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags, SVC *const svc) {
3858   ext_refresh_frame_flags->update_pending = 1;
3859   ext_refresh_frame_flags->last_frame = svc->refresh[svc->ref_idx[0]];
3860   ext_refresh_frame_flags->golden_frame = svc->refresh[svc->ref_idx[3]];
3861   ext_refresh_frame_flags->bwd_ref_frame = svc->refresh[svc->ref_idx[4]];
3862   ext_refresh_frame_flags->alt2_ref_frame = svc->refresh[svc->ref_idx[5]];
3863   ext_refresh_frame_flags->alt_ref_frame = svc->refresh[svc->ref_idx[6]];
3864   svc->non_reference_frame = 1;
3865   for (int i = 0; i < REF_FRAMES; i++) {
3866     if (svc->refresh[i] == 1) {
3867       svc->non_reference_frame = 0;
3868       break;
3869     }
3870   }
3871 }
3872 
svc_set_references_external_ref_frame_config(AV1_COMP * cpi)3873 static int svc_set_references_external_ref_frame_config(AV1_COMP *cpi) {
3874   // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
3875   // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
3876   int ref = AOM_REFFRAME_ALL;
3877   for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
3878     if (!cpi->svc.reference[i]) ref ^= (1 << i);
3879   }
3880   return ref;
3881 }
3882 
av1_apply_encoding_flags(AV1_COMP * cpi,aom_enc_frame_flags_t flags)3883 void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
3884   // TODO(yunqingwang): For what references to use, external encoding flags
3885   // should be consistent with internal reference frame selection. Need to
3886   // ensure that there is not conflict between the two. In AV1 encoder, the
3887   // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
3888   // GOLDEN, BWDREF, ALTREF2.
3889 
3890   ExternalFlags *const ext_flags = &cpi->ext_flags;
3891   ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
3892       &ext_flags->refresh_frame;
3893   ext_flags->ref_frame_flags = AOM_REFFRAME_ALL;
3894   if (flags &
3895       (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
3896        AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
3897        AOM_EFLAG_NO_REF_ARF2)) {
3898     int ref = AOM_REFFRAME_ALL;
3899 
3900     if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
3901     if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
3902     if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
3903 
3904     if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
3905 
3906     if (flags & AOM_EFLAG_NO_REF_ARF) {
3907       ref ^= AOM_ALT_FLAG;
3908       ref ^= AOM_BWD_FLAG;
3909       ref ^= AOM_ALT2_FLAG;
3910     } else {
3911       if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
3912       if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
3913     }
3914 
3915     av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
3916   } else {
3917     if (cpi->svc.external_ref_frame_config) {
3918       int ref = svc_set_references_external_ref_frame_config(cpi);
3919       av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
3920     }
3921   }
3922 
3923   if (flags &
3924       (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
3925     int upd = AOM_REFFRAME_ALL;
3926 
3927     // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
3928     if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
3929 
3930     if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
3931 
3932     if (flags & AOM_EFLAG_NO_UPD_ARF) {
3933       upd ^= AOM_ALT_FLAG;
3934       upd ^= AOM_BWD_FLAG;
3935       upd ^= AOM_ALT2_FLAG;
3936     }
3937 
3938     ext_refresh_frame_flags->last_frame = (upd & AOM_LAST_FLAG) != 0;
3939     ext_refresh_frame_flags->golden_frame = (upd & AOM_GOLD_FLAG) != 0;
3940     ext_refresh_frame_flags->alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
3941     ext_refresh_frame_flags->bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
3942     ext_refresh_frame_flags->alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
3943     ext_refresh_frame_flags->update_pending = 1;
3944   } else {
3945     if (cpi->svc.external_ref_frame_config)
3946       svc_set_updates_external_ref_frame_config(ext_refresh_frame_flags,
3947                                                 &cpi->svc);
3948     else
3949       ext_refresh_frame_flags->update_pending = 0;
3950   }
3951 
3952   ext_flags->use_ref_frame_mvs = cpi->oxcf.tool_cfg.enable_ref_frame_mvs &
3953                                  ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
3954   ext_flags->use_error_resilient = cpi->oxcf.tool_cfg.error_resilient_mode |
3955                                    ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
3956   ext_flags->use_s_frame =
3957       cpi->oxcf.kf_cfg.enable_sframe | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
3958   ext_flags->use_primary_ref_none =
3959       (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
3960 
3961   if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
3962     update_entropy(&ext_flags->refresh_frame_context,
3963                    &ext_flags->refresh_frame_context_pending, 0);
3964   }
3965 }
3966 
av1_get_global_headers(AV1_COMP * cpi)3967 aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
3968   if (!cpi) return NULL;
3969 
3970   uint8_t header_buf[512] = { 0 };
3971   const uint32_t sequence_header_size =
3972       av1_write_sequence_header_obu(&cpi->common.seq_params, &header_buf[0]);
3973   assert(sequence_header_size <= sizeof(header_buf));
3974   if (sequence_header_size == 0) return NULL;
3975 
3976   const size_t obu_header_size = 1;
3977   const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
3978   const size_t payload_offset = obu_header_size + size_field_size;
3979 
3980   if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
3981   memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
3982 
3983   if (av1_write_obu_header(&cpi->level_params, OBU_SEQUENCE_HEADER, 0,
3984                            &header_buf[0]) != obu_header_size) {
3985     return NULL;
3986   }
3987 
3988   size_t coded_size_field_size = 0;
3989   if (aom_uleb_encode(sequence_header_size, size_field_size,
3990                       &header_buf[obu_header_size],
3991                       &coded_size_field_size) != 0) {
3992     return NULL;
3993   }
3994   assert(coded_size_field_size == size_field_size);
3995 
3996   aom_fixed_buf_t *global_headers =
3997       (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
3998   if (!global_headers) return NULL;
3999 
4000   const size_t global_header_buf_size =
4001       obu_header_size + size_field_size + sequence_header_size;
4002 
4003   global_headers->buf = malloc(global_header_buf_size);
4004   if (!global_headers->buf) {
4005     free(global_headers);
4006     return NULL;
4007   }
4008 
4009   memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
4010   global_headers->sz = global_header_buf_size;
4011   return global_headers;
4012 }
4013