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

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
       3             :  *
       4             :  * This source code is subject to the terms of the BSD 2 Clause License and
       5             :  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
       6             :  * was not distributed with this source code in the LICENSE file, you can
       7             :  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
       8             :  * Media Patent License 1.0 was not distributed with this source code in the
       9             :  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
      10             :  */
      11             : 
      12             : #include <assert.h>
      13             : #include <limits.h>
      14             : #include <math.h>
      15             : #include <stdio.h>
      16             : #include <stdlib.h>
      17             : #include <string.h>
      18             : 
      19             : #include "aom_dsp/aom_dsp_common.h"
      20             : #include "aom_mem/aom_mem.h"
      21             : #include "aom_ports/mem.h"
      22             : #include "aom_ports/system_state.h"
      23             : 
      24             : #include "av1/common/alloccommon.h"
      25             : #include "av1/encoder/aq_cyclicrefresh.h"
      26             : #include "av1/common/common.h"
      27             : #include "av1/common/entropymode.h"
      28             : #include "av1/common/quant_common.h"
      29             : #include "av1/common/seg_common.h"
      30             : 
      31             : #include "av1/encoder/encodemv.h"
      32             : #include "av1/encoder/ratectrl.h"
      33             : 
      34             : // Max rate target for 1080P and below encodes under normal circumstances
      35             : // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
      36             : #define MAX_MB_RATE 250
      37             : #define MAXRATE_1080P 2025000
      38             : 
      39             : #define DEFAULT_KF_BOOST 2000
      40             : #define DEFAULT_GF_BOOST 2000
      41             : 
      42             : #define MIN_BPB_FACTOR 0.005
      43             : #define MAX_BPB_FACTOR 50
      44             : 
      45             : #define FRAME_OVERHEAD_BITS 200
      46             : #if CONFIG_HIGHBITDEPTH
      47             : #define ASSIGN_MINQ_TABLE(bit_depth, name)                   \
      48             :   do {                                                       \
      49             :     switch (bit_depth) {                                     \
      50             :       case AOM_BITS_8: name = name##_8; break;               \
      51             :       case AOM_BITS_10: name = name##_10; break;             \
      52             :       case AOM_BITS_12: name = name##_12; break;             \
      53             :       default:                                               \
      54             :         assert(0 &&                                          \
      55             :                "bit_depth should be AOM_BITS_8, AOM_BITS_10" \
      56             :                " or AOM_BITS_12");                           \
      57             :         name = NULL;                                         \
      58             :     }                                                        \
      59             :   } while (0)
      60             : #else
      61             : #define ASSIGN_MINQ_TABLE(bit_depth, name) \
      62             :   do {                                     \
      63             :     (void)bit_depth;                       \
      64             :     name = name##_8;                       \
      65             :   } while (0)
      66             : #endif
      67             : 
      68             : // Tables relating active max Q to active min Q
      69             : static int kf_low_motion_minq_8[QINDEX_RANGE];
      70             : static int kf_high_motion_minq_8[QINDEX_RANGE];
      71             : static int arfgf_low_motion_minq_8[QINDEX_RANGE];
      72             : static int arfgf_high_motion_minq_8[QINDEX_RANGE];
      73             : static int inter_minq_8[QINDEX_RANGE];
      74             : static int rtc_minq_8[QINDEX_RANGE];
      75             : 
      76             : #if CONFIG_HIGHBITDEPTH
      77             : static int kf_low_motion_minq_10[QINDEX_RANGE];
      78             : static int kf_high_motion_minq_10[QINDEX_RANGE];
      79             : static int arfgf_low_motion_minq_10[QINDEX_RANGE];
      80             : static int arfgf_high_motion_minq_10[QINDEX_RANGE];
      81             : static int inter_minq_10[QINDEX_RANGE];
      82             : static int rtc_minq_10[QINDEX_RANGE];
      83             : static int kf_low_motion_minq_12[QINDEX_RANGE];
      84             : static int kf_high_motion_minq_12[QINDEX_RANGE];
      85             : static int arfgf_low_motion_minq_12[QINDEX_RANGE];
      86             : static int arfgf_high_motion_minq_12[QINDEX_RANGE];
      87             : static int inter_minq_12[QINDEX_RANGE];
      88             : static int rtc_minq_12[QINDEX_RANGE];
      89             : #endif
      90             : 
      91             : static int gf_high = 2000;
      92             : static int gf_low = 400;
      93             : static int kf_high = 5000;
      94             : static int kf_low = 400;
      95             : 
      96           0 : double av1_resize_rate_factor(const AV1_COMP *cpi) {
      97           0 :   return (double)(cpi->resize_scale_den * cpi->resize_scale_den) /
      98           0 :          (cpi->resize_scale_num * cpi->resize_scale_num);
      99             : }
     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             :                           aom_bit_depth_t bit_depth) {
     107             :   int i;
     108           0 :   const double minqtarget = AOMMIN(((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 <= av1_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             :                            aom_bit_depth_t bit_depth) {
     124             :   int i;
     125           0 :   for (i = 0; i < QINDEX_RANGE; i++) {
     126           0 :     const double maxq = av1_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.90, 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 av1_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, AOM_BITS_8);
     140             : #if CONFIG_HIGHBITDEPTH
     141           0 :   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, AOM_BITS_10);
     144           0 :   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, AOM_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 av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth) {
     154             : // Convert the index to a real Q value (scaled down to match old Q values)
     155             : #if CONFIG_HIGHBITDEPTH
     156           0 :   switch (bit_depth) {
     157           0 :     case AOM_BITS_8: return av1_ac_quant(qindex, 0, bit_depth) / 4.0;
     158           0 :     case AOM_BITS_10: return av1_ac_quant(qindex, 0, bit_depth) / 16.0;
     159           0 :     case AOM_BITS_12: return av1_ac_quant(qindex, 0, bit_depth) / 64.0;
     160             :     default:
     161           0 :       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
     162             :       return -1.0;
     163             :   }
     164             : #else
     165             :   return av1_ac_quant(qindex, 0, bit_depth) / 4.0;
     166             : #endif
     167             : }
     168             : 
     169           0 : int av1_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
     170             :                        double correction_factor, aom_bit_depth_t bit_depth) {
     171           0 :   const double q = av1_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 av1_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
     183             :                            double correction_factor,
     184             :                            aom_bit_depth_t bit_depth) {
     185           0 :   const int bpm =
     186             :       (int)(av1_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth));
     187           0 :   return AOMMAX(FRAME_OVERHEAD_BITS,
     188             :                 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
     189             : }
     190             : 
     191           0 : int av1_rc_clamp_pframe_target_size(const AV1_COMP *const cpi, int target) {
     192           0 :   const RATE_CONTROL *rc = &cpi->rc;
     193           0 :   const AV1EncoderConfig *oxcf = &cpi->oxcf;
     194           0 :   const int min_frame_target =
     195           0 :       AOMMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
     196             : // Clip the frame target to the minimum setup value.
     197             : #if CONFIG_EXT_REFS
     198           0 :   if (cpi->rc.is_src_frame_alt_ref) {
     199             : #else
     200             :   if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
     201             : #endif  // CONFIG_EXT_REFS
     202             :     // If there is an active ARF at this location use the minimum
     203             :     // bits on this frame even if it is a constructed arf.
     204             :     // The active maximum quantizer insures that an appropriate
     205             :     // number of bits will be spent if needed for constructed ARFs.
     206           0 :     target = min_frame_target;
     207           0 :   } else if (target < min_frame_target) {
     208           0 :     target = min_frame_target;
     209             :   }
     210             : 
     211             :   // Clip the frame target to the maximum allowed value.
     212           0 :   if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
     213           0 :   if (oxcf->rc_max_inter_bitrate_pct) {
     214           0 :     const int max_rate =
     215           0 :         rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
     216           0 :     target = AOMMIN(target, max_rate);
     217             :   }
     218             : 
     219           0 :   return target;
     220             : }
     221             : 
     222           0 : int av1_rc_clamp_iframe_target_size(const AV1_COMP *const cpi, int target) {
     223           0 :   const RATE_CONTROL *rc = &cpi->rc;
     224           0 :   const AV1EncoderConfig *oxcf = &cpi->oxcf;
     225           0 :   if (oxcf->rc_max_intra_bitrate_pct) {
     226           0 :     const int max_rate =
     227           0 :         rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
     228           0 :     target = AOMMIN(target, max_rate);
     229             :   }
     230           0 :   if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
     231           0 :   return target;
     232             : }
     233             : 
     234             : // Update the buffer level: leaky bucket model.
     235           0 : static void update_buffer_level(AV1_COMP *cpi, int encoded_frame_size) {
     236           0 :   const AV1_COMMON *const cm = &cpi->common;
     237           0 :   RATE_CONTROL *const rc = &cpi->rc;
     238             : 
     239             : // Non-viewable frames are a special case and are treated as pure overhead.
     240             : #if CONFIG_EXT_REFS
     241             :   // TODO(zoeliu): To further explore whether we should treat BWDREF_FRAME
     242             :   //               differently, since it is a no-show frame.
     243           0 :   if (!cm->show_frame && !rc->is_bwd_ref_frame)
     244             : #else
     245             :   if (!cm->show_frame)
     246             : #endif  // CONFIG_EXT_REFS
     247           0 :     rc->bits_off_target -= encoded_frame_size;
     248             :   else
     249           0 :     rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
     250             : 
     251             :   // Clip the buffer level to the maximum specified buffer size.
     252           0 :   rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
     253           0 :   rc->buffer_level = rc->bits_off_target;
     254           0 : }
     255             : 
     256           0 : int av1_rc_get_default_min_gf_interval(int width, int height,
     257             :                                        double framerate) {
     258             :   // Assume we do not need any constraint lower than 4K 20 fps
     259             :   static const double factor_safe = 3840 * 2160 * 20.0;
     260           0 :   const double factor = width * height * framerate;
     261           0 :   const int default_interval =
     262           0 :       clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
     263             : 
     264           0 :   if (factor <= factor_safe)
     265           0 :     return default_interval;
     266             :   else
     267           0 :     return AOMMAX(default_interval,
     268             :                   (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
     269             :   // Note this logic makes:
     270             :   // 4K24: 5
     271             :   // 4K30: 6
     272             :   // 4K60: 12
     273             : }
     274             : 
     275           0 : int av1_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
     276           0 :   int interval = AOMMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
     277           0 :   interval += (interval & 0x01);  // Round to even value
     278           0 :   return AOMMAX(interval, min_gf_interval);
     279             : }
     280             : 
     281           0 : void av1_rc_init(const AV1EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
     282             :   int i;
     283             : 
     284           0 :   if (pass == 0 && oxcf->rc_mode == AOM_CBR) {
     285           0 :     rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
     286           0 :     rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
     287             :   } else {
     288           0 :     rc->avg_frame_qindex[KEY_FRAME] =
     289           0 :         (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
     290           0 :     rc->avg_frame_qindex[INTER_FRAME] =
     291           0 :         (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
     292             :   }
     293             : 
     294           0 :   rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
     295           0 :   rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
     296             : 
     297           0 :   rc->buffer_level = rc->starting_buffer_level;
     298           0 :   rc->bits_off_target = rc->starting_buffer_level;
     299             : 
     300           0 :   rc->rolling_target_bits = rc->avg_frame_bandwidth;
     301           0 :   rc->rolling_actual_bits = rc->avg_frame_bandwidth;
     302           0 :   rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
     303           0 :   rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
     304             : 
     305           0 :   rc->total_actual_bits = 0;
     306           0 :   rc->total_target_bits = 0;
     307           0 :   rc->total_target_vs_actual = 0;
     308             : 
     309           0 :   rc->frames_since_key = 8;  // Sensible default for first frame.
     310           0 :   rc->this_key_frame_forced = 0;
     311           0 :   rc->next_key_frame_forced = 0;
     312           0 :   rc->source_alt_ref_pending = 0;
     313           0 :   rc->source_alt_ref_active = 0;
     314             : 
     315           0 :   rc->frames_till_gf_update_due = 0;
     316           0 :   rc->ni_av_qi = oxcf->worst_allowed_q;
     317           0 :   rc->ni_tot_qi = 0;
     318           0 :   rc->ni_frames = 0;
     319             : 
     320           0 :   rc->tot_q = 0.0;
     321           0 :   rc->avg_q = av1_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
     322             : 
     323           0 :   for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
     324           0 :     rc->rate_correction_factors[i] = 1.0;
     325             :   }
     326             : 
     327           0 :   rc->min_gf_interval = oxcf->min_gf_interval;
     328           0 :   rc->max_gf_interval = oxcf->max_gf_interval;
     329           0 :   if (rc->min_gf_interval == 0)
     330           0 :     rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
     331             :         oxcf->width, oxcf->height, oxcf->init_framerate);
     332           0 :   if (rc->max_gf_interval == 0)
     333           0 :     rc->max_gf_interval = av1_rc_get_default_max_gf_interval(
     334             :         oxcf->init_framerate, rc->min_gf_interval);
     335           0 :   rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
     336           0 : }
     337             : 
     338           0 : int av1_rc_drop_frame(AV1_COMP *cpi) {
     339           0 :   const AV1EncoderConfig *oxcf = &cpi->oxcf;
     340           0 :   RATE_CONTROL *const rc = &cpi->rc;
     341             : 
     342           0 :   if (!oxcf->drop_frames_water_mark) {
     343           0 :     return 0;
     344             :   } else {
     345           0 :     if (rc->buffer_level < 0) {
     346             :       // Always drop if buffer is below 0.
     347           0 :       return 1;
     348             :     } else {
     349             :       // If buffer is below drop_mark, for now just drop every other frame
     350             :       // (starting with the next frame) until it increases back over drop_mark.
     351           0 :       int drop_mark =
     352           0 :           (int)(oxcf->drop_frames_water_mark * rc->optimal_buffer_level / 100);
     353           0 :       if ((rc->buffer_level > drop_mark) && (rc->decimation_factor > 0)) {
     354           0 :         --rc->decimation_factor;
     355           0 :       } else if (rc->buffer_level <= drop_mark && rc->decimation_factor == 0) {
     356           0 :         rc->decimation_factor = 1;
     357             :       }
     358           0 :       if (rc->decimation_factor > 0) {
     359           0 :         if (rc->decimation_count > 0) {
     360           0 :           --rc->decimation_count;
     361           0 :           return 1;
     362             :         } else {
     363           0 :           rc->decimation_count = rc->decimation_factor;
     364           0 :           return 0;
     365             :         }
     366             :       } else {
     367           0 :         rc->decimation_count = 0;
     368           0 :         return 0;
     369             :       }
     370             :     }
     371             :   }
     372             : }
     373             : 
     374           0 : static double get_rate_correction_factor(const AV1_COMP *cpi) {
     375           0 :   const RATE_CONTROL *const rc = &cpi->rc;
     376             :   double rcf;
     377             : 
     378           0 :   if (cpi->common.frame_type == KEY_FRAME) {
     379           0 :     rcf = rc->rate_correction_factors[KF_STD];
     380           0 :   } else if (cpi->oxcf.pass == 2) {
     381           0 :     RATE_FACTOR_LEVEL rf_lvl =
     382           0 :         cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
     383           0 :     rcf = rc->rate_correction_factors[rf_lvl];
     384             :   } else {
     385           0 :     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
     386           0 :         !rc->is_src_frame_alt_ref &&
     387           0 :         (cpi->oxcf.rc_mode != AOM_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
     388           0 :       rcf = rc->rate_correction_factors[GF_ARF_STD];
     389             :     else
     390           0 :       rcf = rc->rate_correction_factors[INTER_NORMAL];
     391             :   }
     392           0 :   rcf *= av1_resize_rate_factor(cpi);
     393           0 :   return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
     394             : }
     395             : 
     396           0 : static void set_rate_correction_factor(AV1_COMP *cpi, double factor) {
     397           0 :   RATE_CONTROL *const rc = &cpi->rc;
     398             : 
     399             :   // Normalize RCF to account for the size-dependent scaling factor.
     400           0 :   factor /= av1_resize_rate_factor(cpi);
     401             : 
     402           0 :   factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
     403             : 
     404           0 :   if (cpi->common.frame_type == KEY_FRAME) {
     405           0 :     rc->rate_correction_factors[KF_STD] = factor;
     406           0 :   } else if (cpi->oxcf.pass == 2) {
     407           0 :     RATE_FACTOR_LEVEL rf_lvl =
     408           0 :         cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
     409           0 :     rc->rate_correction_factors[rf_lvl] = factor;
     410             :   } else {
     411           0 :     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
     412           0 :         !rc->is_src_frame_alt_ref &&
     413           0 :         (cpi->oxcf.rc_mode != AOM_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
     414           0 :       rc->rate_correction_factors[GF_ARF_STD] = factor;
     415             :     else
     416           0 :       rc->rate_correction_factors[INTER_NORMAL] = factor;
     417             :   }
     418           0 : }
     419             : 
     420           0 : void av1_rc_update_rate_correction_factors(AV1_COMP *cpi) {
     421           0 :   const AV1_COMMON *const cm = &cpi->common;
     422           0 :   int correction_factor = 100;
     423           0 :   double rate_correction_factor = get_rate_correction_factor(cpi);
     424             :   double adjustment_limit;
     425             : 
     426           0 :   int projected_size_based_on_q = 0;
     427             : 
     428             :   // Do not update the rate factors for arf overlay frames.
     429           0 :   if (cpi->rc.is_src_frame_alt_ref) return;
     430             : 
     431             :   // Clear down mmx registers to allow floating point in what follows
     432           0 :   aom_clear_system_state();
     433             : 
     434             :   // Work out how big we would have expected the frame to be at this Q given
     435             :   // the current correction factor.
     436             :   // Stay in double to avoid int overflow when values are large
     437           0 :   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
     438           0 :     projected_size_based_on_q =
     439             :         av1_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
     440             :   } else {
     441           0 :     projected_size_based_on_q =
     442           0 :         av1_estimate_bits_at_q(cpi->common.frame_type, cm->base_qindex, cm->MBs,
     443             :                                rate_correction_factor, cm->bit_depth);
     444             :   }
     445             :   // Work out a size correction factor.
     446           0 :   if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
     447           0 :     correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
     448             :                               projected_size_based_on_q);
     449             : 
     450             :   // More heavily damped adjustment used if we have been oscillating either side
     451             :   // of target.
     452           0 :   if (correction_factor > 0) {
     453           0 :     adjustment_limit =
     454           0 :         0.25 + 0.5 * AOMMIN(1, fabs(log10(0.01 * correction_factor)));
     455             :   } else {
     456           0 :     adjustment_limit = 0.75;
     457             :   }
     458             : 
     459           0 :   cpi->rc.q_2_frame = cpi->rc.q_1_frame;
     460           0 :   cpi->rc.q_1_frame = cm->base_qindex;
     461           0 :   cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
     462           0 :   if (correction_factor > 110)
     463           0 :     cpi->rc.rc_1_frame = -1;
     464           0 :   else if (correction_factor < 90)
     465           0 :     cpi->rc.rc_1_frame = 1;
     466             :   else
     467           0 :     cpi->rc.rc_1_frame = 0;
     468             : 
     469           0 :   if (correction_factor > 102) {
     470             :     // We are not already at the worst allowable quality
     471           0 :     correction_factor =
     472           0 :         (int)(100 + ((correction_factor - 100) * adjustment_limit));
     473           0 :     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
     474             :     // Keep rate_correction_factor within limits
     475           0 :     if (rate_correction_factor > MAX_BPB_FACTOR)
     476           0 :       rate_correction_factor = MAX_BPB_FACTOR;
     477           0 :   } else if (correction_factor < 99) {
     478             :     // We are not already at the best allowable quality
     479           0 :     correction_factor =
     480           0 :         (int)(100 - ((100 - correction_factor) * adjustment_limit));
     481           0 :     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
     482             : 
     483             :     // Keep rate_correction_factor within limits
     484           0 :     if (rate_correction_factor < MIN_BPB_FACTOR)
     485           0 :       rate_correction_factor = MIN_BPB_FACTOR;
     486             :   }
     487             : 
     488           0 :   set_rate_correction_factor(cpi, rate_correction_factor);
     489             : }
     490             : 
     491           0 : int av1_rc_regulate_q(const AV1_COMP *cpi, int target_bits_per_frame,
     492             :                       int active_best_quality, int active_worst_quality) {
     493           0 :   const AV1_COMMON *const cm = &cpi->common;
     494           0 :   int q = active_worst_quality;
     495           0 :   int last_error = INT_MAX;
     496             :   int i, target_bits_per_mb, bits_per_mb_at_this_q;
     497           0 :   const double correction_factor = get_rate_correction_factor(cpi);
     498             : 
     499             :   // Calculate required scaling factor based on target frame size and size of
     500             :   // frame produced using previous Q.
     501           0 :   target_bits_per_mb =
     502           0 :       (int)((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
     503             : 
     504           0 :   i = active_best_quality;
     505             : 
     506             :   do {
     507           0 :     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
     508           0 :       bits_per_mb_at_this_q =
     509             :           (int)av1_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
     510             :     } else {
     511           0 :       bits_per_mb_at_this_q = (int)av1_rc_bits_per_mb(
     512             :           cm->frame_type, i, correction_factor, cm->bit_depth);
     513             :     }
     514             : 
     515           0 :     if (bits_per_mb_at_this_q <= target_bits_per_mb) {
     516           0 :       if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
     517           0 :         q = i;
     518             :       else
     519           0 :         q = i - 1;
     520             : 
     521           0 :       break;
     522             :     } else {
     523           0 :       last_error = bits_per_mb_at_this_q - target_bits_per_mb;
     524             :     }
     525           0 :   } while (++i <= active_worst_quality);
     526             : 
     527             :   // In CBR mode, this makes sure q is between oscillating Qs to prevent
     528             :   // resonance.
     529           0 :   if (cpi->oxcf.rc_mode == AOM_CBR &&
     530           0 :       (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
     531           0 :       cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
     532           0 :     q = clamp(q, AOMMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
     533           0 :               AOMMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
     534             :   }
     535           0 :   return q;
     536             : }
     537             : 
     538           0 : static int get_active_quality(int q, int gfu_boost, int low, int high,
     539             :                               int *low_motion_minq, int *high_motion_minq) {
     540           0 :   if (gfu_boost > high) {
     541           0 :     return low_motion_minq[q];
     542           0 :   } else if (gfu_boost < low) {
     543           0 :     return high_motion_minq[q];
     544             :   } else {
     545           0 :     const int gap = high - low;
     546           0 :     const int offset = high - gfu_boost;
     547           0 :     const int qdiff = high_motion_minq[q] - low_motion_minq[q];
     548           0 :     const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
     549           0 :     return low_motion_minq[q] + adjustment;
     550             :   }
     551             : }
     552             : 
     553           0 : static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
     554             :                                  aom_bit_depth_t bit_depth) {
     555             :   int *kf_low_motion_minq;
     556             :   int *kf_high_motion_minq;
     557           0 :   ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
     558           0 :   ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
     559           0 :   return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
     560             :                             kf_low_motion_minq, kf_high_motion_minq);
     561             : }
     562             : 
     563           0 : static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
     564             :                                  aom_bit_depth_t bit_depth) {
     565             :   int *arfgf_low_motion_minq;
     566             :   int *arfgf_high_motion_minq;
     567           0 :   ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
     568           0 :   ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
     569           0 :   return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
     570             :                             arfgf_low_motion_minq, arfgf_high_motion_minq);
     571             : }
     572             : 
     573           0 : static int calc_active_worst_quality_one_pass_vbr(const AV1_COMP *cpi) {
     574           0 :   const RATE_CONTROL *const rc = &cpi->rc;
     575           0 :   const unsigned int curr_frame = cpi->common.current_video_frame;
     576             :   int active_worst_quality;
     577             : 
     578           0 :   if (cpi->common.frame_type == KEY_FRAME) {
     579           0 :     active_worst_quality =
     580           0 :         curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] * 2;
     581             :   } else {
     582           0 :     if (!rc->is_src_frame_alt_ref &&
     583           0 :         (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
     584           0 :       active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
     585           0 :                                              : rc->last_q[INTER_FRAME];
     586             :     } else {
     587           0 :       active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
     588           0 :                                              : rc->last_q[INTER_FRAME] * 2;
     589             :     }
     590             :   }
     591           0 :   return AOMMIN(active_worst_quality, rc->worst_quality);
     592             : }
     593             : 
     594             : // Adjust active_worst_quality level based on buffer level.
     595           0 : static int calc_active_worst_quality_one_pass_cbr(const AV1_COMP *cpi) {
     596             :   // Adjust active_worst_quality: If buffer is above the optimal/target level,
     597             :   // bring active_worst_quality down depending on fullness of buffer.
     598             :   // If buffer is below the optimal level, let the active_worst_quality go from
     599             :   // ambient Q (at buffer = optimal level) to worst_quality level
     600             :   // (at buffer = critical level).
     601           0 :   const AV1_COMMON *const cm = &cpi->common;
     602           0 :   const RATE_CONTROL *rc = &cpi->rc;
     603             :   // Buffer level below which we push active_worst to worst_quality.
     604           0 :   int64_t critical_level = rc->optimal_buffer_level >> 3;
     605           0 :   int64_t buff_lvl_step = 0;
     606           0 :   int adjustment = 0;
     607             :   int active_worst_quality;
     608             :   int ambient_qp;
     609           0 :   if (cm->frame_type == KEY_FRAME) return rc->worst_quality;
     610             :   // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
     611             :   // for the first few frames following key frame. These are both initialized
     612             :   // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
     613             :   // So for first few frames following key, the qp of that key frame is weighted
     614             :   // into the active_worst_quality setting.
     615           0 :   ambient_qp = (cm->current_video_frame < 5)
     616           0 :                    ? AOMMIN(rc->avg_frame_qindex[INTER_FRAME],
     617             :                             rc->avg_frame_qindex[KEY_FRAME])
     618           0 :                    : rc->avg_frame_qindex[INTER_FRAME];
     619           0 :   active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp * 5 / 4);
     620           0 :   if (rc->buffer_level > rc->optimal_buffer_level) {
     621             :     // Adjust down.
     622             :     // Maximum limit for down adjustment, ~30%.
     623           0 :     int max_adjustment_down = active_worst_quality / 3;
     624           0 :     if (max_adjustment_down) {
     625           0 :       buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
     626             :                        max_adjustment_down);
     627           0 :       if (buff_lvl_step)
     628           0 :         adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
     629             :                            buff_lvl_step);
     630           0 :       active_worst_quality -= adjustment;
     631             :     }
     632           0 :   } else if (rc->buffer_level > critical_level) {
     633             :     // Adjust up from ambient Q.
     634           0 :     if (critical_level) {
     635           0 :       buff_lvl_step = (rc->optimal_buffer_level - critical_level);
     636           0 :       if (buff_lvl_step) {
     637           0 :         adjustment = (int)((rc->worst_quality - ambient_qp) *
     638           0 :                            (rc->optimal_buffer_level - rc->buffer_level) /
     639             :                            buff_lvl_step);
     640             :       }
     641           0 :       active_worst_quality = ambient_qp + adjustment;
     642             :     }
     643             :   } else {
     644             :     // Set to worst_quality if buffer is below critical level.
     645           0 :     active_worst_quality = rc->worst_quality;
     646             :   }
     647           0 :   return active_worst_quality;
     648             : }
     649             : 
     650           0 : static int rc_pick_q_and_bounds_one_pass_cbr(const AV1_COMP *cpi,
     651             :                                              int *bottom_index,
     652             :                                              int *top_index) {
     653           0 :   const AV1_COMMON *const cm = &cpi->common;
     654           0 :   const RATE_CONTROL *const rc = &cpi->rc;
     655             :   int active_best_quality;
     656           0 :   int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
     657             :   int q;
     658             :   int *rtc_minq;
     659           0 :   ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
     660             : 
     661           0 :   if (frame_is_intra_only(cm)) {
     662           0 :     active_best_quality = rc->best_quality;
     663             :     // Handle the special case for key frames forced when we have reached
     664             :     // the maximum key frame interval. Here force the Q to a range
     665             :     // based on the ambient Q to reduce the risk of popping.
     666           0 :     if (rc->this_key_frame_forced) {
     667           0 :       int qindex = rc->last_boosted_qindex;
     668           0 :       double last_boosted_q = av1_convert_qindex_to_q(qindex, cm->bit_depth);
     669           0 :       int delta_qindex = av1_compute_qdelta(
     670             :           rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth);
     671           0 :       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
     672           0 :     } else if (cm->current_video_frame > 0) {
     673             :       // not first frame of one pass and kf_boost is set
     674           0 :       double q_adj_factor = 1.0;
     675             :       double q_val;
     676             : 
     677           0 :       active_best_quality = get_kf_active_quality(
     678             :           rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
     679             : 
     680             :       // Allow somewhat lower kf minq with small image formats.
     681           0 :       if ((cm->width * cm->height) <= (352 * 288)) {
     682           0 :         q_adj_factor -= 0.25;
     683             :       }
     684             : 
     685             :       // Convert the adjustment factor to a qindex delta
     686             :       // on active_best_quality.
     687           0 :       q_val = av1_convert_qindex_to_q(active_best_quality, cm->bit_depth);
     688           0 :       active_best_quality +=
     689           0 :           av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
     690             :     }
     691           0 :   } else if (!rc->is_src_frame_alt_ref &&
     692           0 :              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
     693             :     // Use the lower of active_worst_quality and recent
     694             :     // average Q as basis for GF/ARF best Q limit unless last frame was
     695             :     // a key frame.
     696           0 :     if (rc->frames_since_key > 1 &&
     697           0 :         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
     698           0 :       q = rc->avg_frame_qindex[INTER_FRAME];
     699             :     } else {
     700           0 :       q = active_worst_quality;
     701             :     }
     702           0 :     active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
     703             :   } else {
     704             :     // Use the lower of active_worst_quality and recent/average Q.
     705           0 :     if (cm->current_video_frame > 1) {
     706           0 :       if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
     707           0 :         active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
     708             :       else
     709           0 :         active_best_quality = rtc_minq[active_worst_quality];
     710             :     } else {
     711           0 :       if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
     712           0 :         active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
     713             :       else
     714           0 :         active_best_quality = rtc_minq[active_worst_quality];
     715             :     }
     716             :   }
     717             : 
     718             :   // Clip the active best and worst quality values to limits
     719           0 :   active_best_quality =
     720           0 :       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
     721           0 :   active_worst_quality =
     722           0 :       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
     723             : 
     724           0 :   *top_index = active_worst_quality;
     725           0 :   *bottom_index = active_best_quality;
     726             : 
     727             :   // Limit Q range for the adaptive loop.
     728           0 :   if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
     729           0 :       !(cm->current_video_frame == 0)) {
     730           0 :     int qdelta = 0;
     731           0 :     aom_clear_system_state();
     732           0 :     qdelta = av1_compute_qdelta_by_rate(
     733             :         &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
     734           0 :     *top_index = active_worst_quality + qdelta;
     735           0 :     *top_index = AOMMAX(*top_index, *bottom_index);
     736             :   }
     737             : 
     738             :   // Special case code to try and match quality with forced key frames
     739           0 :   if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
     740           0 :     q = rc->last_boosted_qindex;
     741             :   } else {
     742           0 :     q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
     743             :                           active_worst_quality);
     744           0 :     if (q > *top_index) {
     745             :       // Special case when we are targeting the max allowed rate
     746           0 :       if (rc->this_frame_target >= rc->max_frame_bandwidth)
     747           0 :         *top_index = q;
     748             :       else
     749           0 :         q = *top_index;
     750             :     }
     751             :   }
     752             : 
     753           0 :   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
     754           0 :   assert(*bottom_index <= rc->worst_quality &&
     755             :          *bottom_index >= rc->best_quality);
     756           0 :   assert(q <= rc->worst_quality && q >= rc->best_quality);
     757           0 :   return q;
     758             : }
     759             : 
     760           0 : static int get_active_cq_level(const RATE_CONTROL *rc,
     761             :                                const AV1EncoderConfig *const oxcf) {
     762             :   static const double cq_adjust_threshold = 0.1;
     763           0 :   int active_cq_level = oxcf->cq_level;
     764           0 :   if (oxcf->rc_mode == AOM_CQ && rc->total_target_bits > 0) {
     765           0 :     const double x = (double)rc->total_actual_bits / rc->total_target_bits;
     766           0 :     if (x < cq_adjust_threshold) {
     767           0 :       active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
     768             :     }
     769             :   }
     770           0 :   return active_cq_level;
     771             : }
     772             : 
     773           0 : static int rc_pick_q_and_bounds_one_pass_vbr(const AV1_COMP *cpi,
     774             :                                              int *bottom_index,
     775             :                                              int *top_index) {
     776           0 :   const AV1_COMMON *const cm = &cpi->common;
     777           0 :   const RATE_CONTROL *const rc = &cpi->rc;
     778           0 :   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
     779           0 :   const int cq_level = get_active_cq_level(rc, oxcf);
     780             :   int active_best_quality;
     781           0 :   int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
     782             :   int q;
     783             :   int *inter_minq;
     784           0 :   ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
     785             : 
     786           0 :   if (frame_is_intra_only(cm)) {
     787           0 :     if (oxcf->rc_mode == AOM_Q) {
     788           0 :       const int qindex = cq_level;
     789           0 :       const double q_val = av1_convert_qindex_to_q(qindex, cm->bit_depth);
     790           0 :       const int delta_qindex =
     791           0 :           av1_compute_qdelta(rc, q_val, q_val * 0.25, cm->bit_depth);
     792           0 :       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
     793           0 :     } else if (rc->this_key_frame_forced) {
     794           0 :       const int qindex = rc->last_boosted_qindex;
     795           0 :       const double last_boosted_q =
     796           0 :           av1_convert_qindex_to_q(qindex, cm->bit_depth);
     797           0 :       const int delta_qindex = av1_compute_qdelta(
     798             :           rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
     799           0 :       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
     800             :     } else {  // not first frame of one pass and kf_boost is set
     801           0 :       double q_adj_factor = 1.0;
     802             : 
     803           0 :       active_best_quality = get_kf_active_quality(
     804             :           rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
     805             : 
     806             :       // Allow somewhat lower kf minq with small image formats.
     807           0 :       if ((cm->width * cm->height) <= (352 * 288)) {
     808           0 :         q_adj_factor -= 0.25;
     809             :       }
     810             : 
     811             :       // Convert the adjustment factor to a qindex delta on active_best_quality.
     812             :       {
     813           0 :         const double q_val =
     814           0 :             av1_convert_qindex_to_q(active_best_quality, cm->bit_depth);
     815           0 :         active_best_quality +=
     816           0 :             av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
     817             :       }
     818             :     }
     819           0 :   } else if (!rc->is_src_frame_alt_ref &&
     820           0 :              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
     821             :     // Use the lower of active_worst_quality and recent
     822             :     // average Q as basis for GF/ARF best Q limit unless last frame was
     823             :     // a key frame.
     824           0 :     q = (rc->frames_since_key > 1 &&
     825           0 :          rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
     826             :             ? rc->avg_frame_qindex[INTER_FRAME]
     827           0 :             : rc->avg_frame_qindex[KEY_FRAME];
     828             :     // For constrained quality dont allow Q less than the cq level
     829           0 :     if (oxcf->rc_mode == AOM_CQ) {
     830           0 :       if (q < cq_level) q = cq_level;
     831           0 :       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
     832             :       // Constrained quality use slightly lower active best.
     833           0 :       active_best_quality = active_best_quality * 15 / 16;
     834           0 :     } else if (oxcf->rc_mode == AOM_Q) {
     835           0 :       const int qindex = cq_level;
     836           0 :       const double q_val = av1_convert_qindex_to_q(qindex, cm->bit_depth);
     837           0 :       const int delta_qindex =
     838           0 :           (cpi->refresh_alt_ref_frame)
     839           0 :               ? av1_compute_qdelta(rc, q_val, q_val * 0.40, cm->bit_depth)
     840           0 :               : av1_compute_qdelta(rc, q_val, q_val * 0.50, cm->bit_depth);
     841           0 :       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
     842             :     } else {
     843           0 :       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
     844             :     }
     845             :   } else {
     846           0 :     if (oxcf->rc_mode == AOM_Q) {
     847           0 :       const int qindex = cq_level;
     848           0 :       const double q_val = av1_convert_qindex_to_q(qindex, cm->bit_depth);
     849           0 :       const double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
     850             :                                                      0.70, 1.0, 0.85, 1.0 };
     851           0 :       const int delta_qindex = av1_compute_qdelta(
     852             :           rc, q_val,
     853           0 :           q_val * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL],
     854             :           cm->bit_depth);
     855           0 :       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
     856             :     } else {
     857             :       // Use the lower of active_worst_quality and recent/average Q.
     858           0 :       active_best_quality = (cm->current_video_frame > 1)
     859           0 :                                 ? inter_minq[rc->avg_frame_qindex[INTER_FRAME]]
     860           0 :                                 : inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
     861             :       // For the constrained quality mode we don't want
     862             :       // q to fall below the cq level.
     863           0 :       if ((oxcf->rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
     864           0 :         active_best_quality = cq_level;
     865             :       }
     866             :     }
     867             :   }
     868             : 
     869             :   // Clip the active best and worst quality values to limits
     870           0 :   active_best_quality =
     871           0 :       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
     872           0 :   active_worst_quality =
     873           0 :       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
     874             : 
     875           0 :   *top_index = active_worst_quality;
     876           0 :   *bottom_index = active_best_quality;
     877             : 
     878             :   // Limit Q range for the adaptive loop.
     879             :   {
     880           0 :     int qdelta = 0;
     881           0 :     aom_clear_system_state();
     882           0 :     if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
     883           0 :         !(cm->current_video_frame == 0)) {
     884           0 :       qdelta = av1_compute_qdelta_by_rate(
     885             :           &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
     886           0 :     } else if (!rc->is_src_frame_alt_ref &&
     887           0 :                (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
     888           0 :       qdelta = av1_compute_qdelta_by_rate(
     889             :           &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth);
     890             :     }
     891           0 :     *top_index = active_worst_quality + qdelta;
     892           0 :     *top_index = AOMMAX(*top_index, *bottom_index);
     893             :   }
     894             : 
     895           0 :   if (oxcf->rc_mode == AOM_Q) {
     896           0 :     q = active_best_quality;
     897             :     // Special case code to try and match quality with forced key frames
     898           0 :   } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
     899           0 :     q = rc->last_boosted_qindex;
     900             :   } else {
     901           0 :     q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
     902             :                           active_worst_quality);
     903           0 :     if (q > *top_index) {
     904             :       // Special case when we are targeting the max allowed rate
     905           0 :       if (rc->this_frame_target >= rc->max_frame_bandwidth)
     906           0 :         *top_index = q;
     907             :       else
     908           0 :         q = *top_index;
     909             :     }
     910             :   }
     911             : 
     912           0 :   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
     913           0 :   assert(*bottom_index <= rc->worst_quality &&
     914             :          *bottom_index >= rc->best_quality);
     915           0 :   assert(q <= rc->worst_quality && q >= rc->best_quality);
     916           0 :   return q;
     917             : }
     918             : 
     919           0 : int av1_frame_type_qdelta(const AV1_COMP *cpi, int rf_level, int q) {
     920             :   static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
     921             :     1.00,  // INTER_NORMAL
     922             : #if CONFIG_EXT_REFS
     923             :     0.80,  // INTER_LOW
     924             :     1.50,  // INTER_HIGH
     925             :     1.25,  // GF_ARF_LOW
     926             : #else
     927             :     1.00,  // INTER_HIGH
     928             :     1.50,  // GF_ARF_LOW
     929             : #endif     // CONFIG_EXT_REFS
     930             :     2.00,  // GF_ARF_STD
     931             :     2.00,  // KF_STD
     932             :   };
     933             :   static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] =
     934             : #if CONFIG_EXT_REFS
     935             :       { INTER_FRAME, INTER_FRAME, INTER_FRAME,
     936             :         INTER_FRAME, INTER_FRAME, KEY_FRAME };
     937             : #else
     938             :       { INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME };
     939             : #endif  // CONFIG_EXT_REFS
     940           0 :   const AV1_COMMON *const cm = &cpi->common;
     941           0 :   int qdelta =
     942           0 :       av1_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level], q,
     943             :                                  rate_factor_deltas[rf_level], cm->bit_depth);
     944           0 :   return qdelta;
     945             : }
     946             : 
     947             : #define STATIC_MOTION_THRESH 95
     948           0 : static int rc_pick_q_and_bounds_two_pass(const AV1_COMP *cpi, int *bottom_index,
     949             :                                          int *top_index) {
     950           0 :   const AV1_COMMON *const cm = &cpi->common;
     951           0 :   const RATE_CONTROL *const rc = &cpi->rc;
     952           0 :   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
     953           0 :   const GF_GROUP *gf_group = &cpi->twopass.gf_group;
     954           0 :   const int cq_level = get_active_cq_level(rc, oxcf);
     955             :   int active_best_quality;
     956           0 :   int active_worst_quality = cpi->twopass.active_worst_quality;
     957             :   int q;
     958             :   int *inter_minq;
     959           0 :   ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
     960             : 
     961           0 :   if (frame_is_intra_only(cm)) {
     962             :     // Handle the special case for key frames forced when we have reached
     963             :     // the maximum key frame interval. Here force the Q to a range
     964             :     // based on the ambient Q to reduce the risk of popping.
     965           0 :     if (rc->this_key_frame_forced) {
     966             :       double last_boosted_q;
     967             :       int delta_qindex;
     968             :       int qindex;
     969             : 
     970           0 :       if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
     971           0 :         qindex = AOMMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
     972           0 :         active_best_quality = qindex;
     973           0 :         last_boosted_q = av1_convert_qindex_to_q(qindex, cm->bit_depth);
     974           0 :         delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
     975             :                                           last_boosted_q * 1.25, cm->bit_depth);
     976           0 :         active_worst_quality =
     977           0 :             AOMMIN(qindex + delta_qindex, active_worst_quality);
     978             :       } else {
     979           0 :         qindex = rc->last_boosted_qindex;
     980           0 :         last_boosted_q = av1_convert_qindex_to_q(qindex, cm->bit_depth);
     981           0 :         delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
     982             :                                           last_boosted_q * 0.75, cm->bit_depth);
     983           0 :         active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
     984             :       }
     985             :     } else {
     986             :       // Not forced keyframe.
     987           0 :       double q_adj_factor = 1.0;
     988             :       double q_val;
     989             : 
     990             :       // Baseline value derived from cpi->active_worst_quality and kf boost.
     991           0 :       active_best_quality =
     992           0 :           get_kf_active_quality(rc, active_worst_quality, cm->bit_depth);
     993             : 
     994             :       // Allow somewhat lower kf minq with small image formats.
     995           0 :       if ((cm->width * cm->height) <= (352 * 288)) {
     996           0 :         q_adj_factor -= 0.25;
     997             :       }
     998             : 
     999             :       // Make a further adjustment based on the kf zero motion measure.
    1000           0 :       q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
    1001             : 
    1002             :       // Convert the adjustment factor to a qindex delta
    1003             :       // on active_best_quality.
    1004           0 :       q_val = av1_convert_qindex_to_q(active_best_quality, cm->bit_depth);
    1005           0 :       active_best_quality +=
    1006           0 :           av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
    1007             :     }
    1008           0 :   } else if (!rc->is_src_frame_alt_ref &&
    1009           0 :              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
    1010             :     // Use the lower of active_worst_quality and recent
    1011             :     // average Q as basis for GF/ARF best Q limit unless last frame was
    1012             :     // a key frame.
    1013           0 :     if (rc->frames_since_key > 1 &&
    1014           0 :         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
    1015           0 :       q = rc->avg_frame_qindex[INTER_FRAME];
    1016             :     } else {
    1017           0 :       q = active_worst_quality;
    1018             :     }
    1019             :     // For constrained quality dont allow Q less than the cq level
    1020           0 :     if (oxcf->rc_mode == AOM_CQ) {
    1021           0 :       if (q < cq_level) q = cq_level;
    1022             : 
    1023           0 :       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
    1024             : 
    1025             :       // Constrained quality use slightly lower active best.
    1026           0 :       active_best_quality = active_best_quality * 15 / 16;
    1027             : 
    1028           0 :     } else if (oxcf->rc_mode == AOM_Q) {
    1029           0 :       if (!cpi->refresh_alt_ref_frame) {
    1030           0 :         active_best_quality = cq_level;
    1031             :       } else {
    1032           0 :         active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
    1033             : 
    1034             :         // Modify best quality for second level arfs. For mode AOM_Q this
    1035             :         // becomes the baseline frame q.
    1036           0 :         if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
    1037           0 :           active_best_quality = (active_best_quality + cq_level + 1) / 2;
    1038             :       }
    1039             :     } else {
    1040           0 :       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
    1041             :     }
    1042             :   } else {
    1043           0 :     if (oxcf->rc_mode == AOM_Q) {
    1044           0 :       active_best_quality = cq_level;
    1045             :     } else {
    1046           0 :       active_best_quality = inter_minq[active_worst_quality];
    1047             : 
    1048             :       // For the constrained quality mode we don't want
    1049             :       // q to fall below the cq level.
    1050           0 :       if ((oxcf->rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
    1051           0 :         active_best_quality = cq_level;
    1052             :       }
    1053             :     }
    1054             :   }
    1055             : 
    1056             :   // Extension to max or min Q if undershoot or overshoot is outside
    1057             :   // the permitted range.
    1058           0 :   if ((cpi->oxcf.rc_mode != AOM_Q) &&
    1059           0 :       (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) {
    1060           0 :     if (frame_is_intra_only(cm) ||
    1061           0 :         (!rc->is_src_frame_alt_ref &&
    1062           0 :          (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
    1063           0 :       active_best_quality -=
    1064           0 :           (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
    1065           0 :       active_worst_quality += (cpi->twopass.extend_maxq / 2);
    1066             :     } else {
    1067           0 :       active_best_quality -=
    1068           0 :           (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
    1069           0 :       active_worst_quality += cpi->twopass.extend_maxq;
    1070             :     }
    1071             :   }
    1072             : 
    1073           0 :   aom_clear_system_state();
    1074             :   // Static forced key frames Q restrictions dealt with elsewhere.
    1075           0 :   if (!(frame_is_intra_only(cm)) || !rc->this_key_frame_forced ||
    1076           0 :       (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
    1077           0 :     int qdelta = av1_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index],
    1078             :                                        active_worst_quality);
    1079           0 :     active_worst_quality =
    1080           0 :         AOMMAX(active_worst_quality + qdelta, active_best_quality);
    1081             :   }
    1082             : 
    1083             :   // Modify active_best_quality for downscaled normal frames.
    1084           0 :   if (!av1_resize_unscaled(cpi) && !frame_is_kf_gf_arf(cpi)) {
    1085           0 :     int qdelta = av1_compute_qdelta_by_rate(
    1086             :         rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth);
    1087           0 :     active_best_quality =
    1088           0 :         AOMMAX(active_best_quality + qdelta, rc->best_quality);
    1089             :   }
    1090             : 
    1091           0 :   active_best_quality =
    1092           0 :       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
    1093           0 :   active_worst_quality =
    1094           0 :       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
    1095             : 
    1096           0 :   if (oxcf->rc_mode == AOM_Q) {
    1097           0 :     q = active_best_quality;
    1098             :     // Special case code to try and match quality with forced key frames.
    1099           0 :   } else if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
    1100             :     // If static since last kf use better of last boosted and last kf q.
    1101           0 :     if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
    1102           0 :       q = AOMMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
    1103             :     } else {
    1104           0 :       q = rc->last_boosted_qindex;
    1105             :     }
    1106             :   } else {
    1107           0 :     q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
    1108             :                           active_worst_quality);
    1109           0 :     if (q > active_worst_quality) {
    1110             :       // Special case when we are targeting the max allowed rate.
    1111           0 :       if (rc->this_frame_target >= rc->max_frame_bandwidth)
    1112           0 :         active_worst_quality = q;
    1113             :       else
    1114           0 :         q = active_worst_quality;
    1115             :     }
    1116             :   }
    1117           0 :   clamp(q, active_best_quality, active_worst_quality);
    1118             : 
    1119           0 :   *top_index = active_worst_quality;
    1120           0 :   *bottom_index = active_best_quality;
    1121             : 
    1122           0 :   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
    1123           0 :   assert(*bottom_index <= rc->worst_quality &&
    1124             :          *bottom_index >= rc->best_quality);
    1125           0 :   assert(q <= rc->worst_quality && q >= rc->best_quality);
    1126           0 :   return q;
    1127             : }
    1128             : 
    1129           0 : int av1_rc_pick_q_and_bounds(const AV1_COMP *cpi, int *bottom_index,
    1130             :                              int *top_index) {
    1131             :   int q;
    1132           0 :   if (cpi->oxcf.pass == 0) {
    1133           0 :     if (cpi->oxcf.rc_mode == AOM_CBR)
    1134           0 :       q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
    1135             :     else
    1136           0 :       q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
    1137             :   } else {
    1138           0 :     q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
    1139             :   }
    1140             : 
    1141           0 :   return q;
    1142             : }
    1143             : 
    1144           0 : void av1_rc_compute_frame_size_bounds(const AV1_COMP *cpi, int frame_target,
    1145             :                                       int *frame_under_shoot_limit,
    1146             :                                       int *frame_over_shoot_limit) {
    1147           0 :   if (cpi->oxcf.rc_mode == AOM_Q) {
    1148           0 :     *frame_under_shoot_limit = 0;
    1149           0 :     *frame_over_shoot_limit = INT_MAX;
    1150             :   } else {
    1151             :     // For very small rate targets where the fractional adjustment
    1152             :     // may be tiny make sure there is at least a minimum range.
    1153           0 :     const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100;
    1154           0 :     *frame_under_shoot_limit = AOMMAX(frame_target - tolerance - 200, 0);
    1155           0 :     *frame_over_shoot_limit =
    1156           0 :         AOMMIN(frame_target + tolerance + 200, cpi->rc.max_frame_bandwidth);
    1157             :   }
    1158           0 : }
    1159             : 
    1160           0 : void av1_rc_set_frame_target(AV1_COMP *cpi, int target) {
    1161           0 :   const AV1_COMMON *const cm = &cpi->common;
    1162           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1163             : 
    1164           0 :   rc->this_frame_target = target;
    1165             : 
    1166             :   // Modify frame size target when down-scaled.
    1167           0 :   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && !av1_resize_unscaled(cpi))
    1168           0 :     rc->this_frame_target =
    1169           0 :         (int)(rc->this_frame_target * av1_resize_rate_factor(cpi));
    1170             : 
    1171             :   // Target rate per SB64 (including partial SB64s.
    1172           0 :   rc->sb64_target_rate = (int)((int64_t)rc->this_frame_target * 64 * 64) /
    1173           0 :                          (cm->width * cm->height);
    1174           0 : }
    1175             : 
    1176           0 : static void update_alt_ref_frame_stats(AV1_COMP *cpi) {
    1177             :   // this frame refreshes means next frames don't unless specified by user
    1178           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1179           0 :   rc->frames_since_golden = 0;
    1180             : 
    1181             :   // Mark the alt ref as done (setting to 0 means no further alt refs pending).
    1182           0 :   rc->source_alt_ref_pending = 0;
    1183             : 
    1184             :   // Set the alternate reference frame active flag
    1185           0 :   rc->source_alt_ref_active = 1;
    1186           0 : }
    1187             : 
    1188           0 : static void update_golden_frame_stats(AV1_COMP *cpi) {
    1189           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1190             : 
    1191             : #if CONFIG_EXT_REFS
    1192             :   // Update the Golden frame usage counts.
    1193             :   // NOTE(weitinglin): If we use show_existing_frame for an OVERLAY frame,
    1194             :   //                   only the virtual indices for the reference frame will be
    1195             :   //                   updated and cpi->refresh_golden_frame will still be zero.
    1196           0 :   if (cpi->refresh_golden_frame || rc->is_src_frame_alt_ref) {
    1197             : #else
    1198             :   // Update the Golden frame usage counts.
    1199             :   if (cpi->refresh_golden_frame) {
    1200             : #endif  // CONFIG_EXT_REFS
    1201             : 
    1202             : #if CONFIG_EXT_REFS
    1203             :     // We will not use internal overlay frames to replace the golden frame
    1204           0 :     if (!rc->is_src_frame_ext_arf)
    1205             : #endif  // CONFIG_EXT_REFS
    1206             :       // this frame refreshes means next frames don't unless specified by user
    1207           0 :       rc->frames_since_golden = 0;
    1208             : 
    1209             :     // If we are not using alt ref in the up and coming group clear the arf
    1210             :     // active flag. In multi arf group case, if the index is not 0 then
    1211             :     // we are overlaying a mid group arf so should not reset the flag.
    1212           0 :     if (cpi->oxcf.pass == 2) {
    1213           0 :       if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
    1214           0 :         rc->source_alt_ref_active = 0;
    1215           0 :     } else if (!rc->source_alt_ref_pending) {
    1216           0 :       rc->source_alt_ref_active = 0;
    1217             :     }
    1218             : 
    1219             :     // Decrement count down till next gf
    1220           0 :     if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
    1221             : 
    1222           0 :   } else if (!cpi->refresh_alt_ref_frame) {
    1223             :     // Decrement count down till next gf
    1224           0 :     if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
    1225             : 
    1226           0 :     rc->frames_since_golden++;
    1227             :   }
    1228           0 : }
    1229             : 
    1230           0 : void av1_rc_postencode_update(AV1_COMP *cpi, uint64_t bytes_used) {
    1231           0 :   const AV1_COMMON *const cm = &cpi->common;
    1232           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1233           0 :   const int qindex = cm->base_qindex;
    1234             : 
    1235           0 :   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
    1236           0 :     av1_cyclic_refresh_postencode(cpi);
    1237             :   }
    1238             : 
    1239             :   // Update rate control heuristics
    1240           0 :   rc->projected_frame_size = (int)(bytes_used << 3);
    1241             : 
    1242             :   // Post encode loop adjustment of Q prediction.
    1243           0 :   av1_rc_update_rate_correction_factors(cpi);
    1244             : 
    1245             :   // Keep a record of last Q and ambient average Q.
    1246           0 :   if (cm->frame_type == KEY_FRAME) {
    1247           0 :     rc->last_q[KEY_FRAME] = qindex;
    1248           0 :     rc->avg_frame_qindex[KEY_FRAME] =
    1249           0 :         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
    1250             :   } else {
    1251           0 :     if (!rc->is_src_frame_alt_ref &&
    1252           0 :         !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
    1253           0 :       rc->last_q[INTER_FRAME] = qindex;
    1254           0 :       rc->avg_frame_qindex[INTER_FRAME] =
    1255           0 :           ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
    1256           0 :       rc->ni_frames++;
    1257           0 :       rc->tot_q += av1_convert_qindex_to_q(qindex, cm->bit_depth);
    1258           0 :       rc->avg_q = rc->tot_q / rc->ni_frames;
    1259             :       // Calculate the average Q for normal inter frames (not key or GFU
    1260             :       // frames).
    1261           0 :       rc->ni_tot_qi += qindex;
    1262           0 :       rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
    1263             :     }
    1264             :   }
    1265             : 
    1266             :   // Keep record of last boosted (KF/GF/ARF) Q value.
    1267             :   // If the current frame is coded at a lower Q then we also update it.
    1268             :   // If all mbs in this group are skipped only update if the Q value is
    1269             :   // better than that already stored.
    1270             :   // This is used to help set quality in forced key frames to reduce popping
    1271           0 :   if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) ||
    1272           0 :       (!rc->constrained_gf_group &&
    1273           0 :        (cpi->refresh_alt_ref_frame ||
    1274           0 :         (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
    1275           0 :     rc->last_boosted_qindex = qindex;
    1276             :   }
    1277           0 :   if (cm->frame_type == KEY_FRAME) rc->last_kf_qindex = qindex;
    1278             : 
    1279           0 :   update_buffer_level(cpi, rc->projected_frame_size);
    1280             : 
    1281             :   // Rolling monitors of whether we are over or underspending used to help
    1282             :   // regulate min and Max Q in two pass.
    1283           0 :   if (cm->frame_type != KEY_FRAME) {
    1284           0 :     rc->rolling_target_bits = ROUND_POWER_OF_TWO(
    1285             :         rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
    1286           0 :     rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
    1287             :         rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
    1288           0 :     rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
    1289             :         rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
    1290           0 :     rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
    1291             :         rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
    1292             :   }
    1293             : 
    1294             :   // Actual bits spent
    1295           0 :   rc->total_actual_bits += rc->projected_frame_size;
    1296             : #if CONFIG_EXT_REFS
    1297           0 :   rc->total_target_bits +=
    1298           0 :       (cm->show_frame || rc->is_bwd_ref_frame) ? rc->avg_frame_bandwidth : 0;
    1299             : #else
    1300             :   rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
    1301             : #endif  // CONFIG_EXT_REFS
    1302             : 
    1303           0 :   rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
    1304             : 
    1305           0 :   if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
    1306           0 :       (cm->frame_type != KEY_FRAME))
    1307             :     // Update the alternate reference frame stats as appropriate.
    1308           0 :     update_alt_ref_frame_stats(cpi);
    1309             :   else
    1310             :     // Update the Golden frame stats as appropriate.
    1311           0 :     update_golden_frame_stats(cpi);
    1312             : 
    1313           0 :   if (cm->frame_type == KEY_FRAME) rc->frames_since_key = 0;
    1314             : 
    1315             : #if CONFIG_EXT_REFS
    1316           0 :   if (cm->show_frame || rc->is_bwd_ref_frame) {
    1317             : #else
    1318             :   if (cm->show_frame) {
    1319             : #endif  // CONFIG_EXT_REFS
    1320           0 :     rc->frames_since_key++;
    1321           0 :     rc->frames_to_key--;
    1322             :   }
    1323           0 : }
    1324             : 
    1325           0 : void av1_rc_postencode_update_drop_frame(AV1_COMP *cpi) {
    1326             :   // Update buffer level with zero size, update frame counters, and return.
    1327           0 :   update_buffer_level(cpi, 0);
    1328           0 :   cpi->rc.frames_since_key++;
    1329           0 :   cpi->rc.frames_to_key--;
    1330           0 :   cpi->rc.rc_2_frame = 0;
    1331           0 :   cpi->rc.rc_1_frame = 0;
    1332           0 : }
    1333             : 
    1334             : // Use this macro to turn on/off use of alt-refs in one-pass mode.
    1335             : #define USE_ALTREF_FOR_ONE_PASS 1
    1336             : 
    1337           0 : static int calc_pframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
    1338             :   static const int af_ratio = 10;
    1339           0 :   const RATE_CONTROL *const rc = &cpi->rc;
    1340             :   int target;
    1341             : #if USE_ALTREF_FOR_ONE_PASS
    1342           0 :   target =
    1343           0 :       (!rc->is_src_frame_alt_ref &&
    1344           0 :        (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
    1345           0 :           ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
    1346           0 :                 (rc->baseline_gf_interval + af_ratio - 1)
    1347           0 :           : (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
    1348           0 :                 (rc->baseline_gf_interval + af_ratio - 1);
    1349             : #else
    1350             :   target = rc->avg_frame_bandwidth;
    1351             : #endif
    1352           0 :   return av1_rc_clamp_pframe_target_size(cpi, target);
    1353             : }
    1354             : 
    1355           0 : static int calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
    1356             :   static const int kf_ratio = 25;
    1357           0 :   const RATE_CONTROL *rc = &cpi->rc;
    1358           0 :   const int target = rc->avg_frame_bandwidth * kf_ratio;
    1359           0 :   return av1_rc_clamp_iframe_target_size(cpi, target);
    1360             : }
    1361             : 
    1362           0 : void av1_rc_get_one_pass_vbr_params(AV1_COMP *cpi) {
    1363           0 :   AV1_COMMON *const cm = &cpi->common;
    1364           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1365             :   int target;
    1366             :   // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
    1367           0 :   if (!cpi->refresh_alt_ref_frame &&
    1368           0 :       (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
    1369           0 :        rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
    1370           0 :     cm->frame_type = KEY_FRAME;
    1371           0 :     rc->this_key_frame_forced =
    1372           0 :         cm->current_video_frame != 0 && rc->frames_to_key == 0;
    1373           0 :     rc->frames_to_key = cpi->oxcf.key_freq;
    1374           0 :     rc->kf_boost = DEFAULT_KF_BOOST;
    1375           0 :     rc->source_alt_ref_active = 0;
    1376             :   } else {
    1377           0 :     cm->frame_type = INTER_FRAME;
    1378             :   }
    1379           0 :   if (rc->frames_till_gf_update_due == 0) {
    1380           0 :     rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
    1381           0 :     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
    1382             :     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
    1383           0 :     if (rc->frames_till_gf_update_due > rc->frames_to_key) {
    1384           0 :       rc->frames_till_gf_update_due = rc->frames_to_key;
    1385           0 :       rc->constrained_gf_group = 1;
    1386             :     } else {
    1387           0 :       rc->constrained_gf_group = 0;
    1388             :     }
    1389           0 :     cpi->refresh_golden_frame = 1;
    1390           0 :     rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
    1391           0 :     rc->gfu_boost = DEFAULT_GF_BOOST;
    1392             :   }
    1393           0 :   if (cm->frame_type == KEY_FRAME)
    1394           0 :     target = calc_iframe_target_size_one_pass_vbr(cpi);
    1395             :   else
    1396           0 :     target = calc_pframe_target_size_one_pass_vbr(cpi);
    1397           0 :   av1_rc_set_frame_target(cpi, target);
    1398           0 : }
    1399             : 
    1400           0 : static int calc_pframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
    1401           0 :   const AV1EncoderConfig *oxcf = &cpi->oxcf;
    1402           0 :   const RATE_CONTROL *rc = &cpi->rc;
    1403           0 :   const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
    1404           0 :   const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
    1405           0 :   int min_frame_target =
    1406           0 :       AOMMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
    1407             :   int target;
    1408             : 
    1409           0 :   if (oxcf->gf_cbr_boost_pct) {
    1410           0 :     const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
    1411           0 :     target = cpi->refresh_golden_frame
    1412           0 :                  ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval *
    1413             :                     af_ratio_pct) /
    1414           0 :                        (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
    1415           0 :                  : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
    1416           0 :                        (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
    1417             :   } else {
    1418           0 :     target = rc->avg_frame_bandwidth;
    1419             :   }
    1420             : 
    1421           0 :   if (diff > 0) {
    1422             :     // Lower the target bandwidth for this frame.
    1423           0 :     const int pct_low = (int)AOMMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
    1424           0 :     target -= (target * pct_low) / 200;
    1425           0 :   } else if (diff < 0) {
    1426             :     // Increase the target bandwidth for this frame.
    1427           0 :     const int pct_high =
    1428           0 :         (int)AOMMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
    1429           0 :     target += (target * pct_high) / 200;
    1430             :   }
    1431           0 :   if (oxcf->rc_max_inter_bitrate_pct) {
    1432           0 :     const int max_rate =
    1433           0 :         rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
    1434           0 :     target = AOMMIN(target, max_rate);
    1435             :   }
    1436           0 :   return AOMMAX(min_frame_target, target);
    1437             : }
    1438             : 
    1439           0 : static int calc_iframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
    1440           0 :   const RATE_CONTROL *rc = &cpi->rc;
    1441             :   int target;
    1442           0 :   if (cpi->common.current_video_frame == 0) {
    1443           0 :     target = ((rc->starting_buffer_level / 2) > INT_MAX)
    1444             :                  ? INT_MAX
    1445           0 :                  : (int)(rc->starting_buffer_level / 2);
    1446             :   } else {
    1447           0 :     int kf_boost = 32;
    1448           0 :     double framerate = cpi->framerate;
    1449             : 
    1450           0 :     kf_boost = AOMMAX(kf_boost, (int)(2 * framerate - 16));
    1451           0 :     if (rc->frames_since_key < framerate / 2) {
    1452           0 :       kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
    1453             :     }
    1454           0 :     target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
    1455             :   }
    1456           0 :   return av1_rc_clamp_iframe_target_size(cpi, target);
    1457             : }
    1458             : 
    1459           0 : void av1_rc_get_one_pass_cbr_params(AV1_COMP *cpi) {
    1460           0 :   AV1_COMMON *const cm = &cpi->common;
    1461           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1462             :   int target;
    1463             :   // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
    1464           0 :   if ((cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
    1465           0 :        rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
    1466           0 :     cm->frame_type = KEY_FRAME;
    1467           0 :     rc->this_key_frame_forced =
    1468           0 :         cm->current_video_frame != 0 && rc->frames_to_key == 0;
    1469           0 :     rc->frames_to_key = cpi->oxcf.key_freq;
    1470           0 :     rc->kf_boost = DEFAULT_KF_BOOST;
    1471           0 :     rc->source_alt_ref_active = 0;
    1472             :   } else {
    1473           0 :     cm->frame_type = INTER_FRAME;
    1474             :   }
    1475           0 :   if (rc->frames_till_gf_update_due == 0) {
    1476           0 :     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
    1477           0 :       av1_cyclic_refresh_set_golden_update(cpi);
    1478             :     else
    1479           0 :       rc->baseline_gf_interval =
    1480           0 :           (rc->min_gf_interval + rc->max_gf_interval) / 2;
    1481           0 :     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
    1482             :     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
    1483           0 :     if (rc->frames_till_gf_update_due > rc->frames_to_key)
    1484           0 :       rc->frames_till_gf_update_due = rc->frames_to_key;
    1485           0 :     cpi->refresh_golden_frame = 1;
    1486           0 :     rc->gfu_boost = DEFAULT_GF_BOOST;
    1487             :   }
    1488             : 
    1489             :   // Any update/change of global cyclic refresh parameters (amount/delta-qp)
    1490             :   // should be done here, before the frame qp is selected.
    1491           0 :   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
    1492           0 :     av1_cyclic_refresh_update_parameters(cpi);
    1493             : 
    1494           0 :   if (cm->frame_type == KEY_FRAME)
    1495           0 :     target = calc_iframe_target_size_one_pass_cbr(cpi);
    1496             :   else
    1497           0 :     target = calc_pframe_target_size_one_pass_cbr(cpi);
    1498             : 
    1499           0 :   av1_rc_set_frame_target(cpi, target);
    1500             :   // TODO(afergs): Decide whether to scale up, down, or not at all
    1501           0 : }
    1502             : 
    1503           0 : int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
    1504             :                        aom_bit_depth_t bit_depth) {
    1505           0 :   int start_index = rc->worst_quality;
    1506           0 :   int target_index = rc->worst_quality;
    1507             :   int i;
    1508             : 
    1509             :   // Convert the average q value to an index.
    1510           0 :   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
    1511           0 :     start_index = i;
    1512           0 :     if (av1_convert_qindex_to_q(i, bit_depth) >= qstart) break;
    1513             :   }
    1514             : 
    1515             :   // Convert the q target to an index
    1516           0 :   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
    1517           0 :     target_index = i;
    1518           0 :     if (av1_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
    1519             :   }
    1520             : 
    1521           0 :   return target_index - start_index;
    1522             : }
    1523             : 
    1524           0 : int av1_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
    1525             :                                int qindex, double rate_target_ratio,
    1526             :                                aom_bit_depth_t bit_depth) {
    1527           0 :   int target_index = rc->worst_quality;
    1528             :   int i;
    1529             : 
    1530             :   // Look up the current projected bits per block for the base index
    1531           0 :   const int base_bits_per_mb =
    1532             :       av1_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
    1533             : 
    1534             :   // Find the target bits per mb based on the base value and given ratio.
    1535           0 :   const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
    1536             : 
    1537             :   // Convert the q target to an index
    1538           0 :   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
    1539           0 :     if (av1_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
    1540             :         target_bits_per_mb) {
    1541           0 :       target_index = i;
    1542           0 :       break;
    1543             :     }
    1544             :   }
    1545           0 :   return target_index - qindex;
    1546             : }
    1547             : 
    1548           0 : void av1_rc_set_gf_interval_range(const AV1_COMP *const cpi,
    1549             :                                   RATE_CONTROL *const rc) {
    1550           0 :   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
    1551             : 
    1552             :   // Special case code for 1 pass fixed Q mode tests
    1553           0 :   if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
    1554           0 :     rc->max_gf_interval = FIXED_GF_INTERVAL;
    1555           0 :     rc->min_gf_interval = FIXED_GF_INTERVAL;
    1556           0 :     rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
    1557             :   } else {
    1558             :     // Set Maximum gf/arf interval
    1559           0 :     rc->max_gf_interval = oxcf->max_gf_interval;
    1560           0 :     rc->min_gf_interval = oxcf->min_gf_interval;
    1561           0 :     if (rc->min_gf_interval == 0)
    1562           0 :       rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
    1563             :           oxcf->width, oxcf->height, cpi->framerate);
    1564           0 :     if (rc->max_gf_interval == 0)
    1565           0 :       rc->max_gf_interval = av1_rc_get_default_max_gf_interval(
    1566             :           cpi->framerate, rc->min_gf_interval);
    1567             : 
    1568             :     // Extended interval for genuinely static scenes
    1569           0 :     rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
    1570             : 
    1571           0 :     if (is_altref_enabled(cpi)) {
    1572           0 :       if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
    1573           0 :         rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
    1574             :     }
    1575             : 
    1576           0 :     if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
    1577           0 :       rc->max_gf_interval = rc->static_scene_max_gf_interval;
    1578             : 
    1579             :     // Clamp min to max
    1580           0 :     rc->min_gf_interval = AOMMIN(rc->min_gf_interval, rc->max_gf_interval);
    1581             :   }
    1582           0 : }
    1583             : 
    1584           0 : void av1_rc_update_framerate(AV1_COMP *cpi) {
    1585           0 :   const AV1_COMMON *const cm = &cpi->common;
    1586           0 :   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
    1587           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1588             :   int vbr_max_bits;
    1589             : 
    1590           0 :   rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
    1591           0 :   rc->min_frame_bandwidth =
    1592           0 :       (int)(rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
    1593             : 
    1594           0 :   rc->min_frame_bandwidth =
    1595           0 :       AOMMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
    1596             : 
    1597             :   // A maximum bitrate for a frame is defined.
    1598             :   // The baseline for this aligns with HW implementations that
    1599             :   // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
    1600             :   // per 16x16 MB (averaged over a frame). However this limit is extended if
    1601             :   // a very high rate is given on the command line or the the rate cannnot
    1602             :   // be acheived because of a user specificed max q (e.g. when the user
    1603             :   // specifies lossless encode.
    1604           0 :   vbr_max_bits =
    1605           0 :       (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section) /
    1606             :             100);
    1607           0 :   rc->max_frame_bandwidth =
    1608           0 :       AOMMAX(AOMMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
    1609             : 
    1610           0 :   av1_rc_set_gf_interval_range(cpi, rc);
    1611           0 : }
    1612             : 
    1613             : #define VBR_PCT_ADJUSTMENT_LIMIT 50
    1614             : // For VBR...adjustment to the frame target based on error from previous frames
    1615           0 : static void vbr_rate_correction(AV1_COMP *cpi, int *this_frame_target) {
    1616           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1617           0 :   int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
    1618             :   int max_delta;
    1619           0 :   double position_factor = 1.0;
    1620             : 
    1621             :   // How far through the clip are we.
    1622             :   // This number is used to damp the per frame rate correction.
    1623             :   // Range 0 - 1.0
    1624           0 :   if (cpi->twopass.total_stats.count != 0.) {
    1625           0 :     position_factor = sqrt((double)cpi->common.current_video_frame /
    1626           0 :                            cpi->twopass.total_stats.count);
    1627             :   }
    1628           0 :   max_delta = (int)(position_factor *
    1629           0 :                     ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
    1630             : 
    1631             :   // vbr_bits_off_target > 0 means we have extra bits to spend
    1632           0 :   if (vbr_bits_off_target > 0) {
    1633           0 :     *this_frame_target += (vbr_bits_off_target > max_delta)
    1634             :                               ? max_delta
    1635           0 :                               : (int)vbr_bits_off_target;
    1636             :   } else {
    1637           0 :     *this_frame_target -= (vbr_bits_off_target < -max_delta)
    1638             :                               ? max_delta
    1639           0 :                               : (int)-vbr_bits_off_target;
    1640             :   }
    1641             : 
    1642             :   // Fast redistribution of bits arising from massive local undershoot.
    1643             :   // Dont do it for kf,arf,gf or overlay frames.
    1644           0 :   if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
    1645           0 :       rc->vbr_bits_off_target_fast) {
    1646           0 :     int one_frame_bits = AOMMAX(rc->avg_frame_bandwidth, *this_frame_target);
    1647             :     int fast_extra_bits;
    1648           0 :     fast_extra_bits = (int)AOMMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
    1649           0 :     fast_extra_bits = (int)AOMMIN(
    1650             :         fast_extra_bits,
    1651             :         AOMMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
    1652           0 :     *this_frame_target += (int)fast_extra_bits;
    1653           0 :     rc->vbr_bits_off_target_fast -= fast_extra_bits;
    1654             :   }
    1655           0 : }
    1656             : 
    1657           0 : void av1_set_target_rate(AV1_COMP *cpi) {
    1658           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1659           0 :   int target_rate = rc->base_frame_target;
    1660             : 
    1661             :   // Correction to rate target based on prior over or under shoot.
    1662           0 :   if (cpi->oxcf.rc_mode == AOM_VBR || cpi->oxcf.rc_mode == AOM_CQ)
    1663           0 :     vbr_rate_correction(cpi, &target_rate);
    1664           0 :   av1_rc_set_frame_target(cpi, target_rate);
    1665           0 : }

Generated by: LCOV version 1.13