tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

encoder.c (225737B)


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