Home | History | Annotate | Download | only in encoder
      1 /*
      2  * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include <math.h>
     12 #include <stdio.h>
     13 #include <limits.h>
     14 
     15 #include "./vp9_rtcd.h"
     16 #include "./vpx_config.h"
     17 #include "./vpx_dsp_rtcd.h"
     18 #include "./vpx_scale_rtcd.h"
     19 #include "vpx_dsp/psnr.h"
     20 #include "vpx_dsp/vpx_dsp_common.h"
     21 #include "vpx_dsp/vpx_filter.h"
     22 #if CONFIG_INTERNAL_STATS
     23 #include "vpx_dsp/ssim.h"
     24 #endif
     25 #include "vpx_ports/mem.h"
     26 #include "vpx_ports/system_state.h"
     27 #include "vpx_ports/vpx_timer.h"
     28 
     29 #include "vp9/common/vp9_alloccommon.h"
     30 #include "vp9/common/vp9_filter.h"
     31 #include "vp9/common/vp9_idct.h"
     32 #if CONFIG_VP9_POSTPROC
     33 #include "vp9/common/vp9_postproc.h"
     34 #endif
     35 #include "vp9/common/vp9_reconinter.h"
     36 #include "vp9/common/vp9_reconintra.h"
     37 #include "vp9/common/vp9_tile_common.h"
     38 
     39 #include "vp9/encoder/vp9_alt_ref_aq.h"
     40 #include "vp9/encoder/vp9_aq_360.h"
     41 #include "vp9/encoder/vp9_aq_complexity.h"
     42 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
     43 #include "vp9/encoder/vp9_aq_variance.h"
     44 #include "vp9/encoder/vp9_bitstream.h"
     45 #include "vp9/encoder/vp9_context_tree.h"
     46 #include "vp9/encoder/vp9_encodeframe.h"
     47 #include "vp9/encoder/vp9_encodemv.h"
     48 #include "vp9/encoder/vp9_encoder.h"
     49 #include "vp9/encoder/vp9_extend.h"
     50 #include "vp9/encoder/vp9_ethread.h"
     51 #include "vp9/encoder/vp9_firstpass.h"
     52 #include "vp9/encoder/vp9_mbgraph.h"
     53 #include "vp9/encoder/vp9_multi_thread.h"
     54 #include "vp9/encoder/vp9_noise_estimate.h"
     55 #include "vp9/encoder/vp9_picklpf.h"
     56 #include "vp9/encoder/vp9_ratectrl.h"
     57 #include "vp9/encoder/vp9_rd.h"
     58 #include "vp9/encoder/vp9_resize.h"
     59 #include "vp9/encoder/vp9_segmentation.h"
     60 #include "vp9/encoder/vp9_skin_detection.h"
     61 #include "vp9/encoder/vp9_speed_features.h"
     62 #include "vp9/encoder/vp9_svc_layercontext.h"
     63 #include "vp9/encoder/vp9_temporal_filter.h"
     64 
     65 #define AM_SEGMENT_ID_INACTIVE 7
     66 #define AM_SEGMENT_ID_ACTIVE 0
     67 
     68 #define ALTREF_HIGH_PRECISION_MV 1     // Whether to use high precision mv
     69                                        //  for altref computation.
     70 #define HIGH_PRECISION_MV_QTHRESH 200  // Q threshold for high precision
     71                                        // mv. Choose a very high value for
     72                                        // now so that HIGH_PRECISION is always
     73                                        // chosen.
     74 
     75 #define FRAME_SIZE_FACTOR 128  // empirical params for context model threshold
     76 #define FRAME_RATE_FACTOR 8
     77 
     78 #ifdef OUTPUT_YUV_DENOISED
     79 FILE *yuv_denoised_file = NULL;
     80 #endif
     81 #ifdef OUTPUT_YUV_SKINMAP
     82 static FILE *yuv_skinmap_file = NULL;
     83 #endif
     84 #ifdef OUTPUT_YUV_REC
     85 FILE *yuv_rec_file;
     86 #endif
     87 
     88 #if 0
     89 FILE *framepsnr;
     90 FILE *kf_list;
     91 FILE *keyfile;
     92 #endif
     93 
     94 #ifdef ENABLE_KF_DENOISE
     95 // Test condition for spatial denoise of source.
     96 static int is_spatial_denoise_enabled(VP9_COMP *cpi) {
     97   VP9_COMMON *const cm = &cpi->common;
     98   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
     99 
    100   return (oxcf->pass != 1) && !is_lossless_requested(&cpi->oxcf) &&
    101          frame_is_intra_only(cm);
    102 }
    103 #endif
    104 
    105 // compute adaptive threshold for skip recoding
    106 static int compute_context_model_thresh(const VP9_COMP *const cpi) {
    107   const VP9_COMMON *const cm = &cpi->common;
    108   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    109   const int frame_size = (cm->width * cm->height) >> 10;
    110   const int bitrate = (int)(oxcf->target_bandwidth >> 10);
    111   const int qindex_factor = cm->base_qindex + (MAXQ >> 1);
    112 
    113   // This equation makes the threshold adaptive to frame size.
    114   // Coding gain obtained by recoding comes from alternate frames of large
    115   // content change. We skip recoding if the difference of previous and current
    116   // frame context probability model is less than a certain threshold.
    117   // The first component is the most critical part to guarantee adaptivity.
    118   // Other parameters are estimated based on normal setting of hd resolution
    119   // parameters. e.g frame_size = 1920x1080, bitrate = 8000, qindex_factor < 50
    120   const int thresh =
    121       ((FRAME_SIZE_FACTOR * frame_size - FRAME_RATE_FACTOR * bitrate) *
    122        qindex_factor) >>
    123       9;
    124 
    125   return thresh;
    126 }
    127 
    128 // compute the total cost difference between current
    129 // and previous frame context prob model.
    130 static int compute_context_model_diff(const VP9_COMMON *const cm) {
    131   const FRAME_CONTEXT *const pre_fc =
    132       &cm->frame_contexts[cm->frame_context_idx];
    133   const FRAME_CONTEXT *const cur_fc = cm->fc;
    134   const FRAME_COUNTS *counts = &cm->counts;
    135   vpx_prob pre_last_prob, cur_last_prob;
    136   int diff = 0;
    137   int i, j, k, l, m, n;
    138 
    139   // y_mode_prob
    140   for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
    141     for (j = 0; j < INTRA_MODES - 1; ++j) {
    142       diff += (int)counts->y_mode[i][j] *
    143               (pre_fc->y_mode_prob[i][j] - cur_fc->y_mode_prob[i][j]);
    144     }
    145     pre_last_prob = MAX_PROB - pre_fc->y_mode_prob[i][INTRA_MODES - 2];
    146     cur_last_prob = MAX_PROB - cur_fc->y_mode_prob[i][INTRA_MODES - 2];
    147 
    148     diff += (int)counts->y_mode[i][INTRA_MODES - 1] *
    149             (pre_last_prob - cur_last_prob);
    150   }
    151 
    152   // uv_mode_prob
    153   for (i = 0; i < INTRA_MODES; ++i) {
    154     for (j = 0; j < INTRA_MODES - 1; ++j) {
    155       diff += (int)counts->uv_mode[i][j] *
    156               (pre_fc->uv_mode_prob[i][j] - cur_fc->uv_mode_prob[i][j]);
    157     }
    158     pre_last_prob = MAX_PROB - pre_fc->uv_mode_prob[i][INTRA_MODES - 2];
    159     cur_last_prob = MAX_PROB - cur_fc->uv_mode_prob[i][INTRA_MODES - 2];
    160 
    161     diff += (int)counts->uv_mode[i][INTRA_MODES - 1] *
    162             (pre_last_prob - cur_last_prob);
    163   }
    164 
    165   // partition_prob
    166   for (i = 0; i < PARTITION_CONTEXTS; ++i) {
    167     for (j = 0; j < PARTITION_TYPES - 1; ++j) {
    168       diff += (int)counts->partition[i][j] *
    169               (pre_fc->partition_prob[i][j] - cur_fc->partition_prob[i][j]);
    170     }
    171     pre_last_prob = MAX_PROB - pre_fc->partition_prob[i][PARTITION_TYPES - 2];
    172     cur_last_prob = MAX_PROB - cur_fc->partition_prob[i][PARTITION_TYPES - 2];
    173 
    174     diff += (int)counts->partition[i][PARTITION_TYPES - 1] *
    175             (pre_last_prob - cur_last_prob);
    176   }
    177 
    178   // coef_probs
    179   for (i = 0; i < TX_SIZES; ++i) {
    180     for (j = 0; j < PLANE_TYPES; ++j) {
    181       for (k = 0; k < REF_TYPES; ++k) {
    182         for (l = 0; l < COEF_BANDS; ++l) {
    183           for (m = 0; m < BAND_COEFF_CONTEXTS(l); ++m) {
    184             for (n = 0; n < UNCONSTRAINED_NODES; ++n) {
    185               diff += (int)counts->coef[i][j][k][l][m][n] *
    186                       (pre_fc->coef_probs[i][j][k][l][m][n] -
    187                        cur_fc->coef_probs[i][j][k][l][m][n]);
    188             }
    189 
    190             pre_last_prob =
    191                 MAX_PROB -
    192                 pre_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
    193             cur_last_prob =
    194                 MAX_PROB -
    195                 cur_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
    196 
    197             diff += (int)counts->coef[i][j][k][l][m][UNCONSTRAINED_NODES] *
    198                     (pre_last_prob - cur_last_prob);
    199           }
    200         }
    201       }
    202     }
    203   }
    204 
    205   // switchable_interp_prob
    206   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) {
    207     for (j = 0; j < SWITCHABLE_FILTERS - 1; ++j) {
    208       diff += (int)counts->switchable_interp[i][j] *
    209               (pre_fc->switchable_interp_prob[i][j] -
    210                cur_fc->switchable_interp_prob[i][j]);
    211     }
    212     pre_last_prob =
    213         MAX_PROB - pre_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
    214     cur_last_prob =
    215         MAX_PROB - cur_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
    216 
    217     diff += (int)counts->switchable_interp[i][SWITCHABLE_FILTERS - 1] *
    218             (pre_last_prob - cur_last_prob);
    219   }
    220 
    221   // inter_mode_probs
    222   for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
    223     for (j = 0; j < INTER_MODES - 1; ++j) {
    224       diff += (int)counts->inter_mode[i][j] *
    225               (pre_fc->inter_mode_probs[i][j] - cur_fc->inter_mode_probs[i][j]);
    226     }
    227     pre_last_prob = MAX_PROB - pre_fc->inter_mode_probs[i][INTER_MODES - 2];
    228     cur_last_prob = MAX_PROB - cur_fc->inter_mode_probs[i][INTER_MODES - 2];
    229 
    230     diff += (int)counts->inter_mode[i][INTER_MODES - 1] *
    231             (pre_last_prob - cur_last_prob);
    232   }
    233 
    234   // intra_inter_prob
    235   for (i = 0; i < INTRA_INTER_CONTEXTS; ++i) {
    236     diff += (int)counts->intra_inter[i][0] *
    237             (pre_fc->intra_inter_prob[i] - cur_fc->intra_inter_prob[i]);
    238 
    239     pre_last_prob = MAX_PROB - pre_fc->intra_inter_prob[i];
    240     cur_last_prob = MAX_PROB - cur_fc->intra_inter_prob[i];
    241 
    242     diff += (int)counts->intra_inter[i][1] * (pre_last_prob - cur_last_prob);
    243   }
    244 
    245   // comp_inter_prob
    246   for (i = 0; i < COMP_INTER_CONTEXTS; ++i) {
    247     diff += (int)counts->comp_inter[i][0] *
    248             (pre_fc->comp_inter_prob[i] - cur_fc->comp_inter_prob[i]);
    249 
    250     pre_last_prob = MAX_PROB - pre_fc->comp_inter_prob[i];
    251     cur_last_prob = MAX_PROB - cur_fc->comp_inter_prob[i];
    252 
    253     diff += (int)counts->comp_inter[i][1] * (pre_last_prob - cur_last_prob);
    254   }
    255 
    256   // single_ref_prob
    257   for (i = 0; i < REF_CONTEXTS; ++i) {
    258     for (j = 0; j < 2; ++j) {
    259       diff += (int)counts->single_ref[i][j][0] *
    260               (pre_fc->single_ref_prob[i][j] - cur_fc->single_ref_prob[i][j]);
    261 
    262       pre_last_prob = MAX_PROB - pre_fc->single_ref_prob[i][j];
    263       cur_last_prob = MAX_PROB - cur_fc->single_ref_prob[i][j];
    264 
    265       diff +=
    266           (int)counts->single_ref[i][j][1] * (pre_last_prob - cur_last_prob);
    267     }
    268   }
    269 
    270   // comp_ref_prob
    271   for (i = 0; i < REF_CONTEXTS; ++i) {
    272     diff += (int)counts->comp_ref[i][0] *
    273             (pre_fc->comp_ref_prob[i] - cur_fc->comp_ref_prob[i]);
    274 
    275     pre_last_prob = MAX_PROB - pre_fc->comp_ref_prob[i];
    276     cur_last_prob = MAX_PROB - cur_fc->comp_ref_prob[i];
    277 
    278     diff += (int)counts->comp_ref[i][1] * (pre_last_prob - cur_last_prob);
    279   }
    280 
    281   // tx_probs
    282   for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
    283     // p32x32
    284     for (j = 0; j < TX_SIZES - 1; ++j) {
    285       diff += (int)counts->tx.p32x32[i][j] *
    286               (pre_fc->tx_probs.p32x32[i][j] - cur_fc->tx_probs.p32x32[i][j]);
    287     }
    288     pre_last_prob = MAX_PROB - pre_fc->tx_probs.p32x32[i][TX_SIZES - 2];
    289     cur_last_prob = MAX_PROB - cur_fc->tx_probs.p32x32[i][TX_SIZES - 2];
    290 
    291     diff += (int)counts->tx.p32x32[i][TX_SIZES - 1] *
    292             (pre_last_prob - cur_last_prob);
    293 
    294     // p16x16
    295     for (j = 0; j < TX_SIZES - 2; ++j) {
    296       diff += (int)counts->tx.p16x16[i][j] *
    297               (pre_fc->tx_probs.p16x16[i][j] - cur_fc->tx_probs.p16x16[i][j]);
    298     }
    299     pre_last_prob = MAX_PROB - pre_fc->tx_probs.p16x16[i][TX_SIZES - 3];
    300     cur_last_prob = MAX_PROB - cur_fc->tx_probs.p16x16[i][TX_SIZES - 3];
    301 
    302     diff += (int)counts->tx.p16x16[i][TX_SIZES - 2] *
    303             (pre_last_prob - cur_last_prob);
    304 
    305     // p8x8
    306     for (j = 0; j < TX_SIZES - 3; ++j) {
    307       diff += (int)counts->tx.p8x8[i][j] *
    308               (pre_fc->tx_probs.p8x8[i][j] - cur_fc->tx_probs.p8x8[i][j]);
    309     }
    310     pre_last_prob = MAX_PROB - pre_fc->tx_probs.p8x8[i][TX_SIZES - 4];
    311     cur_last_prob = MAX_PROB - cur_fc->tx_probs.p8x8[i][TX_SIZES - 4];
    312 
    313     diff +=
    314         (int)counts->tx.p8x8[i][TX_SIZES - 3] * (pre_last_prob - cur_last_prob);
    315   }
    316 
    317   // skip_probs
    318   for (i = 0; i < SKIP_CONTEXTS; ++i) {
    319     diff += (int)counts->skip[i][0] *
    320             (pre_fc->skip_probs[i] - cur_fc->skip_probs[i]);
    321 
    322     pre_last_prob = MAX_PROB - pre_fc->skip_probs[i];
    323     cur_last_prob = MAX_PROB - cur_fc->skip_probs[i];
    324 
    325     diff += (int)counts->skip[i][1] * (pre_last_prob - cur_last_prob);
    326   }
    327 
    328   // mv
    329   for (i = 0; i < MV_JOINTS - 1; ++i) {
    330     diff += (int)counts->mv.joints[i] *
    331             (pre_fc->nmvc.joints[i] - cur_fc->nmvc.joints[i]);
    332   }
    333   pre_last_prob = MAX_PROB - pre_fc->nmvc.joints[MV_JOINTS - 2];
    334   cur_last_prob = MAX_PROB - cur_fc->nmvc.joints[MV_JOINTS - 2];
    335 
    336   diff +=
    337       (int)counts->mv.joints[MV_JOINTS - 1] * (pre_last_prob - cur_last_prob);
    338 
    339   for (i = 0; i < 2; ++i) {
    340     const nmv_component_counts *nmv_count = &counts->mv.comps[i];
    341     const nmv_component *pre_nmv_prob = &pre_fc->nmvc.comps[i];
    342     const nmv_component *cur_nmv_prob = &cur_fc->nmvc.comps[i];
    343 
    344     // sign
    345     diff += (int)nmv_count->sign[0] * (pre_nmv_prob->sign - cur_nmv_prob->sign);
    346 
    347     pre_last_prob = MAX_PROB - pre_nmv_prob->sign;
    348     cur_last_prob = MAX_PROB - cur_nmv_prob->sign;
    349 
    350     diff += (int)nmv_count->sign[1] * (pre_last_prob - cur_last_prob);
    351 
    352     // classes
    353     for (j = 0; j < MV_CLASSES - 1; ++j) {
    354       diff += (int)nmv_count->classes[j] *
    355               (pre_nmv_prob->classes[j] - cur_nmv_prob->classes[j]);
    356     }
    357     pre_last_prob = MAX_PROB - pre_nmv_prob->classes[MV_CLASSES - 2];
    358     cur_last_prob = MAX_PROB - cur_nmv_prob->classes[MV_CLASSES - 2];
    359 
    360     diff += (int)nmv_count->classes[MV_CLASSES - 1] *
    361             (pre_last_prob - cur_last_prob);
    362 
    363     // class0
    364     for (j = 0; j < CLASS0_SIZE - 1; ++j) {
    365       diff += (int)nmv_count->class0[j] *
    366               (pre_nmv_prob->class0[j] - cur_nmv_prob->class0[j]);
    367     }
    368     pre_last_prob = MAX_PROB - pre_nmv_prob->class0[CLASS0_SIZE - 2];
    369     cur_last_prob = MAX_PROB - cur_nmv_prob->class0[CLASS0_SIZE - 2];
    370 
    371     diff += (int)nmv_count->class0[CLASS0_SIZE - 1] *
    372             (pre_last_prob - cur_last_prob);
    373 
    374     // bits
    375     for (j = 0; j < MV_OFFSET_BITS; ++j) {
    376       diff += (int)nmv_count->bits[j][0] *
    377               (pre_nmv_prob->bits[j] - cur_nmv_prob->bits[j]);
    378 
    379       pre_last_prob = MAX_PROB - pre_nmv_prob->bits[j];
    380       cur_last_prob = MAX_PROB - cur_nmv_prob->bits[j];
    381 
    382       diff += (int)nmv_count->bits[j][1] * (pre_last_prob - cur_last_prob);
    383     }
    384 
    385     // class0_fp
    386     for (j = 0; j < CLASS0_SIZE; ++j) {
    387       for (k = 0; k < MV_FP_SIZE - 1; ++k) {
    388         diff += (int)nmv_count->class0_fp[j][k] *
    389                 (pre_nmv_prob->class0_fp[j][k] - cur_nmv_prob->class0_fp[j][k]);
    390       }
    391       pre_last_prob = MAX_PROB - pre_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
    392       cur_last_prob = MAX_PROB - cur_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
    393 
    394       diff += (int)nmv_count->class0_fp[j][MV_FP_SIZE - 1] *
    395               (pre_last_prob - cur_last_prob);
    396     }
    397 
    398     // fp
    399     for (j = 0; j < MV_FP_SIZE - 1; ++j) {
    400       diff +=
    401           (int)nmv_count->fp[j] * (pre_nmv_prob->fp[j] - cur_nmv_prob->fp[j]);
    402     }
    403     pre_last_prob = MAX_PROB - pre_nmv_prob->fp[MV_FP_SIZE - 2];
    404     cur_last_prob = MAX_PROB - cur_nmv_prob->fp[MV_FP_SIZE - 2];
    405 
    406     diff +=
    407         (int)nmv_count->fp[MV_FP_SIZE - 1] * (pre_last_prob - cur_last_prob);
    408 
    409     // class0_hp
    410     diff += (int)nmv_count->class0_hp[0] *
    411             (pre_nmv_prob->class0_hp - cur_nmv_prob->class0_hp);
    412 
    413     pre_last_prob = MAX_PROB - pre_nmv_prob->class0_hp;
    414     cur_last_prob = MAX_PROB - cur_nmv_prob->class0_hp;
    415 
    416     diff += (int)nmv_count->class0_hp[1] * (pre_last_prob - cur_last_prob);
    417 
    418     // hp
    419     diff += (int)nmv_count->hp[0] * (pre_nmv_prob->hp - cur_nmv_prob->hp);
    420 
    421     pre_last_prob = MAX_PROB - pre_nmv_prob->hp;
    422     cur_last_prob = MAX_PROB - cur_nmv_prob->hp;
    423 
    424     diff += (int)nmv_count->hp[1] * (pre_last_prob - cur_last_prob);
    425   }
    426 
    427   return -diff;
    428 }
    429 
    430 // Test for whether to calculate metrics for the frame.
    431 static int is_psnr_calc_enabled(VP9_COMP *cpi) {
    432   VP9_COMMON *const cm = &cpi->common;
    433   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    434 
    435   return cpi->b_calculate_psnr && (oxcf->pass != 1) && cm->show_frame;
    436 }
    437 
    438 /* clang-format off */
    439 const Vp9LevelSpec vp9_level_defs[VP9_LEVELS] = {
    440   //         sample rate    size   breadth  bitrate  cpb
    441   { LEVEL_1,   829440,      36864,    512,   200,    400,    2, 1,  4,  8 },
    442   { LEVEL_1_1, 2764800,     73728,    768,   800,    1000,   2, 1,  4,  8 },
    443   { LEVEL_2,   4608000,     122880,   960,   1800,   1500,   2, 1,  4,  8 },
    444   { LEVEL_2_1, 9216000,     245760,   1344,  3600,   2800,   2, 2,  4,  8 },
    445   { LEVEL_3,   20736000,    552960,   2048,  7200,   6000,   2, 4,  4,  8 },
    446   { LEVEL_3_1, 36864000,    983040,   2752,  12000,  10000,  2, 4,  4,  8 },
    447   { LEVEL_4,   83558400,    2228224,  4160,  18000,  16000,  4, 4,  4,  8 },
    448   { LEVEL_4_1, 160432128,   2228224,  4160,  30000,  18000,  4, 4,  5,  6 },
    449   { LEVEL_5,   311951360,   8912896,  8384,  60000,  36000,  6, 8,  6,  4 },
    450   { LEVEL_5_1, 588251136,   8912896,  8384,  120000, 46000,  8, 8,  10, 4 },
    451   // TODO(huisu): update max_cpb_size for level 5_2 ~ 6_2 when
    452   // they are finalized (currently tentative).
    453   { LEVEL_5_2, 1176502272,  8912896,  8384,  180000, 90000,  8, 8,  10, 4 },
    454   { LEVEL_6,   1176502272,  35651584, 16832, 180000, 90000,  8, 16, 10, 4 },
    455   { LEVEL_6_1, 2353004544u, 35651584, 16832, 240000, 180000, 8, 16, 10, 4 },
    456   { LEVEL_6_2, 4706009088u, 35651584, 16832, 480000, 360000, 8, 16, 10, 4 },
    457 };
    458 /* clang-format on */
    459 
    460 static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] = {
    461   "The average bit-rate is too high.",
    462   "The picture size is too large.",
    463   "The picture width/height is too large.",
    464   "The luma sample rate is too large.",
    465   "The CPB size is too large.",
    466   "The compression ratio is too small",
    467   "Too many column tiles are used.",
    468   "The alt-ref distance is too small.",
    469   "Too many reference buffers are used."
    470 };
    471 
    472 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
    473   switch (mode) {
    474     case NORMAL:
    475       *hr = 1;
    476       *hs = 1;
    477       break;
    478     case FOURFIVE:
    479       *hr = 4;
    480       *hs = 5;
    481       break;
    482     case THREEFIVE:
    483       *hr = 3;
    484       *hs = 5;
    485       break;
    486     case ONETWO:
    487       *hr = 1;
    488       *hs = 2;
    489       break;
    490     default:
    491       *hr = 1;
    492       *hs = 1;
    493       assert(0);
    494       break;
    495   }
    496 }
    497 
    498 // Mark all inactive blocks as active. Other segmentation features may be set
    499 // so memset cannot be used, instead only inactive blocks should be reset.
    500 static void suppress_active_map(VP9_COMP *cpi) {
    501   unsigned char *const seg_map = cpi->segmentation_map;
    502 
    503   if (cpi->active_map.enabled || cpi->active_map.update) {
    504     const int rows = cpi->common.mi_rows;
    505     const int cols = cpi->common.mi_cols;
    506     int i;
    507 
    508     for (i = 0; i < rows * cols; ++i)
    509       if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
    510         seg_map[i] = AM_SEGMENT_ID_ACTIVE;
    511   }
    512 }
    513 
    514 static void apply_active_map(VP9_COMP *cpi) {
    515   struct segmentation *const seg = &cpi->common.seg;
    516   unsigned char *const seg_map = cpi->segmentation_map;
    517   const unsigned char *const active_map = cpi->active_map.map;
    518   int i;
    519 
    520   assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
    521 
    522   if (frame_is_intra_only(&cpi->common)) {
    523     cpi->active_map.enabled = 0;
    524     cpi->active_map.update = 1;
    525   }
    526 
    527   if (cpi->active_map.update) {
    528     if (cpi->active_map.enabled) {
    529       for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
    530         if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
    531       vp9_enable_segmentation(seg);
    532       vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
    533       vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
    534       // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
    535       // filter level being zero regardless of the value of seg->abs_delta.
    536       vp9_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
    537                       -MAX_LOOP_FILTER);
    538     } else {
    539       vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
    540       vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
    541       if (seg->enabled) {
    542         seg->update_data = 1;
    543         seg->update_map = 1;
    544       }
    545     }
    546     cpi->active_map.update = 0;
    547   }
    548 }
    549 
    550 static void init_level_info(Vp9LevelInfo *level_info) {
    551   Vp9LevelStats *const level_stats = &level_info->level_stats;
    552   Vp9LevelSpec *const level_spec = &level_info->level_spec;
    553 
    554   memset(level_stats, 0, sizeof(*level_stats));
    555   memset(level_spec, 0, sizeof(*level_spec));
    556   level_spec->level = LEVEL_UNKNOWN;
    557   level_spec->min_altref_distance = INT_MAX;
    558 }
    559 
    560 VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec) {
    561   int i;
    562   const Vp9LevelSpec *this_level;
    563 
    564   vpx_clear_system_state();
    565 
    566   for (i = 0; i < VP9_LEVELS; ++i) {
    567     this_level = &vp9_level_defs[i];
    568     if ((double)level_spec->max_luma_sample_rate >
    569             (double)this_level->max_luma_sample_rate *
    570                 (1 + SAMPLE_RATE_GRACE_P) ||
    571         level_spec->max_luma_picture_size > this_level->max_luma_picture_size ||
    572         level_spec->max_luma_picture_breadth >
    573             this_level->max_luma_picture_breadth ||
    574         level_spec->average_bitrate > this_level->average_bitrate ||
    575         level_spec->max_cpb_size > this_level->max_cpb_size ||
    576         level_spec->compression_ratio < this_level->compression_ratio ||
    577         level_spec->max_col_tiles > this_level->max_col_tiles ||
    578         level_spec->min_altref_distance < this_level->min_altref_distance ||
    579         level_spec->max_ref_frame_buffers > this_level->max_ref_frame_buffers)
    580       continue;
    581     break;
    582   }
    583   return (i == VP9_LEVELS) ? LEVEL_UNKNOWN : vp9_level_defs[i].level;
    584 }
    585 
    586 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
    587                        int cols) {
    588   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
    589     unsigned char *const active_map_8x8 = cpi->active_map.map;
    590     const int mi_rows = cpi->common.mi_rows;
    591     const int mi_cols = cpi->common.mi_cols;
    592     cpi->active_map.update = 1;
    593     if (new_map_16x16) {
    594       int r, c;
    595       for (r = 0; r < mi_rows; ++r) {
    596         for (c = 0; c < mi_cols; ++c) {
    597           active_map_8x8[r * mi_cols + c] =
    598               new_map_16x16[(r >> 1) * cols + (c >> 1)]
    599                   ? AM_SEGMENT_ID_ACTIVE
    600                   : AM_SEGMENT_ID_INACTIVE;
    601         }
    602       }
    603       cpi->active_map.enabled = 1;
    604     } else {
    605       cpi->active_map.enabled = 0;
    606     }
    607     return 0;
    608   } else {
    609     return -1;
    610   }
    611 }
    612 
    613 int vp9_get_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
    614                        int cols) {
    615   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
    616       new_map_16x16) {
    617     unsigned char *const seg_map_8x8 = cpi->segmentation_map;
    618     const int mi_rows = cpi->common.mi_rows;
    619     const int mi_cols = cpi->common.mi_cols;
    620     memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
    621     if (cpi->active_map.enabled) {
    622       int r, c;
    623       for (r = 0; r < mi_rows; ++r) {
    624         for (c = 0; c < mi_cols; ++c) {
    625           // Cyclic refresh segments are considered active despite not having
    626           // AM_SEGMENT_ID_ACTIVE
    627           new_map_16x16[(r >> 1) * cols + (c >> 1)] |=
    628               seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
    629         }
    630       }
    631     }
    632     return 0;
    633   } else {
    634     return -1;
    635   }
    636 }
    637 
    638 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
    639   MACROBLOCK *const mb = &cpi->td.mb;
    640   cpi->common.allow_high_precision_mv = allow_high_precision_mv;
    641   if (cpi->common.allow_high_precision_mv) {
    642     mb->mvcost = mb->nmvcost_hp;
    643     mb->mvsadcost = mb->nmvsadcost_hp;
    644   } else {
    645     mb->mvcost = mb->nmvcost;
    646     mb->mvsadcost = mb->nmvsadcost;
    647   }
    648 }
    649 
    650 static void setup_frame(VP9_COMP *cpi) {
    651   VP9_COMMON *const cm = &cpi->common;
    652   // Set up entropy context depending on frame type. The decoder mandates
    653   // the use of the default context, index 0, for keyframes and inter
    654   // frames where the error_resilient_mode or intra_only flag is set. For
    655   // other inter-frames the encoder currently uses only two contexts;
    656   // context 1 for ALTREF frames and context 0 for the others.
    657   if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    658     vp9_setup_past_independence(cm);
    659   } else {
    660     if (!cpi->use_svc) cm->frame_context_idx = cpi->refresh_alt_ref_frame;
    661   }
    662 
    663   if (cm->frame_type == KEY_FRAME) {
    664     if (!is_two_pass_svc(cpi)) cpi->refresh_golden_frame = 1;
    665     cpi->refresh_alt_ref_frame = 1;
    666     vp9_zero(cpi->interp_filter_selected);
    667   } else {
    668     *cm->fc = cm->frame_contexts[cm->frame_context_idx];
    669     vp9_zero(cpi->interp_filter_selected[0]);
    670   }
    671 }
    672 
    673 static void vp9_enc_setup_mi(VP9_COMMON *cm) {
    674   int i;
    675   cm->mi = cm->mip + cm->mi_stride + 1;
    676   memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
    677   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
    678   // Clear top border row
    679   memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
    680   // Clear left border column
    681   for (i = 1; i < cm->mi_rows + 1; ++i)
    682     memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
    683 
    684   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
    685   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
    686 
    687   memset(cm->mi_grid_base, 0,
    688          cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
    689 }
    690 
    691 static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
    692   cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
    693   if (!cm->mip) return 1;
    694   cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
    695   if (!cm->prev_mip) return 1;
    696   cm->mi_alloc_size = mi_size;
    697 
    698   cm->mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
    699   if (!cm->mi_grid_base) return 1;
    700   cm->prev_mi_grid_base =
    701       (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
    702   if (!cm->prev_mi_grid_base) return 1;
    703 
    704   return 0;
    705 }
    706 
    707 static void vp9_enc_free_mi(VP9_COMMON *cm) {
    708   vpx_free(cm->mip);
    709   cm->mip = NULL;
    710   vpx_free(cm->prev_mip);
    711   cm->prev_mip = NULL;
    712   vpx_free(cm->mi_grid_base);
    713   cm->mi_grid_base = NULL;
    714   vpx_free(cm->prev_mi_grid_base);
    715   cm->prev_mi_grid_base = NULL;
    716 }
    717 
    718 static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
    719   // Current mip will be the prev_mip for the next frame.
    720   MODE_INFO **temp_base = cm->prev_mi_grid_base;
    721   MODE_INFO *temp = cm->prev_mip;
    722   cm->prev_mip = cm->mip;
    723   cm->mip = temp;
    724 
    725   // Update the upper left visible macroblock ptrs.
    726   cm->mi = cm->mip + cm->mi_stride + 1;
    727   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
    728 
    729   cm->prev_mi_grid_base = cm->mi_grid_base;
    730   cm->mi_grid_base = temp_base;
    731   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
    732   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
    733 }
    734 
    735 void vp9_initialize_enc(void) {
    736   static volatile int init_done = 0;
    737 
    738   if (!init_done) {
    739     vp9_rtcd();
    740     vpx_dsp_rtcd();
    741     vpx_scale_rtcd();
    742     vp9_init_intra_predictors();
    743     vp9_init_me_luts();
    744     vp9_rc_init_minq_luts();
    745     vp9_entropy_mv_init();
    746 #if !CONFIG_REALTIME_ONLY
    747     vp9_temporal_filter_init();
    748 #endif
    749     init_done = 1;
    750   }
    751 }
    752 
    753 static void dealloc_compressor_data(VP9_COMP *cpi) {
    754   VP9_COMMON *const cm = &cpi->common;
    755   int i;
    756 
    757   vpx_free(cpi->mbmi_ext_base);
    758   cpi->mbmi_ext_base = NULL;
    759 
    760   vpx_free(cpi->tile_data);
    761   cpi->tile_data = NULL;
    762 
    763   vpx_free(cpi->segmentation_map);
    764   cpi->segmentation_map = NULL;
    765   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
    766   cpi->coding_context.last_frame_seg_map_copy = NULL;
    767 
    768   vpx_free(cpi->nmvcosts[0]);
    769   vpx_free(cpi->nmvcosts[1]);
    770   cpi->nmvcosts[0] = NULL;
    771   cpi->nmvcosts[1] = NULL;
    772 
    773   vpx_free(cpi->nmvcosts_hp[0]);
    774   vpx_free(cpi->nmvcosts_hp[1]);
    775   cpi->nmvcosts_hp[0] = NULL;
    776   cpi->nmvcosts_hp[1] = NULL;
    777 
    778   vpx_free(cpi->nmvsadcosts[0]);
    779   vpx_free(cpi->nmvsadcosts[1]);
    780   cpi->nmvsadcosts[0] = NULL;
    781   cpi->nmvsadcosts[1] = NULL;
    782 
    783   vpx_free(cpi->nmvsadcosts_hp[0]);
    784   vpx_free(cpi->nmvsadcosts_hp[1]);
    785   cpi->nmvsadcosts_hp[0] = NULL;
    786   cpi->nmvsadcosts_hp[1] = NULL;
    787 
    788   vpx_free(cpi->skin_map);
    789   cpi->skin_map = NULL;
    790 
    791   vpx_free(cpi->prev_partition);
    792   cpi->prev_partition = NULL;
    793 
    794   vpx_free(cpi->svc.prev_partition_svc);
    795   cpi->svc.prev_partition_svc = NULL;
    796 
    797   vpx_free(cpi->prev_segment_id);
    798   cpi->prev_segment_id = NULL;
    799 
    800   vpx_free(cpi->prev_variance_low);
    801   cpi->prev_variance_low = NULL;
    802 
    803   vpx_free(cpi->copied_frame_cnt);
    804   cpi->copied_frame_cnt = NULL;
    805 
    806   vpx_free(cpi->content_state_sb_fd);
    807   cpi->content_state_sb_fd = NULL;
    808 
    809   vpx_free(cpi->count_arf_frame_usage);
    810   cpi->count_arf_frame_usage = NULL;
    811   vpx_free(cpi->count_lastgolden_frame_usage);
    812   cpi->count_lastgolden_frame_usage = NULL;
    813 
    814   vp9_cyclic_refresh_free(cpi->cyclic_refresh);
    815   cpi->cyclic_refresh = NULL;
    816 
    817   vpx_free(cpi->active_map.map);
    818   cpi->active_map.map = NULL;
    819 
    820   vpx_free(cpi->consec_zero_mv);
    821   cpi->consec_zero_mv = NULL;
    822 
    823   vp9_free_ref_frame_buffers(cm->buffer_pool);
    824 #if CONFIG_VP9_POSTPROC
    825   vp9_free_postproc_buffers(cm);
    826 #endif
    827   vp9_free_context_buffers(cm);
    828 
    829   vpx_free_frame_buffer(&cpi->last_frame_uf);
    830   vpx_free_frame_buffer(&cpi->scaled_source);
    831   vpx_free_frame_buffer(&cpi->scaled_last_source);
    832   vpx_free_frame_buffer(&cpi->alt_ref_buffer);
    833 #ifdef ENABLE_KF_DENOISE
    834   vpx_free_frame_buffer(&cpi->raw_unscaled_source);
    835   vpx_free_frame_buffer(&cpi->raw_scaled_source);
    836 #endif
    837 
    838   vp9_lookahead_destroy(cpi->lookahead);
    839 
    840   vpx_free(cpi->tile_tok[0][0]);
    841   cpi->tile_tok[0][0] = 0;
    842 
    843   vpx_free(cpi->tplist[0][0]);
    844   cpi->tplist[0][0] = NULL;
    845 
    846   vp9_free_pc_tree(&cpi->td);
    847 
    848   for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
    849     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
    850     vpx_free(lc->rc_twopass_stats_in.buf);
    851     lc->rc_twopass_stats_in.buf = NULL;
    852     lc->rc_twopass_stats_in.sz = 0;
    853   }
    854 
    855   if (cpi->source_diff_var != NULL) {
    856     vpx_free(cpi->source_diff_var);
    857     cpi->source_diff_var = NULL;
    858   }
    859 
    860   for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
    861     vpx_free_frame_buffer(&cpi->svc.scaled_frames[i]);
    862   }
    863   memset(&cpi->svc.scaled_frames[0], 0,
    864          MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
    865 
    866   vpx_free_frame_buffer(&cpi->svc.scaled_temp);
    867   memset(&cpi->svc.scaled_temp, 0, sizeof(cpi->svc.scaled_temp));
    868 
    869   vpx_free_frame_buffer(&cpi->svc.empty_frame.img);
    870   memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
    871 
    872   vp9_free_svc_cyclic_refresh(cpi);
    873 }
    874 
    875 static void save_coding_context(VP9_COMP *cpi) {
    876   CODING_CONTEXT *const cc = &cpi->coding_context;
    877   VP9_COMMON *cm = &cpi->common;
    878 
    879   // Stores a snapshot of key state variables which can subsequently be
    880   // restored with a call to vp9_restore_coding_context. These functions are
    881   // intended for use in a re-code loop in vp9_compress_frame where the
    882   // quantizer value is adjusted between loop iterations.
    883   vp9_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
    884 
    885   memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
    886          MV_VALS * sizeof(*cpi->nmvcosts[0]));
    887   memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
    888          MV_VALS * sizeof(*cpi->nmvcosts[1]));
    889   memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
    890          MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
    891   memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
    892          MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
    893 
    894   vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
    895 
    896   memcpy(cpi->coding_context.last_frame_seg_map_copy, cm->last_frame_seg_map,
    897          (cm->mi_rows * cm->mi_cols));
    898 
    899   vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
    900   vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
    901 
    902   cc->fc = *cm->fc;
    903 }
    904 
    905 static void restore_coding_context(VP9_COMP *cpi) {
    906   CODING_CONTEXT *const cc = &cpi->coding_context;
    907   VP9_COMMON *cm = &cpi->common;
    908 
    909   // Restore key state variables to the snapshot state stored in the
    910   // previous call to vp9_save_coding_context.
    911   vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
    912 
    913   memcpy(cpi->nmvcosts[0], cc->nmvcosts[0], MV_VALS * sizeof(*cc->nmvcosts[0]));
    914   memcpy(cpi->nmvcosts[1], cc->nmvcosts[1], MV_VALS * sizeof(*cc->nmvcosts[1]));
    915   memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
    916          MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
    917   memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
    918          MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
    919 
    920   vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
    921 
    922   memcpy(cm->last_frame_seg_map, cpi->coding_context.last_frame_seg_map_copy,
    923          (cm->mi_rows * cm->mi_cols));
    924 
    925   vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
    926   vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
    927 
    928   *cm->fc = cc->fc;
    929 }
    930 
    931 #if !CONFIG_REALTIME_ONLY
    932 static void configure_static_seg_features(VP9_COMP *cpi) {
    933   VP9_COMMON *const cm = &cpi->common;
    934   const RATE_CONTROL *const rc = &cpi->rc;
    935   struct segmentation *const seg = &cm->seg;
    936 
    937   int high_q = (int)(rc->avg_q > 48.0);
    938   int qi_delta;
    939 
    940   // Disable and clear down for KF
    941   if (cm->frame_type == KEY_FRAME) {
    942     // Clear down the global segmentation map
    943     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    944     seg->update_map = 0;
    945     seg->update_data = 0;
    946     cpi->static_mb_pct = 0;
    947 
    948     // Disable segmentation
    949     vp9_disable_segmentation(seg);
    950 
    951     // Clear down the segment features.
    952     vp9_clearall_segfeatures(seg);
    953   } else if (cpi->refresh_alt_ref_frame) {
    954     // If this is an alt ref frame
    955     // Clear down the global segmentation map
    956     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    957     seg->update_map = 0;
    958     seg->update_data = 0;
    959     cpi->static_mb_pct = 0;
    960 
    961     // Disable segmentation and individual segment features by default
    962     vp9_disable_segmentation(seg);
    963     vp9_clearall_segfeatures(seg);
    964 
    965     // Scan frames from current to arf frame.
    966     // This function re-enables segmentation if appropriate.
    967     vp9_update_mbgraph_stats(cpi);
    968 
    969     // If segmentation was enabled set those features needed for the
    970     // arf itself.
    971     if (seg->enabled) {
    972       seg->update_map = 1;
    973       seg->update_data = 1;
    974 
    975       qi_delta =
    976           vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
    977       vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
    978       vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
    979 
    980       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
    981       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
    982 
    983       // Where relevant assume segment data is delta data
    984       seg->abs_delta = SEGMENT_DELTADATA;
    985     }
    986   } else if (seg->enabled) {
    987     // All other frames if segmentation has been enabled
    988 
    989     // First normal frame in a valid gf or alt ref group
    990     if (rc->frames_since_golden == 0) {
    991       // Set up segment features for normal frames in an arf group
    992       if (rc->source_alt_ref_active) {
    993         seg->update_map = 0;
    994         seg->update_data = 1;
    995         seg->abs_delta = SEGMENT_DELTADATA;
    996 
    997         qi_delta =
    998             vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
    999         vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
   1000         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
   1001 
   1002         vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
   1003         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
   1004 
   1005         // Segment coding disabled for compred testing
   1006         if (high_q || (cpi->static_mb_pct == 100)) {
   1007           vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
   1008           vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
   1009           vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
   1010         }
   1011       } else {
   1012         // Disable segmentation and clear down features if alt ref
   1013         // is not active for this group
   1014 
   1015         vp9_disable_segmentation(seg);
   1016 
   1017         memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
   1018 
   1019         seg->update_map = 0;
   1020         seg->update_data = 0;
   1021 
   1022         vp9_clearall_segfeatures(seg);
   1023       }
   1024     } else if (rc->is_src_frame_alt_ref) {
   1025       // Special case where we are coding over the top of a previous
   1026       // alt ref frame.
   1027       // Segment coding disabled for compred testing
   1028 
   1029       // Enable ref frame features for segment 0 as well
   1030       vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
   1031       vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
   1032 
   1033       // All mbs should use ALTREF_FRAME
   1034       vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
   1035       vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
   1036       vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
   1037       vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
   1038 
   1039       // Skip all MBs if high Q (0,0 mv and skip coeffs)
   1040       if (high_q) {
   1041         vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
   1042         vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
   1043       }
   1044       // Enable data update
   1045       seg->update_data = 1;
   1046     } else {
   1047       // All other frames.
   1048 
   1049       // No updates.. leave things as they are.
   1050       seg->update_map = 0;
   1051       seg->update_data = 0;
   1052     }
   1053   }
   1054 }
   1055 #endif  // !CONFIG_REALTIME_ONLY
   1056 
   1057 static void update_reference_segmentation_map(VP9_COMP *cpi) {
   1058   VP9_COMMON *const cm = &cpi->common;
   1059   MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
   1060   uint8_t *cache_ptr = cm->last_frame_seg_map;
   1061   int row, col;
   1062 
   1063   for (row = 0; row < cm->mi_rows; row++) {
   1064     MODE_INFO **mi_8x8 = mi_8x8_ptr;
   1065     uint8_t *cache = cache_ptr;
   1066     for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
   1067       cache[0] = mi_8x8[0]->segment_id;
   1068     mi_8x8_ptr += cm->mi_stride;
   1069     cache_ptr += cm->mi_cols;
   1070   }
   1071 }
   1072 
   1073 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
   1074   VP9_COMMON *cm = &cpi->common;
   1075   const VP9EncoderConfig *oxcf = &cpi->oxcf;
   1076 
   1077   if (!cpi->lookahead)
   1078     cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
   1079                                         cm->subsampling_x, cm->subsampling_y,
   1080 #if CONFIG_VP9_HIGHBITDEPTH
   1081                                         cm->use_highbitdepth,
   1082 #endif
   1083                                         oxcf->lag_in_frames);
   1084   if (!cpi->lookahead)
   1085     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   1086                        "Failed to allocate lag buffers");
   1087 
   1088   // TODO(agrange) Check if ARF is enabled and skip allocation if not.
   1089   if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
   1090                                cm->subsampling_x, cm->subsampling_y,
   1091 #if CONFIG_VP9_HIGHBITDEPTH
   1092                                cm->use_highbitdepth,
   1093 #endif
   1094                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
   1095                                NULL, NULL, NULL))
   1096     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   1097                        "Failed to allocate altref buffer");
   1098 }
   1099 
   1100 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
   1101   VP9_COMMON *const cm = &cpi->common;
   1102   if (vpx_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
   1103                                cm->subsampling_x, cm->subsampling_y,
   1104 #if CONFIG_VP9_HIGHBITDEPTH
   1105                                cm->use_highbitdepth,
   1106 #endif
   1107                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
   1108                                NULL, NULL, NULL))
   1109     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   1110                        "Failed to allocate last frame buffer");
   1111 
   1112   if (vpx_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
   1113                                cm->subsampling_x, cm->subsampling_y,
   1114 #if CONFIG_VP9_HIGHBITDEPTH
   1115                                cm->use_highbitdepth,
   1116 #endif
   1117                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
   1118                                NULL, NULL, NULL))
   1119     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   1120                        "Failed to allocate scaled source buffer");
   1121 
   1122   // For 1 pass cbr: allocate scaled_frame that may be used as an intermediate
   1123   // buffer for a 2 stage down-sampling: two stages of 1:2 down-sampling for a
   1124   // target of 1/4x1/4.
   1125   if (is_one_pass_cbr_svc(cpi) && !cpi->svc.scaled_temp_is_alloc) {
   1126     cpi->svc.scaled_temp_is_alloc = 1;
   1127     if (vpx_realloc_frame_buffer(
   1128             &cpi->svc.scaled_temp, cm->width >> 1, cm->height >> 1,
   1129             cm->subsampling_x, cm->subsampling_y,
   1130 #if CONFIG_VP9_HIGHBITDEPTH
   1131             cm->use_highbitdepth,
   1132 #endif
   1133             VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
   1134       vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
   1135                          "Failed to allocate scaled_frame for svc ");
   1136   }
   1137 
   1138   if (vpx_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
   1139                                cm->subsampling_x, cm->subsampling_y,
   1140 #if CONFIG_VP9_HIGHBITDEPTH
   1141                                cm->use_highbitdepth,
   1142 #endif
   1143                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
   1144                                NULL, NULL, NULL))
   1145     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   1146                        "Failed to allocate scaled last source buffer");
   1147 #ifdef ENABLE_KF_DENOISE
   1148   if (vpx_realloc_frame_buffer(&cpi->raw_unscaled_source, cm->width, cm->height,
   1149                                cm->subsampling_x, cm->subsampling_y,
   1150 #if CONFIG_VP9_HIGHBITDEPTH
   1151                                cm->use_highbitdepth,
   1152 #endif
   1153                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
   1154                                NULL, NULL, NULL))
   1155     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   1156                        "Failed to allocate unscaled raw source frame buffer");
   1157 
   1158   if (vpx_realloc_frame_buffer(&cpi->raw_scaled_source, cm->width, cm->height,
   1159                                cm->subsampling_x, cm->subsampling_y,
   1160 #if CONFIG_VP9_HIGHBITDEPTH
   1161                                cm->use_highbitdepth,
   1162 #endif
   1163                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
   1164                                NULL, NULL, NULL))
   1165     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   1166                        "Failed to allocate scaled raw source frame buffer");
   1167 #endif
   1168 }
   1169 
   1170 static int alloc_context_buffers_ext(VP9_COMP *cpi) {
   1171   VP9_COMMON *cm = &cpi->common;
   1172   int mi_size = cm->mi_cols * cm->mi_rows;
   1173 
   1174   cpi->mbmi_ext_base = vpx_calloc(mi_size, sizeof(*cpi->mbmi_ext_base));
   1175   if (!cpi->mbmi_ext_base) return 1;
   1176 
   1177   return 0;
   1178 }
   1179 
   1180 static void alloc_compressor_data(VP9_COMP *cpi) {
   1181   VP9_COMMON *cm = &cpi->common;
   1182   int sb_rows;
   1183 
   1184   vp9_alloc_context_buffers(cm, cm->width, cm->height);
   1185 
   1186   alloc_context_buffers_ext(cpi);
   1187 
   1188   vpx_free(cpi->tile_tok[0][0]);
   1189 
   1190   {
   1191     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
   1192     CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
   1193                     vpx_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
   1194   }
   1195 
   1196   sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
   1197   vpx_free(cpi->tplist[0][0]);
   1198   CHECK_MEM_ERROR(
   1199       cm, cpi->tplist[0][0],
   1200       vpx_calloc(sb_rows * 4 * (1 << 6), sizeof(*cpi->tplist[0][0])));
   1201 
   1202   vp9_setup_pc_tree(&cpi->common, &cpi->td);
   1203 }
   1204 
   1205 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
   1206   cpi->framerate = framerate < 0.1 ? 30 : framerate;
   1207   vp9_rc_update_framerate(cpi);
   1208 }
   1209 
   1210 static void set_tile_limits(VP9_COMP *cpi) {
   1211   VP9_COMMON *const cm = &cpi->common;
   1212 
   1213   int min_log2_tile_cols, max_log2_tile_cols;
   1214   vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
   1215 
   1216   if (is_two_pass_svc(cpi) && (cpi->svc.encode_empty_frame_state == ENCODING ||
   1217                                cpi->svc.number_spatial_layers > 1)) {
   1218     cm->log2_tile_cols = 0;
   1219     cm->log2_tile_rows = 0;
   1220   } else {
   1221     cm->log2_tile_cols =
   1222         clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
   1223     cm->log2_tile_rows = cpi->oxcf.tile_rows;
   1224   }
   1225 
   1226   if (cpi->oxcf.target_level == LEVEL_AUTO) {
   1227     const int level_tile_cols =
   1228         log_tile_cols_from_picsize_level(cpi->common.width, cpi->common.height);
   1229     if (cm->log2_tile_cols > level_tile_cols) {
   1230       cm->log2_tile_cols = VPXMAX(level_tile_cols, min_log2_tile_cols);
   1231     }
   1232   }
   1233 }
   1234 
   1235 static void update_frame_size(VP9_COMP *cpi) {
   1236   VP9_COMMON *const cm = &cpi->common;
   1237   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
   1238 
   1239   vp9_set_mb_mi(cm, cm->width, cm->height);
   1240   vp9_init_context_buffers(cm);
   1241   vp9_init_macroblockd(cm, xd, NULL);
   1242   cpi->td.mb.mbmi_ext_base = cpi->mbmi_ext_base;
   1243   memset(cpi->mbmi_ext_base, 0,
   1244          cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
   1245 
   1246   set_tile_limits(cpi);
   1247 
   1248   if (is_two_pass_svc(cpi)) {
   1249     if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, cm->width, cm->height,
   1250                                  cm->subsampling_x, cm->subsampling_y,
   1251 #if CONFIG_VP9_HIGHBITDEPTH
   1252                                  cm->use_highbitdepth,
   1253 #endif
   1254                                  VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
   1255                                  NULL, NULL, NULL))
   1256       vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   1257                          "Failed to reallocate alt_ref_buffer");
   1258   }
   1259 }
   1260 
   1261 static void init_buffer_indices(VP9_COMP *cpi) {
   1262   cpi->lst_fb_idx = 0;
   1263   cpi->gld_fb_idx = 1;
   1264   cpi->alt_fb_idx = 2;
   1265 }
   1266 
   1267 static void init_level_constraint(LevelConstraint *lc) {
   1268   lc->level_index = -1;
   1269   lc->max_cpb_size = INT_MAX;
   1270   lc->max_frame_size = INT_MAX;
   1271   lc->rc_config_updated = 0;
   1272   lc->fail_flag = 0;
   1273 }
   1274 
   1275 static void set_level_constraint(LevelConstraint *ls, int8_t level_index) {
   1276   vpx_clear_system_state();
   1277   ls->level_index = level_index;
   1278   if (level_index >= 0) {
   1279     ls->max_cpb_size = vp9_level_defs[level_index].max_cpb_size * (double)1000;
   1280   }
   1281 }
   1282 
   1283 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
   1284   VP9_COMMON *const cm = &cpi->common;
   1285 
   1286   cpi->oxcf = *oxcf;
   1287   cpi->framerate = oxcf->init_framerate;
   1288   cm->profile = oxcf->profile;
   1289   cm->bit_depth = oxcf->bit_depth;
   1290 #if CONFIG_VP9_HIGHBITDEPTH
   1291   cm->use_highbitdepth = oxcf->use_highbitdepth;
   1292 #endif
   1293   cm->color_space = oxcf->color_space;
   1294   cm->color_range = oxcf->color_range;
   1295 
   1296   cpi->target_level = oxcf->target_level;
   1297   cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
   1298   set_level_constraint(&cpi->level_constraint,
   1299                        get_level_index(cpi->target_level));
   1300 
   1301   cm->width = oxcf->width;
   1302   cm->height = oxcf->height;
   1303   alloc_compressor_data(cpi);
   1304 
   1305   cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
   1306 
   1307   // Single thread case: use counts in common.
   1308   cpi->td.counts = &cm->counts;
   1309 
   1310   // Spatial scalability.
   1311   cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
   1312   // Temporal scalability.
   1313   cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
   1314 
   1315   if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
   1316       ((cpi->svc.number_temporal_layers > 1 ||
   1317         cpi->svc.number_spatial_layers > 1) &&
   1318        cpi->oxcf.pass != 1)) {
   1319     vp9_init_layer_context(cpi);
   1320   }
   1321 
   1322   // change includes all joint functionality
   1323   vp9_change_config(cpi, oxcf);
   1324 
   1325   cpi->static_mb_pct = 0;
   1326   cpi->ref_frame_flags = 0;
   1327 
   1328   init_buffer_indices(cpi);
   1329 
   1330   vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
   1331 }
   1332 
   1333 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
   1334                                 const VP9EncoderConfig *oxcf) {
   1335   const int64_t bandwidth = oxcf->target_bandwidth;
   1336   const int64_t starting = oxcf->starting_buffer_level_ms;
   1337   const int64_t optimal = oxcf->optimal_buffer_level_ms;
   1338   const int64_t maximum = oxcf->maximum_buffer_size_ms;
   1339 
   1340   rc->starting_buffer_level = starting * bandwidth / 1000;
   1341   rc->optimal_buffer_level =
   1342       (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
   1343   rc->maximum_buffer_size =
   1344       (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
   1345 }
   1346 
   1347 #if CONFIG_VP9_HIGHBITDEPTH
   1348 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF) \
   1349   cpi->fn_ptr[BT].sdf = SDF;                             \
   1350   cpi->fn_ptr[BT].sdaf = SDAF;                           \
   1351   cpi->fn_ptr[BT].vf = VF;                               \
   1352   cpi->fn_ptr[BT].svf = SVF;                             \
   1353   cpi->fn_ptr[BT].svaf = SVAF;                           \
   1354   cpi->fn_ptr[BT].sdx4df = SDX4DF;
   1355 
   1356 #define MAKE_BFP_SAD_WRAPPER(fnname)                                           \
   1357   static unsigned int fnname##_bits8(const uint8_t *src_ptr,                   \
   1358                                      int source_stride,                        \
   1359                                      const uint8_t *ref_ptr, int ref_stride) { \
   1360     return fnname(src_ptr, source_stride, ref_ptr, ref_stride);                \
   1361   }                                                                            \
   1362   static unsigned int fnname##_bits10(                                         \
   1363       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1364       int ref_stride) {                                                        \
   1365     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2;           \
   1366   }                                                                            \
   1367   static unsigned int fnname##_bits12(                                         \
   1368       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1369       int ref_stride) {                                                        \
   1370     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4;           \
   1371   }
   1372 
   1373 #define MAKE_BFP_SADAVG_WRAPPER(fnname)                                        \
   1374   static unsigned int fnname##_bits8(                                          \
   1375       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1376       int ref_stride, const uint8_t *second_pred) {                            \
   1377     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred);   \
   1378   }                                                                            \
   1379   static unsigned int fnname##_bits10(                                         \
   1380       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1381       int ref_stride, const uint8_t *second_pred) {                            \
   1382     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
   1383            2;                                                                  \
   1384   }                                                                            \
   1385   static unsigned int fnname##_bits12(                                         \
   1386       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
   1387       int ref_stride, const uint8_t *second_pred) {                            \
   1388     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
   1389            4;                                                                  \
   1390   }
   1391 
   1392 #define MAKE_BFP_SAD4D_WRAPPER(fnname)                                        \
   1393   static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,       \
   1394                              const uint8_t *const ref_ptr[], int ref_stride,  \
   1395                              unsigned int *sad_array) {                       \
   1396     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
   1397   }                                                                           \
   1398   static void fnname##_bits10(const uint8_t *src_ptr, int source_stride,      \
   1399                               const uint8_t *const ref_ptr[], int ref_stride, \
   1400                               unsigned int *sad_array) {                      \
   1401     int i;                                                                    \
   1402     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
   1403     for (i = 0; i < 4; i++) sad_array[i] >>= 2;                               \
   1404   }                                                                           \
   1405   static void fnname##_bits12(const uint8_t *src_ptr, int source_stride,      \
   1406                               const uint8_t *const ref_ptr[], int ref_stride, \
   1407                               unsigned int *sad_array) {                      \
   1408     int i;                                                                    \
   1409     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
   1410     for (i = 0; i < 4; i++) sad_array[i] >>= 4;                               \
   1411   }
   1412 
   1413 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x16)
   1414 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)
   1415 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x16x4d)
   1416 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x32)
   1417 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x32_avg)
   1418 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x32x4d)
   1419 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x32)
   1420 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x32_avg)
   1421 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x32x4d)
   1422 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x64)
   1423 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x64_avg)
   1424 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x64x4d)
   1425 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x32)
   1426 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x32_avg)
   1427 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x32x4d)
   1428 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x64)
   1429 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x64_avg)
   1430 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x64x4d)
   1431 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x16)
   1432 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x16_avg)
   1433 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x16x4d)
   1434 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x8)
   1435 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x8_avg)
   1436 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x8x4d)
   1437 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x16)
   1438 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x16_avg)
   1439 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x16x4d)
   1440 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x8)
   1441 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x8_avg)
   1442 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x8x4d)
   1443 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x4)
   1444 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x4_avg)
   1445 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x4x4d)
   1446 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x8)
   1447 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x8_avg)
   1448 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x8x4d)
   1449 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x4)
   1450 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x4_avg)
   1451 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x4x4d)
   1452 
   1453 static void highbd_set_var_fns(VP9_COMP *const cpi) {
   1454   VP9_COMMON *const cm = &cpi->common;
   1455   if (cm->use_highbitdepth) {
   1456     switch (cm->bit_depth) {
   1457       case VPX_BITS_8:
   1458         HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits8,
   1459                    vpx_highbd_sad32x16_avg_bits8, vpx_highbd_8_variance32x16,
   1460                    vpx_highbd_8_sub_pixel_variance32x16,
   1461                    vpx_highbd_8_sub_pixel_avg_variance32x16,
   1462                    vpx_highbd_sad32x16x4d_bits8)
   1463 
   1464         HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits8,
   1465                    vpx_highbd_sad16x32_avg_bits8, vpx_highbd_8_variance16x32,
   1466                    vpx_highbd_8_sub_pixel_variance16x32,
   1467                    vpx_highbd_8_sub_pixel_avg_variance16x32,
   1468                    vpx_highbd_sad16x32x4d_bits8)
   1469 
   1470         HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits8,
   1471                    vpx_highbd_sad64x32_avg_bits8, vpx_highbd_8_variance64x32,
   1472                    vpx_highbd_8_sub_pixel_variance64x32,
   1473                    vpx_highbd_8_sub_pixel_avg_variance64x32,
   1474                    vpx_highbd_sad64x32x4d_bits8)
   1475 
   1476         HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits8,
   1477                    vpx_highbd_sad32x64_avg_bits8, vpx_highbd_8_variance32x64,
   1478                    vpx_highbd_8_sub_pixel_variance32x64,
   1479                    vpx_highbd_8_sub_pixel_avg_variance32x64,
   1480                    vpx_highbd_sad32x64x4d_bits8)
   1481 
   1482         HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits8,
   1483                    vpx_highbd_sad32x32_avg_bits8, vpx_highbd_8_variance32x32,
   1484                    vpx_highbd_8_sub_pixel_variance32x32,
   1485                    vpx_highbd_8_sub_pixel_avg_variance32x32,
   1486                    vpx_highbd_sad32x32x4d_bits8)
   1487 
   1488         HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits8,
   1489                    vpx_highbd_sad64x64_avg_bits8, vpx_highbd_8_variance64x64,
   1490                    vpx_highbd_8_sub_pixel_variance64x64,
   1491                    vpx_highbd_8_sub_pixel_avg_variance64x64,
   1492                    vpx_highbd_sad64x64x4d_bits8)
   1493 
   1494         HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits8,
   1495                    vpx_highbd_sad16x16_avg_bits8, vpx_highbd_8_variance16x16,
   1496                    vpx_highbd_8_sub_pixel_variance16x16,
   1497                    vpx_highbd_8_sub_pixel_avg_variance16x16,
   1498                    vpx_highbd_sad16x16x4d_bits8)
   1499 
   1500         HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits8,
   1501                    vpx_highbd_sad16x8_avg_bits8, vpx_highbd_8_variance16x8,
   1502                    vpx_highbd_8_sub_pixel_variance16x8,
   1503                    vpx_highbd_8_sub_pixel_avg_variance16x8,
   1504                    vpx_highbd_sad16x8x4d_bits8)
   1505 
   1506         HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits8,
   1507                    vpx_highbd_sad8x16_avg_bits8, vpx_highbd_8_variance8x16,
   1508                    vpx_highbd_8_sub_pixel_variance8x16,
   1509                    vpx_highbd_8_sub_pixel_avg_variance8x16,
   1510                    vpx_highbd_sad8x16x4d_bits8)
   1511 
   1512         HIGHBD_BFP(
   1513             BLOCK_8X8, vpx_highbd_sad8x8_bits8, vpx_highbd_sad8x8_avg_bits8,
   1514             vpx_highbd_8_variance8x8, vpx_highbd_8_sub_pixel_variance8x8,
   1515             vpx_highbd_8_sub_pixel_avg_variance8x8, vpx_highbd_sad8x8x4d_bits8)
   1516 
   1517         HIGHBD_BFP(
   1518             BLOCK_8X4, vpx_highbd_sad8x4_bits8, vpx_highbd_sad8x4_avg_bits8,
   1519             vpx_highbd_8_variance8x4, vpx_highbd_8_sub_pixel_variance8x4,
   1520             vpx_highbd_8_sub_pixel_avg_variance8x4, vpx_highbd_sad8x4x4d_bits8)
   1521 
   1522         HIGHBD_BFP(
   1523             BLOCK_4X8, vpx_highbd_sad4x8_bits8, vpx_highbd_sad4x8_avg_bits8,
   1524             vpx_highbd_8_variance4x8, vpx_highbd_8_sub_pixel_variance4x8,
   1525             vpx_highbd_8_sub_pixel_avg_variance4x8, vpx_highbd_sad4x8x4d_bits8)
   1526 
   1527         HIGHBD_BFP(
   1528             BLOCK_4X4, vpx_highbd_sad4x4_bits8, vpx_highbd_sad4x4_avg_bits8,
   1529             vpx_highbd_8_variance4x4, vpx_highbd_8_sub_pixel_variance4x4,
   1530             vpx_highbd_8_sub_pixel_avg_variance4x4, vpx_highbd_sad4x4x4d_bits8)
   1531         break;
   1532 
   1533       case VPX_BITS_10:
   1534         HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits10,
   1535                    vpx_highbd_sad32x16_avg_bits10, vpx_highbd_10_variance32x16,
   1536                    vpx_highbd_10_sub_pixel_variance32x16,
   1537                    vpx_highbd_10_sub_pixel_avg_variance32x16,
   1538                    vpx_highbd_sad32x16x4d_bits10)
   1539 
   1540         HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits10,
   1541                    vpx_highbd_sad16x32_avg_bits10, vpx_highbd_10_variance16x32,
   1542                    vpx_highbd_10_sub_pixel_variance16x32,
   1543                    vpx_highbd_10_sub_pixel_avg_variance16x32,
   1544                    vpx_highbd_sad16x32x4d_bits10)
   1545 
   1546         HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits10,
   1547                    vpx_highbd_sad64x32_avg_bits10, vpx_highbd_10_variance64x32,
   1548                    vpx_highbd_10_sub_pixel_variance64x32,
   1549                    vpx_highbd_10_sub_pixel_avg_variance64x32,
   1550                    vpx_highbd_sad64x32x4d_bits10)
   1551 
   1552         HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits10,
   1553                    vpx_highbd_sad32x64_avg_bits10, vpx_highbd_10_variance32x64,
   1554                    vpx_highbd_10_sub_pixel_variance32x64,
   1555                    vpx_highbd_10_sub_pixel_avg_variance32x64,
   1556                    vpx_highbd_sad32x64x4d_bits10)
   1557 
   1558         HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits10,
   1559                    vpx_highbd_sad32x32_avg_bits10, vpx_highbd_10_variance32x32,
   1560                    vpx_highbd_10_sub_pixel_variance32x32,
   1561                    vpx_highbd_10_sub_pixel_avg_variance32x32,
   1562                    vpx_highbd_sad32x32x4d_bits10)
   1563 
   1564         HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits10,
   1565                    vpx_highbd_sad64x64_avg_bits10, vpx_highbd_10_variance64x64,
   1566                    vpx_highbd_10_sub_pixel_variance64x64,
   1567                    vpx_highbd_10_sub_pixel_avg_variance64x64,
   1568                    vpx_highbd_sad64x64x4d_bits10)
   1569 
   1570         HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits10,
   1571                    vpx_highbd_sad16x16_avg_bits10, vpx_highbd_10_variance16x16,
   1572                    vpx_highbd_10_sub_pixel_variance16x16,
   1573                    vpx_highbd_10_sub_pixel_avg_variance16x16,
   1574                    vpx_highbd_sad16x16x4d_bits10)
   1575 
   1576         HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits10,
   1577                    vpx_highbd_sad16x8_avg_bits10, vpx_highbd_10_variance16x8,
   1578                    vpx_highbd_10_sub_pixel_variance16x8,
   1579                    vpx_highbd_10_sub_pixel_avg_variance16x8,
   1580                    vpx_highbd_sad16x8x4d_bits10)
   1581 
   1582         HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits10,
   1583                    vpx_highbd_sad8x16_avg_bits10, vpx_highbd_10_variance8x16,
   1584                    vpx_highbd_10_sub_pixel_variance8x16,
   1585                    vpx_highbd_10_sub_pixel_avg_variance8x16,
   1586                    vpx_highbd_sad8x16x4d_bits10)
   1587 
   1588         HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits10,
   1589                    vpx_highbd_sad8x8_avg_bits10, vpx_highbd_10_variance8x8,
   1590                    vpx_highbd_10_sub_pixel_variance8x8,
   1591                    vpx_highbd_10_sub_pixel_avg_variance8x8,
   1592                    vpx_highbd_sad8x8x4d_bits10)
   1593 
   1594         HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits10,
   1595                    vpx_highbd_sad8x4_avg_bits10, vpx_highbd_10_variance8x4,
   1596                    vpx_highbd_10_sub_pixel_variance8x4,
   1597                    vpx_highbd_10_sub_pixel_avg_variance8x4,
   1598                    vpx_highbd_sad8x4x4d_bits10)
   1599 
   1600         HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits10,
   1601                    vpx_highbd_sad4x8_avg_bits10, vpx_highbd_10_variance4x8,
   1602                    vpx_highbd_10_sub_pixel_variance4x8,
   1603                    vpx_highbd_10_sub_pixel_avg_variance4x8,
   1604                    vpx_highbd_sad4x8x4d_bits10)
   1605 
   1606         HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits10,
   1607                    vpx_highbd_sad4x4_avg_bits10, vpx_highbd_10_variance4x4,
   1608                    vpx_highbd_10_sub_pixel_variance4x4,
   1609                    vpx_highbd_10_sub_pixel_avg_variance4x4,
   1610                    vpx_highbd_sad4x4x4d_bits10)
   1611         break;
   1612 
   1613       case VPX_BITS_12:
   1614         HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits12,
   1615                    vpx_highbd_sad32x16_avg_bits12, vpx_highbd_12_variance32x16,
   1616                    vpx_highbd_12_sub_pixel_variance32x16,
   1617                    vpx_highbd_12_sub_pixel_avg_variance32x16,
   1618                    vpx_highbd_sad32x16x4d_bits12)
   1619 
   1620         HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits12,
   1621                    vpx_highbd_sad16x32_avg_bits12, vpx_highbd_12_variance16x32,
   1622                    vpx_highbd_12_sub_pixel_variance16x32,
   1623                    vpx_highbd_12_sub_pixel_avg_variance16x32,
   1624                    vpx_highbd_sad16x32x4d_bits12)
   1625 
   1626         HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits12,
   1627                    vpx_highbd_sad64x32_avg_bits12, vpx_highbd_12_variance64x32,
   1628                    vpx_highbd_12_sub_pixel_variance64x32,
   1629                    vpx_highbd_12_sub_pixel_avg_variance64x32,
   1630                    vpx_highbd_sad64x32x4d_bits12)
   1631 
   1632         HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits12,
   1633                    vpx_highbd_sad32x64_avg_bits12, vpx_highbd_12_variance32x64,
   1634                    vpx_highbd_12_sub_pixel_variance32x64,
   1635                    vpx_highbd_12_sub_pixel_avg_variance32x64,
   1636                    vpx_highbd_sad32x64x4d_bits12)
   1637 
   1638         HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits12,
   1639                    vpx_highbd_sad32x32_avg_bits12, vpx_highbd_12_variance32x32,
   1640                    vpx_highbd_12_sub_pixel_variance32x32,
   1641                    vpx_highbd_12_sub_pixel_avg_variance32x32,
   1642                    vpx_highbd_sad32x32x4d_bits12)
   1643 
   1644         HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits12,
   1645                    vpx_highbd_sad64x64_avg_bits12, vpx_highbd_12_variance64x64,
   1646                    vpx_highbd_12_sub_pixel_variance64x64,
   1647                    vpx_highbd_12_sub_pixel_avg_variance64x64,
   1648                    vpx_highbd_sad64x64x4d_bits12)
   1649 
   1650         HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits12,
   1651                    vpx_highbd_sad16x16_avg_bits12, vpx_highbd_12_variance16x16,
   1652                    vpx_highbd_12_sub_pixel_variance16x16,
   1653                    vpx_highbd_12_sub_pixel_avg_variance16x16,
   1654                    vpx_highbd_sad16x16x4d_bits12)
   1655 
   1656         HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits12,
   1657                    vpx_highbd_sad16x8_avg_bits12, vpx_highbd_12_variance16x8,
   1658                    vpx_highbd_12_sub_pixel_variance16x8,
   1659                    vpx_highbd_12_sub_pixel_avg_variance16x8,
   1660                    vpx_highbd_sad16x8x4d_bits12)
   1661 
   1662         HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits12,
   1663                    vpx_highbd_sad8x16_avg_bits12, vpx_highbd_12_variance8x16,
   1664                    vpx_highbd_12_sub_pixel_variance8x16,
   1665                    vpx_highbd_12_sub_pixel_avg_variance8x16,
   1666                    vpx_highbd_sad8x16x4d_bits12)
   1667 
   1668         HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits12,
   1669                    vpx_highbd_sad8x8_avg_bits12, vpx_highbd_12_variance8x8,
   1670                    vpx_highbd_12_sub_pixel_variance8x8,
   1671                    vpx_highbd_12_sub_pixel_avg_variance8x8,
   1672                    vpx_highbd_sad8x8x4d_bits12)
   1673 
   1674         HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits12,
   1675                    vpx_highbd_sad8x4_avg_bits12, vpx_highbd_12_variance8x4,
   1676                    vpx_highbd_12_sub_pixel_variance8x4,
   1677                    vpx_highbd_12_sub_pixel_avg_variance8x4,
   1678                    vpx_highbd_sad8x4x4d_bits12)
   1679 
   1680         HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits12,
   1681                    vpx_highbd_sad4x8_avg_bits12, vpx_highbd_12_variance4x8,
   1682                    vpx_highbd_12_sub_pixel_variance4x8,
   1683                    vpx_highbd_12_sub_pixel_avg_variance4x8,
   1684                    vpx_highbd_sad4x8x4d_bits12)
   1685 
   1686         HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits12,
   1687                    vpx_highbd_sad4x4_avg_bits12, vpx_highbd_12_variance4x4,
   1688                    vpx_highbd_12_sub_pixel_variance4x4,
   1689                    vpx_highbd_12_sub_pixel_avg_variance4x4,
   1690                    vpx_highbd_sad4x4x4d_bits12)
   1691         break;
   1692 
   1693       default:
   1694         assert(0 &&
   1695                "cm->bit_depth should be VPX_BITS_8, "
   1696                "VPX_BITS_10 or VPX_BITS_12");
   1697     }
   1698   }
   1699 }
   1700 #endif  // CONFIG_VP9_HIGHBITDEPTH
   1701 
   1702 static void realloc_segmentation_maps(VP9_COMP *cpi) {
   1703   VP9_COMMON *const cm = &cpi->common;
   1704 
   1705   // Create the encoder segmentation map and set all entries to 0
   1706   vpx_free(cpi->segmentation_map);
   1707   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
   1708                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
   1709 
   1710   // Create a map used for cyclic background refresh.
   1711   if (cpi->cyclic_refresh) vp9_cyclic_refresh_free(cpi->cyclic_refresh);
   1712   CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
   1713                   vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
   1714 
   1715   // Create a map used to mark inactive areas.
   1716   vpx_free(cpi->active_map.map);
   1717   CHECK_MEM_ERROR(cm, cpi->active_map.map,
   1718                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
   1719 
   1720   // And a place holder structure is the coding context
   1721   // for use if we want to save and restore it
   1722   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
   1723   CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
   1724                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
   1725 }
   1726 
   1727 static void alloc_copy_partition_data(VP9_COMP *cpi) {
   1728   VP9_COMMON *const cm = &cpi->common;
   1729   if (cpi->prev_partition == NULL) {
   1730     CHECK_MEM_ERROR(cm, cpi->prev_partition,
   1731                     (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
   1732                                              sizeof(*cpi->prev_partition)));
   1733   }
   1734   if (cpi->prev_segment_id == NULL) {
   1735     CHECK_MEM_ERROR(
   1736         cm, cpi->prev_segment_id,
   1737         (int8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
   1738                              sizeof(*cpi->prev_segment_id)));
   1739   }
   1740   if (cpi->prev_variance_low == NULL) {
   1741     CHECK_MEM_ERROR(cm, cpi->prev_variance_low,
   1742                     (uint8_t *)vpx_calloc(
   1743                         (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) * 25,
   1744                         sizeof(*cpi->prev_variance_low)));
   1745   }
   1746   if (cpi->copied_frame_cnt == NULL) {
   1747     CHECK_MEM_ERROR(
   1748         cm, cpi->copied_frame_cnt,
   1749         (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
   1750                               sizeof(*cpi->copied_frame_cnt)));
   1751   }
   1752 }
   1753 
   1754 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
   1755   VP9_COMMON *const cm = &cpi->common;
   1756   RATE_CONTROL *const rc = &cpi->rc;
   1757   int last_w = cpi->oxcf.width;
   1758   int last_h = cpi->oxcf.height;
   1759 
   1760   if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
   1761   cm->bit_depth = oxcf->bit_depth;
   1762   cm->color_space = oxcf->color_space;
   1763   cm->color_range = oxcf->color_range;
   1764 
   1765   cpi->target_level = oxcf->target_level;
   1766   cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
   1767   set_level_constraint(&cpi->level_constraint,
   1768                        get_level_index(cpi->target_level));
   1769 
   1770   if (cm->profile <= PROFILE_1)
   1771     assert(cm->bit_depth == VPX_BITS_8);
   1772   else
   1773     assert(cm->bit_depth > VPX_BITS_8);
   1774 
   1775   cpi->oxcf = *oxcf;
   1776 #if CONFIG_VP9_HIGHBITDEPTH
   1777   cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
   1778 #endif  // CONFIG_VP9_HIGHBITDEPTH
   1779 
   1780   if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
   1781     rc->baseline_gf_interval = FIXED_GF_INTERVAL;
   1782   } else {
   1783     rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
   1784   }
   1785 
   1786   cpi->refresh_golden_frame = 0;
   1787   cpi->refresh_last_frame = 1;
   1788   cm->refresh_frame_context = 1;
   1789   cm->reset_frame_context = 0;
   1790 
   1791   vp9_reset_segment_features(&cm->seg);
   1792   vp9_set_high_precision_mv(cpi, 0);
   1793 
   1794   {
   1795     int i;
   1796 
   1797     for (i = 0; i < MAX_SEGMENTS; i++)
   1798       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
   1799   }
   1800   cpi->encode_breakout = cpi->oxcf.encode_breakout;
   1801 
   1802   set_rc_buffer_sizes(rc, &cpi->oxcf);
   1803 
   1804   // Under a configuration change, where maximum_buffer_size may change,
   1805   // keep buffer level clipped to the maximum allowed buffer size.
   1806   rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
   1807   rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size);
   1808 
   1809   // Set up frame rate and related parameters rate control values.
   1810   vp9_new_framerate(cpi, cpi->framerate);
   1811 
   1812   // Set absolute upper and lower quality limits
   1813   rc->worst_quality = cpi->oxcf.worst_allowed_q;
   1814   rc->best_quality = cpi->oxcf.best_allowed_q;
   1815 
   1816   cm->interp_filter = cpi->sf.default_interp_filter;
   1817 
   1818   if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
   1819     cm->render_width = cpi->oxcf.render_width;
   1820     cm->render_height = cpi->oxcf.render_height;
   1821   } else {
   1822     cm->render_width = cpi->oxcf.width;
   1823     cm->render_height = cpi->oxcf.height;
   1824   }
   1825   if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
   1826     cm->width = cpi->oxcf.width;
   1827     cm->height = cpi->oxcf.height;
   1828     cpi->external_resize = 1;
   1829   }
   1830 
   1831   if (cpi->initial_width) {
   1832     int new_mi_size = 0;
   1833     vp9_set_mb_mi(cm, cm->width, cm->height);
   1834     new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
   1835     if (cm->mi_alloc_size < new_mi_size) {
   1836       vp9_free_context_buffers(cm);
   1837       alloc_compressor_data(cpi);
   1838       realloc_segmentation_maps(cpi);
   1839       cpi->initial_width = cpi->initial_height = 0;
   1840       cpi->external_resize = 0;
   1841     } else if (cm->mi_alloc_size == new_mi_size &&
   1842                (cpi->oxcf.width > last_w || cpi->oxcf.height > last_h)) {
   1843       vp9_alloc_loop_filter(cm);
   1844     }
   1845   }
   1846 
   1847   if (cm->current_video_frame == 0 || last_w != cpi->oxcf.width ||
   1848       last_h != cpi->oxcf.height)
   1849     update_frame_size(cpi);
   1850 
   1851   if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
   1852     memset(cpi->consec_zero_mv, 0,
   1853            cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
   1854     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
   1855       vp9_cyclic_refresh_reset_resize(cpi);
   1856     rc->rc_1_frame = 0;
   1857     rc->rc_2_frame = 0;
   1858   }
   1859 
   1860   if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
   1861       ((cpi->svc.number_temporal_layers > 1 ||
   1862         cpi->svc.number_spatial_layers > 1) &&
   1863        cpi->oxcf.pass != 1)) {
   1864     vp9_update_layer_context_change_config(cpi,
   1865                                            (int)cpi->oxcf.target_bandwidth);
   1866   }
   1867 
   1868   // Check for resetting the rc flags (rc_1_frame, rc_2_frame) if the
   1869   // configuration change has a large change in avg_frame_bandwidth.
   1870   // For SVC check for resetting based on spatial layer average bandwidth.
   1871   // Also reset buffer level to optimal level.
   1872   if (cm->current_video_frame > 0) {
   1873     if (cpi->use_svc) {
   1874       vp9_svc_check_reset_layer_rc_flag(cpi);
   1875     } else {
   1876       if (rc->avg_frame_bandwidth > (3 * rc->last_avg_frame_bandwidth >> 1) ||
   1877           rc->avg_frame_bandwidth < (rc->last_avg_frame_bandwidth >> 1)) {
   1878         rc->rc_1_frame = 0;
   1879         rc->rc_2_frame = 0;
   1880         rc->bits_off_target = rc->optimal_buffer_level;
   1881         rc->buffer_level = rc->optimal_buffer_level;
   1882       }
   1883     }
   1884   }
   1885 
   1886   cpi->alt_ref_source = NULL;
   1887   rc->is_src_frame_alt_ref = 0;
   1888 
   1889 #if 0
   1890   // Experimental RD Code
   1891   cpi->frame_distortion = 0;
   1892   cpi->last_frame_distortion = 0;
   1893 #endif
   1894 
   1895   set_tile_limits(cpi);
   1896 
   1897   cpi->ext_refresh_frame_flags_pending = 0;
   1898   cpi->ext_refresh_frame_context_pending = 0;
   1899 
   1900 #if CONFIG_VP9_HIGHBITDEPTH
   1901   highbd_set_var_fns(cpi);
   1902 #endif
   1903 
   1904   vp9_set_row_mt(cpi);
   1905 }
   1906 
   1907 #ifndef M_LOG2_E
   1908 #define M_LOG2_E 0.693147180559945309417
   1909 #endif
   1910 #define log2f(x) (log(x) / (float)M_LOG2_E)
   1911 
   1912 /***********************************************************************
   1913  * Read before modifying 'cal_nmvjointsadcost' or 'cal_nmvsadcosts'    *
   1914  ***********************************************************************
   1915  * The following 2 functions ('cal_nmvjointsadcost' and                *
   1916  * 'cal_nmvsadcosts') are used to calculate cost lookup tables         *
   1917  * used by 'vp9_diamond_search_sad'. The C implementation of the       *
   1918  * function is generic, but the AVX intrinsics optimised version       *
   1919  * relies on the following properties of the computed tables:          *
   1920  * For cal_nmvjointsadcost:                                            *
   1921  *   - mvjointsadcost[1] == mvjointsadcost[2] == mvjointsadcost[3]     *
   1922  * For cal_nmvsadcosts:                                                *
   1923  *   - For all i: mvsadcost[0][i] == mvsadcost[1][i]                   *
   1924  *         (Equal costs for both components)                           *
   1925  *   - For all i: mvsadcost[0][i] == mvsadcost[0][-i]                  *
   1926  *         (Cost function is even)                                     *
   1927  * If these do not hold, then the AVX optimised version of the         *
   1928  * 'vp9_diamond_search_sad' function cannot be used as it is, in which *
   1929  * case you can revert to using the C function instead.                *
   1930  ***********************************************************************/
   1931 
   1932 static void cal_nmvjointsadcost(int *mvjointsadcost) {
   1933   /*********************************************************************
   1934    * Warning: Read the comments above before modifying this function   *
   1935    *********************************************************************/
   1936   mvjointsadcost[0] = 600;
   1937   mvjointsadcost[1] = 300;
   1938   mvjointsadcost[2] = 300;
   1939   mvjointsadcost[3] = 300;
   1940 }
   1941 
   1942 static void cal_nmvsadcosts(int *mvsadcost[2]) {
   1943   /*********************************************************************
   1944    * Warning: Read the comments above before modifying this function   *
   1945    *********************************************************************/
   1946   int i = 1;
   1947 
   1948   mvsadcost[0][0] = 0;
   1949   mvsadcost[1][0] = 0;
   1950 
   1951   do {
   1952     double z = 256 * (2 * (log2f(8 * i) + .6));
   1953     mvsadcost[0][i] = (int)z;
   1954     mvsadcost[1][i] = (int)z;
   1955     mvsadcost[0][-i] = (int)z;
   1956     mvsadcost[1][-i] = (int)z;
   1957   } while (++i <= MV_MAX);
   1958 }
   1959 
   1960 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
   1961   int i = 1;
   1962 
   1963   mvsadcost[0][0] = 0;
   1964   mvsadcost[1][0] = 0;
   1965 
   1966   do {
   1967     double z = 256 * (2 * (log2f(8 * i) + .6));
   1968     mvsadcost[0][i] = (int)z;
   1969     mvsadcost[1][i] = (int)z;
   1970     mvsadcost[0][-i] = (int)z;
   1971     mvsadcost[1][-i] = (int)z;
   1972   } while (++i <= MV_MAX);
   1973 }
   1974 
   1975 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
   1976                                 BufferPool *const pool) {
   1977   unsigned int i;
   1978   VP9_COMP *volatile const cpi = vpx_memalign(32, sizeof(VP9_COMP));
   1979   VP9_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
   1980 
   1981   if (!cm) return NULL;
   1982 
   1983   vp9_zero(*cpi);
   1984 
   1985   if (setjmp(cm->error.jmp)) {
   1986     cm->error.setjmp = 0;
   1987     vp9_remove_compressor(cpi);
   1988     return 0;
   1989   }
   1990 
   1991   cm->error.setjmp = 1;
   1992   cm->alloc_mi = vp9_enc_alloc_mi;
   1993   cm->free_mi = vp9_enc_free_mi;
   1994   cm->setup_mi = vp9_enc_setup_mi;
   1995 
   1996   CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
   1997   CHECK_MEM_ERROR(
   1998       cm, cm->frame_contexts,
   1999       (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
   2000 
   2001   cpi->use_svc = 0;
   2002   cpi->resize_state = ORIG;
   2003   cpi->external_resize = 0;
   2004   cpi->resize_avg_qp = 0;
   2005   cpi->resize_buffer_underflow = 0;
   2006   cpi->use_skin_detection = 0;
   2007   cpi->common.buffer_pool = pool;
   2008 
   2009   cpi->force_update_segmentation = 0;
   2010 
   2011   init_config(cpi, oxcf);
   2012   vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
   2013 
   2014   cm->current_video_frame = 0;
   2015   cpi->partition_search_skippable_frame = 0;
   2016   cpi->tile_data = NULL;
   2017 
   2018   realloc_segmentation_maps(cpi);
   2019 
   2020   CHECK_MEM_ERROR(cm, cpi->skin_map, vpx_calloc(cm->mi_rows * cm->mi_cols,
   2021                                                 sizeof(cpi->skin_map[0])));
   2022 
   2023   CHECK_MEM_ERROR(cm, cpi->alt_ref_aq, vp9_alt_ref_aq_create());
   2024 
   2025   CHECK_MEM_ERROR(
   2026       cm, cpi->consec_zero_mv,
   2027       vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
   2028 
   2029   CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
   2030                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
   2031   CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
   2032                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
   2033   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
   2034                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
   2035   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
   2036                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
   2037   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
   2038                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
   2039   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
   2040                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
   2041   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
   2042                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
   2043   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
   2044                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
   2045 
   2046   for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
   2047        i++) {
   2048     CHECK_MEM_ERROR(
   2049         cm, cpi->mbgraph_stats[i].mb_stats,
   2050         vpx_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
   2051   }
   2052 
   2053 #if CONFIG_FP_MB_STATS
   2054   cpi->use_fp_mb_stats = 0;
   2055   if (cpi->use_fp_mb_stats) {
   2056     // a place holder used to store the first pass mb stats in the first pass
   2057     CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
   2058                     vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
   2059   } else {
   2060     cpi->twopass.frame_mb_stats_buf = NULL;
   2061   }
   2062 #endif
   2063 
   2064   cpi->refresh_alt_ref_frame = 0;
   2065   cpi->multi_arf_last_grp_enabled = 0;
   2066 
   2067   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
   2068 
   2069   init_level_info(&cpi->level_info);
   2070   init_level_constraint(&cpi->level_constraint);
   2071 
   2072 #if CONFIG_INTERNAL_STATS
   2073   cpi->b_calculate_blockiness = 1;
   2074   cpi->b_calculate_consistency = 1;
   2075   cpi->total_inconsistency = 0;
   2076   cpi->psnr.worst = 100.0;
   2077   cpi->worst_ssim = 100.0;
   2078 
   2079   cpi->count = 0;
   2080   cpi->bytes = 0;
   2081 
   2082   if (cpi->b_calculate_psnr) {
   2083     cpi->total_sq_error = 0;
   2084     cpi->total_samples = 0;
   2085 
   2086     cpi->totalp_sq_error = 0;
   2087     cpi->totalp_samples = 0;
   2088 
   2089     cpi->tot_recode_hits = 0;
   2090     cpi->summed_quality = 0;
   2091     cpi->summed_weights = 0;
   2092     cpi->summedp_quality = 0;
   2093     cpi->summedp_weights = 0;
   2094   }
   2095 
   2096   cpi->fastssim.worst = 100.0;
   2097 
   2098   cpi->psnrhvs.worst = 100.0;
   2099 
   2100   if (cpi->b_calculate_blockiness) {
   2101     cpi->total_blockiness = 0;
   2102     cpi->worst_blockiness = 0.0;
   2103   }
   2104 
   2105   if (cpi->b_calculate_consistency) {
   2106     CHECK_MEM_ERROR(cm, cpi->ssim_vars,
   2107                     vpx_malloc(sizeof(*cpi->ssim_vars) * 4 *
   2108                                cpi->common.mi_rows * cpi->common.mi_cols));
   2109     cpi->worst_consistency = 100.0;
   2110   }
   2111 
   2112 #endif
   2113 
   2114   cpi->first_time_stamp_ever = INT64_MAX;
   2115 
   2116   /*********************************************************************
   2117    * Warning: Read the comments around 'cal_nmvjointsadcost' and       *
   2118    * 'cal_nmvsadcosts' before modifying how these tables are computed. *
   2119    *********************************************************************/
   2120   cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
   2121   cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
   2122   cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
   2123   cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
   2124   cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
   2125   cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
   2126 
   2127   cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
   2128   cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
   2129   cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
   2130   cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
   2131   cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
   2132 
   2133 #if CONFIG_VP9_TEMPORAL_DENOISING
   2134 #ifdef OUTPUT_YUV_DENOISED
   2135   yuv_denoised_file = fopen("denoised.yuv", "ab");
   2136 #endif
   2137 #endif
   2138 #ifdef OUTPUT_YUV_SKINMAP
   2139   yuv_skinmap_file = fopen("skinmap.yuv", "wb");
   2140 #endif
   2141 #ifdef OUTPUT_YUV_REC
   2142   yuv_rec_file = fopen("rec.yuv", "wb");
   2143 #endif
   2144 
   2145 #if 0
   2146   framepsnr = fopen("framepsnr.stt", "a");
   2147   kf_list = fopen("kf_list.stt", "w");
   2148 #endif
   2149 
   2150   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
   2151 
   2152 #if !CONFIG_REALTIME_ONLY
   2153   if (oxcf->pass == 1) {
   2154     vp9_init_first_pass(cpi);
   2155   } else if (oxcf->pass == 2) {
   2156     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
   2157     const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
   2158 
   2159     if (cpi->svc.number_spatial_layers > 1 ||
   2160         cpi->svc.number_temporal_layers > 1) {
   2161       FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
   2162       FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = { 0 };
   2163       int i;
   2164 
   2165       for (i = 0; i < oxcf->ss_number_layers; ++i) {
   2166         FIRSTPASS_STATS *const last_packet_for_layer =
   2167             &stats[packets - oxcf->ss_number_layers + i];
   2168         const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
   2169         const int packets_in_layer = (int)last_packet_for_layer->count + 1;
   2170         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
   2171           LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
   2172 
   2173           vpx_free(lc->rc_twopass_stats_in.buf);
   2174 
   2175           lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
   2176           CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
   2177                           vpx_malloc(lc->rc_twopass_stats_in.sz));
   2178           lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
   2179           lc->twopass.stats_in = lc->twopass.stats_in_start;
   2180           lc->twopass.stats_in_end =
   2181               lc->twopass.stats_in_start + packets_in_layer - 1;
   2182           stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
   2183         }
   2184       }
   2185 
   2186       for (i = 0; i < packets; ++i) {
   2187         const int layer_id = (int)stats[i].spatial_layer_id;
   2188         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers &&
   2189             stats_copy[layer_id] != NULL) {
   2190           *stats_copy[layer_id] = stats[i];
   2191           ++stats_copy[layer_id];
   2192         }
   2193       }
   2194 
   2195       vp9_init_second_pass_spatial_svc(cpi);
   2196     } else {
   2197 #if CONFIG_FP_MB_STATS
   2198       if (cpi->use_fp_mb_stats) {
   2199         const size_t psz = cpi->common.MBs * sizeof(uint8_t);
   2200         const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
   2201 
   2202         cpi->twopass.firstpass_mb_stats.mb_stats_start =
   2203             oxcf->firstpass_mb_stats_in.buf;
   2204         cpi->twopass.firstpass_mb_stats.mb_stats_end =
   2205             cpi->twopass.firstpass_mb_stats.mb_stats_start +
   2206             (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
   2207       }
   2208 #endif
   2209 
   2210       cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
   2211       cpi->twopass.stats_in = cpi->twopass.stats_in_start;
   2212       cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
   2213 
   2214       vp9_init_second_pass(cpi);
   2215     }
   2216   }
   2217 #endif  // !CONFIG_REALTIME_ONLY
   2218 
   2219   vp9_set_speed_features_framesize_independent(cpi);
   2220   vp9_set_speed_features_framesize_dependent(cpi);
   2221 
   2222   // Allocate memory to store variances for a frame.
   2223   CHECK_MEM_ERROR(cm, cpi->source_diff_var, vpx_calloc(cm->MBs, sizeof(diff)));
   2224   cpi->source_var_thresh = 0;
   2225   cpi->frames_till_next_var_check = 0;
   2226 
   2227 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF) \
   2228   cpi->fn_ptr[BT].sdf = SDF;                      \
   2229   cpi->fn_ptr[BT].sdaf = SDAF;                    \
   2230   cpi->fn_ptr[BT].vf = VF;                        \
   2231   cpi->fn_ptr[BT].svf = SVF;                      \
   2232   cpi->fn_ptr[BT].svaf = SVAF;                    \
   2233   cpi->fn_ptr[BT].sdx4df = SDX4DF;
   2234 
   2235   BFP(BLOCK_32X16, vpx_sad32x16, vpx_sad32x16_avg, vpx_variance32x16,
   2236       vpx_sub_pixel_variance32x16, vpx_sub_pixel_avg_variance32x16,
   2237       vpx_sad32x16x4d)
   2238 
   2239   BFP(BLOCK_16X32, vpx_sad16x32, vpx_sad16x32_avg, vpx_variance16x32,
   2240       vpx_sub_pixel_variance16x32, vpx_sub_pixel_avg_variance16x32,
   2241       vpx_sad16x32x4d)
   2242 
   2243   BFP(BLOCK_64X32, vpx_sad64x32, vpx_sad64x32_avg, vpx_variance64x32,
   2244       vpx_sub_pixel_variance64x32, vpx_sub_pixel_avg_variance64x32,
   2245       vpx_sad64x32x4d)
   2246 
   2247   BFP(BLOCK_32X64, vpx_sad32x64, vpx_sad32x64_avg, vpx_variance32x64,
   2248       vpx_sub_pixel_variance32x64, vpx_sub_pixel_avg_variance32x64,
   2249       vpx_sad32x64x4d)
   2250 
   2251   BFP(BLOCK_32X32, vpx_sad32x32, vpx_sad32x32_avg, vpx_variance32x32,
   2252       vpx_sub_pixel_variance32x32, vpx_sub_pixel_avg_variance32x32,
   2253       vpx_sad32x32x4d)
   2254 
   2255   BFP(BLOCK_64X64, vpx_sad64x64, vpx_sad64x64_avg, vpx_variance64x64,
   2256       vpx_sub_pixel_variance64x64, vpx_sub_pixel_avg_variance64x64,
   2257       vpx_sad64x64x4d)
   2258 
   2259   BFP(BLOCK_16X16, vpx_sad16x16, vpx_sad16x16_avg, vpx_variance16x16,
   2260       vpx_sub_pixel_variance16x16, vpx_sub_pixel_avg_variance16x16,
   2261       vpx_sad16x16x4d)
   2262 
   2263   BFP(BLOCK_16X8, vpx_sad16x8, vpx_sad16x8_avg, vpx_variance16x8,
   2264       vpx_sub_pixel_variance16x8, vpx_sub_pixel_avg_variance16x8,
   2265       vpx_sad16x8x4d)
   2266 
   2267   BFP(BLOCK_8X16, vpx_sad8x16, vpx_sad8x16_avg, vpx_variance8x16,
   2268       vpx_sub_pixel_variance8x16, vpx_sub_pixel_avg_variance8x16,
   2269       vpx_sad8x16x4d)
   2270 
   2271   BFP(BLOCK_8X8, vpx_sad8x8, vpx_sad8x8_avg, vpx_variance8x8,
   2272       vpx_sub_pixel_variance8x8, vpx_sub_pixel_avg_variance8x8, vpx_sad8x8x4d)
   2273 
   2274   BFP(BLOCK_8X4, vpx_sad8x4, vpx_sad8x4_avg, vpx_variance8x4,
   2275       vpx_sub_pixel_variance8x4, vpx_sub_pixel_avg_variance8x4, vpx_sad8x4x4d)
   2276 
   2277   BFP(BLOCK_4X8, vpx_sad4x8, vpx_sad4x8_avg, vpx_variance4x8,
   2278       vpx_sub_pixel_variance4x8, vpx_sub_pixel_avg_variance4x8, vpx_sad4x8x4d)
   2279 
   2280   BFP(BLOCK_4X4, vpx_sad4x4, vpx_sad4x4_avg, vpx_variance4x4,
   2281       vpx_sub_pixel_variance4x4, vpx_sub_pixel_avg_variance4x4, vpx_sad4x4x4d)
   2282 
   2283 #if CONFIG_VP9_HIGHBITDEPTH
   2284   highbd_set_var_fns(cpi);
   2285 #endif
   2286 
   2287   /* vp9_init_quantizer() is first called here. Add check in
   2288    * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
   2289    * called later when needed. This will avoid unnecessary calls of
   2290    * vp9_init_quantizer() for every frame.
   2291    */
   2292   vp9_init_quantizer(cpi);
   2293 
   2294   vp9_loop_filter_init(cm);
   2295 
   2296   cm->error.setjmp = 0;
   2297 
   2298   return cpi;
   2299 }
   2300 
   2301 #if CONFIG_INTERNAL_STATS
   2302 #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
   2303 
   2304 #define SNPRINT2(H, T, V) \
   2305   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
   2306 #endif  // CONFIG_INTERNAL_STATS
   2307 
   2308 void vp9_remove_compressor(VP9_COMP *cpi) {
   2309   VP9_COMMON *cm;
   2310   unsigned int i;
   2311   int t;
   2312 
   2313   if (!cpi) return;
   2314 
   2315   cm = &cpi->common;
   2316   if (cm->current_video_frame > 0) {
   2317 #if CONFIG_INTERNAL_STATS
   2318     vpx_clear_system_state();
   2319 
   2320     if (cpi->oxcf.pass != 1) {
   2321       char headings[512] = { 0 };
   2322       char results[512] = { 0 };
   2323       FILE *f = fopen("opsnr.stt", "a");
   2324       double time_encoded =
   2325           (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
   2326           10000000.000;
   2327       double total_encode_time =
   2328           (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
   2329       const double dr =
   2330           (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
   2331       const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
   2332       const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
   2333       const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
   2334 
   2335       if (cpi->b_calculate_psnr) {
   2336         const double total_psnr = vpx_sse_to_psnr(
   2337             (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
   2338         const double totalp_psnr = vpx_sse_to_psnr(
   2339             (double)cpi->totalp_samples, peak, (double)cpi->totalp_sq_error);
   2340         const double total_ssim =
   2341             100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
   2342         const double totalp_ssim =
   2343             100 * pow(cpi->summedp_quality / cpi->summedp_weights, 8.0);
   2344 
   2345         snprintf(headings, sizeof(headings),
   2346                  "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
   2347                  "VPXSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
   2348                  "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
   2349                  "AVPsnrY\tAPsnrCb\tAPsnrCr");
   2350         snprintf(results, sizeof(results),
   2351                  "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
   2352                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
   2353                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
   2354                  "%7.3f\t%7.3f\t%7.3f",
   2355                  dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
   2356                  cpi->psnrp.stat[ALL] / cpi->count, totalp_psnr, total_ssim,
   2357                  totalp_ssim, cpi->fastssim.stat[ALL] / cpi->count,
   2358                  cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
   2359                  cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
   2360                  cpi->psnr.stat[Y] / cpi->count, cpi->psnr.stat[U] / cpi->count,
   2361                  cpi->psnr.stat[V] / cpi->count);
   2362 
   2363         if (cpi->b_calculate_blockiness) {
   2364           SNPRINT(headings, "\t  Block\tWstBlck");
   2365           SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
   2366           SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
   2367         }
   2368 
   2369         if (cpi->b_calculate_consistency) {
   2370           double consistency =
   2371               vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
   2372                               (double)cpi->total_inconsistency);
   2373 
   2374           SNPRINT(headings, "\tConsist\tWstCons");
   2375           SNPRINT2(results, "\t%7.3f", consistency);
   2376           SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
   2377         }
   2378 
   2379         fprintf(f, "%s\t    Time\tRcErr\tAbsErr\n", headings);
   2380         fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
   2381                 rate_err, fabs(rate_err));
   2382       }
   2383 
   2384       fclose(f);
   2385     }
   2386 
   2387 #endif
   2388 
   2389 #if 0
   2390     {
   2391       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
   2392       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
   2393       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
   2394              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
   2395              cpi->time_compress_data / 1000,
   2396              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
   2397     }
   2398 #endif
   2399   }
   2400 
   2401 #if CONFIG_VP9_TEMPORAL_DENOISING
   2402   vp9_denoiser_free(&(cpi->denoiser));
   2403 #endif
   2404 
   2405   for (t = 0; t < cpi->num_workers; ++t) {
   2406     VPxWorker *const worker = &cpi->workers[t];
   2407     EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
   2408 
   2409     // Deallocate allocated threads.
   2410     vpx_get_worker_interface()->end(worker);
   2411 
   2412     // Deallocate allocated thread data.
   2413     if (t < cpi->num_workers - 1) {
   2414       vpx_free(thread_data->td->counts);
   2415       vp9_free_pc_tree(thread_data->td);
   2416       vpx_free(thread_data->td);
   2417     }
   2418   }
   2419   vpx_free(cpi->tile_thr_data);
   2420   vpx_free(cpi->workers);
   2421   vp9_row_mt_mem_dealloc(cpi);
   2422 
   2423   if (cpi->num_workers > 1) {
   2424     vp9_loop_filter_dealloc(&cpi->lf_row_sync);
   2425     vp9_bitstream_encode_tiles_buffer_dealloc(cpi);
   2426   }
   2427 
   2428   vp9_alt_ref_aq_destroy(cpi->alt_ref_aq);
   2429 
   2430   dealloc_compressor_data(cpi);
   2431 
   2432   for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
   2433        ++i) {
   2434     vpx_free(cpi->mbgraph_stats[i].mb_stats);
   2435   }
   2436 
   2437 #if CONFIG_FP_MB_STATS
   2438   if (cpi->use_fp_mb_stats) {
   2439     vpx_free(cpi->twopass.frame_mb_stats_buf);
   2440     cpi->twopass.frame_mb_stats_buf = NULL;
   2441   }
   2442 #endif
   2443 
   2444   vp9_remove_common(cm);
   2445   vp9_free_ref_frame_buffers(cm->buffer_pool);
   2446 #if CONFIG_VP9_POSTPROC
   2447   vp9_free_postproc_buffers(cm);
   2448 #endif
   2449   vpx_free(cpi);
   2450 
   2451 #if CONFIG_VP9_TEMPORAL_DENOISING
   2452 #ifdef OUTPUT_YUV_DENOISED
   2453   fclose(yuv_denoised_file);
   2454 #endif
   2455 #endif
   2456 #ifdef OUTPUT_YUV_SKINMAP
   2457   fclose(yuv_skinmap_file);
   2458 #endif
   2459 #ifdef OUTPUT_YUV_REC
   2460   fclose(yuv_rec_file);
   2461 #endif
   2462 
   2463 #if 0
   2464 
   2465   if (keyfile)
   2466     fclose(keyfile);
   2467 
   2468   if (framepsnr)
   2469     fclose(framepsnr);
   2470 
   2471   if (kf_list)
   2472     fclose(kf_list);
   2473 
   2474 #endif
   2475 }
   2476 
   2477 static void generate_psnr_packet(VP9_COMP *cpi) {
   2478   struct vpx_codec_cx_pkt pkt;
   2479   int i;
   2480   PSNR_STATS psnr;
   2481 #if CONFIG_VP9_HIGHBITDEPTH
   2482   vpx_calc_highbd_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr,
   2483                        cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
   2484 #else
   2485   vpx_calc_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr);
   2486 #endif
   2487 
   2488   for (i = 0; i < 4; ++i) {
   2489     pkt.data.psnr.samples[i] = psnr.samples[i];
   2490     pkt.data.psnr.sse[i] = psnr.sse[i];
   2491     pkt.data.psnr.psnr[i] = psnr.psnr[i];
   2492   }
   2493   pkt.kind = VPX_CODEC_PSNR_PKT;
   2494   if (cpi->use_svc)
   2495     cpi->svc
   2496         .layer_context[cpi->svc.spatial_layer_id *
   2497                        cpi->svc.number_temporal_layers]
   2498         .psnr_pkt = pkt.data.psnr;
   2499   else
   2500     vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
   2501 }
   2502 
   2503 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
   2504   if (ref_frame_flags > 7) return -1;
   2505 
   2506   cpi->ref_frame_flags = ref_frame_flags;
   2507   return 0;
   2508 }
   2509 
   2510 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
   2511   cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
   2512   cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
   2513   cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
   2514   cpi->ext_refresh_frame_flags_pending = 1;
   2515 }
   2516 
   2517 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(
   2518     VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag) {
   2519   MV_REFERENCE_FRAME ref_frame = NONE;
   2520   if (ref_frame_flag == VP9_LAST_FLAG)
   2521     ref_frame = LAST_FRAME;
   2522   else if (ref_frame_flag == VP9_GOLD_FLAG)
   2523     ref_frame = GOLDEN_FRAME;
   2524   else if (ref_frame_flag == VP9_ALT_FLAG)
   2525     ref_frame = ALTREF_FRAME;
   2526 
   2527   return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
   2528 }
   2529 
   2530 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
   2531                            YV12_BUFFER_CONFIG *sd) {
   2532   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
   2533   if (cfg) {
   2534     vpx_yv12_copy_frame(cfg, sd);
   2535     return 0;
   2536   } else {
   2537     return -1;
   2538   }
   2539 }
   2540 
   2541 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
   2542                           YV12_BUFFER_CONFIG *sd) {
   2543   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
   2544   if (cfg) {
   2545     vpx_yv12_copy_frame(sd, cfg);
   2546     return 0;
   2547   } else {
   2548     return -1;
   2549   }
   2550 }
   2551 
   2552 int vp9_update_entropy(VP9_COMP *cpi, int update) {
   2553   cpi->ext_refresh_frame_context = update;
   2554   cpi->ext_refresh_frame_context_pending = 1;
   2555   return 0;
   2556 }
   2557 
   2558 #ifdef OUTPUT_YUV_REC
   2559 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
   2560   YV12_BUFFER_CONFIG *s = cm->frame_to_show;
   2561   uint8_t *src = s->y_buffer;
   2562   int h = cm->height;
   2563 
   2564 #if CONFIG_VP9_HIGHBITDEPTH
   2565   if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
   2566     uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
   2567 
   2568     do {
   2569       fwrite(src16, s->y_width, 2, yuv_rec_file);
   2570       src16 += s->y_stride;
   2571     } while (--h);
   2572 
   2573     src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
   2574     h = s->uv_height;
   2575 
   2576     do {
   2577       fwrite(src16, s->uv_width, 2, yuv_rec_file);
   2578       src16 += s->uv_stride;
   2579     } while (--h);
   2580 
   2581     src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
   2582     h = s->uv_height;
   2583 
   2584     do {
   2585       fwrite(src16, s->uv_width, 2, yuv_rec_file);
   2586       src16 += s->uv_stride;
   2587     } while (--h);
   2588 
   2589     fflush(yuv_rec_file);
   2590     return;
   2591   }
   2592 #endif  // CONFIG_VP9_HIGHBITDEPTH
   2593 
   2594   do {
   2595     fwrite(src, s->y_width, 1, yuv_rec_file);
   2596     src += s->y_stride;
   2597   } while (--h);
   2598 
   2599   src = s->u_buffer;
   2600   h = s->uv_height;
   2601 
   2602   do {
   2603     fwrite(src, s->uv_width, 1, yuv_rec_file);
   2604     src += s->uv_stride;
   2605   } while (--h);
   2606 
   2607   src = s->v_buffer;
   2608   h = s->uv_height;
   2609 
   2610   do {
   2611     fwrite(src, s->uv_width, 1, yuv_rec_file);
   2612     src += s->uv_stride;
   2613   } while (--h);
   2614 
   2615   fflush(yuv_rec_file);
   2616 }
   2617 #endif
   2618 
   2619 #if CONFIG_VP9_HIGHBITDEPTH
   2620 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
   2621                                                 YV12_BUFFER_CONFIG *dst,
   2622                                                 int bd) {
   2623 #else
   2624 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
   2625                                                 YV12_BUFFER_CONFIG *dst) {
   2626 #endif  // CONFIG_VP9_HIGHBITDEPTH
   2627   // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
   2628   int i;
   2629   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
   2630                                    src->v_buffer };
   2631   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
   2632   const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
   2633                               src->uv_crop_width };
   2634   const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
   2635                                src->uv_crop_height };
   2636   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
   2637   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
   2638   const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
   2639                               dst->uv_crop_width };
   2640   const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
   2641                                dst->uv_crop_height };
   2642 
   2643   for (i = 0; i < MAX_MB_PLANE; ++i) {
   2644 #if CONFIG_VP9_HIGHBITDEPTH
   2645     if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
   2646       vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
   2647                               src_strides[i], dsts[i], dst_heights[i],
   2648                               dst_widths[i], dst_strides[i], bd);
   2649     } else {
   2650       vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
   2651                        dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
   2652     }
   2653 #else
   2654     vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
   2655                      dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
   2656 #endif  // CONFIG_VP9_HIGHBITDEPTH
   2657   }
   2658   vpx_extend_frame_borders(dst);
   2659 }
   2660 
   2661 #if CONFIG_VP9_HIGHBITDEPTH
   2662 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
   2663                                    YV12_BUFFER_CONFIG *dst, int bd,
   2664                                    INTERP_FILTER filter_type,
   2665                                    int phase_scaler) {
   2666   const int src_w = src->y_crop_width;
   2667   const int src_h = src->y_crop_height;
   2668   const int dst_w = dst->y_crop_width;
   2669   const int dst_h = dst->y_crop_height;
   2670   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
   2671                                    src->v_buffer };
   2672   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
   2673   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
   2674   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
   2675   const InterpKernel *const kernel = vp9_filter_kernels[filter_type];
   2676   int x, y, i;
   2677 
   2678   for (i = 0; i < MAX_MB_PLANE; ++i) {
   2679     const int factor = (i == 0 || i == 3 ? 1 : 2);
   2680     const int src_stride = src_strides[i];
   2681     const int dst_stride = dst_strides[i];
   2682     for (y = 0; y < dst_h; y += 16) {
   2683       const int y_q4 = y * (16 / factor) * src_h / dst_h + phase_scaler;
   2684       for (x = 0; x < dst_w; x += 16) {
   2685         const int x_q4 = x * (16 / factor) * src_w / dst_w + phase_scaler;
   2686         const uint8_t *src_ptr = srcs[i] +
   2687                                  (y / factor) * src_h / dst_h * src_stride +
   2688                                  (x / factor) * src_w / dst_w;
   2689         uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
   2690 
   2691         if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
   2692           vpx_highbd_convolve8(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
   2693                                CONVERT_TO_SHORTPTR(dst_ptr), dst_stride, kernel,
   2694                                x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
   2695                                16 * src_h / dst_h, 16 / factor, 16 / factor,
   2696                                bd);
   2697         } else {
   2698           vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
   2699                         x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
   2700                         16 * src_h / dst_h, 16 / factor, 16 / factor);
   2701         }
   2702       }
   2703     }
   2704   }
   2705 
   2706   vpx_extend_frame_borders(dst);
   2707 }
   2708 #endif  // CONFIG_VP9_HIGHBITDEPTH
   2709 
   2710 static int scale_down(VP9_COMP *cpi, int q) {
   2711   RATE_CONTROL *const rc = &cpi->rc;
   2712   GF_GROUP *const gf_group = &cpi->twopass.gf_group;
   2713   int scale = 0;
   2714   assert(frame_is_kf_gf_arf(cpi));
   2715 
   2716   if (rc->frame_size_selector == UNSCALED &&
   2717       q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
   2718     const int max_size_thresh =
   2719         (int)(rate_thresh_mult[SCALE_STEP1] *
   2720               VPXMAX(rc->this_frame_target, rc->avg_frame_bandwidth));
   2721     scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
   2722   }
   2723   return scale;
   2724 }
   2725 
   2726 static int big_rate_miss_high_threshold(VP9_COMP *cpi) {
   2727   const RATE_CONTROL *const rc = &cpi->rc;
   2728   int big_miss_high;
   2729 
   2730   if (frame_is_kf_gf_arf(cpi))
   2731     big_miss_high = rc->this_frame_target * 3 / 2;
   2732   else
   2733     big_miss_high = rc->this_frame_target * 2;
   2734 
   2735   return big_miss_high;
   2736 }
   2737 
   2738 static int big_rate_miss(VP9_COMP *cpi) {
   2739   const RATE_CONTROL *const rc = &cpi->rc;
   2740   int big_miss_high;
   2741   int big_miss_low;
   2742 
   2743   // Ignore for overlay frames
   2744   if (rc->is_src_frame_alt_ref) {
   2745     return 0;
   2746   } else {
   2747     big_miss_low = (rc->this_frame_target / 2);
   2748     big_miss_high = big_rate_miss_high_threshold(cpi);
   2749 
   2750     return (rc->projected_frame_size > big_miss_high) ||
   2751            (rc->projected_frame_size < big_miss_low);
   2752   }
   2753 }
   2754 
   2755 // test in two pass for the first
   2756 static int two_pass_first_group_inter(VP9_COMP *cpi) {
   2757   TWO_PASS *const twopass = &cpi->twopass;
   2758   GF_GROUP *const gf_group = &twopass->gf_group;
   2759   if ((cpi->oxcf.pass == 2) &&
   2760       (gf_group->index == gf_group->first_inter_index)) {
   2761     return 1;
   2762   } else {
   2763     return 0;
   2764   }
   2765 }
   2766 
   2767 // Function to test for conditions that indicate we should loop
   2768 // back and recode a frame.
   2769 static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
   2770                             int maxq, int minq) {
   2771   const RATE_CONTROL *const rc = &cpi->rc;
   2772   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
   2773   const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
   2774   int force_recode = 0;
   2775 
   2776   if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
   2777       big_rate_miss(cpi) || (cpi->sf.recode_loop == ALLOW_RECODE) ||
   2778       (two_pass_first_group_inter(cpi) &&
   2779        (cpi->sf.recode_loop == ALLOW_RECODE_FIRST)) ||
   2780       (frame_is_kfgfarf && (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF))) {
   2781     if (frame_is_kfgfarf && (oxcf->resize_mode == RESIZE_DYNAMIC) &&
   2782         scale_down(cpi, q)) {
   2783       // Code this group at a lower resolution.
   2784       cpi->resize_pending = 1;
   2785       return 1;
   2786     }
   2787 
   2788     // Force recode for extreme overshoot.
   2789     if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
   2790         (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
   2791          rc->projected_frame_size >= big_rate_miss_high_threshold(cpi))) {
   2792       return 1;
   2793     }
   2794 
   2795     // TODO(agrange) high_limit could be greater than the scale-down threshold.
   2796     if ((rc->projected_frame_size > high_limit && q < maxq) ||
   2797         (rc->projected_frame_size < low_limit && q > minq)) {
   2798       force_recode = 1;
   2799     } else if (cpi->oxcf.rc_mode == VPX_CQ) {
   2800       // Deal with frame undershoot and whether or not we are
   2801       // below the automatically set cq level.
   2802       if (q > oxcf->cq_level &&
   2803           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
   2804         force_recode = 1;
   2805       }
   2806     }
   2807   }
   2808   return force_recode;
   2809 }
   2810 
   2811 void vp9_update_reference_frames(VP9_COMP *cpi) {
   2812   VP9_COMMON *const cm = &cpi->common;
   2813   BufferPool *const pool = cm->buffer_pool;
   2814 
   2815   // At this point the new frame has been encoded.
   2816   // If any buffer copy / swapping is signaled it should be done here.
   2817   if (cm->frame_type == KEY_FRAME) {
   2818     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
   2819                cm->new_fb_idx);
   2820     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
   2821                cm->new_fb_idx);
   2822   } else if (vp9_preserve_existing_gf(cpi)) {
   2823     // We have decided to preserve the previously existing golden frame as our
   2824     // new ARF frame. However, in the short term in function
   2825     // vp9_get_refresh_mask() we left it in the GF slot and, if
   2826     // we're updating the GF with the current decoded frame, we save it to the
   2827     // ARF slot instead.
   2828     // We now have to update the ARF with the current frame and swap gld_fb_idx
   2829     // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
   2830     // slot and, if we're updating the GF, the current frame becomes the new GF.
   2831     int tmp;
   2832 
   2833     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
   2834                cm->new_fb_idx);
   2835 
   2836     tmp = cpi->alt_fb_idx;
   2837     cpi->alt_fb_idx = cpi->gld_fb_idx;
   2838     cpi->gld_fb_idx = tmp;
   2839 
   2840     if (is_two_pass_svc(cpi)) {
   2841       cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
   2842       cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
   2843     }
   2844   } else { /* For non key/golden frames */
   2845     if (cpi->refresh_alt_ref_frame) {
   2846       int arf_idx = cpi->alt_fb_idx;
   2847       if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
   2848         const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
   2849         arf_idx = gf_group->arf_update_idx[gf_group->index];
   2850       }
   2851 
   2852       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
   2853       memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
   2854              cpi->interp_filter_selected[0],
   2855              sizeof(cpi->interp_filter_selected[0]));
   2856     }
   2857 
   2858     if (cpi->refresh_golden_frame) {
   2859       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
   2860                  cm->new_fb_idx);
   2861       if (!cpi->rc.is_src_frame_alt_ref)
   2862         memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
   2863                cpi->interp_filter_selected[0],
   2864                sizeof(cpi->interp_filter_selected[0]));
   2865       else
   2866         memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
   2867                cpi->interp_filter_selected[ALTREF_FRAME],
   2868                sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
   2869     }
   2870   }
   2871 
   2872   if (cpi->refresh_last_frame) {
   2873     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
   2874                cm->new_fb_idx);
   2875     if (!cpi->rc.is_src_frame_alt_ref)
   2876       memcpy(cpi->interp_filter_selected[LAST_FRAME],
   2877              cpi->interp_filter_selected[0],
   2878              sizeof(cpi->interp_filter_selected[0]));
   2879   }
   2880 #if CONFIG_VP9_TEMPORAL_DENOISING
   2881   if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
   2882       cpi->denoiser.denoising_level > kDenLowLow) {
   2883     int svc_base_is_key = 0;
   2884     int denoise_svc_second_layer = 0;
   2885     if (cpi->use_svc) {
   2886       int realloc_fail = 0;
   2887       const int svc_buf_shift =
   2888           cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id == 2
   2889               ? cpi->denoiser.num_ref_frames
   2890               : 0;
   2891       int layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
   2892                                    cpi->svc.temporal_layer_id,
   2893                                    cpi->svc.number_temporal_layers);
   2894       LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
   2895       svc_base_is_key = lc->is_key_frame;
   2896       denoise_svc_second_layer =
   2897           cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id == 2 ? 1
   2898                                                                           : 0;
   2899       // Check if we need to allocate extra buffers in the denoiser
   2900       // for
   2901       // refreshed frames.
   2902       realloc_fail = vp9_denoiser_realloc_svc(
   2903           cm, &cpi->denoiser, svc_buf_shift, cpi->refresh_alt_ref_frame,
   2904           cpi->refresh_golden_frame, cpi->refresh_last_frame, cpi->alt_fb_idx,
   2905           cpi->gld_fb_idx, cpi->lst_fb_idx);
   2906       if (realloc_fail)
   2907         vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   2908                            "Failed to re-allocate denoiser for SVC");
   2909     }
   2910     vp9_denoiser_update_frame_info(
   2911         &cpi->denoiser, *cpi->Source, cpi->common.frame_type,
   2912         cpi->refresh_alt_ref_frame, cpi->refresh_golden_frame,
   2913         cpi->refresh_last_frame, cpi->alt_fb_idx, cpi->gld_fb_idx,
   2914         cpi->lst_fb_idx, cpi->resize_pending, svc_base_is_key,
   2915         denoise_svc_second_layer);
   2916   }
   2917 #endif
   2918   if (is_one_pass_cbr_svc(cpi)) {
   2919     // Keep track of frame index for each reference frame.
   2920     SVC *const svc = &cpi->svc;
   2921     if (cm->frame_type == KEY_FRAME) {
   2922       svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
   2923       svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
   2924       svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
   2925     } else {
   2926       if (cpi->refresh_last_frame)
   2927         svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
   2928       if (cpi->refresh_golden_frame)
   2929         svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
   2930       if (cpi->refresh_alt_ref_frame)
   2931         svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
   2932     }
   2933   }
   2934 }
   2935 
   2936 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
   2937   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
   2938   struct loopfilter *lf = &cm->lf;
   2939 
   2940   const int is_reference_frame =
   2941       (cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
   2942        cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
   2943 
   2944   if (xd->lossless) {
   2945     lf->filter_level = 0;
   2946     lf->last_filt_level = 0;
   2947   } else {
   2948     struct vpx_usec_timer timer;
   2949 
   2950     vpx_clear_system_state();
   2951 
   2952     vpx_usec_timer_start(&timer);
   2953 
   2954     if (!cpi->rc.is_src_frame_alt_ref) {
   2955       if ((cpi->common.frame_type == KEY_FRAME) &&
   2956           (!cpi->rc.this_key_frame_forced)) {
   2957         lf->last_filt_level = 0;
   2958       }
   2959       vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
   2960       lf->last_filt_level = lf->filter_level;
   2961     } else {
   2962       lf->filter_level = 0;
   2963     }
   2964 
   2965     vpx_usec_timer_mark(&timer);
   2966     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
   2967   }
   2968 
   2969   if (lf->filter_level > 0 && is_reference_frame) {
   2970     vp9_build_mask_frame(cm, lf->filter_level, 0);
   2971 
   2972     if (cpi->num_workers > 1)
   2973       vp9_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
   2974                                lf->filter_level, 0, 0, cpi->workers,
   2975                                cpi->num_workers, &cpi->lf_row_sync);
   2976     else
   2977       vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
   2978   }
   2979 
   2980   vpx_extend_frame_inner_borders(cm->frame_to_show);
   2981 }
   2982 
   2983 static INLINE void alloc_frame_mvs(VP9_COMMON *const cm, int buffer_idx) {
   2984   RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
   2985   if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
   2986       new_fb_ptr->mi_cols < cm->mi_cols) {
   2987     vpx_free(new_fb_ptr->mvs);
   2988     CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
   2989                     (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
   2990                                          sizeof(*new_fb_ptr->mvs)));
   2991     new_fb_ptr->mi_rows = cm->mi_rows;
   2992     new_fb_ptr->mi_cols = cm->mi_cols;
   2993   }
   2994 }
   2995 
   2996 void vp9_scale_references(VP9_COMP *cpi) {
   2997   VP9_COMMON *cm = &cpi->common;
   2998   MV_REFERENCE_FRAME ref_frame;
   2999   const VP9_REFFRAME ref_mask[3] = { VP9_LAST_FLAG, VP9_GOLD_FLAG,
   3000                                      VP9_ALT_FLAG };
   3001 
   3002   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
   3003     // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
   3004     if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
   3005       BufferPool *const pool = cm->buffer_pool;
   3006       const YV12_BUFFER_CONFIG *const ref =
   3007           get_ref_frame_buffer(cpi, ref_frame);
   3008 
   3009       if (ref == NULL) {
   3010         cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
   3011         continue;
   3012       }
   3013 
   3014 #if CONFIG_VP9_HIGHBITDEPTH
   3015       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
   3016         RefCntBuffer *new_fb_ptr = NULL;
   3017         int force_scaling = 0;
   3018         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
   3019         if (new_fb == INVALID_IDX) {
   3020           new_fb = get_free_fb(cm);
   3021           force_scaling = 1;
   3022         }
   3023         if (new_fb == INVALID_IDX) return;
   3024         new_fb_ptr = &pool->frame_bufs[new_fb];
   3025         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
   3026             new_fb_ptr->buf.y_crop_height != cm->height) {
   3027           if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
   3028                                        cm->subsampling_x, cm->subsampling_y,
   3029                                        cm->use_highbitdepth,
   3030                                        VP9_ENC_BORDER_IN_PIXELS,
   3031                                        cm->byte_alignment, NULL, NULL, NULL))
   3032             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   3033                                "Failed to allocate frame buffer");
   3034           scale_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth,
   3035                                  EIGHTTAP, 0);
   3036           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
   3037           alloc_frame_mvs(cm, new_fb);
   3038         }
   3039 #else
   3040       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
   3041         RefCntBuffer *new_fb_ptr = NULL;
   3042         int force_scaling = 0;
   3043         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
   3044         if (new_fb == INVALID_IDX) {
   3045           new_fb = get_free_fb(cm);
   3046           force_scaling = 1;
   3047         }
   3048         if (new_fb == INVALID_IDX) return;
   3049         new_fb_ptr = &pool->frame_bufs[new_fb];
   3050         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
   3051             new_fb_ptr->buf.y_crop_height != cm->height) {
   3052           if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
   3053                                        cm->subsampling_x, cm->subsampling_y,
   3054                                        VP9_ENC_BORDER_IN_PIXELS,
   3055                                        cm->byte_alignment, NULL, NULL, NULL))
   3056             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   3057                                "Failed to allocate frame buffer");
   3058           vp9_scale_and_extend_frame(ref, &new_fb_ptr->buf, EIGHTTAP, 0);
   3059           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
   3060           alloc_frame_mvs(cm, new_fb);
   3061         }
   3062 #endif  // CONFIG_VP9_HIGHBITDEPTH
   3063       } else {
   3064         int buf_idx;
   3065         RefCntBuffer *buf = NULL;
   3066         if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
   3067           // Check for release of scaled reference.
   3068           buf_idx = cpi->scaled_ref_idx[ref_frame - 1];
   3069           buf = (buf_idx != INVALID_IDX) ? &pool->frame_bufs[buf_idx] : NULL;
   3070           if (buf != NULL) {
   3071             --buf->ref_count;
   3072             cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
   3073           }
   3074         }
   3075         buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
   3076         buf = &pool->frame_bufs[buf_idx];
   3077         buf->buf.y_crop_width = ref->y_crop_width;
   3078         buf->buf.y_crop_height = ref->y_crop_height;
   3079         cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
   3080         ++buf->ref_count;
   3081       }
   3082     } else {
   3083       if (cpi->oxcf.pass != 0 || cpi->use_svc)
   3084         cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
   3085     }
   3086   }
   3087 }
   3088 
   3089 static void release_scaled_references(VP9_COMP *cpi) {
   3090   VP9_COMMON *cm = &cpi->common;
   3091   int i;
   3092   if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
   3093     // Only release scaled references under certain conditions:
   3094     // if reference will be updated, or if scaled reference has same resolution.
   3095     int refresh[3];
   3096     refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
   3097     refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
   3098     refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
   3099     for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
   3100       const int idx = cpi->scaled_ref_idx[i - 1];
   3101       RefCntBuffer *const buf =
   3102           idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
   3103       const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
   3104       if (buf != NULL &&
   3105           (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
   3106                               buf->buf.y_crop_height == ref->y_crop_height))) {
   3107         --buf->ref_count;
   3108         cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
   3109       }
   3110     }
   3111   } else {
   3112     for (i = 0; i < MAX_REF_FRAMES; ++i) {
   3113       const int idx = cpi->scaled_ref_idx[i];
   3114       RefCntBuffer *const buf =
   3115           idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
   3116       if (buf != NULL) {
   3117         --buf->ref_count;
   3118         cpi->scaled_ref_idx[i] = INVALID_IDX;
   3119       }
   3120     }
   3121   }
   3122 }
   3123 
   3124 static void full_to_model_count(unsigned int *model_count,
   3125                                 unsigned int *full_count) {
   3126   int n;
   3127   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
   3128   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
   3129   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
   3130   for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
   3131     model_count[TWO_TOKEN] += full_count[n];
   3132   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
   3133 }
   3134 
   3135 static void full_to_model_counts(vp9_coeff_count_model *model_count,
   3136                                  vp9_coeff_count *full_count) {
   3137   int i, j, k, l;
   3138 
   3139   for (i = 0; i < PLANE_TYPES; ++i)
   3140     for (j = 0; j < REF_TYPES; ++j)
   3141       for (k = 0; k < COEF_BANDS; ++k)
   3142         for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
   3143           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
   3144 }
   3145 
   3146 #if 0 && CONFIG_INTERNAL_STATS
   3147 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
   3148   VP9_COMMON *const cm = &cpi->common;
   3149   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
   3150   int64_t recon_err;
   3151 
   3152   vpx_clear_system_state();
   3153 
   3154 #if CONFIG_VP9_HIGHBITDEPTH
   3155   if (cm->use_highbitdepth) {
   3156     recon_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
   3157   } else {
   3158     recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
   3159   }
   3160 #else
   3161   recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
   3162 #endif  // CONFIG_VP9_HIGHBITDEPTH
   3163 
   3164 
   3165   if (cpi->twopass.total_left_stats.coded_error != 0.0) {
   3166     double dc_quant_devisor;
   3167 #if CONFIG_VP9_HIGHBITDEPTH
   3168     switch (cm->bit_depth) {
   3169       case VPX_BITS_8:
   3170         dc_quant_devisor = 4.0;
   3171         break;
   3172       case VPX_BITS_10:
   3173         dc_quant_devisor = 16.0;
   3174         break;
   3175       case VPX_BITS_12:
   3176         dc_quant_devisor = 64.0;
   3177         break;
   3178       default:
   3179         assert(0 && "bit_depth must be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
   3180         break;
   3181     }
   3182 #else
   3183     dc_quant_devisor = 4.0;
   3184 #endif
   3185 
   3186     if (!cm->current_video_frame) {
   3187       fprintf(f, "frame, width, height, last ts, last end ts, "
   3188           "source_alt_ref_pending, source_alt_ref_active, "
   3189           "this_frame_target, projected_frame_size, "
   3190           "projected_frame_size / MBs, "
   3191           "projected_frame_size - this_frame_target, "
   3192           "vbr_bits_off_target, vbr_bits_off_target_fast, "
   3193           "twopass.extend_minq, twopass.extend_minq_fast, "
   3194           "total_target_vs_actual, "
   3195           "starting_buffer_level - bits_off_target, "
   3196           "total_actual_bits, base_qindex, q for base_qindex, "
   3197           "dc quant, q for active_worst_quality, avg_q, q for oxcf.cq_level, "
   3198           "refresh_last_frame, refresh_golden_frame, refresh_alt_ref_frame, "
   3199           "frame_type, gfu_boost, "
   3200           "twopass.bits_left, "
   3201           "twopass.total_left_stats.coded_error, "
   3202           "twopass.bits_left / (1 + twopass.total_left_stats.coded_error), "
   3203           "tot_recode_hits, recon_err, kf_boost, "
   3204           "twopass.kf_zeromotion_pct, twopass.fr_content_type, "
   3205           "filter_level, seg.aq_av_offset\n");
   3206     }
   3207 
   3208     fprintf(f, "%10u, %d, %d, %10"PRId64", %10"PRId64", %d, %d, %10d, %10d, "
   3209         "%10d, %10d, %10"PRId64", %10"PRId64", %5d, %5d, %10"PRId64", "
   3210         "%10"PRId64", %10"PRId64", %10d, %7.2lf, %7.2lf, %7.2lf, %7.2lf, "
   3211         "%7.2lf, %6d, %6d, %5d, %5d, %5d, %10"PRId64", %10.3lf, %10lf, %8u, "
   3212         "%10"PRId64", %10d, %10d, %10d, %10d, %10d\n",
   3213         cpi->common.current_video_frame,
   3214         cm->width, cm->height,
   3215         cpi->last_time_stamp_seen,
   3216         cpi->last_end_time_stamp_seen,
   3217         cpi->rc.source_alt_ref_pending,
   3218         cpi->rc.source_alt_ref_active,
   3219         cpi->rc.this_frame_target,
   3220         cpi->rc.projected_frame_size,
   3221         cpi->rc.projected_frame_size / cpi->common.MBs,
   3222         (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
   3223         cpi->rc.vbr_bits_off_target,
   3224         cpi->rc.vbr_bits_off_target_fast,
   3225         cpi->twopass.extend_minq,
   3226         cpi->twopass.extend_minq_fast,
   3227         cpi->rc.total_target_vs_actual,
   3228         (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
   3229         cpi->rc.total_actual_bits, cm->base_qindex,
   3230         vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
   3231         (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) /
   3232             dc_quant_devisor,
   3233         vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
   3234                                 cm->bit_depth),
   3235         cpi->rc.avg_q,
   3236         vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
   3237         cpi->refresh_last_frame, cpi->refresh_golden_frame,
   3238         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
   3239         cpi->twopass.bits_left,
   3240         cpi->twopass.total_left_stats.coded_error,
   3241         cpi->twopass.bits_left /
   3242             (1 + cpi->twopass.total_left_stats.coded_error),
   3243         cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
   3244         cpi->twopass.kf_zeromotion_pct,
   3245         cpi->twopass.fr_content_type,
   3246         cm->lf.filter_level,
   3247         cm->seg.aq_av_offset);
   3248   }
   3249   fclose(f);
   3250 
   3251   if (0) {
   3252     FILE *const fmodes = fopen("Modes.stt", "a");
   3253     int i;
   3254 
   3255     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
   3256             cm->frame_type, cpi->refresh_golden_frame,
   3257             cpi->refresh_alt_ref_frame);
   3258 
   3259     for (i = 0; i < MAX_MODES; ++i)
   3260       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
   3261 
   3262     fprintf(fmodes, "\n");
   3263 
   3264     fclose(fmodes);
   3265   }
   3266 }
   3267 #endif
   3268 
   3269 static void set_mv_search_params(VP9_COMP *cpi) {
   3270   const VP9_COMMON *const cm = &cpi->common;
   3271   const unsigned int max_mv_def = VPXMIN(cm->width, cm->height);
   3272 
   3273   // Default based on max resolution.
   3274   cpi->mv_step_param = vp9_init_search_range(max_mv_def);
   3275 
   3276   if (cpi->sf.mv.auto_mv_step_size) {
   3277     if (frame_is_intra_only(cm)) {
   3278       // Initialize max_mv_magnitude for use in the first INTER frame
   3279       // after a key/intra-only frame.
   3280       cpi->max_mv_magnitude = max_mv_def;
   3281     } else {
   3282       if (cm->show_frame) {
   3283         // Allow mv_steps to correspond to twice the max mv magnitude found
   3284         // in the previous frame, capped by the default max_mv_magnitude based
   3285         // on resolution.
   3286         cpi->mv_step_param = vp9_init_search_range(
   3287             VPXMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
   3288       }
   3289       cpi->max_mv_magnitude = 0;
   3290     }
   3291   }
   3292 }
   3293 
   3294 static void set_size_independent_vars(VP9_COMP *cpi) {
   3295   vp9_set_speed_features_framesize_independent(cpi);
   3296   vp9_set_rd_speed_thresholds(cpi);
   3297   vp9_set_rd_speed_thresholds_sub8x8(cpi);
   3298   cpi->common.interp_filter = cpi->sf.default_interp_filter;
   3299 }
   3300 
   3301 static void set_size_dependent_vars(VP9_COMP *cpi, int *q, int *bottom_index,
   3302                                     int *top_index) {
   3303   VP9_COMMON *const cm = &cpi->common;
   3304 
   3305   // Setup variables that depend on the dimensions of the frame.
   3306   vp9_set_speed_features_framesize_dependent(cpi);
   3307 
   3308   // Decide q and q bounds.
   3309   *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
   3310 
   3311   if (!frame_is_intra_only(cm)) {
   3312     vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
   3313   }
   3314 
   3315 #if !CONFIG_REALTIME_ONLY
   3316   // Configure experimental use of segmentation for enhanced coding of
   3317   // static regions if indicated.
   3318   // Only allowed in the second pass of a two pass encode, as it requires
   3319   // lagged coding, and if the relevant speed feature flag is set.
   3320   if (cpi->oxcf.pass == 2 && cpi->sf.static_segmentation)
   3321     configure_static_seg_features(cpi);
   3322 #endif  // !CONFIG_REALTIME_ONLY
   3323 
   3324 #if CONFIG_VP9_POSTPROC && !(CONFIG_VP9_TEMPORAL_DENOISING)
   3325   if (cpi->oxcf.noise_sensitivity > 0) {
   3326     int l = 0;
   3327     switch (cpi->oxcf.noise_sensitivity) {
   3328       case 1: l = 20; break;
   3329       case 2: l = 40; break;
   3330       case 3: l = 60; break;
   3331       case 4:
   3332       case 5: l = 100; break;
   3333       case 6: l = 150; break;
   3334     }
   3335     if (!cpi->common.postproc_state.limits) {
   3336       cpi->common.postproc_state.limits =
   3337           vpx_calloc(cpi->un_scaled_source->y_width,
   3338                      sizeof(*cpi->common.postproc_state.limits));
   3339     }
   3340     vp9_denoise(cpi->Source, cpi->Source, l, cpi->common.postproc_state.limits);
   3341   }
   3342 #endif  // CONFIG_VP9_POSTPROC
   3343 }
   3344 
   3345 #if CONFIG_VP9_TEMPORAL_DENOISING
   3346 static void setup_denoiser_buffer(VP9_COMP *cpi) {
   3347   VP9_COMMON *const cm = &cpi->common;
   3348   if (cpi->oxcf.noise_sensitivity > 0 &&
   3349       !cpi->denoiser.frame_buffer_initialized) {
   3350     if (vp9_denoiser_alloc(cm, &cpi->svc, &cpi->denoiser, cpi->use_svc,
   3351                            cpi->oxcf.noise_sensitivity, cm->width, cm->height,
   3352                            cm->subsampling_x, cm->subsampling_y,
   3353 #if CONFIG_VP9_HIGHBITDEPTH
   3354                            cm->use_highbitdepth,
   3355 #endif
   3356                            VP9_ENC_BORDER_IN_PIXELS))
   3357       vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   3358                          "Failed to allocate denoiser");
   3359   }
   3360 }
   3361 #endif
   3362 
   3363 static void init_motion_estimation(VP9_COMP *cpi) {
   3364   int y_stride = cpi->scaled_source.y_stride;
   3365 
   3366   if (cpi->sf.mv.search_method == NSTEP) {
   3367     vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
   3368   } else if (cpi->sf.mv.search_method == DIAMOND) {
   3369     vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
   3370   }
   3371 }
   3372 
   3373 static void set_frame_size(VP9_COMP *cpi) {
   3374   int ref_frame;
   3375   VP9_COMMON *const cm = &cpi->common;
   3376   VP9EncoderConfig *const oxcf = &cpi->oxcf;
   3377   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
   3378 
   3379 #if !CONFIG_REALTIME_ONLY
   3380   if (oxcf->pass == 2 && oxcf->rc_mode == VPX_VBR &&
   3381       ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
   3382        (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
   3383     calculate_coded_size(cpi, &oxcf->scaled_frame_width,
   3384                          &oxcf->scaled_frame_height);
   3385 
   3386     // There has been a change in frame size.
   3387     vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
   3388                          oxcf->scaled_frame_height);
   3389   }
   3390 #endif  // !CONFIG_REALTIME_ONLY
   3391 
   3392   if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR && !cpi->use_svc &&
   3393       oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending != 0) {
   3394     oxcf->scaled_frame_width =
   3395         (oxcf->width * cpi->resize_scale_num) / cpi->resize_scale_den;
   3396     oxcf->scaled_frame_height =
   3397         (oxcf->height * cpi->resize_scale_num) / cpi->resize_scale_den;
   3398     // There has been a change in frame size.
   3399     vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
   3400                          oxcf->scaled_frame_height);
   3401 
   3402     // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
   3403     set_mv_search_params(cpi);
   3404 
   3405     vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
   3406 #if CONFIG_VP9_TEMPORAL_DENOISING
   3407     // Reset the denoiser on the resized frame.
   3408     if (cpi->oxcf.noise_sensitivity > 0) {
   3409       vp9_denoiser_free(&(cpi->denoiser));
   3410       setup_denoiser_buffer(cpi);
   3411       // Dynamic resize is only triggered for non-SVC, so we can force
   3412       // golden frame update here as temporary fix to denoiser.
   3413       cpi->refresh_golden_frame = 1;
   3414     }
   3415 #endif
   3416   }
   3417 
   3418   if ((oxcf->pass == 2) &&
   3419       (!cpi->use_svc || (is_two_pass_svc(cpi) &&
   3420                          cpi->svc.encode_empty_frame_state != ENCODING))) {
   3421     vp9_set_target_rate(cpi);
   3422   }
   3423 
   3424   alloc_frame_mvs(cm, cm->new_fb_idx);
   3425 
   3426   // Reset the frame pointers to the current frame size.
   3427   if (vpx_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
   3428                                cm->subsampling_x, cm->subsampling_y,
   3429 #if CONFIG_VP9_HIGHBITDEPTH
   3430                                cm->use_highbitdepth,
   3431 #endif
   3432                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
   3433                                NULL, NULL, NULL))
   3434     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   3435                        "Failed to allocate frame buffer");
   3436 
   3437   alloc_util_frame_buffers(cpi);
   3438   init_motion_estimation(cpi);
   3439 
   3440   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
   3441     RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
   3442     const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
   3443 
   3444     ref_buf->idx = buf_idx;
   3445 
   3446     if (buf_idx != INVALID_IDX) {
   3447       YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
   3448       ref_buf->buf = buf;
   3449 #if CONFIG_VP9_HIGHBITDEPTH
   3450       vp9_setup_scale_factors_for_frame(
   3451           &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
   3452           cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
   3453 #else
   3454       vp9_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
   3455                                         buf->y_crop_height, cm->width,
   3456                                         cm->height);
   3457 #endif  // CONFIG_VP9_HIGHBITDEPTH
   3458       if (vp9_is_scaled(&ref_buf->sf)) vpx_extend_frame_borders(buf);
   3459     } else {
   3460       ref_buf->buf = NULL;
   3461     }
   3462   }
   3463 
   3464   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
   3465 }
   3466 
   3467 static void encode_without_recode_loop(VP9_COMP *cpi, size_t *size,
   3468                                        uint8_t *dest) {
   3469   VP9_COMMON *const cm = &cpi->common;
   3470   int q = 0, bottom_index = 0, top_index = 0;  // Dummy variables.
   3471   const INTERP_FILTER filter_scaler =
   3472       (is_one_pass_cbr_svc(cpi))
   3473           ? cpi->svc.downsample_filter_type[cpi->svc.spatial_layer_id]
   3474           : EIGHTTAP;
   3475   const int phase_scaler =
   3476       (is_one_pass_cbr_svc(cpi))
   3477           ? cpi->svc.downsample_filter_phase[cpi->svc.spatial_layer_id]
   3478           : 0;
   3479 
   3480   // Flag to check if its valid to compute the source sad (used for
   3481   // scene detection and for superblock content state in CBR mode).
   3482   // The flag may get reset below based on SVC or resizing state.
   3483   cpi->compute_source_sad_onepass = cpi->oxcf.mode == REALTIME;
   3484 
   3485   vpx_clear_system_state();
   3486 
   3487   set_frame_size(cpi);
   3488 
   3489   if (is_one_pass_cbr_svc(cpi) &&
   3490       cpi->un_scaled_source->y_width == cm->width << 2 &&
   3491       cpi->un_scaled_source->y_height == cm->height << 2 &&
   3492       cpi->svc.scaled_temp.y_width == cm->width << 1 &&
   3493       cpi->svc.scaled_temp.y_height == cm->height << 1) {
   3494     // For svc, if it is a 1/4x1/4 downscaling, do a two-stage scaling to take
   3495     // advantage of the 1:2 optimized scaler. In the process, the 1/2x1/2
   3496     // result will be saved in scaled_temp and might be used later.
   3497     const INTERP_FILTER filter_scaler2 = cpi->svc.downsample_filter_type[1];
   3498     const int phase_scaler2 = cpi->svc.downsample_filter_phase[1];
   3499     cpi->Source = vp9_svc_twostage_scale(
   3500         cm, cpi->un_scaled_source, &cpi->scaled_source, &cpi->svc.scaled_temp,
   3501         filter_scaler, phase_scaler, filter_scaler2, phase_scaler2);
   3502     cpi->svc.scaled_one_half = 1;
   3503   } else if (is_one_pass_cbr_svc(cpi) &&
   3504              cpi->un_scaled_source->y_width == cm->width << 1 &&
   3505              cpi->un_scaled_source->y_height == cm->height << 1 &&
   3506              cpi->svc.scaled_one_half) {
   3507     // If the spatial layer is 1/2x1/2 and the scaling is already done in the
   3508     // two-stage scaling, use the result directly.
   3509     cpi->Source = &cpi->svc.scaled_temp;
   3510     cpi->svc.scaled_one_half = 0;
   3511   } else {
   3512     cpi->Source = vp9_scale_if_required(
   3513         cm, cpi->un_scaled_source, &cpi->scaled_source, (cpi->oxcf.pass == 0),
   3514         filter_scaler, phase_scaler);
   3515   }
   3516   // Unfiltered raw source used in metrics calculation if the source
   3517   // has been filtered.
   3518   if (is_psnr_calc_enabled(cpi)) {
   3519 #ifdef ENABLE_KF_DENOISE
   3520     if (is_spatial_denoise_enabled(cpi)) {
   3521       cpi->raw_source_frame = vp9_scale_if_required(
   3522           cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
   3523           (cpi->oxcf.pass == 0), EIGHTTAP, phase_scaler);
   3524     } else {
   3525       cpi->raw_source_frame = cpi->Source;
   3526     }
   3527 #else
   3528     cpi->raw_source_frame = cpi->Source;
   3529 #endif
   3530   }
   3531 
   3532   if ((cpi->use_svc &&
   3533        (cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1 ||
   3534         cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1 ||
   3535         cpi->svc.current_superframe < 1)) ||
   3536       cpi->resize_pending || cpi->resize_state || cpi->external_resize ||
   3537       cpi->resize_state != ORIG) {
   3538     cpi->compute_source_sad_onepass = 0;
   3539     if (cpi->content_state_sb_fd != NULL)
   3540       memset(cpi->content_state_sb_fd, 0,
   3541              (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) *
   3542                  sizeof(*cpi->content_state_sb_fd));
   3543   }
   3544 
   3545   // Avoid scaling last_source unless its needed.
   3546   // Last source is needed if avg_source_sad() is used, or if
   3547   // partition_search_type == SOURCE_VAR_BASED_PARTITION, or if noise
   3548   // estimation is enabled.
   3549   if (cpi->unscaled_last_source != NULL &&
   3550       (cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
   3551        (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_VBR &&
   3552         cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5) ||
   3553        cpi->sf.partition_search_type == SOURCE_VAR_BASED_PARTITION ||
   3554        (cpi->noise_estimate.enabled && !cpi->oxcf.noise_sensitivity) ||
   3555        cpi->compute_source_sad_onepass))
   3556     cpi->Last_Source = vp9_scale_if_required(
   3557         cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
   3558         (cpi->oxcf.pass == 0), EIGHTTAP, 0);
   3559 
   3560   if (cpi->Last_Source == NULL ||
   3561       cpi->Last_Source->y_width != cpi->Source->y_width ||
   3562       cpi->Last_Source->y_height != cpi->Source->y_height)
   3563     cpi->compute_source_sad_onepass = 0;
   3564 
   3565   if (cm->frame_type == KEY_FRAME || cpi->resize_pending != 0) {
   3566     memset(cpi->consec_zero_mv, 0,
   3567            cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
   3568   }
   3569 
   3570   vp9_update_noise_estimate(cpi);
   3571 
   3572   // Scene detection is always used for VBR mode or screen-content case.
   3573   // For other cases (e.g., CBR mode) use it for 5 <= speed < 8 for now
   3574   // (need to check encoding time cost for doing this for speed 8).
   3575   cpi->rc.high_source_sad = 0;
   3576   if (cpi->compute_source_sad_onepass && cm->show_frame &&
   3577       (cpi->oxcf.rc_mode == VPX_VBR ||
   3578        cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
   3579        (cpi->oxcf.speed >= 5 && cpi->oxcf.speed < 8 && !cpi->use_svc)))
   3580     vp9_scene_detection_onepass(cpi);
   3581 
   3582   // For 1 pass CBR SVC, only ZEROMV is allowed for spatial reference frame
   3583   // when svc->force_zero_mode_spatial_ref = 1. Under those conditions we can
   3584   // avoid this frame-level upsampling (for non intra_only frames).
   3585   if (frame_is_intra_only(cm) == 0 &&
   3586       !(is_one_pass_cbr_svc(cpi) && cpi->svc.force_zero_mode_spatial_ref)) {
   3587     vp9_scale_references(cpi);
   3588   }
   3589 
   3590   set_size_independent_vars(cpi);
   3591   set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
   3592 
   3593   if (cpi->sf.copy_partition_flag) alloc_copy_partition_data(cpi);
   3594 
   3595   if (cpi->sf.svc_use_lowres_part &&
   3596       cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 2) {
   3597     if (cpi->svc.prev_partition_svc == NULL) {
   3598       CHECK_MEM_ERROR(
   3599           cm, cpi->svc.prev_partition_svc,
   3600           (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
   3601                                    sizeof(*cpi->svc.prev_partition_svc)));
   3602     }
   3603   }
   3604 
   3605   if (cpi->oxcf.speed >= 5 && cpi->oxcf.pass == 0 &&
   3606       cpi->oxcf.rc_mode == VPX_CBR &&
   3607       cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
   3608       cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
   3609     cpi->use_skin_detection = 1;
   3610   }
   3611 
   3612   vp9_set_quantizer(cm, q);
   3613   vp9_set_variance_partition_thresholds(cpi, q, 0);
   3614 
   3615   setup_frame(cpi);
   3616 
   3617   suppress_active_map(cpi);
   3618 
   3619   // Variance adaptive and in frame q adjustment experiments are mutually
   3620   // exclusive.
   3621   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   3622     vp9_vaq_frame_setup(cpi);
   3623   } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
   3624     vp9_360aq_frame_setup(cpi);
   3625   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
   3626     vp9_setup_in_frame_q_adj(cpi);
   3627   } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
   3628     vp9_cyclic_refresh_setup(cpi);
   3629   } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
   3630     // it may be pretty bad for rate-control,
   3631     // and I should handle it somehow
   3632     vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
   3633   }
   3634 
   3635   apply_active_map(cpi);
   3636 
   3637   vp9_encode_frame(cpi);
   3638 
   3639   // Check if we should drop this frame because of high overshoot.
   3640   // Only for frames where high temporal-source SAD is detected.
   3641   if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
   3642       cpi->resize_state == ORIG && cm->frame_type != KEY_FRAME &&
   3643       cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
   3644       cpi->rc.high_source_sad == 1) {
   3645     int frame_size = 0;
   3646     // Get an estimate of the encoded frame size.
   3647     save_coding_context(cpi);
   3648     vp9_pack_bitstream(cpi, dest, size);
   3649     restore_coding_context(cpi);
   3650     frame_size = (int)(*size) << 3;
   3651     // Check if encoded frame will overshoot too much, and if so, set the q and
   3652     // adjust some rate control parameters, and return to re-encode the frame.
   3653     if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
   3654       vpx_clear_system_state();
   3655       vp9_set_quantizer(cm, q);
   3656       vp9_set_variance_partition_thresholds(cpi, q, 0);
   3657       suppress_active_map(cpi);
   3658       // Turn-off cyclic refresh for re-encoded frame.
   3659       if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
   3660         unsigned char *const seg_map = cpi->segmentation_map;
   3661         memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
   3662         vp9_disable_segmentation(&cm->seg);
   3663       }
   3664       apply_active_map(cpi);
   3665       vp9_encode_frame(cpi);
   3666     }
   3667   }
   3668 
   3669   // Update some stats from cyclic refresh, and check for golden frame update.
   3670   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
   3671       cm->frame_type != KEY_FRAME)
   3672     vp9_cyclic_refresh_postencode(cpi);
   3673 
   3674   // Update the skip mb flag probabilities based on the distribution
   3675   // seen in the last encoder iteration.
   3676   // update_base_skip_probs(cpi);
   3677   vpx_clear_system_state();
   3678 }
   3679 
   3680 #define MAX_QSTEP_ADJ 4
   3681 static int get_qstep_adj(int rate_excess, int rate_limit) {
   3682   int qstep =
   3683       rate_limit ? ((rate_excess + rate_limit / 2) / rate_limit) : INT_MAX;
   3684   return VPXMIN(qstep, MAX_QSTEP_ADJ);
   3685 }
   3686 
   3687 static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
   3688                                     uint8_t *dest) {
   3689   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
   3690   VP9_COMMON *const cm = &cpi->common;
   3691   RATE_CONTROL *const rc = &cpi->rc;
   3692   int bottom_index, top_index;
   3693   int loop_count = 0;
   3694   int loop_at_this_size = 0;
   3695   int loop = 0;
   3696   int overshoot_seen = 0;
   3697   int undershoot_seen = 0;
   3698   int frame_over_shoot_limit;
   3699   int frame_under_shoot_limit;
   3700   int q = 0, q_low = 0, q_high = 0;
   3701   int enable_acl;
   3702 #ifdef AGGRESSIVE_VBR
   3703   int qrange_adj = 1;
   3704 #endif
   3705 
   3706   set_size_independent_vars(cpi);
   3707 
   3708   enable_acl = cpi->sf.allow_acl
   3709                    ? (cm->frame_type == KEY_FRAME) || (cm->show_frame == 0)
   3710                    : 0;
   3711 
   3712   do {
   3713     vpx_clear_system_state();
   3714 
   3715     set_frame_size(cpi);
   3716 
   3717     if (loop_count == 0 || cpi->resize_pending != 0) {
   3718       set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
   3719 
   3720 #ifdef AGGRESSIVE_VBR
   3721       if (two_pass_first_group_inter(cpi)) {
   3722         // Adjustment limits for min and max q
   3723         qrange_adj = VPXMAX(1, (top_index - bottom_index) / 2);
   3724 
   3725         bottom_index =
   3726             VPXMAX(bottom_index - qrange_adj / 2, oxcf->best_allowed_q);
   3727         top_index = VPXMIN(oxcf->worst_allowed_q, top_index + qrange_adj / 2);
   3728       }
   3729 #endif
   3730       // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
   3731       set_mv_search_params(cpi);
   3732 
   3733       // Reset the loop state for new frame size.
   3734       overshoot_seen = 0;
   3735       undershoot_seen = 0;
   3736 
   3737       // Reconfiguration for change in frame size has concluded.
   3738       cpi->resize_pending = 0;
   3739 
   3740       q_low = bottom_index;
   3741       q_high = top_index;
   3742 
   3743       loop_at_this_size = 0;
   3744     }
   3745 
   3746     // Decide frame size bounds first time through.
   3747     if (loop_count == 0) {
   3748       vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
   3749                                        &frame_under_shoot_limit,
   3750                                        &frame_over_shoot_limit);
   3751     }
   3752 
   3753     cpi->Source =
   3754         vp9_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source,
   3755                               (oxcf->pass == 0), EIGHTTAP, 0);
   3756 
   3757     // Unfiltered raw source used in metrics calculation if the source
   3758     // has been filtered.
   3759     if (is_psnr_calc_enabled(cpi)) {
   3760 #ifdef ENABLE_KF_DENOISE
   3761       if (is_spatial_denoise_enabled(cpi)) {
   3762         cpi->raw_source_frame = vp9_scale_if_required(
   3763             cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
   3764             (oxcf->pass == 0), EIGHTTAP, 0);
   3765       } else {
   3766         cpi->raw_source_frame = cpi->Source;
   3767       }
   3768 #else
   3769       cpi->raw_source_frame = cpi->Source;
   3770 #endif
   3771     }
   3772 
   3773     if (cpi->unscaled_last_source != NULL)
   3774       cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
   3775                                                &cpi->scaled_last_source,
   3776                                                (oxcf->pass == 0), EIGHTTAP, 0);
   3777 
   3778     if (frame_is_intra_only(cm) == 0) {
   3779       if (loop_count > 0) {
   3780         release_scaled_references(cpi);
   3781       }
   3782       vp9_scale_references(cpi);
   3783     }
   3784 
   3785     vp9_set_quantizer(cm, q);
   3786 
   3787     if (loop_count == 0) setup_frame(cpi);
   3788 
   3789     // Variance adaptive and in frame q adjustment experiments are mutually
   3790     // exclusive.
   3791     if (oxcf->aq_mode == VARIANCE_AQ) {
   3792       vp9_vaq_frame_setup(cpi);
   3793     } else if (oxcf->aq_mode == EQUATOR360_AQ) {
   3794       vp9_360aq_frame_setup(cpi);
   3795     } else if (oxcf->aq_mode == COMPLEXITY_AQ) {
   3796       vp9_setup_in_frame_q_adj(cpi);
   3797     } else if (oxcf->aq_mode == LOOKAHEAD_AQ) {
   3798       vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
   3799     }
   3800 
   3801     vp9_encode_frame(cpi);
   3802 
   3803     // Update the skip mb flag probabilities based on the distribution
   3804     // seen in the last encoder iteration.
   3805     // update_base_skip_probs(cpi);
   3806 
   3807     vpx_clear_system_state();
   3808 
   3809     // Dummy pack of the bitstream using up to date stats to get an
   3810     // accurate estimate of output frame size to determine if we need
   3811     // to recode.
   3812     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
   3813       save_coding_context(cpi);
   3814       if (!cpi->sf.use_nonrd_pick_mode) vp9_pack_bitstream(cpi, dest, size);
   3815 
   3816       rc->projected_frame_size = (int)(*size) << 3;
   3817 
   3818       if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
   3819     }
   3820 
   3821     if (oxcf->rc_mode == VPX_Q) {
   3822       loop = 0;
   3823     } else {
   3824       if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
   3825           (rc->projected_frame_size < rc->max_frame_bandwidth)) {
   3826         int last_q = q;
   3827         int64_t kf_err;
   3828 
   3829         int64_t high_err_target = cpi->ambient_err;
   3830         int64_t low_err_target = cpi->ambient_err >> 1;
   3831 
   3832 #if CONFIG_VP9_HIGHBITDEPTH
   3833         if (cm->use_highbitdepth) {
   3834           kf_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
   3835         } else {
   3836           kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
   3837         }
   3838 #else
   3839         kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
   3840 #endif  // CONFIG_VP9_HIGHBITDEPTH
   3841 
   3842         // Prevent possible divide by zero error below for perfect KF
   3843         kf_err += !kf_err;
   3844 
   3845         // The key frame is not good enough or we can afford
   3846         // to make it better without undue risk of popping.
   3847         if ((kf_err > high_err_target &&
   3848              rc->projected_frame_size <= frame_over_shoot_limit) ||
   3849             (kf_err > low_err_target &&
   3850              rc->projected_frame_size <= frame_under_shoot_limit)) {
   3851           // Lower q_high
   3852           q_high = q > q_low ? q - 1 : q_low;
   3853 
   3854           // Adjust Q
   3855           q = (int)((q * high_err_target) / kf_err);
   3856           q = VPXMIN(q, (q_high + q_low) >> 1);
   3857         } else if (kf_err < low_err_target &&
   3858                    rc->projected_frame_size >= frame_under_shoot_limit) {
   3859           // The key frame is much better than the previous frame
   3860           // Raise q_low
   3861           q_low = q < q_high ? q + 1 : q_high;
   3862 
   3863           // Adjust Q
   3864           q = (int)((q * low_err_target) / kf_err);
   3865           q = VPXMIN(q, (q_high + q_low + 1) >> 1);
   3866         }
   3867 
   3868         // Clamp Q to upper and lower limits:
   3869         q = clamp(q, q_low, q_high);
   3870 
   3871         loop = q != last_q;
   3872       } else if (recode_loop_test(cpi, frame_over_shoot_limit,
   3873                                   frame_under_shoot_limit, q,
   3874                                   VPXMAX(q_high, top_index), bottom_index)) {
   3875         // Is the projected frame size out of range and are we allowed
   3876         // to attempt to recode.
   3877         int last_q = q;
   3878         int retries = 0;
   3879         int qstep;
   3880 
   3881         if (cpi->resize_pending == 1) {
   3882           // Change in frame size so go back around the recode loop.
   3883           cpi->rc.frame_size_selector =
   3884               SCALE_STEP1 - cpi->rc.frame_size_selector;
   3885           cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
   3886 
   3887 #if CONFIG_INTERNAL_STATS
   3888           ++cpi->tot_recode_hits;
   3889 #endif
   3890           ++loop_count;
   3891           loop = 1;
   3892           continue;
   3893         }
   3894 
   3895         // Frame size out of permitted range:
   3896         // Update correction factor & compute new Q to try...
   3897 
   3898         // Frame is too large
   3899         if (rc->projected_frame_size > rc->this_frame_target) {
   3900           // Special case if the projected size is > the max allowed.
   3901           if ((q == q_high) &&
   3902               ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
   3903                (rc->projected_frame_size >=
   3904                 big_rate_miss_high_threshold(cpi)))) {
   3905             int max_rate = VPXMAX(1, VPXMIN(rc->max_frame_bandwidth,
   3906                                             big_rate_miss_high_threshold(cpi)));
   3907             double q_val_high;
   3908             q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
   3909             q_val_high =
   3910                 q_val_high * ((double)rc->projected_frame_size / max_rate);
   3911             q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
   3912             q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
   3913           }
   3914 
   3915           // Raise Qlow as to at least the current value
   3916           qstep =
   3917               get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
   3918           q_low = VPXMIN(q + qstep, q_high);
   3919 
   3920           if (undershoot_seen || loop_at_this_size > 1) {
   3921             // Update rate_correction_factor unless
   3922             vp9_rc_update_rate_correction_factors(cpi);
   3923 
   3924             q = (q_high + q_low + 1) / 2;
   3925           } else {
   3926             // Update rate_correction_factor unless
   3927             vp9_rc_update_rate_correction_factors(cpi);
   3928 
   3929             q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
   3930                                   VPXMAX(q_high, top_index));
   3931 
   3932             while (q < q_low && retries < 10) {
   3933               vp9_rc_update_rate_correction_factors(cpi);
   3934               q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
   3935                                     VPXMAX(q_high, top_index));
   3936               retries++;
   3937             }
   3938           }
   3939 
   3940           overshoot_seen = 1;
   3941         } else {
   3942           // Frame is too small
   3943           qstep =
   3944               get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
   3945           q_high = VPXMAX(q - qstep, q_low);
   3946 
   3947           if (overshoot_seen || loop_at_this_size > 1) {
   3948             vp9_rc_update_rate_correction_factors(cpi);
   3949             q = (q_high + q_low) / 2;
   3950           } else {
   3951             vp9_rc_update_rate_correction_factors(cpi);
   3952             q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
   3953                                   VPXMIN(q_low, bottom_index), top_index);
   3954             // Special case reset for qlow for constrained quality.
   3955             // This should only trigger where there is very substantial
   3956             // undershoot on a frame and the auto cq level is above
   3957             // the user passsed in value.
   3958             if (oxcf->rc_mode == VPX_CQ && q < q_low) {
   3959               q_low = q;
   3960             }
   3961 
   3962             while (q > q_high && retries < 10) {
   3963               vp9_rc_update_rate_correction_factors(cpi);
   3964               q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
   3965                                     VPXMIN(q_low, bottom_index), top_index);
   3966               retries++;
   3967             }
   3968           }
   3969           undershoot_seen = 1;
   3970         }
   3971 
   3972         // Clamp Q to upper and lower limits:
   3973         q = clamp(q, q_low, q_high);
   3974 
   3975         loop = (q != last_q);
   3976       } else {
   3977         loop = 0;
   3978       }
   3979     }
   3980 
   3981     // Special case for overlay frame.
   3982     if (rc->is_src_frame_alt_ref &&
   3983         rc->projected_frame_size < rc->max_frame_bandwidth)
   3984       loop = 0;
   3985 
   3986     if (loop) {
   3987       ++loop_count;
   3988       ++loop_at_this_size;
   3989 
   3990 #if CONFIG_INTERNAL_STATS
   3991       ++cpi->tot_recode_hits;
   3992 #endif
   3993     }
   3994 
   3995     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF)
   3996       if (loop || !enable_acl) restore_coding_context(cpi);
   3997   } while (loop);
   3998 
   3999 #ifdef AGGRESSIVE_VBR
   4000   if (two_pass_first_group_inter(cpi)) {
   4001     cpi->twopass.active_worst_quality =
   4002         VPXMIN(q + qrange_adj, oxcf->worst_allowed_q);
   4003   } else if (!frame_is_kf_gf_arf(cpi)) {
   4004 #else
   4005   if (!frame_is_kf_gf_arf(cpi)) {
   4006 #endif
   4007     // Have we been forced to adapt Q outside the expected range by an extreme
   4008     // rate miss. If so adjust the active maxQ for the subsequent frames.
   4009     if (q > cpi->twopass.active_worst_quality) {
   4010       cpi->twopass.active_worst_quality = q;
   4011     } else if (oxcf->vbr_corpus_complexity && q == q_low &&
   4012                rc->projected_frame_size < rc->this_frame_target) {
   4013       cpi->twopass.active_worst_quality =
   4014           VPXMAX(q, cpi->twopass.active_worst_quality - 1);
   4015     }
   4016   }
   4017 
   4018   if (enable_acl) {
   4019     // Skip recoding, if model diff is below threshold
   4020     const int thresh = compute_context_model_thresh(cpi);
   4021     const int diff = compute_context_model_diff(cm);
   4022     if (diff < thresh) {
   4023       vpx_clear_system_state();
   4024       restore_coding_context(cpi);
   4025       return;
   4026     }
   4027 
   4028     vp9_encode_frame(cpi);
   4029     vpx_clear_system_state();
   4030     restore_coding_context(cpi);
   4031     vp9_pack_bitstream(cpi, dest, size);
   4032 
   4033     vp9_encode_frame(cpi);
   4034     vpx_clear_system_state();
   4035 
   4036     restore_coding_context(cpi);
   4037   }
   4038 }
   4039 
   4040 static int get_ref_frame_flags(const VP9_COMP *cpi) {
   4041   const int *const map = cpi->common.ref_frame_map;
   4042   const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
   4043   const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
   4044   const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
   4045   int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
   4046 
   4047   if (gold_is_last) flags &= ~VP9_GOLD_FLAG;
   4048 
   4049   if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
   4050       (cpi->svc.number_temporal_layers == 1 &&
   4051        cpi->svc.number_spatial_layers == 1))
   4052     flags &= ~VP9_GOLD_FLAG;
   4053 
   4054   if (alt_is_last) flags &= ~VP9_ALT_FLAG;
   4055 
   4056   if (gold_is_alt) flags &= ~VP9_ALT_FLAG;
   4057 
   4058   return flags;
   4059 }
   4060 
   4061 static void set_ext_overrides(VP9_COMP *cpi) {
   4062   // Overrides the defaults with the externally supplied values with
   4063   // vp9_update_reference() and vp9_update_entropy() calls
   4064   // Note: The overrides are valid only for the next frame passed
   4065   // to encode_frame_to_data_rate() function
   4066   if (cpi->ext_refresh_frame_context_pending) {
   4067     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
   4068     cpi->ext_refresh_frame_context_pending = 0;
   4069   }
   4070   if (cpi->ext_refresh_frame_flags_pending) {
   4071     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
   4072     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
   4073     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
   4074   }
   4075 }
   4076 
   4077 YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(
   4078     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
   4079     YV12_BUFFER_CONFIG *scaled_temp, INTERP_FILTER filter_type,
   4080     int phase_scaler, INTERP_FILTER filter_type2, int phase_scaler2) {
   4081   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
   4082       cm->mi_rows * MI_SIZE != unscaled->y_height) {
   4083 #if CONFIG_VP9_HIGHBITDEPTH
   4084     if (cm->bit_depth == VPX_BITS_8) {
   4085       vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
   4086                                  phase_scaler2);
   4087       vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type,
   4088                                  phase_scaler);
   4089     } else {
   4090       scale_and_extend_frame(unscaled, scaled_temp, (int)cm->bit_depth,
   4091                              filter_type2, phase_scaler2);
   4092       scale_and_extend_frame(scaled_temp, scaled, (int)cm->bit_depth,
   4093                              filter_type, phase_scaler);
   4094     }
   4095 #else
   4096     vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
   4097                                phase_scaler2);
   4098     vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type, phase_scaler);
   4099 #endif  // CONFIG_VP9_HIGHBITDEPTH
   4100     return scaled;
   4101   } else {
   4102     return unscaled;
   4103   }
   4104 }
   4105 
   4106 YV12_BUFFER_CONFIG *vp9_scale_if_required(
   4107     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
   4108     int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler) {
   4109   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
   4110       cm->mi_rows * MI_SIZE != unscaled->y_height) {
   4111 #if CONFIG_VP9_HIGHBITDEPTH
   4112     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
   4113         unscaled->y_height <= (scaled->y_height << 1))
   4114       if (cm->bit_depth == VPX_BITS_8)
   4115         vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
   4116       else
   4117         scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth,
   4118                                filter_type, phase_scaler);
   4119     else
   4120       scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
   4121 #else
   4122     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
   4123         unscaled->y_height <= (scaled->y_height << 1))
   4124       vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
   4125     else
   4126       scale_and_extend_frame_nonnormative(unscaled, scaled);
   4127 #endif  // CONFIG_VP9_HIGHBITDEPTH
   4128     return scaled;
   4129   } else {
   4130     return unscaled;
   4131   }
   4132 }
   4133 
   4134 static void set_arf_sign_bias(VP9_COMP *cpi) {
   4135   VP9_COMMON *const cm = &cpi->common;
   4136   int arf_sign_bias;
   4137 
   4138   if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
   4139     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
   4140     arf_sign_bias = cpi->rc.source_alt_ref_active &&
   4141                     (!cpi->refresh_alt_ref_frame ||
   4142                      (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
   4143   } else {
   4144     arf_sign_bias =
   4145         (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
   4146   }
   4147   cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
   4148 }
   4149 
   4150 static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
   4151   INTERP_FILTER ifilter;
   4152   int ref_total[MAX_REF_FRAMES] = { 0 };
   4153   MV_REFERENCE_FRAME ref;
   4154   int mask = 0;
   4155   if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
   4156     return mask;
   4157   for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
   4158     for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
   4159       ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
   4160 
   4161   for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
   4162     if ((ref_total[LAST_FRAME] &&
   4163          cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
   4164         (ref_total[GOLDEN_FRAME] == 0 ||
   4165          cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
   4166              ref_total[GOLDEN_FRAME]) &&
   4167         (ref_total[ALTREF_FRAME] == 0 ||
   4168          cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50 <
   4169              ref_total[ALTREF_FRAME]))
   4170       mask |= 1 << ifilter;
   4171   }
   4172   return mask;
   4173 }
   4174 
   4175 #ifdef ENABLE_KF_DENOISE
   4176 // Baseline Kernal weights for denoise
   4177 static uint8_t dn_kernal_3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
   4178 static uint8_t dn_kernal_5[25] = { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 4,
   4179                                    2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1 };
   4180 
   4181 static INLINE void add_denoise_point(int centre_val, int data_val, int thresh,
   4182                                      uint8_t point_weight, int *sum_val,
   4183                                      int *sum_weight) {
   4184   if (abs(centre_val - data_val) <= thresh) {
   4185     *sum_weight += point_weight;
   4186     *sum_val += (int)data_val * (int)point_weight;
   4187   }
   4188 }
   4189 
   4190 static void spatial_denoise_point(uint8_t *src_ptr, const int stride,
   4191                                   const int strength) {
   4192   int sum_weight = 0;
   4193   int sum_val = 0;
   4194   int thresh = strength;
   4195   int kernal_size = 5;
   4196   int half_k_size = 2;
   4197   int i, j;
   4198   int max_diff = 0;
   4199   uint8_t *tmp_ptr;
   4200   uint8_t *kernal_ptr;
   4201 
   4202   // Find the maximum deviation from the source point in the locale.
   4203   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
   4204   for (i = 0; i < kernal_size + 2; ++i) {
   4205     for (j = 0; j < kernal_size + 2; ++j) {
   4206       max_diff = VPXMAX(max_diff, abs((int)*src_ptr - (int)tmp_ptr[j]));
   4207     }
   4208     tmp_ptr += stride;
   4209   }
   4210 
   4211   // Select the kernal size.
   4212   if (max_diff > (strength + (strength >> 1))) {
   4213     kernal_size = 3;
   4214     half_k_size = 1;
   4215     thresh = thresh >> 1;
   4216   }
   4217   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
   4218 
   4219   // Apply the kernal
   4220   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
   4221   for (i = 0; i < kernal_size; ++i) {
   4222     for (j = 0; j < kernal_size; ++j) {
   4223       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
   4224                         &sum_val, &sum_weight);
   4225       ++kernal_ptr;
   4226     }
   4227     tmp_ptr += stride;
   4228   }
   4229 
   4230   // Update the source value with the new filtered value
   4231   *src_ptr = (uint8_t)((sum_val + (sum_weight >> 1)) / sum_weight);
   4232 }
   4233 
   4234 #if CONFIG_VP9_HIGHBITDEPTH
   4235 static void highbd_spatial_denoise_point(uint16_t *src_ptr, const int stride,
   4236                                          const int strength) {
   4237   int sum_weight = 0;
   4238   int sum_val = 0;
   4239   int thresh = strength;
   4240   int kernal_size = 5;
   4241   int half_k_size = 2;
   4242   int i, j;
   4243   int max_diff = 0;
   4244   uint16_t *tmp_ptr;
   4245   uint8_t *kernal_ptr;
   4246 
   4247   // Find the maximum deviation from the source point in the locale.
   4248   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
   4249   for (i = 0; i < kernal_size + 2; ++i) {
   4250     for (j = 0; j < kernal_size + 2; ++j) {
   4251       max_diff = VPXMAX(max_diff, abs((int)src_ptr - (int)tmp_ptr[j]));
   4252     }
   4253     tmp_ptr += stride;
   4254   }
   4255 
   4256   // Select the kernal size.
   4257   if (max_diff > (strength + (strength >> 1))) {
   4258     kernal_size = 3;
   4259     half_k_size = 1;
   4260     thresh = thresh >> 1;
   4261   }
   4262   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
   4263 
   4264   // Apply the kernal
   4265   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
   4266   for (i = 0; i < kernal_size; ++i) {
   4267     for (j = 0; j < kernal_size; ++j) {
   4268       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
   4269                         &sum_val, &sum_weight);
   4270       ++kernal_ptr;
   4271     }
   4272     tmp_ptr += stride;
   4273   }
   4274 
   4275   // Update the source value with the new filtered value
   4276   *src_ptr = (uint16_t)((sum_val + (sum_weight >> 1)) / sum_weight);
   4277 }
   4278 #endif  // CONFIG_VP9_HIGHBITDEPTH
   4279 
   4280 // Apply thresholded spatial noise supression to a given buffer.
   4281 static void spatial_denoise_buffer(VP9_COMP *cpi, uint8_t *buffer,
   4282                                    const int stride, const int width,
   4283                                    const int height, const int strength) {
   4284   VP9_COMMON *const cm = &cpi->common;
   4285   uint8_t *src_ptr = buffer;
   4286   int row;
   4287   int col;
   4288 
   4289   for (row = 0; row < height; ++row) {
   4290     for (col = 0; col < width; ++col) {
   4291 #if CONFIG_VP9_HIGHBITDEPTH
   4292       if (cm->use_highbitdepth)
   4293         highbd_spatial_denoise_point(CONVERT_TO_SHORTPTR(&src_ptr[col]), stride,
   4294                                      strength);
   4295       else
   4296         spatial_denoise_point(&src_ptr[col], stride, strength);
   4297 #else
   4298       spatial_denoise_point(&src_ptr[col], stride, strength);
   4299 #endif  // CONFIG_VP9_HIGHBITDEPTH
   4300     }
   4301     src_ptr += stride;
   4302   }
   4303 }
   4304 
   4305 // Apply thresholded spatial noise supression to source.
   4306 static void spatial_denoise_frame(VP9_COMP *cpi) {
   4307   YV12_BUFFER_CONFIG *src = cpi->Source;
   4308   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
   4309   TWO_PASS *const twopass = &cpi->twopass;
   4310   VP9_COMMON *const cm = &cpi->common;
   4311 
   4312   // Base the filter strength on the current active max Q.
   4313   const int q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
   4314                                               cm->bit_depth));
   4315   int strength =
   4316       VPXMAX(oxcf->arnr_strength >> 2, VPXMIN(oxcf->arnr_strength, (q >> 4)));
   4317 
   4318   // Denoise each of Y,U and V buffers.
   4319   spatial_denoise_buffer(cpi, src->y_buffer, src->y_stride, src->y_width,
   4320                          src->y_height, strength);
   4321 
   4322   strength += (strength >> 1);
   4323   spatial_denoise_buffer(cpi, src->u_buffer, src->uv_stride, src->uv_width,
   4324                          src->uv_height, strength << 1);
   4325 
   4326   spatial_denoise_buffer(cpi, src->v_buffer, src->uv_stride, src->uv_width,
   4327                          src->uv_height, strength << 1);
   4328 }
   4329 #endif  // ENABLE_KF_DENOISE
   4330 
   4331 static void vp9_try_disable_lookahead_aq(VP9_COMP *cpi, size_t *size,
   4332                                          uint8_t *dest) {
   4333   if (cpi->common.seg.enabled)
   4334     if (ALT_REF_AQ_PROTECT_GAIN) {
   4335       size_t nsize = *size;
   4336       int overhead;
   4337 
   4338       // TODO(yuryg): optimize this, as
   4339       // we don't really need to repack
   4340 
   4341       save_coding_context(cpi);
   4342       vp9_disable_segmentation(&cpi->common.seg);
   4343       vp9_pack_bitstream(cpi, dest, &nsize);
   4344       restore_coding_context(cpi);
   4345 
   4346       overhead = (int)*size - (int)nsize;
   4347 
   4348       if (vp9_alt_ref_aq_disable_if(cpi->alt_ref_aq, overhead, (int)*size))
   4349         vp9_encode_frame(cpi);
   4350       else
   4351         vp9_enable_segmentation(&cpi->common.seg);
   4352     }
   4353 }
   4354 
   4355 static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size,
   4356                                       uint8_t *dest,
   4357                                       unsigned int *frame_flags) {
   4358   VP9_COMMON *const cm = &cpi->common;
   4359   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
   4360   struct segmentation *const seg = &cm->seg;
   4361   TX_SIZE t;
   4362 
   4363   set_ext_overrides(cpi);
   4364   vpx_clear_system_state();
   4365 
   4366 #ifdef ENABLE_KF_DENOISE
   4367   // Spatial denoise of key frame.
   4368   if (is_spatial_denoise_enabled(cpi)) spatial_denoise_frame(cpi);
   4369 #endif
   4370 
   4371   // Set the arf sign bias for this frame.
   4372   set_arf_sign_bias(cpi);
   4373 
   4374   // Set default state for segment based loop filter update flags.
   4375   cm->lf.mode_ref_delta_update = 0;
   4376 
   4377   if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
   4378     cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
   4379 
   4380   // Set various flags etc to special state if it is a key frame.
   4381   if (frame_is_intra_only(cm)) {
   4382     // Reset the loop filter deltas and segmentation map.
   4383     vp9_reset_segment_features(&cm->seg);
   4384 
   4385     // If segmentation is enabled force a map update for key frames.
   4386     if (seg->enabled) {
   4387       seg->update_map = 1;
   4388       seg->update_data = 1;
   4389     }
   4390 
   4391     // The alternate reference frame cannot be active for a key frame.
   4392     cpi->rc.source_alt_ref_active = 0;
   4393 
   4394     cm->error_resilient_mode = oxcf->error_resilient_mode;
   4395     cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
   4396 
   4397     // By default, encoder assumes decoder can use prev_mi.
   4398     if (cm->error_resilient_mode) {
   4399       cm->frame_parallel_decoding_mode = 1;
   4400       cm->reset_frame_context = 0;
   4401       cm->refresh_frame_context = 0;
   4402     } else if (cm->intra_only) {
   4403       // Only reset the current context.
   4404       cm->reset_frame_context = 2;
   4405     }
   4406   }
   4407   if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
   4408     // Use context 0 for intra only empty frame, but the last frame context
   4409     // for other empty frames.
   4410     if (cpi->svc.encode_empty_frame_state == ENCODING) {
   4411       if (cpi->svc.encode_intra_empty_frame != 0)
   4412         cm->frame_context_idx = 0;
   4413       else
   4414         cm->frame_context_idx = FRAME_CONTEXTS - 1;
   4415     } else {
   4416       cm->frame_context_idx =
   4417           cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
   4418           cpi->svc.temporal_layer_id;
   4419     }
   4420 
   4421     cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
   4422 
   4423     // The probs will be updated based on the frame type of its previous
   4424     // frame if frame_parallel_decoding_mode is 0. The type may vary for
   4425     // the frame after a key frame in base layer since we may drop enhancement
   4426     // layers. So set frame_parallel_decoding_mode to 1 in this case.
   4427     if (cm->frame_parallel_decoding_mode == 0) {
   4428       if (cpi->svc.number_temporal_layers == 1) {
   4429         if (cpi->svc.spatial_layer_id == 0 &&
   4430             cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
   4431           cm->frame_parallel_decoding_mode = 1;
   4432       } else if (cpi->svc.spatial_layer_id == 0) {
   4433         // Find the 2nd frame in temporal base layer and 1st frame in temporal
   4434         // enhancement layers from the key frame.
   4435         int i;
   4436         for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
   4437           if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
   4438             cm->frame_parallel_decoding_mode = 1;
   4439             break;
   4440           }
   4441         }
   4442       }
   4443     }
   4444   }
   4445 
   4446   // For 1 pass CBR, check if we are dropping this frame.
   4447   // For spatial layers, for now only check for frame-dropping on first spatial
   4448   // layer, and if decision is to drop, we drop whole super-frame.
   4449   if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR &&
   4450       cm->frame_type != KEY_FRAME) {
   4451     if (vp9_rc_drop_frame(cpi) ||
   4452         (is_one_pass_cbr_svc(cpi) && cpi->svc.rc_drop_superframe == 1)) {
   4453       vp9_rc_postencode_update_drop_frame(cpi);
   4454       ++cm->current_video_frame;
   4455       cpi->ext_refresh_frame_flags_pending = 0;
   4456       cpi->svc.rc_drop_superframe = 1;
   4457       cpi->last_frame_dropped = 1;
   4458       // TODO(marpan): Advancing the svc counters on dropped frames can break
   4459       // the referencing scheme for the fixed svc patterns defined in
   4460       // vp9_one_pass_cbr_svc_start_layer(). Look into fixing this issue, but
   4461       // for now, don't advance the svc frame counters on dropped frame.
   4462       // if (cpi->use_svc)
   4463       //   vp9_inc_frame_in_layer(cpi);
   4464 
   4465       return;
   4466     }
   4467   }
   4468 
   4469   vpx_clear_system_state();
   4470 
   4471 #if CONFIG_INTERNAL_STATS
   4472   memset(cpi->mode_chosen_counts, 0,
   4473          MAX_MODES * sizeof(*cpi->mode_chosen_counts));
   4474 #endif
   4475 
   4476   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
   4477     encode_without_recode_loop(cpi, size, dest);
   4478   } else {
   4479     encode_with_recode_loop(cpi, size, dest);
   4480   }
   4481 
   4482   cpi->last_frame_dropped = 0;
   4483 
   4484   // Disable segmentation if it decrease rate/distortion ratio
   4485   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
   4486     vp9_try_disable_lookahead_aq(cpi, size, dest);
   4487 
   4488 #if CONFIG_VP9_TEMPORAL_DENOISING
   4489 #ifdef OUTPUT_YUV_DENOISED
   4490   if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
   4491     vpx_write_yuv_frame(yuv_denoised_file,
   4492                         &cpi->denoiser.running_avg_y[INTRA_FRAME]);
   4493   }
   4494 #endif
   4495 #endif
   4496 #ifdef OUTPUT_YUV_SKINMAP
   4497   if (cpi->common.current_video_frame > 1) {
   4498     vp9_output_skin_map(cpi, yuv_skinmap_file);
   4499   }
   4500 #endif
   4501 
   4502   // Special case code to reduce pulsing when key frames are forced at a
   4503   // fixed interval. Note the reconstruction error if it is the frame before
   4504   // the force key frame
   4505   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
   4506 #if CONFIG_VP9_HIGHBITDEPTH
   4507     if (cm->use_highbitdepth) {
   4508       cpi->ambient_err =
   4509           vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
   4510     } else {
   4511       cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
   4512     }
   4513 #else
   4514     cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
   4515 #endif  // CONFIG_VP9_HIGHBITDEPTH
   4516   }
   4517 
   4518   // If the encoder forced a KEY_FRAME decision
   4519   if (cm->frame_type == KEY_FRAME) cpi->refresh_last_frame = 1;
   4520 
   4521   cm->frame_to_show = get_frame_new_buffer(cm);
   4522   cm->frame_to_show->color_space = cm->color_space;
   4523   cm->frame_to_show->color_range = cm->color_range;
   4524   cm->frame_to_show->render_width = cm->render_width;
   4525   cm->frame_to_show->render_height = cm->render_height;
   4526 
   4527   // Pick the loop filter level for the frame.
   4528   loopfilter_frame(cpi, cm);
   4529 
   4530   // build the bitstream
   4531   vp9_pack_bitstream(cpi, dest, size);
   4532 
   4533   if (cm->seg.update_map) update_reference_segmentation_map(cpi);
   4534 
   4535   if (frame_is_intra_only(cm) == 0) {
   4536     release_scaled_references(cpi);
   4537   }
   4538   vp9_update_reference_frames(cpi);
   4539 
   4540   for (t = TX_4X4; t <= TX_32X32; t++)
   4541     full_to_model_counts(cpi->td.counts->coef[t],
   4542                          cpi->td.rd_counts.coef_counts[t]);
   4543 
   4544   if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
   4545     vp9_adapt_coef_probs(cm);
   4546 
   4547   if (!frame_is_intra_only(cm)) {
   4548     if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
   4549       vp9_adapt_mode_probs(cm);
   4550       vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
   4551     }
   4552   }
   4553 
   4554   cpi->ext_refresh_frame_flags_pending = 0;
   4555 
   4556   if (cpi->refresh_golden_frame == 1)
   4557     cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
   4558   else
   4559     cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
   4560 
   4561   if (cpi->refresh_alt_ref_frame == 1)
   4562     cpi->frame_flags |= FRAMEFLAGS_ALTREF;
   4563   else
   4564     cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
   4565 
   4566   cpi->ref_frame_flags = get_ref_frame_flags(cpi);
   4567 
   4568   cm->last_frame_type = cm->frame_type;
   4569 
   4570   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
   4571     vp9_rc_postencode_update(cpi, *size);
   4572 
   4573 #if 0
   4574   output_frame_level_debug_stats(cpi);
   4575 #endif
   4576 
   4577   if (cm->frame_type == KEY_FRAME) {
   4578     // Tell the caller that the frame was coded as a key frame
   4579     *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
   4580   } else {
   4581     *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
   4582   }
   4583 
   4584   // Clear the one shot update flags for segmentation map and mode/ref loop
   4585   // filter deltas.
   4586   cm->seg.update_map = 0;
   4587   cm->seg.update_data = 0;
   4588   cm->lf.mode_ref_delta_update = 0;
   4589 
   4590   // keep track of the last coded dimensions
   4591   cm->last_width = cm->width;
   4592   cm->last_height = cm->height;
   4593 
   4594   // reset to normal state now that we are done.
   4595   if (!cm->show_existing_frame) cm->last_show_frame = cm->show_frame;
   4596 
   4597   if (cm->show_frame) {
   4598     vp9_swap_mi_and_prev_mi(cm);
   4599     // Don't increment frame counters if this was an altref buffer
   4600     // update not a real frame
   4601     ++cm->current_video_frame;
   4602     if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
   4603   }
   4604   cm->prev_frame = cm->cur_frame;
   4605 
   4606   if (cpi->use_svc)
   4607     cpi->svc
   4608         .layer_context[cpi->svc.spatial_layer_id *
   4609                            cpi->svc.number_temporal_layers +
   4610                        cpi->svc.temporal_layer_id]
   4611         .last_frame_type = cm->frame_type;
   4612 
   4613   cpi->force_update_segmentation = 0;
   4614 
   4615   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
   4616     vp9_alt_ref_aq_unset_all(cpi->alt_ref_aq, cpi);
   4617 }
   4618 
   4619 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
   4620                       unsigned int *frame_flags) {
   4621   vp9_rc_get_svc_params(cpi);
   4622   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
   4623 }
   4624 
   4625 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
   4626                         unsigned int *frame_flags) {
   4627   if (cpi->oxcf.rc_mode == VPX_CBR) {
   4628     vp9_rc_get_one_pass_cbr_params(cpi);
   4629   } else {
   4630     vp9_rc_get_one_pass_vbr_params(cpi);
   4631   }
   4632   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
   4633 }
   4634 
   4635 #if !CONFIG_REALTIME_ONLY
   4636 static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
   4637                         unsigned int *frame_flags) {
   4638   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
   4639   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
   4640 
   4641   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
   4642     vp9_twopass_postencode_update(cpi);
   4643 }
   4644 #endif  // !CONFIG_REALTIME_ONLY
   4645 
   4646 static void init_ref_frame_bufs(VP9_COMMON *cm) {
   4647   int i;
   4648   BufferPool *const pool = cm->buffer_pool;
   4649   cm->new_fb_idx = INVALID_IDX;
   4650   for (i = 0; i < REF_FRAMES; ++i) {
   4651     cm->ref_frame_map[i] = INVALID_IDX;
   4652     pool->frame_bufs[i].ref_count = 0;
   4653   }
   4654 }
   4655 
   4656 static void check_initial_width(VP9_COMP *cpi,
   4657 #if CONFIG_VP9_HIGHBITDEPTH
   4658                                 int use_highbitdepth,
   4659 #endif
   4660                                 int subsampling_x, int subsampling_y) {
   4661   VP9_COMMON *const cm = &cpi->common;
   4662 
   4663   if (!cpi->initial_width ||
   4664 #if CONFIG_VP9_HIGHBITDEPTH
   4665       cm->use_highbitdepth != use_highbitdepth ||
   4666 #endif
   4667       cm->subsampling_x != subsampling_x ||
   4668       cm->subsampling_y != subsampling_y) {
   4669     cm->subsampling_x = subsampling_x;
   4670     cm->subsampling_y = subsampling_y;
   4671 #if CONFIG_VP9_HIGHBITDEPTH
   4672     cm->use_highbitdepth = use_highbitdepth;
   4673 #endif
   4674 
   4675     alloc_raw_frame_buffers(cpi);
   4676     init_ref_frame_bufs(cm);
   4677     alloc_util_frame_buffers(cpi);
   4678 
   4679     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
   4680 
   4681     cpi->initial_width = cm->width;
   4682     cpi->initial_height = cm->height;
   4683     cpi->initial_mbs = cm->MBs;
   4684   }
   4685 }
   4686 
   4687 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
   4688                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
   4689                           int64_t end_time) {
   4690   VP9_COMMON *const cm = &cpi->common;
   4691   struct vpx_usec_timer timer;
   4692   int res = 0;
   4693   const int subsampling_x = sd->subsampling_x;
   4694   const int subsampling_y = sd->subsampling_y;
   4695 #if CONFIG_VP9_HIGHBITDEPTH
   4696   const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
   4697 #endif
   4698 
   4699 #if CONFIG_VP9_HIGHBITDEPTH
   4700   check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
   4701 #else
   4702   check_initial_width(cpi, subsampling_x, subsampling_y);
   4703 #endif  // CONFIG_VP9_HIGHBITDEPTH
   4704 
   4705 #if CONFIG_VP9_TEMPORAL_DENOISING
   4706   setup_denoiser_buffer(cpi);
   4707 #endif
   4708   vpx_usec_timer_start(&timer);
   4709 
   4710   if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
   4711 #if CONFIG_VP9_HIGHBITDEPTH
   4712                          use_highbitdepth,
   4713 #endif  // CONFIG_VP9_HIGHBITDEPTH
   4714                          frame_flags))
   4715     res = -1;
   4716   vpx_usec_timer_mark(&timer);
   4717   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
   4718 
   4719   if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
   4720       (subsampling_x != 1 || subsampling_y != 1)) {
   4721     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
   4722                        "Non-4:2:0 color format requires profile 1 or 3");
   4723     res = -1;
   4724   }
   4725   if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
   4726       (subsampling_x == 1 && subsampling_y == 1)) {
   4727     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
   4728                        "4:2:0 color format requires profile 0 or 2");
   4729     res = -1;
   4730   }
   4731 
   4732   return res;
   4733 }
   4734 
   4735 static int frame_is_reference(const VP9_COMP *cpi) {
   4736   const VP9_COMMON *cm = &cpi->common;
   4737 
   4738   return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
   4739          cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
   4740          cm->refresh_frame_context || cm->lf.mode_ref_delta_update ||
   4741          cm->seg.update_map || cm->seg.update_data;
   4742 }
   4743 
   4744 static void adjust_frame_rate(VP9_COMP *cpi,
   4745                               const struct lookahead_entry *source) {
   4746   int64_t this_duration;
   4747   int step = 0;
   4748 
   4749   if (source->ts_start == cpi->first_time_stamp_ever) {
   4750     this_duration = source->ts_end - source->ts_start;
   4751     step = 1;
   4752   } else {
   4753     int64_t last_duration =
   4754         cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
   4755 
   4756     this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
   4757 
   4758     // do a step update if the duration changes by 10%
   4759     if (last_duration)
   4760       step = (int)((this_duration - last_duration) * 10 / last_duration);
   4761   }
   4762 
   4763   if (this_duration) {
   4764     if (step) {
   4765       vp9_new_framerate(cpi, 10000000.0 / this_duration);
   4766     } else {
   4767       // Average this frame's rate into the last second's average
   4768       // frame rate. If we haven't seen 1 second yet, then average
   4769       // over the whole interval seen.
   4770       const double interval = VPXMIN(
   4771           (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
   4772       double avg_duration = 10000000.0 / cpi->framerate;
   4773       avg_duration *= (interval - avg_duration + this_duration);
   4774       avg_duration /= interval;
   4775 
   4776       vp9_new_framerate(cpi, 10000000.0 / avg_duration);
   4777     }
   4778   }
   4779   cpi->last_time_stamp_seen = source->ts_start;
   4780   cpi->last_end_time_stamp_seen = source->ts_end;
   4781 }
   4782 
   4783 // Returns 0 if this is not an alt ref else the offset of the source frame
   4784 // used as the arf midpoint.
   4785 static int get_arf_src_index(VP9_COMP *cpi) {
   4786   RATE_CONTROL *const rc = &cpi->rc;
   4787   int arf_src_index = 0;
   4788   if (is_altref_enabled(cpi)) {
   4789     if (cpi->oxcf.pass == 2) {
   4790       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
   4791       if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
   4792         arf_src_index = gf_group->arf_src_offset[gf_group->index];
   4793       }
   4794     } else if (rc->source_alt_ref_pending) {
   4795       arf_src_index = rc->frames_till_gf_update_due;
   4796     }
   4797   }
   4798   return arf_src_index;
   4799 }
   4800 
   4801 static void check_src_altref(VP9_COMP *cpi,
   4802                              const struct lookahead_entry *source) {
   4803   RATE_CONTROL *const rc = &cpi->rc;
   4804 
   4805   if (cpi->oxcf.pass == 2) {
   4806     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
   4807     rc->is_src_frame_alt_ref =
   4808         (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
   4809   } else {
   4810     rc->is_src_frame_alt_ref =
   4811         cpi->alt_ref_source && (source == cpi->alt_ref_source);
   4812   }
   4813 
   4814   if (rc->is_src_frame_alt_ref) {
   4815     // Current frame is an ARF overlay frame.
   4816     cpi->alt_ref_source = NULL;
   4817 
   4818     // Don't refresh the last buffer for an ARF overlay frame. It will
   4819     // become the GF so preserve last as an alternative prediction option.
   4820     cpi->refresh_last_frame = 0;
   4821   }
   4822 }
   4823 
   4824 #if CONFIG_INTERNAL_STATS
   4825 extern double vp9_get_blockiness(const uint8_t *img1, int img1_pitch,
   4826                                  const uint8_t *img2, int img2_pitch, int width,
   4827                                  int height);
   4828 
   4829 static void adjust_image_stat(double y, double u, double v, double all,
   4830                               ImageStat *s) {
   4831   s->stat[Y] += y;
   4832   s->stat[U] += u;
   4833   s->stat[V] += v;
   4834   s->stat[ALL] += all;
   4835   s->worst = VPXMIN(s->worst, all);
   4836 }
   4837 #endif  // CONFIG_INTERNAL_STATS
   4838 
   4839 // Adjust the maximum allowable frame size for the target level.
   4840 static void level_rc_framerate(VP9_COMP *cpi, int arf_src_index) {
   4841   RATE_CONTROL *const rc = &cpi->rc;
   4842   LevelConstraint *const ls = &cpi->level_constraint;
   4843   VP9_COMMON *const cm = &cpi->common;
   4844   const double max_cpb_size = ls->max_cpb_size;
   4845   vpx_clear_system_state();
   4846   rc->max_frame_bandwidth = VPXMIN(rc->max_frame_bandwidth, ls->max_frame_size);
   4847   if (frame_is_intra_only(cm)) {
   4848     rc->max_frame_bandwidth =
   4849         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.5));
   4850   } else if (arf_src_index > 0) {
   4851     rc->max_frame_bandwidth =
   4852         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.4));
   4853   } else {
   4854     rc->max_frame_bandwidth =
   4855         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.2));
   4856   }
   4857 }
   4858 
   4859 static void update_level_info(VP9_COMP *cpi, size_t *size, int arf_src_index) {
   4860   VP9_COMMON *const cm = &cpi->common;
   4861   Vp9LevelInfo *const level_info = &cpi->level_info;
   4862   Vp9LevelSpec *const level_spec = &level_info->level_spec;
   4863   Vp9LevelStats *const level_stats = &level_info->level_stats;
   4864   int i, idx;
   4865   uint64_t luma_samples, dur_end;
   4866   const uint32_t luma_pic_size = cm->width * cm->height;
   4867   const uint32_t luma_pic_breadth = VPXMAX(cm->width, cm->height);
   4868   LevelConstraint *const level_constraint = &cpi->level_constraint;
   4869   const int8_t level_index = level_constraint->level_index;
   4870   double cpb_data_size;
   4871 
   4872   vpx_clear_system_state();
   4873 
   4874   // update level_stats
   4875   level_stats->total_compressed_size += *size;
   4876   if (cm->show_frame) {
   4877     level_stats->total_uncompressed_size +=
   4878         luma_pic_size +
   4879         2 * (luma_pic_size >> (cm->subsampling_x + cm->subsampling_y));
   4880     level_stats->time_encoded =
   4881         (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
   4882         (double)TICKS_PER_SEC;
   4883   }
   4884 
   4885   if (arf_src_index > 0) {
   4886     if (!level_stats->seen_first_altref) {
   4887       level_stats->seen_first_altref = 1;
   4888     } else if (level_stats->frames_since_last_altref <
   4889                level_spec->min_altref_distance) {
   4890       level_spec->min_altref_distance = level_stats->frames_since_last_altref;
   4891     }
   4892     level_stats->frames_since_last_altref = 0;
   4893   } else {
   4894     ++level_stats->frames_since_last_altref;
   4895   }
   4896 
   4897   if (level_stats->frame_window_buffer.len < FRAME_WINDOW_SIZE - 1) {
   4898     idx = (level_stats->frame_window_buffer.start +
   4899            level_stats->frame_window_buffer.len++) %
   4900           FRAME_WINDOW_SIZE;
   4901   } else {
   4902     idx = level_stats->frame_window_buffer.start;
   4903     level_stats->frame_window_buffer.start = (idx + 1) % FRAME_WINDOW_SIZE;
   4904   }
   4905   level_stats->frame_window_buffer.buf[idx].ts = cpi->last_time_stamp_seen;
   4906   level_stats->frame_window_buffer.buf[idx].size = (uint32_t)(*size);
   4907   level_stats->frame_window_buffer.buf[idx].luma_samples = luma_pic_size;
   4908 
   4909   if (cm->frame_type == KEY_FRAME) {
   4910     level_stats->ref_refresh_map = 0;
   4911   } else {
   4912     int count = 0;
   4913     level_stats->ref_refresh_map |= vp9_get_refresh_mask(cpi);
   4914     // Also need to consider the case where the encoder refers to a buffer
   4915     // that has been implicitly refreshed after encoding a keyframe.
   4916     if (!cm->intra_only) {
   4917       level_stats->ref_refresh_map |= (1 << cpi->lst_fb_idx);
   4918       level_stats->ref_refresh_map |= (1 << cpi->gld_fb_idx);
   4919       level_stats->ref_refresh_map |= (1 << cpi->alt_fb_idx);
   4920     }
   4921     for (i = 0; i < REF_FRAMES; ++i) {
   4922       count += (level_stats->ref_refresh_map >> i) & 1;
   4923     }
   4924     if (count > level_spec->max_ref_frame_buffers) {
   4925       level_spec->max_ref_frame_buffers = count;
   4926     }
   4927   }
   4928 
   4929   // update average_bitrate
   4930   level_spec->average_bitrate = (double)level_stats->total_compressed_size /
   4931                                 125.0 / level_stats->time_encoded;
   4932 
   4933   // update max_luma_sample_rate
   4934   luma_samples = 0;
   4935   for (i = 0; i < level_stats->frame_window_buffer.len; ++i) {
   4936     idx = (level_stats->frame_window_buffer.start +
   4937            level_stats->frame_window_buffer.len - 1 - i) %
   4938           FRAME_WINDOW_SIZE;
   4939     if (i == 0) {
   4940       dur_end = level_stats->frame_window_buffer.buf[idx].ts;
   4941     }
   4942     if (dur_end - level_stats->frame_window_buffer.buf[idx].ts >=
   4943         TICKS_PER_SEC) {
   4944       break;
   4945     }
   4946     luma_samples += level_stats->frame_window_buffer.buf[idx].luma_samples;
   4947   }
   4948   if (luma_samples > level_spec->max_luma_sample_rate) {
   4949     level_spec->max_luma_sample_rate = luma_samples;
   4950   }
   4951 
   4952   // update max_cpb_size
   4953   cpb_data_size = 0;
   4954   for (i = 0; i < CPB_WINDOW_SIZE; ++i) {
   4955     if (i >= level_stats->frame_window_buffer.len) break;
   4956     idx = (level_stats->frame_window_buffer.start +
   4957            level_stats->frame_window_buffer.len - 1 - i) %
   4958           FRAME_WINDOW_SIZE;
   4959     cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
   4960   }
   4961   cpb_data_size = cpb_data_size / 125.0;
   4962   if (cpb_data_size > level_spec->max_cpb_size) {
   4963     level_spec->max_cpb_size = cpb_data_size;
   4964   }
   4965 
   4966   // update max_luma_picture_size
   4967   if (luma_pic_size > level_spec->max_luma_picture_size) {
   4968     level_spec->max_luma_picture_size = luma_pic_size;
   4969   }
   4970 
   4971   // update max_luma_picture_breadth
   4972   if (luma_pic_breadth > level_spec->max_luma_picture_breadth) {
   4973     level_spec->max_luma_picture_breadth = luma_pic_breadth;
   4974   }
   4975 
   4976   // update compression_ratio
   4977   level_spec->compression_ratio = (double)level_stats->total_uncompressed_size *
   4978                                   cm->bit_depth /
   4979                                   level_stats->total_compressed_size / 8.0;
   4980 
   4981   // update max_col_tiles
   4982   if (level_spec->max_col_tiles < (1 << cm->log2_tile_cols)) {
   4983     level_spec->max_col_tiles = (1 << cm->log2_tile_cols);
   4984   }
   4985 
   4986   if (level_index >= 0 && level_constraint->fail_flag == 0) {
   4987     if (level_spec->max_luma_picture_size >
   4988         vp9_level_defs[level_index].max_luma_picture_size) {
   4989       level_constraint->fail_flag |= (1 << LUMA_PIC_SIZE_TOO_LARGE);
   4990       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
   4991                          "Failed to encode to the target level %d. %s",
   4992                          vp9_level_defs[level_index].level,
   4993                          level_fail_messages[LUMA_PIC_SIZE_TOO_LARGE]);
   4994     }
   4995 
   4996     if (level_spec->max_luma_picture_breadth >
   4997         vp9_level_defs[level_index].max_luma_picture_breadth) {
   4998       level_constraint->fail_flag |= (1 << LUMA_PIC_BREADTH_TOO_LARGE);
   4999       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
   5000                          "Failed to encode to the target level %d. %s",
   5001                          vp9_level_defs[level_index].level,
   5002                          level_fail_messages[LUMA_PIC_BREADTH_TOO_LARGE]);
   5003     }
   5004 
   5005     if ((double)level_spec->max_luma_sample_rate >
   5006         (double)vp9_level_defs[level_index].max_luma_sample_rate *
   5007             (1 + SAMPLE_RATE_GRACE_P)) {
   5008       level_constraint->fail_flag |= (1 << LUMA_SAMPLE_RATE_TOO_LARGE);
   5009       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
   5010                          "Failed to encode to the target level %d. %s",
   5011                          vp9_level_defs[level_index].level,
   5012                          level_fail_messages[LUMA_SAMPLE_RATE_TOO_LARGE]);
   5013     }
   5014 
   5015     if (level_spec->max_col_tiles > vp9_level_defs[level_index].max_col_tiles) {
   5016       level_constraint->fail_flag |= (1 << TOO_MANY_COLUMN_TILE);
   5017       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
   5018                          "Failed to encode to the target level %d. %s",
   5019                          vp9_level_defs[level_index].level,
   5020                          level_fail_messages[TOO_MANY_COLUMN_TILE]);
   5021     }
   5022 
   5023     if (level_spec->min_altref_distance <
   5024         vp9_level_defs[level_index].min_altref_distance) {
   5025       level_constraint->fail_flag |= (1 << ALTREF_DIST_TOO_SMALL);
   5026       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
   5027                          "Failed to encode to the target level %d. %s",
   5028                          vp9_level_defs[level_index].level,
   5029                          level_fail_messages[ALTREF_DIST_TOO_SMALL]);
   5030     }
   5031 
   5032     if (level_spec->max_ref_frame_buffers >
   5033         vp9_level_defs[level_index].max_ref_frame_buffers) {
   5034       level_constraint->fail_flag |= (1 << TOO_MANY_REF_BUFFER);
   5035       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
   5036                          "Failed to encode to the target level %d. %s",
   5037                          vp9_level_defs[level_index].level,
   5038                          level_fail_messages[TOO_MANY_REF_BUFFER]);
   5039     }
   5040 
   5041     if (level_spec->max_cpb_size > vp9_level_defs[level_index].max_cpb_size) {
   5042       level_constraint->fail_flag |= (1 << CPB_TOO_LARGE);
   5043       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
   5044                          "Failed to encode to the target level %d. %s",
   5045                          vp9_level_defs[level_index].level,
   5046                          level_fail_messages[CPB_TOO_LARGE]);
   5047     }
   5048 
   5049     // Set an upper bound for the next frame size. It will be used in
   5050     // level_rc_framerate() before encoding the next frame.
   5051     cpb_data_size = 0;
   5052     for (i = 0; i < CPB_WINDOW_SIZE - 1; ++i) {
   5053       if (i >= level_stats->frame_window_buffer.len) break;
   5054       idx = (level_stats->frame_window_buffer.start +
   5055              level_stats->frame_window_buffer.len - 1 - i) %
   5056             FRAME_WINDOW_SIZE;
   5057       cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
   5058     }
   5059     cpb_data_size = cpb_data_size / 125.0;
   5060     level_constraint->max_frame_size =
   5061         (int)((vp9_level_defs[level_index].max_cpb_size - cpb_data_size) *
   5062               1000.0);
   5063     if (level_stats->frame_window_buffer.len < CPB_WINDOW_SIZE - 1)
   5064       level_constraint->max_frame_size >>= 1;
   5065   }
   5066 }
   5067 
   5068 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
   5069                             size_t *size, uint8_t *dest, int64_t *time_stamp,
   5070                             int64_t *time_end, int flush) {
   5071   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
   5072   VP9_COMMON *const cm = &cpi->common;
   5073   BufferPool *const pool = cm->buffer_pool;
   5074   RATE_CONTROL *const rc = &cpi->rc;
   5075   struct vpx_usec_timer cmptimer;
   5076   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
   5077   struct lookahead_entry *last_source = NULL;
   5078   struct lookahead_entry *source = NULL;
   5079   int arf_src_index;
   5080   int i;
   5081 
   5082   if (is_two_pass_svc(cpi)) {
   5083 #if CONFIG_SPATIAL_SVC
   5084     vp9_svc_start_frame(cpi);
   5085     // Use a small empty frame instead of a real frame
   5086     if (cpi->svc.encode_empty_frame_state == ENCODING)
   5087       source = &cpi->svc.empty_frame;
   5088 #endif
   5089     if (oxcf->pass == 2) vp9_restore_layer_context(cpi);
   5090   } else if (is_one_pass_cbr_svc(cpi)) {
   5091     vp9_one_pass_cbr_svc_start_layer(cpi);
   5092   }
   5093 
   5094   vpx_usec_timer_start(&cmptimer);
   5095 
   5096   vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
   5097 
   5098   // Is multi-arf enabled.
   5099   // Note that at the moment multi_arf is only configured for 2 pass VBR and
   5100   // will not work properly with svc.
   5101   if ((oxcf->pass == 2) && !cpi->use_svc && (cpi->oxcf.enable_auto_arf > 1))
   5102     cpi->multi_arf_allowed = 1;
   5103   else
   5104     cpi->multi_arf_allowed = 0;
   5105 
   5106   // Normal defaults
   5107   cm->reset_frame_context = 0;
   5108   cm->refresh_frame_context = 1;
   5109   if (!is_one_pass_cbr_svc(cpi)) {
   5110     cpi->refresh_last_frame = 1;
   5111     cpi->refresh_golden_frame = 0;
   5112     cpi->refresh_alt_ref_frame = 0;
   5113   }
   5114 
   5115   // Should we encode an arf frame.
   5116   arf_src_index = get_arf_src_index(cpi);
   5117 
   5118   // Skip alt frame if we encode the empty frame
   5119   if (is_two_pass_svc(cpi) && source != NULL) arf_src_index = 0;
   5120 
   5121   if (arf_src_index) {
   5122     for (i = 0; i <= arf_src_index; ++i) {
   5123       struct lookahead_entry *e = vp9_lookahead_peek(cpi->lookahead, i);
   5124       // Avoid creating an alt-ref if there's a forced keyframe pending.
   5125       if (e == NULL) {
   5126         break;
   5127       } else if (e->flags == VPX_EFLAG_FORCE_KF) {
   5128         arf_src_index = 0;
   5129         flush = 1;
   5130         break;
   5131       }
   5132     }
   5133   }
   5134 
   5135   if (arf_src_index) {
   5136     assert(arf_src_index <= rc->frames_to_key);
   5137 
   5138     if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
   5139       cpi->alt_ref_source = source;
   5140 
   5141 #if CONFIG_SPATIAL_SVC
   5142       if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
   5143         int i;
   5144         // Reference a hidden frame from a lower layer
   5145         for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
   5146           if (oxcf->ss_enable_auto_arf[i]) {
   5147             cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
   5148             break;
   5149           }
   5150         }
   5151       }
   5152       cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
   5153 #endif
   5154 #if !CONFIG_REALTIME_ONLY
   5155       if ((oxcf->mode != REALTIME) && (oxcf->arnr_max_frames > 0) &&
   5156           (oxcf->arnr_strength > 0)) {
   5157         int bitrate = cpi->rc.avg_frame_bandwidth / 40;
   5158         int not_low_bitrate = bitrate > ALT_REF_AQ_LOW_BITRATE_BOUNDARY;
   5159 
   5160         int not_last_frame = (cpi->lookahead->sz - arf_src_index > 1);
   5161         not_last_frame |= ALT_REF_AQ_APPLY_TO_LAST_FRAME;
   5162 
   5163         // Produce the filtered ARF frame.
   5164         vp9_temporal_filter(cpi, arf_src_index);
   5165         vpx_extend_frame_borders(&cpi->alt_ref_buffer);
   5166 
   5167         // for small bitrates segmentation overhead usually
   5168         // eats all bitrate gain from enabling delta quantizers
   5169         if (cpi->oxcf.alt_ref_aq != 0 && not_low_bitrate && not_last_frame)
   5170           vp9_alt_ref_aq_setup_mode(cpi->alt_ref_aq, cpi);
   5171 
   5172         force_src_buffer = &cpi->alt_ref_buffer;
   5173       }
   5174 #endif
   5175       cm->show_frame = 0;
   5176       cm->intra_only = 0;
   5177       cpi->refresh_alt_ref_frame = 1;
   5178       cpi->refresh_golden_frame = 0;
   5179       cpi->refresh_last_frame = 0;
   5180       rc->is_src_frame_alt_ref = 0;
   5181       rc->source_alt_ref_pending = 0;
   5182     } else {
   5183       rc->source_alt_ref_pending = 0;
   5184     }
   5185   }
   5186 
   5187   if (!source) {
   5188     // Get last frame source.
   5189     if (cm->current_video_frame > 0) {
   5190       if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
   5191         return -1;
   5192     }
   5193 
   5194     // Read in the source frame.
   5195     if (cpi->use_svc)
   5196       source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
   5197     else
   5198       source = vp9_lookahead_pop(cpi->lookahead, flush);
   5199 
   5200     if (source != NULL) {
   5201       cm->show_frame = 1;
   5202       cm->intra_only = 0;
   5203       // if the flags indicate intra frame, but if the current picture is for
   5204       // non-zero spatial layer, it should not be an intra picture.
   5205       if ((source->flags & VPX_EFLAG_FORCE_KF) &&
   5206           cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode) {
   5207         source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
   5208       }
   5209 
   5210       // Check to see if the frame should be encoded as an arf overlay.
   5211       check_src_altref(cpi, source);
   5212     }
   5213   }
   5214 
   5215   if (source) {
   5216     cpi->un_scaled_source = cpi->Source =
   5217         force_src_buffer ? force_src_buffer : &source->img;
   5218 
   5219 #ifdef ENABLE_KF_DENOISE
   5220     // Copy of raw source for metrics calculation.
   5221     if (is_psnr_calc_enabled(cpi))
   5222       vp9_copy_and_extend_frame(cpi->Source, &cpi->raw_unscaled_source);
   5223 #endif
   5224 
   5225     cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
   5226 
   5227     *time_stamp = source->ts_start;
   5228     *time_end = source->ts_end;
   5229     *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
   5230 
   5231   } else {
   5232     *size = 0;
   5233 #if !CONFIG_REALTIME_ONLY
   5234     if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
   5235       vp9_end_first_pass(cpi); /* get last stats packet */
   5236       cpi->twopass.first_pass_done = 1;
   5237     }
   5238 #endif  // !CONFIG_REALTIME_ONLY
   5239     return -1;
   5240   }
   5241 
   5242   if (source->ts_start < cpi->first_time_stamp_ever) {
   5243     cpi->first_time_stamp_ever = source->ts_start;
   5244     cpi->last_end_time_stamp_seen = source->ts_start;
   5245   }
   5246 
   5247   // Clear down mmx registers
   5248   vpx_clear_system_state();
   5249 
   5250   // adjust frame rates based on timestamps given
   5251   if (cm->show_frame) {
   5252     adjust_frame_rate(cpi, source);
   5253   }
   5254 
   5255   if (is_one_pass_cbr_svc(cpi)) {
   5256     vp9_update_temporal_layer_framerate(cpi);
   5257     vp9_restore_layer_context(cpi);
   5258   }
   5259 
   5260   // Find a free buffer for the new frame, releasing the reference previously
   5261   // held.
   5262   if (cm->new_fb_idx != INVALID_IDX) {
   5263     --pool->frame_bufs[cm->new_fb_idx].ref_count;
   5264   }
   5265   cm->new_fb_idx = get_free_fb(cm);
   5266 
   5267   if (cm->new_fb_idx == INVALID_IDX) return -1;
   5268 
   5269   cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
   5270 
   5271   if (!cpi->use_svc && cpi->multi_arf_allowed) {
   5272     if (cm->frame_type == KEY_FRAME) {
   5273       init_buffer_indices(cpi);
   5274     } else if (oxcf->pass == 2) {
   5275       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
   5276       cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
   5277     }
   5278   }
   5279 
   5280   // Start with a 0 size frame.
   5281   *size = 0;
   5282 
   5283   cpi->frame_flags = *frame_flags;
   5284 
   5285 #if !CONFIG_REALTIME_ONLY
   5286   if ((oxcf->pass == 2) &&
   5287       (!cpi->use_svc || (is_two_pass_svc(cpi) &&
   5288                          cpi->svc.encode_empty_frame_state != ENCODING))) {
   5289     vp9_rc_get_second_pass_params(cpi);
   5290   } else if (oxcf->pass == 1) {
   5291     set_frame_size(cpi);
   5292   }
   5293 #endif  // !CONFIG_REALTIME_ONLY
   5294 
   5295   if (oxcf->pass != 1 && cpi->level_constraint.level_index >= 0 &&
   5296       cpi->level_constraint.fail_flag == 0)
   5297     level_rc_framerate(cpi, arf_src_index);
   5298 
   5299   if (cpi->oxcf.pass != 0 || cpi->use_svc || frame_is_intra_only(cm) == 1) {
   5300     for (i = 0; i < MAX_REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
   5301   }
   5302 
   5303   cpi->td.mb.fp_src_pred = 0;
   5304 #if CONFIG_REALTIME_ONLY
   5305   if (cpi->use_svc) {
   5306     SvcEncode(cpi, size, dest, frame_flags);
   5307   } else {
   5308     // One pass encode
   5309     Pass0Encode(cpi, size, dest, frame_flags);
   5310   }
   5311 #else  // !CONFIG_REALTIME_ONLY
   5312   if (oxcf->pass == 1 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
   5313     const int lossless = is_lossless_requested(oxcf);
   5314 #if CONFIG_VP9_HIGHBITDEPTH
   5315     if (cpi->oxcf.use_highbitdepth)
   5316       cpi->td.mb.fwd_txfm4x4 =
   5317           lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
   5318     else
   5319       cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
   5320     cpi->td.mb.highbd_inv_txfm_add =
   5321         lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
   5322 #else
   5323     cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
   5324 #endif  // CONFIG_VP9_HIGHBITDEPTH
   5325     cpi->td.mb.inv_txfm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
   5326     vp9_first_pass(cpi, source);
   5327   } else if (oxcf->pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
   5328     Pass2Encode(cpi, size, dest, frame_flags);
   5329   } else if (cpi->use_svc) {
   5330     SvcEncode(cpi, size, dest, frame_flags);
   5331   } else {
   5332     // One pass encode
   5333     Pass0Encode(cpi, size, dest, frame_flags);
   5334   }
   5335 #endif  // CONFIG_REALTIME_ONLY
   5336 
   5337   if (cm->refresh_frame_context)
   5338     cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
   5339 
   5340   // No frame encoded, or frame was dropped, release scaled references.
   5341   if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
   5342     release_scaled_references(cpi);
   5343   }
   5344 
   5345   if (*size > 0) {
   5346     cpi->droppable = !frame_is_reference(cpi);
   5347   }
   5348 
   5349   // Save layer specific state.
   5350   if (is_one_pass_cbr_svc(cpi) || ((cpi->svc.number_temporal_layers > 1 ||
   5351                                     cpi->svc.number_spatial_layers > 1) &&
   5352                                    oxcf->pass == 2)) {
   5353     vp9_save_layer_context(cpi);
   5354   }
   5355 
   5356   vpx_usec_timer_mark(&cmptimer);
   5357   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
   5358 
   5359   // Should we calculate metrics for the frame.
   5360   if (is_psnr_calc_enabled(cpi)) generate_psnr_packet(cpi);
   5361 
   5362   if (cpi->keep_level_stats && oxcf->pass != 1)
   5363     update_level_info(cpi, size, arf_src_index);
   5364 
   5365 #if CONFIG_INTERNAL_STATS
   5366 
   5367   if (oxcf->pass != 1) {
   5368     double samples = 0.0;
   5369     cpi->bytes += (int)(*size);
   5370 
   5371     if (cm->show_frame) {
   5372       uint32_t bit_depth = 8;
   5373       uint32_t in_bit_depth = 8;
   5374       cpi->count++;
   5375 #if CONFIG_VP9_HIGHBITDEPTH
   5376       if (cm->use_highbitdepth) {
   5377         in_bit_depth = cpi->oxcf.input_bit_depth;
   5378         bit_depth = cm->bit_depth;
   5379       }
   5380 #endif
   5381 
   5382       if (cpi->b_calculate_psnr) {
   5383         YV12_BUFFER_CONFIG *orig = cpi->raw_source_frame;
   5384         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
   5385         YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
   5386         PSNR_STATS psnr;
   5387 #if CONFIG_VP9_HIGHBITDEPTH
   5388         vpx_calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
   5389                              in_bit_depth);
   5390 #else
   5391         vpx_calc_psnr(orig, recon, &psnr);
   5392 #endif  // CONFIG_VP9_HIGHBITDEPTH
   5393 
   5394         adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
   5395                           psnr.psnr[0], &cpi->psnr);
   5396         cpi->total_sq_error += psnr.sse[0];
   5397         cpi->total_samples += psnr.samples[0];
   5398         samples = psnr.samples[0];
   5399 
   5400         {
   5401           PSNR_STATS psnr2;
   5402           double frame_ssim2 = 0, weight = 0;
   5403 #if CONFIG_VP9_POSTPROC
   5404           if (vpx_alloc_frame_buffer(
   5405                   pp, recon->y_crop_width, recon->y_crop_height,
   5406                   cm->subsampling_x, cm->subsampling_y,
   5407 #if CONFIG_VP9_HIGHBITDEPTH
   5408                   cm->use_highbitdepth,
   5409 #endif
   5410                   VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment) < 0) {
   5411             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
   5412                                "Failed to allocate post processing buffer");
   5413           }
   5414           {
   5415             vp9_ppflags_t ppflags;
   5416             ppflags.post_proc_flag = VP9D_DEBLOCK;
   5417             ppflags.deblocking_level = 0;  // not used in vp9_post_proc_frame()
   5418             ppflags.noise_level = 0;       // not used in vp9_post_proc_frame()
   5419             vp9_post_proc_frame(cm, pp, &ppflags);
   5420           }
   5421 #endif
   5422           vpx_clear_system_state();
   5423 
   5424 #if CONFIG_VP9_HIGHBITDEPTH
   5425           vpx_calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
   5426                                cpi->oxcf.input_bit_depth);
   5427 #else
   5428           vpx_calc_psnr(orig, pp, &psnr2);
   5429 #endif  // CONFIG_VP9_HIGHBITDEPTH
   5430 
   5431           cpi->totalp_sq_error += psnr2.sse[0];
   5432           cpi->totalp_samples += psnr2.samples[0];
   5433           adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
   5434                             psnr2.psnr[0], &cpi->psnrp);
   5435 
   5436 #if CONFIG_VP9_HIGHBITDEPTH
   5437           if (cm->use_highbitdepth) {
   5438             frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth,
   5439                                                in_bit_depth);
   5440           } else {
   5441             frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
   5442           }
   5443 #else
   5444           frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
   5445 #endif  // CONFIG_VP9_HIGHBITDEPTH
   5446 
   5447           cpi->worst_ssim = VPXMIN(cpi->worst_ssim, frame_ssim2);
   5448           cpi->summed_quality += frame_ssim2 * weight;
   5449           cpi->summed_weights += weight;
   5450 
   5451 #if CONFIG_VP9_HIGHBITDEPTH
   5452           if (cm->use_highbitdepth) {
   5453             frame_ssim2 = vpx_highbd_calc_ssim(orig, pp, &weight, bit_depth,
   5454                                                in_bit_depth);
   5455           } else {
   5456             frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
   5457           }
   5458 #else
   5459           frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
   5460 #endif  // CONFIG_VP9_HIGHBITDEPTH
   5461 
   5462           cpi->summedp_quality += frame_ssim2 * weight;
   5463           cpi->summedp_weights += weight;
   5464 #if 0
   5465           {
   5466             FILE *f = fopen("q_used.stt", "a");
   5467             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
   5468                     cpi->common.current_video_frame, y2, u2, v2,
   5469                     frame_psnr2, frame_ssim2);
   5470             fclose(f);
   5471           }
   5472 #endif
   5473         }
   5474       }
   5475       if (cpi->b_calculate_blockiness) {
   5476 #if CONFIG_VP9_HIGHBITDEPTH
   5477         if (!cm->use_highbitdepth)
   5478 #endif
   5479         {
   5480           double frame_blockiness = vp9_get_blockiness(
   5481               cpi->Source->y_buffer, cpi->Source->y_stride,
   5482               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
   5483               cpi->Source->y_width, cpi->Source->y_height);
   5484           cpi->worst_blockiness =
   5485               VPXMAX(cpi->worst_blockiness, frame_blockiness);
   5486           cpi->total_blockiness += frame_blockiness;
   5487         }
   5488       }
   5489 
   5490       if (cpi->b_calculate_consistency) {
   5491 #if CONFIG_VP9_HIGHBITDEPTH
   5492         if (!cm->use_highbitdepth)
   5493 #endif
   5494         {
   5495           double this_inconsistency = vpx_get_ssim_metrics(
   5496               cpi->Source->y_buffer, cpi->Source->y_stride,
   5497               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
   5498               cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
   5499               &cpi->metrics, 1);
   5500 
   5501           const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
   5502           double consistency =
   5503               vpx_sse_to_psnr(samples, peak, (double)cpi->total_inconsistency);
   5504           if (consistency > 0.0)
   5505             cpi->worst_consistency =
   5506                 VPXMIN(cpi->worst_consistency, consistency);
   5507           cpi->total_inconsistency += this_inconsistency;
   5508         }
   5509       }
   5510 
   5511       {
   5512         double y, u, v, frame_all;
   5513         frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
   5514                                       &v, bit_depth, in_bit_depth);
   5515         adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
   5516       }
   5517       {
   5518         double y, u, v, frame_all;
   5519         frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v,
   5520                                 bit_depth, in_bit_depth);
   5521         adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
   5522       }
   5523     }
   5524   }
   5525 
   5526 #endif
   5527 
   5528   if (is_two_pass_svc(cpi)) {
   5529     if (cpi->svc.encode_empty_frame_state == ENCODING) {
   5530       cpi->svc.encode_empty_frame_state = ENCODED;
   5531       cpi->svc.encode_intra_empty_frame = 0;
   5532     }
   5533 
   5534     if (cm->show_frame) {
   5535       ++cpi->svc.spatial_layer_to_encode;
   5536       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
   5537         cpi->svc.spatial_layer_to_encode = 0;
   5538 
   5539       // May need the empty frame after an visible frame.
   5540       cpi->svc.encode_empty_frame_state = NEED_TO_ENCODE;
   5541     }
   5542   } else if (is_one_pass_cbr_svc(cpi)) {
   5543     if (cm->show_frame) {
   5544       ++cpi->svc.spatial_layer_to_encode;
   5545       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
   5546         cpi->svc.spatial_layer_to_encode = 0;
   5547     }
   5548   }
   5549 
   5550   vpx_clear_system_state();
   5551   return 0;
   5552 }
   5553 
   5554 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
   5555                               vp9_ppflags_t *flags) {
   5556   VP9_COMMON *cm = &cpi->common;
   5557 #if !CONFIG_VP9_POSTPROC
   5558   (void)flags;
   5559 #endif
   5560 
   5561   if (!cm->show_frame) {
   5562     return -1;
   5563   } else {
   5564     int ret;
   5565 #if CONFIG_VP9_POSTPROC
   5566     ret = vp9_post_proc_frame(cm, dest, flags);
   5567 #else
   5568     if (cm->frame_to_show) {
   5569       *dest = *cm->frame_to_show;
   5570       dest->y_width = cm->width;
   5571       dest->y_height = cm->height;
   5572       dest->uv_width = cm->width >> cm->subsampling_x;
   5573       dest->uv_height = cm->height >> cm->subsampling_y;
   5574       ret = 0;
   5575     } else {
   5576       ret = -1;
   5577     }
   5578 #endif  // !CONFIG_VP9_POSTPROC
   5579     vpx_clear_system_state();
   5580     return ret;
   5581   }
   5582 }
   5583 
   5584 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
   5585                           VPX_SCALING vert_mode) {
   5586   VP9_COMMON *cm = &cpi->common;
   5587   int hr = 0, hs = 0, vr = 0, vs = 0;
   5588 
   5589   if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
   5590 
   5591   Scale2Ratio(horiz_mode, &hr, &hs);
   5592   Scale2Ratio(vert_mode, &vr, &vs);
   5593 
   5594   // always go to the next whole number
   5595   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
   5596   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
   5597   if (cm->current_video_frame) {
   5598     assert(cm->width <= cpi->initial_width);
   5599     assert(cm->height <= cpi->initial_height);
   5600   }
   5601 
   5602   update_frame_size(cpi);
   5603 
   5604   return 0;
   5605 }
   5606 
   5607 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
   5608                          unsigned int height) {
   5609   VP9_COMMON *cm = &cpi->common;
   5610 #if CONFIG_VP9_HIGHBITDEPTH
   5611   check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
   5612 #else
   5613   check_initial_width(cpi, 1, 1);
   5614 #endif  // CONFIG_VP9_HIGHBITDEPTH
   5615 
   5616 #if CONFIG_VP9_TEMPORAL_DENOISING
   5617   setup_denoiser_buffer(cpi);
   5618 #endif
   5619 
   5620   if (width) {
   5621     cm->width = width;
   5622     if (cm->width > cpi->initial_width) {
   5623       cm->width = cpi->initial_width;
   5624       printf("Warning: Desired width too large, changed to %d\n", cm->width);
   5625     }
   5626   }
   5627 
   5628   if (height) {
   5629     cm->height = height;
   5630     if (cm->height > cpi->initial_height) {
   5631       cm->height = cpi->initial_height;
   5632       printf("Warning: Desired height too large, changed to %d\n", cm->height);
   5633     }
   5634   }
   5635   assert(cm->width <= cpi->initial_width);
   5636   assert(cm->height <= cpi->initial_height);
   5637 
   5638   update_frame_size(cpi);
   5639 
   5640   return 0;
   5641 }
   5642 
   5643 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
   5644   cpi->use_svc = use_svc;
   5645   return;
   5646 }
   5647 
   5648 int vp9_get_quantizer(VP9_COMP *cpi) { return cpi->common.base_qindex; }
   5649 
   5650 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
   5651   if (flags &
   5652       (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
   5653     int ref = 7;
   5654 
   5655     if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP9_LAST_FLAG;
   5656 
   5657     if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP9_GOLD_FLAG;
   5658 
   5659     if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP9_ALT_FLAG;
   5660 
   5661     vp9_use_as_reference(cpi, ref);
   5662   }
   5663 
   5664   if (flags &
   5665       (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
   5666        VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
   5667     int upd = 7;
   5668 
   5669     if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP9_LAST_FLAG;
   5670 
   5671     if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP9_GOLD_FLAG;
   5672 
   5673     if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP9_ALT_FLAG;
   5674 
   5675     vp9_update_reference(cpi, upd);
   5676   }
   5677 
   5678   if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
   5679     vp9_update_entropy(cpi, 0);
   5680   }
   5681 }
   5682 
   5683 void vp9_set_row_mt(VP9_COMP *cpi) {
   5684   // Enable row based multi-threading for supported modes of encoding
   5685   cpi->row_mt = 0;
   5686   if (((cpi->oxcf.mode == GOOD || cpi->oxcf.mode == BEST) &&
   5687        cpi->oxcf.speed < 5 && cpi->oxcf.pass == 1) &&
   5688       cpi->oxcf.row_mt && !cpi->use_svc)
   5689     cpi->row_mt = 1;
   5690 
   5691   if (cpi->oxcf.mode == GOOD && cpi->oxcf.speed < 5 &&
   5692       (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) && cpi->oxcf.row_mt &&
   5693       !cpi->use_svc)
   5694     cpi->row_mt = 1;
   5695 
   5696   // In realtime mode, enable row based multi-threading for all the speed levels
   5697   // where non-rd path is used.
   5698   if (cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cpi->oxcf.row_mt) {
   5699     cpi->row_mt = 1;
   5700   }
   5701 
   5702   if (cpi->row_mt)
   5703     cpi->row_mt_bit_exact = 1;
   5704   else
   5705     cpi->row_mt_bit_exact = 0;
   5706 }
   5707