Home | History | Annotate | Download | only in encoder
      1 /*
      2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
      3  *
      4  * This source code is subject to the terms of the BSD 2 Clause License and
      5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
      6  * was not distributed with this source code in the LICENSE file, you can
      7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
      8  * Media Patent License 1.0 was not distributed with this source code in the
      9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
     10  */
     11 
     12 #include <limits.h>
     13 #include <math.h>
     14 #include <stdio.h>
     15 
     16 #include "config/aom_config.h"
     17 #include "config/aom_dsp_rtcd.h"
     18 #include "config/aom_scale_rtcd.h"
     19 #include "config/av1_rtcd.h"
     20 
     21 #include "aom_dsp/aom_dsp_common.h"
     22 #include "aom_dsp/aom_filter.h"
     23 #if CONFIG_DENOISE
     24 #include "aom_dsp/grain_table.h"
     25 #include "aom_dsp/noise_util.h"
     26 #include "aom_dsp/noise_model.h"
     27 #endif
     28 #include "aom_dsp/psnr.h"
     29 #if CONFIG_INTERNAL_STATS
     30 #include "aom_dsp/ssim.h"
     31 #endif
     32 #include "aom_ports/aom_timer.h"
     33 #include "aom_ports/mem.h"
     34 #include "aom_ports/system_state.h"
     35 #include "aom_scale/aom_scale.h"
     36 #if CONFIG_BITSTREAM_DEBUG
     37 #include "aom_util/debug_util.h"
     38 #endif  // CONFIG_BITSTREAM_DEBUG
     39 
     40 #include "av1/common/alloccommon.h"
     41 #include "av1/common/cdef.h"
     42 #include "av1/common/filter.h"
     43 #include "av1/common/idct.h"
     44 #include "av1/common/reconinter.h"
     45 #include "av1/common/reconintra.h"
     46 #include "av1/common/resize.h"
     47 #include "av1/common/tile_common.h"
     48 
     49 #include "av1/encoder/av1_multi_thread.h"
     50 #include "av1/encoder/aq_complexity.h"
     51 #include "av1/encoder/aq_cyclicrefresh.h"
     52 #include "av1/encoder/aq_variance.h"
     53 #include "av1/encoder/bitstream.h"
     54 #include "av1/encoder/context_tree.h"
     55 #include "av1/encoder/encodeframe.h"
     56 #include "av1/encoder/encodemv.h"
     57 #include "av1/encoder/encode_strategy.h"
     58 #include "av1/encoder/encoder.h"
     59 #include "av1/encoder/encodetxb.h"
     60 #include "av1/encoder/ethread.h"
     61 #include "av1/encoder/firstpass.h"
     62 #include "av1/encoder/grain_test_vectors.h"
     63 #include "av1/encoder/hash_motion.h"
     64 #include "av1/encoder/mbgraph.h"
     65 #include "av1/encoder/pass2_strategy.h"
     66 #include "av1/encoder/picklpf.h"
     67 #include "av1/encoder/pickrst.h"
     68 #include "av1/encoder/random.h"
     69 #include "av1/encoder/ratectrl.h"
     70 #include "av1/encoder/rd.h"
     71 #include "av1/encoder/rdopt.h"
     72 #include "av1/encoder/segmentation.h"
     73 #include "av1/encoder/speed_features.h"
     74 #include "av1/encoder/reconinter_enc.h"
     75 #include "av1/encoder/var_based_part.h"
     76 
     77 #define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
     78 
     79 #if CONFIG_ENTROPY_STATS
     80 FRAME_COUNTS aggregate_fc;
     81 #endif  // CONFIG_ENTROPY_STATS
     82 
     83 #define AM_SEGMENT_ID_INACTIVE 7
     84 #define AM_SEGMENT_ID_ACTIVE 0
     85 
     86 // Whether to use high precision mv for altref computation.
     87 #define ALTREF_HIGH_PRECISION_MV 1
     88 
     89 // Q threshold for high precision mv. Choose a very high value for now so that
     90 // HIGH_PRECISION is always chosen.
     91 #define HIGH_PRECISION_MV_QTHRESH 200
     92 
     93 // #define OUTPUT_YUV_REC
     94 #ifdef OUTPUT_YUV_SKINMAP
     95 FILE *yuv_skinmap_file = NULL;
     96 #endif
     97 #ifdef OUTPUT_YUV_REC
     98 FILE *yuv_rec_file;
     99 #define FILE_NAME_LEN 100
    100 #endif
    101 
    102 static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
    103   switch (mode) {
    104     case NORMAL:
    105       *hr = 1;
    106       *hs = 1;
    107       break;
    108     case FOURFIVE:
    109       *hr = 4;
    110       *hs = 5;
    111       break;
    112     case THREEFIVE:
    113       *hr = 3;
    114       *hs = 5;
    115       break;
    116     case ONETWO:
    117       *hr = 1;
    118       *hs = 2;
    119       break;
    120     default:
    121       *hr = 1;
    122       *hs = 1;
    123       assert(0);
    124       break;
    125   }
    126 }
    127 
    128 // Mark all inactive blocks as active. Other segmentation features may be set
    129 // so memset cannot be used, instead only inactive blocks should be reset.
    130 static void suppress_active_map(AV1_COMP *cpi) {
    131   unsigned char *const seg_map = cpi->segmentation_map;
    132   int i;
    133   if (cpi->active_map.enabled || cpi->active_map.update)
    134     for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
    135       if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
    136         seg_map[i] = AM_SEGMENT_ID_ACTIVE;
    137 }
    138 
    139 static void apply_active_map(AV1_COMP *cpi) {
    140   struct segmentation *const seg = &cpi->common.seg;
    141   unsigned char *const seg_map = cpi->segmentation_map;
    142   const unsigned char *const active_map = cpi->active_map.map;
    143   int i;
    144 
    145   assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
    146 
    147   if (frame_is_intra_only(&cpi->common)) {
    148     cpi->active_map.enabled = 0;
    149     cpi->active_map.update = 1;
    150   }
    151 
    152   if (cpi->active_map.update) {
    153     if (cpi->active_map.enabled) {
    154       for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
    155         if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
    156       av1_enable_segmentation(seg);
    157       av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
    158       av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
    159       av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
    160       av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
    161       av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
    162 
    163       av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
    164                       -MAX_LOOP_FILTER);
    165       av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
    166                       -MAX_LOOP_FILTER);
    167       av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
    168                       -MAX_LOOP_FILTER);
    169       av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
    170                       -MAX_LOOP_FILTER);
    171     } else {
    172       av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
    173       av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
    174       av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
    175       av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
    176       av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
    177       if (seg->enabled) {
    178         seg->update_data = 1;
    179         seg->update_map = 1;
    180       }
    181     }
    182     cpi->active_map.update = 0;
    183   }
    184 }
    185 
    186 int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
    187                        int cols) {
    188   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
    189     unsigned char *const active_map_8x8 = cpi->active_map.map;
    190     const int mi_rows = cpi->common.mi_rows;
    191     const int mi_cols = cpi->common.mi_cols;
    192     const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
    193     const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
    194     cpi->active_map.update = 1;
    195     if (new_map_16x16) {
    196       int r, c;
    197       for (r = 0; r < mi_rows; ++r) {
    198         for (c = 0; c < mi_cols; ++c) {
    199           active_map_8x8[r * mi_cols + c] =
    200               new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
    201                   ? AM_SEGMENT_ID_ACTIVE
    202                   : AM_SEGMENT_ID_INACTIVE;
    203         }
    204       }
    205       cpi->active_map.enabled = 1;
    206     } else {
    207       cpi->active_map.enabled = 0;
    208     }
    209     return 0;
    210   } else {
    211     return -1;
    212   }
    213 }
    214 
    215 int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
    216                        int cols) {
    217   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
    218       new_map_16x16) {
    219     unsigned char *const seg_map_8x8 = cpi->segmentation_map;
    220     const int mi_rows = cpi->common.mi_rows;
    221     const int mi_cols = cpi->common.mi_cols;
    222     const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
    223     const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
    224 
    225     memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
    226     if (cpi->active_map.enabled) {
    227       int r, c;
    228       for (r = 0; r < mi_rows; ++r) {
    229         for (c = 0; c < mi_cols; ++c) {
    230           // Cyclic refresh segments are considered active despite not having
    231           // AM_SEGMENT_ID_ACTIVE
    232           new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
    233               seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
    234         }
    235       }
    236     }
    237     return 0;
    238   } else {
    239     return -1;
    240   }
    241 }
    242 
    243 // Compute the horizontal frequency components' energy in a frame
    244 // by calculuating the 16x4 Horizontal DCT. This is to be used to
    245 // decide the superresolution parameters.
    246 void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
    247   uint64_t freq_energy[16] = { 0 };
    248   const YV12_BUFFER_CONFIG *buf = cpi->source;
    249   const int bd = cpi->td.mb.e_mbd.bd;
    250   const int width = buf->y_crop_width;
    251   const int height = buf->y_crop_height;
    252   DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
    253   int n = 0;
    254   memset(freq_energy, 0, sizeof(freq_energy));
    255   if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    256     const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
    257     for (int i = 0; i < height - 4; i += 4) {
    258       for (int j = 0; j < width - 16; j += 16) {
    259         av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
    260                             H_DCT, bd);
    261         for (int k = 1; k < 16; ++k) {
    262           const uint64_t this_energy =
    263               ((int64_t)coeff[k] * coeff[k]) +
    264               ((int64_t)coeff[k + 16] * coeff[k + 16]) +
    265               ((int64_t)coeff[k + 32] * coeff[k + 32]) +
    266               ((int64_t)coeff[k + 48] * coeff[k + 48]);
    267           freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
    268         }
    269         n++;
    270       }
    271     }
    272   } else {
    273     assert(bd == 8);
    274     DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
    275     for (int i = 0; i < height - 4; i += 4) {
    276       for (int j = 0; j < width - 16; j += 16) {
    277         for (int ii = 0; ii < 4; ++ii)
    278           for (int jj = 0; jj < 16; ++jj)
    279             src16[ii * 16 + jj] =
    280                 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
    281         av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
    282         for (int k = 1; k < 16; ++k) {
    283           const uint64_t this_energy =
    284               ((int64_t)coeff[k] * coeff[k]) +
    285               ((int64_t)coeff[k + 16] * coeff[k + 16]) +
    286               ((int64_t)coeff[k + 32] * coeff[k + 32]) +
    287               ((int64_t)coeff[k + 48] * coeff[k + 48]);
    288           freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
    289         }
    290         n++;
    291       }
    292     }
    293   }
    294   if (n) {
    295     for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
    296     // Convert to cumulative energy
    297     for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
    298   } else {
    299     for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
    300   }
    301 }
    302 
    303 static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
    304                                   int cur_frame_force_integer_mv) {
    305   MACROBLOCK *const mb = &cpi->td.mb;
    306   cpi->common.allow_high_precision_mv =
    307       allow_high_precision_mv && cur_frame_force_integer_mv == 0;
    308   const int copy_hp =
    309       cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
    310   int *(*src)[2] = copy_hp ? &mb->nmvcost_hp : &mb->nmvcost;
    311   mb->mv_cost_stack = *src;
    312 }
    313 
    314 static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
    315   const AV1_COMMON *const cm = &cpi->common;
    316 
    317   if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
    318     return BLOCK_64X64;
    319 #if CONFIG_FILEOPTIONS
    320   if (cm->options && cm->options->ext_partition)
    321 #endif
    322     if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
    323       return BLOCK_128X128;
    324 
    325   assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
    326 
    327 // TODO(any): Possibly could improve this with a heuristic.
    328 #if CONFIG_FILEOPTIONS
    329   if (cm->options && !cm->options->ext_partition) return BLOCK_64X64;
    330 #endif
    331 
    332   // When superres / resize is on, 'cm->width / height' can change between
    333   // calls, so we don't apply this heuristic there. Also, this heuristic gives
    334   // compression gain for speed >= 2 only.
    335   // Things break if superblock size changes per-frame which is why this
    336   // heuristic is set based on configured speed rather than actual
    337   // speed-features (which may change per-frame in future)
    338   if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
    339       cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 2) {
    340     return (cm->width >= 480 && cm->height >= 360) ? BLOCK_128X128
    341                                                    : BLOCK_64X64;
    342   }
    343 
    344   return BLOCK_128X128;
    345 }
    346 
    347 static void setup_frame(AV1_COMP *cpi) {
    348   AV1_COMMON *const cm = &cpi->common;
    349   // Set up entropy context depending on frame type. The decoder mandates
    350   // the use of the default context, index 0, for keyframes and inter
    351   // frames where the error_resilient_mode or intra_only flag is set. For
    352   // other inter-frames the encoder currently uses only two contexts;
    353   // context 1 for ALTREF frames and context 0 for the others.
    354 
    355   if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
    356       cpi->ext_use_primary_ref_none) {
    357     av1_setup_past_independence(cm);
    358   }
    359 
    360   if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
    361     set_sb_size(&cm->seq_params, select_sb_size(cpi));
    362   } else if (frame_is_sframe(cm)) {
    363     set_sb_size(&cm->seq_params, select_sb_size(cpi));
    364   } else {
    365     const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
    366     if (primary_ref_buf == NULL) {
    367       av1_setup_past_independence(cm);
    368       cm->seg.update_map = 1;
    369       cm->seg.update_data = 1;
    370     } else {
    371       *cm->fc = primary_ref_buf->frame_context;
    372     }
    373   }
    374 
    375   av1_zero(cm->cur_frame->interp_filter_selected);
    376   cm->prev_frame = get_primary_ref_frame_buf(cm);
    377   cpi->vaq_refresh = 0;
    378 }
    379 
    380 static void enc_setup_mi(AV1_COMMON *cm) {
    381   int i;
    382   int mi_rows_sb_aligned = calc_mi_size(cm->mi_rows);
    383   cm->mi = cm->mip;
    384   memset(cm->mip, 0, cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mip));
    385   cm->prev_mi = cm->prev_mip;
    386   // Clear top border row
    387   memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
    388   // Clear left border column
    389   for (i = 0; i < mi_rows_sb_aligned; ++i)
    390     memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
    391   cm->mi_grid_visible = cm->mi_grid_base;
    392   cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
    393 
    394   memset(cm->mi_grid_base, 0,
    395          cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi_grid_base));
    396 }
    397 
    398 static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
    399   cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
    400   if (!cm->mip) return 1;
    401   cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
    402   if (!cm->prev_mip) return 1;
    403   cm->mi_alloc_size = mi_size;
    404 
    405   cm->mi_grid_base =
    406       (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
    407   if (!cm->mi_grid_base) return 1;
    408   cm->prev_mi_grid_base =
    409       (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
    410   if (!cm->prev_mi_grid_base) return 1;
    411 
    412   return 0;
    413 }
    414 
    415 static void enc_free_mi(AV1_COMMON *cm) {
    416   aom_free(cm->mip);
    417   cm->mip = NULL;
    418   aom_free(cm->prev_mip);
    419   cm->prev_mip = NULL;
    420   aom_free(cm->mi_grid_base);
    421   cm->mi_grid_base = NULL;
    422   aom_free(cm->prev_mi_grid_base);
    423   cm->prev_mi_grid_base = NULL;
    424   cm->mi_alloc_size = 0;
    425 }
    426 
    427 static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
    428   // Current mip will be the prev_mip for the next frame.
    429   MB_MODE_INFO **temp_base = cm->prev_mi_grid_base;
    430   MB_MODE_INFO *temp = cm->prev_mip;
    431   cm->prev_mip = cm->mip;
    432   cm->mip = temp;
    433 
    434   // Update the upper left visible macroblock ptrs.
    435   cm->mi = cm->mip;
    436   cm->prev_mi = cm->prev_mip;
    437 
    438   cm->prev_mi_grid_base = cm->mi_grid_base;
    439   cm->mi_grid_base = temp_base;
    440   cm->mi_grid_visible = cm->mi_grid_base;
    441   cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
    442 }
    443 
    444 void av1_initialize_enc(void) {
    445   av1_rtcd();
    446   aom_dsp_rtcd();
    447   aom_scale_rtcd();
    448   av1_init_intra_predictors();
    449   av1_init_me_luts();
    450   av1_rc_init_minq_luts();
    451   av1_init_wedge_masks();
    452 }
    453 
    454 static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
    455   if (cpi->mbmi_ext_base) {
    456     aom_free(cpi->mbmi_ext_base);
    457     cpi->mbmi_ext_base = NULL;
    458   }
    459 }
    460 
    461 static void alloc_context_buffers_ext(AV1_COMP *cpi) {
    462   AV1_COMMON *cm = &cpi->common;
    463   int mi_size = cm->mi_cols * cm->mi_rows;
    464 
    465   dealloc_context_buffers_ext(cpi);
    466   CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
    467                   aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
    468 }
    469 
    470 static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
    471   pars->num_cr_points = 0;
    472   pars->cr_mult = 0;
    473   pars->cr_luma_mult = 0;
    474   memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
    475   memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
    476   pars->num_cb_points = 0;
    477   pars->cb_mult = 0;
    478   pars->cb_luma_mult = 0;
    479   pars->chroma_scaling_from_luma = 0;
    480   memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
    481   memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
    482 }
    483 
    484 static void update_film_grain_parameters(struct AV1_COMP *cpi,
    485                                          const AV1EncoderConfig *oxcf) {
    486   AV1_COMMON *const cm = &cpi->common;
    487   cpi->oxcf = *oxcf;
    488 
    489   if (cpi->film_grain_table) {
    490     aom_film_grain_table_free(cpi->film_grain_table);
    491     aom_free(cpi->film_grain_table);
    492     cpi->film_grain_table = NULL;
    493   }
    494 
    495   if (oxcf->film_grain_test_vector) {
    496     cm->seq_params.film_grain_params_present = 1;
    497     if (cm->current_frame.frame_type == KEY_FRAME) {
    498       memcpy(&cm->film_grain_params,
    499              film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
    500              sizeof(cm->film_grain_params));
    501       if (oxcf->monochrome)
    502         reset_film_grain_chroma_params(&cm->film_grain_params);
    503       cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
    504       if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
    505         cm->film_grain_params.clip_to_restricted_range = 0;
    506       }
    507     }
    508   } else if (oxcf->film_grain_table_filename) {
    509     cm->seq_params.film_grain_params_present = 1;
    510 
    511     cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
    512     memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
    513 
    514     aom_film_grain_table_read(cpi->film_grain_table,
    515                               oxcf->film_grain_table_filename, &cm->error);
    516   } else {
    517 #if CONFIG_DENOISE
    518     cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
    519 #else
    520     cm->seq_params.film_grain_params_present = 0;
    521 #endif
    522     memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
    523   }
    524 }
    525 
    526 static void dealloc_compressor_data(AV1_COMP *cpi) {
    527   AV1_COMMON *const cm = &cpi->common;
    528   const int num_planes = av1_num_planes(cm);
    529 
    530   dealloc_context_buffers_ext(cpi);
    531 
    532   aom_free(cpi->tile_data);
    533   cpi->tile_data = NULL;
    534 
    535   // Delete sementation map
    536   aom_free(cpi->segmentation_map);
    537   cpi->segmentation_map = NULL;
    538 
    539   av1_cyclic_refresh_free(cpi->cyclic_refresh);
    540   cpi->cyclic_refresh = NULL;
    541 
    542   aom_free(cpi->active_map.map);
    543   cpi->active_map.map = NULL;
    544 
    545   aom_free(cpi->td.mb.above_pred_buf);
    546   cpi->td.mb.above_pred_buf = NULL;
    547 
    548   aom_free(cpi->td.mb.left_pred_buf);
    549   cpi->td.mb.left_pred_buf = NULL;
    550 
    551   aom_free(cpi->td.mb.wsrc_buf);
    552   cpi->td.mb.wsrc_buf = NULL;
    553 
    554   aom_free(cpi->td.mb.inter_modes_info);
    555   cpi->td.mb.inter_modes_info = NULL;
    556 
    557   for (int i = 0; i < 2; i++)
    558     for (int j = 0; j < 2; j++) {
    559       aom_free(cpi->td.mb.hash_value_buffer[i][j]);
    560       cpi->td.mb.hash_value_buffer[i][j] = NULL;
    561     }
    562   aom_free(cpi->td.mb.mask_buf);
    563   cpi->td.mb.mask_buf = NULL;
    564 
    565   aom_free(cm->tpl_mvs);
    566   cm->tpl_mvs = NULL;
    567 
    568   av1_free_ref_frame_buffers(cm->buffer_pool);
    569   av1_free_txb_buf(cpi);
    570   av1_free_context_buffers(cm);
    571 
    572   aom_free_frame_buffer(&cpi->last_frame_uf);
    573   av1_free_restoration_buffers(cm);
    574   aom_free_frame_buffer(&cpi->trial_frame_rst);
    575   aom_free_frame_buffer(&cpi->scaled_source);
    576   aom_free_frame_buffer(&cpi->scaled_last_source);
    577   aom_free_frame_buffer(&cpi->alt_ref_buffer);
    578   av1_lookahead_destroy(cpi->lookahead);
    579 
    580   aom_free(cpi->tile_tok[0][0]);
    581   cpi->tile_tok[0][0] = 0;
    582 
    583   aom_free(cpi->tplist[0][0]);
    584   cpi->tplist[0][0] = NULL;
    585 
    586   av1_free_pc_tree(&cpi->td, num_planes);
    587 
    588   aom_free(cpi->td.mb.palette_buffer);
    589 
    590   aom_free(cpi->td.mb.tmp_conv_dst);
    591   for (int j = 0; j < 2; ++j) {
    592     aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
    593   }
    594 
    595 #if CONFIG_DENOISE
    596   if (cpi->denoise_and_model) {
    597     aom_denoise_and_model_free(cpi->denoise_and_model);
    598     cpi->denoise_and_model = NULL;
    599   }
    600 #endif
    601   if (cpi->film_grain_table) {
    602     aom_film_grain_table_free(cpi->film_grain_table);
    603     cpi->film_grain_table = NULL;
    604   }
    605 }
    606 
    607 static void save_coding_context(AV1_COMP *cpi) {
    608   CODING_CONTEXT *const cc = &cpi->coding_context;
    609   AV1_COMMON *cm = &cpi->common;
    610 
    611   // Stores a snapshot of key state variables which can subsequently be
    612   // restored with a call to av1_restore_coding_context. These functions are
    613   // intended for use in a re-code loop in av1_compress_frame where the
    614   // quantizer value is adjusted between loop iterations.
    615   av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
    616   av1_copy(cc->nmv_costs, cpi->nmv_costs);
    617   av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
    618 
    619   cc->fc = *cm->fc;
    620 }
    621 
    622 static void restore_coding_context(AV1_COMP *cpi) {
    623   CODING_CONTEXT *const cc = &cpi->coding_context;
    624   AV1_COMMON *cm = &cpi->common;
    625 
    626   // Restore key state variables to the snapshot state stored in the
    627   // previous call to av1_save_coding_context.
    628   av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
    629   av1_copy(cpi->nmv_costs, cc->nmv_costs);
    630   av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
    631 
    632   *cm->fc = cc->fc;
    633 }
    634 
    635 static void configure_static_seg_features(AV1_COMP *cpi) {
    636   AV1_COMMON *const cm = &cpi->common;
    637   const RATE_CONTROL *const rc = &cpi->rc;
    638   struct segmentation *const seg = &cm->seg;
    639 
    640   int high_q = (int)(rc->avg_q > 48.0);
    641   int qi_delta;
    642 
    643   // Disable and clear down for KF
    644   if (cm->current_frame.frame_type == KEY_FRAME) {
    645     // Clear down the global segmentation map
    646     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    647     seg->update_map = 0;
    648     seg->update_data = 0;
    649     cpi->static_mb_pct = 0;
    650 
    651     // Disable segmentation
    652     av1_disable_segmentation(seg);
    653 
    654     // Clear down the segment features.
    655     av1_clearall_segfeatures(seg);
    656   } else if (cpi->refresh_alt_ref_frame) {
    657     // If this is an alt ref frame
    658     // Clear down the global segmentation map
    659     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    660     seg->update_map = 0;
    661     seg->update_data = 0;
    662     cpi->static_mb_pct = 0;
    663 
    664     // Disable segmentation and individual segment features by default
    665     av1_disable_segmentation(seg);
    666     av1_clearall_segfeatures(seg);
    667 
    668     // Scan frames from current to arf frame.
    669     // This function re-enables segmentation if appropriate.
    670     av1_update_mbgraph_stats(cpi);
    671 
    672     // If segmentation was enabled set those features needed for the
    673     // arf itself.
    674     if (seg->enabled) {
    675       seg->update_map = 1;
    676       seg->update_data = 1;
    677 
    678       qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
    679                                     cm->seq_params.bit_depth);
    680       av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
    681       av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
    682       av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
    683       av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
    684       av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
    685 
    686       av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
    687       av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
    688       av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
    689       av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
    690 
    691       av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
    692     }
    693   } else if (seg->enabled) {
    694     // All other frames if segmentation has been enabled
    695 
    696     // First normal frame in a valid gf or alt ref group
    697     if (rc->frames_since_golden == 0) {
    698       // Set up segment features for normal frames in an arf group
    699       if (rc->source_alt_ref_active) {
    700         seg->update_map = 0;
    701         seg->update_data = 1;
    702 
    703         qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
    704                                       cm->seq_params.bit_depth);
    705         av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
    706         av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
    707 
    708         av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
    709         av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
    710         av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
    711         av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
    712 
    713         av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
    714         av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
    715         av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
    716         av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
    717 
    718         // Segment coding disabled for compred testing
    719         if (high_q || (cpi->static_mb_pct == 100)) {
    720           av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
    721           av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
    722           av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
    723         }
    724       } else {
    725         // Disable segmentation and clear down features if alt ref
    726         // is not active for this group
    727 
    728         av1_disable_segmentation(seg);
    729 
    730         memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    731 
    732         seg->update_map = 0;
    733         seg->update_data = 0;
    734 
    735         av1_clearall_segfeatures(seg);
    736       }
    737     } else if (rc->is_src_frame_alt_ref) {
    738       // Special case where we are coding over the top of a previous
    739       // alt ref frame.
    740       // Segment coding disabled for compred testing
    741 
    742       // Enable ref frame features for segment 0 as well
    743       av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
    744       av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
    745 
    746       // All mbs should use ALTREF_FRAME
    747       av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
    748       av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
    749       av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
    750       av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
    751 
    752       // Skip all MBs if high Q (0,0 mv and skip coeffs)
    753       if (high_q) {
    754         av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
    755         av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
    756       }
    757       // Enable data update
    758       seg->update_data = 1;
    759     } else {
    760       // All other frames.
    761 
    762       // No updates.. leave things as they are.
    763       seg->update_map = 0;
    764       seg->update_data = 0;
    765     }
    766   }
    767 }
    768 
    769 static void update_reference_segmentation_map(AV1_COMP *cpi) {
    770   AV1_COMMON *const cm = &cpi->common;
    771   MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
    772   uint8_t *cache_ptr = cm->cur_frame->seg_map;
    773   int row, col;
    774 
    775   for (row = 0; row < cm->mi_rows; row++) {
    776     MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
    777     uint8_t *cache = cache_ptr;
    778     for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
    779       cache[0] = mi_4x4[0]->segment_id;
    780     mi_4x4_ptr += cm->mi_stride;
    781     cache_ptr += cm->mi_cols;
    782   }
    783 }
    784 
    785 static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
    786   AV1_COMMON *cm = &cpi->common;
    787   const SequenceHeader *const seq_params = &cm->seq_params;
    788   const AV1EncoderConfig *oxcf = &cpi->oxcf;
    789 
    790   if (!cpi->lookahead) {
    791     int is_scale = (oxcf->resize_mode || oxcf->superres_mode);
    792     cpi->lookahead = av1_lookahead_init(
    793         oxcf->width, oxcf->height, seq_params->subsampling_x,
    794         seq_params->subsampling_y, seq_params->use_highbitdepth,
    795         oxcf->lag_in_frames, oxcf->border_in_pixels, is_scale);
    796   }
    797   if (!cpi->lookahead)
    798     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    799                        "Failed to allocate lag buffers");
    800 
    801   // TODO(agrange) Check if ARF is enabled and skip allocation if not.
    802   if (aom_realloc_frame_buffer(
    803           &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
    804           seq_params->subsampling_x, seq_params->subsampling_y,
    805           seq_params->use_highbitdepth, oxcf->border_in_pixels,
    806           cm->byte_alignment, NULL, NULL, NULL))
    807     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    808                        "Failed to allocate altref buffer");
    809 }
    810 
    811 static void alloc_util_frame_buffers(AV1_COMP *cpi) {
    812   AV1_COMMON *const cm = &cpi->common;
    813   const SequenceHeader *const seq_params = &cm->seq_params;
    814   if (aom_realloc_frame_buffer(
    815           &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
    816           seq_params->subsampling_y, seq_params->use_highbitdepth,
    817           cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
    818     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    819                        "Failed to allocate last frame buffer");
    820 
    821   if (aom_realloc_frame_buffer(
    822           &cpi->trial_frame_rst, cm->superres_upscaled_width,
    823           cm->superres_upscaled_height, seq_params->subsampling_x,
    824           seq_params->subsampling_y, seq_params->use_highbitdepth,
    825           AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
    826     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    827                        "Failed to allocate trial restored frame buffer");
    828 
    829   if (aom_realloc_frame_buffer(
    830           &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
    831           seq_params->subsampling_y, seq_params->use_highbitdepth,
    832           cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
    833     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    834                        "Failed to allocate scaled source buffer");
    835 
    836   if (aom_realloc_frame_buffer(
    837           &cpi->scaled_last_source, cm->width, cm->height,
    838           seq_params->subsampling_x, seq_params->subsampling_y,
    839           seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
    840           cm->byte_alignment, NULL, NULL, NULL))
    841     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    842                        "Failed to allocate scaled last source buffer");
    843 }
    844 
    845 static void alloc_compressor_data(AV1_COMP *cpi) {
    846   AV1_COMMON *cm = &cpi->common;
    847   const int num_planes = av1_num_planes(cm);
    848 
    849   av1_alloc_context_buffers(cm, cm->width, cm->height);
    850 
    851   int mi_rows_aligned_to_sb =
    852       ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
    853   int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
    854 
    855   av1_alloc_txb_buf(cpi);
    856 
    857   alloc_context_buffers_ext(cpi);
    858 
    859   aom_free(cpi->tile_tok[0][0]);
    860 
    861   {
    862     unsigned int tokens =
    863         get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
    864     CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
    865                     aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
    866   }
    867   aom_free(cpi->tplist[0][0]);
    868 
    869   CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
    870                   aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
    871                              sizeof(*cpi->tplist[0][0])));
    872 
    873   av1_setup_pc_tree(&cpi->common, &cpi->td);
    874 }
    875 
    876 void av1_new_framerate(AV1_COMP *cpi, double framerate) {
    877   cpi->framerate = framerate < 0.1 ? 30 : framerate;
    878   av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
    879 }
    880 
    881 static void set_tile_info(AV1_COMP *cpi) {
    882   AV1_COMMON *const cm = &cpi->common;
    883   int i, start_sb;
    884 
    885   av1_get_tile_limits(cm);
    886 
    887   // configure tile columns
    888   if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
    889     cm->uniform_tile_spacing_flag = 1;
    890     cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
    891     cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
    892   } else {
    893     int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
    894     int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
    895     int size_sb, j = 0;
    896     cm->uniform_tile_spacing_flag = 0;
    897     for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
    898       cm->tile_col_start_sb[i] = start_sb;
    899       size_sb = cpi->oxcf.tile_widths[j++];
    900       if (j >= cpi->oxcf.tile_width_count) j = 0;
    901       start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
    902     }
    903     cm->tile_cols = i;
    904     cm->tile_col_start_sb[i] = sb_cols;
    905   }
    906   av1_calculate_tile_cols(cm);
    907 
    908   // configure tile rows
    909   if (cm->uniform_tile_spacing_flag) {
    910     cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
    911     cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
    912   } else {
    913     int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
    914     int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
    915     int size_sb, j = 0;
    916     for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
    917       cm->tile_row_start_sb[i] = start_sb;
    918       size_sb = cpi->oxcf.tile_heights[j++];
    919       if (j >= cpi->oxcf.tile_height_count) j = 0;
    920       start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
    921     }
    922     cm->tile_rows = i;
    923     cm->tile_row_start_sb[i] = sb_rows;
    924   }
    925   av1_calculate_tile_rows(cm);
    926 }
    927 
    928 static void update_frame_size(AV1_COMP *cpi) {
    929   AV1_COMMON *const cm = &cpi->common;
    930   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
    931 
    932   av1_set_mb_mi(cm, cm->width, cm->height);
    933   av1_init_context_buffers(cm);
    934   av1_init_macroblockd(cm, xd, NULL);
    935   memset(cpi->mbmi_ext_base, 0,
    936          cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
    937   set_tile_info(cpi);
    938 }
    939 
    940 static void init_buffer_indices(AV1_COMP *cpi) {
    941   int fb_idx;
    942   for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
    943     cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
    944   cpi->rate_index = 0;
    945   cpi->rate_size = 0;
    946 }
    947 
    948 static INLINE int does_level_match(int width, int height, double fps,
    949                                    int lvl_width, int lvl_height,
    950                                    double lvl_fps, int lvl_dim_mult) {
    951   const int64_t lvl_luma_pels = lvl_width * lvl_height;
    952   const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
    953   const int64_t luma_pels = width * height;
    954   const double display_sample_rate = luma_pels * fps;
    955   return luma_pels <= lvl_luma_pels &&
    956          display_sample_rate <= lvl_display_sample_rate &&
    957          width <= lvl_width * lvl_dim_mult &&
    958          height <= lvl_height * lvl_dim_mult;
    959 }
    960 
    961 static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
    962                                      const AV1EncoderConfig *oxcf) {
    963   // TODO(any): This is a placeholder function that only addresses dimensions
    964   // and max display sample rates.
    965   // Need to add checks for max bit rate, max decoded luma sample rate, header
    966   // rate, etc. that are not covered by this function.
    967   AV1_LEVEL level = SEQ_LEVEL_MAX;
    968   if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
    969                        288, 30.0, 4)) {
    970     level = SEQ_LEVEL_2_0;
    971   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    972                               704, 396, 30.0, 4)) {
    973     level = SEQ_LEVEL_2_1;
    974   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    975                               1088, 612, 30.0, 4)) {
    976     level = SEQ_LEVEL_3_0;
    977   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    978                               1376, 774, 30.0, 4)) {
    979     level = SEQ_LEVEL_3_1;
    980   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    981                               2048, 1152, 30.0, 3)) {
    982     level = SEQ_LEVEL_4_0;
    983   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    984                               2048, 1152, 60.0, 3)) {
    985     level = SEQ_LEVEL_4_1;
    986   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    987                               4096, 2176, 30.0, 2)) {
    988     level = SEQ_LEVEL_5_0;
    989   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    990                               4096, 2176, 60.0, 2)) {
    991     level = SEQ_LEVEL_5_1;
    992   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    993                               4096, 2176, 120.0, 2)) {
    994     level = SEQ_LEVEL_5_2;
    995   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    996                               8192, 4352, 30.0, 2)) {
    997     level = SEQ_LEVEL_6_0;
    998   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
    999                               8192, 4352, 60.0, 2)) {
   1000   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
   1001                               8192, 4352, 120.0, 2)) {
   1002     level = SEQ_LEVEL_6_2;
   1003   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
   1004                               16384, 8704, 30.0, 2)) {
   1005     level = SEQ_LEVEL_7_0;
   1006   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
   1007                               16384, 8704, 60.0, 2)) {
   1008     level = SEQ_LEVEL_7_1;
   1009   } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
   1010                               16384, 8704, 120.0, 2)) {
   1011     level = SEQ_LEVEL_7_2;
   1012   }
   1013   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
   1014     seq->seq_level_idx[i] = level;
   1015     // Set the maximum parameters for bitrate and buffer size for this profile,
   1016     // level, and tier
   1017     cm->op_params[i].bitrate = max_level_bitrate(
   1018         cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
   1019     // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
   1020     // check
   1021     if (cm->op_params[i].bitrate == 0)
   1022       aom_internal_error(
   1023           &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   1024           "AV1 does not support this combination of profile, level, and tier.");
   1025     // Buffer size in bits/s is bitrate in bits/s * 1 s
   1026     cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
   1027   }
   1028 }
   1029 
   1030 static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
   1031                                   const AV1EncoderConfig *oxcf) {
   1032   seq->still_picture = (oxcf->limit == 1);
   1033   seq->reduced_still_picture_hdr = seq->still_picture;
   1034   seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
   1035   seq->force_screen_content_tools = 2;
   1036   seq->force_integer_mv = 2;
   1037   seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
   1038   seq->frame_id_numbers_present_flag =
   1039       !(seq->still_picture && seq->reduced_still_picture_hdr) &&
   1040       !oxcf->large_scale_tile && oxcf->error_resilient_mode;
   1041   if (seq->still_picture && seq->reduced_still_picture_hdr) {
   1042     seq->order_hint_info.enable_order_hint = 0;
   1043     seq->force_screen_content_tools = 2;
   1044     seq->force_integer_mv = 2;
   1045   }
   1046   seq->order_hint_info.order_hint_bits_minus_1 =
   1047       seq->order_hint_info.enable_order_hint
   1048           ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
   1049           : -1;
   1050 
   1051   seq->max_frame_width =
   1052       oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
   1053   seq->max_frame_height = oxcf->forced_max_frame_height
   1054                               ? oxcf->forced_max_frame_height
   1055                               : oxcf->height;
   1056   seq->num_bits_width =
   1057       (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
   1058   seq->num_bits_height =
   1059       (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
   1060   assert(seq->num_bits_width <= 16);
   1061   assert(seq->num_bits_height <= 16);
   1062 
   1063   seq->frame_id_length = FRAME_ID_LENGTH;
   1064   seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
   1065 
   1066   seq->enable_dual_filter = oxcf->enable_dual_filter;
   1067   seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
   1068   seq->order_hint_info.enable_dist_wtd_comp &=
   1069       seq->order_hint_info.enable_order_hint;
   1070   seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
   1071   seq->order_hint_info.enable_ref_frame_mvs &=
   1072       seq->order_hint_info.enable_order_hint;
   1073   seq->enable_superres = oxcf->enable_superres;
   1074   seq->enable_cdef = oxcf->enable_cdef;
   1075   seq->enable_restoration = oxcf->enable_restoration;
   1076   seq->enable_warped_motion = oxcf->enable_warped_motion;
   1077   seq->enable_interintra_compound = oxcf->enable_interintra_comp;
   1078   seq->enable_masked_compound = oxcf->enable_masked_comp;
   1079   seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
   1080   seq->enable_filter_intra = oxcf->enable_filter_intra;
   1081 
   1082   set_bitstream_level_tier(seq, cm, oxcf);
   1083 
   1084   if (seq->operating_points_cnt_minus_1 == 0) {
   1085     seq->operating_point_idc[0] = 0;
   1086   } else {
   1087     // Set operating_point_idc[] such that for the i-th operating point the
   1088     // first (operating_points_cnt-i) spatial layers and the first temporal
   1089     // layer are decoded Note that highest quality operating point should come
   1090     // first
   1091     for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
   1092       seq->operating_point_idc[i] =
   1093           (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
   1094   }
   1095 }
   1096 
   1097 static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
   1098   AV1_COMMON *const cm = &cpi->common;
   1099 
   1100   cpi->oxcf = *oxcf;
   1101   cpi->framerate = oxcf->init_framerate;
   1102 
   1103   cm->seq_params.profile = oxcf->profile;
   1104   cm->seq_params.bit_depth = oxcf->bit_depth;
   1105   cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
   1106   cm->seq_params.color_primaries = oxcf->color_primaries;
   1107   cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
   1108   cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
   1109   cm->seq_params.monochrome = oxcf->monochrome;
   1110   cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
   1111   cm->seq_params.color_range = oxcf->color_range;
   1112   cm->timing_info_present = oxcf->timing_info_present;
   1113   cm->timing_info.num_units_in_display_tick =
   1114       oxcf->timing_info.num_units_in_display_tick;
   1115   cm->timing_info.time_scale = oxcf->timing_info.time_scale;
   1116   cm->timing_info.equal_picture_interval =
   1117       oxcf->timing_info.equal_picture_interval;
   1118   cm->timing_info.num_ticks_per_picture =
   1119       oxcf->timing_info.num_ticks_per_picture;
   1120 
   1121   cm->seq_params.display_model_info_present_flag =
   1122       oxcf->display_model_info_present_flag;
   1123   cm->seq_params.decoder_model_info_present_flag =
   1124       oxcf->decoder_model_info_present_flag;
   1125   if (oxcf->decoder_model_info_present_flag) {
   1126     // set the decoder model parameters in schedule mode
   1127     cm->buffer_model.num_units_in_decoding_tick =
   1128         oxcf->buffer_model.num_units_in_decoding_tick;
   1129     cm->buffer_removal_time_present = 1;
   1130     set_aom_dec_model_info(&cm->buffer_model);
   1131     set_dec_model_op_parameters(&cm->op_params[0]);
   1132   } else if (cm->timing_info_present &&
   1133              cm->timing_info.equal_picture_interval &&
   1134              !cm->seq_params.decoder_model_info_present_flag) {
   1135     // set the decoder model parameters in resource availability mode
   1136     set_resource_availability_parameters(&cm->op_params[0]);
   1137   } else {
   1138     cm->op_params[0].initial_display_delay =
   1139         10;  // Default value (not signaled)
   1140   }
   1141 
   1142   if (cm->seq_params.monochrome) {
   1143     cm->seq_params.subsampling_x = 1;
   1144     cm->seq_params.subsampling_y = 1;
   1145   } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
   1146              cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
   1147              cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
   1148     cm->seq_params.subsampling_x = 0;
   1149     cm->seq_params.subsampling_y = 0;
   1150   } else {
   1151     if (cm->seq_params.profile == 0) {
   1152       cm->seq_params.subsampling_x = 1;
   1153       cm->seq_params.subsampling_y = 1;
   1154     } else if (cm->seq_params.profile == 1) {
   1155       cm->seq_params.subsampling_x = 0;
   1156       cm->seq_params.subsampling_y = 0;
   1157     } else {
   1158       if (cm->seq_params.bit_depth == AOM_BITS_12) {
   1159         cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
   1160         cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
   1161       } else {
   1162         cm->seq_params.subsampling_x = 1;
   1163         cm->seq_params.subsampling_y = 0;
   1164       }
   1165     }
   1166   }
   1167 
   1168   cm->width = oxcf->width;
   1169   cm->height = oxcf->height;
   1170   set_sb_size(&cm->seq_params,
   1171               select_sb_size(cpi));  // set sb size before allocations
   1172   alloc_compressor_data(cpi);
   1173 
   1174   update_film_grain_parameters(cpi, oxcf);
   1175 
   1176   // Single thread case: use counts in common.
   1177   cpi->td.counts = &cpi->counts;
   1178 
   1179   // change includes all joint functionality
   1180   av1_change_config(cpi, oxcf);
   1181 
   1182   cpi->static_mb_pct = 0;
   1183   cpi->ref_frame_flags = 0;
   1184 
   1185   // Reset resize pending flags
   1186   cpi->resize_pending_width = 0;
   1187   cpi->resize_pending_height = 0;
   1188 
   1189   init_buffer_indices(cpi);
   1190 }
   1191 
   1192 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
   1193                                 const AV1EncoderConfig *oxcf) {
   1194   const int64_t bandwidth = oxcf->target_bandwidth;
   1195   const int64_t starting = oxcf->starting_buffer_level_ms;
   1196   const int64_t optimal = oxcf->optimal_buffer_level_ms;
   1197   const int64_t maximum = oxcf->maximum_buffer_size_ms;
   1198 
   1199   rc->starting_buffer_level = starting * bandwidth / 1000;
   1200   rc->optimal_buffer_level =
   1201       (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
   1202   rc->maximum_buffer_size =
   1203       (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
   1204 }
   1205 
   1206 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
   1207   cpi->fn_ptr[BT].sdf = SDF;                                           \
   1208   cpi->fn_ptr[BT].sdaf = SDAF;                                         \
   1209   cpi->fn_ptr[BT].vf = VF;                                             \
   1210   cpi->fn_ptr[BT].svf = SVF;                                           \
   1211   cpi->fn_ptr[BT].svaf = SVAF;                                         \
   1212   cpi->fn_ptr[BT].sdx4df = SDX4DF;                                     \
   1213   cpi->fn_ptr[BT].jsdaf = JSDAF;                                       \
   1214   cpi->fn_ptr[BT].jsvaf = JSVAF;
   1215 
   1216 #define MAKE_BFP_SAD_WRAPPER(fnname)                                           \
   1217   static unsigned int fnname##_bits8(const uint8_t *src_ptr,                   \
   1218                                      int source_stride,                        \
   1219                                      const uint8_t *ref_ptr, int ref_stride) { \
   1220     return fnname(src_ptr, source_stride, ref_ptr, ref_stride);                \
   1221   }                                                                            \
   1222   static unsigned int fnname##_bits10(                                         \
   1223       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1224       int ref_stride) {                                                        \
   1225     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2;           \
   1226   }                                                                            \
   1227   static unsigned int fnname##_bits12(                                         \
   1228       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1229       int ref_stride) {                                                        \
   1230     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4;           \
   1231   }
   1232 
   1233 #define MAKE_BFP_SADAVG_WRAPPER(fnname)                                        \
   1234   static unsigned int fnname##_bits8(                                          \
   1235       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1236       int ref_stride, const uint8_t *second_pred) {                            \
   1237     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred);   \
   1238   }                                                                            \
   1239   static unsigned int fnname##_bits10(                                         \
   1240       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1241       int ref_stride, const uint8_t *second_pred) {                            \
   1242     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
   1243            2;                                                                  \
   1244   }                                                                            \
   1245   static unsigned int fnname##_bits12(                                         \
   1246       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1247       int ref_stride, const uint8_t *second_pred) {                            \
   1248     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
   1249            4;                                                                  \
   1250   }
   1251 
   1252 #define MAKE_BFP_SAD4D_WRAPPER(fnname)                                        \
   1253   static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,       \
   1254                              const uint8_t *const ref_ptr[], int ref_stride,  \
   1255                              unsigned int *sad_array) {                       \
   1256     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
   1257   }                                                                           \
   1258   static void fnname##_bits10(const uint8_t *src_ptr, int source_stride,      \
   1259                               const uint8_t *const ref_ptr[], int ref_stride, \
   1260                               unsigned int *sad_array) {                      \
   1261     int i;                                                                    \
   1262     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
   1263     for (i = 0; i < 4; i++) sad_array[i] >>= 2;                               \
   1264   }                                                                           \
   1265   static void fnname##_bits12(const uint8_t *src_ptr, int source_stride,      \
   1266                               const uint8_t *const ref_ptr[], int ref_stride, \
   1267                               unsigned int *sad_array) {                      \
   1268     int i;                                                                    \
   1269     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
   1270     for (i = 0; i < 4; i++) sad_array[i] >>= 4;                               \
   1271   }
   1272 
   1273 #define MAKE_BFP_JSADAVG_WRAPPER(fnname)                                    \
   1274   static unsigned int fnname##_bits8(                                       \
   1275       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,    \
   1276       int ref_stride, const uint8_t *second_pred,                           \
   1277       const DIST_WTD_COMP_PARAMS *jcp_param) {                              \
   1278     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
   1279                   jcp_param);                                               \
   1280   }                                                                         \
   1281   static unsigned int fnname##_bits10(                                      \
   1282       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,    \
   1283       int ref_stride, const uint8_t *second_pred,                           \
   1284       const DIST_WTD_COMP_PARAMS *jcp_param) {                              \
   1285     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
   1286                   jcp_param) >>                                             \
   1287            2;                                                               \
   1288   }                                                                         \
   1289   static unsigned int fnname##_bits12(                                      \
   1290       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,    \
   1291       int ref_stride, const uint8_t *second_pred,                           \
   1292       const DIST_WTD_COMP_PARAMS *jcp_param) {                              \
   1293     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
   1294                   jcp_param) >>                                             \
   1295            4;                                                               \
   1296   }
   1297 
   1298 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
   1299 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
   1300 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
   1301 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
   1302 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
   1303 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
   1304 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
   1305 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
   1306 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
   1307 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
   1308 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
   1309 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
   1310 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
   1311 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
   1312 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
   1313 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
   1314 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
   1315 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
   1316 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
   1317 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
   1318 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
   1319 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
   1320 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
   1321 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
   1322 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
   1323 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
   1324 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
   1325 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
   1326 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
   1327 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
   1328 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
   1329 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
   1330 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
   1331 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
   1332 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
   1333 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
   1334 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
   1335 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
   1336 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
   1337 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
   1338 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
   1339 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
   1340 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
   1341 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
   1342 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
   1343 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
   1344 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
   1345 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
   1346 
   1347 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
   1348 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
   1349 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
   1350 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
   1351 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
   1352 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
   1353 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
   1354 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
   1355 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
   1356 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
   1357 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
   1358 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
   1359 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
   1360 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
   1361 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
   1362 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
   1363 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
   1364 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
   1365 
   1366 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
   1367 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
   1368 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
   1369 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
   1370 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
   1371 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
   1372 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
   1373 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
   1374 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
   1375 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
   1376 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
   1377 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
   1378 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
   1379 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
   1380 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
   1381 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
   1382 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
   1383 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
   1384 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
   1385 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
   1386 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
   1387 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
   1388 
   1389 #define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
   1390   cpi->fn_ptr[BT].msdf = MCSDF;       \
   1391   cpi->fn_ptr[BT].msvf = MCSVF;
   1392 
   1393 #define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname)                           \
   1394   static unsigned int fnname##_bits8(                                    \
   1395       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
   1396       int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m,  \
   1397       int m_stride, int invert_mask) {                                   \
   1398     return fnname(src_ptr, source_stride, ref_ptr, ref_stride,           \
   1399                   second_pred_ptr, m, m_stride, invert_mask);            \
   1400   }                                                                      \
   1401   static unsigned int fnname##_bits10(                                   \
   1402       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
   1403       int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m,  \
   1404       int m_stride, int invert_mask) {                                   \
   1405     return fnname(src_ptr, source_stride, ref_ptr, ref_stride,           \
   1406                   second_pred_ptr, m, m_stride, invert_mask) >>          \
   1407            2;                                                            \
   1408   }                                                                      \
   1409   static unsigned int fnname##_bits12(                                   \
   1410       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
   1411       int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m,  \
   1412       int m_stride, int invert_mask) {                                   \
   1413     return fnname(src_ptr, source_stride, ref_ptr, ref_stride,           \
   1414                   second_pred_ptr, m, m_stride, invert_mask) >>          \
   1415            4;                                                            \
   1416   }
   1417 
   1418 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
   1419 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
   1420 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
   1421 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
   1422 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
   1423 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
   1424 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
   1425 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
   1426 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
   1427 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
   1428 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
   1429 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
   1430 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
   1431 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
   1432 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
   1433 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
   1434 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
   1435 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
   1436 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
   1437 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
   1438 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
   1439 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
   1440 
   1441 #define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
   1442   cpi->fn_ptr[BT].osdf = OSDF;           \
   1443   cpi->fn_ptr[BT].ovf = OVF;             \
   1444   cpi->fn_ptr[BT].osvf = OSVF;
   1445 
   1446 #define MAKE_OBFP_SAD_WRAPPER(fnname)                                     \
   1447   static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride,  \
   1448                                      const int32_t *wsrc,                 \
   1449                                      const int32_t *msk) {                \
   1450     return fnname(ref, ref_stride, wsrc, msk);                            \
   1451   }                                                                       \
   1452   static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
   1453                                       const int32_t *wsrc,                \
   1454                                       const int32_t *msk) {               \
   1455     return fnname(ref, ref_stride, wsrc, msk) >> 2;                       \
   1456   }                                                                       \
   1457   static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
   1458                                       const int32_t *wsrc,                \
   1459                                       const int32_t *msk) {               \
   1460     return fnname(ref, ref_stride, wsrc, msk) >> 4;                       \
   1461   }
   1462 
   1463 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
   1464 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
   1465 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
   1466 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
   1467 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
   1468 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
   1469 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
   1470 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
   1471 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
   1472 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
   1473 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
   1474 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
   1475 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
   1476 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
   1477 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
   1478 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
   1479 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
   1480 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
   1481 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
   1482 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
   1483 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
   1484 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
   1485 
   1486 static void highbd_set_var_fns(AV1_COMP *const cpi) {
   1487   AV1_COMMON *const cm = &cpi->common;
   1488   if (cm->seq_params.use_highbitdepth) {
   1489     switch (cm->seq_params.bit_depth) {
   1490       case AOM_BITS_8:
   1491         HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
   1492                    aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
   1493                    aom_highbd_8_sub_pixel_variance64x16,
   1494                    aom_highbd_8_sub_pixel_avg_variance64x16,
   1495                    aom_highbd_sad64x16x4d_bits8,
   1496                    aom_highbd_dist_wtd_sad64x16_avg_bits8,
   1497                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
   1498 
   1499         HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
   1500                    aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
   1501                    aom_highbd_8_sub_pixel_variance16x64,
   1502                    aom_highbd_8_sub_pixel_avg_variance16x64,
   1503                    aom_highbd_sad16x64x4d_bits8,
   1504                    aom_highbd_dist_wtd_sad16x64_avg_bits8,
   1505                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
   1506 
   1507         HIGHBD_BFP(
   1508             BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
   1509             aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
   1510             aom_highbd_8_sub_pixel_avg_variance32x8,
   1511             aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
   1512             aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
   1513 
   1514         HIGHBD_BFP(
   1515             BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
   1516             aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
   1517             aom_highbd_8_sub_pixel_avg_variance8x32,
   1518             aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
   1519             aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
   1520 
   1521         HIGHBD_BFP(
   1522             BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
   1523             aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
   1524             aom_highbd_8_sub_pixel_avg_variance16x4,
   1525             aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
   1526             aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
   1527 
   1528         HIGHBD_BFP(
   1529             BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
   1530             aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
   1531             aom_highbd_8_sub_pixel_avg_variance4x16,
   1532             aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
   1533             aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
   1534 
   1535         HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
   1536                    aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
   1537                    aom_highbd_8_sub_pixel_variance32x16,
   1538                    aom_highbd_8_sub_pixel_avg_variance32x16,
   1539                    aom_highbd_sad32x16x4d_bits8,
   1540                    aom_highbd_dist_wtd_sad32x16_avg_bits8,
   1541                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
   1542 
   1543         HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
   1544                    aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
   1545                    aom_highbd_8_sub_pixel_variance16x32,
   1546                    aom_highbd_8_sub_pixel_avg_variance16x32,
   1547                    aom_highbd_sad16x32x4d_bits8,
   1548                    aom_highbd_dist_wtd_sad16x32_avg_bits8,
   1549                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
   1550 
   1551         HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
   1552                    aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
   1553                    aom_highbd_8_sub_pixel_variance64x32,
   1554                    aom_highbd_8_sub_pixel_avg_variance64x32,
   1555                    aom_highbd_sad64x32x4d_bits8,
   1556                    aom_highbd_dist_wtd_sad64x32_avg_bits8,
   1557                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
   1558 
   1559         HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
   1560                    aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
   1561                    aom_highbd_8_sub_pixel_variance32x64,
   1562                    aom_highbd_8_sub_pixel_avg_variance32x64,
   1563                    aom_highbd_sad32x64x4d_bits8,
   1564                    aom_highbd_dist_wtd_sad32x64_avg_bits8,
   1565                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
   1566 
   1567         HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
   1568                    aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
   1569                    aom_highbd_8_sub_pixel_variance32x32,
   1570                    aom_highbd_8_sub_pixel_avg_variance32x32,
   1571                    aom_highbd_sad32x32x4d_bits8,
   1572                    aom_highbd_dist_wtd_sad32x32_avg_bits8,
   1573                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
   1574 
   1575         HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
   1576                    aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
   1577                    aom_highbd_8_sub_pixel_variance64x64,
   1578                    aom_highbd_8_sub_pixel_avg_variance64x64,
   1579                    aom_highbd_sad64x64x4d_bits8,
   1580                    aom_highbd_dist_wtd_sad64x64_avg_bits8,
   1581                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
   1582 
   1583         HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
   1584                    aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
   1585                    aom_highbd_8_sub_pixel_variance16x16,
   1586                    aom_highbd_8_sub_pixel_avg_variance16x16,
   1587                    aom_highbd_sad16x16x4d_bits8,
   1588                    aom_highbd_dist_wtd_sad16x16_avg_bits8,
   1589                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
   1590 
   1591         HIGHBD_BFP(
   1592             BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
   1593             aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
   1594             aom_highbd_8_sub_pixel_avg_variance16x8,
   1595             aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
   1596             aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
   1597 
   1598         HIGHBD_BFP(
   1599             BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
   1600             aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
   1601             aom_highbd_8_sub_pixel_avg_variance8x16,
   1602             aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
   1603             aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
   1604 
   1605         HIGHBD_BFP(
   1606             BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
   1607             aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
   1608             aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
   1609             aom_highbd_dist_wtd_sad8x8_avg_bits8,
   1610             aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
   1611 
   1612         HIGHBD_BFP(
   1613             BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
   1614             aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
   1615             aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
   1616             aom_highbd_dist_wtd_sad8x4_avg_bits8,
   1617             aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
   1618 
   1619         HIGHBD_BFP(
   1620             BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
   1621             aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
   1622             aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
   1623             aom_highbd_dist_wtd_sad4x8_avg_bits8,
   1624             aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
   1625 
   1626         HIGHBD_BFP(
   1627             BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
   1628             aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
   1629             aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
   1630             aom_highbd_dist_wtd_sad4x4_avg_bits8,
   1631             aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
   1632 
   1633         HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
   1634                    aom_highbd_sad128x128_avg_bits8,
   1635                    aom_highbd_8_variance128x128,
   1636                    aom_highbd_8_sub_pixel_variance128x128,
   1637                    aom_highbd_8_sub_pixel_avg_variance128x128,
   1638                    aom_highbd_sad128x128x4d_bits8,
   1639                    aom_highbd_dist_wtd_sad128x128_avg_bits8,
   1640                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
   1641 
   1642         HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
   1643                    aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
   1644                    aom_highbd_8_sub_pixel_variance128x64,
   1645                    aom_highbd_8_sub_pixel_avg_variance128x64,
   1646                    aom_highbd_sad128x64x4d_bits8,
   1647                    aom_highbd_dist_wtd_sad128x64_avg_bits8,
   1648                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
   1649 
   1650         HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
   1651                    aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
   1652                    aom_highbd_8_sub_pixel_variance64x128,
   1653                    aom_highbd_8_sub_pixel_avg_variance64x128,
   1654                    aom_highbd_sad64x128x4d_bits8,
   1655                    aom_highbd_dist_wtd_sad64x128_avg_bits8,
   1656                    aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
   1657 
   1658         HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
   1659                     aom_highbd_8_masked_sub_pixel_variance128x128)
   1660         HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
   1661                     aom_highbd_8_masked_sub_pixel_variance128x64)
   1662         HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
   1663                     aom_highbd_8_masked_sub_pixel_variance64x128)
   1664         HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
   1665                     aom_highbd_8_masked_sub_pixel_variance64x64)
   1666         HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
   1667                     aom_highbd_8_masked_sub_pixel_variance64x32)
   1668         HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
   1669                     aom_highbd_8_masked_sub_pixel_variance32x64)
   1670         HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
   1671                     aom_highbd_8_masked_sub_pixel_variance32x32)
   1672         HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
   1673                     aom_highbd_8_masked_sub_pixel_variance32x16)
   1674         HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
   1675                     aom_highbd_8_masked_sub_pixel_variance16x32)
   1676         HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
   1677                     aom_highbd_8_masked_sub_pixel_variance16x16)
   1678         HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
   1679                     aom_highbd_8_masked_sub_pixel_variance8x16)
   1680         HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
   1681                     aom_highbd_8_masked_sub_pixel_variance16x8)
   1682         HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
   1683                     aom_highbd_8_masked_sub_pixel_variance8x8)
   1684         HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
   1685                     aom_highbd_8_masked_sub_pixel_variance4x8)
   1686         HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
   1687                     aom_highbd_8_masked_sub_pixel_variance8x4)
   1688         HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
   1689                     aom_highbd_8_masked_sub_pixel_variance4x4)
   1690         HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
   1691                     aom_highbd_8_masked_sub_pixel_variance64x16)
   1692         HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
   1693                     aom_highbd_8_masked_sub_pixel_variance16x64)
   1694         HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
   1695                     aom_highbd_8_masked_sub_pixel_variance32x8)
   1696         HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
   1697                     aom_highbd_8_masked_sub_pixel_variance8x32)
   1698         HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
   1699                     aom_highbd_8_masked_sub_pixel_variance16x4)
   1700         HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
   1701                     aom_highbd_8_masked_sub_pixel_variance4x16)
   1702         HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
   1703                     aom_highbd_obmc_variance128x128,
   1704                     aom_highbd_obmc_sub_pixel_variance128x128)
   1705         HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
   1706                     aom_highbd_obmc_variance128x64,
   1707                     aom_highbd_obmc_sub_pixel_variance128x64)
   1708         HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
   1709                     aom_highbd_obmc_variance64x128,
   1710                     aom_highbd_obmc_sub_pixel_variance64x128)
   1711         HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
   1712                     aom_highbd_obmc_variance64x64,
   1713                     aom_highbd_obmc_sub_pixel_variance64x64)
   1714         HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
   1715                     aom_highbd_obmc_variance64x32,
   1716                     aom_highbd_obmc_sub_pixel_variance64x32)
   1717         HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
   1718                     aom_highbd_obmc_variance32x64,
   1719                     aom_highbd_obmc_sub_pixel_variance32x64)
   1720         HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
   1721                     aom_highbd_obmc_variance32x32,
   1722                     aom_highbd_obmc_sub_pixel_variance32x32)
   1723         HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
   1724                     aom_highbd_obmc_variance32x16,
   1725                     aom_highbd_obmc_sub_pixel_variance32x16)
   1726         HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
   1727                     aom_highbd_obmc_variance16x32,
   1728                     aom_highbd_obmc_sub_pixel_variance16x32)
   1729         HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
   1730                     aom_highbd_obmc_variance16x16,
   1731                     aom_highbd_obmc_sub_pixel_variance16x16)
   1732         HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
   1733                     aom_highbd_obmc_variance8x16,
   1734                     aom_highbd_obmc_sub_pixel_variance8x16)
   1735         HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
   1736                     aom_highbd_obmc_variance16x8,
   1737                     aom_highbd_obmc_sub_pixel_variance16x8)
   1738         HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
   1739                     aom_highbd_obmc_variance8x8,
   1740                     aom_highbd_obmc_sub_pixel_variance8x8)
   1741         HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
   1742                     aom_highbd_obmc_variance4x8,
   1743                     aom_highbd_obmc_sub_pixel_variance4x8)
   1744         HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
   1745                     aom_highbd_obmc_variance8x4,
   1746                     aom_highbd_obmc_sub_pixel_variance8x4)
   1747         HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
   1748                     aom_highbd_obmc_variance4x4,
   1749                     aom_highbd_obmc_sub_pixel_variance4x4)
   1750         HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
   1751                     aom_highbd_obmc_variance64x16,
   1752                     aom_highbd_obmc_sub_pixel_variance64x16)
   1753         HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
   1754                     aom_highbd_obmc_variance16x64,
   1755                     aom_highbd_obmc_sub_pixel_variance16x64)
   1756         HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
   1757                     aom_highbd_obmc_variance32x8,
   1758                     aom_highbd_obmc_sub_pixel_variance32x8)
   1759         HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
   1760                     aom_highbd_obmc_variance8x32,
   1761                     aom_highbd_obmc_sub_pixel_variance8x32)
   1762         HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
   1763                     aom_highbd_obmc_variance16x4,
   1764                     aom_highbd_obmc_sub_pixel_variance16x4)
   1765         HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
   1766                     aom_highbd_obmc_variance4x16,
   1767                     aom_highbd_obmc_sub_pixel_variance4x16)
   1768         break;
   1769 
   1770       case AOM_BITS_10:
   1771         HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
   1772                    aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
   1773                    aom_highbd_10_sub_pixel_variance64x16,
   1774                    aom_highbd_10_sub_pixel_avg_variance64x16,
   1775                    aom_highbd_sad64x16x4d_bits10,
   1776                    aom_highbd_dist_wtd_sad64x16_avg_bits10,
   1777                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
   1778 
   1779         HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
   1780                    aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
   1781                    aom_highbd_10_sub_pixel_variance16x64,
   1782                    aom_highbd_10_sub_pixel_avg_variance16x64,
   1783                    aom_highbd_sad16x64x4d_bits10,
   1784                    aom_highbd_dist_wtd_sad16x64_avg_bits10,
   1785                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
   1786 
   1787         HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
   1788                    aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
   1789                    aom_highbd_10_sub_pixel_variance32x8,
   1790                    aom_highbd_10_sub_pixel_avg_variance32x8,
   1791                    aom_highbd_sad32x8x4d_bits10,
   1792                    aom_highbd_dist_wtd_sad32x8_avg_bits10,
   1793                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
   1794 
   1795         HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
   1796                    aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
   1797                    aom_highbd_10_sub_pixel_variance8x32,
   1798                    aom_highbd_10_sub_pixel_avg_variance8x32,
   1799                    aom_highbd_sad8x32x4d_bits10,
   1800                    aom_highbd_dist_wtd_sad8x32_avg_bits10,
   1801                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
   1802 
   1803         HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
   1804                    aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
   1805                    aom_highbd_10_sub_pixel_variance16x4,
   1806                    aom_highbd_10_sub_pixel_avg_variance16x4,
   1807                    aom_highbd_sad16x4x4d_bits10,
   1808                    aom_highbd_dist_wtd_sad16x4_avg_bits10,
   1809                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
   1810 
   1811         HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
   1812                    aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
   1813                    aom_highbd_10_sub_pixel_variance4x16,
   1814                    aom_highbd_10_sub_pixel_avg_variance4x16,
   1815                    aom_highbd_sad4x16x4d_bits10,
   1816                    aom_highbd_dist_wtd_sad4x16_avg_bits10,
   1817                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
   1818 
   1819         HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
   1820                    aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
   1821                    aom_highbd_10_sub_pixel_variance32x16,
   1822                    aom_highbd_10_sub_pixel_avg_variance32x16,
   1823                    aom_highbd_sad32x16x4d_bits10,
   1824                    aom_highbd_dist_wtd_sad32x16_avg_bits10,
   1825                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
   1826 
   1827         HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
   1828                    aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
   1829                    aom_highbd_10_sub_pixel_variance16x32,
   1830                    aom_highbd_10_sub_pixel_avg_variance16x32,
   1831                    aom_highbd_sad16x32x4d_bits10,
   1832                    aom_highbd_dist_wtd_sad16x32_avg_bits10,
   1833                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
   1834 
   1835         HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
   1836                    aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
   1837                    aom_highbd_10_sub_pixel_variance64x32,
   1838                    aom_highbd_10_sub_pixel_avg_variance64x32,
   1839                    aom_highbd_sad64x32x4d_bits10,
   1840                    aom_highbd_dist_wtd_sad64x32_avg_bits10,
   1841                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
   1842 
   1843         HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
   1844                    aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
   1845                    aom_highbd_10_sub_pixel_variance32x64,
   1846                    aom_highbd_10_sub_pixel_avg_variance32x64,
   1847                    aom_highbd_sad32x64x4d_bits10,
   1848                    aom_highbd_dist_wtd_sad32x64_avg_bits10,
   1849                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
   1850 
   1851         HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
   1852                    aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
   1853                    aom_highbd_10_sub_pixel_variance32x32,
   1854                    aom_highbd_10_sub_pixel_avg_variance32x32,
   1855                    aom_highbd_sad32x32x4d_bits10,
   1856                    aom_highbd_dist_wtd_sad32x32_avg_bits10,
   1857                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
   1858 
   1859         HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
   1860                    aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
   1861                    aom_highbd_10_sub_pixel_variance64x64,
   1862                    aom_highbd_10_sub_pixel_avg_variance64x64,
   1863                    aom_highbd_sad64x64x4d_bits10,
   1864                    aom_highbd_dist_wtd_sad64x64_avg_bits10,
   1865                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
   1866 
   1867         HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
   1868                    aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
   1869                    aom_highbd_10_sub_pixel_variance16x16,
   1870                    aom_highbd_10_sub_pixel_avg_variance16x16,
   1871                    aom_highbd_sad16x16x4d_bits10,
   1872                    aom_highbd_dist_wtd_sad16x16_avg_bits10,
   1873                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
   1874 
   1875         HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
   1876                    aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
   1877                    aom_highbd_10_sub_pixel_variance16x8,
   1878                    aom_highbd_10_sub_pixel_avg_variance16x8,
   1879                    aom_highbd_sad16x8x4d_bits10,
   1880                    aom_highbd_dist_wtd_sad16x8_avg_bits10,
   1881                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
   1882 
   1883         HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
   1884                    aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
   1885                    aom_highbd_10_sub_pixel_variance8x16,
   1886                    aom_highbd_10_sub_pixel_avg_variance8x16,
   1887                    aom_highbd_sad8x16x4d_bits10,
   1888                    aom_highbd_dist_wtd_sad8x16_avg_bits10,
   1889                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
   1890 
   1891         HIGHBD_BFP(
   1892             BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
   1893             aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
   1894             aom_highbd_10_sub_pixel_avg_variance8x8,
   1895             aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
   1896             aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
   1897 
   1898         HIGHBD_BFP(
   1899             BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
   1900             aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
   1901             aom_highbd_10_sub_pixel_avg_variance8x4,
   1902             aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
   1903             aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
   1904 
   1905         HIGHBD_BFP(
   1906             BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
   1907             aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
   1908             aom_highbd_10_sub_pixel_avg_variance4x8,
   1909             aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
   1910             aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
   1911 
   1912         HIGHBD_BFP(
   1913             BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
   1914             aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
   1915             aom_highbd_10_sub_pixel_avg_variance4x4,
   1916             aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
   1917             aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
   1918 
   1919         HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
   1920                    aom_highbd_sad128x128_avg_bits10,
   1921                    aom_highbd_10_variance128x128,
   1922                    aom_highbd_10_sub_pixel_variance128x128,
   1923                    aom_highbd_10_sub_pixel_avg_variance128x128,
   1924                    aom_highbd_sad128x128x4d_bits10,
   1925                    aom_highbd_dist_wtd_sad128x128_avg_bits10,
   1926                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
   1927 
   1928         HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
   1929                    aom_highbd_sad128x64_avg_bits10,
   1930                    aom_highbd_10_variance128x64,
   1931                    aom_highbd_10_sub_pixel_variance128x64,
   1932                    aom_highbd_10_sub_pixel_avg_variance128x64,
   1933                    aom_highbd_sad128x64x4d_bits10,
   1934                    aom_highbd_dist_wtd_sad128x64_avg_bits10,
   1935                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
   1936 
   1937         HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
   1938                    aom_highbd_sad64x128_avg_bits10,
   1939                    aom_highbd_10_variance64x128,
   1940                    aom_highbd_10_sub_pixel_variance64x128,
   1941                    aom_highbd_10_sub_pixel_avg_variance64x128,
   1942                    aom_highbd_sad64x128x4d_bits10,
   1943                    aom_highbd_dist_wtd_sad64x128_avg_bits10,
   1944                    aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
   1945 
   1946         HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
   1947                     aom_highbd_10_masked_sub_pixel_variance128x128)
   1948         HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
   1949                     aom_highbd_10_masked_sub_pixel_variance128x64)
   1950         HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
   1951                     aom_highbd_10_masked_sub_pixel_variance64x128)
   1952         HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
   1953                     aom_highbd_10_masked_sub_pixel_variance64x64)
   1954         HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
   1955                     aom_highbd_10_masked_sub_pixel_variance64x32)
   1956         HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
   1957                     aom_highbd_10_masked_sub_pixel_variance32x64)
   1958         HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
   1959                     aom_highbd_10_masked_sub_pixel_variance32x32)
   1960         HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
   1961                     aom_highbd_10_masked_sub_pixel_variance32x16)
   1962         HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
   1963                     aom_highbd_10_masked_sub_pixel_variance16x32)
   1964         HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
   1965                     aom_highbd_10_masked_sub_pixel_variance16x16)
   1966         HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
   1967                     aom_highbd_10_masked_sub_pixel_variance8x16)
   1968         HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
   1969                     aom_highbd_10_masked_sub_pixel_variance16x8)
   1970         HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
   1971                     aom_highbd_10_masked_sub_pixel_variance8x8)
   1972         HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
   1973                     aom_highbd_10_masked_sub_pixel_variance4x8)
   1974         HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
   1975                     aom_highbd_10_masked_sub_pixel_variance8x4)
   1976         HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
   1977                     aom_highbd_10_masked_sub_pixel_variance4x4)
   1978         HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
   1979                     aom_highbd_10_masked_sub_pixel_variance64x16)
   1980         HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
   1981                     aom_highbd_10_masked_sub_pixel_variance16x64)
   1982         HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
   1983                     aom_highbd_10_masked_sub_pixel_variance32x8)
   1984         HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
   1985                     aom_highbd_10_masked_sub_pixel_variance8x32)
   1986         HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
   1987                     aom_highbd_10_masked_sub_pixel_variance16x4)
   1988         HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
   1989                     aom_highbd_10_masked_sub_pixel_variance4x16)
   1990         HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
   1991                     aom_highbd_10_obmc_variance128x128,
   1992                     aom_highbd_10_obmc_sub_pixel_variance128x128)
   1993         HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
   1994                     aom_highbd_10_obmc_variance128x64,
   1995                     aom_highbd_10_obmc_sub_pixel_variance128x64)
   1996         HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
   1997                     aom_highbd_10_obmc_variance64x128,
   1998                     aom_highbd_10_obmc_sub_pixel_variance64x128)
   1999         HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
   2000                     aom_highbd_10_obmc_variance64x64,
   2001                     aom_highbd_10_obmc_sub_pixel_variance64x64)
   2002         HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
   2003                     aom_highbd_10_obmc_variance64x32,
   2004                     aom_highbd_10_obmc_sub_pixel_variance64x32)
   2005         HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
   2006                     aom_highbd_10_obmc_variance32x64,
   2007                     aom_highbd_10_obmc_sub_pixel_variance32x64)
   2008         HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
   2009                     aom_highbd_10_obmc_variance32x32,
   2010                     aom_highbd_10_obmc_sub_pixel_variance32x32)
   2011         HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
   2012                     aom_highbd_10_obmc_variance32x16,
   2013                     aom_highbd_10_obmc_sub_pixel_variance32x16)
   2014         HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
   2015                     aom_highbd_10_obmc_variance16x32,
   2016                     aom_highbd_10_obmc_sub_pixel_variance16x32)
   2017         HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
   2018                     aom_highbd_10_obmc_variance16x16,
   2019                     aom_highbd_10_obmc_sub_pixel_variance16x16)
   2020         HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
   2021                     aom_highbd_10_obmc_variance8x16,
   2022                     aom_highbd_10_obmc_sub_pixel_variance8x16)
   2023         HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
   2024                     aom_highbd_10_obmc_variance16x8,
   2025                     aom_highbd_10_obmc_sub_pixel_variance16x8)
   2026         HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
   2027                     aom_highbd_10_obmc_variance8x8,
   2028                     aom_highbd_10_obmc_sub_pixel_variance8x8)
   2029         HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
   2030                     aom_highbd_10_obmc_variance4x8,
   2031                     aom_highbd_10_obmc_sub_pixel_variance4x8)
   2032         HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
   2033                     aom_highbd_10_obmc_variance8x4,
   2034                     aom_highbd_10_obmc_sub_pixel_variance8x4)
   2035         HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
   2036                     aom_highbd_10_obmc_variance4x4,
   2037                     aom_highbd_10_obmc_sub_pixel_variance4x4)
   2038 
   2039         HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
   2040                     aom_highbd_10_obmc_variance64x16,
   2041                     aom_highbd_10_obmc_sub_pixel_variance64x16)
   2042 
   2043         HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
   2044                     aom_highbd_10_obmc_variance16x64,
   2045                     aom_highbd_10_obmc_sub_pixel_variance16x64)
   2046 
   2047         HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
   2048                     aom_highbd_10_obmc_variance32x8,
   2049                     aom_highbd_10_obmc_sub_pixel_variance32x8)
   2050 
   2051         HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
   2052                     aom_highbd_10_obmc_variance8x32,
   2053                     aom_highbd_10_obmc_sub_pixel_variance8x32)
   2054 
   2055         HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
   2056                     aom_highbd_10_obmc_variance16x4,
   2057                     aom_highbd_10_obmc_sub_pixel_variance16x4)
   2058 
   2059         HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
   2060                     aom_highbd_10_obmc_variance4x16,
   2061                     aom_highbd_10_obmc_sub_pixel_variance4x16)
   2062         break;
   2063 
   2064       case AOM_BITS_12:
   2065         HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
   2066                    aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
   2067                    aom_highbd_12_sub_pixel_variance64x16,
   2068                    aom_highbd_12_sub_pixel_avg_variance64x16,
   2069                    aom_highbd_sad64x16x4d_bits12,
   2070                    aom_highbd_dist_wtd_sad64x16_avg_bits12,
   2071                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
   2072 
   2073         HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
   2074                    aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
   2075                    aom_highbd_12_sub_pixel_variance16x64,
   2076                    aom_highbd_12_sub_pixel_avg_variance16x64,
   2077                    aom_highbd_sad16x64x4d_bits12,
   2078                    aom_highbd_dist_wtd_sad16x64_avg_bits12,
   2079                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
   2080 
   2081         HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
   2082                    aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
   2083                    aom_highbd_12_sub_pixel_variance32x8,
   2084                    aom_highbd_12_sub_pixel_avg_variance32x8,
   2085                    aom_highbd_sad32x8x4d_bits12,
   2086                    aom_highbd_dist_wtd_sad32x8_avg_bits12,
   2087                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
   2088 
   2089         HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
   2090                    aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
   2091                    aom_highbd_12_sub_pixel_variance8x32,
   2092                    aom_highbd_12_sub_pixel_avg_variance8x32,
   2093                    aom_highbd_sad8x32x4d_bits12,
   2094                    aom_highbd_dist_wtd_sad8x32_avg_bits12,
   2095                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
   2096 
   2097         HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
   2098                    aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
   2099                    aom_highbd_12_sub_pixel_variance16x4,
   2100                    aom_highbd_12_sub_pixel_avg_variance16x4,
   2101                    aom_highbd_sad16x4x4d_bits12,
   2102                    aom_highbd_dist_wtd_sad16x4_avg_bits12,
   2103                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
   2104 
   2105         HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
   2106                    aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
   2107                    aom_highbd_12_sub_pixel_variance4x16,
   2108                    aom_highbd_12_sub_pixel_avg_variance4x16,
   2109                    aom_highbd_sad4x16x4d_bits12,
   2110                    aom_highbd_dist_wtd_sad4x16_avg_bits12,
   2111                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
   2112 
   2113         HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
   2114                    aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
   2115                    aom_highbd_12_sub_pixel_variance32x16,
   2116                    aom_highbd_12_sub_pixel_avg_variance32x16,
   2117                    aom_highbd_sad32x16x4d_bits12,
   2118                    aom_highbd_dist_wtd_sad32x16_avg_bits12,
   2119                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
   2120 
   2121         HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
   2122                    aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
   2123                    aom_highbd_12_sub_pixel_variance16x32,
   2124                    aom_highbd_12_sub_pixel_avg_variance16x32,
   2125                    aom_highbd_sad16x32x4d_bits12,
   2126                    aom_highbd_dist_wtd_sad16x32_avg_bits12,
   2127                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
   2128 
   2129         HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
   2130                    aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
   2131                    aom_highbd_12_sub_pixel_variance64x32,
   2132                    aom_highbd_12_sub_pixel_avg_variance64x32,
   2133                    aom_highbd_sad64x32x4d_bits12,
   2134                    aom_highbd_dist_wtd_sad64x32_avg_bits12,
   2135                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
   2136 
   2137         HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
   2138                    aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
   2139                    aom_highbd_12_sub_pixel_variance32x64,
   2140                    aom_highbd_12_sub_pixel_avg_variance32x64,
   2141                    aom_highbd_sad32x64x4d_bits12,
   2142                    aom_highbd_dist_wtd_sad32x64_avg_bits12,
   2143                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
   2144 
   2145         HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
   2146                    aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
   2147                    aom_highbd_12_sub_pixel_variance32x32,
   2148                    aom_highbd_12_sub_pixel_avg_variance32x32,
   2149                    aom_highbd_sad32x32x4d_bits12,
   2150                    aom_highbd_dist_wtd_sad32x32_avg_bits12,
   2151                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
   2152 
   2153         HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
   2154                    aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
   2155                    aom_highbd_12_sub_pixel_variance64x64,
   2156                    aom_highbd_12_sub_pixel_avg_variance64x64,
   2157                    aom_highbd_sad64x64x4d_bits12,
   2158                    aom_highbd_dist_wtd_sad64x64_avg_bits12,
   2159                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
   2160 
   2161         HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
   2162                    aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
   2163                    aom_highbd_12_sub_pixel_variance16x16,
   2164                    aom_highbd_12_sub_pixel_avg_variance16x16,
   2165                    aom_highbd_sad16x16x4d_bits12,
   2166                    aom_highbd_dist_wtd_sad16x16_avg_bits12,
   2167                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
   2168 
   2169         HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
   2170                    aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
   2171                    aom_highbd_12_sub_pixel_variance16x8,
   2172                    aom_highbd_12_sub_pixel_avg_variance16x8,
   2173                    aom_highbd_sad16x8x4d_bits12,
   2174                    aom_highbd_dist_wtd_sad16x8_avg_bits12,
   2175                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
   2176 
   2177         HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
   2178                    aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
   2179                    aom_highbd_12_sub_pixel_variance8x16,
   2180                    aom_highbd_12_sub_pixel_avg_variance8x16,
   2181                    aom_highbd_sad8x16x4d_bits12,
   2182                    aom_highbd_dist_wtd_sad8x16_avg_bits12,
   2183                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
   2184 
   2185         HIGHBD_BFP(
   2186             BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
   2187             aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
   2188             aom_highbd_12_sub_pixel_avg_variance8x8,
   2189             aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
   2190             aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
   2191 
   2192         HIGHBD_BFP(
   2193             BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
   2194             aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
   2195             aom_highbd_12_sub_pixel_avg_variance8x4,
   2196             aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
   2197             aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
   2198 
   2199         HIGHBD_BFP(
   2200             BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
   2201             aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
   2202             aom_highbd_12_sub_pixel_avg_variance4x8,
   2203             aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
   2204             aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
   2205 
   2206         HIGHBD_BFP(
   2207             BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
   2208             aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
   2209             aom_highbd_12_sub_pixel_avg_variance4x4,
   2210             aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
   2211             aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
   2212 
   2213         HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
   2214                    aom_highbd_sad128x128_avg_bits12,
   2215                    aom_highbd_12_variance128x128,
   2216                    aom_highbd_12_sub_pixel_variance128x128,
   2217                    aom_highbd_12_sub_pixel_avg_variance128x128,
   2218                    aom_highbd_sad128x128x4d_bits12,
   2219                    aom_highbd_dist_wtd_sad128x128_avg_bits12,
   2220                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
   2221 
   2222         HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
   2223                    aom_highbd_sad128x64_avg_bits12,
   2224                    aom_highbd_12_variance128x64,
   2225                    aom_highbd_12_sub_pixel_variance128x64,
   2226                    aom_highbd_12_sub_pixel_avg_variance128x64,
   2227                    aom_highbd_sad128x64x4d_bits12,
   2228                    aom_highbd_dist_wtd_sad128x64_avg_bits12,
   2229                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
   2230 
   2231         HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
   2232                    aom_highbd_sad64x128_avg_bits12,
   2233                    aom_highbd_12_variance64x128,
   2234                    aom_highbd_12_sub_pixel_variance64x128,
   2235                    aom_highbd_12_sub_pixel_avg_variance64x128,
   2236                    aom_highbd_sad64x128x4d_bits12,
   2237                    aom_highbd_dist_wtd_sad64x128_avg_bits12,
   2238                    aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
   2239 
   2240         HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
   2241                     aom_highbd_12_masked_sub_pixel_variance128x128)
   2242         HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
   2243                     aom_highbd_12_masked_sub_pixel_variance128x64)
   2244         HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
   2245                     aom_highbd_12_masked_sub_pixel_variance64x128)
   2246         HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
   2247                     aom_highbd_12_masked_sub_pixel_variance64x64)
   2248         HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
   2249                     aom_highbd_12_masked_sub_pixel_variance64x32)
   2250         HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
   2251                     aom_highbd_12_masked_sub_pixel_variance32x64)
   2252         HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
   2253                     aom_highbd_12_masked_sub_pixel_variance32x32)
   2254         HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
   2255                     aom_highbd_12_masked_sub_pixel_variance32x16)
   2256         HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
   2257                     aom_highbd_12_masked_sub_pixel_variance16x32)
   2258         HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
   2259                     aom_highbd_12_masked_sub_pixel_variance16x16)
   2260         HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
   2261                     aom_highbd_12_masked_sub_pixel_variance8x16)
   2262         HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
   2263                     aom_highbd_12_masked_sub_pixel_variance16x8)
   2264         HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
   2265                     aom_highbd_12_masked_sub_pixel_variance8x8)
   2266         HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
   2267                     aom_highbd_12_masked_sub_pixel_variance4x8)
   2268         HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
   2269                     aom_highbd_12_masked_sub_pixel_variance8x4)
   2270         HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
   2271                     aom_highbd_12_masked_sub_pixel_variance4x4)
   2272         HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
   2273                     aom_highbd_12_masked_sub_pixel_variance64x16)
   2274         HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
   2275                     aom_highbd_12_masked_sub_pixel_variance16x64)
   2276         HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
   2277                     aom_highbd_12_masked_sub_pixel_variance32x8)
   2278         HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
   2279                     aom_highbd_12_masked_sub_pixel_variance8x32)
   2280         HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
   2281                     aom_highbd_12_masked_sub_pixel_variance16x4)
   2282         HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
   2283                     aom_highbd_12_masked_sub_pixel_variance4x16)
   2284         HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
   2285                     aom_highbd_12_obmc_variance128x128,
   2286                     aom_highbd_12_obmc_sub_pixel_variance128x128)
   2287         HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
   2288                     aom_highbd_12_obmc_variance128x64,
   2289                     aom_highbd_12_obmc_sub_pixel_variance128x64)
   2290         HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
   2291                     aom_highbd_12_obmc_variance64x128,
   2292                     aom_highbd_12_obmc_sub_pixel_variance64x128)
   2293         HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
   2294                     aom_highbd_12_obmc_variance64x64,
   2295                     aom_highbd_12_obmc_sub_pixel_variance64x64)
   2296         HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
   2297                     aom_highbd_12_obmc_variance64x32,
   2298                     aom_highbd_12_obmc_sub_pixel_variance64x32)
   2299         HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
   2300                     aom_highbd_12_obmc_variance32x64,
   2301                     aom_highbd_12_obmc_sub_pixel_variance32x64)
   2302         HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
   2303                     aom_highbd_12_obmc_variance32x32,
   2304                     aom_highbd_12_obmc_sub_pixel_variance32x32)
   2305         HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
   2306                     aom_highbd_12_obmc_variance32x16,
   2307                     aom_highbd_12_obmc_sub_pixel_variance32x16)
   2308         HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
   2309                     aom_highbd_12_obmc_variance16x32,
   2310                     aom_highbd_12_obmc_sub_pixel_variance16x32)
   2311         HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
   2312                     aom_highbd_12_obmc_variance16x16,
   2313                     aom_highbd_12_obmc_sub_pixel_variance16x16)
   2314         HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
   2315                     aom_highbd_12_obmc_variance8x16,
   2316                     aom_highbd_12_obmc_sub_pixel_variance8x16)
   2317         HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
   2318                     aom_highbd_12_obmc_variance16x8,
   2319                     aom_highbd_12_obmc_sub_pixel_variance16x8)
   2320         HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
   2321                     aom_highbd_12_obmc_variance8x8,
   2322                     aom_highbd_12_obmc_sub_pixel_variance8x8)
   2323         HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
   2324                     aom_highbd_12_obmc_variance4x8,
   2325                     aom_highbd_12_obmc_sub_pixel_variance4x8)
   2326         HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
   2327                     aom_highbd_12_obmc_variance8x4,
   2328                     aom_highbd_12_obmc_sub_pixel_variance8x4)
   2329         HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
   2330                     aom_highbd_12_obmc_variance4x4,
   2331                     aom_highbd_12_obmc_sub_pixel_variance4x4)
   2332         HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
   2333                     aom_highbd_12_obmc_variance64x16,
   2334                     aom_highbd_12_obmc_sub_pixel_variance64x16)
   2335         HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
   2336                     aom_highbd_12_obmc_variance16x64,
   2337                     aom_highbd_12_obmc_sub_pixel_variance16x64)
   2338         HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
   2339                     aom_highbd_12_obmc_variance32x8,
   2340                     aom_highbd_12_obmc_sub_pixel_variance32x8)
   2341         HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
   2342                     aom_highbd_12_obmc_variance8x32,
   2343                     aom_highbd_12_obmc_sub_pixel_variance8x32)
   2344         HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
   2345                     aom_highbd_12_obmc_variance16x4,
   2346                     aom_highbd_12_obmc_sub_pixel_variance16x4)
   2347         HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
   2348                     aom_highbd_12_obmc_variance4x16,
   2349                     aom_highbd_12_obmc_sub_pixel_variance4x16)
   2350         break;
   2351 
   2352       default:
   2353         assert(0 &&
   2354                "cm->seq_params.bit_depth should be AOM_BITS_8, "
   2355                "AOM_BITS_10 or AOM_BITS_12");
   2356     }
   2357   }
   2358 }
   2359 
   2360 static void realloc_segmentation_maps(AV1_COMP *cpi) {
   2361   AV1_COMMON *const cm = &cpi->common;
   2362 
   2363   // Create the encoder segmentation map and set all entries to 0
   2364   aom_free(cpi->segmentation_map);
   2365   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
   2366                   aom_calloc(cm->mi_rows * cm->mi_cols, 1));
   2367 
   2368   // Create a map used for cyclic background refresh.
   2369   if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
   2370   CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
   2371                   av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
   2372 
   2373   // Create a map used to mark inactive areas.
   2374   aom_free(cpi->active_map.map);
   2375   CHECK_MEM_ERROR(cm, cpi->active_map.map,
   2376                   aom_calloc(cm->mi_rows * cm->mi_cols, 1));
   2377 }
   2378 
   2379 void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
   2380   AV1_COMMON *const cm = &cpi->common;
   2381   SequenceHeader *const seq_params = &cm->seq_params;
   2382   const int num_planes = av1_num_planes(cm);
   2383   RATE_CONTROL *const rc = &cpi->rc;
   2384   MACROBLOCK *const x = &cpi->td.mb;
   2385 
   2386   if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
   2387   seq_params->bit_depth = oxcf->bit_depth;
   2388   seq_params->color_primaries = oxcf->color_primaries;
   2389   seq_params->transfer_characteristics = oxcf->transfer_characteristics;
   2390   seq_params->matrix_coefficients = oxcf->matrix_coefficients;
   2391   seq_params->monochrome = oxcf->monochrome;
   2392   seq_params->chroma_sample_position = oxcf->chroma_sample_position;
   2393   seq_params->color_range = oxcf->color_range;
   2394 
   2395   assert(IMPLIES(seq_params->profile <= PROFILE_1,
   2396                  seq_params->bit_depth <= AOM_BITS_10));
   2397 
   2398   memcpy(cpi->target_seq_level_idx, oxcf->target_seq_level_idx,
   2399          sizeof(cpi->target_seq_level_idx));
   2400   cpi->keep_level_stats = 0;
   2401   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
   2402     if (cpi->target_seq_level_idx[i] < SEQ_LEVELS) {
   2403       cpi->keep_level_stats = 1;
   2404       break;
   2405     }
   2406   }
   2407 
   2408   cm->timing_info_present = oxcf->timing_info_present;
   2409   cm->timing_info.num_units_in_display_tick =
   2410       oxcf->timing_info.num_units_in_display_tick;
   2411   cm->timing_info.time_scale = oxcf->timing_info.time_scale;
   2412   cm->timing_info.equal_picture_interval =
   2413       oxcf->timing_info.equal_picture_interval;
   2414   cm->timing_info.num_ticks_per_picture =
   2415       oxcf->timing_info.num_ticks_per_picture;
   2416 
   2417   seq_params->display_model_info_present_flag =
   2418       oxcf->display_model_info_present_flag;
   2419   seq_params->decoder_model_info_present_flag =
   2420       oxcf->decoder_model_info_present_flag;
   2421   if (oxcf->decoder_model_info_present_flag) {
   2422     // set the decoder model parameters in schedule mode
   2423     cm->buffer_model.num_units_in_decoding_tick =
   2424         oxcf->buffer_model.num_units_in_decoding_tick;
   2425     cm->buffer_removal_time_present = 1;
   2426     set_aom_dec_model_info(&cm->buffer_model);
   2427     set_dec_model_op_parameters(&cm->op_params[0]);
   2428   } else if (cm->timing_info_present &&
   2429              cm->timing_info.equal_picture_interval &&
   2430              !seq_params->decoder_model_info_present_flag) {
   2431     // set the decoder model parameters in resource availability mode
   2432     set_resource_availability_parameters(&cm->op_params[0]);
   2433   } else {
   2434     cm->op_params[0].initial_display_delay =
   2435         10;  // Default value (not signaled)
   2436   }
   2437 
   2438   update_film_grain_parameters(cpi, oxcf);
   2439 
   2440   cpi->oxcf = *oxcf;
   2441   cpi->common.options = oxcf->cfg;
   2442   x->e_mbd.bd = (int)seq_params->bit_depth;
   2443   x->e_mbd.global_motion = cm->global_motion;
   2444 
   2445   if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
   2446     rc->baseline_gf_interval = FIXED_GF_INTERVAL;
   2447   } else {
   2448     rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
   2449   }
   2450 
   2451   cpi->refresh_last_frame = 1;
   2452   cpi->refresh_golden_frame = 0;
   2453   cpi->refresh_bwd_ref_frame = 0;
   2454   cpi->refresh_alt2_ref_frame = 0;
   2455 
   2456   cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
   2457                                   ? REFRESH_FRAME_CONTEXT_DISABLED
   2458                                   : REFRESH_FRAME_CONTEXT_BACKWARD;
   2459   if (oxcf->large_scale_tile)
   2460     cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
   2461 
   2462   if (x->palette_buffer == NULL) {
   2463     CHECK_MEM_ERROR(cm, x->palette_buffer,
   2464                     aom_memalign(16, sizeof(*x->palette_buffer)));
   2465   }
   2466 
   2467   if (x->tmp_conv_dst == NULL) {
   2468     CHECK_MEM_ERROR(
   2469         cm, x->tmp_conv_dst,
   2470         aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
   2471     x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
   2472   }
   2473   for (int i = 0; i < 2; ++i) {
   2474     if (x->tmp_obmc_bufs[i] == NULL) {
   2475       CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
   2476                       aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
   2477                                            sizeof(*x->tmp_obmc_bufs[i])));
   2478       x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
   2479     }
   2480   }
   2481 
   2482   av1_reset_segment_features(cm);
   2483   set_high_precision_mv(cpi, 1, 0);
   2484 
   2485   set_rc_buffer_sizes(rc, &cpi->oxcf);
   2486 
   2487   // Under a configuration change, where maximum_buffer_size may change,
   2488   // keep buffer level clipped to the maximum allowed buffer size.
   2489   rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
   2490   rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
   2491 
   2492   // Set up frame rate and related parameters rate control values.
   2493   av1_new_framerate(cpi, cpi->framerate);
   2494 
   2495   // Set absolute upper and lower quality limits
   2496   rc->worst_quality = cpi->oxcf.worst_allowed_q;
   2497   rc->best_quality = cpi->oxcf.best_allowed_q;
   2498 
   2499   cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
   2500   cm->switchable_motion_mode = 1;
   2501 
   2502   if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
   2503     cm->render_width = cpi->oxcf.render_width;
   2504     cm->render_height = cpi->oxcf.render_height;
   2505   } else {
   2506     cm->render_width = cpi->oxcf.width;
   2507     cm->render_height = cpi->oxcf.height;
   2508   }
   2509   cm->width = cpi->oxcf.width;
   2510   cm->height = cpi->oxcf.height;
   2511 
   2512   int sb_size = seq_params->sb_size;
   2513   // Superblock size should not be updated after the first key frame.
   2514   if (!cpi->seq_params_locked) {
   2515     set_sb_size(&cm->seq_params, select_sb_size(cpi));
   2516     for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
   2517       seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
   2518   }
   2519 
   2520   if (cpi->initial_width || sb_size != seq_params->sb_size) {
   2521     if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
   2522         seq_params->sb_size != sb_size) {
   2523       av1_free_context_buffers(cm);
   2524       av1_free_pc_tree(&cpi->td, num_planes);
   2525       alloc_compressor_data(cpi);
   2526       realloc_segmentation_maps(cpi);
   2527       cpi->initial_width = cpi->initial_height = 0;
   2528     }
   2529   }
   2530   update_frame_size(cpi);
   2531 
   2532   cpi->alt_ref_source = NULL;
   2533   rc->is_src_frame_alt_ref = 0;
   2534 
   2535   set_tile_info(cpi);
   2536 
   2537   cpi->ext_refresh_frame_flags_pending = 0;
   2538   cpi->ext_refresh_frame_context_pending = 0;
   2539 
   2540   highbd_set_var_fns(cpi);
   2541 
   2542   // Init sequence level coding tools
   2543   // This should not be called after the first key frame.
   2544   if (!cpi->seq_params_locked) {
   2545     seq_params->operating_points_cnt_minus_1 =
   2546         cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
   2547     init_seq_coding_tools(&cm->seq_params, cm, oxcf);
   2548   }
   2549 }
   2550 
   2551 static void init_level_info(AV1LevelInfo *level_info) {
   2552   memset(level_info, 0, MAX_NUM_OPERATING_POINTS * sizeof(*level_info));
   2553   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
   2554     AV1LevelSpec *const level_spec = &level_info[i].level_spec;
   2555     level_spec->level = SEQ_LEVEL_MAX;
   2556     AV1LevelStats *const level_stats = &level_info[i].level_stats;
   2557     level_stats->min_cropped_tile_width = INT_MAX;
   2558     level_stats->min_cropped_tile_height = INT_MAX;
   2559     level_stats->min_frame_width = INT_MAX;
   2560     level_stats->min_frame_height = INT_MAX;
   2561     level_stats->tile_width_is_valid = 1;
   2562     level_stats->min_cr = 1e8;
   2563   }
   2564 }
   2565 
   2566 AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
   2567                                 BufferPool *const pool) {
   2568   unsigned int i;
   2569   AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
   2570   AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
   2571 
   2572   if (!cm) return NULL;
   2573 
   2574   av1_zero(*cpi);
   2575 
   2576   // The jmp_buf is valid only for the duration of the function that calls
   2577   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
   2578   // before it returns.
   2579   if (setjmp(cm->error.jmp)) {
   2580     cm->error.setjmp = 0;
   2581     av1_remove_compressor(cpi);
   2582     return 0;
   2583   }
   2584 
   2585   cm->error.setjmp = 1;
   2586   cm->alloc_mi = enc_alloc_mi;
   2587   cm->free_mi = enc_free_mi;
   2588   cm->setup_mi = enc_setup_mi;
   2589 
   2590   CHECK_MEM_ERROR(cm, cm->fc,
   2591                   (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
   2592   CHECK_MEM_ERROR(
   2593       cm, cm->default_frame_context,
   2594       (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
   2595   memset(cm->fc, 0, sizeof(*cm->fc));
   2596   memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
   2597 
   2598   cpi->resize_state = 0;
   2599   cpi->resize_avg_qp = 0;
   2600   cpi->resize_buffer_underflow = 0;
   2601 
   2602   cpi->common.buffer_pool = pool;
   2603 
   2604   init_config(cpi, oxcf);
   2605   av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
   2606 
   2607   cm->current_frame.frame_number = 0;
   2608   cm->current_frame_id = -1;
   2609   cpi->seq_params_locked = 0;
   2610   cpi->partition_search_skippable_frame = 0;
   2611   cpi->tile_data = NULL;
   2612   cpi->last_show_frame_buf = NULL;
   2613   realloc_segmentation_maps(cpi);
   2614 
   2615   memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
   2616   memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
   2617 
   2618   for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
   2619        i++) {
   2620     CHECK_MEM_ERROR(
   2621         cm, cpi->mbgraph_stats[i].mb_stats,
   2622         aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
   2623   }
   2624 
   2625   cpi->refresh_alt_ref_frame = 0;
   2626 
   2627   init_level_info(cpi->level_info);
   2628 
   2629   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
   2630 #if CONFIG_INTERNAL_STATS
   2631   cpi->b_calculate_blockiness = 1;
   2632   cpi->b_calculate_consistency = 1;
   2633   cpi->total_inconsistency = 0;
   2634   cpi->psnr.worst = 100.0;
   2635   cpi->worst_ssim = 100.0;
   2636 
   2637   cpi->count = 0;
   2638   cpi->bytes = 0;
   2639 #if CONFIG_SPEED_STATS
   2640   cpi->tx_search_count = 0;
   2641 #endif  // CONFIG_SPEED_STATS
   2642 
   2643   if (cpi->b_calculate_psnr) {
   2644     cpi->total_sq_error = 0;
   2645     cpi->total_samples = 0;
   2646     cpi->tot_recode_hits = 0;
   2647     cpi->summed_quality = 0;
   2648     cpi->summed_weights = 0;
   2649   }
   2650 
   2651   cpi->fastssim.worst = 100.0;
   2652   cpi->psnrhvs.worst = 100.0;
   2653 
   2654   if (cpi->b_calculate_blockiness) {
   2655     cpi->total_blockiness = 0;
   2656     cpi->worst_blockiness = 0.0;
   2657   }
   2658 
   2659   if (cpi->b_calculate_consistency) {
   2660     CHECK_MEM_ERROR(cm, cpi->ssim_vars,
   2661                     aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
   2662                                cpi->common.mi_rows * cpi->common.mi_cols));
   2663     cpi->worst_consistency = 100.0;
   2664   }
   2665 #endif
   2666 #if CONFIG_ENTROPY_STATS
   2667   av1_zero(aggregate_fc);
   2668 #endif  // CONFIG_ENTROPY_STATS
   2669 
   2670   cpi->first_time_stamp_ever = INT64_MAX;
   2671 
   2672   cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
   2673   cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
   2674   cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
   2675   cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
   2676 
   2677 #ifdef OUTPUT_YUV_SKINMAP
   2678   yuv_skinmap_file = fopen("skinmap.yuv", "ab");
   2679 #endif
   2680 #ifdef OUTPUT_YUV_REC
   2681   yuv_rec_file = fopen("rec.yuv", "wb");
   2682 #endif
   2683 
   2684   if (oxcf->pass == 1) {
   2685     av1_init_first_pass(cpi);
   2686   } else if (oxcf->pass == 2) {
   2687     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
   2688     const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
   2689 
   2690     cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
   2691     cpi->twopass.stats_in = cpi->twopass.stats_in_start;
   2692     cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
   2693 
   2694     av1_init_second_pass(cpi);
   2695   }
   2696 
   2697   CHECK_MEM_ERROR(
   2698       cm, cpi->td.mb.above_pred_buf,
   2699       (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
   2700                                       sizeof(*cpi->td.mb.above_pred_buf)));
   2701   CHECK_MEM_ERROR(
   2702       cm, cpi->td.mb.left_pred_buf,
   2703       (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
   2704                                       sizeof(*cpi->td.mb.left_pred_buf)));
   2705 
   2706   CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
   2707                   (int32_t *)aom_memalign(
   2708                       16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
   2709 
   2710   CHECK_MEM_ERROR(
   2711       cm, cpi->td.mb.inter_modes_info,
   2712       (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
   2713 
   2714   for (int x = 0; x < 2; x++)
   2715     for (int y = 0; y < 2; y++)
   2716       CHECK_MEM_ERROR(
   2717           cm, cpi->td.mb.hash_value_buffer[x][y],
   2718           (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
   2719                                  sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
   2720 
   2721   cpi->td.mb.g_crc_initialized = 0;
   2722 
   2723   CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
   2724                   (int32_t *)aom_memalign(
   2725                       16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
   2726 
   2727   av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
   2728   av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
   2729 
   2730   for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
   2731     int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
   2732     int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
   2733 
   2734     CHECK_MEM_ERROR(cm, cpi->tpl_stats[frame].tpl_stats_ptr,
   2735                     aom_calloc(mi_rows * mi_cols,
   2736                                sizeof(*cpi->tpl_stats[frame].tpl_stats_ptr)));
   2737     cpi->tpl_stats[frame].is_valid = 0;
   2738     cpi->tpl_stats[frame].width = mi_cols;
   2739     cpi->tpl_stats[frame].height = mi_rows;
   2740     cpi->tpl_stats[frame].stride = mi_cols;
   2741     cpi->tpl_stats[frame].mi_rows = cm->mi_rows;
   2742     cpi->tpl_stats[frame].mi_cols = cm->mi_cols;
   2743   }
   2744 
   2745 #if CONFIG_COLLECT_PARTITION_STATS == 2
   2746   av1_zero(cpi->partition_stats);
   2747 #endif
   2748 
   2749 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
   2750   cpi->fn_ptr[BT].sdf = SDF;                                    \
   2751   cpi->fn_ptr[BT].sdaf = SDAF;                                  \
   2752   cpi->fn_ptr[BT].vf = VF;                                      \
   2753   cpi->fn_ptr[BT].svf = SVF;                                    \
   2754   cpi->fn_ptr[BT].svaf = SVAF;                                  \
   2755   cpi->fn_ptr[BT].sdx4df = SDX4DF;                              \
   2756   cpi->fn_ptr[BT].jsdaf = JSDAF;                                \
   2757   cpi->fn_ptr[BT].jsvaf = JSVAF;
   2758 
   2759   BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
   2760       aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
   2761       aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
   2762       aom_dist_wtd_sub_pixel_avg_variance4x16)
   2763 
   2764   BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
   2765       aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
   2766       aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
   2767       aom_dist_wtd_sub_pixel_avg_variance16x4)
   2768 
   2769   BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
   2770       aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
   2771       aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
   2772       aom_dist_wtd_sub_pixel_avg_variance8x32)
   2773 
   2774   BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
   2775       aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
   2776       aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
   2777       aom_dist_wtd_sub_pixel_avg_variance32x8)
   2778 
   2779   BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
   2780       aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
   2781       aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
   2782       aom_dist_wtd_sub_pixel_avg_variance16x64)
   2783 
   2784   BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
   2785       aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
   2786       aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
   2787       aom_dist_wtd_sub_pixel_avg_variance64x16)
   2788 
   2789   BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
   2790       aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
   2791       aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
   2792       aom_dist_wtd_sub_pixel_avg_variance128x128)
   2793 
   2794   BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
   2795       aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
   2796       aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
   2797       aom_dist_wtd_sub_pixel_avg_variance128x64)
   2798 
   2799   BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
   2800       aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
   2801       aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
   2802       aom_dist_wtd_sub_pixel_avg_variance64x128)
   2803 
   2804   BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
   2805       aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
   2806       aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
   2807       aom_dist_wtd_sub_pixel_avg_variance32x16)
   2808 
   2809   BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
   2810       aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
   2811       aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
   2812       aom_dist_wtd_sub_pixel_avg_variance16x32)
   2813 
   2814   BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
   2815       aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
   2816       aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
   2817       aom_dist_wtd_sub_pixel_avg_variance64x32)
   2818 
   2819   BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
   2820       aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
   2821       aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
   2822       aom_dist_wtd_sub_pixel_avg_variance32x64)
   2823 
   2824   BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
   2825       aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
   2826       aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
   2827       aom_dist_wtd_sub_pixel_avg_variance32x32)
   2828 
   2829   BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
   2830       aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
   2831       aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
   2832       aom_dist_wtd_sub_pixel_avg_variance64x64)
   2833 
   2834   BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
   2835       aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
   2836       aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
   2837       aom_dist_wtd_sub_pixel_avg_variance16x16)
   2838 
   2839   BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
   2840       aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
   2841       aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
   2842       aom_dist_wtd_sub_pixel_avg_variance16x8)
   2843 
   2844   BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
   2845       aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
   2846       aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
   2847       aom_dist_wtd_sub_pixel_avg_variance8x16)
   2848 
   2849   BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
   2850       aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
   2851       aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
   2852 
   2853   BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
   2854       aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
   2855       aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
   2856 
   2857   BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
   2858       aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
   2859       aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
   2860 
   2861   BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
   2862       aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
   2863       aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
   2864 
   2865 #define OBFP(BT, OSDF, OVF, OSVF) \
   2866   cpi->fn_ptr[BT].osdf = OSDF;    \
   2867   cpi->fn_ptr[BT].ovf = OVF;      \
   2868   cpi->fn_ptr[BT].osvf = OSVF;
   2869 
   2870   OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
   2871        aom_obmc_sub_pixel_variance128x128)
   2872   OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
   2873        aom_obmc_sub_pixel_variance128x64)
   2874   OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
   2875        aom_obmc_sub_pixel_variance64x128)
   2876   OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
   2877        aom_obmc_sub_pixel_variance64x64)
   2878   OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
   2879        aom_obmc_sub_pixel_variance64x32)
   2880   OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
   2881        aom_obmc_sub_pixel_variance32x64)
   2882   OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
   2883        aom_obmc_sub_pixel_variance32x32)
   2884   OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
   2885        aom_obmc_sub_pixel_variance32x16)
   2886   OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
   2887        aom_obmc_sub_pixel_variance16x32)
   2888   OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
   2889        aom_obmc_sub_pixel_variance16x16)
   2890   OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
   2891        aom_obmc_sub_pixel_variance16x8)
   2892   OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
   2893        aom_obmc_sub_pixel_variance8x16)
   2894   OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
   2895        aom_obmc_sub_pixel_variance8x8)
   2896   OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
   2897        aom_obmc_sub_pixel_variance4x8)
   2898   OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
   2899        aom_obmc_sub_pixel_variance8x4)
   2900   OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
   2901        aom_obmc_sub_pixel_variance4x4)
   2902   OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
   2903        aom_obmc_sub_pixel_variance4x16)
   2904   OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
   2905        aom_obmc_sub_pixel_variance16x4)
   2906   OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
   2907        aom_obmc_sub_pixel_variance8x32)
   2908   OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
   2909        aom_obmc_sub_pixel_variance32x8)
   2910   OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
   2911        aom_obmc_sub_pixel_variance16x64)
   2912   OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
   2913        aom_obmc_sub_pixel_variance64x16)
   2914 
   2915 #define MBFP(BT, MCSDF, MCSVF)  \
   2916   cpi->fn_ptr[BT].msdf = MCSDF; \
   2917   cpi->fn_ptr[BT].msvf = MCSVF;
   2918 
   2919   MBFP(BLOCK_128X128, aom_masked_sad128x128,
   2920        aom_masked_sub_pixel_variance128x128)
   2921   MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
   2922   MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
   2923   MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
   2924   MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
   2925   MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
   2926   MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
   2927   MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
   2928   MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
   2929   MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
   2930   MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
   2931   MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
   2932   MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
   2933   MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
   2934   MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
   2935   MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
   2936 
   2937   MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
   2938 
   2939   MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
   2940 
   2941   MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
   2942 
   2943   MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
   2944 
   2945   MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
   2946 
   2947   MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
   2948 
   2949   highbd_set_var_fns(cpi);
   2950 
   2951   /* av1_init_quantizer() is first called here. Add check in
   2952    * av1_frame_init_quantizer() so that av1_init_quantizer is only
   2953    * called later when needed. This will avoid unnecessary calls of
   2954    * av1_init_quantizer() for every frame.
   2955    */
   2956   av1_init_quantizer(cpi);
   2957   av1_qm_init(cm);
   2958 
   2959   av1_loop_filter_init(cm);
   2960   cm->superres_scale_denominator = SCALE_NUMERATOR;
   2961   cm->superres_upscaled_width = oxcf->width;
   2962   cm->superres_upscaled_height = oxcf->height;
   2963   av1_loop_restoration_precal();
   2964 
   2965   cm->error.setjmp = 0;
   2966 
   2967   return cpi;
   2968 }
   2969 
   2970 #if CONFIG_INTERNAL_STATS
   2971 #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
   2972 
   2973 #define SNPRINT2(H, T, V) \
   2974   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
   2975 #endif  // CONFIG_INTERNAL_STATS
   2976 
   2977 void av1_remove_compressor(AV1_COMP *cpi) {
   2978   AV1_COMMON *cm;
   2979   unsigned int i;
   2980   int t;
   2981 
   2982   if (!cpi) return;
   2983 
   2984   cm = &cpi->common;
   2985   const int num_planes = av1_num_planes(cm);
   2986 
   2987   if (cm->current_frame.frame_number > 0) {
   2988 #if CONFIG_ENTROPY_STATS
   2989     if (cpi->oxcf.pass != 1) {
   2990       fprintf(stderr, "Writing counts.stt\n");
   2991       FILE *f = fopen("counts.stt", "wb");
   2992       fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
   2993       fclose(f);
   2994     }
   2995 #endif  // CONFIG_ENTROPY_STATS
   2996 #if CONFIG_INTERNAL_STATS
   2997     aom_clear_system_state();
   2998 
   2999     if (cpi->oxcf.pass != 1) {
   3000       char headings[512] = { 0 };
   3001       char results[512] = { 0 };
   3002       FILE *f = fopen("opsnr.stt", "a");
   3003       double time_encoded =
   3004           (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
   3005           10000000.000;
   3006       double total_encode_time =
   3007           (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
   3008       const double dr =
   3009           (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
   3010       const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
   3011       const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
   3012       const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
   3013 
   3014       if (cpi->b_calculate_psnr) {
   3015         const double total_psnr = aom_sse_to_psnr(
   3016             (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
   3017         const double total_ssim =
   3018             100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
   3019         snprintf(headings, sizeof(headings),
   3020                  "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
   3021                  "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
   3022                  "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
   3023                  "AVPsrnY\tAPsnrCb\tAPsnrCr");
   3024         snprintf(results, sizeof(results),
   3025                  "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
   3026                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
   3027                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
   3028                  "%7.3f\t%7.3f\t%7.3f",
   3029                  dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
   3030                  cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
   3031                  total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
   3032                  cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
   3033                  cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
   3034                  cpi->psnr.stat[STAT_Y] / cpi->count,
   3035                  cpi->psnr.stat[STAT_U] / cpi->count,
   3036                  cpi->psnr.stat[STAT_V] / cpi->count);
   3037 
   3038         if (cpi->b_calculate_blockiness) {
   3039           SNPRINT(headings, "\t  Block\tWstBlck");
   3040           SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
   3041           SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
   3042         }
   3043 
   3044         if (cpi->b_calculate_consistency) {
   3045           double consistency =
   3046               aom_sse_to_psnr((double)cpi->total_samples, peak,
   3047                               (double)cpi->total_inconsistency);
   3048 
   3049           SNPRINT(headings, "\tConsist\tWstCons");
   3050           SNPRINT2(results, "\t%7.3f", consistency);
   3051           SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
   3052         }
   3053         fprintf(f, "%s\t    Time\tRcErr\tAbsErr\n", headings);
   3054         fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
   3055                 rate_err, fabs(rate_err));
   3056       }
   3057 
   3058       fclose(f);
   3059     }
   3060 #endif  // CONFIG_INTERNAL_STATS
   3061 #if CONFIG_SPEED_STATS
   3062     if (cpi->oxcf.pass != 1) {
   3063       fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
   3064     }
   3065 #endif  // CONFIG_SPEED_STATS
   3066 
   3067 #if CONFIG_COLLECT_PARTITION_STATS == 2
   3068     if (cpi->oxcf.pass != 1) {
   3069       av1_print_partition_stats(&cpi->partition_stats);
   3070     }
   3071 #endif
   3072   }
   3073 
   3074   for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
   3075     aom_free(cpi->tpl_stats[frame].tpl_stats_ptr);
   3076     cpi->tpl_stats[frame].is_valid = 0;
   3077   }
   3078 
   3079   for (t = cpi->num_workers - 1; t >= 0; --t) {
   3080     AVxWorker *const worker = &cpi->workers[t];
   3081     EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
   3082 
   3083     // Deallocate allocated threads.
   3084     aom_get_worker_interface()->end(worker);
   3085 
   3086     // Deallocate allocated thread data.
   3087     if (cpi->row_mt == 1) aom_free(thread_data->td->tctx);
   3088     if (t > 0) {
   3089       aom_free(thread_data->td->palette_buffer);
   3090       aom_free(thread_data->td->tmp_conv_dst);
   3091       for (int j = 0; j < 2; ++j) {
   3092         aom_free(thread_data->td->tmp_obmc_bufs[j]);
   3093       }
   3094       aom_free(thread_data->td->above_pred_buf);
   3095       aom_free(thread_data->td->left_pred_buf);
   3096       aom_free(thread_data->td->wsrc_buf);
   3097 
   3098       aom_free(thread_data->td->inter_modes_info);
   3099       for (int x = 0; x < 2; x++) {
   3100         for (int y = 0; y < 2; y++) {
   3101           aom_free(thread_data->td->hash_value_buffer[x][y]);
   3102           thread_data->td->hash_value_buffer[x][y] = NULL;
   3103         }
   3104       }
   3105       aom_free(thread_data->td->mask_buf);
   3106       aom_free(thread_data->td->counts);
   3107       av1_free_pc_tree(thread_data->td, num_planes);
   3108       aom_free(thread_data->td);
   3109     }
   3110   }
   3111 #if CONFIG_MULTITHREAD
   3112   if (cpi->row_mt == 1) {
   3113     if (cpi->row_mt_mutex_ != NULL) {
   3114       pthread_mutex_destroy(cpi->row_mt_mutex_);
   3115       aom_free(cpi->row_mt_mutex_);
   3116     }
   3117   }
   3118 #endif
   3119   av1_row_mt_mem_dealloc(cpi);
   3120   aom_free(cpi->tile_thr_data);
   3121   aom_free(cpi->workers);
   3122 
   3123   if (cpi->num_workers > 1) {
   3124     av1_loop_filter_dealloc(&cpi->lf_row_sync);
   3125     av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
   3126   }
   3127 
   3128   dealloc_compressor_data(cpi);
   3129 
   3130   for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
   3131        ++i) {
   3132     aom_free(cpi->mbgraph_stats[i].mb_stats);
   3133   }
   3134 
   3135 #if CONFIG_INTERNAL_STATS
   3136   aom_free(cpi->ssim_vars);
   3137   cpi->ssim_vars = NULL;
   3138 #endif  // CONFIG_INTERNAL_STATS
   3139 
   3140   av1_remove_common(cm);
   3141   for (i = 0; i < FRAME_BUFFERS; ++i) {
   3142     av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
   3143   }
   3144   if (cpi->sf.use_hash_based_trellis) hbt_destroy();
   3145   av1_free_ref_frame_buffers(cm->buffer_pool);
   3146   aom_free(cpi);
   3147 
   3148 #ifdef OUTPUT_YUV_SKINMAP
   3149   fclose(yuv_skinmap_file);
   3150 #endif
   3151 #ifdef OUTPUT_YUV_REC
   3152   fclose(yuv_rec_file);
   3153 #endif
   3154 }
   3155 
   3156 static void generate_psnr_packet(AV1_COMP *cpi) {
   3157   struct aom_codec_cx_pkt pkt;
   3158   int i;
   3159   PSNR_STATS psnr;
   3160   aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
   3161                        cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
   3162 
   3163   for (i = 0; i < 4; ++i) {
   3164     pkt.data.psnr.samples[i] = psnr.samples[i];
   3165     pkt.data.psnr.sse[i] = psnr.sse[i];
   3166     pkt.data.psnr.psnr[i] = psnr.psnr[i];
   3167   }
   3168   pkt.kind = AOM_CODEC_PSNR_PKT;
   3169   aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
   3170 }
   3171 
   3172 int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
   3173   if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
   3174 
   3175   cpi->ext_ref_frame_flags = ref_frame_flags;
   3176   return 0;
   3177 }
   3178 
   3179 int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
   3180   AV1_COMMON *const cm = &cpi->common;
   3181   const int num_planes = av1_num_planes(cm);
   3182   YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
   3183   if (cfg) {
   3184     aom_yv12_copy_frame(cfg, sd, num_planes);
   3185     return 0;
   3186   } else {
   3187     return -1;
   3188   }
   3189 }
   3190 
   3191 int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
   3192   AV1_COMMON *const cm = &cpi->common;
   3193   const int num_planes = av1_num_planes(cm);
   3194   YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
   3195   if (cfg) {
   3196     aom_yv12_copy_frame(sd, cfg, num_planes);
   3197     return 0;
   3198   } else {
   3199     return -1;
   3200   }
   3201 }
   3202 
   3203 int av1_update_entropy(AV1_COMP *cpi, int update) {
   3204   cpi->ext_refresh_frame_context = update;
   3205   cpi->ext_refresh_frame_context_pending = 1;
   3206   return 0;
   3207 }
   3208 
   3209 #if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
   3210 // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
   3211 // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
   3212 // not denoise the UV channels at this time. If ever we implement UV channel
   3213 // denoising we will have to modify this.
   3214 void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
   3215   uint8_t *src = s->y_buffer;
   3216   int h = s->y_height;
   3217 
   3218   do {
   3219     fwrite(src, s->y_width, 1, f);
   3220     src += s->y_stride;
   3221   } while (--h);
   3222 
   3223   src = s->u_buffer;
   3224   h = s->uv_height;
   3225 
   3226   do {
   3227     fwrite(src, s->uv_width, 1, f);
   3228     src += s->uv_stride;
   3229   } while (--h);
   3230 
   3231   src = s->v_buffer;
   3232   h = s->uv_height;
   3233 
   3234   do {
   3235     fwrite(src, s->uv_width, 1, f);
   3236     src += s->uv_stride;
   3237   } while (--h);
   3238 }
   3239 #endif
   3240 
   3241 #ifdef OUTPUT_YUV_REC
   3242 void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
   3243   uint8_t *src = s->y_buffer;
   3244   int h = cm->height;
   3245   if (yuv_rec_file == NULL) return;
   3246   if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
   3247     uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
   3248 
   3249     do {
   3250       fwrite(src16, s->y_width, 2, yuv_rec_file);
   3251       src16 += s->y_stride;
   3252     } while (--h);
   3253 
   3254     src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
   3255     h = s->uv_height;
   3256 
   3257     do {
   3258       fwrite(src16, s->uv_width, 2, yuv_rec_file);
   3259       src16 += s->uv_stride;
   3260     } while (--h);
   3261 
   3262     src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
   3263     h = s->uv_height;
   3264 
   3265     do {
   3266       fwrite(src16, s->uv_width, 2, yuv_rec_file);
   3267       src16 += s->uv_stride;
   3268     } while (--h);
   3269 
   3270     fflush(yuv_rec_file);
   3271     return;
   3272   }
   3273 
   3274   do {
   3275     fwrite(src, s->y_width, 1, yuv_rec_file);
   3276     src += s->y_stride;
   3277   } while (--h);
   3278 
   3279   src = s->u_buffer;
   3280   h = s->uv_height;
   3281 
   3282   do {
   3283     fwrite(src, s->uv_width, 1, yuv_rec_file);
   3284     src += s->uv_stride;
   3285   } while (--h);
   3286 
   3287   src = s->v_buffer;
   3288   h = s->uv_height;
   3289 
   3290   do {
   3291     fwrite(src, s->uv_width, 1, yuv_rec_file);
   3292     src += s->uv_stride;
   3293   } while (--h);
   3294 
   3295   fflush(yuv_rec_file);
   3296 }
   3297 #endif  // OUTPUT_YUV_REC
   3298 
   3299 #define GM_RECODE_LOOP_NUM4X4_FACTOR 192
   3300 static int recode_loop_test_global_motion(AV1_COMP *cpi) {
   3301   int i;
   3302   int recode = 0;
   3303   RD_COUNTS *const rdc = &cpi->td.rd_counts;
   3304   AV1_COMMON *const cm = &cpi->common;
   3305   for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
   3306     if (cm->global_motion[i].wmtype != IDENTITY &&
   3307         rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
   3308             cpi->gmparams_cost[i]) {
   3309       cm->global_motion[i] = default_warp_params;
   3310       assert(cm->global_motion[i].wmtype == IDENTITY);
   3311       cpi->gmparams_cost[i] = 0;
   3312       recode = 1;
   3313       // TODO(sarahparker): The earlier condition for recoding here was:
   3314       // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
   3315       // similar to that back to speed up global motion?
   3316     }
   3317   }
   3318   return recode;
   3319 }
   3320 
   3321 // Function to test for conditions that indicate we should loop
   3322 // back and recode a frame.
   3323 static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
   3324                             int maxq, int minq) {
   3325   const RATE_CONTROL *const rc = &cpi->rc;
   3326   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
   3327   const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
   3328   int force_recode = 0;
   3329 
   3330   if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
   3331       (cpi->sf.recode_loop == ALLOW_RECODE) ||
   3332       (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
   3333     // TODO(agrange) high_limit could be greater than the scale-down threshold.
   3334     if ((rc->projected_frame_size > high_limit && q < maxq) ||
   3335         (rc->projected_frame_size < low_limit && q > minq)) {
   3336       force_recode = 1;
   3337     } else if (cpi->oxcf.rc_mode == AOM_CQ) {
   3338       // Deal with frame undershoot and whether or not we are
   3339       // below the automatically set cq level.
   3340       if (q > oxcf->cq_level &&
   3341           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
   3342         force_recode = 1;
   3343       }
   3344     }
   3345   }
   3346   return force_recode;
   3347 }
   3348 
   3349 static void scale_references(AV1_COMP *cpi) {
   3350   AV1_COMMON *cm = &cpi->common;
   3351   const int num_planes = av1_num_planes(cm);
   3352   MV_REFERENCE_FRAME ref_frame;
   3353   const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
   3354     AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
   3355     AOM_BWD_FLAG,  AOM_ALT2_FLAG,  AOM_ALT_FLAG
   3356   };
   3357 
   3358   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
   3359     // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
   3360     if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
   3361       BufferPool *const pool = cm->buffer_pool;
   3362       const YV12_BUFFER_CONFIG *const ref =
   3363           get_ref_frame_yv12_buf(cm, ref_frame);
   3364 
   3365       if (ref == NULL) {
   3366         cpi->scaled_ref_buf[ref_frame - 1] = NULL;
   3367         continue;
   3368       }
   3369 
   3370       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
   3371         // Replace the reference buffer with a copy having a thicker border,
   3372         // if the reference buffer is higher resolution than the current
   3373         // frame, and the border is thin.
   3374         if ((ref->y_crop_width > cm->width ||
   3375              ref->y_crop_height > cm->height) &&
   3376             ref->border < AOM_BORDER_IN_PIXELS) {
   3377           RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
   3378           if (aom_yv12_realloc_with_new_border(
   3379                   &ref_fb->buf, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
   3380                   num_planes) != 0) {
   3381             aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
   3382                                "Failed to allocate frame buffer");
   3383           }
   3384         }
   3385         int force_scaling = 0;
   3386         RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
   3387         if (new_fb == NULL) {
   3388           const int new_fb_idx = get_free_fb(cm);
   3389           if (new_fb_idx == INVALID_IDX) {
   3390             aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
   3391                                "Unable to find free frame buffer");
   3392           }
   3393           force_scaling = 1;
   3394           new_fb = &pool->frame_bufs[new_fb_idx];
   3395         }
   3396 
   3397         if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
   3398             new_fb->buf.y_crop_height != cm->height) {
   3399           if (aom_realloc_frame_buffer(
   3400                   &new_fb->buf, cm->width, cm->height,
   3401                   cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
   3402                   cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
   3403                   cm->byte_alignment, NULL, NULL, NULL)) {
   3404             if (force_scaling) {
   3405               // Release the reference acquired in the get_free_fb() call above.
   3406               --new_fb->ref_count;
   3407             }
   3408             aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
   3409                                "Failed to allocate frame buffer");
   3410           }
   3411           av1_resize_and_extend_frame(
   3412               ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
   3413           cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
   3414           alloc_frame_mvs(cm, new_fb);
   3415         }
   3416       } else {
   3417         RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
   3418         buf->buf.y_crop_width = ref->y_crop_width;
   3419         buf->buf.y_crop_height = ref->y_crop_height;
   3420         cpi->scaled_ref_buf[ref_frame - 1] = buf;
   3421         ++buf->ref_count;
   3422       }
   3423     } else {
   3424       if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
   3425     }
   3426   }
   3427 }
   3428 
   3429 static void release_scaled_references(AV1_COMP *cpi) {
   3430   // TODO(isbs): only refresh the necessary frames, rather than all of them
   3431   for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
   3432     RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
   3433     if (buf != NULL) {
   3434       --buf->ref_count;
   3435       cpi->scaled_ref_buf[i] = NULL;
   3436     }
   3437   }
   3438 }
   3439 
   3440 static void set_mv_search_params(AV1_COMP *cpi) {
   3441   const AV1_COMMON *const cm = &cpi->common;
   3442   const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
   3443 
   3444   // Default based on max resolution.
   3445   cpi->mv_step_param = av1_init_search_range(max_mv_def);
   3446 
   3447   if (cpi->sf.mv.auto_mv_step_size) {
   3448     if (frame_is_intra_only(cm)) {
   3449       // Initialize max_mv_magnitude for use in the first INTER frame
   3450       // after a key/intra-only frame.
   3451       cpi->max_mv_magnitude = max_mv_def;
   3452     } else {
   3453       if (cm->show_frame) {
   3454         // Allow mv_steps to correspond to twice the max mv magnitude found
   3455         // in the previous frame, capped by the default max_mv_magnitude based
   3456         // on resolution.
   3457         cpi->mv_step_param = av1_init_search_range(
   3458             AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
   3459       }
   3460       cpi->max_mv_magnitude = 0;
   3461     }
   3462   }
   3463 }
   3464 
   3465 static void set_screen_content_options(AV1_COMP *cpi) {
   3466   AV1_COMMON *cm = &cpi->common;
   3467 
   3468   if (cm->seq_params.force_screen_content_tools != 2) {
   3469     cm->allow_screen_content_tools = cm->allow_intrabc =
   3470         cm->seq_params.force_screen_content_tools;
   3471     return;
   3472   }
   3473 
   3474   if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
   3475     cm->allow_screen_content_tools = cm->allow_intrabc = 1;
   3476     return;
   3477   }
   3478 
   3479   // Estimate if the source frame is screen content, based on the portion of
   3480   // blocks that have few luma colors.
   3481   const uint8_t *src = cpi->source->y_buffer;
   3482   assert(src != NULL);
   3483   const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
   3484   const int stride = cpi->source->y_stride;
   3485   const int width = cpi->source->y_width;
   3486   const int height = cpi->source->y_height;
   3487   const int bd = cm->seq_params.bit_depth;
   3488   const int blk_w = 16;
   3489   const int blk_h = 16;
   3490   // These threshold values are selected experimentally.
   3491   const int color_thresh = 4;
   3492   const unsigned int var_thresh = 0;
   3493   // Counts of blocks with no more than color_thresh colors.
   3494   int counts_1 = 0;
   3495   // Counts of blocks with no more than color_thresh colors and variance larger
   3496   // than var_thresh.
   3497   int counts_2 = 0;
   3498 
   3499   for (int r = 0; r + blk_h <= height; r += blk_h) {
   3500     for (int c = 0; c + blk_w <= width; c += blk_w) {
   3501       int count_buf[1 << 12];  // Maximum (1 << 12) color levels.
   3502       const uint8_t *const this_src = src + r * stride + c;
   3503       const int n_colors =
   3504           use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
   3505                                             count_buf)
   3506                   : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
   3507       if (n_colors > 1 && n_colors <= color_thresh) {
   3508         ++counts_1;
   3509         struct buf_2d buf;
   3510         buf.stride = stride;
   3511         buf.buf = (uint8_t *)this_src;
   3512         const unsigned int var =
   3513             use_hbd
   3514                 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
   3515                 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
   3516         if (var > var_thresh) ++counts_2;
   3517       }
   3518     }
   3519   }
   3520 
   3521   // The threshold values are selected experimentally.
   3522   cm->allow_screen_content_tools =
   3523       counts_1 * blk_h * blk_w * 10 > width * height;
   3524   // IntraBC would force loop filters off, so we use more strict rules that also
   3525   // requires that the block has high variance.
   3526   cm->allow_intrabc = cm->allow_screen_content_tools &&
   3527                       counts_2 * blk_h * blk_w * 15 > width * height;
   3528 }
   3529 
   3530 static void set_size_independent_vars(AV1_COMP *cpi) {
   3531   int i;
   3532   AV1_COMMON *cm = &cpi->common;
   3533   for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
   3534     cm->global_motion[i] = default_warp_params;
   3535   }
   3536   cpi->global_motion_search_done = 0;
   3537 
   3538   if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
   3539   cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
   3540 
   3541   av1_set_speed_features_framesize_independent(cpi, cpi->speed);
   3542   av1_set_rd_speed_thresholds(cpi);
   3543   cm->interp_filter = SWITCHABLE;
   3544   cm->switchable_motion_mode = 1;
   3545 }
   3546 
   3547 static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
   3548                                     int *top_index) {
   3549   AV1_COMMON *const cm = &cpi->common;
   3550   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
   3551 
   3552   // Setup variables that depend on the dimensions of the frame.
   3553   av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
   3554 
   3555   // Decide q and q bounds.
   3556   *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
   3557                                 top_index);
   3558 
   3559   if (!frame_is_intra_only(cm)) {
   3560     set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
   3561                           cpi->common.cur_frame_force_integer_mv);
   3562   }
   3563 
   3564   // Configure experimental use of segmentation for enhanced coding of
   3565   // static regions if indicated.
   3566   // Only allowed in the second pass of a two pass encode, as it requires
   3567   // lagged coding, and if the relevant speed feature flag is set.
   3568   if (oxcf->pass == 2 && cpi->sf.static_segmentation)
   3569     configure_static_seg_features(cpi);
   3570 }
   3571 
   3572 static void init_motion_estimation(AV1_COMP *cpi) {
   3573   int y_stride = cpi->scaled_source.y_stride;
   3574   int y_stride_src = (cpi->oxcf.resize_mode || cpi->oxcf.superres_mode)
   3575                          ? y_stride
   3576                          : cpi->lookahead->buf->img.y_stride;
   3577 
   3578   if (cpi->sf.mv.search_method == NSTEP) {
   3579     av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
   3580     av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
   3581   } else if (cpi->sf.mv.search_method == DIAMOND) {
   3582     av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
   3583     av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
   3584                                    y_stride_src);
   3585   }
   3586 }
   3587 
   3588 #define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
   3589 static void set_restoration_unit_size(int width, int height, int sx, int sy,
   3590                                       RestorationInfo *rst) {
   3591   (void)width;
   3592   (void)height;
   3593   (void)sx;
   3594   (void)sy;
   3595 #if COUPLED_CHROMA_FROM_LUMA_RESTORATION
   3596   int s = AOMMIN(sx, sy);
   3597 #else
   3598   int s = 0;
   3599 #endif  // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
   3600 
   3601   if (width * height > 352 * 288)
   3602     rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
   3603   else
   3604     rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
   3605   rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
   3606   rst[2].restoration_unit_size = rst[1].restoration_unit_size;
   3607 }
   3608 
   3609 static void init_ref_frame_bufs(AV1_COMP *cpi) {
   3610   AV1_COMMON *const cm = &cpi->common;
   3611   int i;
   3612   BufferPool *const pool = cm->buffer_pool;
   3613   cm->cur_frame = NULL;
   3614   for (i = 0; i < REF_FRAMES; ++i) {
   3615     cm->ref_frame_map[i] = NULL;
   3616   }
   3617   for (i = 0; i < FRAME_BUFFERS; ++i) {
   3618     pool->frame_bufs[i].ref_count = 0;
   3619   }
   3620   if (cm->seq_params.force_screen_content_tools) {
   3621     for (i = 0; i < FRAME_BUFFERS; ++i) {
   3622       av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
   3623     }
   3624   }
   3625 }
   3626 
   3627 static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
   3628                                 int subsampling_x, int subsampling_y) {
   3629   AV1_COMMON *const cm = &cpi->common;
   3630   SequenceHeader *const seq_params = &cm->seq_params;
   3631 
   3632   if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
   3633       seq_params->subsampling_x != subsampling_x ||
   3634       seq_params->subsampling_y != subsampling_y) {
   3635     seq_params->subsampling_x = subsampling_x;
   3636     seq_params->subsampling_y = subsampling_y;
   3637     seq_params->use_highbitdepth = use_highbitdepth;
   3638 
   3639     alloc_raw_frame_buffers(cpi);
   3640     init_ref_frame_bufs(cpi);
   3641     alloc_util_frame_buffers(cpi);
   3642 
   3643     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
   3644 
   3645     cpi->initial_width = cm->width;
   3646     cpi->initial_height = cm->height;
   3647     cpi->initial_mbs = cm->MBs;
   3648   }
   3649 }
   3650 
   3651 // Returns 1 if the assigned width or height was <= 0.
   3652 static int set_size_literal(AV1_COMP *cpi, int width, int height) {
   3653   AV1_COMMON *cm = &cpi->common;
   3654   const int num_planes = av1_num_planes(cm);
   3655   check_initial_width(cpi, cm->seq_params.use_highbitdepth,
   3656                       cm->seq_params.subsampling_x,
   3657                       cm->seq_params.subsampling_y);
   3658 
   3659   if (width <= 0 || height <= 0) return 1;
   3660 
   3661   cm->width = width;
   3662   cm->height = height;
   3663 
   3664   if (cpi->initial_width && cpi->initial_height &&
   3665       (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
   3666     av1_free_context_buffers(cm);
   3667     av1_free_pc_tree(&cpi->td, num_planes);
   3668     alloc_compressor_data(cpi);
   3669     realloc_segmentation_maps(cpi);
   3670     cpi->initial_width = cpi->initial_height = 0;
   3671   }
   3672   update_frame_size(cpi);
   3673 
   3674   return 0;
   3675 }
   3676 
   3677 void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
   3678   AV1_COMMON *const cm = &cpi->common;
   3679   const SequenceHeader *const seq_params = &cm->seq_params;
   3680   const int num_planes = av1_num_planes(cm);
   3681   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
   3682   int ref_frame;
   3683 
   3684   if (width != cm->width || height != cm->height) {
   3685     // There has been a change in the encoded frame size
   3686     set_size_literal(cpi, width, height);
   3687     set_mv_search_params(cpi);
   3688     // Recalculate 'all_lossless' in case super-resolution was (un)selected.
   3689     cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
   3690   }
   3691 
   3692   if (cpi->oxcf.pass == 2) {
   3693     av1_set_target_rate(cpi, cm->width, cm->height);
   3694   }
   3695 
   3696   alloc_frame_mvs(cm, cm->cur_frame);
   3697 
   3698   // Allocate above context buffers
   3699   if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
   3700       cm->num_allocated_above_context_mi_col < cm->mi_cols ||
   3701       cm->num_allocated_above_contexts < cm->tile_rows) {
   3702     av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
   3703     if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
   3704       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
   3705                          "Failed to allocate context buffers");
   3706   }
   3707 
   3708   // Reset the frame pointers to the current frame size.
   3709   if (aom_realloc_frame_buffer(
   3710           &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
   3711           seq_params->subsampling_y, seq_params->use_highbitdepth,
   3712           cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
   3713     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
   3714                        "Failed to allocate frame buffer");
   3715 
   3716   const int frame_width = cm->superres_upscaled_width;
   3717   const int frame_height = cm->superres_upscaled_height;
   3718   set_restoration_unit_size(frame_width, frame_height,
   3719                             seq_params->subsampling_x,
   3720                             seq_params->subsampling_y, cm->rst_info);
   3721   for (int i = 0; i < num_planes; ++i)
   3722     cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
   3723 
   3724   av1_alloc_restoration_buffers(cm);
   3725   alloc_util_frame_buffers(cpi);
   3726   init_motion_estimation(cpi);
   3727 
   3728   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
   3729     RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
   3730     if (buf != NULL) {
   3731       struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
   3732       av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
   3733                                         buf->buf.y_crop_height, cm->width,
   3734                                         cm->height);
   3735       if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
   3736     }
   3737   }
   3738 
   3739   av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
   3740                                     cm->width, cm->height);
   3741 
   3742   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
   3743 }
   3744 
   3745 static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
   3746   // Choose an arbitrary random number
   3747   static unsigned int seed = 56789;
   3748   const AV1EncoderConfig *oxcf = &cpi->oxcf;
   3749   if (oxcf->pass == 1) return SCALE_NUMERATOR;
   3750   uint8_t new_denom = SCALE_NUMERATOR;
   3751 
   3752   if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
   3753   switch (oxcf->resize_mode) {
   3754     case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
   3755     case RESIZE_FIXED:
   3756       if (cpi->common.current_frame.frame_type == KEY_FRAME)
   3757         new_denom = oxcf->resize_kf_scale_denominator;
   3758       else
   3759         new_denom = oxcf->resize_scale_denominator;
   3760       break;
   3761     case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
   3762     default: assert(0);
   3763   }
   3764   return new_denom;
   3765 }
   3766 
   3767 #define ENERGY_BY_Q2_THRESH 0.01
   3768 #define ENERGY_BY_AC_THRESH 0.2
   3769 
   3770 static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
   3771                                                      double threshq,
   3772                                                      double threshp) {
   3773   const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
   3774   const double tq = threshq * q * q;
   3775   const double tp = threshp * energy[1];
   3776   const double thresh = AOMMIN(tq, tp);
   3777   int k;
   3778   for (k = 16; k > 8; --k) {
   3779     if (energy[k - 1] > thresh) break;
   3780   }
   3781   return 3 * SCALE_NUMERATOR - k;
   3782 }
   3783 
   3784 static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex) {
   3785   double energy[16];
   3786   analyze_hor_freq(cpi, energy);
   3787   /*
   3788   printf("\nenergy = [");
   3789   for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
   3790   printf("]\n");
   3791   */
   3792   return get_superres_denom_from_qindex_energy(
   3793       qindex, energy, ENERGY_BY_Q2_THRESH, ENERGY_BY_AC_THRESH);
   3794 }
   3795 
   3796 static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
   3797   // Choose an arbitrary random number
   3798   static unsigned int seed = 34567;
   3799   const AV1EncoderConfig *oxcf = &cpi->oxcf;
   3800   if (oxcf->pass == 1) return SCALE_NUMERATOR;
   3801   uint8_t new_denom = SCALE_NUMERATOR;
   3802 
   3803   // Make sure that superres mode of the frame is consistent with the
   3804   // sequence-level flag.
   3805   assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
   3806                  cpi->common.seq_params.enable_superres));
   3807   assert(IMPLIES(!cpi->common.seq_params.enable_superres,
   3808                  oxcf->superres_mode == SUPERRES_NONE));
   3809 
   3810   switch (oxcf->superres_mode) {
   3811     case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
   3812     case SUPERRES_FIXED:
   3813       if (cpi->common.current_frame.frame_type == KEY_FRAME)
   3814         new_denom = oxcf->superres_kf_scale_denominator;
   3815       else
   3816         new_denom = oxcf->superres_scale_denominator;
   3817       break;
   3818     case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
   3819     case SUPERRES_QTHRESH: {
   3820       // Do not use superres when screen content tools are used.
   3821       if (cpi->common.allow_screen_content_tools) break;
   3822       if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
   3823         av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
   3824       int bottom_index, top_index;
   3825       const int q = av1_rc_pick_q_and_bounds(
   3826           cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
   3827 
   3828       const int qthresh = (frame_is_intra_only(&cpi->common))
   3829                               ? oxcf->superres_kf_qthresh
   3830                               : oxcf->superres_qthresh;
   3831       if (q <= qthresh) {
   3832         new_denom = SCALE_NUMERATOR;
   3833       } else {
   3834         new_denom = get_superres_denom_for_qindex(cpi, q);
   3835       }
   3836       break;
   3837     }
   3838     case SUPERRES_AUTO: {
   3839       // Don't use when screen content tools are used.
   3840       if (cpi->common.allow_screen_content_tools) break;
   3841       // Don't use for inter frames.
   3842       if (!frame_is_intra_only(&cpi->common)) break;
   3843       // Don't use for keyframes that can be used as references.
   3844       if (cpi->rc.frames_to_key != 1) break;
   3845 
   3846       // Now decide the use of superres based on 'q'.
   3847       int bottom_index, top_index;
   3848       const int q = av1_rc_pick_q_and_bounds(
   3849           cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
   3850 
   3851       const int qthresh = 128;
   3852       if (q <= qthresh) {
   3853         new_denom = SCALE_NUMERATOR;
   3854       } else {
   3855         new_denom = get_superres_denom_for_qindex(cpi, q);
   3856       }
   3857       break;
   3858     }
   3859     default: assert(0);
   3860   }
   3861   return new_denom;
   3862 }
   3863 
   3864 static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
   3865   return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
   3866 }
   3867 
   3868 static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
   3869   // Only need to check the width, as scaling is horizontal only.
   3870   (void)oheight;
   3871   return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
   3872 }
   3873 
   3874 static int validate_size_scales(RESIZE_MODE resize_mode,
   3875                                 SUPERRES_MODE superres_mode, int owidth,
   3876                                 int oheight, size_params_type *rsz) {
   3877   if (dimensions_are_ok(owidth, oheight, rsz)) {  // Nothing to do.
   3878     return 1;
   3879   }
   3880 
   3881   // Calculate current resize scale.
   3882   int resize_denom =
   3883       AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
   3884              DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
   3885 
   3886   if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
   3887     // Alter superres scale as needed to enforce conformity.
   3888     rsz->superres_denom =
   3889         (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
   3890     if (!dimensions_are_ok(owidth, oheight, rsz)) {
   3891       if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
   3892     }
   3893   } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
   3894     // Alter resize scale as needed to enforce conformity.
   3895     resize_denom =
   3896         (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
   3897     rsz->resize_width = owidth;
   3898     rsz->resize_height = oheight;
   3899     av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
   3900                               resize_denom);
   3901     if (!dimensions_are_ok(owidth, oheight, rsz)) {
   3902       if (resize_denom > SCALE_NUMERATOR) {
   3903         --resize_denom;
   3904         rsz->resize_width = owidth;
   3905         rsz->resize_height = oheight;
   3906         av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
   3907                                   resize_denom);
   3908       }
   3909     }
   3910   } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
   3911     // Alter both resize and superres scales as needed to enforce conformity.
   3912     do {
   3913       if (resize_denom > rsz->superres_denom)
   3914         --resize_denom;
   3915       else
   3916         --rsz->superres_denom;
   3917       rsz->resize_width = owidth;
   3918       rsz->resize_height = oheight;
   3919       av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
   3920                                 resize_denom);
   3921     } while (!dimensions_are_ok(owidth, oheight, rsz) &&
   3922              (resize_denom > SCALE_NUMERATOR ||
   3923               rsz->superres_denom > SCALE_NUMERATOR));
   3924   } else {  // We are allowed to alter neither resize scale nor superres
   3925             // scale.
   3926     return 0;
   3927   }
   3928   return dimensions_are_ok(owidth, oheight, rsz);
   3929 }
   3930 
   3931 // Calculates resize and superres params for next frame
   3932 static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
   3933   const AV1EncoderConfig *oxcf = &cpi->oxcf;
   3934   size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
   3935   int resize_denom;
   3936   if (oxcf->pass == 1) return rsz;
   3937   if (cpi->resize_pending_width && cpi->resize_pending_height) {
   3938     rsz.resize_width = cpi->resize_pending_width;
   3939     rsz.resize_height = cpi->resize_pending_height;
   3940     cpi->resize_pending_width = cpi->resize_pending_height = 0;
   3941   } else {
   3942     resize_denom = calculate_next_resize_scale(cpi);
   3943     rsz.resize_width = cpi->oxcf.width;
   3944     rsz.resize_height = cpi->oxcf.height;
   3945     av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
   3946                               resize_denom);
   3947   }
   3948   rsz.superres_denom = calculate_next_superres_scale(cpi);
   3949   if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
   3950                             oxcf->height, &rsz))
   3951     assert(0 && "Invalid scale parameters");
   3952   return rsz;
   3953 }
   3954 
   3955 static void setup_frame_size_from_params(AV1_COMP *cpi,
   3956                                          const size_params_type *rsz) {
   3957   int encode_width = rsz->resize_width;
   3958   int encode_height = rsz->resize_height;
   3959 
   3960   AV1_COMMON *cm = &cpi->common;
   3961   cm->superres_upscaled_width = encode_width;
   3962   cm->superres_upscaled_height = encode_height;
   3963   cm->superres_scale_denominator = rsz->superres_denom;
   3964   av1_calculate_scaled_superres_size(&encode_width, &encode_height,
   3965                                      rsz->superres_denom);
   3966   av1_set_frame_size(cpi, encode_width, encode_height);
   3967 }
   3968 
   3969 void av1_setup_frame_size(AV1_COMP *cpi) {
   3970   AV1_COMMON *cm = &cpi->common;
   3971   // Reset superres params from previous frame.
   3972   cm->superres_scale_denominator = SCALE_NUMERATOR;
   3973   const size_params_type rsz = calculate_next_size_params(cpi);
   3974   setup_frame_size_from_params(cpi, &rsz);
   3975 
   3976   assert(is_min_tile_width_satisfied(cm));
   3977 }
   3978 
   3979 static void superres_post_encode(AV1_COMP *cpi) {
   3980   AV1_COMMON *cm = &cpi->common;
   3981   const int num_planes = av1_num_planes(cm);
   3982 
   3983   if (!av1_superres_scaled(cm)) return;
   3984 
   3985   assert(cpi->oxcf.enable_superres);
   3986   assert(!is_lossless_requested(&cpi->oxcf));
   3987   assert(!cm->all_lossless);
   3988 
   3989   av1_superres_upscale(cm, NULL);
   3990 
   3991   // If regular resizing is occurring the source will need to be downscaled to
   3992   // match the upscaled superres resolution. Otherwise the original source is
   3993   // used.
   3994   if (!av1_resize_scaled(cm)) {
   3995     cpi->source = cpi->unscaled_source;
   3996     if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
   3997   } else {
   3998     assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
   3999     assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
   4000     // Do downscale. cm->(width|height) has been updated by
   4001     // av1_superres_upscale
   4002     if (aom_realloc_frame_buffer(
   4003             &cpi->scaled_source, cm->superres_upscaled_width,
   4004             cm->superres_upscaled_height, cm->seq_params.subsampling_x,
   4005             cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
   4006             AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
   4007       aom_internal_error(
   4008           &cm->error, AOM_CODEC_MEM_ERROR,
   4009           "Failed to reallocate scaled source buffer for superres");
   4010     assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
   4011     assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
   4012     av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
   4013                                 (int)cm->seq_params.bit_depth, num_planes);
   4014     cpi->source = &cpi->scaled_source;
   4015   }
   4016 }
   4017 
   4018 static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
   4019   const int num_planes = av1_num_planes(cm);
   4020   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
   4021 
   4022   assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
   4023                  cm->coded_lossless && cm->all_lossless));
   4024 
   4025   const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
   4026   const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
   4027                        !cm->large_scale_tile;
   4028   const int use_restoration = cm->seq_params.enable_restoration &&
   4029                               !cm->all_lossless && !cm->large_scale_tile;
   4030 
   4031   struct loopfilter *lf = &cm->lf;
   4032 
   4033 #if CONFIG_COLLECT_COMPONENT_TIMING
   4034   start_timing(cpi, loop_filter_time);
   4035 #endif
   4036   if (use_loopfilter) {
   4037     aom_clear_system_state();
   4038     av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
   4039   } else {
   4040     lf->filter_level[0] = 0;
   4041     lf->filter_level[1] = 0;
   4042   }
   4043 
   4044   if (lf->filter_level[0] || lf->filter_level[1]) {
   4045     if (cpi->num_workers > 1)
   4046       av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
   4047 #if LOOP_FILTER_BITMASK
   4048                                0,
   4049 #endif
   4050                                cpi->workers, cpi->num_workers,
   4051                                &cpi->lf_row_sync);
   4052     else
   4053       av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
   4054 #if LOOP_FILTER_BITMASK
   4055                             0,
   4056 #endif
   4057                             0, num_planes, 0);
   4058   }
   4059 #if CONFIG_COLLECT_COMPONENT_TIMING
   4060   end_timing(cpi, loop_filter_time);
   4061 #endif
   4062 
   4063   if (use_restoration)
   4064     av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
   4065 
   4066   if (use_cdef) {
   4067 #if CONFIG_COLLECT_COMPONENT_TIMING
   4068     start_timing(cpi, cdef_time);
   4069 #endif
   4070     // Find CDEF parameters
   4071     av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
   4072                     cpi->sf.fast_cdef_search);
   4073 
   4074     // Apply the filter
   4075     av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
   4076 #if CONFIG_COLLECT_COMPONENT_TIMING
   4077     end_timing(cpi, cdef_time);
   4078 #endif
   4079   } else {
   4080     cm->cdef_info.cdef_bits = 0;
   4081     cm->cdef_info.cdef_strengths[0] = 0;
   4082     cm->cdef_info.nb_cdef_strengths = 1;
   4083     cm->cdef_info.cdef_uv_strengths[0] = 0;
   4084   }
   4085 
   4086   superres_post_encode(cpi);
   4087 
   4088 #if CONFIG_COLLECT_COMPONENT_TIMING
   4089   start_timing(cpi, loop_restoration_time);
   4090 #endif
   4091   if (use_restoration) {
   4092     av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
   4093     av1_pick_filter_restoration(cpi->source, cpi);
   4094     if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
   4095         cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
   4096         cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
   4097       if (cpi->num_workers > 1)
   4098         av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
   4099                                              cpi->workers, cpi->num_workers,
   4100                                              &cpi->lr_row_sync, &cpi->lr_ctxt);
   4101       else
   4102         av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
   4103                                           &cpi->lr_ctxt);
   4104     }
   4105   } else {
   4106     cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
   4107     cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
   4108     cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
   4109   }
   4110 #if CONFIG_COLLECT_COMPONENT_TIMING
   4111   end_timing(cpi, loop_restoration_time);
   4112 #endif
   4113 }
   4114 
   4115 static void fix_interp_filter(InterpFilter *const interp_filter,
   4116                               const FRAME_COUNTS *const counts) {
   4117   if (*interp_filter == SWITCHABLE) {
   4118     // Check to see if only one of the filters is actually used
   4119     int count[SWITCHABLE_FILTERS] = { 0 };
   4120     int num_filters_used = 0;
   4121     for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
   4122       for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
   4123         count[i] += counts->switchable_interp[j][i];
   4124       num_filters_used += (count[i] > 0);
   4125     }
   4126     if (num_filters_used == 1) {
   4127       // Only one filter is used. So set the filter at frame level
   4128       for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
   4129         if (count[i]) {
   4130           if (i == EIGHTTAP_REGULAR) *interp_filter = i;
   4131           break;
   4132         }
   4133       }
   4134     }
   4135   }
   4136 }
   4137 
   4138 static void finalize_encoded_frame(AV1_COMP *const cpi) {
   4139   AV1_COMMON *const cm = &cpi->common;
   4140   CurrentFrame *const current_frame = &cm->current_frame;
   4141 
   4142   if (!cm->seq_params.reduced_still_picture_hdr &&
   4143       encode_show_existing_frame(cm)) {
   4144     RefCntBuffer *const frame_to_show =
   4145         cm->ref_frame_map[cpi->existing_fb_idx_to_show];
   4146 
   4147     if (frame_to_show == NULL) {
   4148       aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   4149                          "Buffer does not contain a reconstructed frame");
   4150     }
   4151     assert(frame_to_show->ref_count > 0);
   4152     assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
   4153   }
   4154 
   4155   if (!encode_show_existing_frame(cm) &&
   4156       cm->seq_params.film_grain_params_present &&
   4157       (cm->show_frame || cm->showable_frame)) {
   4158     // Copy the current frame's film grain params to the its corresponding
   4159     // RefCntBuffer slot.
   4160     cm->cur_frame->film_grain_params = cm->film_grain_params;
   4161 
   4162     // We must update the parameters if this is not an INTER_FRAME
   4163     if (current_frame->frame_type != INTER_FRAME)
   4164       cm->cur_frame->film_grain_params.update_parameters = 1;
   4165 
   4166     // Iterate the random seed for the next frame.
   4167     cm->film_grain_params.random_seed += 3381;
   4168     if (cm->film_grain_params.random_seed == 0)
   4169       cm->film_grain_params.random_seed = 7391;
   4170   }
   4171 
   4172   // Initialise all tiles' contexts from the global frame context
   4173   for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
   4174     for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
   4175       const int tile_idx = tile_row * cm->tile_cols + tile_col;
   4176       cpi->tile_data[tile_idx].tctx = *cm->fc;
   4177     }
   4178   }
   4179 
   4180   fix_interp_filter(&cm->interp_filter, cpi->td.counts);
   4181 }
   4182 
   4183 static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
   4184                                      int top_index, int bottom_index) {
   4185   const AV1_COMMON *const cm = &cpi->common;
   4186   const RATE_CONTROL *const rc = &cpi->rc;
   4187 
   4188   av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
   4189 
   4190   int q_regulated =
   4191       av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
   4192                         AOMMAX(q_high, top_index), cm->width, cm->height);
   4193 
   4194   int retries = 0;
   4195   while (q_regulated < q_low && retries < 10) {
   4196     av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
   4197     q_regulated =
   4198         av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
   4199                           AOMMAX(q_high, top_index), cm->width, cm->height);
   4200     retries++;
   4201   }
   4202   return q_regulated;
   4203 }
   4204 
   4205 static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
   4206                                       int top_index, int bottom_index) {
   4207   const AV1_COMMON *const cm = &cpi->common;
   4208   const RATE_CONTROL *const rc = &cpi->rc;
   4209 
   4210   av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
   4211   int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
   4212                                       top_index, cm->width, cm->height);
   4213 
   4214   int retries = 0;
   4215   while (q_regulated > q_high && retries < 10) {
   4216     av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
   4217     q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
   4218                                     top_index, cm->width, cm->height);
   4219     retries++;
   4220   }
   4221   return q_regulated;
   4222 }
   4223 
   4224 // Called after encode_with_recode_loop() has just encoded a frame and packed
   4225 // its bitstream.  This function works out whether we under- or over-shot
   4226 // our bitrate target and adjusts q as appropriate.  Also decides whether
   4227 // or not we should do another recode loop, indicated by *loop
   4228 static void recode_loop_update_q(AV1_COMP *const cpi, int *const loop,
   4229                                  int *const q, int *const q_low,
   4230                                  int *const q_high, const int top_index,
   4231                                  const int bottom_index,
   4232                                  int *const undershoot_seen,
   4233                                  int *const overshoot_seen,
   4234                                  const int loop_at_this_size) {
   4235   AV1_COMMON *const cm = &cpi->common;
   4236   RATE_CONTROL *const rc = &cpi->rc;
   4237 
   4238   int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
   4239   av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
   4240                                    &frame_under_shoot_limit,
   4241                                    &frame_over_shoot_limit);
   4242   if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
   4243 
   4244   if ((cm->current_frame.frame_type == KEY_FRAME) &&
   4245       rc->this_key_frame_forced &&
   4246       (rc->projected_frame_size < rc->max_frame_bandwidth)) {
   4247     int last_q = *q;
   4248     int64_t kf_err;
   4249 
   4250     int64_t high_err_target = cpi->ambient_err;
   4251     int64_t low_err_target = cpi->ambient_err >> 1;
   4252 
   4253     if (cm->seq_params.use_highbitdepth) {
   4254       kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
   4255     } else {
   4256       kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
   4257     }
   4258     // Prevent possible divide by zero error below for perfect KF
   4259     kf_err += !kf_err;
   4260 
   4261     // The key frame is not good enough or we can afford
   4262     // to make it better without undue risk of popping.
   4263     if ((kf_err > high_err_target &&
   4264          rc->projected_frame_size <= frame_over_shoot_limit) ||
   4265         (kf_err > low_err_target &&
   4266          rc->projected_frame_size <= frame_under_shoot_limit)) {
   4267       // Lower q_high
   4268       *q_high = *q > *q_low ? *q - 1 : *q_low;
   4269 
   4270       // Adjust Q
   4271       *q = (int)((*q * high_err_target) / kf_err);
   4272       *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
   4273     } else if (kf_err < low_err_target &&
   4274                rc->projected_frame_size >= frame_under_shoot_limit) {
   4275       // The key frame is much better than the previous frame
   4276       // Raise q_low
   4277       *q_low = *q < *q_high ? *q + 1 : *q_high;
   4278 
   4279       // Adjust Q
   4280       *q = (int)((*q * low_err_target) / kf_err);
   4281       *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
   4282     }
   4283 
   4284     // Clamp Q to upper and lower limits:
   4285     *q = clamp(*q, *q_low, *q_high);
   4286 
   4287     *loop = *q != last_q;
   4288   } else if (recode_loop_test(cpi, frame_over_shoot_limit,
   4289                               frame_under_shoot_limit, *q,
   4290                               AOMMAX(*q_high, top_index), bottom_index)) {
   4291     // Is the projected frame size out of range and are we allowed
   4292     // to attempt to recode.
   4293     int last_q = *q;
   4294 
   4295     // Frame size out of permitted range:
   4296     // Update correction factor & compute new Q to try...
   4297     // Frame is too large
   4298     if (rc->projected_frame_size > rc->this_frame_target) {
   4299       // Special case if the projected size is > the max allowed.
   4300       if (rc->projected_frame_size >= rc->max_frame_bandwidth)
   4301         *q_high = rc->worst_quality;
   4302 
   4303       // Raise Qlow as to at least the current value
   4304       *q_low = *q < *q_high ? *q + 1 : *q_high;
   4305 
   4306       if (*undershoot_seen || loop_at_this_size > 2 ||
   4307           (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
   4308         av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
   4309 
   4310         *q = (*q_high + *q_low + 1) / 2;
   4311       } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
   4312         const int q_mid = (*q_high + *q_low + 1) / 2;
   4313         const int q_regulated = get_regulated_q_overshoot(
   4314             cpi, *q_low, *q_high, top_index, bottom_index);
   4315         // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
   4316         // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
   4317         *q = (q_mid + q_regulated + 1) / 2;
   4318       } else {
   4319         *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
   4320                                        bottom_index);
   4321       }
   4322 
   4323       *overshoot_seen = 1;
   4324     } else {
   4325       // Frame is too small
   4326       *q_high = *q > *q_low ? *q - 1 : *q_low;
   4327 
   4328       if (*overshoot_seen || loop_at_this_size > 2 ||
   4329           (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
   4330         av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
   4331         *q = (*q_high + *q_low) / 2;
   4332       } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
   4333         const int q_mid = (*q_high + *q_low) / 2;
   4334         const int q_regulated =
   4335             get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
   4336         // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
   4337         // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
   4338         *q = (q_mid + q_regulated) / 2;
   4339 
   4340         // Special case reset for qlow for constrained quality.
   4341         // This should only trigger where there is very substantial
   4342         // undershoot on a frame and the auto cq level is above
   4343         // the user passsed in value.
   4344         if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
   4345           *q_low = *q;
   4346         }
   4347       } else {
   4348         *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
   4349 
   4350         // Special case reset for qlow for constrained quality.
   4351         // This should only trigger where there is very substantial
   4352         // undershoot on a frame and the auto cq level is above
   4353         // the user passsed in value.
   4354         if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
   4355           *q_low = *q;
   4356         }
   4357       }
   4358 
   4359       *undershoot_seen = 1;
   4360     }
   4361 
   4362     // Clamp Q to upper and lower limits:
   4363     *q = clamp(*q, *q_low, *q_high);
   4364 
   4365     *loop = (*q != last_q);
   4366   } else {
   4367     *loop = 0;
   4368   }
   4369 }
   4370 
   4371 static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
   4372   AV1_COMMON *const cm = &cpi->common;
   4373   RATE_CONTROL *const rc = &cpi->rc;
   4374   const int allow_recode = cpi->sf.recode_loop != DISALLOW_RECODE;
   4375 
   4376   set_size_independent_vars(cpi);
   4377 
   4378   cpi->source->buf_8bit_valid = 0;
   4379 
   4380   av1_setup_frame_size(cpi);
   4381 
   4382   int top_index = 0, bottom_index = 0;
   4383   int q = 0, q_low = 0, q_high = 0;
   4384   set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
   4385   q_low = bottom_index;
   4386   q_high = top_index;
   4387 
   4388   // Loop variables
   4389   int loop_count = 0;
   4390   int loop_at_this_size = 0;
   4391   int loop = 0;
   4392   int overshoot_seen = 0;
   4393   int undershoot_seen = 0;
   4394 
   4395 #if CONFIG_COLLECT_COMPONENT_TIMING
   4396   printf("\n Encoding a frame:");
   4397 #endif
   4398   do {
   4399     aom_clear_system_state();
   4400 
   4401     // if frame was scaled calculate global_motion_search again if already
   4402     // done
   4403     if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
   4404       if (cpi->source->y_crop_width != cm->width ||
   4405           cpi->source->y_crop_height != cm->height) {
   4406         cpi->global_motion_search_done = 0;
   4407       }
   4408     }
   4409     cpi->source =
   4410         av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
   4411     if (cpi->unscaled_last_source != NULL) {
   4412       cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
   4413                                                &cpi->scaled_last_source);
   4414     }
   4415 
   4416     if (!frame_is_intra_only(cm)) {
   4417       if (loop_count > 0) {
   4418         release_scaled_references(cpi);
   4419       }
   4420       scale_references(cpi);
   4421     }
   4422     av1_set_quantizer(cm, q);
   4423     av1_init_quantizer(cpi);
   4424 
   4425     av1_set_variance_partition_thresholds(cpi, q, 0);
   4426 
   4427     // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
   4428     //        cm->current_frame.frame_number, cm->show_frame, q,
   4429     //        cm->current_frame.frame_type, cm->superres_scale_denominator);
   4430 
   4431     if (loop_count == 0) {
   4432       setup_frame(cpi);
   4433     } else if (get_primary_ref_frame_buf(cm) == NULL) {
   4434       // Base q-index may have changed, so we need to assign proper default coef
   4435       // probs before every iteration.
   4436       av1_default_coef_probs(cm);
   4437       av1_setup_frame_contexts(cm);
   4438     }
   4439 
   4440     if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   4441       av1_vaq_frame_setup(cpi);
   4442     } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
   4443       av1_setup_in_frame_q_adj(cpi);
   4444     } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
   4445       suppress_active_map(cpi);
   4446       av1_cyclic_refresh_setup(cpi);
   4447       apply_active_map(cpi);
   4448     }
   4449 
   4450     if (cm->seg.enabled) {
   4451       if (!cm->seg.update_data && cm->prev_frame) {
   4452         segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
   4453       } else {
   4454         calculate_segdata(&cm->seg);
   4455       }
   4456     } else {
   4457       memset(&cm->seg, 0, sizeof(cm->seg));
   4458     }
   4459     segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
   4460 
   4461     if (allow_recode) save_coding_context(cpi);
   4462 #if CONFIG_COLLECT_COMPONENT_TIMING
   4463     start_timing(cpi, av1_encode_frame_time);
   4464 #endif
   4465     // transform / motion compensation build reconstruction frame
   4466     av1_encode_frame(cpi);
   4467 #if CONFIG_COLLECT_COMPONENT_TIMING
   4468     end_timing(cpi, av1_encode_frame_time);
   4469 #endif
   4470 
   4471     aom_clear_system_state();
   4472 
   4473     // Dummy pack of the bitstream using up to date stats to get an
   4474     // accurate estimate of output frame size to determine if we need
   4475     // to recode.
   4476     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
   4477       restore_coding_context(cpi);
   4478 
   4479       finalize_encoded_frame(cpi);
   4480       int largest_tile_id = 0;  // Output from bitstream: unused here
   4481       if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
   4482         return AOM_CODEC_ERROR;
   4483 
   4484       rc->projected_frame_size = (int)(*size) << 3;
   4485       restore_coding_context(cpi);
   4486     }
   4487 
   4488     if (allow_recode && cpi->oxcf.rc_mode != AOM_Q) {
   4489       // Update q and decide whether to do a recode loop
   4490       recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
   4491                            bottom_index, &undershoot_seen, &overshoot_seen,
   4492                            loop_at_this_size);
   4493     }
   4494 
   4495     // Special case for overlay frame.
   4496     if (rc->is_src_frame_alt_ref &&
   4497         rc->projected_frame_size < rc->max_frame_bandwidth)
   4498       loop = 0;
   4499 
   4500     if (allow_recode && !cpi->sf.gm_disable_recode &&
   4501         recode_loop_test_global_motion(cpi)) {
   4502       loop = 1;
   4503     }
   4504 
   4505     if (loop) {
   4506       ++loop_count;
   4507       ++loop_at_this_size;
   4508 
   4509 #if CONFIG_INTERNAL_STATS
   4510       ++cpi->tot_recode_hits;
   4511 #endif
   4512     }
   4513 #if CONFIG_COLLECT_COMPONENT_TIMING
   4514     if (loop) printf("\n Recoding:");
   4515 #endif
   4516   } while (loop);
   4517 
   4518   return AOM_CODEC_OK;
   4519 }
   4520 
   4521 #define DUMP_RECON_FRAMES 0
   4522 
   4523 #if DUMP_RECON_FRAMES == 1
   4524 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
   4525 static void dump_filtered_recon_frames(AV1_COMP *cpi) {
   4526   AV1_COMMON *const cm = &cpi->common;
   4527   const CurrentFrame *const current_frame = &cm->current_frame;
   4528   const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
   4529 
   4530   if (recon_buf == NULL) {
   4531     printf("Frame %d is not ready.\n", current_frame->frame_number);
   4532     return;
   4533   }
   4534 
   4535   static const int flag_list[REF_FRAMES] = { 0,
   4536                                              AOM_LAST_FLAG,
   4537                                              AOM_LAST2_FLAG,
   4538                                              AOM_LAST3_FLAG,
   4539                                              AOM_GOLD_FLAG,
   4540                                              AOM_BWD_FLAG,
   4541                                              AOM_ALT2_FLAG,
   4542                                              AOM_ALT_FLAG };
   4543   printf(
   4544       "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
   4545       "show_existing_frame=%d) "
   4546       "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
   4547       current_frame->frame_number, current_frame->order_hint, cm->show_frame,
   4548       cm->show_existing_frame);
   4549   for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
   4550     const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
   4551     const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
   4552     printf(" %d(%c)", ref_offset,
   4553            (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
   4554   }
   4555   printf(" ]\n");
   4556 
   4557   if (!cm->show_frame) {
   4558     printf("Frame %d is a no show frame, so no image dump.\n",
   4559            current_frame->frame_number);
   4560     return;
   4561   }
   4562 
   4563   int h;
   4564   char file_name[256] = "/tmp/enc_filtered_recon.yuv";
   4565   FILE *f_recon = NULL;
   4566 
   4567   if (current_frame->frame_number == 0) {
   4568     if ((f_recon = fopen(file_name, "wb")) == NULL) {
   4569       printf("Unable to open file %s to write.\n", file_name);
   4570       return;
   4571     }
   4572   } else {
   4573     if ((f_recon = fopen(file_name, "ab")) == NULL) {
   4574       printf("Unable to open file %s to append.\n", file_name);
   4575       return;
   4576     }
   4577   }
   4578   printf(
   4579       "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
   4580       "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
   4581       "refresh_alt_ref_frame=%d, "
   4582       "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
   4583       current_frame->frame_number, cpi->twopass.gf_group.index,
   4584       cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
   4585       current_frame->order_hint, cm->show_frame, cm->show_existing_frame,
   4586       cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
   4587       recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
   4588 #if 0
   4589   int ref_frame;
   4590   printf("get_ref_frame_map_idx: [");
   4591   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
   4592     printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
   4593   printf(" ]\n");
   4594 #endif  // 0
   4595 
   4596   // --- Y ---
   4597   for (h = 0; h < cm->height; ++h) {
   4598     fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
   4599            f_recon);
   4600   }
   4601   // --- U ---
   4602   for (h = 0; h < (cm->height >> 1); ++h) {
   4603     fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
   4604            f_recon);
   4605   }
   4606   // --- V ---
   4607   for (h = 0; h < (cm->height >> 1); ++h) {
   4608     fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
   4609            f_recon);
   4610   }
   4611 
   4612   fclose(f_recon);
   4613 }
   4614 #endif  // DUMP_RECON_FRAMES
   4615 
   4616 static int get_interp_filter_selected(const AV1_COMMON *const cm,
   4617                                       MV_REFERENCE_FRAME ref,
   4618                                       InterpFilters ifilter) {
   4619   const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
   4620   if (buf == NULL) return 0;
   4621   return buf->interp_filter_selected[ifilter];
   4622 }
   4623 
   4624 static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
   4625   const AV1_COMMON *const cm = &cpi->common;
   4626   int ref_total[REF_FRAMES] = { 0 };
   4627 
   4628   if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
   4629     return 0;
   4630 
   4631   for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
   4632     for (InterpFilters ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
   4633          ++ifilter) {
   4634       ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
   4635     }
   4636   }
   4637   int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
   4638                          ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
   4639                          ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
   4640 
   4641   int mask = 0;
   4642   for (InterpFilters ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
   4643        ++ifilter) {
   4644     int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
   4645     if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
   4646       int filter_score =
   4647           get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
   4648           get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
   4649           get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
   4650           get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
   4651           get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
   4652           get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
   4653       if (filter_score < ref_total_total) mask |= 1 << ifilter;
   4654     }
   4655   }
   4656   return mask;
   4657 }
   4658 
   4659 static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
   4660                          const YV12_BUFFER_CONFIG *last_picture,
   4661                          hash_table *last_hash_table) {
   4662   aom_clear_system_state();
   4663   // check use hash ME
   4664   int k;
   4665   uint32_t hash_value_1;
   4666   uint32_t hash_value_2;
   4667 
   4668   const int block_size = 8;
   4669   const double threshold_current = 0.8;
   4670   const double threshold_average = 0.95;
   4671   const int max_history_size = 32;
   4672   int T = 0;  // total block
   4673   int C = 0;  // match with collocated block
   4674   int S = 0;  // smooth region but not match with collocated block
   4675   int M = 0;  // match with other block
   4676 
   4677   const int pic_width = cur_picture->y_width;
   4678   const int pic_height = cur_picture->y_height;
   4679   for (int i = 0; i + block_size <= pic_height; i += block_size) {
   4680     for (int j = 0; j + block_size <= pic_width; j += block_size) {
   4681       const int x_pos = j;
   4682       const int y_pos = i;
   4683       int match = 1;
   4684       T++;
   4685 
   4686       // check whether collocated block match with current
   4687       uint8_t *p_cur = cur_picture->y_buffer;
   4688       uint8_t *p_ref = last_picture->y_buffer;
   4689       int stride_cur = cur_picture->y_stride;
   4690       int stride_ref = last_picture->y_stride;
   4691       p_cur += (y_pos * stride_cur + x_pos);
   4692       p_ref += (y_pos * stride_ref + x_pos);
   4693 
   4694       if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
   4695         uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
   4696         uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
   4697         for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
   4698           for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
   4699             if (p16_cur[tmpX] != p16_ref[tmpX]) {
   4700               match = 0;
   4701             }
   4702           }
   4703           p16_cur += stride_cur;
   4704           p16_ref += stride_ref;
   4705         }
   4706       } else {
   4707         for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
   4708           for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
   4709             if (p_cur[tmpX] != p_ref[tmpX]) {
   4710               match = 0;
   4711             }
   4712           }
   4713           p_cur += stride_cur;
   4714           p_ref += stride_ref;
   4715         }
   4716       }
   4717 
   4718       if (match) {
   4719         C++;
   4720         continue;
   4721       }
   4722 
   4723       if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
   4724                                          y_pos) ||
   4725           av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
   4726         S++;
   4727         continue;
   4728       }
   4729 
   4730       av1_get_block_hash_value(
   4731           cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
   4732           block_size, &hash_value_1, &hash_value_2,
   4733           (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
   4734       // Hashing does not work for highbitdepth currently.
   4735       // TODO(Roger): Make it work for highbitdepth.
   4736       if (av1_use_hash_me(&cpi->common)) {
   4737         if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
   4738           M++;
   4739         }
   4740       }
   4741     }
   4742   }
   4743 
   4744   assert(T > 0);
   4745   double csm_rate = ((double)(C + S + M)) / ((double)(T));
   4746   double m_rate = ((double)(M)) / ((double)(T));
   4747 
   4748   cpi->csm_rate_array[cpi->rate_index] = csm_rate;
   4749   cpi->m_rate_array[cpi->rate_index] = m_rate;
   4750 
   4751   cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
   4752   cpi->rate_size++;
   4753   cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
   4754 
   4755   if (csm_rate < threshold_current) {
   4756     return 0;
   4757   }
   4758 
   4759   if (C == T) {
   4760     return 1;
   4761   }
   4762 
   4763   double csm_average = 0.0;
   4764   double m_average = 0.0;
   4765 
   4766   for (k = 0; k < cpi->rate_size; k++) {
   4767     csm_average += cpi->csm_rate_array[k];
   4768     m_average += cpi->m_rate_array[k];
   4769   }
   4770   csm_average /= cpi->rate_size;
   4771   m_average /= cpi->rate_size;
   4772 
   4773   if (csm_average < threshold_average) {
   4774     return 0;
   4775   }
   4776 
   4777   if (M > (T - C - S) / 3) {
   4778     return 1;
   4779   }
   4780 
   4781   if (csm_rate > 0.99 && m_rate > 0.01) {
   4782     return 1;
   4783   }
   4784 
   4785   if (csm_average + m_average > 1.01) {
   4786     return 1;
   4787   }
   4788 
   4789   return 0;
   4790 }
   4791 
   4792 // Refresh reference frame buffers according to refresh_frame_flags.
   4793 static void refresh_reference_frames(AV1_COMP *cpi) {
   4794   AV1_COMMON *const cm = &cpi->common;
   4795   // All buffers are refreshed for shown keyframes and S-frames.
   4796 
   4797   for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
   4798     if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
   4799       assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
   4800     }
   4801   }
   4802 }
   4803 
   4804 static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
   4805                                      uint8_t *dest) {
   4806   AV1_COMMON *const cm = &cpi->common;
   4807   SequenceHeader *const seq_params = &cm->seq_params;
   4808   CurrentFrame *const current_frame = &cm->current_frame;
   4809   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
   4810   struct segmentation *const seg = &cm->seg;
   4811 
   4812 #if CONFIG_COLLECT_COMPONENT_TIMING
   4813   start_timing(cpi, encode_frame_to_data_rate_time);
   4814 #endif
   4815 
   4816   // frame type has been decided outside of this function call
   4817   cm->cur_frame->frame_type = current_frame->frame_type;
   4818 
   4819   cm->large_scale_tile = cpi->oxcf.large_scale_tile;
   4820   cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
   4821 
   4822   cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
   4823   // cm->allow_ref_frame_mvs needs to be written into the frame header while
   4824   // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
   4825   // separated from frame_might_allow_ref_frame_mvs().
   4826   cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
   4827 
   4828   cm->allow_warped_motion =
   4829       cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
   4830 
   4831   cm->last_frame_type = current_frame->frame_type;
   4832   if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
   4833     cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
   4834 
   4835   cpi->two_pass_partition_search = cpi->sf.two_pass_partition_search &&
   4836                                    !cpi->partition_search_skippable_frame;
   4837 
   4838   if (encode_show_existing_frame(cm)) {
   4839     restore_coding_context(cpi);
   4840 
   4841     finalize_encoded_frame(cpi);
   4842     // Build the bitstream
   4843     int largest_tile_id = 0;  // Output from bitstream: unused here
   4844     if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
   4845       return AOM_CODEC_ERROR;
   4846 
   4847     if (seq_params->frame_id_numbers_present_flag &&
   4848         current_frame->frame_type == KEY_FRAME) {
   4849       // Displaying a forward key-frame, so reset the ref buffer IDs
   4850       int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
   4851       for (int i = 0; i < REF_FRAMES; i++)
   4852         cm->ref_frame_id[i] = display_frame_id;
   4853     }
   4854 
   4855     cpi->seq_params_locked = 1;
   4856 
   4857 #if DUMP_RECON_FRAMES == 1
   4858     // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
   4859     dump_filtered_recon_frames(cpi);
   4860 #endif  // DUMP_RECON_FRAMES
   4861 
   4862     // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
   4863     //       for the purpose to verify no mismatch between encoder and decoder.
   4864     if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
   4865 
   4866     refresh_reference_frames(cpi);
   4867 
   4868     // Since we allocate a spot for the OVERLAY frame in the gf group, we need
   4869     // to do post-encoding update accordingly.
   4870     if (cpi->rc.is_src_frame_alt_ref) {
   4871       av1_set_target_rate(cpi, cm->width, cm->height);
   4872       av1_rc_postencode_update(cpi, *size);
   4873     }
   4874 
   4875     ++current_frame->frame_number;
   4876 
   4877     return AOM_CODEC_OK;
   4878   }
   4879 
   4880   // Work out whether to force_integer_mv this frame
   4881   if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
   4882       !frame_is_intra_only(cm)) {
   4883     if (cpi->common.seq_params.force_integer_mv == 2) {
   4884       // Adaptive mode: see what previous frame encoded did
   4885       if (cpi->unscaled_last_source != NULL) {
   4886         cm->cur_frame_force_integer_mv =
   4887             is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
   4888                           cpi->previous_hash_table);
   4889       } else {
   4890         cpi->common.cur_frame_force_integer_mv = 0;
   4891       }
   4892     } else {
   4893       cpi->common.cur_frame_force_integer_mv =
   4894           cpi->common.seq_params.force_integer_mv;
   4895     }
   4896   } else {
   4897     cpi->common.cur_frame_force_integer_mv = 0;
   4898   }
   4899 
   4900   // Set default state for segment based loop filter update flags.
   4901   cm->lf.mode_ref_delta_update = 0;
   4902 
   4903   // Set various flags etc to special state if it is a key frame.
   4904   if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
   4905     // Reset the loop filter deltas and segmentation map.
   4906     av1_reset_segment_features(cm);
   4907 
   4908     // If segmentation is enabled force a map update for key frames.
   4909     if (seg->enabled) {
   4910       seg->update_map = 1;
   4911       seg->update_data = 1;
   4912     }
   4913 
   4914     // The alternate reference frame cannot be active for a key frame.
   4915     cpi->rc.source_alt_ref_active = 0;
   4916   }
   4917   if (cpi->oxcf.mtu == 0) {
   4918     cm->num_tg = cpi->oxcf.num_tile_groups;
   4919   } else {
   4920     // Use a default value for the purposes of weighting costs in probability
   4921     // updates
   4922     cm->num_tg = DEFAULT_MAX_NUM_TG;
   4923   }
   4924 
   4925   // For 1 pass CBR, check if we are dropping this frame.
   4926   // Never drop on key frame.
   4927   if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
   4928       current_frame->frame_type != KEY_FRAME) {
   4929     if (av1_rc_drop_frame(cpi)) {
   4930       av1_rc_postencode_update_drop_frame(cpi);
   4931       release_scaled_references(cpi);
   4932       return AOM_CODEC_OK;
   4933     }
   4934   }
   4935 
   4936   aom_clear_system_state();
   4937 
   4938 #if CONFIG_INTERNAL_STATS
   4939   memset(cpi->mode_chosen_counts, 0,
   4940          MAX_MODES * sizeof(*cpi->mode_chosen_counts));
   4941 #endif
   4942 
   4943   if (seq_params->frame_id_numbers_present_flag) {
   4944     /* Non-normative definition of current_frame_id ("frame counter" with
   4945      * wraparound) */
   4946     if (cm->current_frame_id == -1) {
   4947       int lsb, msb;
   4948       /* quasi-random initialization of current_frame_id for a key frame */
   4949       if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
   4950         lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
   4951         msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
   4952       } else {
   4953         lsb = cpi->source->y_buffer[0] & 0xff;
   4954         msb = cpi->source->y_buffer[1] & 0xff;
   4955       }
   4956       cm->current_frame_id =
   4957           ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
   4958 
   4959       // S_frame is meant for stitching different streams of different
   4960       // resolutions together, so current_frame_id must be the
   4961       // same across different streams of the same content current_frame_id
   4962       // should be the same and not random. 0x37 is a chosen number as start
   4963       // point
   4964       if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
   4965     } else {
   4966       cm->current_frame_id =
   4967           (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
   4968           (1 << seq_params->frame_id_length);
   4969     }
   4970   }
   4971 
   4972   switch (cpi->oxcf.cdf_update_mode) {
   4973     case 0:  // No CDF update for any frames(4~6% compression loss).
   4974       cm->disable_cdf_update = 1;
   4975       break;
   4976     case 1:  // Enable CDF update for all frames.
   4977       cm->disable_cdf_update = 0;
   4978       break;
   4979     case 2:
   4980       // Strategically determine at which frames to do CDF update.
   4981       // Currently only enable CDF update for all-intra and no-show frames(1.5%
   4982       // compression loss).
   4983       // TODO(huisu (at) google.com): design schemes for various trade-offs between
   4984       // compression quality and decoding speed.
   4985       cm->disable_cdf_update =
   4986           (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
   4987       break;
   4988   }
   4989   cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
   4990 
   4991 #if CONFIG_COLLECT_COMPONENT_TIMING
   4992   start_timing(cpi, encode_with_recode_loop_time);
   4993 #endif
   4994   if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
   4995     return AOM_CODEC_ERROR;
   4996 #if CONFIG_COLLECT_COMPONENT_TIMING
   4997   end_timing(cpi, encode_with_recode_loop_time);
   4998 #endif
   4999 
   5000 #ifdef OUTPUT_YUV_SKINMAP
   5001   if (cpi->common.current_frame.frame_number > 1) {
   5002     av1_compute_skin_map(cpi, yuv_skinmap_file);
   5003   }
   5004 #endif  // OUTPUT_YUV_SKINMAP
   5005 
   5006   // Special case code to reduce pulsing when key frames are forced at a
   5007   // fixed interval. Note the reconstruction error if it is the frame before
   5008   // the force key frame
   5009   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
   5010     if (seq_params->use_highbitdepth) {
   5011       cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
   5012     } else {
   5013       cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
   5014     }
   5015   }
   5016 
   5017   cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
   5018   cm->cur_frame->buf.transfer_characteristics =
   5019       seq_params->transfer_characteristics;
   5020   cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
   5021   cm->cur_frame->buf.monochrome = seq_params->monochrome;
   5022   cm->cur_frame->buf.chroma_sample_position =
   5023       seq_params->chroma_sample_position;
   5024   cm->cur_frame->buf.color_range = seq_params->color_range;
   5025   cm->cur_frame->buf.render_width = cm->render_width;
   5026   cm->cur_frame->buf.render_height = cm->render_height;
   5027 
   5028   // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
   5029   // off.
   5030 
   5031   // Pick the loop filter level for the frame.
   5032   if (!cm->allow_intrabc) {
   5033     loopfilter_frame(cpi, cm);
   5034   } else {
   5035     cm->lf.filter_level[0] = 0;
   5036     cm->lf.filter_level[1] = 0;
   5037     cm->cdef_info.cdef_bits = 0;
   5038     cm->cdef_info.cdef_strengths[0] = 0;
   5039     cm->cdef_info.nb_cdef_strengths = 1;
   5040     cm->cdef_info.cdef_uv_strengths[0] = 0;
   5041     cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
   5042     cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
   5043     cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
   5044   }
   5045 
   5046   // TODO(debargha): Fix mv search range on encoder side
   5047   // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
   5048   aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
   5049 
   5050 #ifdef OUTPUT_YUV_REC
   5051   aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
   5052 #endif
   5053 
   5054   finalize_encoded_frame(cpi);
   5055   // Build the bitstream
   5056   int largest_tile_id = 0;  // Output from pack_bitstream
   5057 #if CONFIG_COLLECT_COMPONENT_TIMING
   5058   start_timing(cpi, av1_pack_bitstream_final_time);
   5059 #endif
   5060   if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
   5061     return AOM_CODEC_ERROR;
   5062 #if CONFIG_COLLECT_COMPONENT_TIMING
   5063   end_timing(cpi, av1_pack_bitstream_final_time);
   5064 #endif
   5065 
   5066   cpi->seq_params_locked = 1;
   5067 
   5068   // Update reference frame ids for reference frames this frame will overwrite
   5069   if (seq_params->frame_id_numbers_present_flag) {
   5070     for (int i = 0; i < REF_FRAMES; i++) {
   5071       if ((current_frame->refresh_frame_flags >> i) & 1) {
   5072         cm->ref_frame_id[i] = cm->current_frame_id;
   5073       }
   5074     }
   5075   }
   5076 
   5077 #if DUMP_RECON_FRAMES == 1
   5078   // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
   5079   dump_filtered_recon_frames(cpi);
   5080 #endif  // DUMP_RECON_FRAMES
   5081 
   5082   if (cm->seg.enabled) {
   5083     if (cm->seg.update_map) {
   5084       update_reference_segmentation_map(cpi);
   5085     } else if (cm->last_frame_seg_map) {
   5086       memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
   5087              cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
   5088     }
   5089   }
   5090 
   5091   if (frame_is_intra_only(cm) == 0) {
   5092     release_scaled_references(cpi);
   5093   }
   5094 
   5095   // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
   5096   //       for the purpose to verify no mismatch between encoder and decoder.
   5097   if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
   5098 
   5099   refresh_reference_frames(cpi);
   5100 
   5101 #if CONFIG_ENTROPY_STATS
   5102   av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
   5103 #endif  // CONFIG_ENTROPY_STATS
   5104 
   5105   if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
   5106     *cm->fc = cpi->tile_data[largest_tile_id].tctx;
   5107     av1_reset_cdf_symbol_counters(cm->fc);
   5108   }
   5109   if (!cm->large_scale_tile) {
   5110     cm->cur_frame->frame_context = *cm->fc;
   5111   }
   5112 #define EXT_TILE_DEBUG 0
   5113 #if EXT_TILE_DEBUG
   5114   if (cm->large_scale_tile && oxcf->pass == 2) {
   5115     char fn[20] = "./fc";
   5116     fn[4] = current_frame->frame_number / 100 + '0';
   5117     fn[5] = (current_frame->frame_number % 100) / 10 + '0';
   5118     fn[6] = (current_frame->frame_number % 10) + '0';
   5119     fn[7] = '\0';
   5120     av1_print_frame_contexts(cm->fc, fn);
   5121   }
   5122 #endif  // EXT_TILE_DEBUG
   5123 #undef EXT_TILE_DEBUG
   5124 
   5125 #if CONFIG_COLLECT_COMPONENT_TIMING
   5126   end_timing(cpi, encode_frame_to_data_rate_time);
   5127 
   5128   // Print out timing information.
   5129   int i;
   5130   fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
   5131           cm->current_frame.frame_number,
   5132           get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
   5133   for (i = 0; i < kTimingComponents; i++) {
   5134     cpi->component_time[i] += cpi->frame_component_time[i];
   5135     fprintf(stderr, " %s:  %" PRId64 " us (total: %" PRId64 " us)\n",
   5136             get_component_name(i), cpi->frame_component_time[i],
   5137             cpi->component_time[i]);
   5138     cpi->frame_component_time[i] = 0;
   5139   }
   5140 #endif
   5141 
   5142   cm->last_frame_type = current_frame->frame_type;
   5143 
   5144   av1_rc_postencode_update(cpi, *size);
   5145 
   5146   // Store encoded frame's hash table for is_integer_mv() next time
   5147   if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
   5148     cpi->previous_hash_table = &cm->cur_frame->hash_table;
   5149   }
   5150 
   5151   // Clear the one shot update flags for segmentation map and mode/ref loop
   5152   // filter deltas.
   5153   cm->seg.update_map = 0;
   5154   cm->seg.update_data = 0;
   5155   cm->lf.mode_ref_delta_update = 0;
   5156 
   5157   // A droppable frame might not be shown but it always
   5158   // takes a space in the gf group. Therefore, even when
   5159   // it is not shown, we still need update the count down.
   5160 
   5161   if (cm->show_frame) {
   5162     // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
   5163     // are
   5164     // being used as reference.
   5165     swap_mi_and_prev_mi(cm);
   5166     // Don't increment frame counters if this was an altref buffer
   5167     // update not a real frame
   5168 
   5169     ++current_frame->frame_number;
   5170   }
   5171 
   5172   return AOM_CODEC_OK;
   5173 }
   5174 
   5175 int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
   5176                const EncodeFrameInput *const frame_input,
   5177                const EncodeFrameParams *const frame_params,
   5178                EncodeFrameResults *const frame_results) {
   5179   AV1_COMMON *const cm = &cpi->common;
   5180   CurrentFrame *const current_frame = &cm->current_frame;
   5181 
   5182   cpi->unscaled_source = frame_input->source;
   5183   cpi->source = frame_input->source;
   5184   cpi->unscaled_last_source = frame_input->last_source;
   5185 
   5186   current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
   5187   cm->error_resilient_mode = frame_params->error_resilient_mode;
   5188   cm->primary_ref_frame = frame_params->primary_ref_frame;
   5189   cm->current_frame.frame_type = frame_params->frame_type;
   5190   cm->show_frame = frame_params->show_frame;
   5191   cpi->ref_frame_flags = frame_params->ref_frame_flags;
   5192   cpi->speed = frame_params->speed;
   5193   cm->show_existing_frame = frame_params->show_existing_frame;
   5194   cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
   5195 
   5196   memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
   5197          REF_FRAMES * sizeof(*cm->remapped_ref_idx));
   5198 
   5199   cpi->refresh_last_frame = frame_params->refresh_last_frame;
   5200   cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
   5201   cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
   5202   cpi->refresh_alt2_ref_frame = frame_params->refresh_alt2_ref_frame;
   5203   cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
   5204 
   5205   if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
   5206     current_frame->frame_number = 0;
   5207 
   5208   if (cm->show_existing_frame) {
   5209     current_frame->order_hint = cm->cur_frame->order_hint;
   5210   } else {
   5211     current_frame->order_hint =
   5212         current_frame->frame_number + frame_params->order_offset;
   5213     current_frame->order_hint %=
   5214         (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
   5215   }
   5216 
   5217   if (cpi->oxcf.pass == 1) {
   5218     av1_first_pass(cpi, frame_input->ts_duration);
   5219   } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
   5220     if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
   5221         AOM_CODEC_OK) {
   5222       return AOM_CODEC_ERROR;
   5223     }
   5224   } else {
   5225     return AOM_CODEC_ERROR;
   5226   }
   5227 
   5228   return AOM_CODEC_OK;
   5229 }
   5230 
   5231 #if CONFIG_DENOISE
   5232 static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
   5233                             int block_size, float noise_level,
   5234                             int64_t time_stamp, int64_t end_time) {
   5235   AV1_COMMON *const cm = &cpi->common;
   5236   if (!cpi->denoise_and_model) {
   5237     cpi->denoise_and_model = aom_denoise_and_model_alloc(
   5238         cm->seq_params.bit_depth, block_size, noise_level);
   5239     if (!cpi->denoise_and_model) {
   5240       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
   5241                          "Error allocating denoise and model");
   5242       return -1;
   5243     }
   5244   }
   5245   if (!cpi->film_grain_table) {
   5246     cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
   5247     if (!cpi->film_grain_table) {
   5248       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
   5249                          "Error allocating grain table");
   5250       return -1;
   5251     }
   5252     memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
   5253   }
   5254   if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
   5255                                 &cm->film_grain_params)) {
   5256     if (cm->film_grain_params.apply_grain) {
   5257       aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
   5258                                   &cm->film_grain_params);
   5259     }
   5260   }
   5261   return 0;
   5262 }
   5263 #endif
   5264 
   5265 int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
   5266                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
   5267                           int64_t end_time) {
   5268   AV1_COMMON *const cm = &cpi->common;
   5269   const SequenceHeader *const seq_params = &cm->seq_params;
   5270   int res = 0;
   5271   const int subsampling_x = sd->subsampling_x;
   5272   const int subsampling_y = sd->subsampling_y;
   5273   const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
   5274 
   5275   check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
   5276 
   5277 #if CONFIG_INTERNAL_STATS
   5278   struct aom_usec_timer timer;
   5279   aom_usec_timer_start(&timer);
   5280 #endif
   5281 #if CONFIG_DENOISE
   5282   if (cpi->oxcf.noise_level > 0)
   5283     if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
   5284                          cpi->oxcf.noise_level, time_stamp, end_time) < 0)
   5285       res = -1;
   5286 #endif  //  CONFIG_DENOISE
   5287 
   5288   if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
   5289                          use_highbitdepth, frame_flags))
   5290     res = -1;
   5291 #if CONFIG_INTERNAL_STATS
   5292   aom_usec_timer_mark(&timer);
   5293   cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
   5294 #endif
   5295   if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
   5296       (subsampling_x != 1 || subsampling_y != 1)) {
   5297     aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
   5298                        "Non-4:2:0 color format requires profile 1 or 2");
   5299     res = -1;
   5300   }
   5301   if ((seq_params->profile == PROFILE_1) &&
   5302       !(subsampling_x == 0 && subsampling_y == 0)) {
   5303     aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
   5304                        "Profile 1 requires 4:4:4 color format");
   5305     res = -1;
   5306   }
   5307   if ((seq_params->profile == PROFILE_2) &&
   5308       (seq_params->bit_depth <= AOM_BITS_10) &&
   5309       !(subsampling_x == 1 && subsampling_y == 0)) {
   5310     aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
   5311                        "Profile 2 bit-depth < 10 requires 4:2:2 color format");
   5312     res = -1;
   5313   }
   5314 
   5315   return res;
   5316 }
   5317 
   5318 #if CONFIG_INTERNAL_STATS
   5319 extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
   5320                                  const unsigned char *img2, int img2_pitch,
   5321                                  int width, int height);
   5322 
   5323 static void adjust_image_stat(double y, double u, double v, double all,
   5324                               ImageStat *s) {
   5325   s->stat[STAT_Y] += y;
   5326   s->stat[STAT_U] += u;
   5327   s->stat[STAT_V] += v;
   5328   s->stat[STAT_ALL] += all;
   5329   s->worst = AOMMIN(s->worst, all);
   5330 }
   5331 
   5332 static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
   5333   AV1_COMMON *const cm = &cpi->common;
   5334   double samples = 0.0;
   5335   uint32_t in_bit_depth = 8;
   5336   uint32_t bit_depth = 8;
   5337 
   5338 #if CONFIG_INTER_STATS_ONLY
   5339   if (cm->current_frame.frame_type == KEY_FRAME) return;  // skip key frame
   5340 #endif
   5341   cpi->bytes += frame_bytes;
   5342 
   5343   if (cm->seq_params.use_highbitdepth) {
   5344     in_bit_depth = cpi->oxcf.input_bit_depth;
   5345     bit_depth = cm->seq_params.bit_depth;
   5346   }
   5347   if (cm->show_frame) {
   5348     const YV12_BUFFER_CONFIG *orig = cpi->source;
   5349     const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
   5350     double y, u, v, frame_all;
   5351 
   5352     cpi->count++;
   5353     if (cpi->b_calculate_psnr) {
   5354       PSNR_STATS psnr;
   5355       double frame_ssim2 = 0.0, weight = 0.0;
   5356       aom_clear_system_state();
   5357       // TODO(yaowu): unify these two versions into one.
   5358       aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
   5359 
   5360       adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
   5361                         &cpi->psnr);
   5362       cpi->total_sq_error += psnr.sse[0];
   5363       cpi->total_samples += psnr.samples[0];
   5364       samples = psnr.samples[0];
   5365       // TODO(yaowu): unify these two versions into one.
   5366       if (cm->seq_params.use_highbitdepth)
   5367         frame_ssim2 =
   5368             aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
   5369       else
   5370         frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
   5371 
   5372       cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
   5373       cpi->summed_quality += frame_ssim2 * weight;
   5374       cpi->summed_weights += weight;
   5375 
   5376 #if 0
   5377       {
   5378         FILE *f = fopen("q_used.stt", "a");
   5379         double y2 = psnr.psnr[1];
   5380         double u2 = psnr.psnr[2];
   5381         double v2 = psnr.psnr[3];
   5382         double frame_psnr2 = psnr.psnr[0];
   5383         fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
   5384                 cm->current_frame.frame_number, y2, u2, v2,
   5385                 frame_psnr2, frame_ssim2);
   5386         fclose(f);
   5387       }
   5388 #endif
   5389     }
   5390     if (cpi->b_calculate_blockiness) {
   5391       if (!cm->seq_params.use_highbitdepth) {
   5392         const double frame_blockiness =
   5393             av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
   5394                                recon->y_stride, orig->y_width, orig->y_height);
   5395         cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
   5396         cpi->total_blockiness += frame_blockiness;
   5397       }
   5398 
   5399       if (cpi->b_calculate_consistency) {
   5400         if (!cm->seq_params.use_highbitdepth) {
   5401           const double this_inconsistency = aom_get_ssim_metrics(
   5402               orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
   5403               orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
   5404 
   5405           const double peak = (double)((1 << in_bit_depth) - 1);
   5406           const double consistency =
   5407               aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
   5408           if (consistency > 0.0)
   5409             cpi->worst_consistency =
   5410                 AOMMIN(cpi->worst_consistency, consistency);
   5411           cpi->total_inconsistency += this_inconsistency;
   5412         }
   5413       }
   5414     }
   5415 
   5416     frame_all =
   5417         aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
   5418     adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
   5419     frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
   5420     adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
   5421   }
   5422 }
   5423 #endif  // CONFIG_INTERNAL_STATS
   5424 int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
   5425                             size_t *size, uint8_t *dest, int64_t *time_stamp,
   5426                             int64_t *time_end, int flush,
   5427                             const aom_rational_t *timebase) {
   5428   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
   5429   AV1_COMMON *const cm = &cpi->common;
   5430 
   5431 #if CONFIG_BITSTREAM_DEBUG
   5432   assert(cpi->oxcf.max_threads == 0 &&
   5433          "bitstream debug tool does not support multithreading");
   5434   bitstream_queue_record_write();
   5435   bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
   5436                                   cm->show_frame);
   5437 #endif
   5438 
   5439   // Indicates whether or not to use an adaptive quantize b rather than
   5440   // the traditional version
   5441   cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
   5442 
   5443   cm->showable_frame = 0;
   5444   *size = 0;
   5445 #if CONFIG_INTERNAL_STATS
   5446   struct aom_usec_timer cmptimer;
   5447   aom_usec_timer_start(&cmptimer);
   5448 #endif
   5449   set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
   5450 
   5451   // Normal defaults
   5452   cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
   5453                                   ? REFRESH_FRAME_CONTEXT_DISABLED
   5454                                   : REFRESH_FRAME_CONTEXT_BACKWARD;
   5455   if (oxcf->large_scale_tile)
   5456     cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
   5457 
   5458   // Initialize fields related to forward keyframes
   5459   cpi->no_show_kf = 0;
   5460 
   5461   if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
   5462 
   5463   const int result = av1_encode_strategy(cpi, size, dest, frame_flags,
   5464                                          time_stamp, time_end, timebase, flush);
   5465   if (result != AOM_CODEC_OK && result != -1) {
   5466     return AOM_CODEC_ERROR;
   5467   } else if (result == -1) {
   5468     // Returning -1 indicates no frame encoded; more input is required
   5469     return -1;
   5470   }
   5471 #if CONFIG_INTERNAL_STATS
   5472   aom_usec_timer_mark(&cmptimer);
   5473   cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
   5474 #endif
   5475   if (cpi->b_calculate_psnr) {
   5476     if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
   5477       generate_psnr_packet(cpi);
   5478     }
   5479   }
   5480   if (cpi->keep_level_stats && oxcf->pass != 1)
   5481     av1_update_level_info(cpi, *size, *time_stamp, *time_end);
   5482 
   5483 #if CONFIG_INTERNAL_STATS
   5484   if (oxcf->pass != 1) {
   5485     compute_internal_stats(cpi, (int)(*size));
   5486   }
   5487 #endif  // CONFIG_INTERNAL_STATS
   5488 #if CONFIG_SPEED_STATS
   5489   if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
   5490     cpi->tx_search_count += cpi->td.mb.tx_search_count;
   5491     cpi->td.mb.tx_search_count = 0;
   5492   }
   5493 #endif  // CONFIG_SPEED_STATS
   5494 
   5495   aom_clear_system_state();
   5496 
   5497   return 0;
   5498 }
   5499 
   5500 int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
   5501   AV1_COMMON *cm = &cpi->common;
   5502   if (!cm->show_frame) {
   5503     return -1;
   5504   } else {
   5505     int ret;
   5506     if (cm->cur_frame != NULL) {
   5507       *dest = cm->cur_frame->buf;
   5508       dest->y_width = cm->width;
   5509       dest->y_height = cm->height;
   5510       dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
   5511       dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
   5512       ret = 0;
   5513     } else {
   5514       ret = -1;
   5515     }
   5516     aom_clear_system_state();
   5517     return ret;
   5518   }
   5519 }
   5520 
   5521 int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
   5522   if (cpi->last_show_frame_buf == NULL) return -1;
   5523 
   5524   *frame = cpi->last_show_frame_buf->buf;
   5525   return 0;
   5526 }
   5527 
   5528 static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
   5529                                        const YV12_BUFFER_CONFIG *b) {
   5530   return a->y_height == b->y_height && a->y_width == b->y_width &&
   5531          a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
   5532          a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
   5533          a->border == b->border &&
   5534          (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
   5535              (b->flags & YV12_FLAG_HIGHBITDEPTH);
   5536 }
   5537 
   5538 aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
   5539                                        YV12_BUFFER_CONFIG *new_frame,
   5540                                        YV12_BUFFER_CONFIG *sd) {
   5541   const int num_planes = av1_num_planes(cm);
   5542   if (!equal_dimensions_and_border(new_frame, sd))
   5543     aom_internal_error(&cm->error, AOM_CODEC_ERROR,
   5544                        "Incorrect buffer dimensions");
   5545   else
   5546     aom_yv12_copy_frame(new_frame, sd, num_planes);
   5547 
   5548   return cm->error.error_code;
   5549 }
   5550 
   5551 int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
   5552                           AOM_SCALING vert_mode) {
   5553   int hr = 0, hs = 0, vr = 0, vs = 0;
   5554 
   5555   if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
   5556 
   5557   Scale2Ratio(horiz_mode, &hr, &hs);
   5558   Scale2Ratio(vert_mode, &vr, &vs);
   5559 
   5560   // always go to the next whole number
   5561   cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
   5562   cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
   5563 
   5564   return 0;
   5565 }
   5566 
   5567 int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
   5568 
   5569 int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
   5570   size_t output_size = 0;
   5571   size_t total_bytes_read = 0;
   5572   size_t remaining_size = *frame_size;
   5573   uint8_t *buff_ptr = buffer;
   5574 
   5575   // go through each OBUs
   5576   while (total_bytes_read < *frame_size) {
   5577     uint8_t saved_obu_header[2];
   5578     uint64_t obu_payload_size;
   5579     size_t length_of_payload_size;
   5580     size_t length_of_obu_size;
   5581     uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
   5582     size_t obu_bytes_read = obu_header_size;  // bytes read for current obu
   5583 
   5584     // save the obu header (1 or 2 bytes)
   5585     memmove(saved_obu_header, buff_ptr, obu_header_size);
   5586     // clear the obu_has_size_field
   5587     saved_obu_header[0] = saved_obu_header[0] & (~0x2);
   5588 
   5589     // get the payload_size and length of payload_size
   5590     if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
   5591                         &obu_payload_size, &length_of_payload_size) != 0) {
   5592       return AOM_CODEC_ERROR;
   5593     }
   5594     obu_bytes_read += length_of_payload_size;
   5595 
   5596     // calculate the length of size of the obu header plus payload
   5597     length_of_obu_size =
   5598         aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
   5599 
   5600     // move the rest of data to new location
   5601     memmove(buff_ptr + length_of_obu_size + obu_header_size,
   5602             buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
   5603     obu_bytes_read += (size_t)obu_payload_size;
   5604 
   5605     // write the new obu size
   5606     const uint64_t obu_size = obu_header_size + obu_payload_size;
   5607     size_t coded_obu_size;
   5608     if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
   5609                         &coded_obu_size) != 0) {
   5610       return AOM_CODEC_ERROR;
   5611     }
   5612 
   5613     // write the saved (modified) obu_header following obu size
   5614     memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
   5615 
   5616     total_bytes_read += obu_bytes_read;
   5617     remaining_size -= obu_bytes_read;
   5618     buff_ptr += length_of_obu_size + obu_size;
   5619     output_size += length_of_obu_size + (size_t)obu_size;
   5620   }
   5621 
   5622   *frame_size = output_size;
   5623   return AOM_CODEC_OK;
   5624 }
   5625 
   5626 void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
   5627   // TODO(yunqingwang): For what references to use, external encoding flags
   5628   // should be consistent with internal reference frame selection. Need to
   5629   // ensure that there is not conflict between the two. In AV1 encoder, the
   5630   // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
   5631   // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
   5632   // LAST.
   5633   cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
   5634   if (flags &
   5635       (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
   5636        AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
   5637        AOM_EFLAG_NO_REF_ARF2)) {
   5638     if (flags & AOM_EFLAG_NO_REF_LAST) {
   5639       cpi->ext_ref_frame_flags = 0;
   5640     } else {
   5641       int ref = AOM_REFFRAME_ALL;
   5642 
   5643       if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
   5644       if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
   5645 
   5646       if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
   5647 
   5648       if (flags & AOM_EFLAG_NO_REF_ARF) {
   5649         ref ^= AOM_ALT_FLAG;
   5650         ref ^= AOM_BWD_FLAG;
   5651         ref ^= AOM_ALT2_FLAG;
   5652       } else {
   5653         if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
   5654         if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
   5655       }
   5656 
   5657       av1_use_as_reference(cpi, ref);
   5658     }
   5659   }
   5660 
   5661   if (flags &
   5662       (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
   5663     int upd = AOM_REFFRAME_ALL;
   5664 
   5665     // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
   5666     if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
   5667 
   5668     if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
   5669 
   5670     if (flags & AOM_EFLAG_NO_UPD_ARF) {
   5671       upd ^= AOM_ALT_FLAG;
   5672       upd ^= AOM_BWD_FLAG;
   5673       upd ^= AOM_ALT2_FLAG;
   5674     }
   5675 
   5676     cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
   5677     cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
   5678     cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
   5679     cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
   5680     cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
   5681     cpi->ext_refresh_frame_flags_pending = 1;
   5682   } else {
   5683     cpi->ext_refresh_frame_flags_pending = 0;
   5684   }
   5685 
   5686   cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
   5687                                ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
   5688   cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
   5689                                  ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
   5690   cpi->ext_use_s_frame =
   5691       cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
   5692   cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
   5693 
   5694   if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
   5695     av1_update_entropy(cpi, 0);
   5696   }
   5697 }
   5698 
   5699 aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
   5700   if (!cpi) return NULL;
   5701 
   5702   uint8_t header_buf[512] = { 0 };
   5703   const uint32_t sequence_header_size =
   5704       write_sequence_header_obu(cpi, &header_buf[0]);
   5705   assert(sequence_header_size <= sizeof(header_buf));
   5706   if (sequence_header_size == 0) return NULL;
   5707 
   5708   const size_t obu_header_size = 1;
   5709   const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
   5710   const size_t payload_offset = obu_header_size + size_field_size;
   5711 
   5712   if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
   5713   memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
   5714 
   5715   if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
   5716       obu_header_size) {
   5717     return NULL;
   5718   }
   5719 
   5720   size_t coded_size_field_size = 0;
   5721   if (aom_uleb_encode(sequence_header_size, size_field_size,
   5722                       &header_buf[obu_header_size],
   5723                       &coded_size_field_size) != 0) {
   5724     return NULL;
   5725   }
   5726   assert(coded_size_field_size == size_field_size);
   5727 
   5728   aom_fixed_buf_t *global_headers =
   5729       (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
   5730   if (!global_headers) return NULL;
   5731 
   5732   const size_t global_header_buf_size =
   5733       obu_header_size + size_field_size + sequence_header_size;
   5734 
   5735   global_headers->buf = malloc(global_header_buf_size);
   5736   if (!global_headers->buf) {
   5737     free(global_headers);
   5738     return NULL;
   5739   }
   5740 
   5741   memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
   5742   global_headers->sz = global_header_buf_size;
   5743   return global_headers;
   5744 }
   5745