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