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