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 <assert.h>
     12 #include <limits.h>
     13 #include <math.h>
     14 #include <stdio.h>
     15 #include <stdlib.h>
     16 #include <string.h>
     17 
     18 #include "vpx_dsp/vpx_dsp_common.h"
     19 #include "vpx_mem/vpx_mem.h"
     20 #include "vpx_ports/mem.h"
     21 #include "vpx_ports/system_state.h"
     22 
     23 #include "vp9/common/vp9_alloccommon.h"
     24 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
     25 #include "vp9/common/vp9_common.h"
     26 #include "vp9/common/vp9_entropymode.h"
     27 #include "vp9/common/vp9_quant_common.h"
     28 #include "vp9/common/vp9_seg_common.h"
     29 
     30 #include "vp9/encoder/vp9_encodemv.h"
     31 #include "vp9/encoder/vp9_ratectrl.h"
     32 
     33 // Max rate target for 1080P and below encodes under normal circumstances
     34 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
     35 #define MAX_MB_RATE 250
     36 #define MAXRATE_1080P 2025000
     37 
     38 #define DEFAULT_KF_BOOST 2000
     39 #define DEFAULT_GF_BOOST 2000
     40 
     41 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
     42 
     43 #define MIN_BPB_FACTOR 0.005
     44 #define MAX_BPB_FACTOR 50
     45 
     46 #define FRAME_OVERHEAD_BITS 200
     47 
     48 #if CONFIG_VP9_HIGHBITDEPTH
     49 #define ASSIGN_MINQ_TABLE(bit_depth, name) \
     50   do { \
     51     switch (bit_depth) { \
     52       case VPX_BITS_8: \
     53         name = name##_8; \
     54         break; \
     55       case VPX_BITS_10: \
     56         name = name##_10; \
     57         break; \
     58       case VPX_BITS_12: \
     59         name = name##_12; \
     60         break; \
     61       default: \
     62         assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10" \
     63                     " or VPX_BITS_12"); \
     64         name = NULL; \
     65     } \
     66   } while (0)
     67 #else
     68 #define ASSIGN_MINQ_TABLE(bit_depth, name) \
     69   do { \
     70     (void) bit_depth; \
     71     name = name##_8; \
     72   } while (0)
     73 #endif
     74 
     75 // Tables relating active max Q to active min Q
     76 static int kf_low_motion_minq_8[QINDEX_RANGE];
     77 static int kf_high_motion_minq_8[QINDEX_RANGE];
     78 static int arfgf_low_motion_minq_8[QINDEX_RANGE];
     79 static int arfgf_high_motion_minq_8[QINDEX_RANGE];
     80 static int inter_minq_8[QINDEX_RANGE];
     81 static int rtc_minq_8[QINDEX_RANGE];
     82 
     83 #if CONFIG_VP9_HIGHBITDEPTH
     84 static int kf_low_motion_minq_10[QINDEX_RANGE];
     85 static int kf_high_motion_minq_10[QINDEX_RANGE];
     86 static int arfgf_low_motion_minq_10[QINDEX_RANGE];
     87 static int arfgf_high_motion_minq_10[QINDEX_RANGE];
     88 static int inter_minq_10[QINDEX_RANGE];
     89 static int rtc_minq_10[QINDEX_RANGE];
     90 static int kf_low_motion_minq_12[QINDEX_RANGE];
     91 static int kf_high_motion_minq_12[QINDEX_RANGE];
     92 static int arfgf_low_motion_minq_12[QINDEX_RANGE];
     93 static int arfgf_high_motion_minq_12[QINDEX_RANGE];
     94 static int inter_minq_12[QINDEX_RANGE];
     95 static int rtc_minq_12[QINDEX_RANGE];
     96 #endif
     97 
     98 static int gf_high = 2000;
     99 static int gf_low = 400;
    100 static int kf_high = 5000;
    101 static int kf_low = 400;
    102 
    103 // Functions to compute the active minq lookup table entries based on a
    104 // formulaic approach to facilitate easier adjustment of the Q tables.
    105 // The formulae were derived from computing a 3rd order polynomial best
    106 // fit to the original data (after plotting real maxq vs minq (not q index))
    107 static int get_minq_index(double maxq, double x3, double x2, double x1,
    108                           vpx_bit_depth_t bit_depth) {
    109   int i;
    110   const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq,
    111                                    maxq);
    112 
    113   // Special case handling to deal with the step from q2.0
    114   // down to lossless mode represented by q 1.0.
    115   if (minqtarget <= 2.0)
    116     return 0;
    117 
    118   for (i = 0; i < QINDEX_RANGE; i++) {
    119     if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth))
    120       return i;
    121   }
    122 
    123   return QINDEX_RANGE - 1;
    124 }
    125 
    126 static void init_minq_luts(int *kf_low_m, int *kf_high_m,
    127                            int *arfgf_low, int *arfgf_high,
    128                            int *inter, int *rtc, vpx_bit_depth_t bit_depth) {
    129   int i;
    130   for (i = 0; i < QINDEX_RANGE; i++) {
    131     const double maxq = vp9_convert_qindex_to_q(i, bit_depth);
    132     kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
    133     kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
    134     arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
    135     arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
    136     inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth);
    137     rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
    138   }
    139 }
    140 
    141 void vp9_rc_init_minq_luts(void) {
    142   init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
    143                  arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
    144                  inter_minq_8, rtc_minq_8, VPX_BITS_8);
    145 #if CONFIG_VP9_HIGHBITDEPTH
    146   init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
    147                  arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
    148                  inter_minq_10, rtc_minq_10, VPX_BITS_10);
    149   init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
    150                  arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
    151                  inter_minq_12, rtc_minq_12, VPX_BITS_12);
    152 #endif
    153 }
    154 
    155 // These functions use formulaic calculations to make playing with the
    156 // quantizer tables easier. If necessary they can be replaced by lookup
    157 // tables if and when things settle down in the experimental bitstream
    158 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) {
    159   // Convert the index to a real Q value (scaled down to match old Q values)
    160 #if CONFIG_VP9_HIGHBITDEPTH
    161   switch (bit_depth) {
    162     case VPX_BITS_8:
    163       return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
    164     case VPX_BITS_10:
    165       return vp9_ac_quant(qindex, 0, bit_depth) / 16.0;
    166     case VPX_BITS_12:
    167       return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
    168     default:
    169       assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
    170       return -1.0;
    171   }
    172 #else
    173   return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
    174 #endif
    175 }
    176 
    177 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
    178                        double correction_factor,
    179                        vpx_bit_depth_t bit_depth) {
    180   const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
    181   int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
    182 
    183   assert(correction_factor <= MAX_BPB_FACTOR &&
    184          correction_factor >= MIN_BPB_FACTOR);
    185 
    186   // q based adjustment to baseline enumerator
    187   enumerator += (int)(enumerator * q) >> 12;
    188   return (int)(enumerator * correction_factor / q);
    189 }
    190 
    191 int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
    192                            double correction_factor,
    193                            vpx_bit_depth_t bit_depth) {
    194   const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor,
    195                                            bit_depth));
    196   return VPXMAX(FRAME_OVERHEAD_BITS,
    197                 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
    198 }
    199 
    200 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
    201   const RATE_CONTROL *rc = &cpi->rc;
    202   const VP9EncoderConfig *oxcf = &cpi->oxcf;
    203   const int min_frame_target = VPXMAX(rc->min_frame_bandwidth,
    204                                       rc->avg_frame_bandwidth >> 5);
    205   if (target < min_frame_target)
    206     target = min_frame_target;
    207   if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
    208     // If there is an active ARF at this location use the minimum
    209     // bits on this frame even if it is a constructed arf.
    210     // The active maximum quantizer insures that an appropriate
    211     // number of bits will be spent if needed for constructed ARFs.
    212     target = min_frame_target;
    213   }
    214   // Clip the frame target to the maximum allowed value.
    215   if (target > rc->max_frame_bandwidth)
    216     target = rc->max_frame_bandwidth;
    217   if (oxcf->rc_max_inter_bitrate_pct) {
    218     const int max_rate = rc->avg_frame_bandwidth *
    219                          oxcf->rc_max_inter_bitrate_pct / 100;
    220     target = VPXMIN(target, max_rate);
    221   }
    222   return target;
    223 }
    224 
    225 int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
    226   const RATE_CONTROL *rc = &cpi->rc;
    227   const VP9EncoderConfig *oxcf = &cpi->oxcf;
    228   if (oxcf->rc_max_intra_bitrate_pct) {
    229     const int max_rate = rc->avg_frame_bandwidth *
    230                              oxcf->rc_max_intra_bitrate_pct / 100;
    231     target = VPXMIN(target, max_rate);
    232   }
    233   if (target > rc->max_frame_bandwidth)
    234     target = rc->max_frame_bandwidth;
    235   return target;
    236 }
    237 
    238 // Update the buffer level for higher temporal layers, given the encoded current
    239 // temporal layer.
    240 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
    241   int i = 0;
    242   int current_temporal_layer = svc->temporal_layer_id;
    243   for (i = current_temporal_layer + 1;
    244       i < svc->number_temporal_layers; ++i) {
    245     const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
    246                                        svc->number_temporal_layers);
    247     LAYER_CONTEXT *lc = &svc->layer_context[layer];
    248     RATE_CONTROL *lrc = &lc->rc;
    249     int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
    250         encoded_frame_size);
    251     lrc->bits_off_target += bits_off_for_this_layer;
    252 
    253     // Clip buffer level to maximum buffer size for the layer.
    254     lrc->bits_off_target =
    255         VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
    256     lrc->buffer_level = lrc->bits_off_target;
    257   }
    258 }
    259 
    260 // Update the buffer level: leaky bucket model.
    261 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
    262   const VP9_COMMON *const cm = &cpi->common;
    263   RATE_CONTROL *const rc = &cpi->rc;
    264 
    265   // Non-viewable frames are a special case and are treated as pure overhead.
    266   if (!cm->show_frame) {
    267     rc->bits_off_target -= encoded_frame_size;
    268   } else {
    269     rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
    270   }
    271 
    272   // Clip the buffer level to the maximum specified buffer size.
    273   rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
    274 
    275   // For screen-content mode, and if frame-dropper is off, don't let buffer
    276   // level go below threshold, given here as -rc->maximum_ buffer_size.
    277   if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
    278       cpi->oxcf.drop_frames_water_mark == 0)
    279     rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size);
    280 
    281   rc->buffer_level = rc->bits_off_target;
    282 
    283   if (is_one_pass_cbr_svc(cpi)) {
    284     update_layer_buffer_level(&cpi->svc, encoded_frame_size);
    285   }
    286 }
    287 
    288 int vp9_rc_get_default_min_gf_interval(
    289     int width, int height, double framerate) {
    290   // Assume we do not need any constraint lower than 4K 20 fps
    291   static const double factor_safe = 3840 * 2160 * 20.0;
    292   const double factor = width * height * framerate;
    293   const int default_interval =
    294       clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
    295 
    296   if (factor <= factor_safe)
    297     return default_interval;
    298   else
    299     return VPXMAX(default_interval,
    300                   (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
    301   // Note this logic makes:
    302   // 4K24: 5
    303   // 4K30: 6
    304   // 4K60: 12
    305 }
    306 
    307 int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
    308   int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
    309   interval += (interval & 0x01);  // Round to even value
    310   return VPXMAX(interval, min_gf_interval);
    311 }
    312 
    313 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
    314   int i;
    315 
    316   if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
    317     rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
    318     rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
    319   } else {
    320     rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
    321                                        oxcf->best_allowed_q) / 2;
    322     rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
    323                                          oxcf->best_allowed_q) / 2;
    324   }
    325 
    326   rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
    327   rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
    328 
    329   rc->buffer_level =    rc->starting_buffer_level;
    330   rc->bits_off_target = rc->starting_buffer_level;
    331 
    332   rc->rolling_target_bits      = rc->avg_frame_bandwidth;
    333   rc->rolling_actual_bits      = rc->avg_frame_bandwidth;
    334   rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
    335   rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
    336 
    337   rc->total_actual_bits = 0;
    338   rc->total_target_bits = 0;
    339   rc->total_target_vs_actual = 0;
    340 
    341   rc->frames_since_key = 8;  // Sensible default for first frame.
    342   rc->this_key_frame_forced = 0;
    343   rc->next_key_frame_forced = 0;
    344   rc->source_alt_ref_pending = 0;
    345   rc->source_alt_ref_active = 0;
    346 
    347   rc->frames_till_gf_update_due = 0;
    348   rc->ni_av_qi = oxcf->worst_allowed_q;
    349   rc->ni_tot_qi = 0;
    350   rc->ni_frames = 0;
    351 
    352   rc->tot_q = 0.0;
    353   rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
    354 
    355   for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
    356     rc->rate_correction_factors[i] = 1.0;
    357   }
    358 
    359   rc->min_gf_interval = oxcf->min_gf_interval;
    360   rc->max_gf_interval = oxcf->max_gf_interval;
    361   if (rc->min_gf_interval == 0)
    362     rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
    363         oxcf->width, oxcf->height, oxcf->init_framerate);
    364   if (rc->max_gf_interval == 0)
    365     rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
    366         oxcf->init_framerate, rc->min_gf_interval);
    367   rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
    368 }
    369 
    370 int vp9_rc_drop_frame(VP9_COMP *cpi) {
    371   const VP9EncoderConfig *oxcf = &cpi->oxcf;
    372   RATE_CONTROL *const rc = &cpi->rc;
    373 
    374   if (!oxcf->drop_frames_water_mark) {
    375     return 0;
    376   } else {
    377     if (rc->buffer_level < 0) {
    378       // Always drop if buffer is below 0.
    379       return 1;
    380     } else {
    381       // If buffer is below drop_mark, for now just drop every other frame
    382       // (starting with the next frame) until it increases back over drop_mark.
    383       int drop_mark = (int)(oxcf->drop_frames_water_mark *
    384           rc->optimal_buffer_level / 100);
    385       if ((rc->buffer_level > drop_mark) &&
    386           (rc->decimation_factor > 0)) {
    387         --rc->decimation_factor;
    388       } else if (rc->buffer_level <= drop_mark &&
    389           rc->decimation_factor == 0) {
    390         rc->decimation_factor = 1;
    391       }
    392       if (rc->decimation_factor > 0) {
    393         if (rc->decimation_count > 0) {
    394           --rc->decimation_count;
    395           return 1;
    396         } else {
    397           rc->decimation_count = rc->decimation_factor;
    398           return 0;
    399         }
    400       } else {
    401         rc->decimation_count = 0;
    402         return 0;
    403       }
    404     }
    405   }
    406 }
    407 
    408 static double get_rate_correction_factor(const VP9_COMP *cpi) {
    409   const RATE_CONTROL *const rc = &cpi->rc;
    410   double rcf;
    411 
    412   if (cpi->common.frame_type == KEY_FRAME) {
    413     rcf = rc->rate_correction_factors[KF_STD];
    414   } else if (cpi->oxcf.pass == 2) {
    415     RATE_FACTOR_LEVEL rf_lvl =
    416       cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
    417     rcf = rc->rate_correction_factors[rf_lvl];
    418   } else {
    419     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
    420         !rc->is_src_frame_alt_ref && !cpi->use_svc &&
    421         (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
    422       rcf = rc->rate_correction_factors[GF_ARF_STD];
    423     else
    424       rcf = rc->rate_correction_factors[INTER_NORMAL];
    425   }
    426   rcf *= rcf_mult[rc->frame_size_selector];
    427   return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
    428 }
    429 
    430 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
    431   RATE_CONTROL *const rc = &cpi->rc;
    432 
    433   // Normalize RCF to account for the size-dependent scaling factor.
    434   factor /= rcf_mult[cpi->rc.frame_size_selector];
    435 
    436   factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
    437 
    438   if (cpi->common.frame_type == KEY_FRAME) {
    439     rc->rate_correction_factors[KF_STD] = factor;
    440   } else if (cpi->oxcf.pass == 2) {
    441     RATE_FACTOR_LEVEL rf_lvl =
    442       cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
    443     rc->rate_correction_factors[rf_lvl] = factor;
    444   } else {
    445     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
    446         !rc->is_src_frame_alt_ref && !cpi->use_svc &&
    447         (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
    448       rc->rate_correction_factors[GF_ARF_STD] = factor;
    449     else
    450       rc->rate_correction_factors[INTER_NORMAL] = factor;
    451   }
    452 }
    453 
    454 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) {
    455   const VP9_COMMON *const cm = &cpi->common;
    456   int correction_factor = 100;
    457   double rate_correction_factor = get_rate_correction_factor(cpi);
    458   double adjustment_limit;
    459 
    460   int projected_size_based_on_q = 0;
    461 
    462   // Do not update the rate factors for arf overlay frames.
    463   if (cpi->rc.is_src_frame_alt_ref)
    464     return;
    465 
    466   // Clear down mmx registers to allow floating point in what follows
    467   vpx_clear_system_state();
    468 
    469   // Work out how big we would have expected the frame to be at this Q given
    470   // the current correction factor.
    471   // Stay in double to avoid int overflow when values are large
    472   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
    473     projected_size_based_on_q =
    474         vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
    475   } else {
    476     projected_size_based_on_q = vp9_estimate_bits_at_q(cpi->common.frame_type,
    477                                                        cm->base_qindex,
    478                                                        cm->MBs,
    479                                                        rate_correction_factor,
    480                                                        cm->bit_depth);
    481   }
    482   // Work out a size correction factor.
    483   if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
    484     correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
    485                         projected_size_based_on_q);
    486 
    487   // More heavily damped adjustment used if we have been oscillating either side
    488   // of target.
    489   adjustment_limit = 0.25 +
    490       0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
    491 
    492   cpi->rc.q_2_frame = cpi->rc.q_1_frame;
    493   cpi->rc.q_1_frame = cm->base_qindex;
    494   cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
    495   if (correction_factor > 110)
    496     cpi->rc.rc_1_frame = -1;
    497   else if (correction_factor < 90)
    498     cpi->rc.rc_1_frame = 1;
    499   else
    500     cpi->rc.rc_1_frame = 0;
    501 
    502   if (correction_factor > 102) {
    503     // We are not already at the worst allowable quality
    504     correction_factor = (int)(100 + ((correction_factor - 100) *
    505                                   adjustment_limit));
    506     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
    507     // Keep rate_correction_factor within limits
    508     if (rate_correction_factor > MAX_BPB_FACTOR)
    509       rate_correction_factor = MAX_BPB_FACTOR;
    510   } else if (correction_factor < 99) {
    511     // We are not already at the best allowable quality
    512     correction_factor = (int)(100 - ((100 - correction_factor) *
    513                                   adjustment_limit));
    514     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
    515 
    516     // Keep rate_correction_factor within limits
    517     if (rate_correction_factor < MIN_BPB_FACTOR)
    518       rate_correction_factor = MIN_BPB_FACTOR;
    519   }
    520 
    521   set_rate_correction_factor(cpi, rate_correction_factor);
    522 }
    523 
    524 
    525 int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
    526                       int active_best_quality, int active_worst_quality) {
    527   const VP9_COMMON *const cm = &cpi->common;
    528   int q = active_worst_quality;
    529   int last_error = INT_MAX;
    530   int i, target_bits_per_mb, bits_per_mb_at_this_q;
    531   const double correction_factor = get_rate_correction_factor(cpi);
    532 
    533   // Calculate required scaling factor based on target frame size and size of
    534   // frame produced using previous Q.
    535   target_bits_per_mb =
    536       ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
    537 
    538   i = active_best_quality;
    539 
    540   do {
    541     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
    542         cm->seg.enabled &&
    543         cpi->svc.temporal_layer_id == 0) {
    544       bits_per_mb_at_this_q =
    545           (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
    546     } else {
    547       bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i,
    548                                                       correction_factor,
    549                                                       cm->bit_depth);
    550     }
    551 
    552     if (bits_per_mb_at_this_q <= target_bits_per_mb) {
    553       if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
    554         q = i;
    555       else
    556         q = i - 1;
    557 
    558       break;
    559     } else {
    560       last_error = bits_per_mb_at_this_q - target_bits_per_mb;
    561     }
    562   } while (++i <= active_worst_quality);
    563 
    564   // In CBR mode, this makes sure q is between oscillating Qs to prevent
    565   // resonance.
    566   if (cpi->oxcf.rc_mode == VPX_CBR &&
    567       (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
    568       cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
    569     q = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
    570               VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
    571   }
    572   return q;
    573 }
    574 
    575 static int get_active_quality(int q, int gfu_boost, int low, int high,
    576                               int *low_motion_minq, int *high_motion_minq) {
    577   if (gfu_boost > high) {
    578     return low_motion_minq[q];
    579   } else if (gfu_boost < low) {
    580     return high_motion_minq[q];
    581   } else {
    582     const int gap = high - low;
    583     const int offset = high - gfu_boost;
    584     const int qdiff = high_motion_minq[q] - low_motion_minq[q];
    585     const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
    586     return low_motion_minq[q] + adjustment;
    587   }
    588 }
    589 
    590 static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
    591                                  vpx_bit_depth_t bit_depth) {
    592   int *kf_low_motion_minq;
    593   int *kf_high_motion_minq;
    594   ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
    595   ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
    596   return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
    597                             kf_low_motion_minq, kf_high_motion_minq);
    598 }
    599 
    600 static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
    601                                  vpx_bit_depth_t bit_depth) {
    602   int *arfgf_low_motion_minq;
    603   int *arfgf_high_motion_minq;
    604   ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
    605   ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
    606   return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
    607                             arfgf_low_motion_minq, arfgf_high_motion_minq);
    608 }
    609 
    610 static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
    611   const RATE_CONTROL *const rc = &cpi->rc;
    612   const unsigned int curr_frame = cpi->common.current_video_frame;
    613   int active_worst_quality;
    614 
    615   if (cpi->common.frame_type == KEY_FRAME) {
    616     active_worst_quality = curr_frame == 0 ? rc->worst_quality
    617                                            : rc->last_q[KEY_FRAME] * 2;
    618   } else {
    619     if (!rc->is_src_frame_alt_ref &&
    620         (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
    621       active_worst_quality =  curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
    622                                               : rc->last_q[INTER_FRAME];
    623     } else {
    624       active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
    625                                              : rc->last_q[INTER_FRAME] * 2;
    626     }
    627   }
    628   return VPXMIN(active_worst_quality, rc->worst_quality);
    629 }
    630 
    631 // Adjust active_worst_quality level based on buffer level.
    632 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
    633   // Adjust active_worst_quality: If buffer is above the optimal/target level,
    634   // bring active_worst_quality down depending on fullness of buffer.
    635   // If buffer is below the optimal level, let the active_worst_quality go from
    636   // ambient Q (at buffer = optimal level) to worst_quality level
    637   // (at buffer = critical level).
    638   const VP9_COMMON *const cm = &cpi->common;
    639   const RATE_CONTROL *rc = &cpi->rc;
    640   // Buffer level below which we push active_worst to worst_quality.
    641   int64_t critical_level = rc->optimal_buffer_level >> 3;
    642   int64_t buff_lvl_step = 0;
    643   int adjustment = 0;
    644   int active_worst_quality;
    645   int ambient_qp;
    646   unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
    647   if (cm->frame_type == KEY_FRAME)
    648     return rc->worst_quality;
    649   // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
    650   // for the first few frames following key frame. These are both initialized
    651   // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
    652   // So for first few frames following key, the qp of that key frame is weighted
    653   // into the active_worst_quality setting.
    654   ambient_qp = (cm->current_video_frame < num_frames_weight_key) ?
    655                    VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
    656                           rc->avg_frame_qindex[KEY_FRAME]) :
    657                    rc->avg_frame_qindex[INTER_FRAME];
    658   active_worst_quality = VPXMIN(rc->worst_quality, ambient_qp * 5 / 4);
    659   if (rc->buffer_level > rc->optimal_buffer_level) {
    660     // Adjust down.
    661     // Maximum limit for down adjustment, ~30%.
    662     int max_adjustment_down = active_worst_quality / 3;
    663     if (max_adjustment_down) {
    664       buff_lvl_step = ((rc->maximum_buffer_size -
    665                         rc->optimal_buffer_level) / max_adjustment_down);
    666       if (buff_lvl_step)
    667         adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
    668                             buff_lvl_step);
    669       active_worst_quality -= adjustment;
    670     }
    671   } else if (rc->buffer_level > critical_level) {
    672     // Adjust up from ambient Q.
    673     if (critical_level) {
    674       buff_lvl_step = (rc->optimal_buffer_level - critical_level);
    675       if (buff_lvl_step) {
    676         adjustment = (int)((rc->worst_quality - ambient_qp) *
    677                            (rc->optimal_buffer_level - rc->buffer_level) /
    678                            buff_lvl_step);
    679       }
    680       active_worst_quality = ambient_qp + adjustment;
    681     }
    682   } else {
    683     // Set to worst_quality if buffer is below critical level.
    684     active_worst_quality = rc->worst_quality;
    685   }
    686   return active_worst_quality;
    687 }
    688 
    689 static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
    690                                              int *bottom_index,
    691                                              int *top_index) {
    692   const VP9_COMMON *const cm = &cpi->common;
    693   const RATE_CONTROL *const rc = &cpi->rc;
    694   int active_best_quality;
    695   int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
    696   int q;
    697   int *rtc_minq;
    698   ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
    699 
    700   if (frame_is_intra_only(cm)) {
    701     active_best_quality = rc->best_quality;
    702     // Handle the special case for key frames forced when we have reached
    703     // the maximum key frame interval. Here force the Q to a range
    704     // based on the ambient Q to reduce the risk of popping.
    705     if (rc->this_key_frame_forced) {
    706       int qindex = rc->last_boosted_qindex;
    707       double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
    708       int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
    709                                             (last_boosted_q * 0.75),
    710                                             cm->bit_depth);
    711       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
    712     } else if (cm->current_video_frame > 0) {
    713       // not first frame of one pass and kf_boost is set
    714       double q_adj_factor = 1.0;
    715       double q_val;
    716 
    717       active_best_quality =
    718           get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
    719                                 cm->bit_depth);
    720 
    721       // Allow somewhat lower kf minq with small image formats.
    722       if ((cm->width * cm->height) <= (352 * 288)) {
    723         q_adj_factor -= 0.25;
    724       }
    725 
    726       // Convert the adjustment factor to a qindex delta
    727       // on active_best_quality.
    728       q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
    729       active_best_quality += vp9_compute_qdelta(rc, q_val,
    730                                                 q_val * q_adj_factor,
    731                                                 cm->bit_depth);
    732     }
    733   } else if (!rc->is_src_frame_alt_ref &&
    734              !cpi->use_svc &&
    735              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
    736     // Use the lower of active_worst_quality and recent
    737     // average Q as basis for GF/ARF best Q limit unless last frame was
    738     // a key frame.
    739     if (rc->frames_since_key > 1 &&
    740         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
    741       q = rc->avg_frame_qindex[INTER_FRAME];
    742     } else {
    743       q = active_worst_quality;
    744     }
    745     active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
    746   } else {
    747     // Use the lower of active_worst_quality and recent/average Q.
    748     if (cm->current_video_frame > 1) {
    749       if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
    750         active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
    751       else
    752         active_best_quality = rtc_minq[active_worst_quality];
    753     } else {
    754       if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
    755         active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
    756       else
    757         active_best_quality = rtc_minq[active_worst_quality];
    758     }
    759   }
    760 
    761   // Clip the active best and worst quality values to limits
    762   active_best_quality = clamp(active_best_quality,
    763                               rc->best_quality, rc->worst_quality);
    764   active_worst_quality = clamp(active_worst_quality,
    765                                active_best_quality, rc->worst_quality);
    766 
    767   *top_index = active_worst_quality;
    768   *bottom_index = active_best_quality;
    769 
    770 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
    771   // Limit Q range for the adaptive loop.
    772   if (cm->frame_type == KEY_FRAME &&
    773       !rc->this_key_frame_forced  &&
    774       !(cm->current_video_frame == 0)) {
    775     int qdelta = 0;
    776     vpx_clear_system_state();
    777     qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
    778                                         active_worst_quality, 2.0,
    779                                         cm->bit_depth);
    780     *top_index = active_worst_quality + qdelta;
    781     *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
    782   }
    783 #endif
    784 
    785   // Special case code to try and match quality with forced key frames
    786   if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
    787     q = rc->last_boosted_qindex;
    788   } else {
    789     q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
    790                           active_best_quality, active_worst_quality);
    791     if (q > *top_index) {
    792       // Special case when we are targeting the max allowed rate
    793       if (rc->this_frame_target >= rc->max_frame_bandwidth)
    794         *top_index = q;
    795       else
    796         q = *top_index;
    797     }
    798   }
    799   assert(*top_index <= rc->worst_quality &&
    800          *top_index >= rc->best_quality);
    801   assert(*bottom_index <= rc->worst_quality &&
    802          *bottom_index >= rc->best_quality);
    803   assert(q <= rc->worst_quality && q >= rc->best_quality);
    804   return q;
    805 }
    806 
    807 static int get_active_cq_level(const RATE_CONTROL *rc,
    808                                const VP9EncoderConfig *const oxcf) {
    809   static const double cq_adjust_threshold = 0.1;
    810   int active_cq_level = oxcf->cq_level;
    811   if (oxcf->rc_mode == VPX_CQ &&
    812       rc->total_target_bits > 0) {
    813     const double x = (double)rc->total_actual_bits / rc->total_target_bits;
    814     if (x < cq_adjust_threshold) {
    815       active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
    816     }
    817   }
    818   return active_cq_level;
    819 }
    820 
    821 static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
    822                                              int *bottom_index,
    823                                              int *top_index) {
    824   const VP9_COMMON *const cm = &cpi->common;
    825   const RATE_CONTROL *const rc = &cpi->rc;
    826   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    827   const int cq_level = get_active_cq_level(rc, oxcf);
    828   int active_best_quality;
    829   int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
    830   int q;
    831   int *inter_minq;
    832   ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
    833 
    834   if (frame_is_intra_only(cm)) {
    835     // Handle the special case for key frames forced when we have reached
    836     // the maximum key frame interval. Here force the Q to a range
    837     // based on the ambient Q to reduce the risk of popping.
    838     if (rc->this_key_frame_forced) {
    839       int qindex = rc->last_boosted_qindex;
    840       double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
    841       int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
    842                                             last_boosted_q * 0.75,
    843                                             cm->bit_depth);
    844       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
    845     } else {
    846       // not first frame of one pass and kf_boost is set
    847       double q_adj_factor = 1.0;
    848       double q_val;
    849 
    850       active_best_quality =
    851           get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
    852                                 cm->bit_depth);
    853 
    854       // Allow somewhat lower kf minq with small image formats.
    855       if ((cm->width * cm->height) <= (352 * 288)) {
    856         q_adj_factor -= 0.25;
    857       }
    858 
    859       // Convert the adjustment factor to a qindex delta
    860       // on active_best_quality.
    861       q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
    862       active_best_quality += vp9_compute_qdelta(rc, q_val,
    863                                                 q_val * q_adj_factor,
    864                                                 cm->bit_depth);
    865     }
    866   } else if (!rc->is_src_frame_alt_ref &&
    867              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
    868     // Use the lower of active_worst_quality and recent
    869     // average Q as basis for GF/ARF best Q limit unless last frame was
    870     // a key frame.
    871     if (rc->frames_since_key > 1 &&
    872         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
    873       q = rc->avg_frame_qindex[INTER_FRAME];
    874     } else {
    875       q = rc->avg_frame_qindex[KEY_FRAME];
    876     }
    877     // For constrained quality dont allow Q less than the cq level
    878     if (oxcf->rc_mode == VPX_CQ) {
    879       if (q < cq_level)
    880         q = cq_level;
    881 
    882       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
    883 
    884       // Constrained quality use slightly lower active best.
    885       active_best_quality = active_best_quality * 15 / 16;
    886 
    887     } else if (oxcf->rc_mode == VPX_Q) {
    888       if (!cpi->refresh_alt_ref_frame) {
    889         active_best_quality = cq_level;
    890       } else {
    891         active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
    892       }
    893     } else {
    894       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
    895     }
    896   } else {
    897     if (oxcf->rc_mode == VPX_Q) {
    898       active_best_quality = cq_level;
    899     } else {
    900       // Use the lower of active_worst_quality and recent/average Q.
    901       if (cm->current_video_frame > 1)
    902         active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
    903       else
    904         active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
    905       // For the constrained quality mode we don't want
    906       // q to fall below the cq level.
    907       if ((oxcf->rc_mode == VPX_CQ) &&
    908           (active_best_quality < cq_level)) {
    909         active_best_quality = cq_level;
    910       }
    911     }
    912   }
    913 
    914   // Clip the active best and worst quality values to limits
    915   active_best_quality = clamp(active_best_quality,
    916                               rc->best_quality, rc->worst_quality);
    917   active_worst_quality = clamp(active_worst_quality,
    918                                active_best_quality, rc->worst_quality);
    919 
    920   *top_index = active_worst_quality;
    921   *bottom_index = active_best_quality;
    922 
    923 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
    924   {
    925     int qdelta = 0;
    926     vpx_clear_system_state();
    927 
    928     // Limit Q range for the adaptive loop.
    929     if (cm->frame_type == KEY_FRAME &&
    930         !rc->this_key_frame_forced &&
    931         !(cm->current_video_frame == 0)) {
    932       qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
    933                                           active_worst_quality, 2.0,
    934                                           cm->bit_depth);
    935     } else if (!rc->is_src_frame_alt_ref &&
    936                (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
    937       qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
    938                                           active_worst_quality, 1.75,
    939                                           cm->bit_depth);
    940     }
    941     *top_index = active_worst_quality + qdelta;
    942     *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
    943   }
    944 #endif
    945 
    946   if (oxcf->rc_mode == VPX_Q) {
    947     q = active_best_quality;
    948   // Special case code to try and match quality with forced key frames
    949   } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
    950     q = rc->last_boosted_qindex;
    951   } else {
    952     q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
    953                           active_best_quality, active_worst_quality);
    954     if (q > *top_index) {
    955       // Special case when we are targeting the max allowed rate
    956       if (rc->this_frame_target >= rc->max_frame_bandwidth)
    957         *top_index = q;
    958       else
    959         q = *top_index;
    960     }
    961   }
    962 
    963   assert(*top_index <= rc->worst_quality &&
    964          *top_index >= rc->best_quality);
    965   assert(*bottom_index <= rc->worst_quality &&
    966          *bottom_index >= rc->best_quality);
    967   assert(q <= rc->worst_quality && q >= rc->best_quality);
    968   return q;
    969 }
    970 
    971 int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) {
    972   static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
    973     1.00,  // INTER_NORMAL
    974     1.00,  // INTER_HIGH
    975     1.50,  // GF_ARF_LOW
    976     1.75,  // GF_ARF_STD
    977     2.00,  // KF_STD
    978   };
    979   static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] =
    980       {INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME};
    981   const VP9_COMMON *const cm = &cpi->common;
    982   int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level],
    983                                           q, rate_factor_deltas[rf_level],
    984                                           cm->bit_depth);
    985   return qdelta;
    986 }
    987 
    988 #define STATIC_MOTION_THRESH 95
    989 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi,
    990                                          int *bottom_index,
    991                                          int *top_index) {
    992   const VP9_COMMON *const cm = &cpi->common;
    993   const RATE_CONTROL *const rc = &cpi->rc;
    994   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    995   const GF_GROUP *gf_group = &cpi->twopass.gf_group;
    996   const int cq_level = get_active_cq_level(rc, oxcf);
    997   int active_best_quality;
    998   int active_worst_quality = cpi->twopass.active_worst_quality;
    999   int q;
   1000   int *inter_minq;
   1001   ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
   1002 
   1003   if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
   1004     // Handle the special case for key frames forced when we have reached
   1005     // the maximum key frame interval. Here force the Q to a range
   1006     // based on the ambient Q to reduce the risk of popping.
   1007     if (rc->this_key_frame_forced) {
   1008       double last_boosted_q;
   1009       int delta_qindex;
   1010       int qindex;
   1011 
   1012       if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
   1013         qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
   1014         active_best_quality = qindex;
   1015         last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
   1016         delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
   1017                                               last_boosted_q * 1.25,
   1018                                               cm->bit_depth);
   1019         active_worst_quality =
   1020             VPXMIN(qindex + delta_qindex, active_worst_quality);
   1021       } else {
   1022         qindex = rc->last_boosted_qindex;
   1023         last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
   1024         delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
   1025                                               last_boosted_q * 0.75,
   1026                                               cm->bit_depth);
   1027         active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
   1028       }
   1029     } else {
   1030       // Not forced keyframe.
   1031       double q_adj_factor = 1.0;
   1032       double q_val;
   1033       // Baseline value derived from cpi->active_worst_quality and kf boost.
   1034       active_best_quality = get_kf_active_quality(rc, active_worst_quality,
   1035                                                   cm->bit_depth);
   1036 
   1037       // Allow somewhat lower kf minq with small image formats.
   1038       if ((cm->width * cm->height) <= (352 * 288)) {
   1039         q_adj_factor -= 0.25;
   1040       }
   1041 
   1042       // Make a further adjustment based on the kf zero motion measure.
   1043       q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
   1044 
   1045       // Convert the adjustment factor to a qindex delta
   1046       // on active_best_quality.
   1047       q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
   1048       active_best_quality += vp9_compute_qdelta(rc, q_val,
   1049                                                 q_val * q_adj_factor,
   1050                                                 cm->bit_depth);
   1051     }
   1052   } else if (!rc->is_src_frame_alt_ref &&
   1053              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
   1054     // Use the lower of active_worst_quality and recent
   1055     // average Q as basis for GF/ARF best Q limit unless last frame was
   1056     // a key frame.
   1057     if (rc->frames_since_key > 1 &&
   1058         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
   1059       q = rc->avg_frame_qindex[INTER_FRAME];
   1060     } else {
   1061       q = active_worst_quality;
   1062     }
   1063     // For constrained quality dont allow Q less than the cq level
   1064     if (oxcf->rc_mode == VPX_CQ) {
   1065       if (q < cq_level)
   1066         q = cq_level;
   1067 
   1068       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
   1069 
   1070       // Constrained quality use slightly lower active best.
   1071       active_best_quality = active_best_quality * 15 / 16;
   1072 
   1073     } else if (oxcf->rc_mode == VPX_Q) {
   1074       if (!cpi->refresh_alt_ref_frame) {
   1075         active_best_quality = cq_level;
   1076       } else {
   1077        active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
   1078 
   1079         // Modify best quality for second level arfs. For mode VPX_Q this
   1080         // becomes the baseline frame q.
   1081         if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
   1082           active_best_quality = (active_best_quality + cq_level + 1) / 2;
   1083       }
   1084     } else {
   1085       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
   1086     }
   1087   } else {
   1088     if (oxcf->rc_mode == VPX_Q) {
   1089       active_best_quality = cq_level;
   1090     } else {
   1091       active_best_quality = inter_minq[active_worst_quality];
   1092 
   1093       // For the constrained quality mode we don't want
   1094       // q to fall below the cq level.
   1095       if ((oxcf->rc_mode == VPX_CQ) &&
   1096           (active_best_quality < cq_level)) {
   1097         active_best_quality = cq_level;
   1098       }
   1099     }
   1100   }
   1101 
   1102   // Extension to max or min Q if undershoot or overshoot is outside
   1103   // the permitted range.
   1104   if ((cpi->oxcf.rc_mode != VPX_Q) &&
   1105       (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) {
   1106     if (frame_is_intra_only(cm) ||
   1107         (!rc->is_src_frame_alt_ref &&
   1108          (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
   1109       active_best_quality -=
   1110         (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
   1111       active_worst_quality += (cpi->twopass.extend_maxq / 2);
   1112     } else {
   1113       active_best_quality -=
   1114         (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
   1115       active_worst_quality += cpi->twopass.extend_maxq;
   1116     }
   1117   }
   1118 
   1119 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
   1120   vpx_clear_system_state();
   1121   // Static forced key frames Q restrictions dealt with elsewhere.
   1122   if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) ||
   1123       !rc->this_key_frame_forced ||
   1124       (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
   1125     int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index],
   1126                                        active_worst_quality);
   1127     active_worst_quality = VPXMAX(active_worst_quality + qdelta,
   1128                                   active_best_quality);
   1129   }
   1130 #endif
   1131 
   1132   // Modify active_best_quality for downscaled normal frames.
   1133   if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
   1134     int qdelta = vp9_compute_qdelta_by_rate(rc, cm->frame_type,
   1135                                             active_best_quality, 2.0,
   1136                                             cm->bit_depth);
   1137     active_best_quality =
   1138         VPXMAX(active_best_quality + qdelta, rc->best_quality);
   1139   }
   1140 
   1141   active_best_quality = clamp(active_best_quality,
   1142                               rc->best_quality, rc->worst_quality);
   1143   active_worst_quality = clamp(active_worst_quality,
   1144                                active_best_quality, rc->worst_quality);
   1145 
   1146   if (oxcf->rc_mode == VPX_Q) {
   1147     q = active_best_quality;
   1148   // Special case code to try and match quality with forced key frames.
   1149   } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) &&
   1150              rc->this_key_frame_forced) {
   1151     // If static since last kf use better of last boosted and last kf q.
   1152     if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
   1153       q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
   1154     } else {
   1155       q = rc->last_boosted_qindex;
   1156     }
   1157   } else {
   1158     q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
   1159                           active_best_quality, active_worst_quality);
   1160     if (q > active_worst_quality) {
   1161       // Special case when we are targeting the max allowed rate.
   1162       if (rc->this_frame_target >= rc->max_frame_bandwidth)
   1163         active_worst_quality = q;
   1164       else
   1165         q = active_worst_quality;
   1166     }
   1167   }
   1168   clamp(q, active_best_quality, active_worst_quality);
   1169 
   1170   *top_index = active_worst_quality;
   1171   *bottom_index = active_best_quality;
   1172 
   1173   assert(*top_index <= rc->worst_quality &&
   1174          *top_index >= rc->best_quality);
   1175   assert(*bottom_index <= rc->worst_quality &&
   1176          *bottom_index >= rc->best_quality);
   1177   assert(q <= rc->worst_quality && q >= rc->best_quality);
   1178   return q;
   1179 }
   1180 
   1181 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi,
   1182                              int *bottom_index, int *top_index) {
   1183   int q;
   1184   if (cpi->oxcf.pass == 0) {
   1185     if (cpi->oxcf.rc_mode == VPX_CBR)
   1186       q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
   1187     else
   1188       q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
   1189   } else {
   1190     q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
   1191   }
   1192   if (cpi->sf.use_nonrd_pick_mode) {
   1193     if (cpi->sf.force_frame_boost == 1)
   1194       q -= cpi->sf.max_delta_qindex;
   1195 
   1196     if (q < *bottom_index)
   1197       *bottom_index = q;
   1198     else if (q > *top_index)
   1199       *top_index = q;
   1200   }
   1201   return q;
   1202 }
   1203 
   1204 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi,
   1205                                       int frame_target,
   1206                                       int *frame_under_shoot_limit,
   1207                                       int *frame_over_shoot_limit) {
   1208   if (cpi->oxcf.rc_mode == VPX_Q) {
   1209     *frame_under_shoot_limit = 0;
   1210     *frame_over_shoot_limit  = INT_MAX;
   1211   } else {
   1212     // For very small rate targets where the fractional adjustment
   1213     // may be tiny make sure there is at least a minimum range.
   1214     const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100;
   1215     *frame_under_shoot_limit = VPXMAX(frame_target - tolerance - 200, 0);
   1216     *frame_over_shoot_limit = VPXMIN(frame_target + tolerance + 200,
   1217                                      cpi->rc.max_frame_bandwidth);
   1218   }
   1219 }
   1220 
   1221 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
   1222   const VP9_COMMON *const cm = &cpi->common;
   1223   RATE_CONTROL *const rc = &cpi->rc;
   1224 
   1225   rc->this_frame_target = target;
   1226 
   1227   // Modify frame size target when down-scaling.
   1228   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
   1229       rc->frame_size_selector != UNSCALED)
   1230     rc->this_frame_target = (int)(rc->this_frame_target
   1231         * rate_thresh_mult[rc->frame_size_selector]);
   1232 
   1233   // Target rate per SB64 (including partial SB64s.
   1234   rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) /
   1235                              (cm->width * cm->height);
   1236 }
   1237 
   1238 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
   1239   // this frame refreshes means next frames don't unless specified by user
   1240   RATE_CONTROL *const rc = &cpi->rc;
   1241   rc->frames_since_golden = 0;
   1242 
   1243   // Mark the alt ref as done (setting to 0 means no further alt refs pending).
   1244   rc->source_alt_ref_pending = 0;
   1245 
   1246   // Set the alternate reference frame active flag
   1247   rc->source_alt_ref_active = 1;
   1248 }
   1249 
   1250 static void update_golden_frame_stats(VP9_COMP *cpi) {
   1251   RATE_CONTROL *const rc = &cpi->rc;
   1252 
   1253   // Update the Golden frame usage counts.
   1254   if (cpi->refresh_golden_frame) {
   1255     // this frame refreshes means next frames don't unless specified by user
   1256     rc->frames_since_golden = 0;
   1257 
   1258     // If we are not using alt ref in the up and coming group clear the arf
   1259     // active flag.
   1260     if (!rc->source_alt_ref_pending) {
   1261       rc->source_alt_ref_active = 0;
   1262     }
   1263 
   1264     // Decrement count down till next gf
   1265     if (rc->frames_till_gf_update_due > 0)
   1266       rc->frames_till_gf_update_due--;
   1267 
   1268   } else if (!cpi->refresh_alt_ref_frame) {
   1269     // Decrement count down till next gf
   1270     if (rc->frames_till_gf_update_due > 0)
   1271       rc->frames_till_gf_update_due--;
   1272 
   1273     rc->frames_since_golden++;
   1274   }
   1275 }
   1276 
   1277 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
   1278   const VP9_COMMON *const cm = &cpi->common;
   1279   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
   1280   RATE_CONTROL *const rc = &cpi->rc;
   1281   const int qindex = cm->base_qindex;
   1282 
   1283   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
   1284     vp9_cyclic_refresh_postencode(cpi);
   1285   }
   1286 
   1287   // Update rate control heuristics
   1288   rc->projected_frame_size = (int)(bytes_used << 3);
   1289 
   1290   // Post encode loop adjustment of Q prediction.
   1291   vp9_rc_update_rate_correction_factors(cpi);
   1292 
   1293   // Keep a record of last Q and ambient average Q.
   1294   if (cm->frame_type == KEY_FRAME) {
   1295     rc->last_q[KEY_FRAME] = qindex;
   1296     rc->avg_frame_qindex[KEY_FRAME] =
   1297         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
   1298     if (cpi->use_svc) {
   1299       int i = 0;
   1300       SVC *svc = &cpi->svc;
   1301       for (i = 0; i < svc->number_temporal_layers; ++i) {
   1302         const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
   1303                                            svc->number_temporal_layers);
   1304         LAYER_CONTEXT *lc = &svc->layer_context[layer];
   1305         RATE_CONTROL *lrc = &lc->rc;
   1306         lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME];
   1307         lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME];
   1308       }
   1309     }
   1310   } else {
   1311     if (rc->is_src_frame_alt_ref ||
   1312         !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) ||
   1313         (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) {
   1314       rc->last_q[INTER_FRAME] = qindex;
   1315       rc->avg_frame_qindex[INTER_FRAME] =
   1316         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
   1317       rc->ni_frames++;
   1318       rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
   1319       rc->avg_q = rc->tot_q / rc->ni_frames;
   1320       // Calculate the average Q for normal inter frames (not key or GFU
   1321       // frames).
   1322       rc->ni_tot_qi += qindex;
   1323       rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
   1324     }
   1325   }
   1326 
   1327   // Keep record of last boosted (KF/KF/ARF) Q value.
   1328   // If the current frame is coded at a lower Q then we also update it.
   1329   // If all mbs in this group are skipped only update if the Q value is
   1330   // better than that already stored.
   1331   // This is used to help set quality in forced key frames to reduce popping
   1332   if ((qindex < rc->last_boosted_qindex) ||
   1333       (cm->frame_type == KEY_FRAME) ||
   1334       (!rc->constrained_gf_group &&
   1335        (cpi->refresh_alt_ref_frame ||
   1336         (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
   1337     rc->last_boosted_qindex = qindex;
   1338   }
   1339   if (cm->frame_type == KEY_FRAME)
   1340     rc->last_kf_qindex = qindex;
   1341 
   1342   update_buffer_level(cpi, rc->projected_frame_size);
   1343 
   1344   // Rolling monitors of whether we are over or underspending used to help
   1345   // regulate min and Max Q in two pass.
   1346   if (cm->frame_type != KEY_FRAME) {
   1347     rc->rolling_target_bits = ROUND_POWER_OF_TWO(
   1348         rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
   1349     rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
   1350         rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
   1351     rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
   1352         rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
   1353     rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
   1354         rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
   1355   }
   1356 
   1357   // Actual bits spent
   1358   rc->total_actual_bits += rc->projected_frame_size;
   1359   rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
   1360 
   1361   rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
   1362 
   1363   if (!cpi->use_svc || is_two_pass_svc(cpi)) {
   1364     if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
   1365         (cm->frame_type != KEY_FRAME))
   1366       // Update the alternate reference frame stats as appropriate.
   1367       update_alt_ref_frame_stats(cpi);
   1368     else
   1369       // Update the Golden frame stats as appropriate.
   1370       update_golden_frame_stats(cpi);
   1371   }
   1372 
   1373   if (cm->frame_type == KEY_FRAME)
   1374     rc->frames_since_key = 0;
   1375   if (cm->show_frame) {
   1376     rc->frames_since_key++;
   1377     rc->frames_to_key--;
   1378   }
   1379 
   1380   // Trigger the resizing of the next frame if it is scaled.
   1381   if (oxcf->pass != 0) {
   1382     cpi->resize_pending =
   1383         rc->next_frame_size_selector != rc->frame_size_selector;
   1384     rc->frame_size_selector = rc->next_frame_size_selector;
   1385   }
   1386 }
   1387 
   1388 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
   1389   // Update buffer level with zero size, update frame counters, and return.
   1390   update_buffer_level(cpi, 0);
   1391   cpi->rc.frames_since_key++;
   1392   cpi->rc.frames_to_key--;
   1393   cpi->rc.rc_2_frame = 0;
   1394   cpi->rc.rc_1_frame = 0;
   1395 }
   1396 
   1397 // Use this macro to turn on/off use of alt-refs in one-pass mode.
   1398 #define USE_ALTREF_FOR_ONE_PASS   1
   1399 
   1400 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
   1401   static const int af_ratio = 10;
   1402   const RATE_CONTROL *const rc = &cpi->rc;
   1403   int target;
   1404 #if USE_ALTREF_FOR_ONE_PASS
   1405   target = (!rc->is_src_frame_alt_ref &&
   1406             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) ?
   1407       (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
   1408       (rc->baseline_gf_interval + af_ratio - 1) :
   1409       (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
   1410       (rc->baseline_gf_interval + af_ratio - 1);
   1411 #else
   1412   target = rc->avg_frame_bandwidth;
   1413 #endif
   1414   return vp9_rc_clamp_pframe_target_size(cpi, target);
   1415 }
   1416 
   1417 static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
   1418   static const int kf_ratio = 25;
   1419   const RATE_CONTROL *rc = &cpi->rc;
   1420   const int target = rc->avg_frame_bandwidth * kf_ratio;
   1421   return vp9_rc_clamp_iframe_target_size(cpi, target);
   1422 }
   1423 
   1424 void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
   1425   VP9_COMMON *const cm = &cpi->common;
   1426   RATE_CONTROL *const rc = &cpi->rc;
   1427   int target;
   1428   // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
   1429   if (!cpi->refresh_alt_ref_frame &&
   1430       (cm->current_video_frame == 0 ||
   1431        (cpi->frame_flags & FRAMEFLAGS_KEY) ||
   1432        rc->frames_to_key == 0 ||
   1433        (cpi->oxcf.auto_key && 0))) {
   1434     cm->frame_type = KEY_FRAME;
   1435     rc->this_key_frame_forced = cm->current_video_frame != 0 &&
   1436                                 rc->frames_to_key == 0;
   1437     rc->frames_to_key = cpi->oxcf.key_freq;
   1438     rc->kf_boost = DEFAULT_KF_BOOST;
   1439     rc->source_alt_ref_active = 0;
   1440   } else {
   1441     cm->frame_type = INTER_FRAME;
   1442   }
   1443   if (rc->frames_till_gf_update_due == 0) {
   1444     rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
   1445     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
   1446     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
   1447     if (rc->frames_till_gf_update_due > rc->frames_to_key) {
   1448       rc->frames_till_gf_update_due = rc->frames_to_key;
   1449       rc->constrained_gf_group = 1;
   1450     } else {
   1451       rc->constrained_gf_group = 0;
   1452     }
   1453     cpi->refresh_golden_frame = 1;
   1454     rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
   1455     rc->gfu_boost = DEFAULT_GF_BOOST;
   1456   }
   1457   if (cm->frame_type == KEY_FRAME)
   1458     target = calc_iframe_target_size_one_pass_vbr(cpi);
   1459   else
   1460     target = calc_pframe_target_size_one_pass_vbr(cpi);
   1461   vp9_rc_set_frame_target(cpi, target);
   1462 }
   1463 
   1464 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
   1465   const VP9EncoderConfig *oxcf = &cpi->oxcf;
   1466   const RATE_CONTROL *rc = &cpi->rc;
   1467   const SVC *const svc = &cpi->svc;
   1468   const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
   1469   const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
   1470   int min_frame_target =
   1471       VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
   1472   int target;
   1473 
   1474   if (oxcf->gf_cbr_boost_pct) {
   1475     const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
   1476     target =  cpi->refresh_golden_frame ?
   1477       (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) /
   1478       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) :
   1479       (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
   1480       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
   1481   } else {
   1482     target = rc->avg_frame_bandwidth;
   1483   }
   1484   if (is_one_pass_cbr_svc(cpi)) {
   1485     // Note that for layers, avg_frame_bandwidth is the cumulative
   1486     // per-frame-bandwidth. For the target size of this frame, use the
   1487     // layer average frame size (i.e., non-cumulative per-frame-bw).
   1488     int layer =
   1489         LAYER_IDS_TO_IDX(svc->spatial_layer_id,
   1490             svc->temporal_layer_id, svc->number_temporal_layers);
   1491     const LAYER_CONTEXT *lc = &svc->layer_context[layer];
   1492     target = lc->avg_frame_size;
   1493     min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
   1494   }
   1495   if (diff > 0) {
   1496     // Lower the target bandwidth for this frame.
   1497     const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
   1498     target -= (target * pct_low) / 200;
   1499   } else if (diff < 0) {
   1500     // Increase the target bandwidth for this frame.
   1501     const int pct_high =
   1502         (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
   1503     target += (target * pct_high) / 200;
   1504   }
   1505   if (oxcf->rc_max_inter_bitrate_pct) {
   1506     const int max_rate = rc->avg_frame_bandwidth *
   1507                          oxcf->rc_max_inter_bitrate_pct / 100;
   1508     target = VPXMIN(target, max_rate);
   1509   }
   1510   return VPXMAX(min_frame_target, target);
   1511 }
   1512 
   1513 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
   1514   const RATE_CONTROL *rc = &cpi->rc;
   1515   const VP9EncoderConfig *oxcf = &cpi->oxcf;
   1516   const SVC *const svc = &cpi->svc;
   1517   int target;
   1518   if (cpi->common.current_video_frame == 0) {
   1519     target = ((rc->starting_buffer_level / 2) > INT_MAX)
   1520       ? INT_MAX : (int)(rc->starting_buffer_level / 2);
   1521   } else {
   1522     int kf_boost = 32;
   1523     double framerate = cpi->framerate;
   1524     if (svc->number_temporal_layers > 1 &&
   1525         oxcf->rc_mode == VPX_CBR) {
   1526       // Use the layer framerate for temporal layers CBR mode.
   1527       const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id,
   1528           svc->temporal_layer_id, svc->number_temporal_layers);
   1529       const LAYER_CONTEXT *lc = &svc->layer_context[layer];
   1530       framerate = lc->framerate;
   1531     }
   1532     kf_boost = VPXMAX(kf_boost, (int)(2 * framerate - 16));
   1533     if (rc->frames_since_key <  framerate / 2) {
   1534       kf_boost = (int)(kf_boost * rc->frames_since_key /
   1535                        (framerate / 2));
   1536     }
   1537     target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
   1538   }
   1539   return vp9_rc_clamp_iframe_target_size(cpi, target);
   1540 }
   1541 
   1542 // Reset information needed to set proper reference frames and buffer updates
   1543 // for temporal layering. This is called when a key frame is encoded.
   1544 static void reset_temporal_layer_to_zero(VP9_COMP *cpi) {
   1545   int sl;
   1546   LAYER_CONTEXT *lc = NULL;
   1547   cpi->svc.temporal_layer_id = 0;
   1548 
   1549   for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
   1550     lc = &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers];
   1551     lc->current_video_frame_in_layer = 0;
   1552     lc->frames_from_key_frame = 0;
   1553   }
   1554 }
   1555 
   1556 void vp9_rc_get_svc_params(VP9_COMP *cpi) {
   1557   VP9_COMMON *const cm = &cpi->common;
   1558   RATE_CONTROL *const rc = &cpi->rc;
   1559   int target = rc->avg_frame_bandwidth;
   1560   const int layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
   1561       cpi->svc.temporal_layer_id, cpi->svc.number_temporal_layers);
   1562 
   1563   if ((cm->current_video_frame == 0) ||
   1564       (cpi->frame_flags & FRAMEFLAGS_KEY) ||
   1565       (cpi->oxcf.auto_key && (rc->frames_since_key %
   1566           cpi->oxcf.key_freq == 0))) {
   1567     cm->frame_type = KEY_FRAME;
   1568     rc->source_alt_ref_active = 0;
   1569 
   1570     if (is_two_pass_svc(cpi)) {
   1571       cpi->svc.layer_context[layer].is_key_frame = 1;
   1572       cpi->ref_frame_flags &=
   1573           (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
   1574     } else if (is_one_pass_cbr_svc(cpi)) {
   1575       cpi->svc.layer_context[layer].is_key_frame = 1;
   1576       reset_temporal_layer_to_zero(cpi);
   1577       cpi->ref_frame_flags &=
   1578                 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
   1579       // Assumption here is that LAST_FRAME is being updated for a keyframe.
   1580       // Thus no change in update flags.
   1581       target = calc_iframe_target_size_one_pass_cbr(cpi);
   1582     }
   1583   } else {
   1584     cm->frame_type = INTER_FRAME;
   1585     if (is_two_pass_svc(cpi)) {
   1586       LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
   1587       if (cpi->svc.spatial_layer_id == 0) {
   1588         lc->is_key_frame = 0;
   1589       } else {
   1590         lc->is_key_frame =
   1591             cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
   1592         if (lc->is_key_frame)
   1593           cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
   1594       }
   1595       cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
   1596     } else if (is_one_pass_cbr_svc(cpi)) {
   1597       LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
   1598       if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode) {
   1599         lc->is_key_frame = 0;
   1600       } else {
   1601         lc->is_key_frame =
   1602             cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
   1603       }
   1604       target = calc_pframe_target_size_one_pass_cbr(cpi);
   1605     }
   1606   }
   1607 
   1608   // Any update/change of global cyclic refresh parameters (amount/delta-qp)
   1609   // should be done here, before the frame qp is selected.
   1610   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
   1611     vp9_cyclic_refresh_update_parameters(cpi);
   1612 
   1613   vp9_rc_set_frame_target(cpi, target);
   1614   rc->frames_till_gf_update_due = INT_MAX;
   1615   rc->baseline_gf_interval = INT_MAX;
   1616 }
   1617 
   1618 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
   1619   VP9_COMMON *const cm = &cpi->common;
   1620   RATE_CONTROL *const rc = &cpi->rc;
   1621   int target;
   1622   // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
   1623   if ((cm->current_video_frame == 0 ||
   1624       (cpi->frame_flags & FRAMEFLAGS_KEY) ||
   1625       rc->frames_to_key == 0 ||
   1626       (cpi->oxcf.auto_key && 0))) {
   1627     cm->frame_type = KEY_FRAME;
   1628     rc->this_key_frame_forced = cm->current_video_frame != 0 &&
   1629                                 rc->frames_to_key == 0;
   1630     rc->frames_to_key = cpi->oxcf.key_freq;
   1631     rc->kf_boost = DEFAULT_KF_BOOST;
   1632     rc->source_alt_ref_active = 0;
   1633   } else {
   1634     cm->frame_type = INTER_FRAME;
   1635   }
   1636   if (rc->frames_till_gf_update_due == 0) {
   1637     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
   1638       vp9_cyclic_refresh_set_golden_update(cpi);
   1639     else
   1640       rc->baseline_gf_interval =
   1641           (rc->min_gf_interval + rc->max_gf_interval) / 2;
   1642     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
   1643     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
   1644     if (rc->frames_till_gf_update_due > rc->frames_to_key)
   1645       rc->frames_till_gf_update_due = rc->frames_to_key;
   1646     cpi->refresh_golden_frame = 1;
   1647     rc->gfu_boost = DEFAULT_GF_BOOST;
   1648   }
   1649 
   1650   // Any update/change of global cyclic refresh parameters (amount/delta-qp)
   1651   // should be done here, before the frame qp is selected.
   1652   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
   1653     vp9_cyclic_refresh_update_parameters(cpi);
   1654 
   1655   if (cm->frame_type == KEY_FRAME)
   1656     target = calc_iframe_target_size_one_pass_cbr(cpi);
   1657   else
   1658     target = calc_pframe_target_size_one_pass_cbr(cpi);
   1659 
   1660   vp9_rc_set_frame_target(cpi, target);
   1661   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC)
   1662     cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
   1663   else
   1664     cpi->resize_pending = 0;
   1665 }
   1666 
   1667 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
   1668                        vpx_bit_depth_t bit_depth) {
   1669   int start_index = rc->worst_quality;
   1670   int target_index = rc->worst_quality;
   1671   int i;
   1672 
   1673   // Convert the average q value to an index.
   1674   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
   1675     start_index = i;
   1676     if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart)
   1677       break;
   1678   }
   1679 
   1680   // Convert the q target to an index
   1681   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
   1682     target_index = i;
   1683     if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget)
   1684       break;
   1685   }
   1686 
   1687   return target_index - start_index;
   1688 }
   1689 
   1690 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
   1691                                int qindex, double rate_target_ratio,
   1692                                vpx_bit_depth_t bit_depth) {
   1693   int target_index = rc->worst_quality;
   1694   int i;
   1695 
   1696   // Look up the current projected bits per block for the base index
   1697   const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0,
   1698                                                   bit_depth);
   1699 
   1700   // Find the target bits per mb based on the base value and given ratio.
   1701   const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
   1702 
   1703   // Convert the q target to an index
   1704   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
   1705     if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
   1706         target_bits_per_mb) {
   1707       target_index = i;
   1708       break;
   1709     }
   1710   }
   1711   return target_index - qindex;
   1712 }
   1713 
   1714 void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi,
   1715                                   RATE_CONTROL *const rc) {
   1716   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
   1717 
   1718   // Set Maximum gf/arf interval
   1719   rc->max_gf_interval = oxcf->max_gf_interval;
   1720   rc->min_gf_interval = oxcf->min_gf_interval;
   1721   if (rc->min_gf_interval == 0)
   1722     rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
   1723         oxcf->width, oxcf->height, cpi->framerate);
   1724   if (rc->max_gf_interval == 0)
   1725     rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
   1726         cpi->framerate, rc->min_gf_interval);
   1727 
   1728   // Extended interval for genuinely static scenes
   1729   rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
   1730 
   1731   if (is_altref_enabled(cpi)) {
   1732     if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
   1733       rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
   1734   }
   1735 
   1736   if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
   1737     rc->max_gf_interval = rc->static_scene_max_gf_interval;
   1738 
   1739   // Clamp min to max
   1740   rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
   1741 }
   1742 
   1743 void vp9_rc_update_framerate(VP9_COMP *cpi) {
   1744   const VP9_COMMON *const cm = &cpi->common;
   1745   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
   1746   RATE_CONTROL *const rc = &cpi->rc;
   1747   int vbr_max_bits;
   1748 
   1749   rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
   1750   rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth *
   1751                                 oxcf->two_pass_vbrmin_section / 100);
   1752 
   1753   rc->min_frame_bandwidth =
   1754       VPXMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
   1755 
   1756   // A maximum bitrate for a frame is defined.
   1757   // The baseline for this aligns with HW implementations that
   1758   // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
   1759   // per 16x16 MB (averaged over a frame). However this limit is extended if
   1760   // a very high rate is given on the command line or the the rate cannnot
   1761   // be acheived because of a user specificed max q (e.g. when the user
   1762   // specifies lossless encode.
   1763   vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth *
   1764                      oxcf->two_pass_vbrmax_section) / 100);
   1765   rc->max_frame_bandwidth =
   1766       VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
   1767 
   1768   vp9_rc_set_gf_interval_range(cpi, rc);
   1769 }
   1770 
   1771 #define VBR_PCT_ADJUSTMENT_LIMIT 50
   1772 // For VBR...adjustment to the frame target based on error from previous frames
   1773 static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
   1774   RATE_CONTROL *const rc = &cpi->rc;
   1775   int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
   1776   int max_delta;
   1777   double position_factor = 1.0;
   1778 
   1779   // How far through the clip are we.
   1780   // This number is used to damp the per frame rate correction.
   1781   // Range 0 - 1.0
   1782   if (cpi->twopass.total_stats.count) {
   1783     position_factor = sqrt((double)cpi->common.current_video_frame /
   1784                            cpi->twopass.total_stats.count);
   1785   }
   1786   max_delta = (int)(position_factor *
   1787                     ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
   1788 
   1789   // vbr_bits_off_target > 0 means we have extra bits to spend
   1790   if (vbr_bits_off_target > 0) {
   1791     *this_frame_target +=
   1792       (vbr_bits_off_target > max_delta) ? max_delta
   1793                                         : (int)vbr_bits_off_target;
   1794   } else {
   1795     *this_frame_target -=
   1796       (vbr_bits_off_target < -max_delta) ? max_delta
   1797                                          : (int)-vbr_bits_off_target;
   1798   }
   1799 
   1800   // Fast redistribution of bits arising from massive local undershoot.
   1801   // Dont do it for kf,arf,gf or overlay frames.
   1802   if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
   1803       rc->vbr_bits_off_target_fast) {
   1804     int one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, *this_frame_target);
   1805     int fast_extra_bits;
   1806     fast_extra_bits = (int)VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
   1807     fast_extra_bits = (int)VPXMIN(
   1808         fast_extra_bits,
   1809         VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
   1810     *this_frame_target += (int)fast_extra_bits;
   1811     rc->vbr_bits_off_target_fast -= fast_extra_bits;
   1812   }
   1813 }
   1814 
   1815 void vp9_set_target_rate(VP9_COMP *cpi) {
   1816   RATE_CONTROL *const rc = &cpi->rc;
   1817   int target_rate = rc->base_frame_target;
   1818 
   1819   if (cpi->common.frame_type == KEY_FRAME)
   1820     target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
   1821   else
   1822     target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
   1823 
   1824   // Correction to rate target based on prior over or under shoot.
   1825   if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
   1826     vbr_rate_correction(cpi, &target_rate);
   1827   vp9_rc_set_frame_target(cpi, target_rate);
   1828 }
   1829 
   1830 // Check if we should resize, based on average QP from past x frames.
   1831 // Only allow for resize at most one scale down for now, scaling factor is 2.
   1832 int vp9_resize_one_pass_cbr(VP9_COMP *cpi) {
   1833   const VP9_COMMON *const cm = &cpi->common;
   1834   RATE_CONTROL *const rc = &cpi->rc;
   1835   RESIZE_ACTION resize_action = NO_RESIZE;
   1836   int avg_qp_thr1 = 70;
   1837   int avg_qp_thr2 = 50;
   1838   cpi->resize_scale_num = 1;
   1839   cpi->resize_scale_den = 1;
   1840   // Don't resize on key frame; reset the counters on key frame.
   1841   if (cm->frame_type == KEY_FRAME) {
   1842     cpi->resize_avg_qp = 0;
   1843     cpi->resize_count = 0;
   1844     return 0;
   1845   }
   1846 
   1847 #if CONFIG_VP9_TEMPORAL_DENOISING
   1848   // If denoiser is on, apply a smaller qp threshold.
   1849   if (cpi->oxcf.noise_sensitivity > 0) {
   1850     avg_qp_thr1 = 60;
   1851     avg_qp_thr2 = 40;
   1852   }
   1853 #endif
   1854 
   1855   // Resize based on average buffer underflow and QP over some window.
   1856   // Ignore samples close to key frame, since QP is usually high after key.
   1857   if (cpi->rc.frames_since_key > 1 * cpi->framerate) {
   1858     const int window = (int)(4 * cpi->framerate);
   1859     cpi->resize_avg_qp += cm->base_qindex;
   1860     if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100))
   1861       ++cpi->resize_buffer_underflow;
   1862     ++cpi->resize_count;
   1863     // Check for resize action every "window" frames.
   1864     if (cpi->resize_count >= window) {
   1865       int avg_qp = cpi->resize_avg_qp / cpi->resize_count;
   1866       // Resize down if buffer level has underflowed sufficient amount in past
   1867       // window, and we are at original or 3/4 of original resolution.
   1868       // Resize back up if average QP is low, and we are currently in a resized
   1869       // down state, i.e. 1/2 or 3/4 of original resolution.
   1870       // Currently, use a flag to turn 3/4 resizing feature on/off.
   1871       if (cpi->resize_buffer_underflow > (cpi->resize_count >> 2)) {
   1872         if (cpi->resize_state == THREE_QUARTER) {
   1873           resize_action = DOWN_ONEHALF;
   1874           cpi->resize_state = ONE_HALF;
   1875         } else if (cpi->resize_state == ORIG) {
   1876           resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
   1877           cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
   1878         }
   1879       } else if (cpi->resize_state != ORIG &&
   1880                  avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
   1881         if (cpi->resize_state == THREE_QUARTER ||
   1882             avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100 ||
   1883             ONEHALFONLY_RESIZE) {
   1884           resize_action = UP_ORIG;
   1885           cpi->resize_state = ORIG;
   1886         } else if (cpi->resize_state == ONE_HALF) {
   1887           resize_action = UP_THREEFOUR;
   1888           cpi->resize_state = THREE_QUARTER;
   1889         }
   1890       }
   1891       // Reset for next window measurement.
   1892       cpi->resize_avg_qp = 0;
   1893       cpi->resize_count = 0;
   1894       cpi->resize_buffer_underflow = 0;
   1895     }
   1896   }
   1897   // If decision is to resize, reset some quantities, and check is we should
   1898   // reduce rate correction factor,
   1899   if (resize_action != NO_RESIZE) {
   1900     int target_bits_per_frame;
   1901     int active_worst_quality;
   1902     int qindex;
   1903     int tot_scale_change;
   1904     if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
   1905       cpi->resize_scale_num = 3;
   1906       cpi->resize_scale_den = 4;
   1907     } else if (resize_action == DOWN_ONEHALF) {
   1908       cpi->resize_scale_num = 1;
   1909       cpi->resize_scale_den = 2;
   1910     } else {  // UP_ORIG or anything else
   1911       cpi->resize_scale_num = 1;
   1912       cpi->resize_scale_den = 1;
   1913     }
   1914     tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) /
   1915         (cpi->resize_scale_num * cpi->resize_scale_num);
   1916     // Reset buffer level to optimal, update target size.
   1917     rc->buffer_level = rc->optimal_buffer_level;
   1918     rc->bits_off_target = rc->optimal_buffer_level;
   1919     rc->this_frame_target = calc_pframe_target_size_one_pass_cbr(cpi);
   1920     // Get the projected qindex, based on the scaled target frame size (scaled
   1921     // so target_bits_per_mb in vp9_rc_regulate_q will be correct target).
   1922     target_bits_per_frame = (resize_action >= 0) ?
   1923         rc->this_frame_target * tot_scale_change :
   1924         rc->this_frame_target / tot_scale_change;
   1925     active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
   1926     qindex = vp9_rc_regulate_q(cpi,
   1927                                target_bits_per_frame,
   1928                                rc->best_quality,
   1929                                active_worst_quality);
   1930     // If resize is down, check if projected q index is close to worst_quality,
   1931     // and if so, reduce the rate correction factor (since likely can afford
   1932     // lower q for resized frame).
   1933     if (resize_action > 0 &&
   1934         qindex > 90 * cpi->rc.worst_quality / 100) {
   1935       rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
   1936     }
   1937     // If resize is back up, check if projected q index is too much above the
   1938     // current base_qindex, and if so, reduce the rate correction factor
   1939     // (since prefer to keep q for resized frame at least close to previous q).
   1940     if (resize_action < 0 &&
   1941        qindex > 130 * cm->base_qindex / 100) {
   1942       rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
   1943     }
   1944   }
   1945   return resize_action;
   1946 }
   1947 
   1948 // Compute average source sad (temporal sad: between current source and
   1949 // previous source) over a subset of superblocks. Use this is detect big changes
   1950 // in content and allow rate control to react.
   1951 // TODO(marpan): Superblock sad is computed again in variance partition for
   1952 // non-rd mode (but based on last reconstructed frame). Should try to reuse
   1953 // these computations.
   1954 void vp9_avg_source_sad(VP9_COMP *cpi) {
   1955   VP9_COMMON * const cm = &cpi->common;
   1956   RATE_CONTROL *const rc = &cpi->rc;
   1957   rc->high_source_sad = 0;
   1958   if (cpi->Last_Source != NULL) {
   1959     const uint8_t *src_y = cpi->Source->y_buffer;
   1960     const int src_ystride = cpi->Source->y_stride;
   1961     const uint8_t *last_src_y = cpi->Last_Source->y_buffer;
   1962     const int last_src_ystride = cpi->Last_Source->y_stride;
   1963     int sbi_row, sbi_col;
   1964     const BLOCK_SIZE bsize = BLOCK_64X64;
   1965     // Loop over sub-sample of frame, and compute average sad over 64x64 blocks.
   1966     uint64_t avg_sad = 0;
   1967     int num_samples = 0;
   1968     int sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
   1969     int sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
   1970     for (sbi_row = 0; sbi_row < sb_rows; sbi_row ++) {
   1971       for (sbi_col = 0; sbi_col < sb_cols; sbi_col ++) {
   1972         // Checker-board pattern, ignore boundary.
   1973         if ((sbi_row > 0 && sbi_col > 0) &&
   1974             (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) &&
   1975             ((sbi_row % 2 == 0 && sbi_col % 2 == 0) ||
   1976             (sbi_row % 2 != 0 && sbi_col % 2 != 0))) {
   1977           num_samples++;
   1978           avg_sad += cpi->fn_ptr[bsize].sdf(src_y,
   1979                                             src_ystride,
   1980                                             last_src_y,
   1981                                             last_src_ystride);
   1982         }
   1983         src_y += 64;
   1984         last_src_y += 64;
   1985       }
   1986       src_y += (src_ystride << 6) - (sb_cols << 6);
   1987       last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
   1988     }
   1989     if (num_samples > 0)
   1990       avg_sad = avg_sad / num_samples;
   1991     // Set high_source_sad flag if we detect very high increase in avg_sad
   1992     // between current and the previous frame value(s). Use a minimum threshold
   1993     // for cases where there is small change from content that is completely
   1994     // static.
   1995     if (avg_sad > VPXMAX(4000, (rc->avg_source_sad << 3)) &&
   1996         rc->frames_since_key > 1)
   1997       rc->high_source_sad = 1;
   1998     else
   1999       rc->high_source_sad = 0;
   2000     rc->avg_source_sad = (rc->avg_source_sad + avg_sad) >> 1;
   2001   }
   2002 }
   2003 
   2004 // Test if encoded frame will significantly overshoot the target bitrate, and
   2005 // if so, set the QP, reset/adjust some rate control parameters, and return 1.
   2006 int vp9_encodedframe_overshoot(VP9_COMP *cpi,
   2007                                int frame_size,
   2008                                int *q) {
   2009   VP9_COMMON * const cm = &cpi->common;
   2010   RATE_CONTROL *const rc = &cpi->rc;
   2011   int thresh_qp = 3 * (rc->worst_quality >> 2);
   2012   int thresh_rate = rc->avg_frame_bandwidth * 10;
   2013   if (cm->base_qindex < thresh_qp &&
   2014       frame_size > thresh_rate) {
   2015     double rate_correction_factor =
   2016         cpi->rc.rate_correction_factors[INTER_NORMAL];
   2017     const int target_size = cpi->rc.avg_frame_bandwidth;
   2018     double new_correction_factor;
   2019     int target_bits_per_mb;
   2020     double q2;
   2021     int enumerator;
   2022     // Force a re-encode, and for now use max-QP.
   2023     *q = cpi->rc.worst_quality;
   2024     // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
   2025     // these parameters will affect QP selection for subsequent frames. If they
   2026     // have settled down to a very different (low QP) state, then not adjusting
   2027     // them may cause next frame to select low QP and overshoot again.
   2028     cpi->rc.avg_frame_qindex[INTER_FRAME] = *q;
   2029     rc->buffer_level = rc->optimal_buffer_level;
   2030     rc->bits_off_target = rc->optimal_buffer_level;
   2031     // Reset rate under/over-shoot flags.
   2032     cpi->rc.rc_1_frame = 0;
   2033     cpi->rc.rc_2_frame = 0;
   2034     // Adjust rate correction factor.
   2035     target_bits_per_mb = ((uint64_t)target_size << BPER_MB_NORMBITS) / cm->MBs;
   2036     // Rate correction factor based on target_bits_per_mb and qp (==max_QP).
   2037     // This comes from the inverse computation of vp9_rc_bits_per_mb().
   2038     q2 = vp9_convert_qindex_to_q(*q, cm->bit_depth);
   2039     enumerator = 1800000;  // Factor for inter frame.
   2040     enumerator += (int)(enumerator * q2) >> 12;
   2041     new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
   2042     if (new_correction_factor > rate_correction_factor) {
   2043       rate_correction_factor =
   2044           VPXMIN(2.0 * rate_correction_factor, new_correction_factor);
   2045       if (rate_correction_factor > MAX_BPB_FACTOR)
   2046         rate_correction_factor = MAX_BPB_FACTOR;
   2047       cpi->rc.rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
   2048     }
   2049     // For temporal layers, reset the rate control parametes across all
   2050     // temporal layers.
   2051     if (cpi->use_svc) {
   2052       int i = 0;
   2053       SVC *svc = &cpi->svc;
   2054       for (i = 0; i < svc->number_temporal_layers; ++i) {
   2055         const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
   2056                                            svc->number_temporal_layers);
   2057         LAYER_CONTEXT *lc = &svc->layer_context[layer];
   2058         RATE_CONTROL *lrc = &lc->rc;
   2059         lrc->avg_frame_qindex[INTER_FRAME] = *q;
   2060         lrc->buffer_level = rc->optimal_buffer_level;
   2061         lrc->bits_off_target = rc->optimal_buffer_level;
   2062         lrc->rc_1_frame = 0;
   2063         lrc->rc_2_frame = 0;
   2064         lrc->rate_correction_factors[INTER_NORMAL] =
   2065             rate_correction_factor;
   2066       }
   2067     }
   2068     return 1;
   2069   } else {
   2070     return 0;
   2071   }
   2072 }
   2073