LCOV - code coverage report
Current view: top level - media/libvpx/libvpx/vp9/encoder - vp9_ratectrl.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 1318 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 55 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13