LCOV - code coverage report
Current view: top level - third_party/aom/av1/encoder - encoder.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 2058 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 332 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "./aom_config.h"
      17             : 
      18             : #include "av1/common/alloccommon.h"
      19             : #if CONFIG_CDEF
      20             : #include "av1/common/cdef.h"
      21             : #include "av1/common/clpf.h"
      22             : #endif  // CONFIG_CDEF
      23             : #include "av1/common/filter.h"
      24             : #include "av1/common/idct.h"
      25             : #include "av1/common/reconinter.h"
      26             : #include "av1/common/reconintra.h"
      27             : #include "av1/common/resize.h"
      28             : #include "av1/common/tile_common.h"
      29             : 
      30             : #include "av1/encoder/aq_complexity.h"
      31             : #include "av1/encoder/aq_cyclicrefresh.h"
      32             : #include "av1/encoder/aq_variance.h"
      33             : #include "av1/encoder/bitstream.h"
      34             : #if CONFIG_ANS
      35             : #include "aom_dsp/buf_ans.h"
      36             : #endif
      37             : #include "av1/encoder/context_tree.h"
      38             : #include "av1/encoder/encodeframe.h"
      39             : #include "av1/encoder/encodemv.h"
      40             : #include "av1/encoder/encoder.h"
      41             : #if CONFIG_LV_MAP
      42             : #include "av1/encoder/encodetxb.h"
      43             : #endif
      44             : #include "av1/encoder/ethread.h"
      45             : #include "av1/encoder/firstpass.h"
      46             : #include "av1/encoder/mbgraph.h"
      47             : #include "av1/encoder/picklpf.h"
      48             : #if CONFIG_LOOP_RESTORATION
      49             : #include "av1/encoder/pickrst.h"
      50             : #endif  // CONFIG_LOOP_RESTORATION
      51             : #include "av1/encoder/ratectrl.h"
      52             : #include "av1/encoder/rd.h"
      53             : #include "av1/encoder/segmentation.h"
      54             : #include "av1/encoder/speed_features.h"
      55             : #include "av1/encoder/temporal_filter.h"
      56             : 
      57             : #include "./av1_rtcd.h"
      58             : #include "./aom_dsp_rtcd.h"
      59             : #include "./aom_scale_rtcd.h"
      60             : #include "aom_dsp/psnr.h"
      61             : #if CONFIG_INTERNAL_STATS
      62             : #include "aom_dsp/ssim.h"
      63             : #endif
      64             : #include "aom_dsp/aom_dsp_common.h"
      65             : #include "aom_dsp/aom_filter.h"
      66             : #include "aom_ports/aom_timer.h"
      67             : #include "aom_ports/mem.h"
      68             : #include "aom_ports/system_state.h"
      69             : #include "aom_scale/aom_scale.h"
      70             : #if CONFIG_BITSTREAM_DEBUG
      71             : #include "aom_util/debug_util.h"
      72             : #endif  // CONFIG_BITSTREAM_DEBUG
      73             : 
      74             : #if CONFIG_ENTROPY_STATS
      75             : FRAME_COUNTS aggregate_fc;
      76             : #endif  // CONFIG_ENTROPY_STATS
      77             : 
      78             : #define AM_SEGMENT_ID_INACTIVE 7
      79             : #define AM_SEGMENT_ID_ACTIVE 0
      80             : 
      81             : #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
      82             : 
      83             : #define ALTREF_HIGH_PRECISION_MV 1     // Whether to use high precision mv
      84             :                                        //  for altref computation.
      85             : #define HIGH_PRECISION_MV_QTHRESH 200  // Q threshold for high precision
      86             :                                        // mv. Choose a very high value for
      87             :                                        // now so that HIGH_PRECISION is always
      88             :                                        // chosen.
      89             : // #define OUTPUT_YUV_REC
      90             : #ifdef OUTPUT_YUV_DENOISED
      91             : FILE *yuv_denoised_file = NULL;
      92             : #endif
      93             : #ifdef OUTPUT_YUV_SKINMAP
      94             : FILE *yuv_skinmap_file = NULL;
      95             : #endif
      96             : #ifdef OUTPUT_YUV_REC
      97             : FILE *yuv_rec_file;
      98             : #define FILE_NAME_LEN 100
      99             : #endif
     100             : 
     101             : #if 0
     102             : FILE *framepsnr;
     103             : FILE *kf_list;
     104             : FILE *keyfile;
     105             : #endif
     106             : 
     107             : #if CONFIG_CFL
     108             : CFL_CTX NULL_CFL;
     109             : #endif
     110             : 
     111             : #if CONFIG_INTERNAL_STATS
     112             : typedef enum { Y, U, V, ALL } STAT_TYPE;
     113             : #endif  // CONFIG_INTERNAL_STATS
     114             : 
     115           0 : static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
     116           0 :   switch (mode) {
     117             :     case NORMAL:
     118           0 :       *hr = 1;
     119           0 :       *hs = 1;
     120           0 :       break;
     121             :     case FOURFIVE:
     122           0 :       *hr = 4;
     123           0 :       *hs = 5;
     124           0 :       break;
     125             :     case THREEFIVE:
     126           0 :       *hr = 3;
     127           0 :       *hs = 5;
     128           0 :       break;
     129             :     case ONETWO:
     130           0 :       *hr = 1;
     131           0 :       *hs = 2;
     132           0 :       break;
     133             :     default:
     134           0 :       *hr = 1;
     135           0 :       *hs = 1;
     136           0 :       assert(0);
     137             :       break;
     138             :   }
     139           0 : }
     140             : 
     141             : // Mark all inactive blocks as active. Other segmentation features may be set
     142             : // so memset cannot be used, instead only inactive blocks should be reset.
     143           0 : static void suppress_active_map(AV1_COMP *cpi) {
     144           0 :   unsigned char *const seg_map = cpi->segmentation_map;
     145             :   int i;
     146           0 :   if (cpi->active_map.enabled || cpi->active_map.update)
     147           0 :     for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
     148           0 :       if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
     149           0 :         seg_map[i] = AM_SEGMENT_ID_ACTIVE;
     150           0 : }
     151             : 
     152           0 : static void apply_active_map(AV1_COMP *cpi) {
     153           0 :   struct segmentation *const seg = &cpi->common.seg;
     154           0 :   unsigned char *const seg_map = cpi->segmentation_map;
     155           0 :   const unsigned char *const active_map = cpi->active_map.map;
     156             :   int i;
     157             : 
     158             :   assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
     159             : 
     160           0 :   if (frame_is_intra_only(&cpi->common)) {
     161           0 :     cpi->active_map.enabled = 0;
     162           0 :     cpi->active_map.update = 1;
     163             :   }
     164             : 
     165           0 :   if (cpi->active_map.update) {
     166           0 :     if (cpi->active_map.enabled) {
     167           0 :       for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
     168           0 :         if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
     169           0 :       av1_enable_segmentation(seg);
     170           0 :       av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
     171           0 :       av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
     172             :       // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
     173             :       // filter level being zero regardless of the value of seg->abs_delta.
     174           0 :       av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
     175             :                       -MAX_LOOP_FILTER);
     176             :     } else {
     177           0 :       av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
     178           0 :       av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
     179           0 :       if (seg->enabled) {
     180           0 :         seg->update_data = 1;
     181           0 :         seg->update_map = 1;
     182             :       }
     183             :     }
     184           0 :     cpi->active_map.update = 0;
     185             :   }
     186           0 : }
     187             : 
     188           0 : int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
     189             :                        int cols) {
     190           0 :   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
     191           0 :     unsigned char *const active_map_8x8 = cpi->active_map.map;
     192           0 :     const int mi_rows = cpi->common.mi_rows;
     193           0 :     const int mi_cols = cpi->common.mi_cols;
     194           0 :     const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
     195           0 :     const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
     196           0 :     cpi->active_map.update = 1;
     197           0 :     if (new_map_16x16) {
     198             :       int r, c;
     199           0 :       for (r = 0; r < mi_rows; ++r) {
     200           0 :         for (c = 0; c < mi_cols; ++c) {
     201           0 :           active_map_8x8[r * mi_cols + c] =
     202           0 :               new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
     203             :                   ? AM_SEGMENT_ID_ACTIVE
     204             :                   : AM_SEGMENT_ID_INACTIVE;
     205             :         }
     206             :       }
     207           0 :       cpi->active_map.enabled = 1;
     208             :     } else {
     209           0 :       cpi->active_map.enabled = 0;
     210             :     }
     211           0 :     return 0;
     212             :   } else {
     213           0 :     return -1;
     214             :   }
     215             : }
     216             : 
     217           0 : int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
     218             :                        int cols) {
     219           0 :   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
     220             :       new_map_16x16) {
     221           0 :     unsigned char *const seg_map_8x8 = cpi->segmentation_map;
     222           0 :     const int mi_rows = cpi->common.mi_rows;
     223           0 :     const int mi_cols = cpi->common.mi_cols;
     224           0 :     const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
     225           0 :     const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
     226             : 
     227           0 :     memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
     228           0 :     if (cpi->active_map.enabled) {
     229             :       int r, c;
     230           0 :       for (r = 0; r < mi_rows; ++r) {
     231           0 :         for (c = 0; c < mi_cols; ++c) {
     232             :           // Cyclic refresh segments are considered active despite not having
     233             :           // AM_SEGMENT_ID_ACTIVE
     234           0 :           new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
     235           0 :               seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
     236             :         }
     237             :       }
     238             :     }
     239           0 :     return 0;
     240             :   } else {
     241           0 :     return -1;
     242             :   }
     243             : }
     244             : 
     245           0 : void av1_set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv) {
     246           0 :   MACROBLOCK *const mb = &cpi->td.mb;
     247           0 :   cpi->common.allow_high_precision_mv = allow_high_precision_mv;
     248             : 
     249           0 :   if (cpi->common.allow_high_precision_mv) {
     250             :     int i;
     251           0 :     for (i = 0; i < NMV_CONTEXTS; ++i) {
     252           0 :       mb->mv_cost_stack[i] = mb->nmvcost_hp[i];
     253             :     }
     254             :   } else {
     255             :     int i;
     256           0 :     for (i = 0; i < NMV_CONTEXTS; ++i) {
     257           0 :       mb->mv_cost_stack[i] = mb->nmvcost[i];
     258             :     }
     259             :   }
     260           0 : }
     261             : 
     262           0 : static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
     263             : #if CONFIG_EXT_PARTITION
     264             :   if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
     265             :     return BLOCK_64X64;
     266             : 
     267             :   if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
     268             :     return BLOCK_128X128;
     269             : 
     270             :   assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
     271             : 
     272             :   assert(IMPLIES(cpi->common.tile_cols > 1,
     273             :                  cpi->common.tile_width % MAX_MIB_SIZE == 0));
     274             :   assert(IMPLIES(cpi->common.tile_rows > 1,
     275             :                  cpi->common.tile_height % MAX_MIB_SIZE == 0));
     276             : 
     277             :   // TODO(any): Possibly could improve this with a heuristic.
     278             :   return BLOCK_128X128;
     279             : #else
     280             :   (void)cpi;
     281           0 :   return BLOCK_64X64;
     282             : #endif  //  CONFIG_EXT_PARTITION
     283             : }
     284             : 
     285           0 : static void setup_frame(AV1_COMP *cpi) {
     286           0 :   AV1_COMMON *const cm = &cpi->common;
     287             :   // Set up entropy context depending on frame type. The decoder mandates
     288             :   // the use of the default context, index 0, for keyframes and inter
     289             :   // frames where the error_resilient_mode or intra_only flag is set. For
     290             :   // other inter-frames the encoder currently uses only two contexts;
     291             :   // context 1 for ALTREF frames and context 0 for the others.
     292           0 :   if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
     293           0 :     av1_setup_past_independence(cm);
     294             :   } else {
     295             : #if CONFIG_EXT_REFS
     296           0 :     const GF_GROUP *gf_group = &cpi->twopass.gf_group;
     297           0 :     if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
     298           0 :       cm->frame_context_idx = EXT_ARF_FRAME;
     299           0 :     else if (cpi->refresh_alt_ref_frame)
     300           0 :       cm->frame_context_idx = ARF_FRAME;
     301             : #else
     302             :     if (cpi->refresh_alt_ref_frame) cm->frame_context_idx = ARF_FRAME;
     303             : #endif  // CONFIG_EXT_REFS
     304           0 :     else if (cpi->rc.is_src_frame_alt_ref)
     305           0 :       cm->frame_context_idx = OVERLAY_FRAME;
     306           0 :     else if (cpi->refresh_golden_frame)
     307           0 :       cm->frame_context_idx = GLD_FRAME;
     308             : #if CONFIG_EXT_REFS
     309           0 :     else if (cpi->refresh_bwd_ref_frame)
     310           0 :       cm->frame_context_idx = BRF_FRAME;
     311             : #endif  // CONFIG_EXT_REFS
     312             :     else
     313           0 :       cm->frame_context_idx = REGULAR_FRAME;
     314             :   }
     315             : 
     316           0 :   if (cm->frame_type == KEY_FRAME) {
     317           0 :     cpi->refresh_golden_frame = 1;
     318           0 :     cpi->refresh_alt_ref_frame = 1;
     319           0 :     av1_zero(cpi->interp_filter_selected);
     320             :   } else {
     321           0 :     *cm->fc = cm->frame_contexts[cm->frame_context_idx];
     322           0 :     av1_zero(cpi->interp_filter_selected[0]);
     323             :   }
     324             : #if CONFIG_EXT_REFS
     325             : #if CONFIG_ONE_SIDED_COMPOUND  // No change to bitstream
     326           0 :   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
     327           0 :     cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
     328           0 :     cpi->rc.is_bipred_frame = 1;
     329             :   }
     330             : #endif
     331             : #endif
     332           0 :   cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
     333             : 
     334           0 :   cpi->vaq_refresh = 0;
     335             : 
     336           0 :   set_sb_size(cm, select_sb_size(cpi));
     337           0 : }
     338             : 
     339           0 : static void av1_enc_setup_mi(AV1_COMMON *cm) {
     340             :   int i;
     341           0 :   cm->mi = cm->mip + cm->mi_stride + 1;
     342           0 :   memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
     343           0 :   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
     344             :   // Clear top border row
     345           0 :   memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
     346             :   // Clear left border column
     347           0 :   for (i = 1; i < cm->mi_rows + 1; ++i)
     348           0 :     memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
     349             : 
     350           0 :   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
     351           0 :   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
     352             : 
     353           0 :   memset(cm->mi_grid_base, 0,
     354           0 :          cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
     355           0 : }
     356             : 
     357           0 : static int av1_enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
     358           0 :   cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
     359           0 :   if (!cm->mip) return 1;
     360           0 :   cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
     361           0 :   if (!cm->prev_mip) return 1;
     362           0 :   cm->mi_alloc_size = mi_size;
     363             : 
     364           0 :   cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
     365           0 :   if (!cm->mi_grid_base) return 1;
     366           0 :   cm->prev_mi_grid_base =
     367           0 :       (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
     368           0 :   if (!cm->prev_mi_grid_base) return 1;
     369             : 
     370           0 :   return 0;
     371             : }
     372             : 
     373           0 : static void av1_enc_free_mi(AV1_COMMON *cm) {
     374           0 :   aom_free(cm->mip);
     375           0 :   cm->mip = NULL;
     376           0 :   aom_free(cm->prev_mip);
     377           0 :   cm->prev_mip = NULL;
     378           0 :   aom_free(cm->mi_grid_base);
     379           0 :   cm->mi_grid_base = NULL;
     380           0 :   aom_free(cm->prev_mi_grid_base);
     381           0 :   cm->prev_mi_grid_base = NULL;
     382           0 : }
     383             : 
     384           0 : static void av1_swap_mi_and_prev_mi(AV1_COMMON *cm) {
     385             :   // Current mip will be the prev_mip for the next frame.
     386           0 :   MODE_INFO **temp_base = cm->prev_mi_grid_base;
     387           0 :   MODE_INFO *temp = cm->prev_mip;
     388           0 :   cm->prev_mip = cm->mip;
     389           0 :   cm->mip = temp;
     390             : 
     391             :   // Update the upper left visible macroblock ptrs.
     392           0 :   cm->mi = cm->mip + cm->mi_stride + 1;
     393           0 :   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
     394             : 
     395           0 :   cm->prev_mi_grid_base = cm->mi_grid_base;
     396           0 :   cm->mi_grid_base = temp_base;
     397           0 :   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
     398           0 :   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
     399           0 : }
     400             : 
     401           0 : void av1_initialize_enc(void) {
     402             :   static volatile int init_done = 0;
     403             : 
     404           0 :   if (!init_done) {
     405           0 :     av1_rtcd();
     406           0 :     aom_dsp_rtcd();
     407           0 :     aom_scale_rtcd();
     408           0 :     av1_init_intra_predictors();
     409           0 :     av1_init_me_luts();
     410             : #if !CONFIG_XIPHRC
     411           0 :     av1_rc_init_minq_luts();
     412             : #endif
     413           0 :     av1_entropy_mv_init();
     414           0 :     av1_encode_token_init();
     415             : #if CONFIG_EXT_INTER
     416           0 :     av1_init_wedge_masks();
     417             : #endif
     418           0 :     init_done = 1;
     419             :   }
     420           0 : }
     421             : 
     422           0 : static void dealloc_compressor_data(AV1_COMP *cpi) {
     423           0 :   AV1_COMMON *const cm = &cpi->common;
     424             :   int i;
     425             : 
     426           0 :   aom_free(cpi->mbmi_ext_base);
     427           0 :   cpi->mbmi_ext_base = NULL;
     428             : 
     429             : #if CONFIG_PVQ
     430             :   if (cpi->oxcf.pass != 1) {
     431             :     const int tile_cols = cm->tile_cols;
     432             :     const int tile_rows = cm->tile_rows;
     433             :     int tile_col, tile_row;
     434             : 
     435             :     for (tile_row = 0; tile_row < tile_rows; ++tile_row)
     436             :       for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
     437             :         TileDataEnc *tile_data =
     438             :             &cpi->tile_data[tile_row * tile_cols + tile_col];
     439             :         aom_free(tile_data->pvq_q.buf);
     440             :       }
     441             :   }
     442             : #endif
     443           0 :   aom_free(cpi->tile_data);
     444           0 :   cpi->tile_data = NULL;
     445             : 
     446             :   // Delete sementation map
     447           0 :   aom_free(cpi->segmentation_map);
     448           0 :   cpi->segmentation_map = NULL;
     449             : 
     450           0 :   av1_cyclic_refresh_free(cpi->cyclic_refresh);
     451           0 :   cpi->cyclic_refresh = NULL;
     452             : 
     453           0 :   aom_free(cpi->active_map.map);
     454           0 :   cpi->active_map.map = NULL;
     455             : 
     456             : #if CONFIG_MOTION_VAR
     457           0 :   aom_free(cpi->td.mb.above_pred_buf);
     458           0 :   cpi->td.mb.above_pred_buf = NULL;
     459             : 
     460           0 :   aom_free(cpi->td.mb.left_pred_buf);
     461           0 :   cpi->td.mb.left_pred_buf = NULL;
     462             : 
     463           0 :   aom_free(cpi->td.mb.wsrc_buf);
     464           0 :   cpi->td.mb.wsrc_buf = NULL;
     465             : 
     466           0 :   aom_free(cpi->td.mb.mask_buf);
     467           0 :   cpi->td.mb.mask_buf = NULL;
     468             : #endif
     469             : 
     470             :   // Free up-sampled reference buffers.
     471           0 :   for (i = 0; i < (REF_FRAMES + 1); i++)
     472           0 :     aom_free_frame_buffer(&cpi->upsampled_ref_bufs[i].buf);
     473             : 
     474           0 :   av1_free_ref_frame_buffers(cm->buffer_pool);
     475             : #if CONFIG_LV_MAP
     476             :   av1_free_txb_buf(cpi);
     477             : #endif
     478           0 :   av1_free_context_buffers(cm);
     479             : 
     480           0 :   aom_free_frame_buffer(&cpi->last_frame_uf);
     481             : #if CONFIG_LOOP_RESTORATION
     482             :   av1_free_restoration_buffers(cm);
     483             :   aom_free_frame_buffer(&cpi->last_frame_db);
     484             :   aom_free_frame_buffer(&cpi->trial_frame_rst);
     485             :   aom_free(cpi->extra_rstbuf);
     486             :   for (i = 0; i < MAX_MB_PLANE; ++i)
     487             :     av1_free_restoration_struct(&cpi->rst_search[i]);
     488             : #endif  // CONFIG_LOOP_RESTORATION
     489           0 :   aom_free_frame_buffer(&cpi->scaled_source);
     490           0 :   aom_free_frame_buffer(&cpi->scaled_last_source);
     491           0 :   aom_free_frame_buffer(&cpi->alt_ref_buffer);
     492           0 :   av1_lookahead_destroy(cpi->lookahead);
     493             : 
     494           0 :   aom_free(cpi->tile_tok[0][0]);
     495           0 :   cpi->tile_tok[0][0] = 0;
     496             : 
     497           0 :   av1_free_pc_tree(&cpi->td);
     498             : 
     499             : #if CONFIG_PALETTE
     500           0 :   if (cpi->common.allow_screen_content_tools)
     501           0 :     aom_free(cpi->td.mb.palette_buffer);
     502             : #endif  // CONFIG_PALETTE
     503             : 
     504             : #if CONFIG_ANS
     505             :   aom_buf_ans_free(&cpi->buf_ans);
     506             : #endif  // CONFIG_ANS
     507           0 : }
     508             : 
     509           0 : static void save_coding_context(AV1_COMP *cpi) {
     510           0 :   CODING_CONTEXT *const cc = &cpi->coding_context;
     511           0 :   AV1_COMMON *cm = &cpi->common;
     512             :   int i;
     513             : 
     514             :   // Stores a snapshot of key state variables which can subsequently be
     515             :   // restored with a call to av1_restore_coding_context. These functions are
     516             :   // intended for use in a re-code loop in av1_compress_frame where the
     517             :   // quantizer value is adjusted between loop iterations.
     518           0 :   for (i = 0; i < NMV_CONTEXTS; ++i) {
     519           0 :     av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
     520           0 :     av1_copy(cc->nmv_costs, cpi->nmv_costs);
     521           0 :     av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
     522             :   }
     523             : 
     524           0 :   av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
     525           0 :   av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
     526             : 
     527           0 :   cc->fc = *cm->fc;
     528           0 : }
     529             : 
     530           0 : static void restore_coding_context(AV1_COMP *cpi) {
     531           0 :   CODING_CONTEXT *const cc = &cpi->coding_context;
     532           0 :   AV1_COMMON *cm = &cpi->common;
     533             :   int i;
     534             : 
     535             :   // Restore key state variables to the snapshot state stored in the
     536             :   // previous call to av1_save_coding_context.
     537           0 :   for (i = 0; i < NMV_CONTEXTS; ++i) {
     538           0 :     av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
     539           0 :     av1_copy(cpi->nmv_costs, cc->nmv_costs);
     540           0 :     av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
     541             :   }
     542             : 
     543           0 :   av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
     544           0 :   av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
     545             : 
     546           0 :   *cm->fc = cc->fc;
     547           0 : }
     548             : 
     549           0 : static void configure_static_seg_features(AV1_COMP *cpi) {
     550           0 :   AV1_COMMON *const cm = &cpi->common;
     551           0 :   const RATE_CONTROL *const rc = &cpi->rc;
     552           0 :   struct segmentation *const seg = &cm->seg;
     553             : 
     554           0 :   int high_q = (int)(rc->avg_q > 48.0);
     555             :   int qi_delta;
     556             : 
     557             :   // Disable and clear down for KF
     558           0 :   if (cm->frame_type == KEY_FRAME) {
     559             :     // Clear down the global segmentation map
     560           0 :     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
     561           0 :     seg->update_map = 0;
     562           0 :     seg->update_data = 0;
     563           0 :     cpi->static_mb_pct = 0;
     564             : 
     565             :     // Disable segmentation
     566           0 :     av1_disable_segmentation(seg);
     567             : 
     568             :     // Clear down the segment features.
     569           0 :     av1_clearall_segfeatures(seg);
     570           0 :   } else if (cpi->refresh_alt_ref_frame) {
     571             :     // If this is an alt ref frame
     572             :     // Clear down the global segmentation map
     573           0 :     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
     574           0 :     seg->update_map = 0;
     575           0 :     seg->update_data = 0;
     576           0 :     cpi->static_mb_pct = 0;
     577             : 
     578             :     // Disable segmentation and individual segment features by default
     579           0 :     av1_disable_segmentation(seg);
     580           0 :     av1_clearall_segfeatures(seg);
     581             : 
     582             :     // Scan frames from current to arf frame.
     583             :     // This function re-enables segmentation if appropriate.
     584           0 :     av1_update_mbgraph_stats(cpi);
     585             : 
     586             :     // If segmentation was enabled set those features needed for the
     587             :     // arf itself.
     588           0 :     if (seg->enabled) {
     589           0 :       seg->update_map = 1;
     590           0 :       seg->update_data = 1;
     591             : 
     592           0 :       qi_delta =
     593           0 :           av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
     594           0 :       av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
     595           0 :       av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
     596             : 
     597           0 :       av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
     598           0 :       av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
     599             : 
     600             :       // Where relevant assume segment data is delta data
     601           0 :       seg->abs_delta = SEGMENT_DELTADATA;
     602             :     }
     603           0 :   } else if (seg->enabled) {
     604             :     // All other frames if segmentation has been enabled
     605             : 
     606             :     // First normal frame in a valid gf or alt ref group
     607           0 :     if (rc->frames_since_golden == 0) {
     608             :       // Set up segment features for normal frames in an arf group
     609           0 :       if (rc->source_alt_ref_active) {
     610           0 :         seg->update_map = 0;
     611           0 :         seg->update_data = 1;
     612           0 :         seg->abs_delta = SEGMENT_DELTADATA;
     613             : 
     614           0 :         qi_delta =
     615           0 :             av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
     616           0 :         av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
     617           0 :         av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
     618             : 
     619           0 :         av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
     620           0 :         av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
     621             : 
     622             :         // Segment coding disabled for compred testing
     623           0 :         if (high_q || (cpi->static_mb_pct == 100)) {
     624           0 :           av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
     625           0 :           av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
     626           0 :           av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
     627             :         }
     628             :       } else {
     629             :         // Disable segmentation and clear down features if alt ref
     630             :         // is not active for this group
     631             : 
     632           0 :         av1_disable_segmentation(seg);
     633             : 
     634           0 :         memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
     635             : 
     636           0 :         seg->update_map = 0;
     637           0 :         seg->update_data = 0;
     638             : 
     639           0 :         av1_clearall_segfeatures(seg);
     640             :       }
     641           0 :     } else if (rc->is_src_frame_alt_ref) {
     642             :       // Special case where we are coding over the top of a previous
     643             :       // alt ref frame.
     644             :       // Segment coding disabled for compred testing
     645             : 
     646             :       // Enable ref frame features for segment 0 as well
     647           0 :       av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
     648           0 :       av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
     649             : 
     650             :       // All mbs should use ALTREF_FRAME
     651           0 :       av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
     652           0 :       av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
     653           0 :       av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
     654           0 :       av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
     655             : 
     656             :       // Skip all MBs if high Q (0,0 mv and skip coeffs)
     657           0 :       if (high_q) {
     658           0 :         av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
     659           0 :         av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
     660             :       }
     661             :       // Enable data update
     662           0 :       seg->update_data = 1;
     663             :     } else {
     664             :       // All other frames.
     665             : 
     666             :       // No updates.. leave things as they are.
     667           0 :       seg->update_map = 0;
     668           0 :       seg->update_data = 0;
     669             :     }
     670             :   }
     671           0 : }
     672             : 
     673           0 : static void update_reference_segmentation_map(AV1_COMP *cpi) {
     674           0 :   AV1_COMMON *const cm = &cpi->common;
     675           0 :   MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
     676           0 :   uint8_t *cache_ptr = cm->last_frame_seg_map;
     677             :   int row, col;
     678             : 
     679           0 :   for (row = 0; row < cm->mi_rows; row++) {
     680           0 :     MODE_INFO **mi_8x8 = mi_8x8_ptr;
     681           0 :     uint8_t *cache = cache_ptr;
     682           0 :     for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
     683           0 :       cache[0] = mi_8x8[0]->mbmi.segment_id;
     684           0 :     mi_8x8_ptr += cm->mi_stride;
     685           0 :     cache_ptr += cm->mi_cols;
     686             :   }
     687           0 : }
     688             : 
     689           0 : static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
     690           0 :   AV1_COMMON *cm = &cpi->common;
     691           0 :   const AV1EncoderConfig *oxcf = &cpi->oxcf;
     692             : 
     693           0 :   if (!cpi->lookahead)
     694           0 :     cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
     695           0 :                                         cm->subsampling_x, cm->subsampling_y,
     696             : #if CONFIG_HIGHBITDEPTH
     697             :                                         cm->use_highbitdepth,
     698             : #endif
     699           0 :                                         oxcf->lag_in_frames);
     700           0 :   if (!cpi->lookahead)
     701           0 :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
     702             :                        "Failed to allocate lag buffers");
     703             : 
     704             :   // TODO(agrange) Check if ARF is enabled and skip allocation if not.
     705           0 :   if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
     706             :                                cm->subsampling_x, cm->subsampling_y,
     707             : #if CONFIG_HIGHBITDEPTH
     708             :                                cm->use_highbitdepth,
     709             : #endif
     710             :                                AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
     711             :                                NULL, NULL))
     712           0 :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
     713             :                        "Failed to allocate altref buffer");
     714           0 : }
     715             : 
     716           0 : static void alloc_util_frame_buffers(AV1_COMP *cpi) {
     717           0 :   AV1_COMMON *const cm = &cpi->common;
     718           0 :   if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
     719             :                                cm->subsampling_x, cm->subsampling_y,
     720             : #if CONFIG_HIGHBITDEPTH
     721             :                                cm->use_highbitdepth,
     722             : #endif
     723             :                                AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
     724             :                                NULL, NULL))
     725           0 :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
     726             :                        "Failed to allocate last frame buffer");
     727             : 
     728             : #if CONFIG_LOOP_RESTORATION
     729             :   if (aom_realloc_frame_buffer(&cpi->last_frame_db, cm->width, cm->height,
     730             :                                cm->subsampling_x, cm->subsampling_y,
     731             : #if CONFIG_HIGHBITDEPTH
     732             :                                cm->use_highbitdepth,
     733             : #endif
     734             :                                AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
     735             :                                NULL, NULL))
     736             :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
     737             :                        "Failed to allocate last frame deblocked buffer");
     738             :   if (aom_realloc_frame_buffer(&cpi->trial_frame_rst, cm->width, cm->height,
     739             :                                cm->subsampling_x, cm->subsampling_y,
     740             : #if CONFIG_HIGHBITDEPTH
     741             :                                cm->use_highbitdepth,
     742             : #endif
     743             :                                AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
     744             :                                NULL, NULL))
     745             :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
     746             :                        "Failed to allocate trial restored frame buffer");
     747             :   int extra_rstbuf_sz = RESTORATION_EXTBUF_SIZE;
     748             :   if (extra_rstbuf_sz > 0) {
     749             :     aom_free(cpi->extra_rstbuf);
     750             :     CHECK_MEM_ERROR(cm, cpi->extra_rstbuf,
     751             :                     (uint8_t *)aom_malloc(extra_rstbuf_sz));
     752             :   } else {
     753             :     cpi->extra_rstbuf = NULL;
     754             :   }
     755             : #endif  // CONFIG_LOOP_RESTORATION
     756             : 
     757           0 :   if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
     758             :                                cm->subsampling_x, cm->subsampling_y,
     759             : #if CONFIG_HIGHBITDEPTH
     760             :                                cm->use_highbitdepth,
     761             : #endif
     762             :                                AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
     763             :                                NULL, NULL))
     764           0 :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
     765             :                        "Failed to allocate scaled source buffer");
     766             : 
     767           0 :   if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
     768             :                                cm->subsampling_x, cm->subsampling_y,
     769             : #if CONFIG_HIGHBITDEPTH
     770             :                                cm->use_highbitdepth,
     771             : #endif
     772             :                                AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
     773             :                                NULL, NULL))
     774           0 :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
     775             :                        "Failed to allocate scaled last source buffer");
     776           0 : }
     777             : 
     778           0 : static void alloc_context_buffers_ext(AV1_COMP *cpi) {
     779           0 :   AV1_COMMON *cm = &cpi->common;
     780           0 :   int mi_size = cm->mi_cols * cm->mi_rows;
     781             : 
     782           0 :   CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
     783             :                   aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
     784           0 : }
     785             : 
     786           0 : void av1_alloc_compressor_data(AV1_COMP *cpi) {
     787           0 :   AV1_COMMON *cm = &cpi->common;
     788             : 
     789           0 :   av1_alloc_context_buffers(cm, cm->width, cm->height);
     790             : 
     791             : #if CONFIG_LV_MAP
     792             :   av1_alloc_txb_buf(cpi);
     793             : #endif
     794             : 
     795           0 :   alloc_context_buffers_ext(cpi);
     796             : 
     797           0 :   aom_free(cpi->tile_tok[0][0]);
     798             : 
     799             :   {
     800           0 :     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
     801           0 :     CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
     802             :                     aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
     803             : #if CONFIG_ANS && !ANS_MAX_SYMBOLS
     804             :     aom_buf_ans_alloc(&cpi->buf_ans, &cm->error, (int)tokens);
     805             : #endif  // CONFIG_ANS
     806             :   }
     807             : 
     808           0 :   av1_setup_pc_tree(&cpi->common, &cpi->td);
     809           0 : }
     810             : 
     811           0 : void av1_new_framerate(AV1_COMP *cpi, double framerate) {
     812           0 :   cpi->framerate = framerate < 0.1 ? 30 : framerate;
     813             : #if CONFIG_XIPHRC
     814             :   if (!cpi->od_rc.cur_frame) return;
     815             :   cpi->od_rc.framerate = cpi->framerate;
     816             :   od_enc_rc_resize(&cpi->od_rc);
     817             : #else
     818           0 :   av1_rc_update_framerate(cpi);
     819             : #endif
     820           0 : }
     821             : 
     822           0 : static void set_tile_info(AV1_COMP *cpi) {
     823           0 :   AV1_COMMON *const cm = &cpi->common;
     824             : #if CONFIG_TILE_GROUPS && CONFIG_DEPENDENT_HORZTILES
     825             :   int tile_row, tile_col, num_tiles_in_tg;
     826             :   int tg_row_start, tg_col_start;
     827             : #endif
     828             : #if CONFIG_EXT_TILE
     829             : #if CONFIG_EXT_PARTITION
     830             :   if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
     831             :     cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
     832             :     cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
     833             :     cm->tile_width <<= MAX_MIB_SIZE_LOG2;
     834             :     cm->tile_height <<= MAX_MIB_SIZE_LOG2;
     835             :   } else {
     836             :     cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
     837             :     cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
     838             :     cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
     839             :     cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
     840             :   }
     841             : #else
     842             :   cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
     843             :   cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
     844             :   cm->tile_width <<= MAX_MIB_SIZE_LOG2;
     845             :   cm->tile_height <<= MAX_MIB_SIZE_LOG2;
     846             : #endif  // CONFIG_EXT_PARTITION
     847             : 
     848             :   cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
     849             :   cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
     850             : 
     851             :   assert(cm->tile_width >> MAX_MIB_SIZE <= 32);
     852             :   assert(cm->tile_height >> MAX_MIB_SIZE <= 32);
     853             : 
     854             :   // Get the number of tiles
     855             :   cm->tile_cols = 1;
     856             :   while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
     857             : 
     858             :   cm->tile_rows = 1;
     859             :   while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
     860             : #else
     861             :   int min_log2_tile_cols, max_log2_tile_cols;
     862           0 :   av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
     863             : 
     864           0 :   cm->log2_tile_cols =
     865           0 :       clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
     866           0 :   cm->log2_tile_rows = cpi->oxcf.tile_rows;
     867             : 
     868           0 :   cm->tile_cols = 1 << cm->log2_tile_cols;
     869           0 :   cm->tile_rows = 1 << cm->log2_tile_rows;
     870             : 
     871           0 :   cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
     872           0 :   cm->tile_width >>= cm->log2_tile_cols;
     873           0 :   cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
     874           0 :   cm->tile_height >>= cm->log2_tile_rows;
     875             : 
     876             :   // round to integer multiples of max superblock size
     877           0 :   cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
     878           0 :   cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
     879             : #endif  // CONFIG_EXT_TILE
     880             : 
     881             : #if CONFIG_DEPENDENT_HORZTILES
     882             :   cm->dependent_horz_tiles = cpi->oxcf.dependent_horz_tiles;
     883             : #if CONFIG_EXT_TILE
     884             :   if (cm->tile_rows <= 1) cm->dependent_horz_tiles = 0;
     885             : #else
     886             :   if (cm->log2_tile_rows == 0) cm->dependent_horz_tiles = 0;
     887             : #endif
     888             : #if CONFIG_TILE_GROUPS
     889             :   if (cpi->oxcf.mtu == 0) {
     890             :     cm->num_tg = cpi->oxcf.num_tile_groups;
     891             :   } else {
     892             :     // Use a default value for the purposes of weighting costs in probability
     893             :     // updates
     894             :     cm->num_tg = DEFAULT_MAX_NUM_TG;
     895             :   }
     896             :   num_tiles_in_tg =
     897             :       (cm->tile_cols * cm->tile_rows + cm->num_tg - 1) / cm->num_tg;
     898             :   tg_row_start = 0;
     899             :   tg_col_start = 0;
     900             :   for (tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
     901             :     for (tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
     902             :       if ((tile_row * cm->tile_cols + tile_col) % num_tiles_in_tg == 0) {
     903             :         tg_row_start = tile_row;
     904             :         tg_col_start = tile_col;
     905             :       }
     906             :       cm->tile_group_start_row[tile_row][tile_col] = tg_row_start;
     907             :       cm->tile_group_start_col[tile_row][tile_col] = tg_col_start;
     908             :     }
     909             :   }
     910             : #endif
     911             : #endif
     912             : 
     913             : #if CONFIG_LOOPFILTERING_ACROSS_TILES
     914           0 :   cm->loop_filter_across_tiles_enabled =
     915           0 :       cpi->oxcf.loop_filter_across_tiles_enabled;
     916             : #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
     917           0 : }
     918             : 
     919           0 : static void update_frame_size(AV1_COMP *cpi) {
     920           0 :   AV1_COMMON *const cm = &cpi->common;
     921           0 :   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
     922             : 
     923           0 :   av1_set_mb_mi(cm, cm->width, cm->height);
     924           0 :   av1_init_context_buffers(cm);
     925           0 :   av1_init_macroblockd(cm, xd,
     926             : #if CONFIG_PVQ
     927             :                        NULL,
     928             : #endif
     929             : #if CONFIG_CFL
     930             :                        &NULL_CFL,
     931             : #endif
     932             :                        NULL);
     933           0 :   memset(cpi->mbmi_ext_base, 0,
     934           0 :          cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
     935             : 
     936           0 :   set_tile_info(cpi);
     937           0 : }
     938             : 
     939           0 : static void init_buffer_indices(AV1_COMP *cpi) {
     940             : #if CONFIG_EXT_REFS
     941             :   int fb_idx;
     942           0 :   for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
     943           0 :     cpi->lst_fb_idxes[fb_idx] = fb_idx;
     944           0 :   cpi->gld_fb_idx = LAST_REF_FRAMES;
     945           0 :   cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
     946           0 :   cpi->alt_fb_idx = LAST_REF_FRAMES + 2;
     947           0 :   for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
     948           0 :     cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
     949             : #else
     950             :   cpi->lst_fb_idx = 0;
     951             :   cpi->gld_fb_idx = 1;
     952             :   cpi->alt_fb_idx = 2;
     953             : #endif  // CONFIG_EXT_REFS
     954           0 : }
     955             : 
     956           0 : static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
     957           0 :   AV1_COMMON *const cm = &cpi->common;
     958             : 
     959           0 :   cpi->oxcf = *oxcf;
     960           0 :   cpi->framerate = oxcf->init_framerate;
     961             : 
     962           0 :   cm->profile = oxcf->profile;
     963           0 :   cm->bit_depth = oxcf->bit_depth;
     964             : #if CONFIG_HIGHBITDEPTH
     965           0 :   cm->use_highbitdepth = oxcf->use_highbitdepth;
     966             : #endif
     967           0 :   cm->color_space = oxcf->color_space;
     968           0 :   cm->color_range = oxcf->color_range;
     969             : 
     970           0 :   cm->width = oxcf->width;
     971           0 :   cm->height = oxcf->height;
     972           0 :   av1_alloc_compressor_data(cpi);
     973             : 
     974             :   // Single thread case: use counts in common.
     975           0 :   cpi->td.counts = &cm->counts;
     976             : 
     977             :   // change includes all joint functionality
     978           0 :   av1_change_config(cpi, oxcf);
     979             : 
     980           0 :   cpi->static_mb_pct = 0;
     981           0 :   cpi->ref_frame_flags = 0;
     982             : 
     983           0 :   init_buffer_indices(cpi);
     984           0 : }
     985             : 
     986           0 : static void set_rc_buffer_sizes(RATE_CONTROL *rc,
     987             :                                 const AV1EncoderConfig *oxcf) {
     988           0 :   const int64_t bandwidth = oxcf->target_bandwidth;
     989           0 :   const int64_t starting = oxcf->starting_buffer_level_ms;
     990           0 :   const int64_t optimal = oxcf->optimal_buffer_level_ms;
     991           0 :   const int64_t maximum = oxcf->maximum_buffer_size_ms;
     992             : 
     993           0 :   rc->starting_buffer_level = starting * bandwidth / 1000;
     994           0 :   rc->optimal_buffer_level =
     995           0 :       (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
     996           0 :   rc->maximum_buffer_size =
     997           0 :       (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
     998           0 : }
     999             : 
    1000             : #if CONFIG_HIGHBITDEPTH
    1001             : #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
    1002             :   cpi->fn_ptr[BT].sdf = SDF;                                           \
    1003             :   cpi->fn_ptr[BT].sdaf = SDAF;                                         \
    1004             :   cpi->fn_ptr[BT].vf = VF;                                             \
    1005             :   cpi->fn_ptr[BT].svf = SVF;                                           \
    1006             :   cpi->fn_ptr[BT].svaf = SVAF;                                         \
    1007             :   cpi->fn_ptr[BT].sdx3f = SDX3F;                                       \
    1008             :   cpi->fn_ptr[BT].sdx8f = SDX8F;                                       \
    1009             :   cpi->fn_ptr[BT].sdx4df = SDX4DF;
    1010             : 
    1011             : #define MAKE_BFP_SAD_WRAPPER(fnname)                                           \
    1012             :   static unsigned int fnname##_bits8(const uint8_t *src_ptr,                   \
    1013             :                                      int source_stride,                        \
    1014             :                                      const uint8_t *ref_ptr, int ref_stride) { \
    1015             :     return fnname(src_ptr, source_stride, ref_ptr, ref_stride);                \
    1016             :   }                                                                            \
    1017             :   static unsigned int fnname##_bits10(                                         \
    1018             :       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
    1019             :       int ref_stride) {                                                        \
    1020             :     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2;           \
    1021             :   }                                                                            \
    1022             :   static unsigned int fnname##_bits12(                                         \
    1023             :       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
    1024             :       int ref_stride) {                                                        \
    1025             :     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4;           \
    1026             :   }
    1027             : 
    1028             : #define MAKE_BFP_SADAVG_WRAPPER(fnname)                                        \
    1029             :   static unsigned int fnname##_bits8(                                          \
    1030             :       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
    1031             :       int ref_stride, const uint8_t *second_pred) {                            \
    1032             :     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred);   \
    1033             :   }                                                                            \
    1034             :   static unsigned int fnname##_bits10(                                         \
    1035             :       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
    1036             :       int ref_stride, const uint8_t *second_pred) {                            \
    1037             :     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
    1038             :            2;                                                                  \
    1039             :   }                                                                            \
    1040             :   static unsigned int fnname##_bits12(                                         \
    1041             :       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
    1042             :       int ref_stride, const uint8_t *second_pred) {                            \
    1043             :     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
    1044             :            4;                                                                  \
    1045             :   }
    1046             : 
    1047             : #define MAKE_BFP_SAD3_WRAPPER(fnname)                                    \
    1048             :   static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,  \
    1049             :                              const uint8_t *ref_ptr, int ref_stride,     \
    1050             :                              unsigned int *sad_array) {                  \
    1051             :     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);      \
    1052             :   }                                                                      \
    1053             :   static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
    1054             :                               const uint8_t *ref_ptr, int ref_stride,    \
    1055             :                               unsigned int *sad_array) {                 \
    1056             :     int i;                                                               \
    1057             :     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);      \
    1058             :     for (i = 0; i < 3; i++) sad_array[i] >>= 2;                          \
    1059             :   }                                                                      \
    1060             :   static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
    1061             :                               const uint8_t *ref_ptr, int ref_stride,    \
    1062             :                               unsigned int *sad_array) {                 \
    1063             :     int i;                                                               \
    1064             :     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);      \
    1065             :     for (i = 0; i < 3; i++) sad_array[i] >>= 4;                          \
    1066             :   }
    1067             : 
    1068             : #define MAKE_BFP_SAD8_WRAPPER(fnname)                                    \
    1069             :   static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,  \
    1070             :                              const uint8_t *ref_ptr, int ref_stride,     \
    1071             :                              unsigned int *sad_array) {                  \
    1072             :     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);      \
    1073             :   }                                                                      \
    1074             :   static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
    1075             :                               const uint8_t *ref_ptr, int ref_stride,    \
    1076             :                               unsigned int *sad_array) {                 \
    1077             :     int i;                                                               \
    1078             :     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);      \
    1079             :     for (i = 0; i < 8; i++) sad_array[i] >>= 2;                          \
    1080             :   }                                                                      \
    1081             :   static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
    1082             :                               const uint8_t *ref_ptr, int ref_stride,    \
    1083             :                               unsigned int *sad_array) {                 \
    1084             :     int i;                                                               \
    1085             :     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);      \
    1086             :     for (i = 0; i < 8; i++) sad_array[i] >>= 4;                          \
    1087             :   }
    1088             : #define MAKE_BFP_SAD4D_WRAPPER(fnname)                                        \
    1089             :   static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,       \
    1090             :                              const uint8_t *const ref_ptr[], int ref_stride,  \
    1091             :                              unsigned int *sad_array) {                       \
    1092             :     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
    1093             :   }                                                                           \
    1094             :   static void fnname##_bits10(const uint8_t *src_ptr, int source_stride,      \
    1095             :                               const uint8_t *const ref_ptr[], int ref_stride, \
    1096             :                               unsigned int *sad_array) {                      \
    1097             :     int i;                                                                    \
    1098             :     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
    1099             :     for (i = 0; i < 4; i++) sad_array[i] >>= 2;                               \
    1100             :   }                                                                           \
    1101             :   static void fnname##_bits12(const uint8_t *src_ptr, int source_stride,      \
    1102             :                               const uint8_t *const ref_ptr[], int ref_stride, \
    1103             :                               unsigned int *sad_array) {                      \
    1104             :     int i;                                                                    \
    1105             :     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
    1106             :     for (i = 0; i < 4; i++) sad_array[i] >>= 4;                               \
    1107             :   }
    1108             : 
    1109             : #if CONFIG_EXT_PARTITION
    1110             : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
    1111             : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
    1112             : MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
    1113             : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
    1114             : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
    1115             : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
    1116             : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
    1117             : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
    1118             : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
    1119             : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
    1120             : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
    1121             : #endif  // CONFIG_EXT_PARTITION
    1122           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
    1123           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
    1124           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
    1125           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
    1126           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
    1127           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
    1128           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
    1129           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
    1130           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
    1131           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
    1132           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
    1133           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
    1134           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
    1135           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
    1136           0 : MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
    1137           0 : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
    1138           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
    1139           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
    1140           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
    1141           0 : MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
    1142           0 : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
    1143           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
    1144           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
    1145           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
    1146           0 : MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
    1147           0 : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
    1148           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
    1149           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
    1150           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
    1151           0 : MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
    1152           0 : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
    1153           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
    1154           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
    1155           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
    1156           0 : MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
    1157           0 : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
    1158           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
    1159           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
    1160           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
    1161           0 : MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
    1162           0 : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
    1163           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
    1164           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
    1165           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
    1166           0 : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
    1167           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
    1168           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
    1169           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
    1170           0 : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
    1171           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
    1172           0 : MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
    1173           0 : MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
    1174           0 : MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
    1175           0 : MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
    1176           0 : MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
    1177             : 
    1178             : #if CONFIG_EXT_INTER
    1179             : #define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
    1180             :   cpi->fn_ptr[BT].msdf = MCSDF;       \
    1181             :   cpi->fn_ptr[BT].msvf = MCSVF;
    1182             : 
    1183             : #define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname)                           \
    1184             :   static unsigned int fnname##_bits8(                                    \
    1185             :       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
    1186             :       int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m,  \
    1187             :       int m_stride, int invert_mask) {                                   \
    1188             :     return fnname(src_ptr, source_stride, ref_ptr, ref_stride,           \
    1189             :                   second_pred_ptr, m, m_stride, invert_mask);            \
    1190             :   }                                                                      \
    1191             :   static unsigned int fnname##_bits10(                                   \
    1192             :       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
    1193             :       int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m,  \
    1194             :       int m_stride, int invert_mask) {                                   \
    1195             :     return fnname(src_ptr, source_stride, ref_ptr, ref_stride,           \
    1196             :                   second_pred_ptr, m, m_stride, invert_mask) >>          \
    1197             :            2;                                                            \
    1198             :   }                                                                      \
    1199             :   static unsigned int fnname##_bits12(                                   \
    1200             :       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
    1201             :       int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m,  \
    1202             :       int m_stride, int invert_mask) {                                   \
    1203             :     return fnname(src_ptr, source_stride, ref_ptr, ref_stride,           \
    1204             :                   second_pred_ptr, m, m_stride, invert_mask) >>          \
    1205             :            4;                                                            \
    1206             :   }
    1207             : 
    1208             : #if CONFIG_EXT_PARTITION
    1209             : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
    1210             : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
    1211             : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
    1212             : #endif  // CONFIG_EXT_PARTITION
    1213           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
    1214           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
    1215           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
    1216           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
    1217           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
    1218           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
    1219           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
    1220           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
    1221           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
    1222           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
    1223           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
    1224           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
    1225           0 : MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
    1226             : #endif  // CONFIG_EXT_INTER
    1227             : 
    1228             : #if CONFIG_MOTION_VAR
    1229             : #define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
    1230             :   cpi->fn_ptr[BT].osdf = OSDF;           \
    1231             :   cpi->fn_ptr[BT].ovf = OVF;             \
    1232             :   cpi->fn_ptr[BT].osvf = OSVF;
    1233             : 
    1234             : #define MAKE_OBFP_SAD_WRAPPER(fnname)                                     \
    1235             :   static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride,  \
    1236             :                                      const int32_t *wsrc,                 \
    1237             :                                      const int32_t *msk) {                \
    1238             :     return fnname(ref, ref_stride, wsrc, msk);                            \
    1239             :   }                                                                       \
    1240             :   static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
    1241             :                                       const int32_t *wsrc,                \
    1242             :                                       const int32_t *msk) {               \
    1243             :     return fnname(ref, ref_stride, wsrc, msk) >> 2;                       \
    1244             :   }                                                                       \
    1245             :   static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
    1246             :                                       const int32_t *wsrc,                \
    1247             :                                       const int32_t *msk) {               \
    1248             :     return fnname(ref, ref_stride, wsrc, msk) >> 4;                       \
    1249             :   }
    1250             : 
    1251             : #if CONFIG_EXT_PARTITION
    1252             : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
    1253             : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
    1254             : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
    1255             : #endif  // CONFIG_EXT_PARTITION
    1256           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
    1257           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
    1258           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
    1259           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
    1260           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
    1261           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
    1262           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
    1263           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
    1264           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
    1265           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
    1266           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
    1267           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
    1268           0 : MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
    1269             : #endif  // CONFIG_MOTION_VAR
    1270             : 
    1271           0 : static void highbd_set_var_fns(AV1_COMP *const cpi) {
    1272           0 :   AV1_COMMON *const cm = &cpi->common;
    1273           0 :   if (cm->use_highbitdepth) {
    1274           0 :     switch (cm->bit_depth) {
    1275             :       case AOM_BITS_8:
    1276           0 :         HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
    1277             :                    aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
    1278             :                    aom_highbd_8_sub_pixel_variance32x16,
    1279             :                    aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
    1280             :                    aom_highbd_sad32x16x4d_bits8)
    1281             : 
    1282           0 :         HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
    1283             :                    aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
    1284             :                    aom_highbd_8_sub_pixel_variance16x32,
    1285             :                    aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
    1286             :                    aom_highbd_sad16x32x4d_bits8)
    1287             : 
    1288           0 :         HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
    1289             :                    aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
    1290             :                    aom_highbd_8_sub_pixel_variance64x32,
    1291             :                    aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
    1292             :                    aom_highbd_sad64x32x4d_bits8)
    1293             : 
    1294           0 :         HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
    1295             :                    aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
    1296             :                    aom_highbd_8_sub_pixel_variance32x64,
    1297             :                    aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
    1298             :                    aom_highbd_sad32x64x4d_bits8)
    1299             : 
    1300           0 :         HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
    1301             :                    aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
    1302             :                    aom_highbd_8_sub_pixel_variance32x32,
    1303             :                    aom_highbd_8_sub_pixel_avg_variance32x32,
    1304             :                    aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
    1305             :                    aom_highbd_sad32x32x4d_bits8)
    1306             : 
    1307           0 :         HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
    1308             :                    aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
    1309             :                    aom_highbd_8_sub_pixel_variance64x64,
    1310             :                    aom_highbd_8_sub_pixel_avg_variance64x64,
    1311             :                    aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
    1312             :                    aom_highbd_sad64x64x4d_bits8)
    1313             : 
    1314           0 :         HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
    1315             :                    aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
    1316             :                    aom_highbd_8_sub_pixel_variance16x16,
    1317             :                    aom_highbd_8_sub_pixel_avg_variance16x16,
    1318             :                    aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
    1319             :                    aom_highbd_sad16x16x4d_bits8)
    1320             : 
    1321           0 :         HIGHBD_BFP(
    1322             :             BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
    1323             :             aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
    1324             :             aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
    1325             :             aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8)
    1326             : 
    1327           0 :         HIGHBD_BFP(
    1328             :             BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
    1329             :             aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
    1330             :             aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
    1331             :             aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8)
    1332             : 
    1333           0 :         HIGHBD_BFP(
    1334             :             BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
    1335             :             aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
    1336             :             aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits8,
    1337             :             aom_highbd_sad8x8x8_bits8, aom_highbd_sad8x8x4d_bits8)
    1338             : 
    1339           0 :         HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
    1340             :                    aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
    1341             :                    aom_highbd_8_sub_pixel_variance8x4,
    1342             :                    aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
    1343             :                    aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8)
    1344             : 
    1345           0 :         HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
    1346             :                    aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
    1347             :                    aom_highbd_8_sub_pixel_variance4x8,
    1348             :                    aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
    1349             :                    aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8)
    1350             : 
    1351           0 :         HIGHBD_BFP(
    1352             :             BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
    1353             :             aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
    1354             :             aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits8,
    1355             :             aom_highbd_sad4x4x8_bits8, aom_highbd_sad4x4x4d_bits8)
    1356             : 
    1357             : #if CONFIG_CB4X4
    1358           0 :         HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_8_variance2x2, NULL, NULL,
    1359             :                    NULL, NULL, NULL)
    1360           0 :         HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_8_variance4x2, NULL, NULL,
    1361             :                    NULL, NULL, NULL)
    1362           0 :         HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_8_variance2x4, NULL, NULL,
    1363             :                    NULL, NULL, NULL)
    1364             : #endif
    1365             : 
    1366             : #if CONFIG_EXT_PARTITION
    1367             :         HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
    1368             :                    aom_highbd_sad128x128_avg_bits8,
    1369             :                    aom_highbd_8_variance128x128,
    1370             :                    aom_highbd_8_sub_pixel_variance128x128,
    1371             :                    aom_highbd_8_sub_pixel_avg_variance128x128,
    1372             :                    aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
    1373             :                    aom_highbd_sad128x128x4d_bits8)
    1374             : 
    1375             :         HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
    1376             :                    aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
    1377             :                    aom_highbd_8_sub_pixel_variance128x64,
    1378             :                    aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
    1379             :                    aom_highbd_sad128x64x4d_bits8)
    1380             : 
    1381             :         HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
    1382             :                    aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
    1383             :                    aom_highbd_8_sub_pixel_variance64x128,
    1384             :                    aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
    1385             :                    aom_highbd_sad64x128x4d_bits8)
    1386             : #endif  // CONFIG_EXT_PARTITION
    1387             : 
    1388             : #if CONFIG_EXT_INTER
    1389             : #if CONFIG_EXT_PARTITION
    1390             :         HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
    1391             :                     aom_highbd_8_masked_sub_pixel_variance128x128)
    1392             :         HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
    1393             :                     aom_highbd_8_masked_sub_pixel_variance128x64)
    1394             :         HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
    1395             :                     aom_highbd_8_masked_sub_pixel_variance64x128)
    1396             : #endif  // CONFIG_EXT_PARTITION
    1397           0 :         HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
    1398             :                     aom_highbd_8_masked_sub_pixel_variance64x64)
    1399           0 :         HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
    1400             :                     aom_highbd_8_masked_sub_pixel_variance64x32)
    1401           0 :         HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
    1402             :                     aom_highbd_8_masked_sub_pixel_variance32x64)
    1403           0 :         HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
    1404             :                     aom_highbd_8_masked_sub_pixel_variance32x32)
    1405           0 :         HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
    1406             :                     aom_highbd_8_masked_sub_pixel_variance32x16)
    1407           0 :         HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
    1408             :                     aom_highbd_8_masked_sub_pixel_variance16x32)
    1409           0 :         HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
    1410             :                     aom_highbd_8_masked_sub_pixel_variance16x16)
    1411           0 :         HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
    1412             :                     aom_highbd_8_masked_sub_pixel_variance8x16)
    1413           0 :         HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
    1414             :                     aom_highbd_8_masked_sub_pixel_variance16x8)
    1415           0 :         HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
    1416             :                     aom_highbd_8_masked_sub_pixel_variance8x8)
    1417           0 :         HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
    1418             :                     aom_highbd_8_masked_sub_pixel_variance4x8)
    1419           0 :         HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
    1420             :                     aom_highbd_8_masked_sub_pixel_variance8x4)
    1421           0 :         HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
    1422             :                     aom_highbd_8_masked_sub_pixel_variance4x4)
    1423             : #endif  // CONFIG_EXT_INTER
    1424             : #if CONFIG_MOTION_VAR
    1425             : #if CONFIG_EXT_PARTITION
    1426             :         HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
    1427             :                     aom_highbd_obmc_variance128x128,
    1428             :                     aom_highbd_obmc_sub_pixel_variance128x128)
    1429             :         HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
    1430             :                     aom_highbd_obmc_variance128x64,
    1431             :                     aom_highbd_obmc_sub_pixel_variance128x64)
    1432             :         HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
    1433             :                     aom_highbd_obmc_variance64x128,
    1434             :                     aom_highbd_obmc_sub_pixel_variance64x128)
    1435             : #endif  // CONFIG_EXT_PARTITION
    1436           0 :         HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
    1437             :                     aom_highbd_obmc_variance64x64,
    1438             :                     aom_highbd_obmc_sub_pixel_variance64x64)
    1439           0 :         HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
    1440             :                     aom_highbd_obmc_variance64x32,
    1441             :                     aom_highbd_obmc_sub_pixel_variance64x32)
    1442           0 :         HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
    1443             :                     aom_highbd_obmc_variance32x64,
    1444             :                     aom_highbd_obmc_sub_pixel_variance32x64)
    1445           0 :         HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
    1446             :                     aom_highbd_obmc_variance32x32,
    1447             :                     aom_highbd_obmc_sub_pixel_variance32x32)
    1448           0 :         HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
    1449             :                     aom_highbd_obmc_variance32x16,
    1450             :                     aom_highbd_obmc_sub_pixel_variance32x16)
    1451           0 :         HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
    1452             :                     aom_highbd_obmc_variance16x32,
    1453             :                     aom_highbd_obmc_sub_pixel_variance16x32)
    1454           0 :         HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
    1455             :                     aom_highbd_obmc_variance16x16,
    1456             :                     aom_highbd_obmc_sub_pixel_variance16x16)
    1457           0 :         HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
    1458             :                     aom_highbd_obmc_variance8x16,
    1459             :                     aom_highbd_obmc_sub_pixel_variance8x16)
    1460           0 :         HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
    1461             :                     aom_highbd_obmc_variance16x8,
    1462             :                     aom_highbd_obmc_sub_pixel_variance16x8)
    1463           0 :         HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
    1464             :                     aom_highbd_obmc_variance8x8,
    1465             :                     aom_highbd_obmc_sub_pixel_variance8x8)
    1466           0 :         HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
    1467             :                     aom_highbd_obmc_variance4x8,
    1468             :                     aom_highbd_obmc_sub_pixel_variance4x8)
    1469           0 :         HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
    1470             :                     aom_highbd_obmc_variance8x4,
    1471             :                     aom_highbd_obmc_sub_pixel_variance8x4)
    1472           0 :         HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
    1473             :                     aom_highbd_obmc_variance4x4,
    1474             :                     aom_highbd_obmc_sub_pixel_variance4x4)
    1475             : #endif  // CONFIG_MOTION_VAR
    1476           0 :         break;
    1477             : 
    1478             :       case AOM_BITS_10:
    1479           0 :         HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
    1480             :                    aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
    1481             :                    aom_highbd_10_sub_pixel_variance32x16,
    1482             :                    aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
    1483             :                    aom_highbd_sad32x16x4d_bits10)
    1484             : 
    1485           0 :         HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
    1486             :                    aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
    1487             :                    aom_highbd_10_sub_pixel_variance16x32,
    1488             :                    aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
    1489             :                    aom_highbd_sad16x32x4d_bits10)
    1490             : 
    1491           0 :         HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
    1492             :                    aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
    1493             :                    aom_highbd_10_sub_pixel_variance64x32,
    1494             :                    aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
    1495             :                    aom_highbd_sad64x32x4d_bits10)
    1496             : 
    1497           0 :         HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
    1498             :                    aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
    1499             :                    aom_highbd_10_sub_pixel_variance32x64,
    1500             :                    aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
    1501             :                    aom_highbd_sad32x64x4d_bits10)
    1502             : 
    1503           0 :         HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
    1504             :                    aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
    1505             :                    aom_highbd_10_sub_pixel_variance32x32,
    1506             :                    aom_highbd_10_sub_pixel_avg_variance32x32,
    1507             :                    aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
    1508             :                    aom_highbd_sad32x32x4d_bits10)
    1509             : 
    1510           0 :         HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
    1511             :                    aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
    1512             :                    aom_highbd_10_sub_pixel_variance64x64,
    1513             :                    aom_highbd_10_sub_pixel_avg_variance64x64,
    1514             :                    aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
    1515             :                    aom_highbd_sad64x64x4d_bits10)
    1516             : 
    1517           0 :         HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
    1518             :                    aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
    1519             :                    aom_highbd_10_sub_pixel_variance16x16,
    1520             :                    aom_highbd_10_sub_pixel_avg_variance16x16,
    1521             :                    aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
    1522             :                    aom_highbd_sad16x16x4d_bits10)
    1523             : 
    1524           0 :         HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
    1525             :                    aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
    1526             :                    aom_highbd_10_sub_pixel_variance16x8,
    1527             :                    aom_highbd_10_sub_pixel_avg_variance16x8,
    1528             :                    aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
    1529             :                    aom_highbd_sad16x8x4d_bits10)
    1530             : 
    1531           0 :         HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
    1532             :                    aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
    1533             :                    aom_highbd_10_sub_pixel_variance8x16,
    1534             :                    aom_highbd_10_sub_pixel_avg_variance8x16,
    1535             :                    aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
    1536             :                    aom_highbd_sad8x16x4d_bits10)
    1537             : 
    1538           0 :         HIGHBD_BFP(
    1539             :             BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
    1540             :             aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
    1541             :             aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
    1542             :             aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10)
    1543             : 
    1544           0 :         HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
    1545             :                    aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
    1546             :                    aom_highbd_10_sub_pixel_variance8x4,
    1547             :                    aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
    1548             :                    aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10)
    1549             : 
    1550           0 :         HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
    1551             :                    aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
    1552             :                    aom_highbd_10_sub_pixel_variance4x8,
    1553             :                    aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
    1554             :                    aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10)
    1555             : 
    1556           0 :         HIGHBD_BFP(
    1557             :             BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
    1558             :             aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
    1559             :             aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
    1560             :             aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10)
    1561             : 
    1562             : #if CONFIG_CB4X4
    1563           0 :         HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_10_variance2x2, NULL, NULL,
    1564             :                    NULL, NULL, NULL)
    1565           0 :         HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_10_variance4x2, NULL, NULL,
    1566             :                    NULL, NULL, NULL)
    1567           0 :         HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_10_variance2x4, NULL, NULL,
    1568             :                    NULL, NULL, NULL)
    1569             : #endif
    1570             : 
    1571             : #if CONFIG_EXT_PARTITION
    1572             :         HIGHBD_BFP(
    1573             :             BLOCK_128X128, aom_highbd_sad128x128_bits10,
    1574             :             aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
    1575             :             aom_highbd_10_sub_pixel_variance128x128,
    1576             :             aom_highbd_10_sub_pixel_avg_variance128x128,
    1577             :             aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
    1578             :             aom_highbd_sad128x128x4d_bits10)
    1579             : 
    1580             :         HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
    1581             :                    aom_highbd_sad128x64_avg_bits10,
    1582             :                    aom_highbd_10_variance128x64,
    1583             :                    aom_highbd_10_sub_pixel_variance128x64,
    1584             :                    aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
    1585             :                    aom_highbd_sad128x64x4d_bits10)
    1586             : 
    1587             :         HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
    1588             :                    aom_highbd_sad64x128_avg_bits10,
    1589             :                    aom_highbd_10_variance64x128,
    1590             :                    aom_highbd_10_sub_pixel_variance64x128,
    1591             :                    aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
    1592             :                    aom_highbd_sad64x128x4d_bits10)
    1593             : #endif  // CONFIG_EXT_PARTITION
    1594             : 
    1595             : #if CONFIG_EXT_INTER
    1596             : #if CONFIG_EXT_PARTITION
    1597             :         HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
    1598             :                     aom_highbd_10_masked_sub_pixel_variance128x128)
    1599             :         HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
    1600             :                     aom_highbd_10_masked_sub_pixel_variance128x64)
    1601             :         HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
    1602             :                     aom_highbd_10_masked_sub_pixel_variance64x128)
    1603             : #endif  // CONFIG_EXT_PARTITION
    1604           0 :         HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
    1605             :                     aom_highbd_10_masked_sub_pixel_variance64x64)
    1606           0 :         HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
    1607             :                     aom_highbd_10_masked_sub_pixel_variance64x32)
    1608           0 :         HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
    1609             :                     aom_highbd_10_masked_sub_pixel_variance32x64)
    1610           0 :         HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
    1611             :                     aom_highbd_10_masked_sub_pixel_variance32x32)
    1612           0 :         HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
    1613             :                     aom_highbd_10_masked_sub_pixel_variance32x16)
    1614           0 :         HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
    1615             :                     aom_highbd_10_masked_sub_pixel_variance16x32)
    1616           0 :         HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
    1617             :                     aom_highbd_10_masked_sub_pixel_variance16x16)
    1618           0 :         HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
    1619             :                     aom_highbd_10_masked_sub_pixel_variance8x16)
    1620           0 :         HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
    1621             :                     aom_highbd_10_masked_sub_pixel_variance16x8)
    1622           0 :         HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
    1623             :                     aom_highbd_10_masked_sub_pixel_variance8x8)
    1624           0 :         HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
    1625             :                     aom_highbd_10_masked_sub_pixel_variance4x8)
    1626           0 :         HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
    1627             :                     aom_highbd_10_masked_sub_pixel_variance8x4)
    1628           0 :         HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
    1629             :                     aom_highbd_10_masked_sub_pixel_variance4x4)
    1630             : #endif  // CONFIG_EXT_INTER
    1631             : #if CONFIG_MOTION_VAR
    1632             : #if CONFIG_EXT_PARTITION
    1633             :         HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
    1634             :                     aom_highbd_10_obmc_variance128x128,
    1635             :                     aom_highbd_10_obmc_sub_pixel_variance128x128)
    1636             :         HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
    1637             :                     aom_highbd_10_obmc_variance128x64,
    1638             :                     aom_highbd_10_obmc_sub_pixel_variance128x64)
    1639             :         HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
    1640             :                     aom_highbd_10_obmc_variance64x128,
    1641             :                     aom_highbd_10_obmc_sub_pixel_variance64x128)
    1642             : #endif  // CONFIG_EXT_PARTITION
    1643           0 :         HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
    1644             :                     aom_highbd_10_obmc_variance64x64,
    1645             :                     aom_highbd_10_obmc_sub_pixel_variance64x64)
    1646           0 :         HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
    1647             :                     aom_highbd_10_obmc_variance64x32,
    1648             :                     aom_highbd_10_obmc_sub_pixel_variance64x32)
    1649           0 :         HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
    1650             :                     aom_highbd_10_obmc_variance32x64,
    1651             :                     aom_highbd_10_obmc_sub_pixel_variance32x64)
    1652           0 :         HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
    1653             :                     aom_highbd_10_obmc_variance32x32,
    1654             :                     aom_highbd_10_obmc_sub_pixel_variance32x32)
    1655           0 :         HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
    1656             :                     aom_highbd_10_obmc_variance32x16,
    1657             :                     aom_highbd_10_obmc_sub_pixel_variance32x16)
    1658           0 :         HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
    1659             :                     aom_highbd_10_obmc_variance16x32,
    1660             :                     aom_highbd_10_obmc_sub_pixel_variance16x32)
    1661           0 :         HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
    1662             :                     aom_highbd_10_obmc_variance16x16,
    1663             :                     aom_highbd_10_obmc_sub_pixel_variance16x16)
    1664           0 :         HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
    1665             :                     aom_highbd_10_obmc_variance8x16,
    1666             :                     aom_highbd_10_obmc_sub_pixel_variance8x16)
    1667           0 :         HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
    1668             :                     aom_highbd_10_obmc_variance16x8,
    1669             :                     aom_highbd_10_obmc_sub_pixel_variance16x8)
    1670           0 :         HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
    1671             :                     aom_highbd_10_obmc_variance8x8,
    1672             :                     aom_highbd_10_obmc_sub_pixel_variance8x8)
    1673           0 :         HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
    1674             :                     aom_highbd_10_obmc_variance4x8,
    1675             :                     aom_highbd_10_obmc_sub_pixel_variance4x8)
    1676           0 :         HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
    1677             :                     aom_highbd_10_obmc_variance8x4,
    1678             :                     aom_highbd_10_obmc_sub_pixel_variance8x4)
    1679           0 :         HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
    1680             :                     aom_highbd_10_obmc_variance4x4,
    1681             :                     aom_highbd_10_obmc_sub_pixel_variance4x4)
    1682             : #endif  // CONFIG_MOTION_VAR
    1683           0 :         break;
    1684             : 
    1685             :       case AOM_BITS_12:
    1686           0 :         HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
    1687             :                    aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
    1688             :                    aom_highbd_12_sub_pixel_variance32x16,
    1689             :                    aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
    1690             :                    aom_highbd_sad32x16x4d_bits12)
    1691             : 
    1692           0 :         HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
    1693             :                    aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
    1694             :                    aom_highbd_12_sub_pixel_variance16x32,
    1695             :                    aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
    1696             :                    aom_highbd_sad16x32x4d_bits12)
    1697             : 
    1698           0 :         HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
    1699             :                    aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
    1700             :                    aom_highbd_12_sub_pixel_variance64x32,
    1701             :                    aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
    1702             :                    aom_highbd_sad64x32x4d_bits12)
    1703             : 
    1704           0 :         HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
    1705             :                    aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
    1706             :                    aom_highbd_12_sub_pixel_variance32x64,
    1707             :                    aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
    1708             :                    aom_highbd_sad32x64x4d_bits12)
    1709             : 
    1710           0 :         HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
    1711             :                    aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
    1712             :                    aom_highbd_12_sub_pixel_variance32x32,
    1713             :                    aom_highbd_12_sub_pixel_avg_variance32x32,
    1714             :                    aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
    1715             :                    aom_highbd_sad32x32x4d_bits12)
    1716             : 
    1717           0 :         HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
    1718             :                    aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
    1719             :                    aom_highbd_12_sub_pixel_variance64x64,
    1720             :                    aom_highbd_12_sub_pixel_avg_variance64x64,
    1721             :                    aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
    1722             :                    aom_highbd_sad64x64x4d_bits12)
    1723             : 
    1724           0 :         HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
    1725             :                    aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
    1726             :                    aom_highbd_12_sub_pixel_variance16x16,
    1727             :                    aom_highbd_12_sub_pixel_avg_variance16x16,
    1728             :                    aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
    1729             :                    aom_highbd_sad16x16x4d_bits12)
    1730             : 
    1731           0 :         HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
    1732             :                    aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
    1733             :                    aom_highbd_12_sub_pixel_variance16x8,
    1734             :                    aom_highbd_12_sub_pixel_avg_variance16x8,
    1735             :                    aom_highbd_sad16x8x3_bits12, aom_highbd_sad16x8x8_bits12,
    1736             :                    aom_highbd_sad16x8x4d_bits12)
    1737             : 
    1738           0 :         HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
    1739             :                    aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
    1740             :                    aom_highbd_12_sub_pixel_variance8x16,
    1741             :                    aom_highbd_12_sub_pixel_avg_variance8x16,
    1742             :                    aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
    1743             :                    aom_highbd_sad8x16x4d_bits12)
    1744             : 
    1745           0 :         HIGHBD_BFP(
    1746             :             BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
    1747             :             aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
    1748             :             aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
    1749             :             aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12)
    1750             : 
    1751           0 :         HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
    1752             :                    aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
    1753             :                    aom_highbd_12_sub_pixel_variance8x4,
    1754             :                    aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
    1755             :                    aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12)
    1756             : 
    1757           0 :         HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
    1758             :                    aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
    1759             :                    aom_highbd_12_sub_pixel_variance4x8,
    1760             :                    aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
    1761             :                    aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12)
    1762             : 
    1763           0 :         HIGHBD_BFP(
    1764             :             BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
    1765             :             aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
    1766             :             aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
    1767             :             aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12)
    1768             : 
    1769             : #if CONFIG_CB4X4
    1770           0 :         HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_12_variance2x2, NULL, NULL,
    1771             :                    NULL, NULL, NULL)
    1772           0 :         HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_12_variance4x2, NULL, NULL,
    1773             :                    NULL, NULL, NULL)
    1774           0 :         HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_12_variance2x4, NULL, NULL,
    1775             :                    NULL, NULL, NULL)
    1776             : #endif
    1777             : 
    1778             : #if CONFIG_EXT_PARTITION
    1779             :         HIGHBD_BFP(
    1780             :             BLOCK_128X128, aom_highbd_sad128x128_bits12,
    1781             :             aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
    1782             :             aom_highbd_12_sub_pixel_variance128x128,
    1783             :             aom_highbd_12_sub_pixel_avg_variance128x128,
    1784             :             aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
    1785             :             aom_highbd_sad128x128x4d_bits12)
    1786             : 
    1787             :         HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
    1788             :                    aom_highbd_sad128x64_avg_bits12,
    1789             :                    aom_highbd_12_variance128x64,
    1790             :                    aom_highbd_12_sub_pixel_variance128x64,
    1791             :                    aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
    1792             :                    aom_highbd_sad128x64x4d_bits12)
    1793             : 
    1794             :         HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
    1795             :                    aom_highbd_sad64x128_avg_bits12,
    1796             :                    aom_highbd_12_variance64x128,
    1797             :                    aom_highbd_12_sub_pixel_variance64x128,
    1798             :                    aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
    1799             :                    aom_highbd_sad64x128x4d_bits12)
    1800             : #endif  // CONFIG_EXT_PARTITION
    1801             : 
    1802             : #if CONFIG_EXT_INTER
    1803             : #if CONFIG_EXT_PARTITION
    1804             :         HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
    1805             :                     aom_highbd_12_masked_sub_pixel_variance128x128)
    1806             :         HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
    1807             :                     aom_highbd_12_masked_sub_pixel_variance128x64)
    1808             :         HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
    1809             :                     aom_highbd_12_masked_sub_pixel_variance64x128)
    1810             : #endif  // CONFIG_EXT_PARTITION
    1811           0 :         HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
    1812             :                     aom_highbd_12_masked_sub_pixel_variance64x64)
    1813           0 :         HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
    1814             :                     aom_highbd_12_masked_sub_pixel_variance64x32)
    1815           0 :         HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
    1816             :                     aom_highbd_12_masked_sub_pixel_variance32x64)
    1817           0 :         HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
    1818             :                     aom_highbd_12_masked_sub_pixel_variance32x32)
    1819           0 :         HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
    1820             :                     aom_highbd_12_masked_sub_pixel_variance32x16)
    1821           0 :         HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
    1822             :                     aom_highbd_12_masked_sub_pixel_variance16x32)
    1823           0 :         HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
    1824             :                     aom_highbd_12_masked_sub_pixel_variance16x16)
    1825           0 :         HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
    1826             :                     aom_highbd_12_masked_sub_pixel_variance8x16)
    1827           0 :         HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
    1828             :                     aom_highbd_12_masked_sub_pixel_variance16x8)
    1829           0 :         HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
    1830             :                     aom_highbd_12_masked_sub_pixel_variance8x8)
    1831           0 :         HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
    1832             :                     aom_highbd_12_masked_sub_pixel_variance4x8)
    1833           0 :         HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
    1834             :                     aom_highbd_12_masked_sub_pixel_variance8x4)
    1835           0 :         HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
    1836             :                     aom_highbd_12_masked_sub_pixel_variance4x4)
    1837             : #endif  // CONFIG_EXT_INTER
    1838             : 
    1839             : #if CONFIG_MOTION_VAR
    1840             : #if CONFIG_EXT_PARTITION
    1841             :         HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
    1842             :                     aom_highbd_12_obmc_variance128x128,
    1843             :                     aom_highbd_12_obmc_sub_pixel_variance128x128)
    1844             :         HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
    1845             :                     aom_highbd_12_obmc_variance128x64,
    1846             :                     aom_highbd_12_obmc_sub_pixel_variance128x64)
    1847             :         HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
    1848             :                     aom_highbd_12_obmc_variance64x128,
    1849             :                     aom_highbd_12_obmc_sub_pixel_variance64x128)
    1850             : #endif  // CONFIG_EXT_PARTITION
    1851           0 :         HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
    1852             :                     aom_highbd_12_obmc_variance64x64,
    1853             :                     aom_highbd_12_obmc_sub_pixel_variance64x64)
    1854           0 :         HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
    1855             :                     aom_highbd_12_obmc_variance64x32,
    1856             :                     aom_highbd_12_obmc_sub_pixel_variance64x32)
    1857           0 :         HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
    1858             :                     aom_highbd_12_obmc_variance32x64,
    1859             :                     aom_highbd_12_obmc_sub_pixel_variance32x64)
    1860           0 :         HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
    1861             :                     aom_highbd_12_obmc_variance32x32,
    1862             :                     aom_highbd_12_obmc_sub_pixel_variance32x32)
    1863           0 :         HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
    1864             :                     aom_highbd_12_obmc_variance32x16,
    1865             :                     aom_highbd_12_obmc_sub_pixel_variance32x16)
    1866           0 :         HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
    1867             :                     aom_highbd_12_obmc_variance16x32,
    1868             :                     aom_highbd_12_obmc_sub_pixel_variance16x32)
    1869           0 :         HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
    1870             :                     aom_highbd_12_obmc_variance16x16,
    1871             :                     aom_highbd_12_obmc_sub_pixel_variance16x16)
    1872           0 :         HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
    1873             :                     aom_highbd_12_obmc_variance8x16,
    1874             :                     aom_highbd_12_obmc_sub_pixel_variance8x16)
    1875           0 :         HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
    1876             :                     aom_highbd_12_obmc_variance16x8,
    1877             :                     aom_highbd_12_obmc_sub_pixel_variance16x8)
    1878           0 :         HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
    1879             :                     aom_highbd_12_obmc_variance8x8,
    1880             :                     aom_highbd_12_obmc_sub_pixel_variance8x8)
    1881           0 :         HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
    1882             :                     aom_highbd_12_obmc_variance4x8,
    1883             :                     aom_highbd_12_obmc_sub_pixel_variance4x8)
    1884           0 :         HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
    1885             :                     aom_highbd_12_obmc_variance8x4,
    1886             :                     aom_highbd_12_obmc_sub_pixel_variance8x4)
    1887           0 :         HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
    1888             :                     aom_highbd_12_obmc_variance4x4,
    1889             :                     aom_highbd_12_obmc_sub_pixel_variance4x4)
    1890             : #endif  // CONFIG_MOTION_VAR
    1891           0 :         break;
    1892             : 
    1893             :       default:
    1894           0 :         assert(0 &&
    1895             :                "cm->bit_depth should be AOM_BITS_8, "
    1896             :                "AOM_BITS_10 or AOM_BITS_12");
    1897             :     }
    1898             :   }
    1899           0 : }
    1900             : #endif  // CONFIG_HIGHBITDEPTH
    1901             : 
    1902           0 : static void realloc_segmentation_maps(AV1_COMP *cpi) {
    1903           0 :   AV1_COMMON *const cm = &cpi->common;
    1904             : 
    1905             :   // Create the encoder segmentation map and set all entries to 0
    1906           0 :   aom_free(cpi->segmentation_map);
    1907           0 :   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
    1908             :                   aom_calloc(cm->mi_rows * cm->mi_cols, 1));
    1909             : 
    1910             :   // Create a map used for cyclic background refresh.
    1911           0 :   if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
    1912           0 :   CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
    1913             :                   av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
    1914             : 
    1915             :   // Create a map used to mark inactive areas.
    1916           0 :   aom_free(cpi->active_map.map);
    1917           0 :   CHECK_MEM_ERROR(cm, cpi->active_map.map,
    1918             :                   aom_calloc(cm->mi_rows * cm->mi_cols, 1));
    1919           0 : }
    1920             : 
    1921             : #if CONFIG_EXT_INTER
    1922           0 : void set_compound_tools(AV1_COMMON *cm) {
    1923             :   (void)cm;
    1924             : #if CONFIG_INTERINTRA
    1925           0 :   cm->allow_interintra_compound = 1;
    1926             : #endif  // CONFIG_INTERINTRA
    1927             : #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
    1928           0 :   cm->allow_masked_compound = 1;
    1929             : #endif  // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
    1930           0 : }
    1931             : #endif  // CONFIG_EXT_INTER
    1932             : 
    1933           0 : void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
    1934           0 :   AV1_COMMON *const cm = &cpi->common;
    1935           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1936             : 
    1937           0 :   if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
    1938           0 :   cm->bit_depth = oxcf->bit_depth;
    1939           0 :   cm->color_space = oxcf->color_space;
    1940           0 :   cm->color_range = oxcf->color_range;
    1941             : 
    1942           0 :   if (cm->profile <= PROFILE_1)
    1943           0 :     assert(cm->bit_depth == AOM_BITS_8);
    1944             :   else
    1945           0 :     assert(cm->bit_depth > AOM_BITS_8);
    1946             : 
    1947           0 :   cpi->oxcf = *oxcf;
    1948           0 :   cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
    1949             : #if CONFIG_GLOBAL_MOTION
    1950           0 :   cpi->td.mb.e_mbd.global_motion = cm->global_motion;
    1951             : #endif  // CONFIG_GLOBAL_MOTION
    1952             : 
    1953           0 :   if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
    1954           0 :     rc->baseline_gf_interval = FIXED_GF_INTERVAL;
    1955             :   } else {
    1956           0 :     rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
    1957             :   }
    1958             : 
    1959           0 :   cpi->refresh_last_frame = 1;
    1960           0 :   cpi->refresh_golden_frame = 0;
    1961             : #if CONFIG_EXT_REFS
    1962           0 :   cpi->refresh_bwd_ref_frame = 0;
    1963             : #endif  // CONFIG_EXT_REFS
    1964             : 
    1965           0 :   cm->refresh_frame_context =
    1966           0 :       (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
    1967             :           ? REFRESH_FRAME_CONTEXT_FORWARD
    1968           0 :           : REFRESH_FRAME_CONTEXT_BACKWARD;
    1969           0 :   cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
    1970             : 
    1971             : #if CONFIG_PALETTE
    1972           0 :   cm->allow_screen_content_tools = (cpi->oxcf.content == AOM_CONTENT_SCREEN);
    1973           0 :   if (cm->allow_screen_content_tools) {
    1974           0 :     MACROBLOCK *x = &cpi->td.mb;
    1975           0 :     if (x->palette_buffer == 0) {
    1976           0 :       CHECK_MEM_ERROR(cm, x->palette_buffer,
    1977             :                       aom_memalign(16, sizeof(*x->palette_buffer)));
    1978             :     }
    1979             :     // Reallocate the pc_tree, as it's contents depends on
    1980             :     // the state of cm->allow_screen_content_tools
    1981           0 :     av1_free_pc_tree(&cpi->td);
    1982           0 :     av1_setup_pc_tree(&cpi->common, &cpi->td);
    1983             :   }
    1984             : #endif  // CONFIG_PALETTE
    1985             : #if CONFIG_EXT_INTER
    1986           0 :   set_compound_tools(cm);
    1987             : #endif  // CONFIG_EXT_INTER
    1988           0 :   av1_reset_segment_features(cm);
    1989           0 :   av1_set_high_precision_mv(cpi, 0);
    1990             : 
    1991           0 :   set_rc_buffer_sizes(rc, &cpi->oxcf);
    1992             : 
    1993             :   // Under a configuration change, where maximum_buffer_size may change,
    1994             :   // keep buffer level clipped to the maximum allowed buffer size.
    1995           0 :   rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
    1996           0 :   rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
    1997             : 
    1998             :   // Set up frame rate and related parameters rate control values.
    1999           0 :   av1_new_framerate(cpi, cpi->framerate);
    2000             : 
    2001             :   // Set absolute upper and lower quality limits
    2002           0 :   rc->worst_quality = cpi->oxcf.worst_allowed_q;
    2003           0 :   rc->best_quality = cpi->oxcf.best_allowed_q;
    2004             : 
    2005           0 :   cm->interp_filter = cpi->sf.default_interp_filter;
    2006             : 
    2007           0 :   if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
    2008           0 :     cm->render_width = cpi->oxcf.render_width;
    2009           0 :     cm->render_height = cpi->oxcf.render_height;
    2010             :   } else {
    2011           0 :     cm->render_width = cpi->oxcf.width;
    2012           0 :     cm->render_height = cpi->oxcf.height;
    2013             :   }
    2014           0 :   cm->width = cpi->oxcf.width;
    2015           0 :   cm->height = cpi->oxcf.height;
    2016             : 
    2017           0 :   if (cpi->initial_width) {
    2018           0 :     if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
    2019           0 :       av1_free_context_buffers(cm);
    2020           0 :       av1_alloc_compressor_data(cpi);
    2021           0 :       realloc_segmentation_maps(cpi);
    2022           0 :       cpi->initial_width = cpi->initial_height = 0;
    2023             :     }
    2024             :   }
    2025           0 :   update_frame_size(cpi);
    2026             : 
    2027           0 :   cpi->alt_ref_source = NULL;
    2028           0 :   rc->is_src_frame_alt_ref = 0;
    2029             : 
    2030             : #if CONFIG_EXT_REFS
    2031           0 :   rc->is_bwd_ref_frame = 0;
    2032           0 :   rc->is_last_bipred_frame = 0;
    2033           0 :   rc->is_bipred_frame = 0;
    2034             : #endif  // CONFIG_EXT_REFS
    2035             : 
    2036             : #if 0
    2037             :   // Experimental RD Code
    2038             :   cpi->frame_distortion = 0;
    2039             :   cpi->last_frame_distortion = 0;
    2040             : #endif
    2041             : 
    2042           0 :   set_tile_info(cpi);
    2043             : 
    2044           0 :   cpi->ext_refresh_frame_flags_pending = 0;
    2045           0 :   cpi->ext_refresh_frame_context_pending = 0;
    2046             : 
    2047             : #if CONFIG_HIGHBITDEPTH
    2048           0 :   highbd_set_var_fns(cpi);
    2049             : #endif
    2050             : 
    2051             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    2052             :   cpi->common.ans_window_size_log2 = cpi->oxcf.ans_window_size_log2;
    2053             :   if (cpi->buf_ans.size != (1 << cpi->common.ans_window_size_log2)) {
    2054             :     aom_buf_ans_free(&cpi->buf_ans);
    2055             :     aom_buf_ans_alloc(&cpi->buf_ans, &cpi->common.error,
    2056             :                       1 << cpi->common.ans_window_size_log2);
    2057             :   }
    2058             : #endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
    2059           0 : }
    2060             : 
    2061           0 : static INLINE void init_upsampled_ref_frame_bufs(AV1_COMP *cpi) {
    2062             :   int i;
    2063             : 
    2064           0 :   for (i = 0; i < (REF_FRAMES + 1); ++i) {
    2065           0 :     cpi->upsampled_ref_bufs[i].ref_count = 0;
    2066           0 :     cpi->upsampled_ref_idx[i] = INVALID_IDX;
    2067             :   }
    2068           0 : }
    2069             : 
    2070           0 : AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
    2071             :                                 BufferPool *const pool) {
    2072             :   unsigned int i;
    2073           0 :   AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
    2074           0 :   AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
    2075             : 
    2076           0 :   if (!cm) return NULL;
    2077             : 
    2078           0 :   av1_zero(*cpi);
    2079             : 
    2080           0 :   if (setjmp(cm->error.jmp)) {
    2081           0 :     cm->error.setjmp = 0;
    2082           0 :     av1_remove_compressor(cpi);
    2083           0 :     return 0;
    2084             :   }
    2085             : 
    2086           0 :   cm->error.setjmp = 1;
    2087           0 :   cm->alloc_mi = av1_enc_alloc_mi;
    2088           0 :   cm->free_mi = av1_enc_free_mi;
    2089           0 :   cm->setup_mi = av1_enc_setup_mi;
    2090             : 
    2091           0 :   CHECK_MEM_ERROR(cm, cm->fc,
    2092             :                   (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
    2093           0 :   CHECK_MEM_ERROR(cm, cm->frame_contexts,
    2094             :                   (FRAME_CONTEXT *)aom_memalign(
    2095             :                       32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
    2096           0 :   memset(cm->fc, 0, sizeof(*cm->fc));
    2097           0 :   memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
    2098             : 
    2099           0 :   cpi->resize_state = 0;
    2100           0 :   cpi->resize_avg_qp = 0;
    2101           0 :   cpi->resize_buffer_underflow = 0;
    2102           0 :   cpi->resize_scale_num = 16;
    2103           0 :   cpi->resize_scale_den = 16;
    2104           0 :   cpi->resize_next_scale_num = 16;
    2105           0 :   cpi->resize_next_scale_den = 16;
    2106             : 
    2107           0 :   cpi->common.buffer_pool = pool;
    2108             : 
    2109           0 :   init_config(cpi, oxcf);
    2110             : #if CONFIG_XIPHRC
    2111             :   cpi->od_rc.framerate = cpi->framerate;
    2112             :   cpi->od_rc.frame_width = cm->render_width;
    2113             :   cpi->od_rc.frame_height = cm->render_height;
    2114             :   cpi->od_rc.keyframe_rate = oxcf->key_freq;
    2115             :   cpi->od_rc.goldenframe_rate = FIXED_GF_INTERVAL;
    2116             :   cpi->od_rc.altref_rate = 25;
    2117             :   cpi->od_rc.firstpass_quant = 1;
    2118             :   cpi->od_rc.bit_depth = cm->bit_depth;
    2119             :   cpi->od_rc.minq = oxcf->best_allowed_q;
    2120             :   cpi->od_rc.maxq = oxcf->worst_allowed_q;
    2121             :   if (cpi->oxcf.rc_mode == AOM_CQ) cpi->od_rc.minq = cpi->od_rc.quality;
    2122             :   cpi->od_rc.quality = cpi->oxcf.rc_mode == AOM_Q ? oxcf->cq_level : -1;
    2123             :   cpi->od_rc.periodic_boosts = oxcf->frame_periodic_boost;
    2124             :   od_enc_rc_init(&cpi->od_rc,
    2125             :                  cpi->oxcf.rc_mode == AOM_Q ? -1 : oxcf->target_bandwidth,
    2126             :                  oxcf->maximum_buffer_size_ms);
    2127             : #else
    2128           0 :   av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
    2129             : #endif
    2130             : 
    2131           0 :   cm->current_video_frame = 0;
    2132           0 :   cpi->partition_search_skippable_frame = 0;
    2133           0 :   cpi->tile_data = NULL;
    2134           0 :   cpi->last_show_frame_buf_idx = INVALID_IDX;
    2135             : 
    2136           0 :   realloc_segmentation_maps(cpi);
    2137             : 
    2138           0 :   for (i = 0; i < NMV_CONTEXTS; ++i) {
    2139           0 :     memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
    2140           0 :     memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
    2141             :   }
    2142             : 
    2143           0 :   for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
    2144           0 :        i++) {
    2145           0 :     CHECK_MEM_ERROR(
    2146             :         cm, cpi->mbgraph_stats[i].mb_stats,
    2147             :         aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
    2148             :   }
    2149             : 
    2150             : #if CONFIG_FP_MB_STATS
    2151             :   cpi->use_fp_mb_stats = 0;
    2152             :   if (cpi->use_fp_mb_stats) {
    2153             :     // a place holder used to store the first pass mb stats in the first pass
    2154             :     CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
    2155             :                     aom_calloc(cm->MBs * sizeof(uint8_t), 1));
    2156             :   } else {
    2157             :     cpi->twopass.frame_mb_stats_buf = NULL;
    2158             :   }
    2159             : #endif
    2160             : 
    2161           0 :   cpi->refresh_alt_ref_frame = 0;
    2162           0 :   cpi->multi_arf_last_grp_enabled = 0;
    2163             : 
    2164           0 :   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
    2165             : #if CONFIG_INTERNAL_STATS
    2166             :   cpi->b_calculate_blockiness = 1;
    2167             :   cpi->b_calculate_consistency = 1;
    2168             :   cpi->total_inconsistency = 0;
    2169             :   cpi->psnr.worst = 100.0;
    2170             :   cpi->worst_ssim = 100.0;
    2171             : 
    2172             :   cpi->count = 0;
    2173             :   cpi->bytes = 0;
    2174             : 
    2175             :   if (cpi->b_calculate_psnr) {
    2176             :     cpi->total_sq_error = 0;
    2177             :     cpi->total_samples = 0;
    2178             :     cpi->tot_recode_hits = 0;
    2179             :     cpi->summed_quality = 0;
    2180             :     cpi->summed_weights = 0;
    2181             :   }
    2182             : 
    2183             :   cpi->fastssim.worst = 100.0;
    2184             :   cpi->psnrhvs.worst = 100.0;
    2185             : 
    2186             :   if (cpi->b_calculate_blockiness) {
    2187             :     cpi->total_blockiness = 0;
    2188             :     cpi->worst_blockiness = 0.0;
    2189             :   }
    2190             : 
    2191             :   if (cpi->b_calculate_consistency) {
    2192             :     CHECK_MEM_ERROR(cm, cpi->ssim_vars,
    2193             :                     aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
    2194             :                                cpi->common.mi_rows * cpi->common.mi_cols));
    2195             :     cpi->worst_consistency = 100.0;
    2196             :   }
    2197             : #endif
    2198             : #if CONFIG_ENTROPY_STATS
    2199             :   av1_zero(aggregate_fc);
    2200             : #endif  // CONFIG_ENTROPY_STATS
    2201             : 
    2202           0 :   cpi->first_time_stamp_ever = INT64_MAX;
    2203             : 
    2204           0 :   for (i = 0; i < NMV_CONTEXTS; ++i) {
    2205           0 :     cpi->td.mb.nmvcost[i][0] = &cpi->nmv_costs[i][0][MV_MAX];
    2206           0 :     cpi->td.mb.nmvcost[i][1] = &cpi->nmv_costs[i][1][MV_MAX];
    2207           0 :     cpi->td.mb.nmvcost_hp[i][0] = &cpi->nmv_costs_hp[i][0][MV_MAX];
    2208           0 :     cpi->td.mb.nmvcost_hp[i][1] = &cpi->nmv_costs_hp[i][1][MV_MAX];
    2209             :   }
    2210             : 
    2211             : #ifdef OUTPUT_YUV_SKINMAP
    2212             :   yuv_skinmap_file = fopen("skinmap.yuv", "ab");
    2213             : #endif
    2214             : #ifdef OUTPUT_YUV_REC
    2215             :   yuv_rec_file = fopen("rec.yuv", "wb");
    2216             : #endif
    2217             : 
    2218             : #if 0
    2219             :   framepsnr = fopen("framepsnr.stt", "a");
    2220             :   kf_list = fopen("kf_list.stt", "w");
    2221             : #endif
    2222             : 
    2223             : #if CONFIG_XIPHRC
    2224             :   if (oxcf->pass == 2) {
    2225             :     cpi->od_rc.twopass_allframes_buf = oxcf->two_pass_stats_in.buf;
    2226             :     cpi->od_rc.twopass_allframes_buf_size = oxcf->two_pass_stats_in.sz;
    2227             :   }
    2228             : #else
    2229           0 :   if (oxcf->pass == 1) {
    2230           0 :     av1_init_first_pass(cpi);
    2231           0 :   } else if (oxcf->pass == 2) {
    2232           0 :     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
    2233           0 :     const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
    2234             : 
    2235             : #if CONFIG_FP_MB_STATS
    2236             :     if (cpi->use_fp_mb_stats) {
    2237             :       const size_t psz = cpi->common.MBs * sizeof(uint8_t);
    2238             :       const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
    2239             : 
    2240             :       cpi->twopass.firstpass_mb_stats.mb_stats_start =
    2241             :           oxcf->firstpass_mb_stats_in.buf;
    2242             :       cpi->twopass.firstpass_mb_stats.mb_stats_end =
    2243             :           cpi->twopass.firstpass_mb_stats.mb_stats_start +
    2244             :           (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
    2245             :     }
    2246             : #endif
    2247             : 
    2248           0 :     cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
    2249           0 :     cpi->twopass.stats_in = cpi->twopass.stats_in_start;
    2250           0 :     cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
    2251             : 
    2252           0 :     av1_init_second_pass(cpi);
    2253             :   }
    2254             : #endif
    2255             : 
    2256             : #if CONFIG_MOTION_VAR
    2257             : #if CONFIG_HIGHBITDEPTH
    2258           0 :   int buf_scaler = 2;
    2259             : #else
    2260             :   int buf_scaler = 1;
    2261             : #endif
    2262           0 :   CHECK_MEM_ERROR(
    2263             :       cm, cpi->td.mb.above_pred_buf,
    2264             :       (uint8_t *)aom_memalign(16, buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
    2265             :                                       sizeof(*cpi->td.mb.above_pred_buf)));
    2266           0 :   CHECK_MEM_ERROR(
    2267             :       cm, cpi->td.mb.left_pred_buf,
    2268             :       (uint8_t *)aom_memalign(16, buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
    2269             :                                       sizeof(*cpi->td.mb.left_pred_buf)));
    2270             : 
    2271           0 :   CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
    2272             :                   (int32_t *)aom_memalign(
    2273             :                       16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
    2274             : 
    2275           0 :   CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
    2276             :                   (int32_t *)aom_memalign(
    2277             :                       16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
    2278             : 
    2279             : #endif
    2280             : 
    2281           0 :   init_upsampled_ref_frame_bufs(cpi);
    2282             : 
    2283           0 :   av1_set_speed_features_framesize_independent(cpi);
    2284           0 :   av1_set_speed_features_framesize_dependent(cpi);
    2285             : 
    2286             : #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
    2287             :   cpi->fn_ptr[BT].sdf = SDF;                                    \
    2288             :   cpi->fn_ptr[BT].sdaf = SDAF;                                  \
    2289             :   cpi->fn_ptr[BT].vf = VF;                                      \
    2290             :   cpi->fn_ptr[BT].svf = SVF;                                    \
    2291             :   cpi->fn_ptr[BT].svaf = SVAF;                                  \
    2292             :   cpi->fn_ptr[BT].sdx3f = SDX3F;                                \
    2293             :   cpi->fn_ptr[BT].sdx8f = SDX8F;                                \
    2294             :   cpi->fn_ptr[BT].sdx4df = SDX4DF;
    2295             : 
    2296             : #if CONFIG_EXT_PARTITION
    2297             :   BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
    2298             :       aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
    2299             :       aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d)
    2300             : 
    2301             :   BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
    2302             :       aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
    2303             :       NULL, aom_sad128x64x4d)
    2304             : 
    2305             :   BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
    2306             :       aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
    2307             :       NULL, aom_sad64x128x4d)
    2308             : #endif  // CONFIG_EXT_PARTITION
    2309             : 
    2310           0 :   BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
    2311             :       aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
    2312             :       aom_sad32x16x4d)
    2313             : 
    2314           0 :   BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
    2315             :       aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
    2316             :       aom_sad16x32x4d)
    2317             : 
    2318           0 :   BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
    2319             :       aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
    2320             :       aom_sad64x32x4d)
    2321             : 
    2322           0 :   BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
    2323             :       aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
    2324             :       aom_sad32x64x4d)
    2325             : 
    2326           0 :   BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
    2327             :       aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
    2328             :       aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
    2329             : 
    2330           0 :   BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
    2331             :       aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
    2332             :       aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
    2333             : 
    2334           0 :   BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
    2335             :       aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
    2336             :       aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
    2337             : 
    2338           0 :   BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
    2339             :       aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
    2340             :       aom_sad16x8x8, aom_sad16x8x4d)
    2341             : 
    2342           0 :   BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
    2343             :       aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
    2344             :       aom_sad8x16x8, aom_sad8x16x4d)
    2345             : 
    2346           0 :   BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
    2347             :       aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
    2348             :       aom_sad8x8x8, aom_sad8x8x4d)
    2349             : 
    2350           0 :   BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
    2351             :       aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
    2352             :       aom_sad8x4x8, aom_sad8x4x4d)
    2353             : 
    2354           0 :   BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
    2355             :       aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
    2356             :       aom_sad4x8x8, aom_sad4x8x4d)
    2357             : 
    2358           0 :   BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
    2359             :       aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
    2360             :       aom_sad4x4x8, aom_sad4x4x4d)
    2361             : 
    2362             : #if CONFIG_CB4X4
    2363           0 :   BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL)
    2364           0 :   BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL)
    2365           0 :   BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL)
    2366             : #endif
    2367             : 
    2368             : #if CONFIG_MOTION_VAR
    2369             : #define OBFP(BT, OSDF, OVF, OSVF) \
    2370             :   cpi->fn_ptr[BT].osdf = OSDF;    \
    2371             :   cpi->fn_ptr[BT].ovf = OVF;      \
    2372             :   cpi->fn_ptr[BT].osvf = OSVF;
    2373             : 
    2374             : #if CONFIG_EXT_PARTITION
    2375             :   OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
    2376             :        aom_obmc_sub_pixel_variance128x128)
    2377             :   OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
    2378             :        aom_obmc_sub_pixel_variance128x64)
    2379             :   OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
    2380             :        aom_obmc_sub_pixel_variance64x128)
    2381             : #endif  // CONFIG_EXT_PARTITION
    2382           0 :   OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
    2383             :        aom_obmc_sub_pixel_variance64x64)
    2384           0 :   OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
    2385             :        aom_obmc_sub_pixel_variance64x32)
    2386           0 :   OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
    2387             :        aom_obmc_sub_pixel_variance32x64)
    2388           0 :   OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
    2389             :        aom_obmc_sub_pixel_variance32x32)
    2390           0 :   OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
    2391             :        aom_obmc_sub_pixel_variance32x16)
    2392           0 :   OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
    2393             :        aom_obmc_sub_pixel_variance16x32)
    2394           0 :   OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
    2395             :        aom_obmc_sub_pixel_variance16x16)
    2396           0 :   OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
    2397             :        aom_obmc_sub_pixel_variance16x8)
    2398           0 :   OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
    2399             :        aom_obmc_sub_pixel_variance8x16)
    2400           0 :   OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
    2401             :        aom_obmc_sub_pixel_variance8x8)
    2402           0 :   OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
    2403             :        aom_obmc_sub_pixel_variance4x8)
    2404           0 :   OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
    2405             :        aom_obmc_sub_pixel_variance8x4)
    2406           0 :   OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
    2407             :        aom_obmc_sub_pixel_variance4x4)
    2408             : #endif  // CONFIG_MOTION_VAR
    2409             : 
    2410             : #if CONFIG_EXT_INTER
    2411             : #define MBFP(BT, MCSDF, MCSVF)  \
    2412             :   cpi->fn_ptr[BT].msdf = MCSDF; \
    2413             :   cpi->fn_ptr[BT].msvf = MCSVF;
    2414             : 
    2415             : #if CONFIG_EXT_PARTITION
    2416             :   MBFP(BLOCK_128X128, aom_masked_sad128x128,
    2417             :        aom_masked_sub_pixel_variance128x128)
    2418             :   MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
    2419             :   MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
    2420             : #endif  // CONFIG_EXT_PARTITION
    2421           0 :   MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
    2422           0 :   MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
    2423           0 :   MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
    2424           0 :   MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
    2425           0 :   MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
    2426           0 :   MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
    2427           0 :   MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
    2428           0 :   MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
    2429           0 :   MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
    2430           0 :   MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
    2431           0 :   MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
    2432           0 :   MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
    2433           0 :   MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
    2434             : #endif  // CONFIG_EXT_INTER
    2435             : 
    2436             : #if CONFIG_HIGHBITDEPTH
    2437           0 :   highbd_set_var_fns(cpi);
    2438             : #endif
    2439             : 
    2440             :   /* av1_init_quantizer() is first called here. Add check in
    2441             :    * av1_frame_init_quantizer() so that av1_init_quantizer is only
    2442             :    * called later when needed. This will avoid unnecessary calls of
    2443             :    * av1_init_quantizer() for every frame.
    2444             :    */
    2445           0 :   av1_init_quantizer(cpi);
    2446             : #if CONFIG_AOM_QM
    2447             :   aom_qm_init(cm);
    2448             : #endif
    2449             : 
    2450           0 :   av1_loop_filter_init(cm);
    2451             : #if CONFIG_FRAME_SUPERRES
    2452             :   cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR;
    2453             : #endif  // CONFIG_FRAME_SUPERRES
    2454             : #if CONFIG_LOOP_RESTORATION
    2455             :   av1_loop_restoration_precal();
    2456             : #endif  // CONFIG_LOOP_RESTORATION
    2457             : 
    2458           0 :   cm->error.setjmp = 0;
    2459             : 
    2460           0 :   return cpi;
    2461             : }
    2462             : 
    2463             : #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
    2464             : 
    2465             : #define SNPRINT2(H, T, V) \
    2466             :   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
    2467             : 
    2468           0 : void av1_remove_compressor(AV1_COMP *cpi) {
    2469             :   AV1_COMMON *cm;
    2470             :   unsigned int i;
    2471             :   int t;
    2472             : 
    2473           0 :   if (!cpi) return;
    2474             : 
    2475           0 :   cm = &cpi->common;
    2476           0 :   if (cm->current_video_frame > 0) {
    2477             : #if CONFIG_ENTROPY_STATS
    2478             :     if (cpi->oxcf.pass != 1) {
    2479             :       fprintf(stderr, "Writing counts.stt\n");
    2480             :       FILE *f = fopen("counts.stt", "wb");
    2481             :       fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
    2482             :       fclose(f);
    2483             :     }
    2484             : #endif  // CONFIG_ENTROPY_STATS
    2485             : #if CONFIG_INTERNAL_STATS
    2486             :     aom_clear_system_state();
    2487             : 
    2488             :     if (cpi->oxcf.pass != 1) {
    2489             :       char headings[512] = { 0 };
    2490             :       char results[512] = { 0 };
    2491             :       FILE *f = fopen("opsnr.stt", "a");
    2492             :       double time_encoded =
    2493             :           (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
    2494             :           10000000.000;
    2495             :       double total_encode_time =
    2496             :           (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
    2497             :       const double dr =
    2498             :           (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
    2499             :       const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
    2500             :       const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
    2501             :       const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
    2502             : 
    2503             :       if (cpi->b_calculate_psnr) {
    2504             :         const double total_psnr = aom_sse_to_psnr(
    2505             :             (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
    2506             :         const double total_ssim =
    2507             :             100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
    2508             :         snprintf(headings, sizeof(headings),
    2509             :                  "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
    2510             :                  "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
    2511             :                  "WstPsnr\tWstSsim\tWstFast\tWstHVS");
    2512             :         snprintf(results, sizeof(results),
    2513             :                  "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
    2514             :                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
    2515             :                  "%7.3f\t%7.3f\t%7.3f\t%7.3f",
    2516             :                  dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
    2517             :                  cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
    2518             :                  total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
    2519             :                  cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
    2520             :                  cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
    2521             : 
    2522             :         if (cpi->b_calculate_blockiness) {
    2523             :           SNPRINT(headings, "\t  Block\tWstBlck");
    2524             :           SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
    2525             :           SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
    2526             :         }
    2527             : 
    2528             :         if (cpi->b_calculate_consistency) {
    2529             :           double consistency =
    2530             :               aom_sse_to_psnr((double)cpi->total_samples, peak,
    2531             :                               (double)cpi->total_inconsistency);
    2532             : 
    2533             :           SNPRINT(headings, "\tConsist\tWstCons");
    2534             :           SNPRINT2(results, "\t%7.3f", consistency);
    2535             :           SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
    2536             :         }
    2537             :         fprintf(f, "%s\t    Time\tRcErr\tAbsErr\n", headings);
    2538             :         fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
    2539             :                 rate_err, fabs(rate_err));
    2540             :       }
    2541             : 
    2542             :       fclose(f);
    2543             :     }
    2544             : 
    2545             : #endif
    2546             : 
    2547             : #if 0
    2548             :     {
    2549             :       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
    2550             :       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
    2551             :       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
    2552             :              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
    2553             :              cpi->time_compress_data / 1000,
    2554             :              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
    2555             :     }
    2556             : #endif
    2557             :   }
    2558             : 
    2559           0 :   for (t = 0; t < cpi->num_workers; ++t) {
    2560           0 :     AVxWorker *const worker = &cpi->workers[t];
    2561           0 :     EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
    2562             : 
    2563             :     // Deallocate allocated threads.
    2564           0 :     aom_get_worker_interface()->end(worker);
    2565             : 
    2566             :     // Deallocate allocated thread data.
    2567           0 :     if (t < cpi->num_workers - 1) {
    2568             : #if CONFIG_PALETTE
    2569           0 :       if (cpi->common.allow_screen_content_tools)
    2570           0 :         aom_free(thread_data->td->palette_buffer);
    2571             : #endif  // CONFIG_PALETTE
    2572             : #if CONFIG_MOTION_VAR
    2573           0 :       aom_free(thread_data->td->above_pred_buf);
    2574           0 :       aom_free(thread_data->td->left_pred_buf);
    2575           0 :       aom_free(thread_data->td->wsrc_buf);
    2576           0 :       aom_free(thread_data->td->mask_buf);
    2577             : #endif  // CONFIG_MOTION_VAR
    2578           0 :       aom_free(thread_data->td->counts);
    2579           0 :       av1_free_pc_tree(thread_data->td);
    2580           0 :       aom_free(thread_data->td);
    2581             :     }
    2582             :   }
    2583           0 :   aom_free(cpi->tile_thr_data);
    2584           0 :   aom_free(cpi->workers);
    2585             : 
    2586           0 :   if (cpi->num_workers > 1) av1_loop_filter_dealloc(&cpi->lf_row_sync);
    2587             : 
    2588           0 :   dealloc_compressor_data(cpi);
    2589             : 
    2590           0 :   for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
    2591           0 :        ++i) {
    2592           0 :     aom_free(cpi->mbgraph_stats[i].mb_stats);
    2593             :   }
    2594             : 
    2595             : #if CONFIG_FP_MB_STATS
    2596             :   if (cpi->use_fp_mb_stats) {
    2597             :     aom_free(cpi->twopass.frame_mb_stats_buf);
    2598             :     cpi->twopass.frame_mb_stats_buf = NULL;
    2599             :   }
    2600             : #endif
    2601             : #if CONFIG_INTERNAL_STATS
    2602             :   aom_free(cpi->ssim_vars);
    2603             :   cpi->ssim_vars = NULL;
    2604             : #endif  // CONFIG_INTERNAL_STATS
    2605             : 
    2606           0 :   av1_remove_common(cm);
    2607           0 :   av1_free_ref_frame_buffers(cm->buffer_pool);
    2608           0 :   aom_free(cpi);
    2609             : 
    2610             : #ifdef OUTPUT_YUV_SKINMAP
    2611             :   fclose(yuv_skinmap_file);
    2612             : #endif
    2613             : #ifdef OUTPUT_YUV_REC
    2614             :   fclose(yuv_rec_file);
    2615             : #endif
    2616             : 
    2617             : #if 0
    2618             : 
    2619             :   if (keyfile)
    2620             :     fclose(keyfile);
    2621             : 
    2622             :   if (framepsnr)
    2623             :     fclose(framepsnr);
    2624             : 
    2625             :   if (kf_list)
    2626             :     fclose(kf_list);
    2627             : 
    2628             : #endif
    2629             : }
    2630             : 
    2631           0 : static void generate_psnr_packet(AV1_COMP *cpi) {
    2632             :   struct aom_codec_cx_pkt pkt;
    2633             :   int i;
    2634             :   PSNR_STATS psnr;
    2635             : #if CONFIG_HIGHBITDEPTH
    2636           0 :   aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
    2637           0 :                        cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
    2638             : #else
    2639             :   aom_calc_psnr(cpi->source, cpi->common.frame_to_show, &psnr);
    2640             : #endif
    2641             : 
    2642           0 :   for (i = 0; i < 4; ++i) {
    2643           0 :     pkt.data.psnr.samples[i] = psnr.samples[i];
    2644           0 :     pkt.data.psnr.sse[i] = psnr.sse[i];
    2645           0 :     pkt.data.psnr.psnr[i] = psnr.psnr[i];
    2646             :   }
    2647           0 :   pkt.kind = AOM_CODEC_PSNR_PKT;
    2648           0 :   aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
    2649           0 : }
    2650             : 
    2651           0 : int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
    2652           0 :   if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
    2653             : 
    2654           0 :   cpi->ref_frame_flags = ref_frame_flags;
    2655           0 :   return 0;
    2656             : }
    2657             : 
    2658           0 : void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags) {
    2659           0 :   cpi->ext_refresh_golden_frame = (ref_frame_flags & AOM_GOLD_FLAG) != 0;
    2660           0 :   cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & AOM_ALT_FLAG) != 0;
    2661           0 :   cpi->ext_refresh_last_frame = (ref_frame_flags & AOM_LAST_FLAG) != 0;
    2662           0 :   cpi->ext_refresh_frame_flags_pending = 1;
    2663           0 : }
    2664             : 
    2665           0 : static YV12_BUFFER_CONFIG *get_av1_ref_frame_buffer(
    2666             :     AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag) {
    2667           0 :   MV_REFERENCE_FRAME ref_frame = NONE_FRAME;
    2668           0 :   if (ref_frame_flag == AOM_LAST_FLAG) ref_frame = LAST_FRAME;
    2669             : #if CONFIG_EXT_REFS
    2670           0 :   else if (ref_frame_flag == AOM_LAST2_FLAG)
    2671           0 :     ref_frame = LAST2_FRAME;
    2672           0 :   else if (ref_frame_flag == AOM_LAST3_FLAG)
    2673           0 :     ref_frame = LAST3_FRAME;
    2674             : #endif  // CONFIG_EXT_REFS
    2675           0 :   else if (ref_frame_flag == AOM_GOLD_FLAG)
    2676           0 :     ref_frame = GOLDEN_FRAME;
    2677             : #if CONFIG_EXT_REFS
    2678           0 :   else if (ref_frame_flag == AOM_BWD_FLAG)
    2679           0 :     ref_frame = BWDREF_FRAME;
    2680             : #endif  // CONFIG_EXT_REFS
    2681           0 :   else if (ref_frame_flag == AOM_ALT_FLAG)
    2682           0 :     ref_frame = ALTREF_FRAME;
    2683             : 
    2684           0 :   return ref_frame == NONE_FRAME ? NULL : get_ref_frame_buffer(cpi, ref_frame);
    2685             : }
    2686             : 
    2687           0 : int av1_copy_reference_enc(AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag,
    2688             :                            YV12_BUFFER_CONFIG *sd) {
    2689           0 :   YV12_BUFFER_CONFIG *cfg = get_av1_ref_frame_buffer(cpi, ref_frame_flag);
    2690           0 :   if (cfg) {
    2691           0 :     aom_yv12_copy_frame(cfg, sd);
    2692           0 :     return 0;
    2693             :   } else {
    2694           0 :     return -1;
    2695             :   }
    2696             : }
    2697             : 
    2698           0 : int av1_set_reference_enc(AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag,
    2699             :                           YV12_BUFFER_CONFIG *sd) {
    2700           0 :   YV12_BUFFER_CONFIG *cfg = get_av1_ref_frame_buffer(cpi, ref_frame_flag);
    2701           0 :   if (cfg) {
    2702           0 :     aom_yv12_copy_frame(sd, cfg);
    2703           0 :     return 0;
    2704             :   } else {
    2705           0 :     return -1;
    2706             :   }
    2707             : }
    2708             : 
    2709           0 : int av1_update_entropy(AV1_COMP *cpi, int update) {
    2710           0 :   cpi->ext_refresh_frame_context = update;
    2711           0 :   cpi->ext_refresh_frame_context_pending = 1;
    2712           0 :   return 0;
    2713             : }
    2714             : 
    2715             : #if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
    2716             : // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
    2717             : // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
    2718             : // not denoise the UV channels at this time. If ever we implement UV channel
    2719             : // denoising we will have to modify this.
    2720             : void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
    2721             :   uint8_t *src = s->y_buffer;
    2722             :   int h = s->y_height;
    2723             : 
    2724             :   do {
    2725             :     fwrite(src, s->y_width, 1, f);
    2726             :     src += s->y_stride;
    2727             :   } while (--h);
    2728             : 
    2729             :   src = s->u_buffer;
    2730             :   h = s->uv_height;
    2731             : 
    2732             :   do {
    2733             :     fwrite(src, s->uv_width, 1, f);
    2734             :     src += s->uv_stride;
    2735             :   } while (--h);
    2736             : 
    2737             :   src = s->v_buffer;
    2738             :   h = s->uv_height;
    2739             : 
    2740             :   do {
    2741             :     fwrite(src, s->uv_width, 1, f);
    2742             :     src += s->uv_stride;
    2743             :   } while (--h);
    2744             : }
    2745             : #endif
    2746             : 
    2747             : #if CONFIG_EXT_REFS && !CONFIG_XIPHRC
    2748           0 : static void check_show_existing_frame(AV1_COMP *cpi) {
    2749           0 :   const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    2750           0 :   AV1_COMMON *const cm = &cpi->common;
    2751           0 :   const FRAME_UPDATE_TYPE next_frame_update_type =
    2752           0 :       gf_group->update_type[gf_group->index];
    2753           0 :   const int which_arf = gf_group->arf_update_idx[gf_group->index];
    2754             : 
    2755           0 :   if (cm->show_existing_frame == 1) {
    2756           0 :     cm->show_existing_frame = 0;
    2757           0 :   } else if (cpi->rc.is_last_bipred_frame) {
    2758             :     // NOTE(zoeliu): If the current frame is a last bi-predictive frame, it is
    2759             :     //               needed next to show the BWDREF_FRAME, which is pointed by
    2760             :     //               the last_fb_idxes[0] after reference frame buffer update
    2761           0 :     cpi->rc.is_last_bipred_frame = 0;
    2762           0 :     cm->show_existing_frame = 1;
    2763           0 :     cpi->existing_fb_idx_to_show = cpi->lst_fb_idxes[0];
    2764           0 :   } else if (cpi->is_arf_filter_off[which_arf] &&
    2765           0 :              (next_frame_update_type == OVERLAY_UPDATE ||
    2766             :               next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
    2767             :     // Other parameters related to OVERLAY_UPDATE will be taken care of
    2768             :     // in av1_rc_get_second_pass_params(cpi)
    2769           0 :     cm->show_existing_frame = 1;
    2770           0 :     cpi->rc.is_src_frame_alt_ref = 1;
    2771           0 :     cpi->existing_fb_idx_to_show = cpi->alt_fb_idx;
    2772           0 :     cpi->is_arf_filter_off[which_arf] = 0;
    2773             :   }
    2774           0 :   cpi->rc.is_src_frame_ext_arf = 0;
    2775           0 : }
    2776             : #endif  // CONFIG_EXT_REFS && !CONFIG_XIPHRC
    2777             : 
    2778             : #ifdef OUTPUT_YUV_REC
    2779             : void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
    2780             :   uint8_t *src = s->y_buffer;
    2781             :   int h = cm->height;
    2782             : 
    2783             : #if CONFIG_HIGHBITDEPTH
    2784             :   if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
    2785             :     uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
    2786             : 
    2787             :     do {
    2788             :       fwrite(src16, s->y_width, 2, yuv_rec_file);
    2789             :       src16 += s->y_stride;
    2790             :     } while (--h);
    2791             : 
    2792             :     src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
    2793             :     h = s->uv_height;
    2794             : 
    2795             :     do {
    2796             :       fwrite(src16, s->uv_width, 2, yuv_rec_file);
    2797             :       src16 += s->uv_stride;
    2798             :     } while (--h);
    2799             : 
    2800             :     src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
    2801             :     h = s->uv_height;
    2802             : 
    2803             :     do {
    2804             :       fwrite(src16, s->uv_width, 2, yuv_rec_file);
    2805             :       src16 += s->uv_stride;
    2806             :     } while (--h);
    2807             : 
    2808             :     fflush(yuv_rec_file);
    2809             :     return;
    2810             :   }
    2811             : #endif  // CONFIG_HIGHBITDEPTH
    2812             : 
    2813             :   do {
    2814             :     fwrite(src, s->y_width, 1, yuv_rec_file);
    2815             :     src += s->y_stride;
    2816             :   } while (--h);
    2817             : 
    2818             :   src = s->u_buffer;
    2819             :   h = s->uv_height;
    2820             : 
    2821             :   do {
    2822             :     fwrite(src, s->uv_width, 1, yuv_rec_file);
    2823             :     src += s->uv_stride;
    2824             :   } while (--h);
    2825             : 
    2826             :   src = s->v_buffer;
    2827             :   h = s->uv_height;
    2828             : 
    2829             :   do {
    2830             :     fwrite(src, s->uv_width, 1, yuv_rec_file);
    2831             :     src += s->uv_stride;
    2832             :   } while (--h);
    2833             : 
    2834             :   fflush(yuv_rec_file);
    2835             : }
    2836             : #endif  // OUTPUT_YUV_REC
    2837             : 
    2838             : #if CONFIG_HIGHBITDEPTH
    2839           0 : static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
    2840             :                                    YV12_BUFFER_CONFIG *dst, int planes,
    2841             :                                    int bd) {
    2842             : #else
    2843             : static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
    2844             :                                    YV12_BUFFER_CONFIG *dst, int planes) {
    2845             : #endif  // CONFIG_HIGHBITDEPTH
    2846           0 :   const int src_w = src->y_crop_width;
    2847           0 :   const int src_h = src->y_crop_height;
    2848           0 :   const int dst_w = dst->y_crop_width;
    2849           0 :   const int dst_h = dst->y_crop_height;
    2850           0 :   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
    2851           0 :                                    src->v_buffer };
    2852           0 :   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
    2853           0 :   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
    2854           0 :   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
    2855           0 :   const InterpFilterParams interp_filter_params =
    2856             :       av1_get_interp_filter_params(EIGHTTAP_REGULAR);
    2857           0 :   const int16_t *kernel = interp_filter_params.filter_ptr;
    2858           0 :   const int taps = interp_filter_params.taps;
    2859             :   int x, y, i;
    2860             : 
    2861           0 :   assert(planes <= 3);
    2862           0 :   for (y = 0; y < dst_h; y += 16) {
    2863           0 :     for (x = 0; x < dst_w; x += 16) {
    2864           0 :       for (i = 0; i < planes; ++i) {
    2865           0 :         const int factor = (i == 0 || i == 3 ? 1 : 2);
    2866           0 :         const int x_q4 = x * (16 / factor) * src_w / dst_w;
    2867           0 :         const int y_q4 = y * (16 / factor) * src_h / dst_h;
    2868           0 :         const int src_stride = src_strides[i];
    2869           0 :         const int dst_stride = dst_strides[i];
    2870           0 :         const uint8_t *src_ptr = srcs[i] +
    2871           0 :                                  (y / factor) * src_h / dst_h * src_stride +
    2872           0 :                                  (x / factor) * src_w / dst_w;
    2873           0 :         uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
    2874             : 
    2875             : #if CONFIG_HIGHBITDEPTH
    2876           0 :         if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
    2877           0 :           aom_highbd_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
    2878           0 :                                &kernel[(x_q4 & 0xf) * taps], 16 * src_w / dst_w,
    2879           0 :                                &kernel[(y_q4 & 0xf) * taps], 16 * src_h / dst_h,
    2880             :                                16 / factor, 16 / factor, bd);
    2881             :         } else {
    2882           0 :           aom_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
    2883           0 :                         &kernel[(x_q4 & 0xf) * taps], 16 * src_w / dst_w,
    2884           0 :                         &kernel[(y_q4 & 0xf) * taps], 16 * src_h / dst_h,
    2885             :                         16 / factor, 16 / factor);
    2886             :         }
    2887             : #else
    2888             :         aom_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
    2889             :                       &kernel[(x_q4 & 0xf) * taps], 16 * src_w / dst_w,
    2890             :                       &kernel[(y_q4 & 0xf) * taps], 16 * src_h / dst_h,
    2891             :                       16 / factor, 16 / factor);
    2892             : #endif  // CONFIG_HIGHBITDEPTH
    2893             :       }
    2894             :     }
    2895             :   }
    2896             : 
    2897           0 :   if (planes == 1)
    2898           0 :     aom_extend_frame_borders_y(dst);
    2899             :   else
    2900           0 :     aom_extend_frame_borders(dst);
    2901           0 : }
    2902             : 
    2903             : #if CONFIG_GLOBAL_MOTION
    2904             : #define GM_RECODE_LOOP_NUM4X4_FACTOR 192
    2905           0 : static int recode_loop_test_global_motion(AV1_COMP *cpi) {
    2906             :   int i;
    2907           0 :   int recode = 0;
    2908           0 :   RD_COUNTS *const rdc = &cpi->td.rd_counts;
    2909           0 :   AV1_COMMON *const cm = &cpi->common;
    2910           0 :   for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
    2911           0 :     if (cm->global_motion[i].wmtype != IDENTITY &&
    2912           0 :         rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
    2913           0 :             cpi->gmparams_cost[i]) {
    2914           0 :       set_default_warp_params(&cm->global_motion[i]);
    2915           0 :       cpi->gmparams_cost[i] = 0;
    2916           0 :       recode = 1;
    2917           0 :       recode |= (rdc->global_motion_used[i] > 0);
    2918             :     }
    2919             :   }
    2920           0 :   return recode;
    2921             : }
    2922             : #endif  // CONFIG_GLOBAL_MOTION
    2923             : 
    2924             : // Function to test for conditions that indicate we should loop
    2925             : // back and recode a frame.
    2926           0 : static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
    2927             :                             int maxq, int minq) {
    2928           0 :   const RATE_CONTROL *const rc = &cpi->rc;
    2929           0 :   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
    2930           0 :   const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
    2931           0 :   int force_recode = 0;
    2932             : 
    2933           0 :   if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
    2934           0 :       (cpi->sf.recode_loop == ALLOW_RECODE) ||
    2935           0 :       (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
    2936             :     // TODO(agrange) high_limit could be greater than the scale-down threshold.
    2937           0 :     if ((rc->projected_frame_size > high_limit && q < maxq) ||
    2938           0 :         (rc->projected_frame_size < low_limit && q > minq)) {
    2939           0 :       force_recode = 1;
    2940           0 :     } else if (cpi->oxcf.rc_mode == AOM_CQ) {
    2941             :       // Deal with frame undershoot and whether or not we are
    2942             :       // below the automatically set cq level.
    2943           0 :       if (q > oxcf->cq_level &&
    2944           0 :           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
    2945           0 :         force_recode = 1;
    2946             :       }
    2947             :     }
    2948             :   }
    2949           0 :   return force_recode;
    2950             : }
    2951             : 
    2952           0 : static INLINE int get_free_upsampled_ref_buf(EncRefCntBuffer *ubufs) {
    2953             :   int i;
    2954             : 
    2955           0 :   for (i = 0; i < (REF_FRAMES + 1); i++) {
    2956           0 :     if (!ubufs[i].ref_count) {
    2957           0 :       return i;
    2958             :     }
    2959             :   }
    2960           0 :   return INVALID_IDX;
    2961             : }
    2962             : 
    2963             : // Up-sample 1 reference frame.
    2964           0 : static INLINE int upsample_ref_frame(AV1_COMP *cpi,
    2965             :                                      const YV12_BUFFER_CONFIG *const ref) {
    2966           0 :   AV1_COMMON *const cm = &cpi->common;
    2967           0 :   EncRefCntBuffer *ubufs = cpi->upsampled_ref_bufs;
    2968           0 :   int new_uidx = get_free_upsampled_ref_buf(ubufs);
    2969             : 
    2970           0 :   if (new_uidx == INVALID_IDX) {
    2971           0 :     return INVALID_IDX;
    2972             :   } else {
    2973           0 :     YV12_BUFFER_CONFIG *upsampled_ref = &ubufs[new_uidx].buf;
    2974             : 
    2975             :     // Can allocate buffer for Y plane only.
    2976           0 :     if (upsampled_ref->buffer_alloc_sz < (ref->buffer_alloc_sz << 6))
    2977           0 :       if (aom_realloc_frame_buffer(upsampled_ref, (cm->width << 3),
    2978           0 :                                    (cm->height << 3), cm->subsampling_x,
    2979             :                                    cm->subsampling_y,
    2980             : #if CONFIG_HIGHBITDEPTH
    2981             :                                    cm->use_highbitdepth,
    2982             : #endif
    2983             :                                    (AOM_BORDER_IN_PIXELS << 3),
    2984             :                                    cm->byte_alignment, NULL, NULL, NULL))
    2985           0 :         aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    2986             :                            "Failed to allocate up-sampled frame buffer");
    2987             : 
    2988             : // Currently, only Y plane is up-sampled, U, V are not used.
    2989             : #if CONFIG_HIGHBITDEPTH
    2990           0 :     scale_and_extend_frame(ref, upsampled_ref, 1, (int)cm->bit_depth);
    2991             : #else
    2992             :     scale_and_extend_frame(ref, upsampled_ref, 1);
    2993             : #endif
    2994           0 :     return new_uidx;
    2995             :   }
    2996             : }
    2997             : 
    2998             : #define DUMP_REF_FRAME_IMAGES 0
    2999             : 
    3000             : #if DUMP_REF_FRAME_IMAGES == 1
    3001             : static int dump_one_image(AV1_COMMON *cm,
    3002             :                           const YV12_BUFFER_CONFIG *const ref_buf,
    3003             :                           char *file_name) {
    3004             :   int h;
    3005             :   FILE *f_ref = NULL;
    3006             : 
    3007             :   if (ref_buf == NULL) {
    3008             :     printf("Frame data buffer is NULL.\n");
    3009             :     return AOM_CODEC_MEM_ERROR;
    3010             :   }
    3011             : 
    3012             :   if ((f_ref = fopen(file_name, "wb")) == NULL) {
    3013             :     printf("Unable to open file %s to write.\n", file_name);
    3014             :     return AOM_CODEC_MEM_ERROR;
    3015             :   }
    3016             : 
    3017             :   // --- Y ---
    3018             :   for (h = 0; h < cm->height; ++h) {
    3019             :     fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
    3020             :   }
    3021             :   // --- U ---
    3022             :   for (h = 0; h < (cm->height >> 1); ++h) {
    3023             :     fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
    3024             :            f_ref);
    3025             :   }
    3026             :   // --- V ---
    3027             :   for (h = 0; h < (cm->height >> 1); ++h) {
    3028             :     fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
    3029             :            f_ref);
    3030             :   }
    3031             : 
    3032             :   fclose(f_ref);
    3033             : 
    3034             :   return AOM_CODEC_OK;
    3035             : }
    3036             : 
    3037             : static void dump_ref_frame_images(AV1_COMP *cpi) {
    3038             :   AV1_COMMON *const cm = &cpi->common;
    3039             :   MV_REFERENCE_FRAME ref_frame;
    3040             : 
    3041             :   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
    3042             :     char file_name[256] = "";
    3043             :     snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
    3044             :              cm->current_video_frame, ref_frame);
    3045             :     dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
    3046             :   }
    3047             : }
    3048             : #endif  // DUMP_REF_FRAME_IMAGES == 1
    3049             : 
    3050             : #if CONFIG_EXT_REFS
    3051             : // This function is used to shift the virtual indices of last reference frames
    3052             : // as follows:
    3053             : // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
    3054             : // when the LAST_FRAME is updated.
    3055           0 : static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
    3056             :   int ref_frame;
    3057           0 :   for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
    3058           0 :     cpi->lst_fb_idxes[ref_frame] = cpi->lst_fb_idxes[ref_frame - 1];
    3059             : 
    3060             :     // [0] is allocated to the current coded frame. The statistics for the
    3061             :     // reference frames start at [LAST_FRAME], i.e. [1].
    3062           0 :     if (!cpi->rc.is_src_frame_alt_ref) {
    3063           0 :       memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
    3064           0 :              cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
    3065             :              sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
    3066             :     }
    3067             :   }
    3068           0 : }
    3069             : #endif  // CONFIG_EXT_REFS
    3070             : 
    3071           0 : void av1_update_reference_frames(AV1_COMP *cpi) {
    3072           0 :   AV1_COMMON *const cm = &cpi->common;
    3073           0 :   BufferPool *const pool = cm->buffer_pool;
    3074           0 :   const int use_upsampled_ref = cpi->sf.use_upsampled_references;
    3075           0 :   int new_uidx = 0;
    3076             : 
    3077             :   // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
    3078             :   //       for the purpose to verify no mismatch between encoder and decoder.
    3079           0 :   if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
    3080             : 
    3081           0 :   if (use_upsampled_ref) {
    3082             : #if CONFIG_EXT_REFS
    3083           0 :     if (cm->show_existing_frame) {
    3084           0 :       new_uidx = cpi->upsampled_ref_idx[cpi->existing_fb_idx_to_show];
    3085             :       // TODO(zoeliu): Once following is confirmed, remove it.
    3086           0 :       assert(cpi->upsampled_ref_bufs[new_uidx].ref_count > 0);
    3087             :     } else {
    3088             : #endif  // CONFIG_EXT_REFS
    3089             :       // Up-sample the current encoded frame.
    3090           0 :       RefCntBuffer *bufs = pool->frame_bufs;
    3091           0 :       const YV12_BUFFER_CONFIG *const ref = &bufs[cm->new_fb_idx].buf;
    3092             : 
    3093           0 :       new_uidx = upsample_ref_frame(cpi, ref);
    3094             : #if CONFIG_EXT_REFS
    3095           0 :       assert(new_uidx != INVALID_IDX);
    3096             :     }
    3097             : #endif  // CONFIG_EXT_REFS
    3098             :   }
    3099             :   // At this point the new frame has been encoded.
    3100             :   // If any buffer copy / swapping is signaled it should be done here.
    3101           0 :   if (cm->frame_type == KEY_FRAME) {
    3102           0 :     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
    3103             :                cm->new_fb_idx);
    3104             : #if CONFIG_EXT_REFS
    3105           0 :     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
    3106             :                cm->new_fb_idx);
    3107             : #endif  // CONFIG_EXT_REFS
    3108           0 :     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
    3109             :                cm->new_fb_idx);
    3110             : 
    3111           0 :     if (use_upsampled_ref) {
    3112           0 :       uref_cnt_fb(cpi->upsampled_ref_bufs,
    3113           0 :                   &cpi->upsampled_ref_idx[cpi->gld_fb_idx], new_uidx);
    3114             : #if CONFIG_EXT_REFS
    3115           0 :       uref_cnt_fb(cpi->upsampled_ref_bufs,
    3116           0 :                   &cpi->upsampled_ref_idx[cpi->bwd_fb_idx], new_uidx);
    3117             : #endif  // CONFIG_EXT_REFS
    3118           0 :       uref_cnt_fb(cpi->upsampled_ref_bufs,
    3119           0 :                   &cpi->upsampled_ref_idx[cpi->alt_fb_idx], new_uidx);
    3120             :     }
    3121           0 :   } else if (av1_preserve_existing_gf(cpi)) {
    3122             :     // We have decided to preserve the previously existing golden frame as our
    3123             :     // new ARF frame. However, in the short term in function
    3124             :     // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
    3125             :     // we're updating the GF with the current decoded frame, we save it to the
    3126             :     // ARF slot instead.
    3127             :     // We now have to update the ARF with the current frame and swap gld_fb_idx
    3128             :     // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
    3129             :     // slot and, if we're updating the GF, the current frame becomes the new GF.
    3130             :     int tmp;
    3131             : 
    3132           0 :     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
    3133             :                cm->new_fb_idx);
    3134           0 :     if (use_upsampled_ref)
    3135           0 :       uref_cnt_fb(cpi->upsampled_ref_bufs,
    3136           0 :                   &cpi->upsampled_ref_idx[cpi->alt_fb_idx], new_uidx);
    3137             : 
    3138           0 :     tmp = cpi->alt_fb_idx;
    3139           0 :     cpi->alt_fb_idx = cpi->gld_fb_idx;
    3140           0 :     cpi->gld_fb_idx = tmp;
    3141             : 
    3142             : #if CONFIG_EXT_REFS
    3143             :     // We need to modify the mapping accordingly
    3144           0 :     cpi->arf_map[0] = cpi->alt_fb_idx;
    3145             : #endif
    3146             : // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
    3147             : // cpi->interp_filter_selected[GOLDEN_FRAME]?
    3148             : #if CONFIG_EXT_REFS
    3149           0 :   } else if (cpi->rc.is_last_bipred_frame) {
    3150             :     // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the LAST3_FRAME
    3151             :     // by updating the virtual indices. Note that the frame BWDREF_FRAME points
    3152             :     // to now should be retired, and it should not be used before refreshed.
    3153           0 :     int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
    3154             : 
    3155           0 :     shift_last_ref_frames(cpi);
    3156           0 :     cpi->lst_fb_idxes[0] = cpi->bwd_fb_idx;
    3157           0 :     cpi->bwd_fb_idx = tmp;
    3158             : 
    3159           0 :     memcpy(cpi->interp_filter_selected[LAST_FRAME],
    3160           0 :            cpi->interp_filter_selected[BWDREF_FRAME],
    3161             :            sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
    3162           0 :   } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
    3163             :     // Deal with the special case for showing existing internal ALTREF_FRAME
    3164             :     // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
    3165             :     // by updating the virtual indices.
    3166           0 :     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    3167           0 :     int which_arf = gf_group->arf_ref_idx[gf_group->index];
    3168           0 :     int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
    3169             : 
    3170           0 :     shift_last_ref_frames(cpi);
    3171           0 :     cpi->lst_fb_idxes[0] = cpi->alt_fb_idx;
    3172           0 :     cpi->alt_fb_idx = tmp;
    3173             : 
    3174             :     // We need to modify the mapping accordingly
    3175           0 :     cpi->arf_map[which_arf] = cpi->alt_fb_idx;
    3176             : 
    3177           0 :     memcpy(cpi->interp_filter_selected[LAST_FRAME],
    3178           0 :            cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
    3179             :            sizeof(cpi->interp_filter_selected[ALTREF_FRAME + which_arf]));
    3180             : #endif     // CONFIG_EXT_REFS
    3181             :   } else { /* For non key/golden frames */
    3182           0 :     if (cpi->refresh_alt_ref_frame) {
    3183           0 :       int arf_idx = cpi->alt_fb_idx;
    3184           0 :       int which_arf = 0;
    3185             : #if CONFIG_EXT_REFS
    3186           0 :       if (cpi->oxcf.pass == 2) {
    3187           0 :         const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    3188           0 :         which_arf = gf_group->arf_update_idx[gf_group->index];
    3189           0 :         arf_idx = cpi->arf_map[which_arf];
    3190             :       }
    3191             : #else
    3192             :       if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
    3193             :         const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    3194             :         arf_idx = gf_group->arf_update_idx[gf_group->index];
    3195             :       }
    3196             : #endif  // CONFIG_EXT_REFS
    3197           0 :       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
    3198           0 :       if (use_upsampled_ref)
    3199           0 :         uref_cnt_fb(cpi->upsampled_ref_bufs, &cpi->upsampled_ref_idx[arf_idx],
    3200             :                     new_uidx);
    3201             : 
    3202           0 :       memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
    3203           0 :              cpi->interp_filter_selected[0],
    3204             :              sizeof(cpi->interp_filter_selected[0]));
    3205             :     }
    3206             : 
    3207           0 :     if (cpi->refresh_golden_frame) {
    3208           0 :       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
    3209             :                  cm->new_fb_idx);
    3210           0 :       if (use_upsampled_ref)
    3211           0 :         uref_cnt_fb(cpi->upsampled_ref_bufs,
    3212           0 :                     &cpi->upsampled_ref_idx[cpi->gld_fb_idx], new_uidx);
    3213             : 
    3214             : #if !CONFIG_EXT_REFS
    3215             :       if (!cpi->rc.is_src_frame_alt_ref)
    3216             : #endif  // !CONFIG_EXT_REFS
    3217           0 :         memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
    3218           0 :                cpi->interp_filter_selected[0],
    3219             :                sizeof(cpi->interp_filter_selected[0]));
    3220             :     }
    3221             : 
    3222             : #if CONFIG_EXT_REFS
    3223           0 :     if (cpi->refresh_bwd_ref_frame) {
    3224           0 :       if (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs) {
    3225             :         // We have swapped the virtual indices to allow bwd_ref_frame to use
    3226             :         // ALT0 as reference frame. We need to swap them back.
    3227             :         // NOTE: The ALT_REFs' are indexed reversely, and ALT0 refers to the
    3228             :         //       farthest ALT_REF from the first frame in the gf group.
    3229           0 :         int tmp = cpi->arf_map[0];
    3230           0 :         cpi->arf_map[0] = cpi->alt_fb_idx;
    3231           0 :         cpi->alt_fb_idx = cpi->bwd_fb_idx;
    3232           0 :         cpi->bwd_fb_idx = tmp;
    3233             :       }
    3234             : 
    3235           0 :       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
    3236             :                  cm->new_fb_idx);
    3237           0 :       if (use_upsampled_ref)
    3238           0 :         uref_cnt_fb(cpi->upsampled_ref_bufs,
    3239           0 :                     &cpi->upsampled_ref_idx[cpi->bwd_fb_idx], new_uidx);
    3240             : 
    3241           0 :       memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
    3242           0 :              cpi->interp_filter_selected[0],
    3243             :              sizeof(cpi->interp_filter_selected[0]));
    3244             :     }
    3245             : #endif  // CONFIG_EXT_REFS
    3246             :   }
    3247             : 
    3248           0 :   if (cpi->refresh_last_frame) {
    3249             : #if CONFIG_EXT_REFS
    3250             :     // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
    3251             :     // reference to the reference frame buffer virtual index; and then (2) from
    3252             :     // the virtual index to the reference frame buffer physical index:
    3253             :     //
    3254             :     // LAST_FRAME,      ..., LAST3_FRAME,     ..., ALTREF_FRAME
    3255             :     //      |                     |                     |
    3256             :     //      v                     v                     v
    3257             :     // lst_fb_idxes[0], ..., lst_fb_idxes[2], ..., alt_fb_idx
    3258             :     //      |                     |                     |
    3259             :     //      v                     v                     v
    3260             :     // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
    3261             :     //
    3262             :     // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
    3263             :     // have the other 2 LAST reference frames shifted as follows:
    3264             :     // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
    3265             :     // , and then have LAST_FRAME refreshed by the newly coded frame.
    3266             :     //
    3267             :     // To fulfill it, the decoder will be notified to execute following 2 steps:
    3268             :     //
    3269             :     // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
    3270             :     //     to point to the newly coded frame, i.e.
    3271             :     //     ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
    3272             :     //
    3273             :     // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
    3274             :     //     original virtual index of LAST3_FRAME and have the other mappings
    3275             :     //     shifted as follows:
    3276             :     // LAST_FRAME,      LAST2_FRAME,     LAST3_FRAME
    3277             :     //      |                |                |
    3278             :     //      v                v                v
    3279             :     // lst_fb_idxes[2], lst_fb_idxes[0], lst_fb_idxes[1]
    3280             :     int ref_frame;
    3281             : 
    3282           0 :     if (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs) {
    3283             :       // We have swapped the virtual indices to use ALT0 as BWD_REF
    3284             :       // and we need to swap them back.
    3285           0 :       int tmp = cpi->arf_map[0];
    3286           0 :       cpi->arf_map[0] = cpi->alt_fb_idx;
    3287           0 :       cpi->alt_fb_idx = cpi->bwd_fb_idx;
    3288           0 :       cpi->bwd_fb_idx = tmp;
    3289             :     }
    3290             : 
    3291           0 :     if (cm->frame_type == KEY_FRAME) {
    3292           0 :       for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
    3293           0 :         ref_cnt_fb(pool->frame_bufs,
    3294           0 :                    &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
    3295             :                    cm->new_fb_idx);
    3296             : 
    3297           0 :         if (use_upsampled_ref)
    3298           0 :           uref_cnt_fb(cpi->upsampled_ref_bufs,
    3299           0 :                       &cpi->upsampled_ref_idx[cpi->lst_fb_idxes[ref_frame]],
    3300             :                       new_uidx);
    3301             :       }
    3302             :     } else {
    3303             :       int tmp;
    3304             : 
    3305           0 :       ref_cnt_fb(pool->frame_bufs,
    3306           0 :                  &cm->ref_frame_map[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
    3307             :                  cm->new_fb_idx);
    3308             : 
    3309           0 :       if (use_upsampled_ref)
    3310           0 :         uref_cnt_fb(
    3311           0 :             cpi->upsampled_ref_bufs,
    3312           0 :             &cpi->upsampled_ref_idx[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
    3313             :             new_uidx);
    3314             : 
    3315           0 :       tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
    3316             : 
    3317           0 :       shift_last_ref_frames(cpi);
    3318           0 :       cpi->lst_fb_idxes[0] = tmp;
    3319             : 
    3320           0 :       assert(cm->show_existing_frame == 0);
    3321             :       // NOTE: Currently only LF_UPDATE and INTNL_OVERLAY_UPDATE frames are to
    3322             :       //       refresh the LAST_FRAME.
    3323           0 :       memcpy(cpi->interp_filter_selected[LAST_FRAME],
    3324           0 :              cpi->interp_filter_selected[0],
    3325             :              sizeof(cpi->interp_filter_selected[0]));
    3326             :     }
    3327             : #else
    3328             :     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
    3329             :                cm->new_fb_idx);
    3330             :     if (use_upsampled_ref)
    3331             :       uref_cnt_fb(cpi->upsampled_ref_bufs,
    3332             :                   &cpi->upsampled_ref_idx[cpi->lst_fb_idx], new_uidx);
    3333             :     if (!cpi->rc.is_src_frame_alt_ref) {
    3334             :       memcpy(cpi->interp_filter_selected[LAST_FRAME],
    3335             :              cpi->interp_filter_selected[0],
    3336             :              sizeof(cpi->interp_filter_selected[0]));
    3337             :     }
    3338             : #endif  // CONFIG_EXT_REFS
    3339             :   }
    3340             : 
    3341             : #if DUMP_REF_FRAME_IMAGES == 1
    3342             :   // Dump out all reference frame images.
    3343             :   dump_ref_frame_images(cpi);
    3344             : #endif  // DUMP_REF_FRAME_IMAGES
    3345           0 : }
    3346             : 
    3347           0 : static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
    3348           0 :   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
    3349           0 :   struct loopfilter *lf = &cm->lf;
    3350           0 :   if (is_lossless_requested(&cpi->oxcf)) {
    3351           0 :     lf->filter_level = 0;
    3352             :   } else {
    3353             :     struct aom_usec_timer timer;
    3354             : 
    3355           0 :     aom_clear_system_state();
    3356             : 
    3357           0 :     aom_usec_timer_start(&timer);
    3358             : 
    3359           0 :     av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
    3360             : 
    3361           0 :     aom_usec_timer_mark(&timer);
    3362           0 :     cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
    3363             :   }
    3364             : 
    3365           0 :   if (lf->filter_level > 0) {
    3366             : #if CONFIG_VAR_TX || CONFIG_EXT_PARTITION || CONFIG_CB4X4
    3367           0 :     av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
    3368             : #else
    3369             :     if (cpi->num_workers > 1)
    3370             :       av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
    3371             :                                lf->filter_level, 0, 0, cpi->workers,
    3372             :                                cpi->num_workers, &cpi->lf_row_sync);
    3373             :     else
    3374             :       av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
    3375             : #endif
    3376             :   }
    3377             : #if CONFIG_CDEF
    3378           0 :   if (is_lossless_requested(&cpi->oxcf)) {
    3379           0 :     cm->cdef_bits = 0;
    3380           0 :     cm->cdef_strengths[0] = 0;
    3381           0 :     cm->nb_cdef_strengths = 1;
    3382             :   } else {
    3383             :     // Find cm->dering_level, cm->clpf_strength_u and cm->clpf_strength_v
    3384           0 :     av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd);
    3385             : 
    3386             :     // Apply the filter
    3387           0 :     av1_cdef_frame(cm->frame_to_show, cm, xd);
    3388             :   }
    3389             : #endif
    3390             : #if CONFIG_LOOP_RESTORATION
    3391             :   av1_pick_filter_restoration(cpi->source, cpi, cpi->sf.lpf_pick);
    3392             :   if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
    3393             :       cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
    3394             :       cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
    3395             :     av1_loop_restoration_frame(cm->frame_to_show, cm, cm->rst_info, 7, 0, NULL);
    3396             :   }
    3397             : #endif  // CONFIG_LOOP_RESTORATION
    3398           0 :   aom_extend_frame_inner_borders(cm->frame_to_show);
    3399           0 : }
    3400             : 
    3401           0 : static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
    3402           0 :   RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
    3403           0 :   if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
    3404           0 :       new_fb_ptr->mi_cols < cm->mi_cols) {
    3405           0 :     aom_free(new_fb_ptr->mvs);
    3406           0 :     CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
    3407             :                     (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
    3408             :                                          sizeof(*new_fb_ptr->mvs)));
    3409           0 :     new_fb_ptr->mi_rows = cm->mi_rows;
    3410           0 :     new_fb_ptr->mi_cols = cm->mi_cols;
    3411             :   }
    3412           0 : }
    3413             : 
    3414           0 : void av1_scale_references(AV1_COMP *cpi) {
    3415           0 :   AV1_COMMON *cm = &cpi->common;
    3416             :   MV_REFERENCE_FRAME ref_frame;
    3417           0 :   const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
    3418             :     AOM_LAST_FLAG,
    3419             : #if CONFIG_EXT_REFS
    3420             :     AOM_LAST2_FLAG,
    3421             :     AOM_LAST3_FLAG,
    3422             : #endif  // CONFIG_EXT_REFS
    3423             :     AOM_GOLD_FLAG,
    3424             : #if CONFIG_EXT_REFS
    3425             :     AOM_BWD_FLAG,
    3426             : #endif  // CONFIG_EXT_REFS
    3427             :     AOM_ALT_FLAG
    3428             :   };
    3429             : 
    3430           0 :   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
    3431             :     // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
    3432           0 :     if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
    3433           0 :       BufferPool *const pool = cm->buffer_pool;
    3434           0 :       const YV12_BUFFER_CONFIG *const ref =
    3435           0 :           get_ref_frame_buffer(cpi, ref_frame);
    3436             : 
    3437           0 :       if (ref == NULL) {
    3438           0 :         cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
    3439           0 :         continue;
    3440             :       }
    3441             : 
    3442             : #if CONFIG_HIGHBITDEPTH
    3443           0 :       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
    3444           0 :         RefCntBuffer *new_fb_ptr = NULL;
    3445           0 :         int force_scaling = 0;
    3446           0 :         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
    3447           0 :         if (new_fb == INVALID_IDX) {
    3448           0 :           new_fb = get_free_fb(cm);
    3449           0 :           force_scaling = 1;
    3450             :         }
    3451           0 :         if (new_fb == INVALID_IDX) return;
    3452           0 :         new_fb_ptr = &pool->frame_bufs[new_fb];
    3453           0 :         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
    3454           0 :             new_fb_ptr->buf.y_crop_height != cm->height) {
    3455           0 :           if (aom_realloc_frame_buffer(
    3456             :                   &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
    3457             :                   cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
    3458             :                   cm->byte_alignment, NULL, NULL, NULL))
    3459           0 :             aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    3460             :                                "Failed to allocate frame buffer");
    3461           0 :           scale_and_extend_frame(ref, &new_fb_ptr->buf, MAX_MB_PLANE,
    3462           0 :                                  (int)cm->bit_depth);
    3463           0 :           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
    3464           0 :           alloc_frame_mvs(cm, new_fb);
    3465             :         }
    3466             : #else
    3467             :       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
    3468             :         RefCntBuffer *new_fb_ptr = NULL;
    3469             :         int force_scaling = 0;
    3470             :         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
    3471             :         if (new_fb == INVALID_IDX) {
    3472             :           new_fb = get_free_fb(cm);
    3473             :           force_scaling = 1;
    3474             :         }
    3475             :         if (new_fb == INVALID_IDX) return;
    3476             :         new_fb_ptr = &pool->frame_bufs[new_fb];
    3477             :         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
    3478             :             new_fb_ptr->buf.y_crop_height != cm->height) {
    3479             :           if (aom_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
    3480             :                                        cm->subsampling_x, cm->subsampling_y,
    3481             :                                        AOM_BORDER_IN_PIXELS, cm->byte_alignment,
    3482             :                                        NULL, NULL, NULL))
    3483             :             aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    3484             :                                "Failed to allocate frame buffer");
    3485             :           scale_and_extend_frame(ref, &new_fb_ptr->buf, MAX_MB_PLANE);
    3486             :           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
    3487             :           alloc_frame_mvs(cm, new_fb);
    3488             :         }
    3489             : #endif  // CONFIG_HIGHBITDEPTH
    3490             : 
    3491           0 :         if (cpi->sf.use_upsampled_references &&
    3492           0 :             (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
    3493           0 :              new_fb_ptr->buf.y_crop_height != cm->height)) {
    3494           0 :           const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
    3495           0 :           EncRefCntBuffer *ubuf =
    3496           0 :               &cpi->upsampled_ref_bufs[cpi->upsampled_ref_idx[map_idx]];
    3497             : 
    3498           0 :           if (aom_realloc_frame_buffer(&ubuf->buf, (cm->width << 3),
    3499           0 :                                        (cm->height << 3), cm->subsampling_x,
    3500             :                                        cm->subsampling_y,
    3501             : #if CONFIG_HIGHBITDEPTH
    3502             :                                        cm->use_highbitdepth,
    3503             : #endif
    3504             :                                        (AOM_BORDER_IN_PIXELS << 3),
    3505             :                                        cm->byte_alignment, NULL, NULL, NULL))
    3506           0 :             aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    3507             :                                "Failed to allocate up-sampled frame buffer");
    3508             : #if CONFIG_HIGHBITDEPTH
    3509           0 :           scale_and_extend_frame(&new_fb_ptr->buf, &ubuf->buf, 1,
    3510           0 :                                  (int)cm->bit_depth);
    3511             : #else
    3512             :           scale_and_extend_frame(&new_fb_ptr->buf, &ubuf->buf, 1);
    3513             : #endif
    3514             :         }
    3515             :       } else {
    3516           0 :         const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
    3517           0 :         RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
    3518           0 :         buf->buf.y_crop_width = ref->y_crop_width;
    3519           0 :         buf->buf.y_crop_height = ref->y_crop_height;
    3520           0 :         cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
    3521           0 :         ++buf->ref_count;
    3522             :       }
    3523             :     } else {
    3524           0 :       if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
    3525             :     }
    3526             :   }
    3527             : }
    3528             : 
    3529           0 : static void release_scaled_references(AV1_COMP *cpi) {
    3530           0 :   AV1_COMMON *cm = &cpi->common;
    3531             :   int i;
    3532           0 :   if (cpi->oxcf.pass == 0) {
    3533             :     // Only release scaled references under certain conditions:
    3534             :     // if reference will be updated, or if scaled reference has same resolution.
    3535             :     int refresh[INTER_REFS_PER_FRAME];
    3536           0 :     refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
    3537             : #if CONFIG_EXT_REFS
    3538           0 :     refresh[1] = refresh[2] = 0;
    3539           0 :     refresh[3] = (cpi->refresh_golden_frame) ? 1 : 0;
    3540           0 :     refresh[4] = (cpi->refresh_bwd_ref_frame) ? 1 : 0;
    3541           0 :     refresh[5] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
    3542             : #else
    3543             :     refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
    3544             :     refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
    3545             : #endif  // CONFIG_EXT_REFS
    3546           0 :     for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
    3547           0 :       const int idx = cpi->scaled_ref_idx[i - 1];
    3548           0 :       RefCntBuffer *const buf =
    3549           0 :           idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
    3550           0 :       const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
    3551           0 :       if (buf != NULL &&
    3552           0 :           (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
    3553           0 :                               buf->buf.y_crop_height == ref->y_crop_height))) {
    3554           0 :         --buf->ref_count;
    3555           0 :         cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
    3556             :       }
    3557             :     }
    3558             :   } else {
    3559           0 :     for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) {
    3560           0 :       const int idx = cpi->scaled_ref_idx[i];
    3561           0 :       RefCntBuffer *const buf =
    3562           0 :           idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
    3563           0 :       if (buf != NULL) {
    3564           0 :         --buf->ref_count;
    3565           0 :         cpi->scaled_ref_idx[i] = INVALID_IDX;
    3566             :       }
    3567             :     }
    3568             :   }
    3569           0 : }
    3570             : 
    3571           0 : static void full_to_model_count(unsigned int *model_count,
    3572             :                                 unsigned int *full_count) {
    3573             :   int n;
    3574           0 :   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
    3575           0 :   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
    3576           0 :   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
    3577           0 :   for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
    3578           0 :     model_count[TWO_TOKEN] += full_count[n];
    3579           0 :   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
    3580           0 : }
    3581             : 
    3582           0 : void av1_full_to_model_counts(av1_coeff_count_model *model_count,
    3583             :                               av1_coeff_count *full_count) {
    3584             :   int i, j, k, l;
    3585             : 
    3586           0 :   for (i = 0; i < PLANE_TYPES; ++i)
    3587           0 :     for (j = 0; j < REF_TYPES; ++j)
    3588           0 :       for (k = 0; k < COEF_BANDS; ++k)
    3589           0 :         for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
    3590           0 :           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
    3591           0 : }
    3592             : 
    3593             : #if 0 && CONFIG_INTERNAL_STATS
    3594             : static void output_frame_level_debug_stats(AV1_COMP *cpi) {
    3595             :   AV1_COMMON *const cm = &cpi->common;
    3596             :   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
    3597             :   int64_t recon_err;
    3598             : 
    3599             :   aom_clear_system_state();
    3600             : 
    3601             :   recon_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
    3602             : 
    3603             :   if (cpi->twopass.total_left_stats.coded_error != 0.0)
    3604             :     fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
    3605             :        "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
    3606             :        "%10"PRId64" %10"PRId64" %10d "
    3607             :        "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
    3608             :         "%6d %6d %5d %5d %5d "
    3609             :         "%10"PRId64" %10.3lf"
    3610             :         "%10lf %8u %10"PRId64" %10d %10d %10d\n",
    3611             :         cpi->common.current_video_frame,
    3612             :         cm->width, cm->height,
    3613             :         cpi->rc.source_alt_ref_pending,
    3614             :         cpi->rc.source_alt_ref_active,
    3615             :         cpi->rc.this_frame_target,
    3616             :         cpi->rc.projected_frame_size,
    3617             :         cpi->rc.projected_frame_size / cpi->common.MBs,
    3618             :         (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
    3619             :         cpi->rc.vbr_bits_off_target,
    3620             :         cpi->rc.vbr_bits_off_target_fast,
    3621             :         cpi->twopass.extend_minq,
    3622             :         cpi->twopass.extend_minq_fast,
    3623             :         cpi->rc.total_target_vs_actual,
    3624             :         (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
    3625             :         cpi->rc.total_actual_bits, cm->base_qindex,
    3626             :         av1_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
    3627             :         (double)av1_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
    3628             :         av1_convert_qindex_to_q(cpi->twopass.active_worst_quality,
    3629             :                                 cm->bit_depth),
    3630             :         cpi->rc.avg_q,
    3631             :         av1_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
    3632             :         cpi->refresh_last_frame, cpi->refresh_golden_frame,
    3633             :         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
    3634             :         cpi->twopass.bits_left,
    3635             :         cpi->twopass.total_left_stats.coded_error,
    3636             :         cpi->twopass.bits_left /
    3637             :             (1 + cpi->twopass.total_left_stats.coded_error),
    3638             :         cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
    3639             :         cpi->twopass.kf_zeromotion_pct,
    3640             :         cpi->twopass.fr_content_type);
    3641             : 
    3642             :   fclose(f);
    3643             : 
    3644             :   if (0) {
    3645             :     FILE *const fmodes = fopen("Modes.stt", "a");
    3646             :     int i;
    3647             : 
    3648             :     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
    3649             :             cm->frame_type, cpi->refresh_golden_frame,
    3650             :             cpi->refresh_alt_ref_frame);
    3651             : 
    3652             :     for (i = 0; i < MAX_MODES; ++i)
    3653             :       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
    3654             : 
    3655             :     fprintf(fmodes, "\n");
    3656             : 
    3657             :     fclose(fmodes);
    3658             :   }
    3659             : }
    3660             : #endif
    3661             : 
    3662           0 : static void set_mv_search_params(AV1_COMP *cpi) {
    3663           0 :   const AV1_COMMON *const cm = &cpi->common;
    3664           0 :   const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
    3665             : 
    3666             :   // Default based on max resolution.
    3667           0 :   cpi->mv_step_param = av1_init_search_range(max_mv_def);
    3668             : 
    3669           0 :   if (cpi->sf.mv.auto_mv_step_size) {
    3670           0 :     if (frame_is_intra_only(cm)) {
    3671             :       // Initialize max_mv_magnitude for use in the first INTER frame
    3672             :       // after a key/intra-only frame.
    3673           0 :       cpi->max_mv_magnitude = max_mv_def;
    3674             :     } else {
    3675           0 :       if (cm->show_frame) {
    3676             :         // Allow mv_steps to correspond to twice the max mv magnitude found
    3677             :         // in the previous frame, capped by the default max_mv_magnitude based
    3678             :         // on resolution.
    3679           0 :         cpi->mv_step_param = av1_init_search_range(
    3680           0 :             AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
    3681             :       }
    3682           0 :       cpi->max_mv_magnitude = 0;
    3683             :     }
    3684             :   }
    3685           0 : }
    3686             : 
    3687           0 : static void set_size_independent_vars(AV1_COMP *cpi) {
    3688             : #if CONFIG_GLOBAL_MOTION
    3689             :   int i;
    3690           0 :   for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
    3691           0 :     set_default_warp_params(&cpi->common.global_motion[i]);
    3692             :   }
    3693           0 :   cpi->global_motion_search_done = 0;
    3694             : #endif  // CONFIG_GLOBAL_MOTION
    3695           0 :   av1_set_speed_features_framesize_independent(cpi);
    3696           0 :   av1_set_rd_speed_thresholds(cpi);
    3697           0 :   av1_set_rd_speed_thresholds_sub8x8(cpi);
    3698           0 :   cpi->common.interp_filter = cpi->sf.default_interp_filter;
    3699             : #if CONFIG_EXT_INTER
    3700           0 :   if (!frame_is_intra_only(&cpi->common)) set_compound_tools(&cpi->common);
    3701             : #endif  // CONFIG_EXT_INTER
    3702           0 : }
    3703             : 
    3704           0 : static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
    3705             :                                     int *top_index) {
    3706           0 :   AV1_COMMON *const cm = &cpi->common;
    3707           0 :   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
    3708             : 
    3709             :   // Setup variables that depend on the dimensions of the frame.
    3710           0 :   av1_set_speed_features_framesize_dependent(cpi);
    3711             : 
    3712             : // Decide q and q bounds.
    3713             : #if CONFIG_XIPHRC
    3714             :   int frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
    3715             :   *q = od_enc_rc_select_quantizers_and_lambdas(
    3716             :       &cpi->od_rc, cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
    3717             :       frame_type, bottom_index, top_index);
    3718             : #else
    3719           0 :   *q = av1_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
    3720             : #endif
    3721             : 
    3722           0 :   if (!frame_is_intra_only(cm)) {
    3723           0 :     av1_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
    3724             :   }
    3725             : 
    3726             :   // Configure experimental use of segmentation for enhanced coding of
    3727             :   // static regions if indicated.
    3728             :   // Only allowed in the second pass of a two pass encode, as it requires
    3729             :   // lagged coding, and if the relevant speed feature flag is set.
    3730           0 :   if (oxcf->pass == 2 && cpi->sf.static_segmentation)
    3731           0 :     configure_static_seg_features(cpi);
    3732           0 : }
    3733             : 
    3734           0 : static void init_motion_estimation(AV1_COMP *cpi) {
    3735           0 :   int y_stride = cpi->scaled_source.y_stride;
    3736             : 
    3737           0 :   if (cpi->sf.mv.search_method == NSTEP) {
    3738           0 :     av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
    3739           0 :   } else if (cpi->sf.mv.search_method == DIAMOND) {
    3740           0 :     av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
    3741             :   }
    3742           0 : }
    3743             : 
    3744             : #if CONFIG_LOOP_RESTORATION
    3745             : static void set_restoration_tilesize(int width, int height,
    3746             :                                      RestorationInfo *rst) {
    3747             :   (void)width;
    3748             :   (void)height;
    3749             :   rst[0].restoration_tilesize = (RESTORATION_TILESIZE_MAX >> 1);
    3750             :   rst[1].restoration_tilesize = rst[0].restoration_tilesize;
    3751             :   rst[2].restoration_tilesize = rst[0].restoration_tilesize;
    3752             : }
    3753             : #endif  // CONFIG_LOOP_RESTORATION
    3754             : 
    3755           0 : static void set_scaled_size(AV1_COMP *cpi) {
    3756           0 :   AV1_COMMON *const cm = &cpi->common;
    3757           0 :   AV1EncoderConfig *const oxcf = &cpi->oxcf;
    3758             : 
    3759             :   // TODO(afergs): Replace with call to av1_resize_pending? Could replace
    3760             :   //               scaled_size_set as well.
    3761             :   // TODO(afergs): Realistically, if resize_pending is true, then the other
    3762             :   //               conditions must already be satisfied.
    3763             :   //               Try this first:
    3764             :   //                 av1_resize_pending &&
    3765             :   //                 (DYNAMIC && (1 Pass CBR || 2 Pass VBR)
    3766             :   //                  STATIC  && FIRST_FRAME)
    3767             :   //               Really, av1_resize_pending should just reflect the above.
    3768             :   // TODO(afergs): Allow fixed resizing in AOM_CBR mode?
    3769             :   // 2 Pass VBR: Resize if fixed resize and first frame, or dynamic resize and
    3770             :   //             a resize is pending.
    3771             :   // 1 Pass CBR: Resize if dynamic resize and resize pending.
    3772           0 :   if ((oxcf->pass == 2 && oxcf->rc_mode == AOM_VBR &&
    3773           0 :        ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
    3774           0 :         (oxcf->resize_mode == RESIZE_DYNAMIC && av1_resize_pending(cpi)))) ||
    3775           0 :       (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
    3776           0 :        oxcf->resize_mode == RESIZE_DYNAMIC && av1_resize_pending(cpi))) {
    3777             :     // TODO(afergs): This feels hacky... Should it just set? Should
    3778             :     //               av1_set_next_scaled_size be a library function?
    3779           0 :     av1_calculate_next_scaled_size(cpi, &oxcf->scaled_frame_width,
    3780             :                                    &oxcf->scaled_frame_height);
    3781             :   }
    3782           0 : }
    3783             : 
    3784           0 : static void set_frame_size(AV1_COMP *cpi, int width, int height) {
    3785             :   int ref_frame;
    3786           0 :   AV1_COMMON *const cm = &cpi->common;
    3787           0 :   AV1EncoderConfig *const oxcf = &cpi->oxcf;
    3788           0 :   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
    3789             : 
    3790           0 :   if (width != cm->width || height != cm->height) {
    3791             :     // There has been a change in the encoded frame size
    3792           0 :     av1_set_size_literal(cpi, width, height);
    3793             : 
    3794             :     // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
    3795             :     // TODO(afergs): Make condition just (pass == 0) or (rc_mode == CBR) -
    3796             :     //               UNLESS CBR starts allowing FIXED resizing. Then the resize
    3797             :     //               mode will need to get checked too.
    3798           0 :     if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
    3799           0 :         oxcf->resize_mode == RESIZE_DYNAMIC)
    3800           0 :       set_mv_search_params(cpi);  // TODO(afergs): Needed? Caller calls after...
    3801             :   }
    3802             : 
    3803             : #if !CONFIG_XIPHRC
    3804           0 :   if (oxcf->pass == 2) {
    3805           0 :     av1_set_target_rate(cpi);
    3806             :   }
    3807             : #endif
    3808             : 
    3809           0 :   alloc_frame_mvs(cm, cm->new_fb_idx);
    3810             : 
    3811             :   // Reset the frame pointers to the current frame size.
    3812           0 :   if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
    3813             :                                cm->subsampling_x, cm->subsampling_y,
    3814             : #if CONFIG_HIGHBITDEPTH
    3815             :                                cm->use_highbitdepth,
    3816             : #endif
    3817             :                                AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
    3818             :                                NULL, NULL))
    3819           0 :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    3820             :                        "Failed to allocate frame buffer");
    3821             : 
    3822             : #if CONFIG_LOOP_RESTORATION
    3823             :   set_restoration_tilesize(cm->width, cm->height, cm->rst_info);
    3824             :   for (int i = 0; i < MAX_MB_PLANE; ++i)
    3825             :     cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
    3826             :   av1_alloc_restoration_buffers(cm);
    3827             :   for (int i = 0; i < MAX_MB_PLANE; ++i) {
    3828             :     cpi->rst_search[i].restoration_tilesize =
    3829             :         cm->rst_info[i].restoration_tilesize;
    3830             :     av1_alloc_restoration_struct(cm, &cpi->rst_search[i], cm->width,
    3831             :                                  cm->height);
    3832             :   }
    3833             : #endif  // CONFIG_LOOP_RESTORATION
    3834           0 :   alloc_util_frame_buffers(cpi);
    3835           0 :   init_motion_estimation(cpi);
    3836             : 
    3837           0 :   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
    3838           0 :     RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
    3839           0 :     const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
    3840             : 
    3841           0 :     ref_buf->idx = buf_idx;
    3842             : 
    3843           0 :     if (buf_idx != INVALID_IDX) {
    3844           0 :       YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
    3845           0 :       ref_buf->buf = buf;
    3846             : #if CONFIG_HIGHBITDEPTH
    3847           0 :       av1_setup_scale_factors_for_frame(
    3848             :           &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
    3849           0 :           cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
    3850             : #else
    3851             :       av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
    3852             :                                         buf->y_crop_height, cm->width,
    3853             :                                         cm->height);
    3854             : #endif  // CONFIG_HIGHBITDEPTH
    3855           0 :       if (av1_is_scaled(&ref_buf->sf)) aom_extend_frame_borders(buf);
    3856             :     } else {
    3857           0 :       ref_buf->buf = NULL;
    3858             :     }
    3859             :   }
    3860             : #if CONFIG_INTRABC
    3861             : #if CONFIG_HIGHBITDEPTH
    3862             :   av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
    3863             :                                     cm->width, cm->height,
    3864             :                                     cm->use_highbitdepth);
    3865             : #else
    3866             :   av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
    3867             :                                     cm->width, cm->height);
    3868             : #endif  // CONFIG_HIGHBITDEPTH
    3869             : #endif  // CONFIG_INTRABC
    3870             : 
    3871           0 :   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
    3872           0 : }
    3873             : 
    3874           0 : static void setup_frame_size(AV1_COMP *cpi) {
    3875           0 :   set_scaled_size(cpi);
    3876             : #if CONFIG_FRAME_SUPERRES
    3877             :   int encode_width;
    3878             :   int encode_height;
    3879             :   av1_calculate_superres_size(cpi, &encode_width, &encode_height);
    3880             :   set_frame_size(cpi, encode_width, encode_height);
    3881             : #else
    3882           0 :   set_frame_size(cpi, cpi->oxcf.scaled_frame_width,
    3883             :                  cpi->oxcf.scaled_frame_height);
    3884             : #endif  // CONFIG_FRAME_SUPERRES
    3885           0 : }
    3886             : 
    3887           0 : static void reset_use_upsampled_references(AV1_COMP *cpi) {
    3888             :   MV_REFERENCE_FRAME ref_frame;
    3889             : 
    3890             :   // reset up-sampled reference buffer structure.
    3891           0 :   init_upsampled_ref_frame_bufs(cpi);
    3892             : 
    3893           0 :   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
    3894           0 :     const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, ref_frame);
    3895           0 :     int new_uidx = upsample_ref_frame(cpi, ref);
    3896             : 
    3897             :     // Update the up-sampled reference index.
    3898           0 :     cpi->upsampled_ref_idx[get_ref_frame_map_idx(cpi, ref_frame)] = new_uidx;
    3899           0 :     cpi->upsampled_ref_bufs[new_uidx].ref_count++;
    3900             :   }
    3901           0 : }
    3902             : 
    3903           0 : static void encode_without_recode_loop(AV1_COMP *cpi) {
    3904           0 :   AV1_COMMON *const cm = &cpi->common;
    3905           0 :   int q = 0, bottom_index = 0, top_index = 0;  // Dummy variables.
    3906           0 :   const int use_upsampled_ref = cpi->sf.use_upsampled_references;
    3907             : 
    3908           0 :   aom_clear_system_state();
    3909             : 
    3910             : #if CONFIG_FRAME_SUPERRES
    3911             :   // TODO(afergs): Figure out when is actually a good time to do superres
    3912             :   cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR;
    3913             :   // (uint8_t)(rand() % 9 + SUPERRES_SCALE_NUMERATOR_MIN);
    3914             :   cpi->superres_pending = cpi->oxcf.superres_enabled && 0;
    3915             : #endif  // CONFIG_FRAME_SUPERRES
    3916             : 
    3917           0 :   setup_frame_size(cpi);
    3918           0 :   av1_resize_step(cpi);
    3919             : 
    3920             :   // For 1 pass CBR under dynamic resize mode: use faster scaling for source.
    3921             :   // Only for 2x2 scaling for now.
    3922           0 :   if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR &&
    3923           0 :       cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
    3924           0 :       cpi->un_scaled_source->y_width == (cm->width << 1) &&
    3925           0 :       cpi->un_scaled_source->y_height == (cm->height << 1)) {
    3926           0 :     cpi->source = av1_scale_if_required_fast(cm, cpi->un_scaled_source,
    3927             :                                              &cpi->scaled_source);
    3928           0 :     if (cpi->unscaled_last_source != NULL)
    3929           0 :       cpi->last_source = av1_scale_if_required_fast(
    3930             :           cm, cpi->unscaled_last_source, &cpi->scaled_last_source);
    3931             :   } else {
    3932           0 :     cpi->source =
    3933           0 :         av1_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source);
    3934           0 :     if (cpi->unscaled_last_source != NULL)
    3935           0 :       cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
    3936             :                                                &cpi->scaled_last_source);
    3937             :   }
    3938             : 
    3939           0 :   if (frame_is_intra_only(cm) == 0) {
    3940           0 :     av1_scale_references(cpi);
    3941             :   }
    3942             : 
    3943           0 :   set_size_independent_vars(cpi);
    3944           0 :   set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
    3945             : 
    3946             :   // cpi->sf.use_upsampled_references can be different from frame to frame.
    3947             :   // Every time when cpi->sf.use_upsampled_references is changed from 0 to 1.
    3948             :   // The reference frames for this frame have to be up-sampled before encoding.
    3949           0 :   if (!use_upsampled_ref && cpi->sf.use_upsampled_references &&
    3950           0 :       cm->frame_type != KEY_FRAME)
    3951           0 :     reset_use_upsampled_references(cpi);
    3952             : 
    3953           0 :   av1_set_quantizer(cm, q);
    3954           0 :   setup_frame(cpi);
    3955           0 :   suppress_active_map(cpi);
    3956             : 
    3957             :   // Variance adaptive and in frame q adjustment experiments are mutually
    3958             :   // exclusive.
    3959           0 :   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
    3960           0 :     av1_vaq_frame_setup(cpi);
    3961           0 :   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
    3962           0 :     av1_setup_in_frame_q_adj(cpi);
    3963           0 :   } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
    3964           0 :     av1_cyclic_refresh_setup(cpi);
    3965             :   }
    3966           0 :   apply_active_map(cpi);
    3967             : 
    3968             :   // transform / motion compensation build reconstruction frame
    3969           0 :   av1_encode_frame(cpi);
    3970             : 
    3971             : #if CONFIG_FRAME_SUPERRES
    3972             :   // TODO(afergs): Upscale the frame to show
    3973             :   cpi->superres_pending = 0;
    3974             : #endif  // CONFIG_FRAME_SUPERRES
    3975             : 
    3976             :   // Update some stats from cyclic refresh, and check if we should not update
    3977             :   // golden reference, for 1 pass CBR.
    3978           0 :   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
    3979           0 :       (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
    3980           0 :     av1_cyclic_refresh_check_golden_update(cpi);
    3981             : 
    3982             :   // Update the skip mb flag probabilities based on the distribution
    3983             :   // seen in the last encoder iteration.
    3984             :   // update_base_skip_probs(cpi);
    3985           0 :   aom_clear_system_state();
    3986           0 : }
    3987             : 
    3988           0 : static void encode_with_recode_loop(AV1_COMP *cpi, size_t *size,
    3989             :                                     uint8_t *dest) {
    3990           0 :   AV1_COMMON *const cm = &cpi->common;
    3991           0 :   RATE_CONTROL *const rc = &cpi->rc;
    3992             :   int bottom_index, top_index;
    3993           0 :   int loop_count = 0;
    3994           0 :   int loop_at_this_size = 0;
    3995           0 :   int loop = 0;
    3996             : #if !CONFIG_XIPHRC
    3997           0 :   int overshoot_seen = 0;
    3998           0 :   int undershoot_seen = 0;
    3999             : #endif
    4000             :   int frame_over_shoot_limit;
    4001             :   int frame_under_shoot_limit;
    4002           0 :   int q = 0, q_low = 0, q_high = 0;
    4003           0 :   const int use_upsampled_ref = cpi->sf.use_upsampled_references;
    4004             : 
    4005           0 :   set_size_independent_vars(cpi);
    4006             : 
    4007             :   do {
    4008           0 :     aom_clear_system_state();
    4009             : 
    4010           0 :     setup_frame_size(cpi);
    4011             : 
    4012             : #if CONFIG_FRAME_SUPERRES
    4013             :     if (loop_count == 0 || av1_resize_pending(cpi) || cpi->superres_pending) {
    4014             : #else
    4015           0 :     if (loop_count == 0 || av1_resize_pending(cpi)) {
    4016             : #endif  // CONFIG_FRAME_SUPERRES
    4017           0 :       set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
    4018             : 
    4019             :       // cpi->sf.use_upsampled_references can be different from frame to frame.
    4020             :       // Every time when cpi->sf.use_upsampled_references is changed from 0 to
    4021             :       // 1.
    4022             :       // The reference frames for this frame have to be up-sampled before
    4023             :       // encoding.
    4024           0 :       if (!use_upsampled_ref && cpi->sf.use_upsampled_references &&
    4025           0 :           cm->frame_type != KEY_FRAME)
    4026           0 :         reset_use_upsampled_references(cpi);
    4027             : 
    4028             :       // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
    4029           0 :       set_mv_search_params(cpi);
    4030             : 
    4031             : #if !CONFIG_XIPHRC
    4032             :       // Reset the loop state for new frame size.
    4033           0 :       overshoot_seen = 0;
    4034           0 :       undershoot_seen = 0;
    4035             : #endif
    4036             : 
    4037             :       // Advance resize to next state now that updates are done
    4038           0 :       av1_resize_step(cpi);
    4039             : 
    4040           0 :       q_low = bottom_index;
    4041           0 :       q_high = top_index;
    4042             : 
    4043           0 :       loop_at_this_size = 0;
    4044             :     }
    4045             : 
    4046             :     // Decide frame size bounds first time through.
    4047           0 :     if (loop_count == 0) {
    4048           0 :       av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
    4049             :                                        &frame_under_shoot_limit,
    4050             :                                        &frame_over_shoot_limit);
    4051             :     }
    4052             : 
    4053           0 :     cpi->source =
    4054           0 :         av1_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source);
    4055             : 
    4056           0 :     if (cpi->unscaled_last_source != NULL)
    4057           0 :       cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
    4058             :                                                &cpi->scaled_last_source);
    4059             : 
    4060           0 :     if (frame_is_intra_only(cm) == 0) {
    4061           0 :       if (loop_count > 0) {
    4062           0 :         release_scaled_references(cpi);
    4063             :       }
    4064           0 :       av1_scale_references(cpi);
    4065             :     }
    4066             : 
    4067           0 :     av1_set_quantizer(cm, q);
    4068             : 
    4069           0 :     if (loop_count == 0) setup_frame(cpi);
    4070             : 
    4071             : #if CONFIG_Q_ADAPT_PROBS
    4072             :     // Base q-index may have changed, so we need to assign proper default coef
    4073             :     // probs before every iteration.
    4074             :     if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    4075             :       int i;
    4076             :       av1_default_coef_probs(cm);
    4077             :       if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
    4078             :           cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
    4079             :         for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
    4080             :       } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
    4081             :         cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
    4082             :       }
    4083             :     }
    4084             : #endif  // CONFIG_Q_ADAPT_PROBS
    4085             : 
    4086             :     // Variance adaptive and in frame q adjustment experiments are mutually
    4087             :     // exclusive.
    4088           0 :     if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
    4089           0 :       av1_vaq_frame_setup(cpi);
    4090           0 :     } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
    4091           0 :       av1_setup_in_frame_q_adj(cpi);
    4092             :     }
    4093             : 
    4094             :     // transform / motion compensation build reconstruction frame
    4095           0 :     av1_encode_frame(cpi);
    4096             : 
    4097             :     // Update the skip mb flag probabilities based on the distribution
    4098             :     // seen in the last encoder iteration.
    4099             :     // update_base_skip_probs(cpi);
    4100             : 
    4101           0 :     aom_clear_system_state();
    4102             : 
    4103             :     // Dummy pack of the bitstream using up to date stats to get an
    4104             :     // accurate estimate of output frame size to determine if we need
    4105             :     // to recode.
    4106           0 :     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
    4107           0 :       save_coding_context(cpi);
    4108             : 
    4109           0 :       av1_pack_bitstream(cpi, dest, size);
    4110             : 
    4111           0 :       rc->projected_frame_size = (int)(*size) << 3;
    4112           0 :       restore_coding_context(cpi);
    4113             : 
    4114           0 :       if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
    4115             :     }
    4116             : 
    4117           0 :     if (cpi->oxcf.rc_mode == AOM_Q) {
    4118           0 :       loop = 0;
    4119             :     } else {
    4120           0 :       if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
    4121           0 :           (rc->projected_frame_size < rc->max_frame_bandwidth)) {
    4122           0 :         int last_q = q;
    4123             :         int64_t kf_err;
    4124             : 
    4125           0 :         int64_t high_err_target = cpi->ambient_err;
    4126           0 :         int64_t low_err_target = cpi->ambient_err >> 1;
    4127             : 
    4128             : #if CONFIG_HIGHBITDEPTH
    4129           0 :         if (cm->use_highbitdepth) {
    4130           0 :           kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
    4131             :         } else {
    4132           0 :           kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
    4133             :         }
    4134             : #else
    4135             :         kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
    4136             : #endif  // CONFIG_HIGHBITDEPTH
    4137             : 
    4138             :         // Prevent possible divide by zero error below for perfect KF
    4139           0 :         kf_err += !kf_err;
    4140             : 
    4141             :         // The key frame is not good enough or we can afford
    4142             :         // to make it better without undue risk of popping.
    4143           0 :         if ((kf_err > high_err_target &&
    4144           0 :              rc->projected_frame_size <= frame_over_shoot_limit) ||
    4145           0 :             (kf_err > low_err_target &&
    4146           0 :              rc->projected_frame_size <= frame_under_shoot_limit)) {
    4147             :           // Lower q_high
    4148           0 :           q_high = q > q_low ? q - 1 : q_low;
    4149             : 
    4150             :           // Adjust Q
    4151           0 :           q = (int)((q * high_err_target) / kf_err);
    4152           0 :           q = AOMMIN(q, (q_high + q_low) >> 1);
    4153           0 :         } else if (kf_err < low_err_target &&
    4154           0 :                    rc->projected_frame_size >= frame_under_shoot_limit) {
    4155             :           // The key frame is much better than the previous frame
    4156             :           // Raise q_low
    4157           0 :           q_low = q < q_high ? q + 1 : q_high;
    4158             : 
    4159             :           // Adjust Q
    4160           0 :           q = (int)((q * low_err_target) / kf_err);
    4161           0 :           q = AOMMIN(q, (q_high + q_low + 1) >> 1);
    4162             :         }
    4163             : 
    4164             :         // Clamp Q to upper and lower limits:
    4165           0 :         q = clamp(q, q_low, q_high);
    4166             : 
    4167           0 :         loop = q != last_q;
    4168           0 :       } else if (recode_loop_test(cpi, frame_over_shoot_limit,
    4169             :                                   frame_under_shoot_limit, q,
    4170             :                                   AOMMAX(q_high, top_index), bottom_index)) {
    4171             :         // Is the projected frame size out of range and are we allowed
    4172             :         // to attempt to recode.
    4173           0 :         int last_q = q;
    4174             : #if !CONFIG_XIPHRC
    4175           0 :         int retries = 0;
    4176             : 
    4177             :         // TODO(afergs): Replace removed recode when av1_resize_pending is true
    4178             : 
    4179             :         // Frame size out of permitted range:
    4180             :         // Update correction factor & compute new Q to try...
    4181             :         // Frame is too large
    4182           0 :         if (rc->projected_frame_size > rc->this_frame_target) {
    4183             :           // Special case if the projected size is > the max allowed.
    4184           0 :           if (rc->projected_frame_size >= rc->max_frame_bandwidth)
    4185           0 :             q_high = rc->worst_quality;
    4186             : 
    4187             :           // Raise Qlow as to at least the current value
    4188           0 :           q_low = q < q_high ? q + 1 : q_high;
    4189             : 
    4190           0 :           if (undershoot_seen || loop_at_this_size > 1) {
    4191             :             // Update rate_correction_factor unless
    4192           0 :             av1_rc_update_rate_correction_factors(cpi);
    4193             : 
    4194           0 :             q = (q_high + q_low + 1) / 2;
    4195             :           } else {
    4196             :             // Update rate_correction_factor unless
    4197           0 :             av1_rc_update_rate_correction_factors(cpi);
    4198             : 
    4199           0 :             q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
    4200             :                                   AOMMAX(q_high, top_index));
    4201             : 
    4202           0 :             while (q < q_low && retries < 10) {
    4203           0 :               av1_rc_update_rate_correction_factors(cpi);
    4204           0 :               q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
    4205             :                                     AOMMAX(q_high, top_index));
    4206           0 :               retries++;
    4207             :             }
    4208             :           }
    4209             : 
    4210           0 :           overshoot_seen = 1;
    4211             :         } else {
    4212             :           // Frame is too small
    4213           0 :           q_high = q > q_low ? q - 1 : q_low;
    4214             : 
    4215           0 :           if (overshoot_seen || loop_at_this_size > 1) {
    4216           0 :             av1_rc_update_rate_correction_factors(cpi);
    4217           0 :             q = (q_high + q_low) / 2;
    4218             :           } else {
    4219           0 :             av1_rc_update_rate_correction_factors(cpi);
    4220           0 :             q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
    4221             :                                   top_index);
    4222             :             // Special case reset for qlow for constrained quality.
    4223             :             // This should only trigger where there is very substantial
    4224             :             // undershoot on a frame and the auto cq level is above
    4225             :             // the user passsed in value.
    4226           0 :             if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
    4227           0 :               q_low = q;
    4228             :             }
    4229             : 
    4230           0 :             while (q > q_high && retries < 10) {
    4231           0 :               av1_rc_update_rate_correction_factors(cpi);
    4232           0 :               q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
    4233             :                                     top_index);
    4234           0 :               retries++;
    4235             :             }
    4236             :           }
    4237             : 
    4238           0 :           undershoot_seen = 1;
    4239             :         }
    4240             : #endif
    4241             : 
    4242             :         // Clamp Q to upper and lower limits:
    4243           0 :         q = clamp(q, q_low, q_high);
    4244             : 
    4245           0 :         loop = (q != last_q);
    4246             :       } else {
    4247           0 :         loop = 0;
    4248             :       }
    4249             :     }
    4250             : 
    4251             :     // Special case for overlay frame.
    4252           0 :     if (rc->is_src_frame_alt_ref &&
    4253           0 :         rc->projected_frame_size < rc->max_frame_bandwidth)
    4254           0 :       loop = 0;
    4255             : 
    4256             : #if CONFIG_GLOBAL_MOTION
    4257           0 :     if (recode_loop_test_global_motion(cpi)) {
    4258           0 :       loop = 1;
    4259             :     }
    4260             : #endif  // CONFIG_GLOBAL_MOTION
    4261             : 
    4262           0 :     if (loop) {
    4263           0 :       ++loop_count;
    4264           0 :       ++loop_at_this_size;
    4265             : 
    4266             : #if CONFIG_INTERNAL_STATS
    4267             :       ++cpi->tot_recode_hits;
    4268             : #endif
    4269             :     }
    4270           0 :   } while (loop);
    4271           0 : }
    4272             : 
    4273           0 : static int get_ref_frame_flags(const AV1_COMP *cpi) {
    4274           0 :   const int *const map = cpi->common.ref_frame_map;
    4275             : 
    4276             : #if CONFIG_EXT_REFS
    4277           0 :   const int last2_is_last =
    4278           0 :       map[cpi->lst_fb_idxes[1]] == map[cpi->lst_fb_idxes[0]];
    4279           0 :   const int last3_is_last =
    4280           0 :       map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[0]];
    4281           0 :   const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[0]];
    4282             : #if CONFIG_ONE_SIDED_COMPOUND
    4283           0 :   const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
    4284           0 :   const int last3_is_last2 =
    4285           0 :       map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
    4286           0 :   const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
    4287           0 :   const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
    4288             : #else
    4289             :   const int bwd_is_last = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[0]];
    4290             :   const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
    4291             : 
    4292             :   const int last3_is_last2 =
    4293             :       map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
    4294             :   const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
    4295             :   const int bwd_is_last2 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[1]];
    4296             : 
    4297             :   const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
    4298             :   const int bwd_is_last3 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[2]];
    4299             : 
    4300             :   const int bwd_is_gld = map[cpi->bwd_fb_idx] == map[cpi->gld_fb_idx];
    4301             : 
    4302             : #endif
    4303           0 :   const int last2_is_alt = map[cpi->lst_fb_idxes[1]] == map[cpi->alt_fb_idx];
    4304           0 :   const int last3_is_alt = map[cpi->lst_fb_idxes[2]] == map[cpi->alt_fb_idx];
    4305           0 :   const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
    4306           0 :   const int bwd_is_alt = map[cpi->bwd_fb_idx] == map[cpi->alt_fb_idx];
    4307             : #else
    4308             :   const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
    4309             :   const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
    4310             :   const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
    4311             : #endif  // CONFIG_EXT_REFS
    4312             : 
    4313           0 :   int flags = AOM_REFFRAME_ALL;
    4314             : 
    4315             : #if CONFIG_EXT_REFS
    4316             :   // Disable the use of BWDREF_FRAME for non-bipredictive frames.
    4317           0 :   if (!(cpi->rc.is_bipred_frame || cpi->rc.is_last_bipred_frame ||
    4318           0 :         (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs)))
    4319           0 :     flags &= ~AOM_BWD_FLAG;
    4320             : #endif  // CONFIG_EXT_REFS
    4321             : 
    4322           0 :   if (gld_is_last || gld_is_alt) flags &= ~AOM_GOLD_FLAG;
    4323             : 
    4324           0 :   if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
    4325             : 
    4326           0 :   if (alt_is_last) flags &= ~AOM_ALT_FLAG;
    4327             : 
    4328             : #if CONFIG_EXT_REFS
    4329           0 :   if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
    4330             : 
    4331           0 :   if (last3_is_last || last3_is_last2 || last3_is_alt) flags &= ~AOM_LAST3_FLAG;
    4332             : 
    4333           0 :   if (gld_is_last2 || gld_is_last3) flags &= ~AOM_GOLD_FLAG;
    4334             : 
    4335             : #if CONFIG_ONE_SIDED_COMPOUND  // Changes LL & HL bitstream
    4336             :   /* Allow biprediction between two identical frames (e.g. bwd_is_last = 1) */
    4337           0 :   if (bwd_is_alt && (flags & AOM_BWD_FLAG)) flags &= ~AOM_BWD_FLAG;
    4338             : #else
    4339             :   if ((bwd_is_last || bwd_is_last2 || bwd_is_last3 || bwd_is_gld ||
    4340             :        bwd_is_alt) &&
    4341             :       (flags & AOM_BWD_FLAG))
    4342             :     flags &= ~AOM_BWD_FLAG;
    4343             : #endif
    4344             : #endif  // CONFIG_EXT_REFS
    4345             : 
    4346           0 :   return flags;
    4347             : }
    4348             : 
    4349           0 : static void set_ext_overrides(AV1_COMP *cpi) {
    4350             :   // Overrides the defaults with the externally supplied values with
    4351             :   // av1_update_reference() and av1_update_entropy() calls
    4352             :   // Note: The overrides are valid only for the next frame passed
    4353             :   // to encode_frame_to_data_rate() function
    4354           0 :   if (cpi->ext_refresh_frame_context_pending) {
    4355           0 :     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
    4356           0 :     cpi->ext_refresh_frame_context_pending = 0;
    4357             :   }
    4358           0 :   if (cpi->ext_refresh_frame_flags_pending) {
    4359           0 :     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
    4360           0 :     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
    4361           0 :     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
    4362           0 :     cpi->ext_refresh_frame_flags_pending = 0;
    4363             :   }
    4364           0 : }
    4365             : 
    4366           0 : static void set_arf_sign_bias(AV1_COMP *cpi) {
    4367           0 :   AV1_COMMON *const cm = &cpi->common;
    4368             :   int arf_sign_bias;
    4369             : #if CONFIG_EXT_REFS
    4370           0 :   const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    4371             :   // The arf_sign_bias will be one for internal ARFs'
    4372           0 :   arf_sign_bias = cpi->rc.source_alt_ref_active &&
    4373           0 :                   (!cpi->refresh_alt_ref_frame ||
    4374           0 :                    (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
    4375             : #else
    4376             :   if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
    4377             :     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    4378             :     arf_sign_bias = cpi->rc.source_alt_ref_active &&
    4379             :                     (!cpi->refresh_alt_ref_frame ||
    4380             :                      (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
    4381             :   } else {
    4382             :     arf_sign_bias =
    4383             :         (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
    4384             :   }
    4385             : #endif  // CONFIG_EXT_REFS
    4386             : 
    4387           0 :   cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
    4388             : #if CONFIG_EXT_REFS
    4389           0 :   cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
    4390             : #endif  // CONFIG_EXT_REFS
    4391           0 : }
    4392             : 
    4393           0 : static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
    4394             :   InterpFilter ifilter;
    4395           0 :   int ref_total[TOTAL_REFS_PER_FRAME] = { 0 };
    4396             :   MV_REFERENCE_FRAME ref;
    4397           0 :   int mask = 0;
    4398           0 :   int arf_idx = ALTREF_FRAME;
    4399             : 
    4400             : #if CONFIG_EXT_REFS
    4401             :   // Get which arf used as ALTREF_FRAME
    4402           0 :   if (cpi->oxcf.pass == 2)
    4403           0 :     arf_idx += cpi->twopass.gf_group.arf_ref_idx[cpi->twopass.gf_group.index];
    4404             : #endif  // CONFIG_EXT_REFS
    4405             : 
    4406           0 :   if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
    4407           0 :     return mask;
    4408             : 
    4409             : #if CONFIG_EXT_REFS
    4410           0 :   for (ref = LAST_FRAME; ref < ALTREF_FRAME; ++ref)
    4411           0 :     for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
    4412           0 :       ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
    4413             : 
    4414           0 :   for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
    4415           0 :     ref_total[ref] += cpi->interp_filter_selected[arf_idx][ifilter];
    4416             : #else
    4417             :   for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
    4418             :     for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
    4419             :       ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
    4420             : #endif  // CONFIG_EXT_REFS
    4421             : 
    4422           0 :   for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
    4423           0 :     if ((ref_total[LAST_FRAME] &&
    4424           0 :          cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
    4425             : #if CONFIG_EXT_REFS
    4426           0 :         (ref_total[LAST2_FRAME] == 0 ||
    4427           0 :          cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
    4428           0 :              ref_total[LAST2_FRAME]) &&
    4429           0 :         (ref_total[LAST3_FRAME] == 0 ||
    4430           0 :          cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
    4431           0 :              ref_total[LAST3_FRAME]) &&
    4432             : #endif  // CONFIG_EXT_REFS
    4433           0 :         (ref_total[GOLDEN_FRAME] == 0 ||
    4434           0 :          cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
    4435           0 :              ref_total[GOLDEN_FRAME]) &&
    4436             : #if CONFIG_EXT_REFS
    4437           0 :         (ref_total[BWDREF_FRAME] == 0 ||
    4438           0 :          cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
    4439           0 :              ref_total[BWDREF_FRAME]) &&
    4440             : #endif  // CONFIG_EXT_REFS
    4441           0 :         (ref_total[ALTREF_FRAME] == 0 ||
    4442           0 :          cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
    4443           0 :              ref_total[ALTREF_FRAME]))
    4444           0 :       mask |= 1 << ifilter;
    4445             :   }
    4446           0 :   return mask;
    4447             : }
    4448             : 
    4449             : #define DUMP_RECON_FRAMES 0
    4450             : 
    4451             : #if DUMP_RECON_FRAMES == 1
    4452             : // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
    4453             : static void dump_filtered_recon_frames(AV1_COMP *cpi) {
    4454             :   AV1_COMMON *const cm = &cpi->common;
    4455             :   const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
    4456             :   int h;
    4457             :   char file_name[256] = "/tmp/enc_filtered_recon.yuv";
    4458             :   FILE *f_recon = NULL;
    4459             : 
    4460             :   if (recon_buf == NULL || !cm->show_frame) {
    4461             :     printf("Frame %d is not ready or no show to dump.\n",
    4462             :            cm->current_video_frame);
    4463             :     return;
    4464             :   }
    4465             : 
    4466             :   if (cm->current_video_frame == 0) {
    4467             :     if ((f_recon = fopen(file_name, "wb")) == NULL) {
    4468             :       printf("Unable to open file %s to write.\n", file_name);
    4469             :       return;
    4470             :     }
    4471             :   } else {
    4472             :     if ((f_recon = fopen(file_name, "ab")) == NULL) {
    4473             :       printf("Unable to open file %s to append.\n", file_name);
    4474             :       return;
    4475             :     }
    4476             :   }
    4477             :   printf(
    4478             :       "\nFrame=%5d, encode_update_type[%5d]=%1d, show_existing_frame=%d, "
    4479             :       "y_stride=%4d, uv_stride=%4d, width=%4d, height=%4d\n",
    4480             :       cm->current_video_frame, cpi->twopass.gf_group.index,
    4481             :       cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
    4482             :       cm->show_existing_frame, recon_buf->y_stride, recon_buf->uv_stride,
    4483             :       cm->width, cm->height);
    4484             : 
    4485             :   // --- Y ---
    4486             :   for (h = 0; h < cm->height; ++h) {
    4487             :     fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
    4488             :            f_recon);
    4489             :   }
    4490             :   // --- U ---
    4491             :   for (h = 0; h < (cm->height >> 1); ++h) {
    4492             :     fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
    4493             :            f_recon);
    4494             :   }
    4495             :   // --- V ---
    4496             :   for (h = 0; h < (cm->height >> 1); ++h) {
    4497             :     fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
    4498             :            f_recon);
    4499             :   }
    4500             : 
    4501             :   fclose(f_recon);
    4502             : }
    4503             : #endif  // DUMP_RECON_FRAMES
    4504             : 
    4505             : #if CONFIG_EC_ADAPT
    4506             : 
    4507           0 : static void make_update_tile_list_enc(AV1_COMP *cpi, const int tile_rows,
    4508             :                                       const int tile_cols,
    4509             :                                       FRAME_CONTEXT *ec_ctxs[]) {
    4510             :   int i;
    4511           0 :   for (i = 0; i < tile_rows * tile_cols; ++i)
    4512           0 :     ec_ctxs[i] = &cpi->tile_data[i].tctx;
    4513           0 : }
    4514             : 
    4515             : #endif
    4516           0 : static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
    4517             :                                       uint8_t *dest, int skip_adapt,
    4518             :                                       unsigned int *frame_flags) {
    4519           0 :   AV1_COMMON *const cm = &cpi->common;
    4520           0 :   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
    4521           0 :   struct segmentation *const seg = &cm->seg;
    4522             :   TX_SIZE t;
    4523             : #if CONFIG_EC_ADAPT
    4524           0 :   FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
    4525             :                                          sizeof(&cpi->tile_data[0].tctx));
    4526           0 :   aom_cdf_prob **cdf_ptrs =
    4527           0 :       aom_malloc(cm->tile_rows * cm->tile_cols *
    4528             :                  sizeof(&cpi->tile_data[0].tctx.partition_cdf[0][0]));
    4529             : #endif
    4530             : #if CONFIG_XIPHRC
    4531             :   int frame_type;
    4532             :   int drop_this_frame = 0;
    4533             : #endif  // CONFIG_XIPHRC
    4534           0 :   set_ext_overrides(cpi);
    4535           0 :   aom_clear_system_state();
    4536             : 
    4537             :   // Set the arf sign bias for this frame.
    4538           0 :   set_arf_sign_bias(cpi);
    4539             : #if CONFIG_TEMPMV_SIGNALING
    4540             :   // frame type has been decided outside of this function call
    4541           0 :   cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
    4542           0 :   cm->use_prev_frame_mvs =
    4543           0 :       !cpi->oxcf.disable_tempmv && !cm->cur_frame->intra_only;
    4544             : #endif
    4545             : 
    4546             : #if CONFIG_EXT_REFS
    4547             :   // NOTE:
    4548             :   // (1) Move the setup of the ref_frame_flags upfront as it would be
    4549             :   //     determined by the current frame properties;
    4550             :   // (2) The setup of the ref_frame_flags applies to both show_existing_frame's
    4551             :   //     and the other cases.
    4552           0 :   if (cm->current_video_frame > 0)
    4553           0 :     cpi->ref_frame_flags = get_ref_frame_flags(cpi);
    4554             : 
    4555           0 :   if (cm->show_existing_frame) {
    4556             :     // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
    4557             :     //               BWDREF_FRAME in the reference frame buffer.
    4558           0 :     cm->frame_type = INTER_FRAME;
    4559           0 :     cm->show_frame = 1;
    4560           0 :     cpi->frame_flags = *frame_flags;
    4561             : 
    4562             :     // In the case of show_existing frame, we will not send fresh flag
    4563             :     // to decoder. Any change in the reference frame buffer can be done by
    4564             :     // switching the virtual indices.
    4565             : 
    4566           0 :     cpi->refresh_last_frame = 0;
    4567           0 :     cpi->refresh_golden_frame = 0;
    4568           0 :     cpi->refresh_bwd_ref_frame = 0;
    4569           0 :     cpi->refresh_alt_ref_frame = 0;
    4570             : 
    4571           0 :     cpi->rc.is_bwd_ref_frame = 0;
    4572           0 :     cpi->rc.is_last_bipred_frame = 0;
    4573           0 :     cpi->rc.is_bipred_frame = 0;
    4574             : 
    4575             :     // Build the bitstream
    4576           0 :     av1_pack_bitstream(cpi, dest, size);
    4577             : 
    4578             :     // Set up frame to show to get ready for stats collection.
    4579           0 :     cm->frame_to_show = get_frame_new_buffer(cm);
    4580             : 
    4581             : #if DUMP_RECON_FRAMES == 1
    4582             :     // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
    4583             :     dump_filtered_recon_frames(cpi);
    4584             : #endif  // DUMP_RECON_FRAMES
    4585             : 
    4586             :     // Update the LAST_FRAME in the reference frame buffer.
    4587           0 :     av1_update_reference_frames(cpi);
    4588             : 
    4589             :     // Update frame flags
    4590           0 :     cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
    4591           0 :     cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
    4592           0 :     cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
    4593             : 
    4594           0 :     *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
    4595             : 
    4596             :     // Update the frame type
    4597           0 :     cm->last_frame_type = cm->frame_type;
    4598             : 
    4599             :     // Since we allocate a spot for the OVERLAY frame in the gf group, we need
    4600             :     // to do post-encoding update accordingly.
    4601           0 :     if (cpi->rc.is_src_frame_alt_ref) {
    4602           0 :       av1_set_target_rate(cpi);
    4603             : #if CONFIG_XIPHRC
    4604             :       frame_type = cm->frame_type == INTER_FRAME ? OD_P_FRAME : OD_I_FRAME;
    4605             :       drop_this_frame = od_enc_rc_update_state(
    4606             :           &cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
    4607             :           cpi->refresh_alt_ref_frame, frame_type, cpi->droppable);
    4608             : #else
    4609           0 :       av1_rc_postencode_update(cpi, *size);
    4610             : #endif
    4611             :     }
    4612             : 
    4613           0 :     cm->last_width = cm->width;
    4614           0 :     cm->last_height = cm->height;
    4615             : 
    4616           0 :     ++cm->current_video_frame;
    4617             : 
    4618             : #if CONFIG_EC_ADAPT
    4619           0 :     aom_free(tile_ctxs);
    4620           0 :     aom_free(cdf_ptrs);
    4621             : #endif
    4622           0 :     return;
    4623             :   }
    4624             : #endif  // CONFIG_EXT_REFS
    4625             : 
    4626             :   // Set default state for segment based loop filter update flags.
    4627           0 :   cm->lf.mode_ref_delta_update = 0;
    4628             : 
    4629           0 :   if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
    4630           0 :     cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
    4631             : 
    4632             :   // Set various flags etc to special state if it is a key frame.
    4633           0 :   if (frame_is_intra_only(cm)) {
    4634             :     // Reset the loop filter deltas and segmentation map.
    4635           0 :     av1_reset_segment_features(cm);
    4636             : 
    4637             :     // If segmentation is enabled force a map update for key frames.
    4638           0 :     if (seg->enabled) {
    4639           0 :       seg->update_map = 1;
    4640           0 :       seg->update_data = 1;
    4641             :     }
    4642             : 
    4643             :     // The alternate reference frame cannot be active for a key frame.
    4644           0 :     cpi->rc.source_alt_ref_active = 0;
    4645             : 
    4646           0 :     cm->error_resilient_mode = oxcf->error_resilient_mode;
    4647             : 
    4648             :     // By default, encoder assumes decoder can use prev_mi.
    4649           0 :     if (cm->error_resilient_mode) {
    4650           0 :       cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
    4651           0 :       cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
    4652           0 :     } else if (cm->intra_only) {
    4653             :       // Only reset the current context.
    4654           0 :       cm->reset_frame_context = RESET_FRAME_CONTEXT_CURRENT;
    4655             :     }
    4656             :   }
    4657             : #if CONFIG_TILE_GROUPS
    4658           0 :   if (cpi->oxcf.mtu == 0) {
    4659           0 :     cm->num_tg = cpi->oxcf.num_tile_groups;
    4660             :   } else {
    4661             :     // Use a default value for the purposes of weighting costs in probability
    4662             :     // updates
    4663           0 :     cm->num_tg = DEFAULT_MAX_NUM_TG;
    4664             :   }
    4665             : #endif
    4666             : 
    4667             : #if CONFIG_EXT_TILE
    4668             :   cm->tile_encoding_mode = cpi->oxcf.tile_encoding_mode;
    4669             : #endif  // CONFIG_EXT_TILE
    4670             : 
    4671             : #if CONFIG_XIPHRC
    4672             :   if (drop_this_frame) {
    4673             :     av1_rc_postencode_update_drop_frame(cpi);
    4674             :     ++cm->current_video_frame;
    4675             : #if CONFIG_EC_ADAPT
    4676             :     aom_free(tile_ctxs);
    4677             :     aom_free(cdf_ptrs);
    4678             : #endif
    4679             :     return;
    4680             :   }
    4681             : #else
    4682             :   // For 1 pass CBR, check if we are dropping this frame.
    4683             :   // Never drop on key frame.
    4684           0 :   if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
    4685           0 :       cm->frame_type != KEY_FRAME) {
    4686           0 :     if (av1_rc_drop_frame(cpi)) {
    4687           0 :       av1_rc_postencode_update_drop_frame(cpi);
    4688           0 :       ++cm->current_video_frame;
    4689             : #if CONFIG_EC_ADAPT
    4690           0 :       aom_free(tile_ctxs);
    4691           0 :       aom_free(cdf_ptrs);
    4692             : #endif
    4693           0 :       return;
    4694             :     }
    4695             :   }
    4696             : #endif
    4697             : 
    4698           0 :   aom_clear_system_state();
    4699             : 
    4700             : #if CONFIG_INTERNAL_STATS
    4701             :   memset(cpi->mode_chosen_counts, 0,
    4702             :          MAX_MODES * sizeof(*cpi->mode_chosen_counts));
    4703             : #endif
    4704             : 
    4705             : #if CONFIG_REFERENCE_BUFFER
    4706             :   {
    4707             :     /* Non-normative definition of current_frame_id ("frame counter" with
    4708             :     * wraparound) */
    4709           0 :     const int frame_id_length = FRAME_ID_LENGTH_MINUS7 + 7;
    4710           0 :     if (cm->current_frame_id == -1) {
    4711             :       int lsb, msb;
    4712             : /* quasi-random initialization of current_frame_id for a key frame */
    4713             : #if CONFIG_HIGHBITDEPTH
    4714           0 :       if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
    4715           0 :         lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
    4716           0 :         msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
    4717             :       } else {
    4718             : #endif
    4719           0 :         lsb = cpi->source->y_buffer[0] & 0xff;
    4720           0 :         msb = cpi->source->y_buffer[1] & 0xff;
    4721             : #if CONFIG_HIGHBITDEPTH
    4722             :       }
    4723             : #endif
    4724           0 :       cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
    4725             :     } else {
    4726           0 :       cm->current_frame_id =
    4727           0 :           (cm->current_frame_id + 1 + (1 << frame_id_length)) %
    4728           0 :           (1 << frame_id_length);
    4729             :     }
    4730             :   }
    4731             : #endif
    4732             : 
    4733             : #if CONFIG_EXT_DELTA_Q
    4734           0 :   cm->delta_q_present_flag = cpi->oxcf.deltaq_mode != NO_DELTA_Q;
    4735           0 :   cm->delta_lf_present_flag = cpi->oxcf.deltaq_mode == DELTA_Q_LF;
    4736             : #endif
    4737             : 
    4738           0 :   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
    4739           0 :     encode_without_recode_loop(cpi);
    4740             :   } else {
    4741           0 :     encode_with_recode_loop(cpi, size, dest);
    4742             :   }
    4743             : 
    4744             : #ifdef OUTPUT_YUV_SKINMAP
    4745             :   if (cpi->common.current_video_frame > 1) {
    4746             :     av1_compute_skin_map(cpi, yuv_skinmap_file);
    4747             :   }
    4748             : #endif  // OUTPUT_YUV_SKINMAP
    4749             : 
    4750             :   // Special case code to reduce pulsing when key frames are forced at a
    4751             :   // fixed interval. Note the reconstruction error if it is the frame before
    4752             :   // the force key frame
    4753           0 :   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
    4754             : #if CONFIG_HIGHBITDEPTH
    4755           0 :     if (cm->use_highbitdepth) {
    4756           0 :       cpi->ambient_err =
    4757           0 :           aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
    4758             :     } else {
    4759           0 :       cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
    4760             :     }
    4761             : #else
    4762             :     cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
    4763             : #endif  // CONFIG_HIGHBITDEPTH
    4764             :   }
    4765             : 
    4766             :   // If the encoder forced a KEY_FRAME decision
    4767           0 :   if (cm->frame_type == KEY_FRAME) {
    4768           0 :     cpi->refresh_last_frame = 1;
    4769             :   }
    4770             : 
    4771           0 :   cm->frame_to_show = get_frame_new_buffer(cm);
    4772           0 :   cm->frame_to_show->color_space = cm->color_space;
    4773           0 :   cm->frame_to_show->color_range = cm->color_range;
    4774           0 :   cm->frame_to_show->render_width = cm->render_width;
    4775           0 :   cm->frame_to_show->render_height = cm->render_height;
    4776             : 
    4777             : #if CONFIG_EXT_REFS
    4778             : // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
    4779             : // off.
    4780             : #endif  // CONFIG_EXT_REFS
    4781             : 
    4782             :   // Pick the loop filter level for the frame.
    4783           0 :   loopfilter_frame(cpi, cm);
    4784             : 
    4785             :   // Build the bitstream
    4786           0 :   av1_pack_bitstream(cpi, dest, size);
    4787             : 
    4788           0 :   if (skip_adapt) {
    4789             : #if CONFIG_EC_ADAPT
    4790           0 :     aom_free(tile_ctxs);
    4791           0 :     aom_free(cdf_ptrs);
    4792             : #endif
    4793           0 :     return;
    4794             :   }
    4795             : 
    4796             : #if CONFIG_REFERENCE_BUFFER
    4797             :   {
    4798             :     int i;
    4799             :     /* Update reference frame id values based on the value of refresh_mask */
    4800           0 :     for (i = 0; i < REF_FRAMES; i++) {
    4801           0 :       if ((cm->refresh_mask >> i) & 1) {
    4802           0 :         cm->ref_frame_id[i] = cm->current_frame_id;
    4803             :       }
    4804             :     }
    4805             :   }
    4806             : #endif
    4807             : 
    4808             : #if DUMP_RECON_FRAMES == 1
    4809             :   // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
    4810             :   if (cm->show_frame) dump_filtered_recon_frames(cpi);
    4811             : #endif  // DUMP_RECON_FRAMES
    4812             : 
    4813           0 :   if (cm->seg.update_map) update_reference_segmentation_map(cpi);
    4814             : 
    4815           0 :   if (frame_is_intra_only(cm) == 0) {
    4816           0 :     release_scaled_references(cpi);
    4817             :   }
    4818             : 
    4819           0 :   av1_update_reference_frames(cpi);
    4820             : 
    4821           0 :   for (t = 0; t < TX_SIZES; t++)
    4822           0 :     av1_full_to_model_counts(cpi->td.counts->coef[t],
    4823           0 :                              cpi->td.rd_counts.coef_counts[t]);
    4824             : #if CONFIG_ENTROPY_STATS
    4825             :   av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
    4826             : #endif  // CONFIG_ENTROPY_STATS
    4827           0 :   if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
    4828           0 :     av1_adapt_coef_probs(cm);
    4829           0 :     av1_adapt_intra_frame_probs(cm);
    4830             : #if CONFIG_EC_ADAPT
    4831           0 :     make_update_tile_list_enc(cpi, cm->tile_rows, cm->tile_cols, tile_ctxs);
    4832           0 :     av1_average_tile_coef_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
    4833           0 :                                cm->tile_rows * cm->tile_cols);
    4834           0 :     av1_average_tile_intra_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
    4835           0 :                                 cm->tile_rows * cm->tile_cols);
    4836             : #if CONFIG_PVQ
    4837             :     av1_average_tile_pvq_cdfs(cpi->common.fc, tile_ctxs,
    4838             :                               cm->tile_rows * cm->tile_cols);
    4839             : #endif  // CONFIG_PVQ
    4840             : #endif  // CONFIG_EC_ADAPT
    4841             : #if CONFIG_ADAPT_SCAN
    4842             :     av1_adapt_scan_order(cm);
    4843             : #endif  // CONFIG_ADAPT_SCAN
    4844             :   }
    4845             : 
    4846           0 :   if (!frame_is_intra_only(cm)) {
    4847           0 :     if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
    4848           0 :       av1_adapt_inter_frame_probs(cm);
    4849           0 :       av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
    4850             : #if CONFIG_EC_ADAPT
    4851           0 :       av1_average_tile_inter_cdfs(&cpi->common, cpi->common.fc, tile_ctxs,
    4852           0 :                                   cdf_ptrs, cm->tile_rows * cm->tile_cols);
    4853           0 :       av1_average_tile_mv_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
    4854           0 :                                cm->tile_rows * cm->tile_cols);
    4855             : #endif
    4856             :     }
    4857             :   }
    4858             : 
    4859           0 :   if (cpi->refresh_golden_frame == 1)
    4860           0 :     cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
    4861             :   else
    4862           0 :     cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
    4863             : 
    4864           0 :   if (cpi->refresh_alt_ref_frame == 1)
    4865           0 :     cpi->frame_flags |= FRAMEFLAGS_ALTREF;
    4866             :   else
    4867           0 :     cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
    4868             : 
    4869             : #if CONFIG_EXT_REFS
    4870           0 :   if (cpi->refresh_bwd_ref_frame == 1)
    4871           0 :     cpi->frame_flags |= FRAMEFLAGS_BWDREF;
    4872             :   else
    4873           0 :     cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
    4874             : #endif  // CONFIG_EXT_REFS
    4875             : 
    4876             : #if !CONFIG_EXT_REFS
    4877             :   cpi->ref_frame_flags = get_ref_frame_flags(cpi);
    4878             : #endif  // !CONFIG_EXT_REFS
    4879             : 
    4880           0 :   cm->last_frame_type = cm->frame_type;
    4881             : 
    4882             : #if CONFIG_XIPHRC
    4883             :   frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
    4884             : 
    4885             :   drop_this_frame =
    4886             :       od_enc_rc_update_state(&cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
    4887             :                              cpi->refresh_alt_ref_frame, frame_type, 0);
    4888             :   if (drop_this_frame) {
    4889             :     av1_rc_postencode_update_drop_frame(cpi);
    4890             :     ++cm->current_video_frame;
    4891             : #if CONFIG_EC_ADAPT
    4892             :     aom_free(tile_ctxs);
    4893             :     aom_free(cdf_ptrs);
    4894             : #endif
    4895             :     return;
    4896             :   }
    4897             : #else   // !CONFIG_XIPHRC
    4898           0 :   av1_rc_postencode_update(cpi, *size);
    4899             : #endif  // CONFIG_XIPHRC
    4900             : 
    4901             : #if 0
    4902             :   output_frame_level_debug_stats(cpi);
    4903             : #endif
    4904             : 
    4905           0 :   if (cm->frame_type == KEY_FRAME) {
    4906             :     // Tell the caller that the frame was coded as a key frame
    4907           0 :     *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
    4908             :   } else {
    4909           0 :     *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
    4910             :   }
    4911             : 
    4912             :   // Clear the one shot update flags for segmentation map and mode/ref loop
    4913             :   // filter deltas.
    4914           0 :   cm->seg.update_map = 0;
    4915           0 :   cm->seg.update_data = 0;
    4916           0 :   cm->lf.mode_ref_delta_update = 0;
    4917             : 
    4918             :   // keep track of the last coded dimensions
    4919           0 :   cm->last_width = cm->width;
    4920           0 :   cm->last_height = cm->height;
    4921             : 
    4922             :   // reset to normal state now that we are done.
    4923           0 :   if (!cm->show_existing_frame) cm->last_show_frame = cm->show_frame;
    4924             : 
    4925           0 :   if (cm->show_frame) {
    4926             : #if CONFIG_EXT_REFS
    4927             : // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that are
    4928             : // being used as reference.
    4929             : #endif  // CONFIG_EXT_REFS
    4930           0 :     av1_swap_mi_and_prev_mi(cm);
    4931             :     // Don't increment frame counters if this was an altref buffer
    4932             :     // update not a real frame
    4933           0 :     ++cm->current_video_frame;
    4934             :   }
    4935             : 
    4936             : #if CONFIG_EXT_REFS
    4937             :   // NOTE: Shall not refer to any frame not used as reference.
    4938           0 :   if (cm->is_reference_frame)
    4939             : #endif  // CONFIG_EXT_REFS
    4940           0 :     cm->prev_frame = cm->cur_frame;
    4941             : #if CONFIG_EC_ADAPT
    4942           0 :   aom_free(tile_ctxs);
    4943           0 :   aom_free(cdf_ptrs);
    4944             : #endif
    4945             : }
    4946             : 
    4947           0 : static void Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
    4948             :                         int skip_adapt, unsigned int *frame_flags) {
    4949             : #if CONFIG_XIPHRC
    4950             :   int64_t ip_count;
    4951             :   int frame_type, is_golden, is_altref;
    4952             : 
    4953             :   /* Not updated during init so update it here */
    4954             :   if (cpi->oxcf.rc_mode == AOM_Q) cpi->od_rc.quality = cpi->oxcf.cq_level;
    4955             : 
    4956             :   frame_type = od_frame_type(&cpi->od_rc, cpi->od_rc.cur_frame, &is_golden,
    4957             :                              &is_altref, &ip_count);
    4958             : 
    4959             :   if (frame_type == OD_I_FRAME) {
    4960             :     frame_type = KEY_FRAME;
    4961             :     cpi->frame_flags &= FRAMEFLAGS_KEY;
    4962             :   } else if (frame_type == OD_P_FRAME) {
    4963             :     frame_type = INTER_FRAME;
    4964             :   }
    4965             : 
    4966             :   if (is_altref) {
    4967             :     cpi->refresh_alt_ref_frame = 1;
    4968             :     cpi->rc.source_alt_ref_active = 1;
    4969             :   }
    4970             : 
    4971             :   cpi->refresh_golden_frame = is_golden;
    4972             :   cpi->common.frame_type = frame_type;
    4973             :   if (is_golden) cpi->frame_flags &= FRAMEFLAGS_GOLDEN;
    4974             : #else
    4975           0 :   if (cpi->oxcf.rc_mode == AOM_CBR) {
    4976           0 :     av1_rc_get_one_pass_cbr_params(cpi);
    4977             :   } else {
    4978           0 :     av1_rc_get_one_pass_vbr_params(cpi);
    4979             :   }
    4980             : #endif
    4981           0 :   encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags);
    4982           0 : }
    4983             : 
    4984             : #if !CONFIG_XIPHRC
    4985           0 : static void Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
    4986             :                         unsigned int *frame_flags) {
    4987           0 :   encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags);
    4988             : 
    4989             : #if CONFIG_EXT_REFS
    4990             :   // Do not do post-encoding update for those frames that do not have a spot in
    4991             :   // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
    4992             :   // even when show_existing_frame is used.
    4993           0 :   if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
    4994           0 :     av1_twopass_postencode_update(cpi);
    4995             :   }
    4996           0 :   check_show_existing_frame(cpi);
    4997             : #else
    4998             :   av1_twopass_postencode_update(cpi);
    4999             : #endif  // CONFIG_EXT_REFS
    5000           0 : }
    5001             : #endif
    5002             : 
    5003           0 : static void init_ref_frame_bufs(AV1_COMMON *cm) {
    5004             :   int i;
    5005           0 :   BufferPool *const pool = cm->buffer_pool;
    5006           0 :   cm->new_fb_idx = INVALID_IDX;
    5007           0 :   for (i = 0; i < REF_FRAMES; ++i) {
    5008           0 :     cm->ref_frame_map[i] = INVALID_IDX;
    5009           0 :     pool->frame_bufs[i].ref_count = 0;
    5010             :   }
    5011           0 : }
    5012             : 
    5013           0 : static void check_initial_width(AV1_COMP *cpi,
    5014             : #if CONFIG_HIGHBITDEPTH
    5015             :                                 int use_highbitdepth,
    5016             : #endif
    5017             :                                 int subsampling_x, int subsampling_y) {
    5018           0 :   AV1_COMMON *const cm = &cpi->common;
    5019             : 
    5020           0 :   if (!cpi->initial_width ||
    5021             : #if CONFIG_HIGHBITDEPTH
    5022           0 :       cm->use_highbitdepth != use_highbitdepth ||
    5023             : #endif
    5024           0 :       cm->subsampling_x != subsampling_x ||
    5025           0 :       cm->subsampling_y != subsampling_y) {
    5026           0 :     cm->subsampling_x = subsampling_x;
    5027           0 :     cm->subsampling_y = subsampling_y;
    5028             : #if CONFIG_HIGHBITDEPTH
    5029           0 :     cm->use_highbitdepth = use_highbitdepth;
    5030             : #endif
    5031             : 
    5032           0 :     alloc_raw_frame_buffers(cpi);
    5033           0 :     init_ref_frame_bufs(cm);
    5034           0 :     alloc_util_frame_buffers(cpi);
    5035             : 
    5036           0 :     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
    5037             : 
    5038           0 :     cpi->initial_width = cm->width;
    5039           0 :     cpi->initial_height = cm->height;
    5040           0 :     cpi->initial_mbs = cm->MBs;
    5041             :   }
    5042           0 : }
    5043             : 
    5044           0 : int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
    5045             :                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
    5046             :                           int64_t end_time) {
    5047           0 :   AV1_COMMON *const cm = &cpi->common;
    5048             :   struct aom_usec_timer timer;
    5049           0 :   int res = 0;
    5050           0 :   const int subsampling_x = sd->subsampling_x;
    5051           0 :   const int subsampling_y = sd->subsampling_y;
    5052             : #if CONFIG_HIGHBITDEPTH
    5053           0 :   const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
    5054             : #endif
    5055             : 
    5056             : #if CONFIG_HIGHBITDEPTH
    5057           0 :   check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
    5058             : #else
    5059             :   check_initial_width(cpi, subsampling_x, subsampling_y);
    5060             : #endif  // CONFIG_HIGHBITDEPTH
    5061             : 
    5062           0 :   aom_usec_timer_start(&timer);
    5063             : 
    5064           0 :   if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
    5065             : #if CONFIG_HIGHBITDEPTH
    5066             :                          use_highbitdepth,
    5067             : #endif  // CONFIG_HIGHBITDEPTH
    5068             :                          frame_flags))
    5069           0 :     res = -1;
    5070           0 :   aom_usec_timer_mark(&timer);
    5071           0 :   cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
    5072             : 
    5073           0 :   if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
    5074           0 :       (subsampling_x != 1 || subsampling_y != 1)) {
    5075           0 :     aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
    5076             :                        "Non-4:2:0 color format requires profile 1 or 3");
    5077           0 :     res = -1;
    5078             :   }
    5079           0 :   if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
    5080           0 :       (subsampling_x == 1 && subsampling_y == 1)) {
    5081           0 :     aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
    5082             :                        "4:2:0 color format requires profile 0 or 2");
    5083           0 :     res = -1;
    5084             :   }
    5085             : 
    5086           0 :   return res;
    5087             : }
    5088             : 
    5089           0 : static int frame_is_reference(const AV1_COMP *cpi) {
    5090           0 :   const AV1_COMMON *cm = &cpi->common;
    5091             : 
    5092           0 :   return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
    5093           0 :          cpi->refresh_golden_frame ||
    5094             : #if CONFIG_EXT_REFS
    5095           0 :          cpi->refresh_bwd_ref_frame ||
    5096             : #endif  // CONFIG_EXT_REFS
    5097           0 :          cpi->refresh_alt_ref_frame || !cm->error_resilient_mode ||
    5098           0 :          cm->lf.mode_ref_delta_update || cm->seg.update_map ||
    5099           0 :          cm->seg.update_data;
    5100             : }
    5101             : 
    5102           0 : static void adjust_frame_rate(AV1_COMP *cpi,
    5103             :                               const struct lookahead_entry *source) {
    5104             :   int64_t this_duration;
    5105           0 :   int step = 0;
    5106             : 
    5107           0 :   if (source->ts_start == cpi->first_time_stamp_ever) {
    5108           0 :     this_duration = source->ts_end - source->ts_start;
    5109           0 :     step = 1;
    5110             :   } else {
    5111           0 :     int64_t last_duration =
    5112           0 :         cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
    5113             : 
    5114           0 :     this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
    5115             : 
    5116             :     // do a step update if the duration changes by 10%
    5117           0 :     if (last_duration)
    5118           0 :       step = (int)((this_duration - last_duration) * 10 / last_duration);
    5119             :   }
    5120             : 
    5121           0 :   if (this_duration) {
    5122           0 :     if (step) {
    5123           0 :       av1_new_framerate(cpi, 10000000.0 / this_duration);
    5124             :     } else {
    5125             :       // Average this frame's rate into the last second's average
    5126             :       // frame rate. If we haven't seen 1 second yet, then average
    5127             :       // over the whole interval seen.
    5128           0 :       const double interval = AOMMIN(
    5129             :           (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
    5130           0 :       double avg_duration = 10000000.0 / cpi->framerate;
    5131           0 :       avg_duration *= (interval - avg_duration + this_duration);
    5132           0 :       avg_duration /= interval;
    5133             : 
    5134           0 :       av1_new_framerate(cpi, 10000000.0 / avg_duration);
    5135             :     }
    5136             :   }
    5137           0 :   cpi->last_time_stamp_seen = source->ts_start;
    5138           0 :   cpi->last_end_time_stamp_seen = source->ts_end;
    5139           0 : }
    5140             : 
    5141             : // Returns 0 if this is not an alt ref else the offset of the source frame
    5142             : // used as the arf midpoint.
    5143           0 : static int get_arf_src_index(AV1_COMP *cpi) {
    5144           0 :   RATE_CONTROL *const rc = &cpi->rc;
    5145           0 :   int arf_src_index = 0;
    5146           0 :   if (is_altref_enabled(cpi)) {
    5147           0 :     if (cpi->oxcf.pass == 2) {
    5148           0 :       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    5149           0 :       if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
    5150           0 :         arf_src_index = gf_group->arf_src_offset[gf_group->index];
    5151             :       }
    5152           0 :     } else if (rc->source_alt_ref_pending) {
    5153           0 :       arf_src_index = rc->frames_till_gf_update_due;
    5154             :     }
    5155             :   }
    5156           0 :   return arf_src_index;
    5157             : }
    5158             : 
    5159             : #if CONFIG_EXT_REFS
    5160           0 : static int get_brf_src_index(AV1_COMP *cpi) {
    5161           0 :   int brf_src_index = 0;
    5162           0 :   const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    5163             : 
    5164             :   // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
    5165             :   //               flag.
    5166           0 :   if (gf_group->bidir_pred_enabled[gf_group->index]) {
    5167           0 :     if (cpi->oxcf.pass == 2) {
    5168           0 :       if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
    5169           0 :         brf_src_index = gf_group->brf_src_offset[gf_group->index];
    5170             :     } else {
    5171             :       // TODO(zoeliu): To re-visit the setup for this scenario
    5172           0 :       brf_src_index = cpi->rc.bipred_group_interval - 1;
    5173             :     }
    5174             :   }
    5175             : 
    5176           0 :   return brf_src_index;
    5177             : }
    5178             : #endif  // CONFIG_EXT_REFS
    5179             : 
    5180           0 : static void check_src_altref(AV1_COMP *cpi,
    5181             :                              const struct lookahead_entry *source) {
    5182           0 :   RATE_CONTROL *const rc = &cpi->rc;
    5183             : 
    5184             :   // If pass == 2, the parameters set here will be reset in
    5185             :   // av1_rc_get_second_pass_params()
    5186             : 
    5187           0 :   if (cpi->oxcf.pass == 2) {
    5188           0 :     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    5189           0 :     rc->is_src_frame_alt_ref =
    5190             : #if CONFIG_EXT_REFS
    5191           0 :         (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
    5192             : #endif  // CONFIG_EXT_REFS
    5193           0 :         (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
    5194             :   } else {
    5195           0 :     rc->is_src_frame_alt_ref =
    5196           0 :         cpi->alt_ref_source && (source == cpi->alt_ref_source);
    5197             :   }
    5198             : 
    5199           0 :   if (rc->is_src_frame_alt_ref) {
    5200             :     // Current frame is an ARF overlay frame.
    5201           0 :     cpi->alt_ref_source = NULL;
    5202             : 
    5203             :     // Don't refresh the last buffer for an ARF overlay frame. It will
    5204             :     // become the GF so preserve last as an alternative prediction option.
    5205           0 :     cpi->refresh_last_frame = 0;
    5206             :   }
    5207           0 : }
    5208             : 
    5209             : #if CONFIG_INTERNAL_STATS
    5210             : extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
    5211             :                                  const unsigned char *img2, int img2_pitch,
    5212             :                                  int width, int height);
    5213             : 
    5214             : static void adjust_image_stat(double y, double u, double v, double all,
    5215             :                               ImageStat *s) {
    5216             :   s->stat[Y] += y;
    5217             :   s->stat[U] += u;
    5218             :   s->stat[V] += v;
    5219             :   s->stat[ALL] += all;
    5220             :   s->worst = AOMMIN(s->worst, all);
    5221             : }
    5222             : 
    5223             : static void compute_internal_stats(AV1_COMP *cpi) {
    5224             :   AV1_COMMON *const cm = &cpi->common;
    5225             :   double samples = 0.0;
    5226             :   uint32_t in_bit_depth = 8;
    5227             :   uint32_t bit_depth = 8;
    5228             : 
    5229             : #if CONFIG_HIGHBITDEPTH
    5230             :   if (cm->use_highbitdepth) {
    5231             :     in_bit_depth = cpi->oxcf.input_bit_depth;
    5232             :     bit_depth = cm->bit_depth;
    5233             :   }
    5234             : #endif
    5235             :   if (cm->show_frame) {
    5236             :     const YV12_BUFFER_CONFIG *orig = cpi->source;
    5237             :     const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
    5238             :     double y, u, v, frame_all;
    5239             : 
    5240             :     cpi->count++;
    5241             :     if (cpi->b_calculate_psnr) {
    5242             :       PSNR_STATS psnr;
    5243             :       double frame_ssim2 = 0.0, weight = 0.0;
    5244             :       aom_clear_system_state();
    5245             : // TODO(yaowu): unify these two versions into one.
    5246             : #if CONFIG_HIGHBITDEPTH
    5247             :       aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
    5248             : #else
    5249             :       aom_calc_psnr(orig, recon, &psnr);
    5250             : #endif  // CONFIG_HIGHBITDEPTH
    5251             : 
    5252             :       adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
    5253             :                         &cpi->psnr);
    5254             :       cpi->total_sq_error += psnr.sse[0];
    5255             :       cpi->total_samples += psnr.samples[0];
    5256             :       samples = psnr.samples[0];
    5257             : // TODO(yaowu): unify these two versions into one.
    5258             : #if CONFIG_HIGHBITDEPTH
    5259             :       if (cm->use_highbitdepth)
    5260             :         frame_ssim2 =
    5261             :             aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
    5262             :       else
    5263             :         frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
    5264             : #else
    5265             :       frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
    5266             : #endif  // CONFIG_HIGHBITDEPTH
    5267             : 
    5268             :       cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
    5269             :       cpi->summed_quality += frame_ssim2 * weight;
    5270             :       cpi->summed_weights += weight;
    5271             : 
    5272             : #if 0
    5273             :       {
    5274             :         FILE *f = fopen("q_used.stt", "a");
    5275             :         fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
    5276             :                 cpi->common.current_video_frame, y2, u2, v2,
    5277             :                 frame_psnr2, frame_ssim2);
    5278             :         fclose(f);
    5279             :       }
    5280             : #endif
    5281             :     }
    5282             :     if (cpi->b_calculate_blockiness) {
    5283             : #if CONFIG_HIGHBITDEPTH
    5284             :       if (!cm->use_highbitdepth)
    5285             : #endif
    5286             :       {
    5287             :         const double frame_blockiness =
    5288             :             av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
    5289             :                                recon->y_stride, orig->y_width, orig->y_height);
    5290             :         cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
    5291             :         cpi->total_blockiness += frame_blockiness;
    5292             :       }
    5293             : 
    5294             :       if (cpi->b_calculate_consistency) {
    5295             : #if CONFIG_HIGHBITDEPTH
    5296             :         if (!cm->use_highbitdepth)
    5297             : #endif
    5298             :         {
    5299             :           const double this_inconsistency = aom_get_ssim_metrics(
    5300             :               orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
    5301             :               orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
    5302             : 
    5303             :           const double peak = (double)((1 << in_bit_depth) - 1);
    5304             :           const double consistency =
    5305             :               aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
    5306             :           if (consistency > 0.0)
    5307             :             cpi->worst_consistency =
    5308             :                 AOMMIN(cpi->worst_consistency, consistency);
    5309             :           cpi->total_inconsistency += this_inconsistency;
    5310             :         }
    5311             :       }
    5312             :     }
    5313             : 
    5314             :     frame_all =
    5315             :         aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
    5316             :     adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
    5317             :     frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
    5318             :     adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
    5319             :   }
    5320             : }
    5321             : #endif  // CONFIG_INTERNAL_STATS
    5322             : 
    5323           0 : int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
    5324             :                             size_t *size, uint8_t *dest, int64_t *time_stamp,
    5325             :                             int64_t *time_end, int flush) {
    5326           0 :   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
    5327           0 :   AV1_COMMON *const cm = &cpi->common;
    5328           0 :   BufferPool *const pool = cm->buffer_pool;
    5329           0 :   RATE_CONTROL *const rc = &cpi->rc;
    5330             :   struct aom_usec_timer cmptimer;
    5331           0 :   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
    5332           0 :   struct lookahead_entry *last_source = NULL;
    5333           0 :   struct lookahead_entry *source = NULL;
    5334             :   int arf_src_index;
    5335             : #if CONFIG_EXT_REFS
    5336             :   int brf_src_index;
    5337             : #endif  // CONFIG_EXT_REFS
    5338             :   int i;
    5339             : 
    5340             : #if CONFIG_XIPHRC
    5341             :   cpi->od_rc.end_of_input = flush;
    5342             : #endif
    5343             : 
    5344             : #if CONFIG_BITSTREAM_DEBUG
    5345             :   assert(cpi->oxcf.max_threads == 0 &&
    5346             :          "bitstream debug tool does not support multithreading");
    5347             :   bitstream_queue_record_write();
    5348             :   bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
    5349             : #endif
    5350             : 
    5351           0 :   aom_usec_timer_start(&cmptimer);
    5352             : 
    5353           0 :   av1_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
    5354             : 
    5355             :   // Is multi-arf enabled.
    5356             :   // Note that at the moment multi_arf is only configured for 2 pass VBR
    5357           0 :   if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
    5358           0 :     cpi->multi_arf_allowed = 1;
    5359             :   else
    5360           0 :     cpi->multi_arf_allowed = 0;
    5361             : 
    5362             :   // Normal defaults
    5363           0 :   cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
    5364           0 :   cm->refresh_frame_context =
    5365           0 :       (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
    5366             :           ? REFRESH_FRAME_CONTEXT_FORWARD
    5367           0 :           : REFRESH_FRAME_CONTEXT_BACKWARD;
    5368             : 
    5369           0 :   cpi->refresh_last_frame = 1;
    5370           0 :   cpi->refresh_golden_frame = 0;
    5371             : #if CONFIG_EXT_REFS
    5372           0 :   cpi->refresh_bwd_ref_frame = 0;
    5373             : #endif  // CONFIG_EXT_REFS
    5374           0 :   cpi->refresh_alt_ref_frame = 0;
    5375             : 
    5376             : #if CONFIG_EXT_REFS && !CONFIG_XIPHRC
    5377           0 :   if (oxcf->pass == 2 && cm->show_existing_frame) {
    5378             :     // Manage the source buffer and flush out the source frame that has been
    5379             :     // coded already; Also get prepared for PSNR calculation if needed.
    5380           0 :     if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
    5381           0 :       *size = 0;
    5382           0 :       return -1;
    5383             :     }
    5384           0 :     cpi->source = &source->img;
    5385             :     // TODO(zoeliu): To track down to determine whether it's needed to adjust
    5386             :     // the frame rate.
    5387           0 :     *time_stamp = source->ts_start;
    5388           0 :     *time_end = source->ts_end;
    5389             : 
    5390             :     // We need to adjust frame rate for an overlay frame
    5391           0 :     if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
    5392             : 
    5393             :     // Find a free buffer for the new frame, releasing the reference previously
    5394             :     // held.
    5395           0 :     if (cm->new_fb_idx != INVALID_IDX) {
    5396           0 :       --pool->frame_bufs[cm->new_fb_idx].ref_count;
    5397             :     }
    5398           0 :     cm->new_fb_idx = get_free_fb(cm);
    5399             : 
    5400           0 :     if (cm->new_fb_idx == INVALID_IDX) return -1;
    5401             : 
    5402             :     // Clear down mmx registers
    5403           0 :     aom_clear_system_state();
    5404             : 
    5405             :     // Start with a 0 size frame.
    5406           0 :     *size = 0;
    5407             : 
    5408             :     // We need to update the gf_group for show_existing overlay frame
    5409           0 :     if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
    5410             : 
    5411           0 :     Pass2Encode(cpi, size, dest, frame_flags);
    5412             : 
    5413           0 :     if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
    5414             : 
    5415             : #if CONFIG_INTERNAL_STATS
    5416             :     compute_internal_stats(cpi);
    5417             :     cpi->bytes += (int)(*size);
    5418             : #endif  // CONFIG_INTERNAL_STATS
    5419             : 
    5420             :     // Clear down mmx registers
    5421           0 :     aom_clear_system_state();
    5422             : 
    5423           0 :     cm->show_existing_frame = 0;
    5424           0 :     return 0;
    5425             :   }
    5426             : #endif  // CONFIG_EXT_REFS && !CONFIG_XIPHRC
    5427             : 
    5428             :   // Should we encode an arf frame.
    5429           0 :   arf_src_index = get_arf_src_index(cpi);
    5430           0 :   if (arf_src_index) {
    5431           0 :     for (i = 0; i <= arf_src_index; ++i) {
    5432           0 :       struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
    5433             :       // Avoid creating an alt-ref if there's a forced keyframe pending.
    5434           0 :       if (e == NULL) {
    5435           0 :         break;
    5436           0 :       } else if (e->flags == AOM_EFLAG_FORCE_KF) {
    5437           0 :         arf_src_index = 0;
    5438           0 :         flush = 1;
    5439           0 :         break;
    5440             :       }
    5441             :     }
    5442             :   }
    5443             : 
    5444           0 :   if (arf_src_index) {
    5445           0 :     assert(arf_src_index <= rc->frames_to_key);
    5446             : 
    5447           0 :     if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
    5448           0 :       cpi->alt_ref_source = source;
    5449             : 
    5450           0 :       if (oxcf->arnr_max_frames > 0) {
    5451             :         // Produce the filtered ARF frame.
    5452           0 :         av1_temporal_filter(cpi, arf_src_index);
    5453           0 :         aom_extend_frame_borders(&cpi->alt_ref_buffer);
    5454           0 :         force_src_buffer = &cpi->alt_ref_buffer;
    5455             :       }
    5456             : 
    5457           0 :       cm->show_frame = 0;
    5458           0 :       cm->intra_only = 0;
    5459           0 :       cpi->refresh_alt_ref_frame = 1;
    5460           0 :       cpi->refresh_golden_frame = 0;
    5461           0 :       cpi->refresh_last_frame = 0;
    5462           0 :       rc->is_src_frame_alt_ref = 0;
    5463             :     }
    5464           0 :     rc->source_alt_ref_pending = 0;
    5465             :   }
    5466             : 
    5467             : #if CONFIG_EXT_REFS
    5468           0 :   rc->is_bwd_ref_frame = 0;
    5469           0 :   brf_src_index = get_brf_src_index(cpi);
    5470           0 :   if (brf_src_index) {
    5471           0 :     assert(brf_src_index <= rc->frames_to_key);
    5472           0 :     if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
    5473           0 :       cm->show_frame = 0;
    5474           0 :       cm->intra_only = 0;
    5475             : 
    5476           0 :       cpi->refresh_bwd_ref_frame = 1;
    5477           0 :       cpi->refresh_last_frame = 0;
    5478           0 :       cpi->refresh_golden_frame = 0;
    5479           0 :       cpi->refresh_alt_ref_frame = 0;
    5480             : 
    5481           0 :       rc->is_bwd_ref_frame = 1;
    5482             :     }
    5483             :   }
    5484             : #endif  // CONFIG_EXT_REFS
    5485             : 
    5486           0 :   if (!source) {
    5487             :     // Get last frame source.
    5488           0 :     if (cm->current_video_frame > 0) {
    5489           0 :       if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
    5490           0 :         return -1;
    5491             :     }
    5492             : 
    5493             :     // Read in the source frame.
    5494           0 :     source = av1_lookahead_pop(cpi->lookahead, flush);
    5495             : 
    5496           0 :     if (source != NULL) {
    5497           0 :       cm->show_frame = 1;
    5498           0 :       cm->intra_only = 0;
    5499             : 
    5500             :       // Check to see if the frame should be encoded as an arf overlay.
    5501           0 :       check_src_altref(cpi, source);
    5502             :     }
    5503             :   }
    5504             : 
    5505           0 :   if (source) {
    5506           0 :     cpi->un_scaled_source = cpi->source =
    5507           0 :         force_src_buffer ? force_src_buffer : &source->img;
    5508             : 
    5509           0 :     cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
    5510             : 
    5511           0 :     *time_stamp = source->ts_start;
    5512           0 :     *time_end = source->ts_end;
    5513           0 :     *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
    5514             : 
    5515             :   } else {
    5516           0 :     *size = 0;
    5517           0 :     if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
    5518             : #if CONFIG_XIPHRC
    5519             :       od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 1);
    5520             : #else
    5521           0 :       av1_end_first_pass(cpi); /* get last stats packet */
    5522             : #endif
    5523           0 :       cpi->twopass.first_pass_done = 1;
    5524             :     }
    5525           0 :     return -1;
    5526             :   }
    5527             : 
    5528           0 :   if (source->ts_start < cpi->first_time_stamp_ever) {
    5529           0 :     cpi->first_time_stamp_ever = source->ts_start;
    5530           0 :     cpi->last_end_time_stamp_seen = source->ts_start;
    5531             :   }
    5532             : 
    5533             :   // Clear down mmx registers
    5534           0 :   aom_clear_system_state();
    5535             : 
    5536             :   // adjust frame rates based on timestamps given
    5537           0 :   if (cm->show_frame) adjust_frame_rate(cpi, source);
    5538             : 
    5539             :   // Find a free buffer for the new frame, releasing the reference previously
    5540             :   // held.
    5541           0 :   if (cm->new_fb_idx != INVALID_IDX) {
    5542           0 :     --pool->frame_bufs[cm->new_fb_idx].ref_count;
    5543             :   }
    5544           0 :   cm->new_fb_idx = get_free_fb(cm);
    5545             : 
    5546           0 :   if (cm->new_fb_idx == INVALID_IDX) return -1;
    5547             : 
    5548           0 :   cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
    5549             : 
    5550             : #if CONFIG_EXT_REFS
    5551           0 :   if (oxcf->pass == 2) {
    5552           0 :     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    5553           0 :     cpi->alt_fb_idx = cpi->arf_map[gf_group->arf_ref_idx[gf_group->index]];
    5554             :   }
    5555             : #else
    5556             :   if (cpi->multi_arf_allowed) {
    5557             :     if (cm->frame_type == KEY_FRAME) {
    5558             :       init_buffer_indices(cpi);
    5559             :     } else if (oxcf->pass == 2) {
    5560             :       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    5561             :       cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
    5562             :     }
    5563             :   }
    5564             : #endif  // CONFIG_EXT_REFS
    5565             : 
    5566             :   // Start with a 0 size frame.
    5567           0 :   *size = 0;
    5568             : 
    5569           0 :   cpi->frame_flags = *frame_flags;
    5570             : 
    5571           0 :   if (oxcf->pass == 2) {
    5572             : #if CONFIG_XIPHRC
    5573             :     if (od_enc_rc_2pass_in(&cpi->od_rc) < 0) return -1;
    5574             :   }
    5575             : #else
    5576           0 :     av1_rc_get_second_pass_params(cpi);
    5577           0 :   } else if (oxcf->pass == 1) {
    5578           0 :     setup_frame_size(cpi);
    5579           0 :     av1_resize_step(cpi);
    5580             :   }
    5581             : #endif
    5582             : 
    5583           0 :   if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
    5584           0 :     for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i)
    5585           0 :       cpi->scaled_ref_idx[i] = INVALID_IDX;
    5586             :   }
    5587             : 
    5588             : #if CONFIG_AOM_QM
    5589             :   cm->using_qmatrix = cpi->oxcf.using_qm;
    5590             :   cm->min_qmlevel = cpi->oxcf.qm_minlevel;
    5591             :   cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
    5592             : #endif
    5593             : 
    5594             : #if CONFIG_REFERENCE_BUFFER
    5595           0 :   if (*time_stamp == 0) {
    5596           0 :     cpi->common.current_frame_id = -1;
    5597             :   }
    5598             : #endif
    5599             : 
    5600             : #if CONFIG_XIPHRC
    5601             :   if (oxcf->pass == 1) {
    5602             :     size_t tmp;
    5603             :     if (cpi->od_rc.cur_frame == 0) Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
    5604             :     cpi->od_rc.firstpass_quant = cpi->od_rc.target_quantizer;
    5605             :     Pass0Encode(cpi, &tmp, dest, 0, frame_flags);
    5606             :     od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 0);
    5607             :   } else if (oxcf->pass == 2) {
    5608             :     Pass0Encode(cpi, size, dest, 0, frame_flags);
    5609             :   } else {
    5610             :     if (cpi->od_rc.cur_frame == 0) {
    5611             :       size_t tmp;
    5612             :       Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
    5613             :     }
    5614             :     Pass0Encode(cpi, size, dest, 0, frame_flags);
    5615             :   }
    5616             : #else
    5617           0 :   if (oxcf->pass == 1) {
    5618           0 :     cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
    5619           0 :     av1_first_pass(cpi, source);
    5620           0 :   } else if (oxcf->pass == 2) {
    5621           0 :     Pass2Encode(cpi, size, dest, frame_flags);
    5622             :   } else {
    5623             :     // One pass encode
    5624           0 :     Pass0Encode(cpi, size, dest, 0, frame_flags);
    5625             :   }
    5626             : #endif
    5627             : 
    5628           0 :   if (!cm->error_resilient_mode)
    5629           0 :     cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
    5630             : 
    5631             :   // No frame encoded, or frame was dropped, release scaled references.
    5632           0 :   if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
    5633           0 :     release_scaled_references(cpi);
    5634             :   }
    5635             : 
    5636           0 :   if (*size > 0) {
    5637           0 :     cpi->droppable = !frame_is_reference(cpi);
    5638             :   }
    5639             : 
    5640           0 :   aom_usec_timer_mark(&cmptimer);
    5641           0 :   cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
    5642             : 
    5643           0 :   if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
    5644           0 :     generate_psnr_packet(cpi);
    5645             : 
    5646             : #if CONFIG_INTERNAL_STATS
    5647             :   if (oxcf->pass != 1) {
    5648             :     compute_internal_stats(cpi);
    5649             :     cpi->bytes += (int)(*size);
    5650             :   }
    5651             : #endif  // CONFIG_INTERNAL_STATS
    5652             : 
    5653             : #if CONFIG_XIPHRC
    5654             :   cpi->od_rc.cur_frame++;
    5655             : #endif
    5656             : 
    5657           0 :   aom_clear_system_state();
    5658             : 
    5659           0 :   return 0;
    5660             : }
    5661             : 
    5662           0 : int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
    5663           0 :   AV1_COMMON *cm = &cpi->common;
    5664           0 :   if (!cm->show_frame) {
    5665           0 :     return -1;
    5666             :   } else {
    5667             :     int ret;
    5668           0 :     if (cm->frame_to_show) {
    5669           0 :       *dest = *cm->frame_to_show;
    5670           0 :       dest->y_width = cm->width;
    5671           0 :       dest->y_height = cm->height;
    5672           0 :       dest->uv_width = cm->width >> cm->subsampling_x;
    5673           0 :       dest->uv_height = cm->height >> cm->subsampling_y;
    5674           0 :       ret = 0;
    5675             :     } else {
    5676           0 :       ret = -1;
    5677             :     }
    5678           0 :     aom_clear_system_state();
    5679           0 :     return ret;
    5680             :   }
    5681             : }
    5682             : 
    5683           0 : int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
    5684           0 :   if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
    5685             : 
    5686           0 :   *frame =
    5687           0 :       cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
    5688           0 :   return 0;
    5689             : }
    5690             : 
    5691           0 : int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
    5692             :                           AOM_SCALING vert_mode) {
    5693           0 :   AV1_COMMON *cm = &cpi->common;
    5694           0 :   int hr = 0, hs = 0, vr = 0, vs = 0;
    5695             : 
    5696           0 :   if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
    5697             : 
    5698           0 :   Scale2Ratio(horiz_mode, &hr, &hs);
    5699           0 :   Scale2Ratio(vert_mode, &vr, &vs);
    5700             : 
    5701             :   // always go to the next whole number
    5702           0 :   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
    5703           0 :   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
    5704           0 :   assert(cm->width <= cpi->initial_width);
    5705           0 :   assert(cm->height <= cpi->initial_height);
    5706             : 
    5707           0 :   update_frame_size(cpi);
    5708             : 
    5709           0 :   return 0;
    5710             : }
    5711             : 
    5712           0 : int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
    5713           0 :   AV1_COMMON *cm = &cpi->common;
    5714             : #if CONFIG_HIGHBITDEPTH
    5715           0 :   check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
    5716             : #else
    5717             :   check_initial_width(cpi, 1, 1);
    5718             : #endif  // CONFIG_HIGHBITDEPTH
    5719             : 
    5720           0 :   if (width <= 0 || height <= 0) return 1;
    5721             : 
    5722           0 :   cm->width = width;
    5723           0 :   if (cm->width > cpi->initial_width) {
    5724           0 :     cm->width = cpi->initial_width;
    5725           0 :     printf("Warning: Desired width too large, changed to %d\n", cm->width);
    5726             :   }
    5727             : 
    5728           0 :   cm->height = height;
    5729           0 :   if (cm->height > cpi->initial_height) {
    5730           0 :     cm->height = cpi->initial_height;
    5731           0 :     printf("Warning: Desired height too large, changed to %d\n", cm->height);
    5732             :   }
    5733             : 
    5734           0 :   assert(cm->width <= cpi->initial_width);
    5735           0 :   assert(cm->height <= cpi->initial_height);
    5736             : 
    5737           0 :   update_frame_size(cpi);
    5738             : 
    5739           0 :   return 0;
    5740             : }
    5741             : 
    5742           0 : int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
    5743             : 
    5744           0 : void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
    5745           0 :   if (flags &
    5746             :       (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF)) {
    5747           0 :     int ref = AOM_REFFRAME_ALL;
    5748             : 
    5749           0 :     if (flags & AOM_EFLAG_NO_REF_LAST) {
    5750           0 :       ref ^= AOM_LAST_FLAG;
    5751             : #if CONFIG_EXT_REFS
    5752           0 :       ref ^= AOM_LAST2_FLAG;
    5753           0 :       ref ^= AOM_LAST3_FLAG;
    5754             : #endif  // CONFIG_EXT_REFS
    5755             :     }
    5756             : 
    5757           0 :     if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
    5758             : 
    5759           0 :     if (flags & AOM_EFLAG_NO_REF_ARF) ref ^= AOM_ALT_FLAG;
    5760             : 
    5761           0 :     av1_use_as_reference(cpi, ref);
    5762             :   }
    5763             : 
    5764           0 :   if (flags &
    5765             :       (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
    5766             :        AOM_EFLAG_FORCE_GF | AOM_EFLAG_FORCE_ARF)) {
    5767           0 :     int upd = AOM_REFFRAME_ALL;
    5768             : 
    5769           0 :     if (flags & AOM_EFLAG_NO_UPD_LAST) {
    5770           0 :       upd ^= AOM_LAST_FLAG;
    5771             : #if CONFIG_EXT_REFS
    5772           0 :       upd ^= AOM_LAST2_FLAG;
    5773           0 :       upd ^= AOM_LAST3_FLAG;
    5774             : #endif  // CONFIG_EXT_REFS
    5775             :     }
    5776             : 
    5777           0 :     if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
    5778             : 
    5779           0 :     if (flags & AOM_EFLAG_NO_UPD_ARF) upd ^= AOM_ALT_FLAG;
    5780             : 
    5781           0 :     av1_update_reference(cpi, upd);
    5782             :   }
    5783             : 
    5784           0 :   if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
    5785           0 :     av1_update_entropy(cpi, 0);
    5786             :   }
    5787           0 : }

Generated by: LCOV version 1.13