xref: /aosp_15_r20/external/libvpx/vp9/encoder/vp9_ratectrl.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 <stdint.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "./vpx_dsp_rtcd.h"
20 #include "vpx_dsp/vpx_dsp_common.h"
21 #include "vpx_mem/vpx_mem.h"
22 #include "vpx_ports/mem.h"
23 #include "vpx_ports/system_state.h"
24 
25 #include "vp9/common/vp9_alloccommon.h"
26 #include "vp9/common/vp9_blockd.h"
27 #include "vp9/common/vp9_common.h"
28 #include "vp9/common/vp9_entropymode.h"
29 #include "vp9/common/vp9_onyxc_int.h"
30 #include "vp9/common/vp9_quant_common.h"
31 #include "vp9/common/vp9_seg_common.h"
32 
33 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
34 #include "vp9/encoder/vp9_encodemv.h"
35 #include "vp9/encoder/vp9_encoder.h"
36 #include "vp9/encoder/vp9_ext_ratectrl.h"
37 #include "vp9/encoder/vp9_firstpass.h"
38 #include "vp9/encoder/vp9_ratectrl.h"
39 #include "vp9/encoder/vp9_svc_layercontext.h"
40 
41 #include "vpx/vpx_codec.h"
42 #include "vpx/vpx_ext_ratectrl.h"
43 #include "vpx/internal/vpx_codec_internal.h"
44 
45 // Max rate per frame for 1080P and below encodes if no level requirement given.
46 // For larger formats limit to MAX_MB_RATE bits per MB
47 // 4Mbits is derived from the level requirement for level 4 (1080P 30) which
48 // requires that HW can sustain a rate of 16Mbits over a 4 frame group.
49 // If a lower level requirement is specified then this may over ride this value.
50 #define MAX_MB_RATE 250
51 #define MAXRATE_1080P 4000000
52 
53 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
54 
55 #define MIN_BPB_FACTOR 0.005
56 #define MAX_BPB_FACTOR 50
57 
58 #if CONFIG_VP9_HIGHBITDEPTH
59 #define ASSIGN_MINQ_TABLE(bit_depth, name)       \
60   do {                                           \
61     switch (bit_depth) {                         \
62       case VPX_BITS_8: name = name##_8; break;   \
63       case VPX_BITS_10: name = name##_10; break; \
64       default:                                   \
65         assert(bit_depth == VPX_BITS_12);        \
66         name = name##_12;                        \
67         break;                                   \
68     }                                            \
69   } while (0)
70 #else
71 #define ASSIGN_MINQ_TABLE(bit_depth, name) \
72   do {                                     \
73     (void)bit_depth;                       \
74     name = name##_8;                       \
75   } while (0)
76 #endif
77 
78 // Tables relating active max Q to active min Q
79 static int kf_low_motion_minq_8[QINDEX_RANGE];
80 static int kf_high_motion_minq_8[QINDEX_RANGE];
81 static int arfgf_low_motion_minq_8[QINDEX_RANGE];
82 static int arfgf_high_motion_minq_8[QINDEX_RANGE];
83 static int inter_minq_8[QINDEX_RANGE];
84 static int rtc_minq_8[QINDEX_RANGE];
85 
86 #if CONFIG_VP9_HIGHBITDEPTH
87 static int kf_low_motion_minq_10[QINDEX_RANGE];
88 static int kf_high_motion_minq_10[QINDEX_RANGE];
89 static int arfgf_low_motion_minq_10[QINDEX_RANGE];
90 static int arfgf_high_motion_minq_10[QINDEX_RANGE];
91 static int inter_minq_10[QINDEX_RANGE];
92 static int rtc_minq_10[QINDEX_RANGE];
93 static int kf_low_motion_minq_12[QINDEX_RANGE];
94 static int kf_high_motion_minq_12[QINDEX_RANGE];
95 static int arfgf_low_motion_minq_12[QINDEX_RANGE];
96 static int arfgf_high_motion_minq_12[QINDEX_RANGE];
97 static int inter_minq_12[QINDEX_RANGE];
98 static int rtc_minq_12[QINDEX_RANGE];
99 #endif
100 
101 #ifdef AGGRESSIVE_VBR
102 static int gf_high = 2400;
103 static int gf_low = 400;
104 static int kf_high = 4000;
105 static int kf_low = 400;
106 #else
107 static int gf_high = 2000;
108 static int gf_low = 400;
109 static int kf_high = 4800;
110 static int kf_low = 300;
111 #endif
112 
113 // Functions to compute the active minq lookup table entries based on a
114 // formulaic approach to facilitate easier adjustment of the Q tables.
115 // The formulae were derived from computing a 3rd order polynomial best
116 // 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)117 static int get_minq_index(double maxq, double x3, double x2, double x1,
118                           vpx_bit_depth_t bit_depth) {
119   int i;
120   const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
121 
122   // Special case handling to deal with the step from q2.0
123   // down to lossless mode represented by q 1.0.
124   if (minqtarget <= 2.0) return 0;
125 
126   for (i = 0; i < QINDEX_RANGE; i++) {
127     if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth)) return i;
128   }
129 
130   return QINDEX_RANGE - 1;
131 }
132 
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)133 static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
134                            int *arfgf_high, int *inter, int *rtc,
135                            vpx_bit_depth_t bit_depth) {
136   int i;
137   for (i = 0; i < QINDEX_RANGE; i++) {
138     const double maxq = vp9_convert_qindex_to_q(i, bit_depth);
139     kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
140     kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth);
141 #ifdef AGGRESSIVE_VBR
142     arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.275, bit_depth);
143     inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.80, bit_depth);
144 #else
145     arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
146     inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
147 #endif
148     arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
149     rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
150   }
151 }
152 
vp9_rc_init_minq_luts(void)153 void vp9_rc_init_minq_luts(void) {
154   init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
155                  arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
156                  inter_minq_8, rtc_minq_8, VPX_BITS_8);
157 #if CONFIG_VP9_HIGHBITDEPTH
158   init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
159                  arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
160                  inter_minq_10, rtc_minq_10, VPX_BITS_10);
161   init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
162                  arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
163                  inter_minq_12, rtc_minq_12, VPX_BITS_12);
164 #endif
165 }
166 
167 // These functions use formulaic calculations to make playing with the
168 // quantizer tables easier. If necessary they can be replaced by lookup
169 // tables if and when things settle down in the experimental bitstream
vp9_convert_qindex_to_q(int qindex,vpx_bit_depth_t bit_depth)170 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) {
171 // Convert the index to a real Q value (scaled down to match old Q values)
172 #if CONFIG_VP9_HIGHBITDEPTH
173   switch (bit_depth) {
174     case VPX_BITS_8: return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
175     case VPX_BITS_10: return vp9_ac_quant(qindex, 0, bit_depth) / 16.0;
176     default:
177       assert(bit_depth == VPX_BITS_12);
178       return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
179   }
180 #else
181   return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
182 #endif
183 }
184 
vp9_convert_q_to_qindex(double q_val,vpx_bit_depth_t bit_depth)185 int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth) {
186   int i;
187 
188   for (i = 0; i < QINDEX_RANGE; ++i)
189     if (vp9_convert_qindex_to_q(i, bit_depth) >= q_val) break;
190 
191   if (i == QINDEX_RANGE) i--;
192 
193   return i;
194 }
195 
vp9_rc_bits_per_mb(FRAME_TYPE frame_type,int qindex,double correction_factor,vpx_bit_depth_t bit_depth)196 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
197                        double correction_factor, vpx_bit_depth_t bit_depth) {
198   const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
199   int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
200 
201   assert(correction_factor <= MAX_BPB_FACTOR &&
202          correction_factor >= MIN_BPB_FACTOR);
203 
204   // q based adjustment to baseline enumerator
205   enumerator += (int)(enumerator * q) >> 12;
206   return (int)(enumerator * correction_factor / q);
207 }
208 
vp9_estimate_bits_at_q(FRAME_TYPE frame_type,int q,int mbs,double correction_factor,vpx_bit_depth_t bit_depth)209 int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
210                            double correction_factor,
211                            vpx_bit_depth_t bit_depth) {
212   const int bpm =
213       (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth));
214   return VPXMAX(FRAME_OVERHEAD_BITS,
215                 (int)(((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS));
216 }
217 
vp9_rc_clamp_pframe_target_size(const VP9_COMP * const cpi,int target)218 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
219   const RATE_CONTROL *rc = &cpi->rc;
220   const VP9EncoderConfig *oxcf = &cpi->oxcf;
221 
222   const int min_frame_target =
223       VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
224   if (target < min_frame_target) target = min_frame_target;
225   if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
226     // If there is an active ARF at this location use the minimum
227     // bits on this frame even if it is a constructed arf.
228     // The active maximum quantizer insures that an appropriate
229     // number of bits will be spent if needed for constructed ARFs.
230     target = min_frame_target;
231   }
232 
233   // Clip the frame target to the maximum allowed value.
234   if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
235 
236   if (oxcf->rc_max_inter_bitrate_pct) {
237     const int64_t max_rate =
238         (int64_t)rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
239     // target is of type int and VPXMIN cannot evaluate to larger than target
240     target = (int)VPXMIN(target, max_rate);
241   }
242   return target;
243 }
244 
vp9_rc_clamp_iframe_target_size(const VP9_COMP * const cpi,int target)245 int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
246   const RATE_CONTROL *rc = &cpi->rc;
247   const VP9EncoderConfig *oxcf = &cpi->oxcf;
248   if (oxcf->rc_max_intra_bitrate_pct) {
249     const int64_t max_rate =
250         (int64_t)rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
251     target = (int)VPXMIN(target, max_rate);
252   }
253   if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
254   return target;
255 }
256 
257 // TODO(marpan/jianj): bits_off_target and buffer_level are used in the same
258 // way for CBR mode, for the buffering updates below. Look into removing one
259 // of these (i.e., bits_off_target).
260 // Update the buffer level before encoding with the per-frame-bandwidth,
vp9_update_buffer_level_preencode(VP9_COMP * cpi)261 void vp9_update_buffer_level_preencode(VP9_COMP *cpi) {
262   RATE_CONTROL *const rc = &cpi->rc;
263   rc->bits_off_target += rc->avg_frame_bandwidth;
264   // Clip the buffer level to the maximum specified buffer size.
265   rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
266   rc->buffer_level = rc->bits_off_target;
267 }
268 
269 // Update the buffer level before encoding with the per-frame-bandwidth
270 // for SVC. The current and all upper temporal layers are updated, needed
271 // for the layered rate control which involves cumulative buffer levels for
272 // the temporal layers. Allow for using the timestamp(pts) delta for the
273 // framerate when the set_ref_frame_config is used.
vp9_update_buffer_level_svc_preencode(VP9_COMP * cpi)274 void vp9_update_buffer_level_svc_preencode(VP9_COMP *cpi) {
275   SVC *const svc = &cpi->svc;
276   int i;
277   // Set this to 1 to use timestamp delta for "framerate" under
278   // ref_frame_config usage.
279   int use_timestamp = 1;
280   const int64_t ts_delta =
281       svc->time_stamp_superframe - svc->time_stamp_prev[svc->spatial_layer_id];
282   for (i = svc->temporal_layer_id; i < svc->number_temporal_layers; ++i) {
283     const int layer =
284         LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
285     LAYER_CONTEXT *const lc = &svc->layer_context[layer];
286     RATE_CONTROL *const lrc = &lc->rc;
287     if (use_timestamp && cpi->svc.use_set_ref_frame_config &&
288         svc->number_temporal_layers == 1 && ts_delta > 0 &&
289         svc->current_superframe > 0) {
290       // TODO(marpan): This may need to be modified for temporal layers.
291       const double framerate_pts = 10000000.0 / ts_delta;
292       lrc->bits_off_target += saturate_cast_double_to_int(
293           round(lc->target_bandwidth / framerate_pts));
294     } else {
295       lrc->bits_off_target += saturate_cast_double_to_int(
296           round(lc->target_bandwidth / lc->framerate));
297     }
298     // Clip buffer level to maximum buffer size for the layer.
299     lrc->bits_off_target =
300         VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
301     lrc->buffer_level = lrc->bits_off_target;
302     if (i == svc->temporal_layer_id) {
303       cpi->rc.bits_off_target = lrc->bits_off_target;
304       cpi->rc.buffer_level = lrc->buffer_level;
305     }
306   }
307 }
308 
309 // Update the buffer level for higher temporal layers, given the encoded current
310 // temporal layer.
update_layer_buffer_level_postencode(SVC * svc,int encoded_frame_size)311 static void update_layer_buffer_level_postencode(SVC *svc,
312                                                  int encoded_frame_size) {
313   int i = 0;
314   const int current_temporal_layer = svc->temporal_layer_id;
315   for (i = current_temporal_layer + 1; i < svc->number_temporal_layers; ++i) {
316     const int layer =
317         LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
318     LAYER_CONTEXT *lc = &svc->layer_context[layer];
319     RATE_CONTROL *lrc = &lc->rc;
320     lrc->bits_off_target -= encoded_frame_size;
321     // Clip buffer level to maximum buffer size for the layer.
322     lrc->bits_off_target =
323         VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
324     lrc->buffer_level = lrc->bits_off_target;
325   }
326 }
327 
328 // Update the buffer level after encoding with encoded frame size.
update_buffer_level_postencode(VP9_COMP * cpi,int encoded_frame_size)329 static void update_buffer_level_postencode(VP9_COMP *cpi,
330                                            int encoded_frame_size) {
331   RATE_CONTROL *const rc = &cpi->rc;
332   rc->bits_off_target -= encoded_frame_size;
333   // Clip the buffer level to the maximum specified buffer size.
334   rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
335   // For screen-content mode, and if frame-dropper is off, don't let buffer
336   // level go below threshold, given here as -rc->maximum_ buffer_size.
337   if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
338       cpi->oxcf.drop_frames_water_mark == 0)
339     rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size);
340 
341   rc->buffer_level = rc->bits_off_target;
342 
343   if (is_one_pass_svc(cpi)) {
344     update_layer_buffer_level_postencode(&cpi->svc, encoded_frame_size);
345   }
346 }
347 
vp9_rc_get_default_min_gf_interval(int width,int height,double framerate)348 int vp9_rc_get_default_min_gf_interval(int width, int height,
349                                        double framerate) {
350   // Assume we do not need any constraint lower than 4K 20 fps
351   static const double factor_safe = 3840 * 2160 * 20.0;
352   const double factor = width * height * framerate;
353   const int default_interval =
354       clamp((int)round(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
355 
356   if (factor <= factor_safe)
357     return default_interval;
358   else
359     return VPXMAX(default_interval,
360                   (int)round(MIN_GF_INTERVAL * factor / factor_safe));
361   // Note this logic makes:
362   // 4K24: 5
363   // 4K30: 6
364   // 4K60: 12
365 }
366 
vp9_rc_get_default_max_gf_interval(double framerate,int min_gf_interval)367 int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
368   int interval = VPXMIN(MAX_GF_INTERVAL, (int)round(framerate * 0.75));
369   interval += (interval & 0x01);  // Round to even value
370   return VPXMAX(interval, min_gf_interval);
371 }
372 
vp9_rc_init(const VP9EncoderConfig * oxcf,int pass,RATE_CONTROL * rc)373 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
374   int i;
375 
376   if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
377     rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
378     rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
379   } else {
380     rc->avg_frame_qindex[KEY_FRAME] =
381         (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
382     rc->avg_frame_qindex[INTER_FRAME] =
383         (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
384   }
385 
386   rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
387   rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
388 
389   rc->buffer_level = rc->starting_buffer_level;
390   rc->bits_off_target = rc->starting_buffer_level;
391 
392   rc->rolling_target_bits = rc->avg_frame_bandwidth;
393   rc->rolling_actual_bits = rc->avg_frame_bandwidth;
394   rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
395   rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
396 
397   rc->total_actual_bits = 0;
398   rc->total_target_bits = 0;
399   rc->total_target_vs_actual = 0;
400   rc->avg_frame_low_motion = 0;
401   rc->count_last_scene_change = 0;
402   rc->af_ratio_onepass_vbr = 10;
403   rc->prev_avg_source_sad_lag = 0;
404   rc->high_source_sad = 0;
405   rc->reset_high_source_sad = 0;
406   rc->high_source_sad_lagindex = -1;
407   rc->high_num_blocks_with_motion = 0;
408   rc->hybrid_intra_scene_change = 0;
409   rc->re_encode_maxq_scene_change = 0;
410   rc->alt_ref_gf_group = 0;
411   rc->last_frame_is_src_altref = 0;
412   rc->fac_active_worst_inter = 150;
413   rc->fac_active_worst_gf = 100;
414   rc->force_qpmin = 0;
415   for (i = 0; i < MAX_LAG_BUFFERS; ++i) rc->avg_source_sad[i] = 0;
416   rc->frames_to_key = 0;
417   rc->frames_since_key = 8;  // Sensible default for first frame.
418   rc->this_key_frame_forced = 0;
419   rc->next_key_frame_forced = 0;
420   rc->source_alt_ref_pending = 0;
421   rc->source_alt_ref_active = 0;
422 
423   rc->frames_till_gf_update_due = 0;
424   rc->constrain_gf_key_freq_onepass_vbr = 1;
425   rc->ni_av_qi = oxcf->worst_allowed_q;
426   rc->ni_tot_qi = 0;
427   rc->ni_frames = 0;
428 
429   rc->tot_q = 0.0;
430   rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
431 
432   for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
433     rc->rate_correction_factors[i] = 1.0;
434     rc->damped_adjustment[i] = 0;
435   }
436 
437   rc->min_gf_interval = oxcf->min_gf_interval;
438   rc->max_gf_interval = oxcf->max_gf_interval;
439   if (rc->min_gf_interval == 0)
440     rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
441         oxcf->width, oxcf->height, oxcf->init_framerate);
442   if (rc->max_gf_interval == 0)
443     rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
444         oxcf->init_framerate, rc->min_gf_interval);
445   rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
446   if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
447     rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
448   } else {
449     rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH;
450   }
451 
452   rc->force_max_q = 0;
453   rc->last_post_encode_dropped_scene_change = 0;
454   rc->use_post_encode_drop = 0;
455   rc->ext_use_post_encode_drop = 0;
456   rc->disable_overshoot_maxq_cbr = 0;
457   rc->arf_active_best_quality_adjustment_factor = 1.0;
458   rc->arf_increase_active_best_quality = 0;
459   rc->preserve_arf_as_gld = 0;
460   rc->preserve_next_arf_as_gld = 0;
461   rc->show_arf_as_gld = 0;
462 }
463 
check_buffer_above_thresh(VP9_COMP * cpi,int drop_mark)464 static int check_buffer_above_thresh(VP9_COMP *cpi, int drop_mark) {
465   SVC *svc = &cpi->svc;
466   if (!cpi->use_svc || cpi->svc.framedrop_mode != FULL_SUPERFRAME_DROP) {
467     RATE_CONTROL *const rc = &cpi->rc;
468     return (rc->buffer_level > drop_mark);
469   } else {
470     int i;
471     // For SVC in the FULL_SUPERFRAME_DROP): the condition on
472     // buffer (if its above threshold, so no drop) is checked on current and
473     // upper spatial layers. If any spatial layer is not above threshold then
474     // we return 0.
475     for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) {
476       const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
477                                          svc->number_temporal_layers);
478       LAYER_CONTEXT *lc = &svc->layer_context[layer];
479       RATE_CONTROL *lrc = &lc->rc;
480       // Exclude check for layer whose bitrate is 0.
481       if (lc->target_bandwidth > 0) {
482         const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] *
483                                           lrc->optimal_buffer_level / 100);
484         if (!(lrc->buffer_level > drop_mark_layer)) return 0;
485       }
486     }
487     return 1;
488   }
489 }
490 
check_buffer_below_thresh(VP9_COMP * cpi,int drop_mark)491 static int check_buffer_below_thresh(VP9_COMP *cpi, int drop_mark) {
492   SVC *svc = &cpi->svc;
493   if (!cpi->use_svc || cpi->svc.framedrop_mode == LAYER_DROP) {
494     RATE_CONTROL *const rc = &cpi->rc;
495     return (rc->buffer_level <= drop_mark);
496   } else {
497     int i;
498     // For SVC in the constrained framedrop mode (svc->framedrop_mode =
499     // CONSTRAINED_LAYER_DROP or FULL_SUPERFRAME_DROP): the condition on
500     // buffer (if its below threshold, so drop frame) is checked on current
501     // and upper spatial layers. For FULL_SUPERFRAME_DROP mode if any
502     // spatial layer is <= threshold, then we return 1 (drop).
503     for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) {
504       const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
505                                          svc->number_temporal_layers);
506       LAYER_CONTEXT *lc = &svc->layer_context[layer];
507       RATE_CONTROL *lrc = &lc->rc;
508       // Exclude check for layer whose bitrate is 0.
509       if (lc->target_bandwidth > 0) {
510         const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] *
511                                           lrc->optimal_buffer_level / 100);
512         if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP) {
513           if (lrc->buffer_level <= drop_mark_layer) return 1;
514         } else {
515           if (!(lrc->buffer_level <= drop_mark_layer)) return 0;
516         }
517       }
518     }
519     if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP)
520       return 0;
521     else
522       return 1;
523   }
524 }
525 
vp9_test_drop(VP9_COMP * cpi)526 int vp9_test_drop(VP9_COMP *cpi) {
527   const VP9EncoderConfig *oxcf = &cpi->oxcf;
528   RATE_CONTROL *const rc = &cpi->rc;
529   SVC *svc = &cpi->svc;
530   int drop_frames_water_mark = oxcf->drop_frames_water_mark;
531   if (cpi->use_svc) {
532     // If we have dropped max_consec_drop frames, then we don't
533     // drop this spatial layer, and reset counter to 0.
534     if (svc->drop_count[svc->spatial_layer_id] == svc->max_consec_drop) {
535       svc->drop_count[svc->spatial_layer_id] = 0;
536       return 0;
537     } else {
538       drop_frames_water_mark = svc->framedrop_thresh[svc->spatial_layer_id];
539     }
540   }
541   if (!drop_frames_water_mark ||
542       (svc->spatial_layer_id > 0 &&
543        svc->framedrop_mode == FULL_SUPERFRAME_DROP)) {
544     return 0;
545   } else {
546     if ((rc->buffer_level < 0 && svc->framedrop_mode != FULL_SUPERFRAME_DROP) ||
547         (check_buffer_below_thresh(cpi, -1) &&
548          svc->framedrop_mode == FULL_SUPERFRAME_DROP)) {
549       // Always drop if buffer is below 0.
550       return 1;
551     } else {
552       // If buffer is below drop_mark, for now just drop every other frame
553       // (starting with the next frame) until it increases back over drop_mark.
554       int drop_mark =
555           (int)(drop_frames_water_mark * rc->optimal_buffer_level / 100);
556       if (check_buffer_above_thresh(cpi, drop_mark) &&
557           (rc->decimation_factor > 0)) {
558         --rc->decimation_factor;
559       } else if (check_buffer_below_thresh(cpi, drop_mark) &&
560                  rc->decimation_factor == 0) {
561         rc->decimation_factor = 1;
562       }
563       if (rc->decimation_factor > 0) {
564         if (rc->decimation_count > 0) {
565           --rc->decimation_count;
566           return 1;
567         } else {
568           rc->decimation_count = rc->decimation_factor;
569           return 0;
570         }
571       } else {
572         rc->decimation_count = 0;
573         return 0;
574       }
575     }
576   }
577 }
578 
post_encode_drop_cbr(VP9_COMP * cpi,size_t * size)579 int post_encode_drop_cbr(VP9_COMP *cpi, size_t *size) {
580   size_t frame_size = *size << 3;
581   int64_t new_buffer_level =
582       cpi->rc.buffer_level + cpi->rc.avg_frame_bandwidth - (int64_t)frame_size;
583 
584   // For now we drop if new buffer level (given the encoded frame size) goes
585   // below 0.
586   if (new_buffer_level < 0) {
587     *size = 0;
588     vp9_rc_postencode_update_drop_frame(cpi);
589     // Update flag to use for next frame.
590     if (cpi->rc.high_source_sad ||
591         (cpi->use_svc && cpi->svc.high_source_sad_superframe))
592       cpi->rc.last_post_encode_dropped_scene_change = 1;
593     // Force max_q on next fame.
594     cpi->rc.force_max_q = 1;
595     cpi->rc.avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality;
596     cpi->last_frame_dropped = 1;
597     cpi->ext_refresh_frame_flags_pending = 0;
598     if (cpi->use_svc) {
599       SVC *svc = &cpi->svc;
600       int sl = 0;
601       int tl = 0;
602       svc->last_layer_dropped[svc->spatial_layer_id] = 1;
603       svc->drop_spatial_layer[svc->spatial_layer_id] = 1;
604       svc->drop_count[svc->spatial_layer_id]++;
605       svc->skip_enhancement_layer = 1;
606       // Postencode drop is only checked on base spatial layer,
607       // for now if max-q is set on base we force it on all layers.
608       for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
609         for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
610           const int layer =
611               LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
612           LAYER_CONTEXT *lc = &svc->layer_context[layer];
613           RATE_CONTROL *lrc = &lc->rc;
614           lrc->force_max_q = 1;
615           lrc->avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality;
616         }
617       }
618     }
619     return 1;
620   }
621 
622   cpi->rc.force_max_q = 0;
623   cpi->rc.last_post_encode_dropped_scene_change = 0;
624   return 0;
625 }
626 
vp9_rc_drop_frame(VP9_COMP * cpi)627 int vp9_rc_drop_frame(VP9_COMP *cpi) {
628   SVC *svc = &cpi->svc;
629   int svc_prev_layer_dropped = 0;
630   // In the constrained or full_superframe framedrop mode for svc
631   // (framedrop_mode != (LAYER_DROP && CONSTRAINED_FROM_ABOVE)),
632   // if the previous spatial layer was dropped, drop the current spatial layer.
633   if (cpi->use_svc && svc->spatial_layer_id > 0 &&
634       svc->drop_spatial_layer[svc->spatial_layer_id - 1])
635     svc_prev_layer_dropped = 1;
636   if ((svc_prev_layer_dropped && svc->framedrop_mode != LAYER_DROP &&
637        svc->framedrop_mode != CONSTRAINED_FROM_ABOVE_DROP) ||
638       svc->force_drop_constrained_from_above[svc->spatial_layer_id] ||
639       vp9_test_drop(cpi)) {
640     vp9_rc_postencode_update_drop_frame(cpi);
641     cpi->ext_refresh_frame_flags_pending = 0;
642     cpi->last_frame_dropped = 1;
643     if (cpi->use_svc) {
644       svc->last_layer_dropped[svc->spatial_layer_id] = 1;
645       svc->drop_spatial_layer[svc->spatial_layer_id] = 1;
646       svc->drop_count[svc->spatial_layer_id]++;
647       svc->skip_enhancement_layer = 1;
648       if (svc->framedrop_mode == LAYER_DROP ||
649           (svc->framedrop_mode == CONSTRAINED_FROM_ABOVE_DROP &&
650            svc->force_drop_constrained_from_above[svc->number_spatial_layers -
651                                                   1] == 0) ||
652           svc->drop_spatial_layer[0] == 0) {
653         // For the case of constrained drop mode where full superframe is
654         // dropped, we don't increment the svc frame counters.
655         // In particular temporal layer counter (which is incremented in
656         // vp9_inc_frame_in_layer()) won't be incremented, so on a dropped
657         // frame we try the same temporal_layer_id on next incoming frame.
658         // This is to avoid an issue with temporal alignment with full
659         // superframe dropping.
660         vp9_inc_frame_in_layer(cpi);
661       }
662       if (svc->spatial_layer_id == svc->number_spatial_layers - 1) {
663         int i;
664         int all_layers_drop = 1;
665         for (i = 0; i < svc->spatial_layer_id; i++) {
666           if (svc->drop_spatial_layer[i] == 0) {
667             all_layers_drop = 0;
668             break;
669           }
670         }
671         if (all_layers_drop == 1) svc->skip_enhancement_layer = 0;
672       }
673     }
674     return 1;
675   }
676   return 0;
677 }
678 
adjust_q_cbr(const VP9_COMP * cpi,int q)679 static int adjust_q_cbr(const VP9_COMP *cpi, int q) {
680   // This makes sure q is between oscillating Qs to prevent resonance.
681   if (!cpi->rc.reset_high_source_sad &&
682       (!cpi->oxcf.gf_cbr_boost_pct ||
683        !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)) &&
684       (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
685       cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
686     int qclamp = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
687                        VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
688     // If the previous frame had overshoot and the current q needs to increase
689     // above the clamped value, reduce the clamp for faster reaction to
690     // overshoot.
691     if (cpi->rc.rc_1_frame == -1 && q > qclamp)
692       q = (q + qclamp) >> 1;
693     else
694       q = qclamp;
695   }
696   if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
697       cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
698     vp9_cyclic_refresh_limit_q(cpi, &q);
699   return VPXMAX(VPXMIN(q, cpi->rc.worst_quality), cpi->rc.best_quality);
700 }
701 
get_rate_correction_factor(const VP9_COMP * cpi)702 static double get_rate_correction_factor(const VP9_COMP *cpi) {
703   const RATE_CONTROL *const rc = &cpi->rc;
704   const VP9_COMMON *const cm = &cpi->common;
705   double rcf;
706 
707   if (frame_is_intra_only(cm)) {
708     rcf = rc->rate_correction_factors[KF_STD];
709   } else if (cpi->oxcf.pass == 2) {
710     RATE_FACTOR_LEVEL rf_lvl =
711         cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
712     rcf = rc->rate_correction_factors[rf_lvl];
713   } else {
714     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
715         !rc->is_src_frame_alt_ref && !cpi->use_svc &&
716         (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
717       rcf = rc->rate_correction_factors[GF_ARF_STD];
718     else
719       rcf = rc->rate_correction_factors[INTER_NORMAL];
720   }
721   rcf *= rcf_mult[rc->frame_size_selector];
722   return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
723 }
724 
set_rate_correction_factor(VP9_COMP * cpi,double factor)725 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
726   RATE_CONTROL *const rc = &cpi->rc;
727   const VP9_COMMON *const cm = &cpi->common;
728 
729   // Normalize RCF to account for the size-dependent scaling factor.
730   factor /= rcf_mult[cpi->rc.frame_size_selector];
731 
732   factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
733 
734   if (frame_is_intra_only(cm)) {
735     rc->rate_correction_factors[KF_STD] = factor;
736   } else if (cpi->oxcf.pass == 2) {
737     RATE_FACTOR_LEVEL rf_lvl =
738         cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
739     rc->rate_correction_factors[rf_lvl] = factor;
740   } else {
741     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
742         !rc->is_src_frame_alt_ref && !cpi->use_svc &&
743         (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
744       rc->rate_correction_factors[GF_ARF_STD] = factor;
745     else
746       rc->rate_correction_factors[INTER_NORMAL] = factor;
747   }
748 }
749 
vp9_rc_update_rate_correction_factors(VP9_COMP * cpi)750 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) {
751   const VP9_COMMON *const cm = &cpi->common;
752   int correction_factor = 100;
753   double rate_correction_factor = get_rate_correction_factor(cpi);
754   double adjustment_limit;
755   RATE_FACTOR_LEVEL rf_lvl =
756       cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
757 
758   int projected_size_based_on_q = 0;
759 
760   // Do not update the rate factors for arf overlay frames.
761   if (cpi->rc.is_src_frame_alt_ref) return;
762 
763   // Clear down mmx registers to allow floating point in what follows
764   vpx_clear_system_state();
765 
766   // Work out how big we would have expected the frame to be at this Q given
767   // the current correction factor.
768   // Stay in double to avoid int overflow when values are large
769   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
770     projected_size_based_on_q =
771         vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
772   } else {
773     FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type;
774     projected_size_based_on_q =
775         vp9_estimate_bits_at_q(frame_type, cm->base_qindex, cm->MBs,
776                                rate_correction_factor, cm->bit_depth);
777   }
778   // Work out a size correction factor.
779   if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
780     correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
781                               projected_size_based_on_q);
782 
783   // Do not use damped adjustment for the first frame of each frame type
784   if (!cpi->rc.damped_adjustment[rf_lvl]) {
785     adjustment_limit = 1.0;
786     cpi->rc.damped_adjustment[rf_lvl] = 1;
787   } else {
788     // More heavily damped adjustment used if we have been oscillating either
789     // side of target.
790     adjustment_limit =
791         0.25 + 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
792   }
793 
794   cpi->rc.q_2_frame = cpi->rc.q_1_frame;
795   cpi->rc.q_1_frame = cm->base_qindex;
796   cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
797   if (correction_factor > 110)
798     cpi->rc.rc_1_frame = -1;
799   else if (correction_factor < 90)
800     cpi->rc.rc_1_frame = 1;
801   else
802     cpi->rc.rc_1_frame = 0;
803 
804   // Turn off oscilation detection in the case of massive overshoot.
805   if (cpi->rc.rc_1_frame == -1 && cpi->rc.rc_2_frame == 1 &&
806       correction_factor > 1000) {
807     cpi->rc.rc_2_frame = 0;
808   }
809 
810   if (correction_factor > 102) {
811     // We are not already at the worst allowable quality
812     correction_factor =
813         (int)(100 + ((correction_factor - 100) * adjustment_limit));
814     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
815     // Keep rate_correction_factor within limits
816     if (rate_correction_factor > MAX_BPB_FACTOR)
817       rate_correction_factor = MAX_BPB_FACTOR;
818   } else if (correction_factor < 99) {
819     // We are not already at the best allowable quality
820     correction_factor =
821         (int)(100 - ((100 - correction_factor) * adjustment_limit));
822     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
823 
824     // Keep rate_correction_factor within limits
825     if (rate_correction_factor < MIN_BPB_FACTOR)
826       rate_correction_factor = MIN_BPB_FACTOR;
827   }
828 
829   set_rate_correction_factor(cpi, rate_correction_factor);
830 }
831 
vp9_rc_regulate_q(const VP9_COMP * cpi,int target_bits_per_frame,int active_best_quality,int active_worst_quality)832 int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
833                       int active_best_quality, int active_worst_quality) {
834   const VP9_COMMON *const cm = &cpi->common;
835   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
836   int q = active_worst_quality;
837   int last_error = INT_MAX;
838   int i, target_bits_per_mb, bits_per_mb_at_this_q;
839   const double correction_factor = get_rate_correction_factor(cpi);
840 
841   // Calculate required scaling factor based on target frame size and size of
842   // frame produced using previous Q.
843   target_bits_per_mb =
844       (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs);
845 
846   i = active_best_quality;
847 
848   do {
849     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cr->apply_cyclic_refresh &&
850         (!cpi->oxcf.gf_cbr_boost_pct || !cpi->refresh_golden_frame)) {
851       bits_per_mb_at_this_q =
852           (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
853     } else {
854       FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type;
855       bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(
856           frame_type, i, correction_factor, cm->bit_depth);
857     }
858 
859     if (bits_per_mb_at_this_q <= target_bits_per_mb) {
860       if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
861         q = i;
862       else
863         q = i - 1;
864 
865       break;
866     } else {
867       last_error = bits_per_mb_at_this_q - target_bits_per_mb;
868     }
869   } while (++i <= active_worst_quality);
870 
871   // Adjustment to q for CBR mode.
872   if (cpi->oxcf.rc_mode == VPX_CBR) return adjust_q_cbr(cpi, q);
873 
874   return q;
875 }
876 
get_active_quality(int q,int gfu_boost,int low,int high,int * low_motion_minq,int * high_motion_minq)877 static int get_active_quality(int q, int gfu_boost, int low, int high,
878                               int *low_motion_minq, int *high_motion_minq) {
879   if (gfu_boost > high) {
880     return low_motion_minq[q];
881   } else if (gfu_boost < low) {
882     return high_motion_minq[q];
883   } else {
884     const int gap = high - low;
885     const int offset = high - gfu_boost;
886     const int qdiff = high_motion_minq[q] - low_motion_minq[q];
887     const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
888     return low_motion_minq[q] + adjustment;
889   }
890 }
891 
get_kf_active_quality(const RATE_CONTROL * const rc,int q,vpx_bit_depth_t bit_depth)892 static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
893                                  vpx_bit_depth_t bit_depth) {
894   int *kf_low_motion_minq;
895   int *kf_high_motion_minq;
896   ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
897   ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
898   return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
899                             kf_low_motion_minq, kf_high_motion_minq);
900 }
901 
get_gf_active_quality(const VP9_COMP * const cpi,int q,vpx_bit_depth_t bit_depth)902 static int get_gf_active_quality(const VP9_COMP *const cpi, int q,
903                                  vpx_bit_depth_t bit_depth) {
904   const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
905   const RATE_CONTROL *const rc = &cpi->rc;
906 
907   int *arfgf_low_motion_minq;
908   int *arfgf_high_motion_minq;
909   const int gfu_boost = cpi->multi_layer_arf
910                             ? gf_group->gfu_boost[gf_group->index]
911                             : rc->gfu_boost;
912   ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
913   ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
914   return get_active_quality(q, gfu_boost, gf_low, gf_high,
915                             arfgf_low_motion_minq, arfgf_high_motion_minq);
916 }
917 
calc_active_worst_quality_one_pass_vbr(const VP9_COMP * cpi)918 static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
919   const RATE_CONTROL *const rc = &cpi->rc;
920   const unsigned int curr_frame = cpi->common.current_video_frame;
921   int active_worst_quality;
922 
923   if (cpi->common.frame_type == KEY_FRAME) {
924     active_worst_quality =
925         curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] << 1;
926   } else {
927     if (!rc->is_src_frame_alt_ref && !cpi->use_svc &&
928         (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
929       active_worst_quality =
930           curr_frame == 1
931               ? rc->last_q[KEY_FRAME] * 5 >> 2
932               : rc->last_q[INTER_FRAME] * rc->fac_active_worst_gf / 100;
933     } else {
934       active_worst_quality = curr_frame == 1
935                                  ? rc->last_q[KEY_FRAME] << 1
936                                  : rc->avg_frame_qindex[INTER_FRAME] *
937                                        rc->fac_active_worst_inter / 100;
938     }
939   }
940   return VPXMIN(active_worst_quality, rc->worst_quality);
941 }
942 
943 // Adjust active_worst_quality level based on buffer level.
calc_active_worst_quality_one_pass_cbr(const VP9_COMP * cpi)944 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
945   // Adjust active_worst_quality: If buffer is above the optimal/target level,
946   // bring active_worst_quality down depending on fullness of buffer.
947   // If buffer is below the optimal level, let the active_worst_quality go from
948   // ambient Q (at buffer = optimal level) to worst_quality level
949   // (at buffer = critical level).
950   const VP9_COMMON *const cm = &cpi->common;
951   const RATE_CONTROL *rc = &cpi->rc;
952   // Buffer level below which we push active_worst to worst_quality.
953   int64_t critical_level = rc->optimal_buffer_level >> 3;
954   int64_t buff_lvl_step = 0;
955   int adjustment = 0;
956   int active_worst_quality;
957   int ambient_qp;
958   unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
959   if (frame_is_intra_only(cm) || rc->reset_high_source_sad || rc->force_max_q)
960     return rc->worst_quality;
961   // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
962   // for the first few frames following key frame. These are both initialized
963   // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
964   // So for first few frames following key, the qp of that key frame is weighted
965   // into the active_worst_quality setting.
966   ambient_qp = (cm->current_video_frame < num_frames_weight_key)
967                    ? VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
968                             rc->avg_frame_qindex[KEY_FRAME])
969                    : rc->avg_frame_qindex[INTER_FRAME];
970   active_worst_quality = VPXMIN(rc->worst_quality, (ambient_qp * 5) >> 2);
971   // For SVC if the current base spatial layer was key frame, use the QP from
972   // that base layer for ambient_qp.
973   if (cpi->use_svc && cpi->svc.spatial_layer_id > 0) {
974     int layer = LAYER_IDS_TO_IDX(0, cpi->svc.temporal_layer_id,
975                                  cpi->svc.number_temporal_layers);
976     const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
977     if (lc->is_key_frame) {
978       const RATE_CONTROL *lrc = &lc->rc;
979       ambient_qp = VPXMIN(ambient_qp, lrc->last_q[KEY_FRAME]);
980       active_worst_quality = VPXMIN(rc->worst_quality, (ambient_qp * 9) >> 3);
981     }
982   }
983   if (rc->buffer_level > rc->optimal_buffer_level) {
984     // Adjust down.
985     // Maximum limit for down adjustment ~30%; make it lower for screen content.
986     int max_adjustment_down = active_worst_quality / 3;
987     if (cpi->oxcf.content == VP9E_CONTENT_SCREEN)
988       max_adjustment_down = active_worst_quality >> 3;
989     if (max_adjustment_down) {
990       buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
991                        max_adjustment_down);
992       if (buff_lvl_step)
993         adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
994                            buff_lvl_step);
995       active_worst_quality -= adjustment;
996     }
997   } else if (rc->buffer_level > critical_level) {
998     // Adjust up from ambient Q.
999     if (critical_level) {
1000       buff_lvl_step = (rc->optimal_buffer_level - critical_level);
1001       if (buff_lvl_step) {
1002         adjustment = (int)((rc->worst_quality - ambient_qp) *
1003                            (rc->optimal_buffer_level - rc->buffer_level) /
1004                            buff_lvl_step);
1005       }
1006       active_worst_quality = ambient_qp + adjustment;
1007     }
1008   } else {
1009     // Set to worst_quality if buffer is below critical level.
1010     active_worst_quality = rc->worst_quality;
1011   }
1012   return active_worst_quality;
1013 }
1014 
rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP * cpi,int * bottom_index,int * top_index)1015 static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
1016                                              int *bottom_index,
1017                                              int *top_index) {
1018   const VP9_COMMON *const cm = &cpi->common;
1019   const RATE_CONTROL *const rc = &cpi->rc;
1020   int active_best_quality;
1021   int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
1022   int q;
1023   int *rtc_minq;
1024   ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
1025 
1026   if (frame_is_intra_only(cm)) {
1027     active_best_quality = rc->best_quality;
1028     // Handle the special case for key frames forced when we have reached
1029     // the maximum key frame interval. Here force the Q to a range
1030     // based on the ambient Q to reduce the risk of popping.
1031     if (rc->this_key_frame_forced) {
1032       int qindex = rc->last_boosted_qindex;
1033       double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1034       int delta_qindex = vp9_compute_qdelta(
1035           rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth);
1036       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1037     } else if (cm->current_video_frame > 0) {
1038       // not first frame of one pass and kf_boost is set
1039       double q_adj_factor = 1.0;
1040       double q_val;
1041 
1042       active_best_quality = get_kf_active_quality(
1043           rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
1044 
1045       // Allow somewhat lower kf minq with small image formats.
1046       if ((cm->width * cm->height) <= (352 * 288)) {
1047         q_adj_factor -= 0.25;
1048       }
1049 
1050       // Convert the adjustment factor to a qindex delta
1051       // on active_best_quality.
1052       q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
1053       active_best_quality +=
1054           vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
1055     }
1056   } else if (!rc->is_src_frame_alt_ref && !cpi->use_svc &&
1057              cpi->oxcf.gf_cbr_boost_pct &&
1058              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1059     // Use the lower of active_worst_quality and recent
1060     // average Q as basis for GF/ARF best Q limit unless last frame was
1061     // a key frame.
1062     if (rc->frames_since_key > 1 &&
1063         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1064       q = rc->avg_frame_qindex[INTER_FRAME];
1065     } else {
1066       q = active_worst_quality;
1067     }
1068     active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1069   } else {
1070     // Use the lower of active_worst_quality and recent/average Q.
1071     if (cm->current_video_frame > 1) {
1072       if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
1073         active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
1074       else
1075         active_best_quality = rtc_minq[active_worst_quality];
1076     } else {
1077       if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
1078         active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
1079       else
1080         active_best_quality = rtc_minq[active_worst_quality];
1081     }
1082   }
1083 
1084   // Clip the active best and worst quality values to limits
1085   active_best_quality =
1086       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1087   active_worst_quality =
1088       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1089 
1090   *top_index = active_worst_quality;
1091   *bottom_index = active_best_quality;
1092 
1093   // Special case code to try and match quality with forced key frames
1094   if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
1095     q = rc->last_boosted_qindex;
1096   } else {
1097     q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1098                           active_worst_quality);
1099     if (q > *top_index) {
1100       // Special case when we are targeting the max allowed rate
1101       if (rc->this_frame_target >= rc->max_frame_bandwidth)
1102         *top_index = q;
1103       else
1104         q = *top_index;
1105     }
1106   }
1107 
1108   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1109   assert(*bottom_index <= rc->worst_quality &&
1110          *bottom_index >= rc->best_quality);
1111   assert(q <= rc->worst_quality && q >= rc->best_quality);
1112   return q;
1113 }
1114 
get_active_cq_level_one_pass(const RATE_CONTROL * rc,const VP9EncoderConfig * const oxcf)1115 static int get_active_cq_level_one_pass(const RATE_CONTROL *rc,
1116                                         const VP9EncoderConfig *const oxcf) {
1117   static const double cq_adjust_threshold = 0.1;
1118   int active_cq_level = oxcf->cq_level;
1119   if (oxcf->rc_mode == VPX_CQ && rc->total_target_bits > 0) {
1120     const double x = (double)rc->total_actual_bits / rc->total_target_bits;
1121     if (x < cq_adjust_threshold) {
1122       active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
1123     }
1124   }
1125   return active_cq_level;
1126 }
1127 
1128 #define SMOOTH_PCT_MIN 0.1
1129 #define SMOOTH_PCT_DIV 0.05
get_active_cq_level_two_pass(const TWO_PASS * twopass,const RATE_CONTROL * rc,const VP9EncoderConfig * const oxcf)1130 static int get_active_cq_level_two_pass(const TWO_PASS *twopass,
1131                                         const RATE_CONTROL *rc,
1132                                         const VP9EncoderConfig *const oxcf) {
1133   static const double cq_adjust_threshold = 0.1;
1134   int active_cq_level = oxcf->cq_level;
1135   if (oxcf->rc_mode == VPX_CQ) {
1136     if (twopass->mb_smooth_pct > SMOOTH_PCT_MIN) {
1137       active_cq_level -=
1138           (int)((twopass->mb_smooth_pct - SMOOTH_PCT_MIN) / SMOOTH_PCT_DIV);
1139       active_cq_level = VPXMAX(active_cq_level, 0);
1140     }
1141     if (rc->total_target_bits > 0) {
1142       const double x = (double)rc->total_actual_bits / rc->total_target_bits;
1143       if (x < cq_adjust_threshold) {
1144         active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
1145       }
1146     }
1147   }
1148   return active_cq_level;
1149 }
1150 
rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP * cpi,int * bottom_index,int * top_index)1151 static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
1152                                              int *bottom_index,
1153                                              int *top_index) {
1154   const VP9_COMMON *const cm = &cpi->common;
1155   const RATE_CONTROL *const rc = &cpi->rc;
1156   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1157   const int cq_level = get_active_cq_level_one_pass(rc, oxcf);
1158   int active_best_quality;
1159   int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
1160   int q;
1161   int *inter_minq;
1162   ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
1163 
1164   if (frame_is_intra_only(cm)) {
1165     if (oxcf->rc_mode == VPX_Q) {
1166       int qindex = cq_level;
1167       double qstart = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1168       int delta_qindex =
1169           vp9_compute_qdelta(rc, qstart, qstart * 0.25, cm->bit_depth);
1170       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1171     } else if (rc->this_key_frame_forced) {
1172       // Handle the special case for key frames forced when we have reached
1173       // the maximum key frame interval. Here force the Q to a range
1174       // based on the ambient Q to reduce the risk of popping.
1175       int qindex = rc->last_boosted_qindex;
1176       double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1177       int delta_qindex = vp9_compute_qdelta(
1178           rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
1179       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1180     } else {
1181       // not first frame of one pass and kf_boost is set
1182       double q_adj_factor = 1.0;
1183       double q_val;
1184 
1185       active_best_quality = get_kf_active_quality(
1186           rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
1187 
1188       // Allow somewhat lower kf minq with small image formats.
1189       if ((cm->width * cm->height) <= (352 * 288)) {
1190         q_adj_factor -= 0.25;
1191       }
1192 
1193       // Convert the adjustment factor to a qindex delta
1194       // on active_best_quality.
1195       q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
1196       active_best_quality +=
1197           vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
1198     }
1199   } else if (!rc->is_src_frame_alt_ref &&
1200              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1201     // Use the lower of active_worst_quality and recent
1202     // average Q as basis for GF/ARF best Q limit unless last frame was
1203     // a key frame.
1204     if (rc->frames_since_key > 1) {
1205       if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1206         q = rc->avg_frame_qindex[INTER_FRAME];
1207       } else {
1208         q = active_worst_quality;
1209       }
1210     } else {
1211       q = rc->avg_frame_qindex[KEY_FRAME];
1212     }
1213     // For constrained quality don't allow Q less than the cq level
1214     if (oxcf->rc_mode == VPX_CQ) {
1215       if (q < cq_level) q = cq_level;
1216 
1217       active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1218 
1219       // Constrained quality use slightly lower active best.
1220       active_best_quality = active_best_quality * 15 / 16;
1221 
1222     } else if (oxcf->rc_mode == VPX_Q) {
1223       int qindex = cq_level;
1224       double qstart = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1225       int delta_qindex;
1226       if (cpi->refresh_alt_ref_frame)
1227         delta_qindex =
1228             vp9_compute_qdelta(rc, qstart, qstart * 0.40, cm->bit_depth);
1229       else
1230         delta_qindex =
1231             vp9_compute_qdelta(rc, qstart, qstart * 0.50, cm->bit_depth);
1232       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1233     } else {
1234       active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1235     }
1236   } else {
1237     if (oxcf->rc_mode == VPX_Q) {
1238       int qindex = cq_level;
1239       double qstart = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1240       double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
1241                                                0.70, 1.0, 0.85, 1.0 };
1242       int delta_qindex = vp9_compute_qdelta(
1243           rc, qstart,
1244           qstart * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL],
1245           cm->bit_depth);
1246       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1247     } else {
1248       // Use the min of the average Q and active_worst_quality as basis for
1249       // active_best.
1250       if (cm->current_video_frame > 1) {
1251         q = VPXMIN(rc->avg_frame_qindex[INTER_FRAME], active_worst_quality);
1252         active_best_quality = inter_minq[q];
1253       } else {
1254         active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
1255       }
1256       // For the constrained quality mode we don't want
1257       // q to fall below the cq level.
1258       if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
1259         active_best_quality = cq_level;
1260       }
1261     }
1262   }
1263 
1264   // Clip the active best and worst quality values to limits
1265   active_best_quality =
1266       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1267   active_worst_quality =
1268       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1269 
1270   *top_index = active_worst_quality;
1271   *bottom_index = active_best_quality;
1272 
1273 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
1274   {
1275     int qdelta = 0;
1276     vpx_clear_system_state();
1277 
1278     // Limit Q range for the adaptive loop.
1279     if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
1280         !(cm->current_video_frame == 0)) {
1281       qdelta = vp9_compute_qdelta_by_rate(
1282           &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
1283     } else if (!rc->is_src_frame_alt_ref &&
1284                (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1285       qdelta = vp9_compute_qdelta_by_rate(
1286           &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth);
1287     }
1288     if (rc->high_source_sad && cpi->sf.use_altref_onepass) qdelta = 0;
1289     *top_index = active_worst_quality + qdelta;
1290     *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
1291   }
1292 #endif
1293 
1294   if (oxcf->rc_mode == VPX_Q) {
1295     q = active_best_quality;
1296     // Special case code to try and match quality with forced key frames
1297   } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
1298     q = rc->last_boosted_qindex;
1299   } else {
1300     q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1301                           active_worst_quality);
1302     if (q > *top_index) {
1303       // Special case when we are targeting the max allowed rate
1304       if (rc->this_frame_target >= rc->max_frame_bandwidth)
1305         *top_index = q;
1306       else
1307         q = *top_index;
1308     }
1309   }
1310 
1311   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1312   assert(*bottom_index <= rc->worst_quality &&
1313          *bottom_index >= rc->best_quality);
1314   assert(q <= rc->worst_quality && q >= rc->best_quality);
1315   return q;
1316 }
1317 
vp9_frame_type_qdelta(const VP9_COMP * cpi,int rf_level,int q)1318 int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) {
1319   static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
1320     1.00,  // INTER_NORMAL
1321     1.00,  // INTER_HIGH
1322     1.50,  // GF_ARF_LOW
1323     1.75,  // GF_ARF_STD
1324     2.00,  // KF_STD
1325   };
1326   const VP9_COMMON *const cm = &cpi->common;
1327 
1328   int qdelta = vp9_compute_qdelta_by_rate(
1329       &cpi->rc, cm->frame_type, q, rate_factor_deltas[rf_level], cm->bit_depth);
1330   return qdelta;
1331 }
1332 
1333 #define STATIC_MOTION_THRESH 95
1334 
pick_kf_q_bound_two_pass(const VP9_COMP * cpi,int * bottom_index,int * top_index)1335 static void pick_kf_q_bound_two_pass(const VP9_COMP *cpi, int *bottom_index,
1336                                      int *top_index) {
1337   const VP9_COMMON *const cm = &cpi->common;
1338   const RATE_CONTROL *const rc = &cpi->rc;
1339   int active_best_quality;
1340   int active_worst_quality = cpi->twopass.active_worst_quality;
1341 
1342   if (rc->this_key_frame_forced) {
1343     // Handle the special case for key frames forced when we have reached
1344     // the maximum key frame interval. Here force the Q to a range
1345     // based on the ambient Q to reduce the risk of popping.
1346     double last_boosted_q;
1347     int delta_qindex;
1348     int qindex;
1349 
1350     if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1351       qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1352       active_best_quality = qindex;
1353       last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1354       delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1355                                         last_boosted_q * 1.25, cm->bit_depth);
1356       active_worst_quality =
1357           VPXMIN(qindex + delta_qindex, active_worst_quality);
1358     } else {
1359       qindex = rc->last_boosted_qindex;
1360       last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1361       delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1362                                         last_boosted_q * 0.75, cm->bit_depth);
1363       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1364     }
1365   } else {
1366     // Not forced keyframe.
1367     double q_adj_factor = 1.0;
1368     double q_val;
1369     // Baseline value derived from cpi->active_worst_quality and kf boost.
1370     active_best_quality =
1371         get_kf_active_quality(rc, active_worst_quality, cm->bit_depth);
1372     if (cpi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH) {
1373       active_best_quality /= 4;
1374     }
1375 
1376     // Don't allow the active min to be lossless (q0) unlesss the max q
1377     // already indicates lossless.
1378     active_best_quality =
1379         VPXMIN(active_worst_quality, VPXMAX(1, active_best_quality));
1380 
1381     // Allow somewhat lower kf minq with small image formats.
1382     if ((cm->width * cm->height) <= (352 * 288)) {
1383       q_adj_factor -= 0.25;
1384     }
1385 
1386     // Make a further adjustment based on the kf zero motion measure.
1387     q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
1388 
1389     // Convert the adjustment factor to a qindex delta
1390     // on active_best_quality.
1391     q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
1392     active_best_quality +=
1393         vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
1394   }
1395   *top_index = active_worst_quality;
1396   *bottom_index = active_best_quality;
1397 }
1398 
rc_constant_q(const VP9_COMP * cpi,int * bottom_index,int * top_index,int gf_group_index)1399 static int rc_constant_q(const VP9_COMP *cpi, int *bottom_index, int *top_index,
1400                          int gf_group_index) {
1401   const VP9_COMMON *const cm = &cpi->common;
1402   const RATE_CONTROL *const rc = &cpi->rc;
1403   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1404   const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1405   const int is_intra_frame = frame_is_intra_only(cm);
1406 
1407   const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf);
1408 
1409   int q = cq_level;
1410   int active_best_quality = cq_level;
1411   int active_worst_quality = cq_level;
1412 
1413   // Key frame qp decision
1414   if (is_intra_frame && rc->frames_to_key > 1)
1415     pick_kf_q_bound_two_pass(cpi, &active_best_quality, &active_worst_quality);
1416 
1417   // ARF / GF qp decision
1418   if (!is_intra_frame && !rc->is_src_frame_alt_ref &&
1419       cpi->refresh_alt_ref_frame) {
1420     active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1421 
1422     // Modify best quality for second level arfs. For mode VPX_Q this
1423     // becomes the baseline frame q.
1424     if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) {
1425       const int layer_depth = gf_group->layer_depth[gf_group_index];
1426       // linearly fit the frame q depending on the layer depth index from
1427       // the base layer ARF.
1428       active_best_quality = ((layer_depth - 1) * cq_level +
1429                              active_best_quality + layer_depth / 2) /
1430                             layer_depth;
1431     }
1432   }
1433 
1434   q = active_best_quality;
1435   *top_index = active_worst_quality;
1436   *bottom_index = active_best_quality;
1437   return q;
1438 }
1439 
vp9_rc_pick_q_and_bounds_two_pass(const VP9_COMP * cpi,int * bottom_index,int * top_index,int gf_group_index)1440 int vp9_rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, int *bottom_index,
1441                                       int *top_index, int gf_group_index) {
1442   const VP9_COMMON *const cm = &cpi->common;
1443   const RATE_CONTROL *const rc = &cpi->rc;
1444   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1445   const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1446   const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf);
1447   int active_best_quality;
1448   int active_worst_quality = cpi->twopass.active_worst_quality;
1449   int q;
1450   int *inter_minq;
1451   int arf_active_best_quality_hl;
1452   int *arfgf_high_motion_minq, *arfgf_low_motion_minq;
1453   const int boost_frame =
1454       !rc->is_src_frame_alt_ref &&
1455       (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
1456 
1457   ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
1458 
1459   if (oxcf->rc_mode == VPX_Q)
1460     return rc_constant_q(cpi, bottom_index, top_index, gf_group_index);
1461 
1462   if (frame_is_intra_only(cm)) {
1463     pick_kf_q_bound_two_pass(cpi, &active_best_quality, &active_worst_quality);
1464   } else if (boost_frame) {
1465     // Use the lower of active_worst_quality and recent
1466     // average Q as basis for GF/ARF best Q limit unless last frame was
1467     // a key frame.
1468     if (rc->frames_since_key > 1 &&
1469         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1470       q = rc->avg_frame_qindex[INTER_FRAME];
1471     } else {
1472       q = active_worst_quality;
1473     }
1474     // For constrained quality don't allow Q less than the cq level
1475     if (oxcf->rc_mode == VPX_CQ) {
1476       if (q < cq_level) q = cq_level;
1477     }
1478     active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1479     arf_active_best_quality_hl = active_best_quality;
1480 
1481     if (rc->arf_increase_active_best_quality == 1) {
1482       ASSIGN_MINQ_TABLE(cm->bit_depth, arfgf_high_motion_minq);
1483       arf_active_best_quality_hl = arfgf_high_motion_minq[q];
1484     } else if (rc->arf_increase_active_best_quality == -1) {
1485       ASSIGN_MINQ_TABLE(cm->bit_depth, arfgf_low_motion_minq);
1486       arf_active_best_quality_hl = arfgf_low_motion_minq[q];
1487     }
1488     active_best_quality =
1489         (int)((double)active_best_quality *
1490                   rc->arf_active_best_quality_adjustment_factor +
1491               (double)arf_active_best_quality_hl *
1492                   (1.0 - rc->arf_active_best_quality_adjustment_factor));
1493 
1494     // Modify best quality for second level arfs. For mode VPX_Q this
1495     // becomes the baseline frame q.
1496     if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) {
1497       const int layer_depth = gf_group->layer_depth[gf_group_index];
1498       // linearly fit the frame q depending on the layer depth index from
1499       // the base layer ARF.
1500       active_best_quality =
1501           ((layer_depth - 1) * q + active_best_quality + layer_depth / 2) /
1502           layer_depth;
1503     }
1504   } else {
1505     active_best_quality = inter_minq[active_worst_quality];
1506 
1507     // For the constrained quality mode we don't want
1508     // q to fall below the cq level.
1509     if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
1510       active_best_quality = cq_level;
1511     }
1512   }
1513 
1514   // Extension to max or min Q if undershoot or overshoot is outside
1515   // the permitted range.
1516   if (frame_is_intra_only(cm) || boost_frame) {
1517     const int layer_depth = gf_group->layer_depth[gf_group_index];
1518     active_best_quality -=
1519         (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
1520     active_worst_quality += (cpi->twopass.extend_maxq / 2);
1521 
1522     if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) {
1523       assert(layer_depth > 1);
1524       active_best_quality =
1525           VPXMAX(active_best_quality,
1526                  cpi->twopass.last_qindex_of_arf_layer[layer_depth - 1]);
1527     }
1528   } else {
1529     const int max_layer_depth = gf_group->max_layer_depth;
1530     assert(max_layer_depth > 0);
1531 
1532     active_best_quality -=
1533         (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
1534     active_worst_quality += cpi->twopass.extend_maxq;
1535 
1536     // For normal frames do not allow an active minq lower than the q used for
1537     // the last boosted frame.
1538     active_best_quality =
1539         VPXMAX(active_best_quality,
1540                cpi->twopass.last_qindex_of_arf_layer[max_layer_depth - 1]);
1541   }
1542 
1543 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
1544   vpx_clear_system_state();
1545   // Static forced key frames Q restrictions dealt with elsewhere.
1546   if (!frame_is_intra_only(cm) || !rc->this_key_frame_forced ||
1547       cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH) {
1548     int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group_index],
1549                                        active_worst_quality);
1550     active_worst_quality =
1551         VPXMAX(active_worst_quality + qdelta, active_best_quality);
1552   }
1553 #endif
1554 
1555   // Modify active_best_quality for downscaled normal frames.
1556   if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
1557     int qdelta = vp9_compute_qdelta_by_rate(
1558         rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth);
1559     active_best_quality =
1560         VPXMAX(active_best_quality + qdelta, rc->best_quality);
1561   }
1562 
1563   active_best_quality =
1564       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1565   active_worst_quality =
1566       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1567 
1568   if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
1569     // If static since last kf use better of last boosted and last kf q.
1570     if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1571       q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1572     } else {
1573       q = rc->last_boosted_qindex;
1574     }
1575   } else if (frame_is_intra_only(cm) && !rc->this_key_frame_forced) {
1576     q = active_best_quality;
1577   } else {
1578     q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1579                           active_worst_quality);
1580     if (q > active_worst_quality) {
1581       // Special case when we are targeting the max allowed rate.
1582       if (rc->this_frame_target >= rc->max_frame_bandwidth)
1583         active_worst_quality = q;
1584       else
1585         q = active_worst_quality;
1586     }
1587   }
1588 
1589   *top_index = active_worst_quality;
1590   *bottom_index = active_best_quality;
1591 
1592   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1593   assert(*bottom_index <= rc->worst_quality &&
1594          *bottom_index >= rc->best_quality);
1595   assert(q <= rc->worst_quality && q >= rc->best_quality);
1596   return q;
1597 }
1598 
vp9_rc_pick_q_and_bounds(const VP9_COMP * cpi,int * bottom_index,int * top_index)1599 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, int *bottom_index,
1600                              int *top_index) {
1601   int q;
1602   const int gf_group_index = cpi->twopass.gf_group.index;
1603   if (cpi->oxcf.pass == 0) {
1604     if (cpi->oxcf.rc_mode == VPX_CBR)
1605       q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
1606     else
1607       q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
1608   } else {
1609     q = vp9_rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index,
1610                                           gf_group_index);
1611   }
1612   if (cpi->sf.use_nonrd_pick_mode) {
1613     if (cpi->sf.force_frame_boost == 1) q -= cpi->sf.max_delta_qindex;
1614 
1615     if (q < *bottom_index)
1616       *bottom_index = q;
1617     else if (q > *top_index)
1618       *top_index = q;
1619   }
1620   return q;
1621 }
1622 
vp9_configure_buffer_updates(VP9_COMP * cpi,int gf_group_index)1623 void vp9_configure_buffer_updates(VP9_COMP *cpi, int gf_group_index) {
1624   VP9_COMMON *cm = &cpi->common;
1625   TWO_PASS *const twopass = &cpi->twopass;
1626 
1627   cpi->rc.is_src_frame_alt_ref = 0;
1628   cm->show_existing_frame = 0;
1629   cpi->rc.show_arf_as_gld = 0;
1630   switch (twopass->gf_group.update_type[gf_group_index]) {
1631     case KF_UPDATE:
1632       cpi->refresh_last_frame = 1;
1633       cpi->refresh_golden_frame = 1;
1634       cpi->refresh_alt_ref_frame = 1;
1635       break;
1636     case LF_UPDATE:
1637       cpi->refresh_last_frame = 1;
1638       cpi->refresh_golden_frame = 0;
1639       cpi->refresh_alt_ref_frame = 0;
1640       break;
1641     case GF_UPDATE:
1642       cpi->refresh_last_frame = 1;
1643       cpi->refresh_golden_frame = 1;
1644       cpi->refresh_alt_ref_frame = 0;
1645       break;
1646     case OVERLAY_UPDATE:
1647       cpi->refresh_last_frame = 0;
1648       cpi->refresh_golden_frame = 1;
1649       cpi->refresh_alt_ref_frame = 0;
1650       cpi->rc.is_src_frame_alt_ref = 1;
1651       if (cpi->rc.preserve_arf_as_gld) {
1652         cpi->rc.show_arf_as_gld = 1;
1653         cpi->refresh_golden_frame = 0;
1654         cm->show_existing_frame = 1;
1655         cm->refresh_frame_context = 0;
1656       }
1657       break;
1658     case MID_OVERLAY_UPDATE:
1659       cpi->refresh_last_frame = 1;
1660       cpi->refresh_golden_frame = 0;
1661       cpi->refresh_alt_ref_frame = 0;
1662       cpi->rc.is_src_frame_alt_ref = 1;
1663       break;
1664     case USE_BUF_FRAME:
1665       cpi->refresh_last_frame = 0;
1666       cpi->refresh_golden_frame = 0;
1667       cpi->refresh_alt_ref_frame = 0;
1668       cpi->rc.is_src_frame_alt_ref = 1;
1669       cm->show_existing_frame = 1;
1670       cm->refresh_frame_context = 0;
1671       break;
1672     default:
1673       assert(twopass->gf_group.update_type[gf_group_index] == ARF_UPDATE);
1674       cpi->refresh_last_frame = 0;
1675       cpi->refresh_golden_frame = 0;
1676       cpi->refresh_alt_ref_frame = 1;
1677       break;
1678   }
1679 }
1680 
vp9_rc_compute_frame_size_bounds(const VP9_COMP * cpi,int frame_target,int * frame_under_shoot_limit,int * frame_over_shoot_limit)1681 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, int frame_target,
1682                                       int *frame_under_shoot_limit,
1683                                       int *frame_over_shoot_limit) {
1684   if (cpi->oxcf.rc_mode == VPX_Q) {
1685     *frame_under_shoot_limit = 0;
1686     *frame_over_shoot_limit = INT_MAX;
1687   } else {
1688     // For very small rate targets where the fractional adjustment
1689     // may be tiny make sure there is at least a minimum range.
1690     const int tol_low =
1691         (int)(((int64_t)cpi->sf.recode_tolerance_low * frame_target) / 100);
1692     const int tol_high =
1693         (int)(((int64_t)cpi->sf.recode_tolerance_high * frame_target) / 100);
1694     *frame_under_shoot_limit = VPXMAX(frame_target - tol_low - 100, 0);
1695     *frame_over_shoot_limit =
1696         VPXMIN(frame_target + tol_high + 100, cpi->rc.max_frame_bandwidth);
1697   }
1698 }
1699 
vp9_rc_set_frame_target(VP9_COMP * cpi,int target)1700 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
1701   const VP9_COMMON *const cm = &cpi->common;
1702   RATE_CONTROL *const rc = &cpi->rc;
1703 
1704   rc->this_frame_target = target;
1705 
1706   // Modify frame size target when down-scaling.
1707   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
1708       rc->frame_size_selector != UNSCALED) {
1709     rc->this_frame_target = (int)(rc->this_frame_target *
1710                                   rate_thresh_mult[rc->frame_size_selector]);
1711   }
1712 
1713 #if CONFIG_RATE_CTRL
1714   if (cpi->oxcf.use_simple_encode_api) {
1715     if (cpi->encode_command.use_external_target_frame_bits) {
1716       rc->this_frame_target = cpi->encode_command.target_frame_bits;
1717     }
1718   }
1719 #endif  // CONFIG_RATE_CTRL
1720 
1721   // Target rate per SB64 (including partial SB64s.
1722   const int64_t sb64_target_rate =
1723       ((int64_t)rc->this_frame_target * 64 * 64) / (cm->width * cm->height);
1724   rc->sb64_target_rate = (int)VPXMIN(sb64_target_rate, INT_MAX);
1725 }
1726 
update_alt_ref_frame_stats(VP9_COMP * cpi)1727 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
1728   // this frame refreshes means next frames don't unless specified by user
1729   RATE_CONTROL *const rc = &cpi->rc;
1730   rc->frames_since_golden = 0;
1731 
1732   // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1733   rc->source_alt_ref_pending = 0;
1734 
1735   // Set the alternate reference frame active flag
1736   rc->source_alt_ref_active = 1;
1737 }
1738 
update_golden_frame_stats(VP9_COMP * cpi)1739 static void update_golden_frame_stats(VP9_COMP *cpi) {
1740   RATE_CONTROL *const rc = &cpi->rc;
1741 
1742   // Update the Golden frame usage counts.
1743   if (cpi->refresh_golden_frame) {
1744     // this frame refreshes means next frames don't unless specified by user
1745     rc->frames_since_golden = 0;
1746 
1747     // If we are not using alt ref in the up and coming group clear the arf
1748     // active flag. In multi arf group case, if the index is not 0 then
1749     // we are overlaying a mid group arf so should not reset the flag.
1750     if (cpi->oxcf.pass == 2) {
1751       if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1752         rc->source_alt_ref_active = 0;
1753     } else if (!rc->source_alt_ref_pending) {
1754       rc->source_alt_ref_active = 0;
1755     }
1756 
1757     // Decrement count down till next gf
1758     if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1759 
1760   } else if (!cpi->refresh_alt_ref_frame) {
1761     // Decrement count down till next gf
1762     if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1763 
1764     rc->frames_since_golden++;
1765 
1766     if (rc->show_arf_as_gld) {
1767       rc->frames_since_golden = 0;
1768       // If we are not using alt ref in the up and coming group clear the arf
1769       // active flag. In multi arf group case, if the index is not 0 then
1770       // we are overlaying a mid group arf so should not reset the flag.
1771       if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1772         rc->source_alt_ref_active = 0;
1773     }
1774   }
1775 }
1776 
update_altref_usage(VP9_COMP * const cpi)1777 static void update_altref_usage(VP9_COMP *const cpi) {
1778   VP9_COMMON *const cm = &cpi->common;
1779   int sum_ref_frame_usage = 0;
1780   int arf_frame_usage = 0;
1781   int mi_row, mi_col;
1782   if (cpi->rc.alt_ref_gf_group && !cpi->rc.is_src_frame_alt_ref &&
1783       !cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame)
1784     for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8) {
1785       for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8) {
1786         int sboffset = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
1787         sum_ref_frame_usage += cpi->count_arf_frame_usage[sboffset] +
1788                                cpi->count_lastgolden_frame_usage[sboffset];
1789         arf_frame_usage += cpi->count_arf_frame_usage[sboffset];
1790       }
1791     }
1792   if (sum_ref_frame_usage > 0) {
1793     double altref_count = 100.0 * arf_frame_usage / sum_ref_frame_usage;
1794     cpi->rc.perc_arf_usage =
1795         0.75 * cpi->rc.perc_arf_usage + 0.25 * altref_count;
1796   }
1797 }
1798 
vp9_compute_frame_low_motion(VP9_COMP * const cpi)1799 void vp9_compute_frame_low_motion(VP9_COMP *const cpi) {
1800   VP9_COMMON *const cm = &cpi->common;
1801   SVC *const svc = &cpi->svc;
1802   int mi_row, mi_col;
1803   MODE_INFO **mi = cm->mi_grid_visible;
1804   RATE_CONTROL *const rc = &cpi->rc;
1805   const int rows = cm->mi_rows, cols = cm->mi_cols;
1806   int cnt_zeromv = 0;
1807   for (mi_row = 0; mi_row < rows; mi_row++) {
1808     for (mi_col = 0; mi_col < cols; mi_col++) {
1809       if (mi[0]->ref_frame[0] == LAST_FRAME &&
1810           abs(mi[0]->mv[0].as_mv.row) < 16 && abs(mi[0]->mv[0].as_mv.col) < 16)
1811         cnt_zeromv++;
1812       mi++;
1813     }
1814     mi += 8;
1815   }
1816   cnt_zeromv = 100 * cnt_zeromv / (rows * cols);
1817   rc->avg_frame_low_motion = (3 * rc->avg_frame_low_motion + cnt_zeromv) >> 2;
1818 
1819   // For SVC: set avg_frame_low_motion (only computed on top spatial layer)
1820   // to all lower spatial layers.
1821   if (cpi->use_svc && svc->spatial_layer_id == svc->number_spatial_layers - 1) {
1822     int i;
1823     for (i = 0; i < svc->number_spatial_layers - 1; ++i) {
1824       const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
1825                                          svc->number_temporal_layers);
1826       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1827       RATE_CONTROL *const lrc = &lc->rc;
1828       lrc->avg_frame_low_motion = rc->avg_frame_low_motion;
1829     }
1830   }
1831 }
1832 
vp9_rc_postencode_update(VP9_COMP * cpi,uint64_t bytes_used)1833 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
1834   const VP9_COMMON *const cm = &cpi->common;
1835   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1836   RATE_CONTROL *const rc = &cpi->rc;
1837   SVC *const svc = &cpi->svc;
1838   const int qindex = cm->base_qindex;
1839   const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1840   const int gf_group_index = cpi->twopass.gf_group.index;
1841   const int layer_depth = gf_group->layer_depth[gf_group_index];
1842 
1843   // Update rate control heuristics
1844   rc->projected_frame_size = (int)(bytes_used << 3);
1845 
1846   // Post encode loop adjustment of Q prediction.
1847   vp9_rc_update_rate_correction_factors(cpi);
1848 
1849   // Keep a record of last Q and ambient average Q.
1850   if (frame_is_intra_only(cm)) {
1851     rc->last_q[KEY_FRAME] = qindex;
1852     rc->avg_frame_qindex[KEY_FRAME] =
1853         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1854     if (cpi->use_svc) {
1855       int i;
1856       for (i = 0; i < svc->number_temporal_layers; ++i) {
1857         const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
1858                                            svc->number_temporal_layers);
1859         LAYER_CONTEXT *lc = &svc->layer_context[layer];
1860         RATE_CONTROL *lrc = &lc->rc;
1861         lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME];
1862         lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME];
1863       }
1864     }
1865   } else {
1866     if ((cpi->use_svc) ||
1867         (!rc->is_src_frame_alt_ref &&
1868          !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1869       rc->last_q[INTER_FRAME] = qindex;
1870       rc->avg_frame_qindex[INTER_FRAME] =
1871           ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1872       rc->ni_frames++;
1873       rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1874       rc->avg_q = rc->tot_q / rc->ni_frames;
1875       // Calculate the average Q for normal inter frames (not key or GFU
1876       // frames).
1877       rc->ni_tot_qi += qindex;
1878       rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1879     }
1880   }
1881 
1882   if (cpi->use_svc) vp9_svc_adjust_avg_frame_qindex(cpi);
1883 
1884   // Keep record of last boosted (KF/KF/ARF) Q value.
1885   // If the current frame is coded at a lower Q then we also update it.
1886   // If all mbs in this group are skipped only update if the Q value is
1887   // better than that already stored.
1888   // This is used to help set quality in forced key frames to reduce popping
1889   if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) ||
1890       (!rc->constrained_gf_group &&
1891        (cpi->refresh_alt_ref_frame ||
1892         (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1893     rc->last_boosted_qindex = qindex;
1894   }
1895 
1896   if ((qindex < cpi->twopass.last_qindex_of_arf_layer[layer_depth]) ||
1897       (cm->frame_type == KEY_FRAME) ||
1898       (!rc->constrained_gf_group &&
1899        (cpi->refresh_alt_ref_frame ||
1900         (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1901     cpi->twopass.last_qindex_of_arf_layer[layer_depth] = qindex;
1902   }
1903 
1904   if (frame_is_intra_only(cm)) rc->last_kf_qindex = qindex;
1905 
1906   update_buffer_level_postencode(cpi, rc->projected_frame_size);
1907 
1908   // Rolling monitors of whether we are over or underspending used to help
1909   // regulate min and Max Q in two pass.
1910   if (!frame_is_intra_only(cm)) {
1911     rc->rolling_target_bits = (int)ROUND64_POWER_OF_TWO(
1912         (int64_t)rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1913     rc->rolling_actual_bits = (int)ROUND64_POWER_OF_TWO(
1914         (int64_t)rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1915     rc->long_rolling_target_bits = (int)ROUND64_POWER_OF_TWO(
1916         (int64_t)rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1917     rc->long_rolling_actual_bits = (int)ROUND64_POWER_OF_TWO(
1918         (int64_t)rc->long_rolling_actual_bits * 31 + rc->projected_frame_size,
1919         5);
1920   }
1921 
1922   // Actual bits spent
1923   rc->total_actual_bits += rc->projected_frame_size;
1924   rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1925 
1926   rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1927 
1928   if (!cpi->use_svc) {
1929     if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1930         (!frame_is_intra_only(cm)))
1931       // Update the alternate reference frame stats as appropriate.
1932       update_alt_ref_frame_stats(cpi);
1933     else
1934       // Update the Golden frame stats as appropriate.
1935       update_golden_frame_stats(cpi);
1936   }
1937 
1938   // If second (long term) temporal reference is used for SVC,
1939   // update the golden frame counter, only for base temporal layer.
1940   if (cpi->use_svc && svc->use_gf_temporal_ref_current_layer &&
1941       svc->temporal_layer_id == 0) {
1942     int i = 0;
1943     if (cpi->refresh_golden_frame)
1944       rc->frames_since_golden = 0;
1945     else
1946       rc->frames_since_golden++;
1947     // Decrement count down till next gf
1948     if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1949     // Update the frames_since_golden for all upper temporal layers.
1950     for (i = 1; i < svc->number_temporal_layers; ++i) {
1951       const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
1952                                          svc->number_temporal_layers);
1953       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1954       RATE_CONTROL *const lrc = &lc->rc;
1955       lrc->frames_since_golden = rc->frames_since_golden;
1956     }
1957   }
1958 
1959   if (frame_is_intra_only(cm)) rc->frames_since_key = 0;
1960   if (cm->show_frame) {
1961     rc->frames_since_key++;
1962     rc->frames_to_key--;
1963   }
1964 
1965   // Trigger the resizing of the next frame if it is scaled.
1966   if (oxcf->pass != 0) {
1967     cpi->resize_pending =
1968         rc->next_frame_size_selector != rc->frame_size_selector;
1969     rc->frame_size_selector = rc->next_frame_size_selector;
1970   }
1971 
1972   if (oxcf->pass == 0) {
1973     if (!frame_is_intra_only(cm))
1974       if (cpi->sf.use_altref_onepass) update_altref_usage(cpi);
1975     cpi->rc.last_frame_is_src_altref = cpi->rc.is_src_frame_alt_ref;
1976   }
1977 
1978   if (!frame_is_intra_only(cm)) rc->reset_high_source_sad = 0;
1979 
1980   rc->last_avg_frame_bandwidth = rc->avg_frame_bandwidth;
1981   if (cpi->use_svc && svc->spatial_layer_id < svc->number_spatial_layers - 1)
1982     svc->lower_layer_qindex = cm->base_qindex;
1983   cpi->deadline_mode_previous_frame = cpi->oxcf.mode;
1984 }
1985 
vp9_rc_postencode_update_drop_frame(VP9_COMP * cpi)1986 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
1987   cpi->common.current_video_frame++;
1988   cpi->rc.frames_since_key++;
1989   cpi->rc.frames_to_key--;
1990   cpi->rc.rc_2_frame = 0;
1991   cpi->rc.rc_1_frame = 0;
1992   cpi->rc.last_avg_frame_bandwidth = cpi->rc.avg_frame_bandwidth;
1993   cpi->rc.last_q[INTER_FRAME] = cpi->common.base_qindex;
1994   // For SVC on dropped frame when framedrop_mode != LAYER_DROP:
1995   // in this mode the whole superframe may be dropped if only a single layer
1996   // has buffer underflow (below threshold). Since this can then lead to
1997   // increasing buffer levels/overflow for certain layers even though whole
1998   // superframe is dropped, we cap buffer level if its already stable.
1999   if (cpi->use_svc && cpi->svc.framedrop_mode != LAYER_DROP &&
2000       cpi->rc.buffer_level > cpi->rc.optimal_buffer_level) {
2001     cpi->rc.buffer_level = cpi->rc.optimal_buffer_level;
2002     cpi->rc.bits_off_target = cpi->rc.optimal_buffer_level;
2003   }
2004   cpi->deadline_mode_previous_frame = cpi->oxcf.mode;
2005 }
2006 
vp9_calc_pframe_target_size_one_pass_vbr(const VP9_COMP * cpi)2007 int vp9_calc_pframe_target_size_one_pass_vbr(const VP9_COMP *cpi) {
2008   const RATE_CONTROL *const rc = &cpi->rc;
2009   const int af_ratio = rc->af_ratio_onepass_vbr;
2010   int64_t target =
2011       (!rc->is_src_frame_alt_ref &&
2012        (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
2013           ? ((int64_t)rc->avg_frame_bandwidth * rc->baseline_gf_interval *
2014              af_ratio) /
2015                 (rc->baseline_gf_interval + af_ratio - 1)
2016           : ((int64_t)rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
2017                 (rc->baseline_gf_interval + af_ratio - 1);
2018   // For SVC: refresh flags are used to define the pattern, so we can't
2019   // use that for boosting the target size here.
2020   // TODO(marpan): Consider adding internal boost on TL0 for VBR-SVC.
2021   // For now just use the CBR logic for setting target size.
2022   if (cpi->use_svc) target = vp9_calc_pframe_target_size_one_pass_cbr(cpi);
2023   if (target > INT_MAX) target = INT_MAX;
2024   return vp9_rc_clamp_pframe_target_size(cpi, (int)target);
2025 }
2026 
vp9_calc_iframe_target_size_one_pass_vbr(const VP9_COMP * cpi)2027 int vp9_calc_iframe_target_size_one_pass_vbr(const VP9_COMP *cpi) {
2028   static const int kf_ratio = 25;
2029   const RATE_CONTROL *rc = &cpi->rc;
2030   int target = rc->avg_frame_bandwidth;
2031   if (target > INT_MAX / kf_ratio)
2032     target = INT_MAX;
2033   else
2034     target = rc->avg_frame_bandwidth * kf_ratio;
2035   return vp9_rc_clamp_iframe_target_size(cpi, target);
2036 }
2037 
adjust_gfint_frame_constraint(VP9_COMP * cpi,int frame_constraint)2038 static void adjust_gfint_frame_constraint(VP9_COMP *cpi, int frame_constraint) {
2039   RATE_CONTROL *const rc = &cpi->rc;
2040   rc->constrained_gf_group = 0;
2041   // Reset gf interval to make more equal spacing for frame_constraint.
2042   if ((frame_constraint <= 7 * rc->baseline_gf_interval >> 2) &&
2043       (frame_constraint > rc->baseline_gf_interval)) {
2044     rc->baseline_gf_interval = frame_constraint >> 1;
2045     if (rc->baseline_gf_interval < 5)
2046       rc->baseline_gf_interval = frame_constraint;
2047     rc->constrained_gf_group = 1;
2048   } else {
2049     // Reset to keep gf_interval <= frame_constraint.
2050     if (rc->baseline_gf_interval > frame_constraint) {
2051       rc->baseline_gf_interval = frame_constraint;
2052       rc->constrained_gf_group = 1;
2053     }
2054   }
2055 }
2056 
vp9_set_gf_update_one_pass_vbr(VP9_COMP * const cpi)2057 void vp9_set_gf_update_one_pass_vbr(VP9_COMP *const cpi) {
2058   RATE_CONTROL *const rc = &cpi->rc;
2059   VP9_COMMON *const cm = &cpi->common;
2060   if (rc->frames_till_gf_update_due == 0) {
2061     double rate_err = 1.0;
2062     rc->gfu_boost = DEFAULT_GF_BOOST;
2063     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0) {
2064       vp9_cyclic_refresh_set_golden_update(cpi);
2065     } else {
2066       rc->baseline_gf_interval = VPXMIN(
2067           20, VPXMAX(10, (rc->min_gf_interval + rc->max_gf_interval) / 2));
2068     }
2069     rc->af_ratio_onepass_vbr = 10;
2070     if (rc->rolling_target_bits > 0)
2071       rate_err =
2072           (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
2073     if (cm->current_video_frame > 30) {
2074       if (rc->avg_frame_qindex[INTER_FRAME] > (7 * rc->worst_quality) >> 3 &&
2075           rate_err > 3.5) {
2076         rc->baseline_gf_interval =
2077             VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
2078       } else if (rc->avg_frame_low_motion > 0 &&
2079                  rc->avg_frame_low_motion < 20) {
2080         // Decrease gf interval for high motion case.
2081         rc->baseline_gf_interval = VPXMAX(6, rc->baseline_gf_interval >> 1);
2082       }
2083       // Adjust boost and af_ratio based on avg_frame_low_motion, which
2084       // varies between 0 and 100 (stationary, 100% zero/small motion).
2085       if (rc->avg_frame_low_motion > 0)
2086         rc->gfu_boost =
2087             VPXMAX(500, DEFAULT_GF_BOOST * (rc->avg_frame_low_motion << 1) /
2088                             (rc->avg_frame_low_motion + 100));
2089       else if (rc->avg_frame_low_motion == 0 && rate_err > 1.0)
2090         rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
2091       rc->af_ratio_onepass_vbr = VPXMIN(15, VPXMAX(5, 3 * rc->gfu_boost / 400));
2092     }
2093     if (rc->constrain_gf_key_freq_onepass_vbr)
2094       adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
2095     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2096     cpi->refresh_golden_frame = 1;
2097     rc->source_alt_ref_pending = 0;
2098     rc->alt_ref_gf_group = 0;
2099     if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
2100       rc->source_alt_ref_pending = 1;
2101       rc->alt_ref_gf_group = 1;
2102     }
2103   }
2104 }
2105 
vp9_rc_get_one_pass_vbr_params(VP9_COMP * cpi)2106 void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
2107   VP9_COMMON *const cm = &cpi->common;
2108   RATE_CONTROL *const rc = &cpi->rc;
2109   int target;
2110   if (!cpi->refresh_alt_ref_frame &&
2111       (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
2112        rc->frames_to_key == 0 ||
2113        (cpi->oxcf.mode != cpi->deadline_mode_previous_frame))) {
2114     cm->frame_type = KEY_FRAME;
2115     rc->this_key_frame_forced =
2116         cm->current_video_frame != 0 && rc->frames_to_key == 0;
2117     rc->frames_to_key = cpi->oxcf.key_freq;
2118     rc->kf_boost = DEFAULT_KF_BOOST;
2119     rc->source_alt_ref_active = 0;
2120   } else {
2121     cm->frame_type = INTER_FRAME;
2122   }
2123   vp9_set_gf_update_one_pass_vbr(cpi);
2124   if (cm->frame_type == KEY_FRAME)
2125     target = vp9_calc_iframe_target_size_one_pass_vbr(cpi);
2126   else
2127     target = vp9_calc_pframe_target_size_one_pass_vbr(cpi);
2128   vp9_rc_set_frame_target(cpi, target);
2129   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0)
2130     vp9_cyclic_refresh_update_parameters(cpi);
2131 }
2132 
vp9_calc_pframe_target_size_one_pass_cbr(const VP9_COMP * cpi)2133 int vp9_calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
2134   const VP9EncoderConfig *oxcf = &cpi->oxcf;
2135   const RATE_CONTROL *rc = &cpi->rc;
2136   const SVC *const svc = &cpi->svc;
2137   const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
2138   const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
2139   int min_frame_target =
2140       VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
2141   int target;
2142 
2143   if (oxcf->gf_cbr_boost_pct) {
2144     const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
2145     target = cpi->refresh_golden_frame
2146                  ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval *
2147                     af_ratio_pct) /
2148                        (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
2149                  : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
2150                        (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
2151   } else {
2152     target = rc->avg_frame_bandwidth;
2153   }
2154   if (is_one_pass_svc(cpi)) {
2155     // Note that for layers, avg_frame_bandwidth is the cumulative
2156     // per-frame-bandwidth. For the target size of this frame, use the
2157     // layer average frame size (i.e., non-cumulative per-frame-bw).
2158     int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2159                                  svc->number_temporal_layers);
2160     const LAYER_CONTEXT *lc = &svc->layer_context[layer];
2161     target = lc->avg_frame_size;
2162     min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
2163   }
2164   if (diff > 0) {
2165     // Lower the target bandwidth for this frame.
2166     const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
2167     target -= (int)(((int64_t)target * pct_low) / 200);
2168   } else if (diff < 0) {
2169     // Increase the target bandwidth for this frame.
2170     const int pct_high =
2171         (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
2172     target += (int)(((int64_t)target * pct_high) / 200);
2173   }
2174   if (oxcf->rc_max_inter_bitrate_pct) {
2175     const int max_rate =
2176         rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
2177     target = VPXMIN(target, max_rate);
2178   }
2179   return VPXMAX(min_frame_target, target);
2180 }
2181 
vp9_calc_iframe_target_size_one_pass_cbr(const VP9_COMP * cpi)2182 int vp9_calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
2183   const RATE_CONTROL *rc = &cpi->rc;
2184   const VP9EncoderConfig *oxcf = &cpi->oxcf;
2185   const SVC *const svc = &cpi->svc;
2186   int64_t target;
2187   if (cpi->common.current_video_frame == 0) {
2188     target = rc->starting_buffer_level / 2;
2189   } else {
2190     int kf_boost = 32;
2191     double framerate = cpi->framerate;
2192     if (svc->number_temporal_layers > 1 && oxcf->rc_mode == VPX_CBR) {
2193       // Use the layer framerate for temporal layers CBR mode.
2194       const int layer =
2195           LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2196                            svc->number_temporal_layers);
2197       const LAYER_CONTEXT *lc = &svc->layer_context[layer];
2198       framerate = lc->framerate;
2199     }
2200     kf_boost = VPXMAX(kf_boost, (int)round(2 * framerate - 16));
2201     if (rc->frames_since_key < framerate / 2) {
2202       kf_boost = (int)round(kf_boost * rc->frames_since_key / (framerate / 2));
2203     }
2204 
2205     target = ((int64_t)(16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
2206   }
2207   target = VPXMIN(INT_MAX, target);
2208   return vp9_rc_clamp_iframe_target_size(cpi, (int)target);
2209 }
2210 
set_intra_only_frame(VP9_COMP * cpi)2211 static void set_intra_only_frame(VP9_COMP *cpi) {
2212   VP9_COMMON *const cm = &cpi->common;
2213   SVC *const svc = &cpi->svc;
2214   // Don't allow intra_only frame for bypass/flexible SVC mode, or if number
2215   // of spatial layers is 1 or if number of spatial or temporal layers > 3.
2216   // Also if intra-only is inserted on very first frame, don't allow if
2217   // if number of temporal layers > 1. This is because on intra-only frame
2218   // only 3 reference buffers can be updated, but for temporal layers > 1
2219   // we generally need to use buffer slots 4 and 5.
2220   if ((cm->current_video_frame == 0 && svc->number_temporal_layers > 1) ||
2221       svc->number_spatial_layers > 3 || svc->number_temporal_layers > 3 ||
2222       svc->number_spatial_layers == 1)
2223     return;
2224   cm->show_frame = 0;
2225   cm->intra_only = 1;
2226   cm->frame_type = INTER_FRAME;
2227   cpi->ext_refresh_frame_flags_pending = 1;
2228   cpi->ext_refresh_last_frame = 1;
2229   cpi->ext_refresh_golden_frame = 1;
2230   cpi->ext_refresh_alt_ref_frame = 1;
2231   if (cm->current_video_frame == 0) {
2232     cpi->lst_fb_idx = 0;
2233     cpi->gld_fb_idx = 1;
2234     cpi->alt_fb_idx = 2;
2235   } else {
2236     int i;
2237     int count = 0;
2238     cpi->lst_fb_idx = -1;
2239     cpi->gld_fb_idx = -1;
2240     cpi->alt_fb_idx = -1;
2241     svc->update_buffer_slot[0] = 0;
2242     // For intra-only frame we need to refresh all slots that were
2243     // being used for the base layer (fb_idx_base[i] == 1).
2244     // Start with assigning last first, then golden and then alt.
2245     for (i = 0; i < REF_FRAMES; ++i) {
2246       if (svc->fb_idx_base[i] == 1) {
2247         svc->update_buffer_slot[0] |= 1 << i;
2248         count++;
2249       }
2250       if (count == 1 && cpi->lst_fb_idx == -1) cpi->lst_fb_idx = i;
2251       if (count == 2 && cpi->gld_fb_idx == -1) cpi->gld_fb_idx = i;
2252       if (count == 3 && cpi->alt_fb_idx == -1) cpi->alt_fb_idx = i;
2253     }
2254     // If golden or alt is not being used for base layer, then set them
2255     // to the lst_fb_idx.
2256     if (cpi->gld_fb_idx == -1) cpi->gld_fb_idx = cpi->lst_fb_idx;
2257     if (cpi->alt_fb_idx == -1) cpi->alt_fb_idx = cpi->lst_fb_idx;
2258     if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
2259       cpi->ext_refresh_last_frame = 0;
2260       cpi->ext_refresh_golden_frame = 0;
2261       cpi->ext_refresh_alt_ref_frame = 0;
2262       cpi->ref_frame_flags = 0;
2263     }
2264   }
2265 }
2266 
vp9_rc_get_svc_params(VP9_COMP * cpi)2267 void vp9_rc_get_svc_params(VP9_COMP *cpi) {
2268   VP9_COMMON *const cm = &cpi->common;
2269   RATE_CONTROL *const rc = &cpi->rc;
2270   SVC *const svc = &cpi->svc;
2271   int target = rc->avg_frame_bandwidth;
2272   int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2273                                svc->number_temporal_layers);
2274   if (svc->first_spatial_layer_to_encode)
2275     svc->layer_context[svc->temporal_layer_id].is_key_frame = 0;
2276   // Periodic key frames is based on the super-frame counter
2277   // (svc.current_superframe), also only base spatial layer is key frame.
2278   // Key frame is set for any of the following: very first frame, frame flags
2279   // indicates key, superframe counter hits key frequency,(non-intra) sync
2280   // flag is set for spatial layer 0, or deadline mode changes.
2281   if ((cm->current_video_frame == 0 && !svc->previous_frame_is_intra_only) ||
2282       (cpi->frame_flags & FRAMEFLAGS_KEY) ||
2283       (cpi->oxcf.auto_key &&
2284        (svc->current_superframe % cpi->oxcf.key_freq == 0) &&
2285        !svc->previous_frame_is_intra_only && svc->spatial_layer_id == 0) ||
2286       (svc->spatial_layer_sync[0] == 1 && svc->spatial_layer_id == 0) ||
2287       (cpi->oxcf.mode != cpi->deadline_mode_previous_frame)) {
2288     cm->frame_type = KEY_FRAME;
2289     rc->source_alt_ref_active = 0;
2290     if (is_one_pass_svc(cpi)) {
2291       if (cm->current_video_frame > 0) vp9_svc_reset_temporal_layers(cpi, 1);
2292       layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2293                                svc->number_temporal_layers);
2294       svc->layer_context[layer].is_key_frame = 1;
2295       cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2296       // Assumption here is that LAST_FRAME is being updated for a keyframe.
2297       // Thus no change in update flags.
2298       if (cpi->oxcf.rc_mode == VPX_CBR)
2299         target = vp9_calc_iframe_target_size_one_pass_cbr(cpi);
2300       else
2301         target = vp9_calc_iframe_target_size_one_pass_vbr(cpi);
2302     }
2303   } else {
2304     cm->frame_type = INTER_FRAME;
2305     if (is_one_pass_svc(cpi)) {
2306       LAYER_CONTEXT *lc = &svc->layer_context[layer];
2307       // Add condition current_video_frame > 0 for the case where first frame
2308       // is intra only followed by overlay/copy frame. In this case we don't
2309       // want to reset is_key_frame to 0 on overlay/copy frame.
2310       lc->is_key_frame =
2311           (svc->spatial_layer_id == 0 && cm->current_video_frame > 0)
2312               ? 0
2313               : svc->layer_context[svc->temporal_layer_id].is_key_frame;
2314       if (cpi->oxcf.rc_mode == VPX_CBR) {
2315         target = vp9_calc_pframe_target_size_one_pass_cbr(cpi);
2316       } else {
2317         double rate_err = 0.0;
2318         rc->fac_active_worst_inter = 140;
2319         rc->fac_active_worst_gf = 100;
2320         if (rc->rolling_target_bits > 0) {
2321           rate_err =
2322               (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
2323           if (rate_err < 1.0)
2324             rc->fac_active_worst_inter = 120;
2325           else if (rate_err > 2.0)
2326             // Increase active_worst faster if rate fluctuation is high.
2327             rc->fac_active_worst_inter = 160;
2328         }
2329         target = vp9_calc_pframe_target_size_one_pass_vbr(cpi);
2330       }
2331     }
2332   }
2333 
2334   if (svc->simulcast_mode) {
2335     if (svc->spatial_layer_id > 0 &&
2336         svc->layer_context[layer].is_key_frame == 1) {
2337       cm->frame_type = KEY_FRAME;
2338       cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2339       if (cpi->oxcf.rc_mode == VPX_CBR)
2340         target = vp9_calc_iframe_target_size_one_pass_cbr(cpi);
2341       else
2342         target = vp9_calc_iframe_target_size_one_pass_vbr(cpi);
2343     }
2344     // Set the buffer idx and refresh flags for key frames in simulcast mode.
2345     // Note the buffer slot for long-term reference is set below (line 2255),
2346     // and alt_ref is used for that on key frame. So use last and golden for
2347     // the other two normal slots.
2348     if (cm->frame_type == KEY_FRAME) {
2349       if (svc->number_spatial_layers == 2) {
2350         if (svc->spatial_layer_id == 0) {
2351           cpi->lst_fb_idx = 0;
2352           cpi->gld_fb_idx = 2;
2353           cpi->alt_fb_idx = 6;
2354         } else if (svc->spatial_layer_id == 1) {
2355           cpi->lst_fb_idx = 1;
2356           cpi->gld_fb_idx = 3;
2357           cpi->alt_fb_idx = 6;
2358         }
2359       } else if (svc->number_spatial_layers == 3) {
2360         if (svc->spatial_layer_id == 0) {
2361           cpi->lst_fb_idx = 0;
2362           cpi->gld_fb_idx = 3;
2363           cpi->alt_fb_idx = 6;
2364         } else if (svc->spatial_layer_id == 1) {
2365           cpi->lst_fb_idx = 1;
2366           cpi->gld_fb_idx = 4;
2367           cpi->alt_fb_idx = 6;
2368         } else if (svc->spatial_layer_id == 2) {
2369           cpi->lst_fb_idx = 2;
2370           cpi->gld_fb_idx = 5;
2371           cpi->alt_fb_idx = 7;
2372         }
2373       }
2374       cpi->ext_refresh_last_frame = 1;
2375       cpi->ext_refresh_golden_frame = 1;
2376       cpi->ext_refresh_alt_ref_frame = 1;
2377     }
2378   }
2379 
2380   // Check if superframe contains a sync layer request.
2381   vp9_svc_check_spatial_layer_sync(cpi);
2382 
2383   // If long term termporal feature is enabled, set the period of the update.
2384   // The update/refresh of this reference frame is always on base temporal
2385   // layer frame.
2386   if (svc->use_gf_temporal_ref_current_layer) {
2387     // Only use gf long-term prediction on non-key superframes.
2388     if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
2389       // Use golden for this reference, which will be used for prediction.
2390       int index = svc->spatial_layer_id;
2391       if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
2392       assert(index >= 0);
2393       cpi->gld_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
2394       // Enable prediction off LAST (last reference) and golden (which will
2395       // generally be further behind/long-term reference).
2396       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
2397     }
2398     // Check for update/refresh of reference: only refresh on base temporal
2399     // layer.
2400     if (svc->temporal_layer_id == 0) {
2401       if (svc->layer_context[svc->temporal_layer_id].is_key_frame) {
2402         // On key frame we update the buffer index used for long term reference.
2403         // Use the alt_ref since it is not used or updated on key frames.
2404         int index = svc->spatial_layer_id;
2405         if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
2406         assert(index >= 0);
2407         cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
2408         cpi->ext_refresh_alt_ref_frame = 1;
2409       } else if (rc->frames_till_gf_update_due == 0) {
2410         // Set perdiod of next update. Make it a multiple of 10, as the cyclic
2411         // refresh is typically ~10%, and we'd like the update to happen after
2412         // a few cylces of the refresh (so it better quality frame). Note the
2413         // cyclic refresh for SVC only operates on base temporal layer frames.
2414         // Choose 20 as perdiod for now (2 cycles).
2415         rc->baseline_gf_interval = 20;
2416         rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2417         cpi->ext_refresh_golden_frame = 1;
2418         rc->gfu_boost = DEFAULT_GF_BOOST;
2419       }
2420     }
2421   } else if (!svc->use_gf_temporal_ref) {
2422     rc->frames_till_gf_update_due = INT_MAX;
2423     rc->baseline_gf_interval = INT_MAX;
2424   }
2425   if (svc->set_intra_only_frame) {
2426     set_intra_only_frame(cpi);
2427     if (cpi->oxcf.rc_mode == VPX_CBR)
2428       target = vp9_calc_iframe_target_size_one_pass_cbr(cpi);
2429     else
2430       target = vp9_calc_iframe_target_size_one_pass_vbr(cpi);
2431   }
2432   // Overlay frame predicts from LAST (intra-only)
2433   if (svc->previous_frame_is_intra_only) cpi->ref_frame_flags |= VP9_LAST_FLAG;
2434 
2435   // Any update/change of global cyclic refresh parameters (amount/delta-qp)
2436   // should be done here, before the frame qp is selected.
2437   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2438     vp9_cyclic_refresh_update_parameters(cpi);
2439 
2440   vp9_rc_set_frame_target(cpi, target);
2441   if (cm->show_frame) vp9_update_buffer_level_svc_preencode(cpi);
2442 
2443   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && svc->single_layer_svc == 1 &&
2444       svc->spatial_layer_id == svc->first_spatial_layer_to_encode &&
2445       svc->temporal_layer_id == 0) {
2446     LAYER_CONTEXT *lc = NULL;
2447     cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
2448     if (cpi->resize_pending) {
2449       int tl, width, height;
2450       // Apply the same scale to all temporal layers.
2451       for (tl = 0; tl < svc->number_temporal_layers; tl++) {
2452         lc = &svc->layer_context[svc->spatial_layer_id *
2453                                      svc->number_temporal_layers +
2454                                  tl];
2455         lc->scaling_factor_num_resize =
2456             cpi->resize_scale_num * lc->scaling_factor_num;
2457         lc->scaling_factor_den_resize =
2458             cpi->resize_scale_den * lc->scaling_factor_den;
2459         // Reset rate control for all temporal layers.
2460         lc->rc.buffer_level = lc->rc.optimal_buffer_level;
2461         lc->rc.bits_off_target = lc->rc.optimal_buffer_level;
2462         lc->rc.rate_correction_factors[INTER_FRAME] =
2463             rc->rate_correction_factors[INTER_FRAME];
2464       }
2465       // Set the size for this current temporal layer.
2466       lc = &svc->layer_context[svc->spatial_layer_id *
2467                                    svc->number_temporal_layers +
2468                                svc->temporal_layer_id];
2469       get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
2470                            lc->scaling_factor_num_resize,
2471                            lc->scaling_factor_den_resize, &width, &height);
2472       vp9_set_size_literal(cpi, width, height);
2473       svc->resize_set = 1;
2474     }
2475   } else {
2476     cpi->resize_pending = 0;
2477     svc->resize_set = 0;
2478   }
2479 }
2480 
vp9_rc_get_one_pass_cbr_params(VP9_COMP * cpi)2481 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
2482   VP9_COMMON *const cm = &cpi->common;
2483   RATE_CONTROL *const rc = &cpi->rc;
2484   int target;
2485   if ((cm->current_video_frame == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
2486       (cpi->oxcf.auto_key && rc->frames_to_key == 0) ||
2487       (cpi->oxcf.mode != cpi->deadline_mode_previous_frame)) {
2488     cm->frame_type = KEY_FRAME;
2489     rc->frames_to_key = cpi->oxcf.key_freq;
2490     rc->kf_boost = DEFAULT_KF_BOOST;
2491     rc->source_alt_ref_active = 0;
2492   } else {
2493     cm->frame_type = INTER_FRAME;
2494   }
2495   if (rc->frames_till_gf_update_due == 0) {
2496     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2497       vp9_cyclic_refresh_set_golden_update(cpi);
2498     else
2499       rc->baseline_gf_interval =
2500           (rc->min_gf_interval + rc->max_gf_interval) / 2;
2501     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2502     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
2503     if (rc->frames_till_gf_update_due > rc->frames_to_key)
2504       rc->frames_till_gf_update_due = rc->frames_to_key;
2505     cpi->refresh_golden_frame = 1;
2506     rc->gfu_boost = DEFAULT_GF_BOOST;
2507   }
2508 
2509   // Any update/change of global cyclic refresh parameters (amount/delta-qp)
2510   // should be done here, before the frame qp is selected.
2511   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2512     vp9_cyclic_refresh_update_parameters(cpi);
2513 
2514   if (frame_is_intra_only(cm))
2515     target = vp9_calc_iframe_target_size_one_pass_cbr(cpi);
2516   else
2517     target = vp9_calc_pframe_target_size_one_pass_cbr(cpi);
2518 
2519   vp9_rc_set_frame_target(cpi, target);
2520 
2521   if (cm->show_frame) vp9_update_buffer_level_preencode(cpi);
2522 
2523   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC)
2524     cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
2525   else
2526     cpi->resize_pending = 0;
2527 }
2528 
vp9_compute_qdelta(const RATE_CONTROL * rc,double qstart,double qtarget,vpx_bit_depth_t bit_depth)2529 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
2530                        vpx_bit_depth_t bit_depth) {
2531   int start_index = rc->worst_quality;
2532   int target_index = rc->worst_quality;
2533   int i;
2534 
2535   // Convert the average q value to an index.
2536   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
2537     start_index = i;
2538     if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart) break;
2539   }
2540 
2541   // Convert the q target to an index
2542   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
2543     target_index = i;
2544     if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
2545   }
2546 
2547   return target_index - start_index;
2548 }
2549 
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)2550 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
2551                                int qindex, double rate_target_ratio,
2552                                vpx_bit_depth_t bit_depth) {
2553   int target_index = rc->worst_quality;
2554   int i;
2555 
2556   // Look up the current projected bits per block for the base index
2557   const int base_bits_per_mb =
2558       vp9_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
2559 
2560   // Find the target bits per mb based on the base value and given ratio.
2561   const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
2562 
2563   // Convert the q target to an index
2564   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
2565     if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
2566         target_bits_per_mb) {
2567       target_index = i;
2568       break;
2569     }
2570   }
2571   return target_index - qindex;
2572 }
2573 
vp9_rc_set_gf_interval_range(const VP9_COMP * const cpi,RATE_CONTROL * const rc)2574 void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi,
2575                                   RATE_CONTROL *const rc) {
2576   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2577 
2578   // Special case code for 1 pass fixed Q mode tests
2579   if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
2580     rc->max_gf_interval = FIXED_GF_INTERVAL;
2581     rc->min_gf_interval = FIXED_GF_INTERVAL;
2582     rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
2583   } else {
2584     double framerate = cpi->framerate;
2585     // Set Maximum gf/arf interval
2586     rc->max_gf_interval = oxcf->max_gf_interval;
2587     rc->min_gf_interval = oxcf->min_gf_interval;
2588 #if CONFIG_RATE_CTRL
2589     if (oxcf->use_simple_encode_api) {
2590       // In this experiment, we avoid framerate being changed dynamically during
2591       // encoding.
2592       framerate = oxcf->init_framerate;
2593     }
2594 #endif  // CONFIG_RATE_CTRL
2595     if (rc->min_gf_interval == 0) {
2596       rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
2597           oxcf->width, oxcf->height, framerate);
2598     }
2599     if (rc->max_gf_interval == 0) {
2600       rc->max_gf_interval =
2601           vp9_rc_get_default_max_gf_interval(framerate, rc->min_gf_interval);
2602     }
2603 
2604     // Extended max interval for genuinely static scenes like slide shows.
2605     rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH;
2606 
2607     if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
2608       rc->max_gf_interval = rc->static_scene_max_gf_interval;
2609 
2610     // Clamp min to max
2611     rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
2612 
2613     if (oxcf->target_level == LEVEL_AUTO) {
2614       const uint32_t pic_size = cpi->common.width * cpi->common.height;
2615       const uint32_t pic_breadth =
2616           VPXMAX(cpi->common.width, cpi->common.height);
2617       int i;
2618       for (i = 0; i < VP9_LEVELS; ++i) {
2619         if (vp9_level_defs[i].max_luma_picture_size >= pic_size &&
2620             vp9_level_defs[i].max_luma_picture_breadth >= pic_breadth) {
2621           if (rc->min_gf_interval <=
2622               (int)vp9_level_defs[i].min_altref_distance) {
2623             rc->min_gf_interval = (int)vp9_level_defs[i].min_altref_distance;
2624             rc->max_gf_interval =
2625                 VPXMAX(rc->max_gf_interval, rc->min_gf_interval);
2626           }
2627           break;
2628         }
2629       }
2630     }
2631   }
2632 }
2633 
vp9_rc_update_framerate(VP9_COMP * cpi)2634 void vp9_rc_update_framerate(VP9_COMP *cpi) {
2635   const VP9_COMMON *const cm = &cpi->common;
2636   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2637   RATE_CONTROL *const rc = &cpi->rc;
2638 
2639   rc->avg_frame_bandwidth = saturate_cast_double_to_int(
2640       round(oxcf->target_bandwidth / cpi->framerate));
2641 
2642   int64_t vbr_min_bits =
2643       (int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100;
2644   vbr_min_bits = VPXMIN(vbr_min_bits, INT_MAX);
2645 
2646   rc->min_frame_bandwidth = VPXMAX((int)vbr_min_bits, FRAME_OVERHEAD_BITS);
2647 
2648   // A maximum bitrate for a frame is defined.
2649   // However this limit is extended if a very high rate is given on the command
2650   // line or the rate can not be achieved because of a user specified max q
2651   // (e.g. when the user specifies lossless encode).
2652   //
2653   // If a level is specified that requires a lower maximum rate then the level
2654   // value take precedence.
2655   int64_t vbr_max_bits =
2656       (int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section / 100;
2657   vbr_max_bits = VPXMIN(vbr_max_bits, INT_MAX);
2658 
2659   rc->max_frame_bandwidth =
2660       VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), (int)vbr_max_bits);
2661 
2662   vp9_rc_set_gf_interval_range(cpi, rc);
2663 }
2664 
2665 #define VBR_PCT_ADJUSTMENT_LIMIT 50
2666 // For VBR...adjustment to the frame target based on error from previous frames
vbr_rate_correction(VP9_COMP * cpi,int * this_frame_target)2667 static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
2668   RATE_CONTROL *const rc = &cpi->rc;
2669   int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
2670   int64_t frame_target = *this_frame_target;
2671   int frame_window = (int)VPXMIN(
2672       16, cpi->twopass.total_stats.count - cpi->common.current_video_frame);
2673 
2674   // Calcluate the adjustment to rate for this frame.
2675   if (frame_window > 0) {
2676     int64_t max_delta = (vbr_bits_off_target > 0)
2677                             ? (vbr_bits_off_target / frame_window)
2678                             : (-vbr_bits_off_target / frame_window);
2679 
2680     max_delta =
2681         VPXMIN(max_delta, ((frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
2682 
2683     // vbr_bits_off_target > 0 means we have extra bits to spend
2684     if (vbr_bits_off_target > 0) {
2685       frame_target += VPXMIN(vbr_bits_off_target, max_delta);
2686     } else {
2687       frame_target -= VPXMIN(-vbr_bits_off_target, max_delta);
2688     }
2689   }
2690 
2691   // Fast redistribution of bits arising from massive local undershoot.
2692   // Don't do it for kf,arf,gf or overlay frames.
2693   if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
2694       rc->vbr_bits_off_target_fast) {
2695     int64_t one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, frame_target);
2696     int64_t fast_extra_bits =
2697         VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
2698     fast_extra_bits =
2699         VPXMIN(fast_extra_bits,
2700                VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
2701     frame_target += fast_extra_bits;
2702     rc->vbr_bits_off_target_fast -= fast_extra_bits;
2703   }
2704 
2705   // Clamp the target for the frame to the maximum allowed for one frame.
2706   *this_frame_target = (int)VPXMIN(frame_target, INT_MAX);
2707 }
2708 
vp9_set_target_rate(VP9_COMP * cpi)2709 void vp9_set_target_rate(VP9_COMP *cpi) {
2710   RATE_CONTROL *const rc = &cpi->rc;
2711   int target_rate = rc->base_frame_target;
2712 
2713   if (cpi->common.frame_type == KEY_FRAME)
2714     target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
2715   else
2716     target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2717 
2718   if (!cpi->oxcf.vbr_corpus_complexity) {
2719     // Correction to rate target based on prior over or under shoot.
2720     if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
2721       vbr_rate_correction(cpi, &target_rate);
2722   }
2723   vp9_rc_set_frame_target(cpi, target_rate);
2724 }
2725 
2726 // Check if we should resize, based on average QP from past x frames.
2727 // Only allow for resize at most one scale down for now, scaling factor is 2.
vp9_resize_one_pass_cbr(VP9_COMP * cpi)2728 int vp9_resize_one_pass_cbr(VP9_COMP *cpi) {
2729   const VP9_COMMON *const cm = &cpi->common;
2730   RATE_CONTROL *const rc = &cpi->rc;
2731   RESIZE_ACTION resize_action = NO_RESIZE;
2732   int avg_qp_thr1 = 70;
2733   int avg_qp_thr2 = 50;
2734   // Don't allow for resized frame to go below 320x180, resize in steps of 3/4.
2735   int min_width = (320 * 4) / 3;
2736   int min_height = (180 * 4) / 3;
2737   int down_size_on = 1;
2738   int force_downsize_rate = 0;
2739   cpi->resize_scale_num = 1;
2740   cpi->resize_scale_den = 1;
2741   // Don't resize on key frame; reset the counters on key frame.
2742   if (cm->frame_type == KEY_FRAME) {
2743     cpi->resize_avg_qp = 0;
2744     cpi->resize_count = 0;
2745     return 0;
2746   }
2747 
2748   // No resizing down if frame size is below some limit.
2749   if ((cm->width * cm->height) < min_width * min_height) down_size_on = 0;
2750 
2751 #if CONFIG_VP9_TEMPORAL_DENOISING
2752   // If denoiser is on, apply a smaller qp threshold.
2753   if (cpi->oxcf.noise_sensitivity > 0) {
2754     avg_qp_thr1 = 60;
2755     avg_qp_thr2 = 40;
2756   }
2757 #endif
2758 
2759   // Force downsize based on per-frame-bandwidth, for extreme case,
2760   // for HD input.
2761   if (cpi->resize_state == ORIG && cm->width * cm->height >= 1280 * 720) {
2762     if (rc->avg_frame_bandwidth < 300000 / 30) {
2763       resize_action = DOWN_ONEHALF;
2764       cpi->resize_state = ONE_HALF;
2765       force_downsize_rate = 1;
2766     } else if (rc->avg_frame_bandwidth < 400000 / 30) {
2767       resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
2768       cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
2769       force_downsize_rate = 1;
2770     }
2771   } else if (cpi->resize_state == THREE_QUARTER &&
2772              cm->width * cm->height >= 960 * 540) {
2773     if (rc->avg_frame_bandwidth < 300000 / 30) {
2774       resize_action = DOWN_ONEHALF;
2775       cpi->resize_state = ONE_HALF;
2776       force_downsize_rate = 1;
2777     }
2778   }
2779 
2780   // Resize based on average buffer underflow and QP over some window.
2781   // Ignore samples close to key frame, since QP is usually high after key.
2782   if (!force_downsize_rate && cpi->rc.frames_since_key > cpi->framerate) {
2783     const int window = VPXMIN(30, (int)round(2 * cpi->framerate));
2784     cpi->resize_avg_qp += rc->last_q[INTER_FRAME];
2785     if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100))
2786       ++cpi->resize_buffer_underflow;
2787     ++cpi->resize_count;
2788     // Check for resize action every "window" frames.
2789     if (cpi->resize_count >= window) {
2790       int avg_qp = cpi->resize_avg_qp / cpi->resize_count;
2791       // Resize down if buffer level has underflowed sufficient amount in past
2792       // window, and we are at original or 3/4 of original resolution.
2793       // Resize back up if average QP is low, and we are currently in a resized
2794       // down state, i.e. 1/2 or 3/4 of original resolution.
2795       // Currently, use a flag to turn 3/4 resizing feature on/off.
2796       if (cpi->resize_buffer_underflow > (cpi->resize_count >> 2) &&
2797           down_size_on) {
2798         if (cpi->resize_state == THREE_QUARTER) {
2799           resize_action = DOWN_ONEHALF;
2800           cpi->resize_state = ONE_HALF;
2801         } else if (cpi->resize_state == ORIG) {
2802           resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
2803           cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
2804         }
2805       } else if (cpi->resize_state != ORIG &&
2806                  avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
2807         if (cpi->resize_state == THREE_QUARTER ||
2808             avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100 ||
2809             ONEHALFONLY_RESIZE) {
2810           resize_action = UP_ORIG;
2811           cpi->resize_state = ORIG;
2812         } else if (cpi->resize_state == ONE_HALF) {
2813           resize_action = UP_THREEFOUR;
2814           cpi->resize_state = THREE_QUARTER;
2815         }
2816       }
2817       // Reset for next window measurement.
2818       cpi->resize_avg_qp = 0;
2819       cpi->resize_count = 0;
2820       cpi->resize_buffer_underflow = 0;
2821     }
2822   }
2823   // If decision is to resize, reset some quantities, and check is we should
2824   // reduce rate correction factor,
2825   if (resize_action != NO_RESIZE) {
2826     int target_bits_per_frame;
2827     int active_worst_quality;
2828     int qindex;
2829     int tot_scale_change;
2830     if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
2831       cpi->resize_scale_num = 3;
2832       cpi->resize_scale_den = 4;
2833     } else if (resize_action == DOWN_ONEHALF) {
2834       cpi->resize_scale_num = 1;
2835       cpi->resize_scale_den = 2;
2836     } else {  // UP_ORIG or anything else
2837       cpi->resize_scale_num = 1;
2838       cpi->resize_scale_den = 1;
2839     }
2840     tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) /
2841                        (cpi->resize_scale_num * cpi->resize_scale_num);
2842     // Reset buffer level to optimal, update target size.
2843     rc->buffer_level = rc->optimal_buffer_level;
2844     rc->bits_off_target = rc->optimal_buffer_level;
2845     rc->this_frame_target = vp9_calc_pframe_target_size_one_pass_cbr(cpi);
2846     // Get the projected qindex, based on the scaled target frame size (scaled
2847     // so target_bits_per_mb in vp9_rc_regulate_q will be correct target).
2848     target_bits_per_frame = (resize_action >= 0)
2849                                 ? rc->this_frame_target * tot_scale_change
2850                                 : rc->this_frame_target / tot_scale_change;
2851     active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
2852     qindex = vp9_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality,
2853                                active_worst_quality);
2854     // If resize is down, check if projected q index is close to worst_quality,
2855     // and if so, reduce the rate correction factor (since likely can afford
2856     // lower q for resized frame).
2857     if (resize_action > 0 && qindex > 90 * cpi->rc.worst_quality / 100) {
2858       rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
2859     }
2860     // If resize is back up, check if projected q index is too much above the
2861     // current base_qindex, and if so, reduce the rate correction factor
2862     // (since prefer to keep q for resized frame at least close to previous q).
2863     if (resize_action < 0 && qindex > 130 * cm->base_qindex / 100) {
2864       rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
2865     }
2866   }
2867   return resize_action;
2868 }
2869 
adjust_gf_boost_lag_one_pass_vbr(VP9_COMP * cpi,uint64_t avg_sad_current)2870 static void adjust_gf_boost_lag_one_pass_vbr(VP9_COMP *cpi,
2871                                              uint64_t avg_sad_current) {
2872   VP9_COMMON *const cm = &cpi->common;
2873   RATE_CONTROL *const rc = &cpi->rc;
2874   int target;
2875   int found = 0;
2876   int found2 = 0;
2877   int frame;
2878   int i;
2879   uint64_t avg_source_sad_lag = avg_sad_current;
2880   int high_source_sad_lagindex = -1;
2881   int steady_sad_lagindex = -1;
2882   uint32_t sad_thresh1 = 70000;
2883   uint32_t sad_thresh2 = 120000;
2884   int low_content = 0;
2885   int high_content = 0;
2886   double rate_err = 1.0;
2887   // Get measure of complexity over the future frames, and get the first
2888   // future frame with high_source_sad/scene-change.
2889   int tot_frames = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
2890   for (frame = tot_frames; frame >= 1; --frame) {
2891     const int lagframe_idx = tot_frames - frame + 1;
2892     uint64_t reference_sad = rc->avg_source_sad[0];
2893     for (i = 1; i < lagframe_idx; ++i) {
2894       if (rc->avg_source_sad[i] > 0)
2895         reference_sad = (3 * reference_sad + rc->avg_source_sad[i]) >> 2;
2896     }
2897     // Detect up-coming scene change.
2898     if (!found &&
2899         (rc->avg_source_sad[lagframe_idx] >
2900              VPXMAX(sad_thresh1, (unsigned int)(reference_sad << 1)) ||
2901          rc->avg_source_sad[lagframe_idx] >
2902              VPXMAX(3 * sad_thresh1 >> 2,
2903                     (unsigned int)(reference_sad << 2)))) {
2904       high_source_sad_lagindex = lagframe_idx;
2905       found = 1;
2906     }
2907     // Detect change from motion to steady.
2908     if (!found2 && lagframe_idx > 1 && lagframe_idx < tot_frames &&
2909         rc->avg_source_sad[lagframe_idx - 1] > (sad_thresh1 >> 2)) {
2910       found2 = 1;
2911       for (i = lagframe_idx; i < tot_frames; ++i) {
2912         if (!(rc->avg_source_sad[i] > 0 &&
2913               rc->avg_source_sad[i] < (sad_thresh1 >> 2) &&
2914               rc->avg_source_sad[i] <
2915                   (rc->avg_source_sad[lagframe_idx - 1] >> 1))) {
2916           found2 = 0;
2917           i = tot_frames;
2918         }
2919       }
2920       if (found2) steady_sad_lagindex = lagframe_idx;
2921     }
2922     avg_source_sad_lag += rc->avg_source_sad[lagframe_idx];
2923   }
2924   if (tot_frames > 0) avg_source_sad_lag = avg_source_sad_lag / tot_frames;
2925   // Constrain distance between detected scene cuts.
2926   if (high_source_sad_lagindex != -1 &&
2927       high_source_sad_lagindex != rc->high_source_sad_lagindex - 1 &&
2928       abs(high_source_sad_lagindex - rc->high_source_sad_lagindex) < 4)
2929     rc->high_source_sad_lagindex = -1;
2930   else
2931     rc->high_source_sad_lagindex = high_source_sad_lagindex;
2932   // Adjust some factors for the next GF group, ignore initial key frame,
2933   // and only for lag_in_frames not too small.
2934   if (cpi->refresh_golden_frame == 1 && cm->current_video_frame > 30 &&
2935       cpi->oxcf.lag_in_frames > 8) {
2936     int frame_constraint;
2937     if (rc->rolling_target_bits > 0)
2938       rate_err =
2939           (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
2940     high_content = high_source_sad_lagindex != -1 ||
2941                    avg_source_sad_lag > (rc->prev_avg_source_sad_lag << 1) ||
2942                    avg_source_sad_lag > sad_thresh2;
2943     low_content = high_source_sad_lagindex == -1 &&
2944                   ((avg_source_sad_lag < (rc->prev_avg_source_sad_lag >> 1)) ||
2945                    (avg_source_sad_lag < sad_thresh1));
2946     if (low_content) {
2947       rc->gfu_boost = DEFAULT_GF_BOOST;
2948       rc->baseline_gf_interval =
2949           VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
2950     } else if (high_content) {
2951       rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
2952       rc->baseline_gf_interval = (rate_err > 3.0)
2953                                      ? VPXMAX(10, rc->baseline_gf_interval >> 1)
2954                                      : VPXMAX(6, rc->baseline_gf_interval >> 1);
2955     }
2956     if (rc->baseline_gf_interval > cpi->oxcf.lag_in_frames - 1)
2957       rc->baseline_gf_interval = cpi->oxcf.lag_in_frames - 1;
2958     // Check for constraining gf_interval for up-coming scene/content changes,
2959     // or for up-coming key frame, whichever is closer.
2960     frame_constraint = rc->frames_to_key;
2961     if (rc->high_source_sad_lagindex > 0 &&
2962         frame_constraint > rc->high_source_sad_lagindex)
2963       frame_constraint = rc->high_source_sad_lagindex;
2964     if (steady_sad_lagindex > 3 && frame_constraint > steady_sad_lagindex)
2965       frame_constraint = steady_sad_lagindex;
2966     adjust_gfint_frame_constraint(cpi, frame_constraint);
2967     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2968     // Adjust factors for active_worst setting & af_ratio for next gf interval.
2969     rc->fac_active_worst_inter = 150;  // corresponds to 3/2 (= 150 /100).
2970     rc->fac_active_worst_gf = 100;
2971     if (rate_err < 2.0 && !high_content) {
2972       rc->fac_active_worst_inter = 120;
2973       rc->fac_active_worst_gf = 90;
2974     } else if (rate_err > 8.0 && rc->avg_frame_qindex[INTER_FRAME] < 16) {
2975       // Increase active_worst faster at low Q if rate fluctuation is high.
2976       rc->fac_active_worst_inter = 200;
2977       if (rc->avg_frame_qindex[INTER_FRAME] < 8)
2978         rc->fac_active_worst_inter = 400;
2979     }
2980     if (low_content && rc->avg_frame_low_motion > 80) {
2981       rc->af_ratio_onepass_vbr = 15;
2982     } else if (high_content || rc->avg_frame_low_motion < 30) {
2983       rc->af_ratio_onepass_vbr = 5;
2984       rc->gfu_boost = DEFAULT_GF_BOOST >> 2;
2985     }
2986     if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
2987       // Flag to disable usage of ARF based on past usage, only allow this
2988       // disabling if current frame/group does not start with key frame or
2989       // scene cut. Note perc_arf_usage is only computed for speed >= 5.
2990       int arf_usage_low =
2991           (cm->frame_type != KEY_FRAME && !rc->high_source_sad &&
2992            cpi->rc.perc_arf_usage < 15 && cpi->oxcf.speed >= 5);
2993       // Don't use alt-ref for this group under certain conditions.
2994       if (arf_usage_low ||
2995           (rc->high_source_sad_lagindex > 0 &&
2996            rc->high_source_sad_lagindex <= rc->frames_till_gf_update_due) ||
2997           (avg_source_sad_lag > 3 * sad_thresh1 >> 3)) {
2998         rc->source_alt_ref_pending = 0;
2999         rc->alt_ref_gf_group = 0;
3000       } else {
3001         rc->source_alt_ref_pending = 1;
3002         rc->alt_ref_gf_group = 1;
3003         // If alt-ref is used for this gf group, limit the interval.
3004         if (rc->baseline_gf_interval > 12) {
3005           rc->baseline_gf_interval = 12;
3006           rc->frames_till_gf_update_due = rc->baseline_gf_interval;
3007         }
3008       }
3009     }
3010     target = vp9_calc_pframe_target_size_one_pass_vbr(cpi);
3011     vp9_rc_set_frame_target(cpi, target);
3012   }
3013   rc->prev_avg_source_sad_lag = avg_source_sad_lag;
3014 }
3015 
3016 // Compute average source sad (temporal sad: between current source and
3017 // previous source) over a subset of superblocks. Use this is detect big changes
3018 // in content and allow rate control to react.
3019 // This function also handles special case of lag_in_frames, to measure content
3020 // level in #future frames set by the lag_in_frames.
vp9_scene_detection_onepass(VP9_COMP * cpi)3021 void vp9_scene_detection_onepass(VP9_COMP *cpi) {
3022   VP9_COMMON *const cm = &cpi->common;
3023   RATE_CONTROL *const rc = &cpi->rc;
3024   YV12_BUFFER_CONFIG const *unscaled_src = cpi->un_scaled_source;
3025   YV12_BUFFER_CONFIG const *unscaled_last_src = cpi->unscaled_last_source;
3026   uint8_t *src_y;
3027   int src_ystride;
3028   int src_width;
3029   int src_height;
3030   uint8_t *last_src_y;
3031   int last_src_ystride;
3032   int last_src_width;
3033   int last_src_height;
3034   if (cpi->un_scaled_source == NULL || cpi->unscaled_last_source == NULL ||
3035       (cpi->use_svc && cpi->svc.current_superframe == 0))
3036     return;
3037   src_y = unscaled_src->y_buffer;
3038   src_ystride = unscaled_src->y_stride;
3039   src_width = unscaled_src->y_width;
3040   src_height = unscaled_src->y_height;
3041   last_src_y = unscaled_last_src->y_buffer;
3042   last_src_ystride = unscaled_last_src->y_stride;
3043   last_src_width = unscaled_last_src->y_width;
3044   last_src_height = unscaled_last_src->y_height;
3045 #if CONFIG_VP9_HIGHBITDEPTH
3046   if (cm->use_highbitdepth) return;
3047 #endif
3048   rc->high_source_sad = 0;
3049   rc->high_num_blocks_with_motion = 0;
3050   // For SVC: scene detection is only checked on first spatial layer of
3051   // the superframe using the original/unscaled resolutions.
3052   if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode &&
3053       src_width == last_src_width && src_height == last_src_height) {
3054     YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = { NULL };
3055     int num_mi_cols = cm->mi_cols;
3056     int num_mi_rows = cm->mi_rows;
3057     int start_frame = 0;
3058     int frames_to_buffer = 1;
3059     int frame = 0;
3060     int scene_cut_force_key_frame = 0;
3061     int num_zero_temp_sad = 0;
3062     uint64_t avg_sad_current = 0;
3063     uint32_t min_thresh = 20000;  // ~5 * 64 * 64
3064     float thresh = 8.0f;
3065     uint32_t thresh_key = 140000;
3066     if (cpi->oxcf.speed <= 5) thresh_key = 240000;
3067     if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) min_thresh = 65000;
3068     if (cpi->oxcf.rc_mode == VPX_VBR) thresh = 2.1f;
3069     if (cpi->use_svc && cpi->svc.number_spatial_layers > 1) {
3070       const int aligned_width = ALIGN_POWER_OF_TWO(src_width, MI_SIZE_LOG2);
3071       const int aligned_height = ALIGN_POWER_OF_TWO(src_height, MI_SIZE_LOG2);
3072       num_mi_cols = aligned_width >> MI_SIZE_LOG2;
3073       num_mi_rows = aligned_height >> MI_SIZE_LOG2;
3074     }
3075     if (cpi->oxcf.lag_in_frames > 0) {
3076       frames_to_buffer = (cm->current_video_frame == 1)
3077                              ? (int)vp9_lookahead_depth(cpi->lookahead) - 1
3078                              : 2;
3079       start_frame = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
3080       for (frame = 0; frame < frames_to_buffer; ++frame) {
3081         const int lagframe_idx = start_frame - frame;
3082         if (lagframe_idx >= 0) {
3083           struct lookahead_entry *buf =
3084               vp9_lookahead_peek(cpi->lookahead, lagframe_idx);
3085           frames[frame] = &buf->img;
3086         }
3087       }
3088       // The avg_sad for this current frame is the value of frame#1
3089       // (first future frame) from previous frame.
3090       avg_sad_current = rc->avg_source_sad[1];
3091       if (avg_sad_current >
3092               VPXMAX(min_thresh,
3093                      (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
3094           cm->current_video_frame > (unsigned int)cpi->oxcf.lag_in_frames)
3095         rc->high_source_sad = 1;
3096       else
3097         rc->high_source_sad = 0;
3098       if (rc->high_source_sad && avg_sad_current > thresh_key)
3099         scene_cut_force_key_frame = 1;
3100       // Update recursive average for current frame.
3101       if (avg_sad_current > 0)
3102         rc->avg_source_sad[0] =
3103             (3 * rc->avg_source_sad[0] + avg_sad_current) >> 2;
3104       // Shift back data, starting at frame#1.
3105       for (frame = 1; frame < cpi->oxcf.lag_in_frames - 1; ++frame)
3106         rc->avg_source_sad[frame] = rc->avg_source_sad[frame + 1];
3107     }
3108     for (frame = 0; frame < frames_to_buffer; ++frame) {
3109       if (cpi->oxcf.lag_in_frames == 0 ||
3110           (frames[frame] != NULL && frames[frame + 1] != NULL &&
3111            frames[frame]->y_width == frames[frame + 1]->y_width &&
3112            frames[frame]->y_height == frames[frame + 1]->y_height)) {
3113         int sbi_row, sbi_col;
3114         const int lagframe_idx =
3115             (cpi->oxcf.lag_in_frames == 0) ? 0 : start_frame - frame + 1;
3116         const BLOCK_SIZE bsize = BLOCK_64X64;
3117         // Loop over sub-sample of frame, compute average sad over 64x64 blocks.
3118         uint64_t avg_sad = 0;
3119         uint64_t tmp_sad = 0;
3120         int num_samples = 0;
3121         int sb_cols = (num_mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
3122         int sb_rows = (num_mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
3123         if (cpi->oxcf.lag_in_frames > 0) {
3124           src_y = frames[frame]->y_buffer;
3125           src_ystride = frames[frame]->y_stride;
3126           last_src_y = frames[frame + 1]->y_buffer;
3127           last_src_ystride = frames[frame + 1]->y_stride;
3128         }
3129         num_zero_temp_sad = 0;
3130         for (sbi_row = 0; sbi_row < sb_rows; ++sbi_row) {
3131           for (sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
3132             // Checker-board pattern, ignore boundary.
3133             if (((sbi_row > 0 && sbi_col > 0) &&
3134                  (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) &&
3135                  ((sbi_row % 2 == 0 && sbi_col % 2 == 0) ||
3136                   (sbi_row % 2 != 0 && sbi_col % 2 != 0)))) {
3137               tmp_sad = cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y,
3138                                                last_src_ystride);
3139               avg_sad += tmp_sad;
3140               num_samples++;
3141               if (tmp_sad == 0) num_zero_temp_sad++;
3142             }
3143             src_y += 64;
3144             last_src_y += 64;
3145           }
3146           src_y += (src_ystride << 6) - (sb_cols << 6);
3147           last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
3148         }
3149         if (num_samples > 0) avg_sad = avg_sad / num_samples;
3150         // Set high_source_sad flag if we detect very high increase in avg_sad
3151         // between current and previous frame value(s). Use minimum threshold
3152         // for cases where there is small change from content that is completely
3153         // static.
3154         if (lagframe_idx == 0) {
3155           if (avg_sad >
3156                   VPXMAX(min_thresh,
3157                          (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
3158               rc->frames_since_key > 1 + cpi->svc.number_spatial_layers &&
3159               num_zero_temp_sad < 3 * (num_samples >> 2))
3160             rc->high_source_sad = 1;
3161           else
3162             rc->high_source_sad = 0;
3163           if (rc->high_source_sad && avg_sad > thresh_key)
3164             scene_cut_force_key_frame = 1;
3165           if (avg_sad > 0 || cpi->oxcf.rc_mode == VPX_CBR)
3166             rc->avg_source_sad[0] = (3 * rc->avg_source_sad[0] + avg_sad) >> 2;
3167         } else {
3168           rc->avg_source_sad[lagframe_idx] = avg_sad;
3169         }
3170         if (num_zero_temp_sad < (3 * num_samples >> 2))
3171           rc->high_num_blocks_with_motion = 1;
3172       }
3173     }
3174     // For CBR non-screen content mode, check if we should reset the rate
3175     // control. Reset is done if high_source_sad is detected and the rate
3176     // control is at very low QP with rate correction factor at min level.
3177     if (cpi->oxcf.rc_mode == VPX_CBR &&
3178         cpi->oxcf.content != VP9E_CONTENT_SCREEN && !cpi->use_svc) {
3179       if (rc->high_source_sad && rc->last_q[INTER_FRAME] == rc->best_quality &&
3180           rc->avg_frame_qindex[INTER_FRAME] < (rc->best_quality << 1) &&
3181           rc->rate_correction_factors[INTER_NORMAL] == MIN_BPB_FACTOR) {
3182         rc->rate_correction_factors[INTER_NORMAL] = 0.5;
3183         rc->avg_frame_qindex[INTER_FRAME] = rc->worst_quality;
3184         rc->buffer_level = rc->optimal_buffer_level;
3185         rc->bits_off_target = rc->optimal_buffer_level;
3186         rc->reset_high_source_sad = 1;
3187       }
3188       if (cm->frame_type != KEY_FRAME && rc->reset_high_source_sad)
3189         rc->this_frame_target = rc->avg_frame_bandwidth;
3190     }
3191     // For SVC the new (updated) avg_source_sad[0] for the current superframe
3192     // updates the setting for all layers.
3193     if (cpi->use_svc) {
3194       int sl, tl;
3195       SVC *const svc = &cpi->svc;
3196       for (sl = 0; sl < svc->number_spatial_layers; ++sl)
3197         for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
3198           int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3199           LAYER_CONTEXT *const lc = &svc->layer_context[layer];
3200           RATE_CONTROL *const lrc = &lc->rc;
3201           lrc->avg_source_sad[0] = rc->avg_source_sad[0];
3202         }
3203     }
3204     // For VBR, under scene change/high content change, force golden refresh.
3205     if (cpi->oxcf.rc_mode == VPX_VBR && cm->frame_type != KEY_FRAME &&
3206         rc->high_source_sad && rc->frames_to_key > 3 &&
3207         rc->count_last_scene_change > 4 &&
3208         cpi->ext_refresh_frame_flags_pending == 0) {
3209       int target;
3210       cpi->refresh_golden_frame = 1;
3211       if (scene_cut_force_key_frame) cm->frame_type = KEY_FRAME;
3212       rc->source_alt_ref_pending = 0;
3213       if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf)
3214         rc->source_alt_ref_pending = 1;
3215       rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
3216       rc->baseline_gf_interval =
3217           VPXMIN(20, VPXMAX(10, rc->baseline_gf_interval));
3218       adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
3219       rc->frames_till_gf_update_due = rc->baseline_gf_interval;
3220       target = vp9_calc_pframe_target_size_one_pass_vbr(cpi);
3221       vp9_rc_set_frame_target(cpi, target);
3222       rc->count_last_scene_change = 0;
3223     } else {
3224       rc->count_last_scene_change++;
3225     }
3226     // If lag_in_frame is used, set the gf boost and interval.
3227     if (cpi->oxcf.lag_in_frames > 0)
3228       adjust_gf_boost_lag_one_pass_vbr(cpi, avg_sad_current);
3229   }
3230 }
3231 
3232 // Test if encoded frame will significantly overshoot the target bitrate, and
3233 // if so, set the QP, reset/adjust some rate control parameters, and return 1.
3234 // frame_size = -1 means frame has not been encoded.
vp9_encodedframe_overshoot(VP9_COMP * cpi,int frame_size,int * q)3235 int vp9_encodedframe_overshoot(VP9_COMP *cpi, int frame_size, int *q) {
3236   VP9_COMMON *const cm = &cpi->common;
3237   RATE_CONTROL *const rc = &cpi->rc;
3238   SPEED_FEATURES *const sf = &cpi->sf;
3239   int thresh_qp = 7 * (rc->worst_quality >> 3);
3240   int thresh_rate = rc->avg_frame_bandwidth << 3;
3241   // Lower thresh_qp for video (more overshoot at lower Q) to be
3242   // more conservative for video.
3243   if (cpi->oxcf.content != VP9E_CONTENT_SCREEN)
3244     thresh_qp = 3 * (rc->worst_quality >> 2);
3245   // If this decision is not based on an encoded frame size but just on
3246   // scene/slide change detection (i.e., re_encode_overshoot_cbr_rt ==
3247   // FAST_DETECTION_MAXQ), for now skip the (frame_size > thresh_rate)
3248   // condition in this case.
3249   // TODO(marpan): Use a better size/rate condition for this case and
3250   // adjust thresholds.
3251   if ((sf->overshoot_detection_cbr_rt == FAST_DETECTION_MAXQ ||
3252        frame_size > thresh_rate) &&
3253       cm->base_qindex < thresh_qp) {
3254     double rate_correction_factor =
3255         cpi->rc.rate_correction_factors[INTER_NORMAL];
3256     const int target_size = cpi->rc.avg_frame_bandwidth;
3257     double new_correction_factor;
3258     int target_bits_per_mb;
3259     double q2;
3260     int enumerator;
3261     // Force a re-encode, and for now use max-QP.
3262     *q = cpi->rc.worst_quality;
3263     cpi->cyclic_refresh->counter_encode_maxq_scene_change = 0;
3264     cpi->rc.re_encode_maxq_scene_change = 1;
3265     // If the frame_size is much larger than the threshold (big content change)
3266     // and the encoded frame used alot of Intra modes, then force hybrid_intra
3267     // encoding for the re-encode on this scene change. hybrid_intra will
3268     // use rd-based intra mode selection for small blocks.
3269     if (sf->overshoot_detection_cbr_rt == RE_ENCODE_MAXQ &&
3270         frame_size > (thresh_rate << 1) && cpi->svc.spatial_layer_id == 0) {
3271       MODE_INFO **mi = cm->mi_grid_visible;
3272       int sum_intra_usage = 0;
3273       int mi_row, mi_col;
3274       for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
3275         for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
3276           if (mi[0]->ref_frame[0] == INTRA_FRAME) sum_intra_usage++;
3277           mi++;
3278         }
3279         mi += 8;
3280       }
3281       sum_intra_usage = 100 * sum_intra_usage / (cm->mi_rows * cm->mi_cols);
3282       if (sum_intra_usage > 60) cpi->rc.hybrid_intra_scene_change = 1;
3283     }
3284     // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
3285     // these parameters will affect QP selection for subsequent frames. If they
3286     // have settled down to a very different (low QP) state, then not adjusting
3287     // them may cause next frame to select low QP and overshoot again.
3288     cpi->rc.avg_frame_qindex[INTER_FRAME] = *q;
3289     rc->buffer_level = rc->optimal_buffer_level;
3290     rc->bits_off_target = rc->optimal_buffer_level;
3291     // Reset rate under/over-shoot flags.
3292     cpi->rc.rc_1_frame = 0;
3293     cpi->rc.rc_2_frame = 0;
3294     // Adjust rate correction factor.
3295     target_bits_per_mb =
3296         (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->MBs);
3297     // Rate correction factor based on target_bits_per_mb and qp (==max_QP).
3298     // This comes from the inverse computation of vp9_rc_bits_per_mb().
3299     q2 = vp9_convert_qindex_to_q(*q, cm->bit_depth);
3300     enumerator = 1800000;  // Factor for inter frame.
3301     enumerator += (int)(enumerator * q2) >> 12;
3302     new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
3303     if (new_correction_factor > rate_correction_factor) {
3304       rate_correction_factor =
3305           VPXMIN(2.0 * rate_correction_factor, new_correction_factor);
3306       if (rate_correction_factor > MAX_BPB_FACTOR)
3307         rate_correction_factor = MAX_BPB_FACTOR;
3308       cpi->rc.rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
3309     }
3310     // For temporal layers, reset the rate control parametes across all
3311     // temporal layers.
3312     // If the first_spatial_layer_to_encode > 0, then this superframe has
3313     // skipped lower base layers. So in this case we should also reset and
3314     // force max-q for spatial layers < first_spatial_layer_to_encode.
3315     // For the case of no inter-layer prediction on delta frames: reset and
3316     // force max-q for all spatial layers, to avoid excessive frame drops.
3317     if (cpi->use_svc) {
3318       int tl = 0;
3319       int sl = 0;
3320       SVC *svc = &cpi->svc;
3321       int num_spatial_layers = VPXMAX(1, svc->first_spatial_layer_to_encode);
3322       if (svc->disable_inter_layer_pred != INTER_LAYER_PRED_ON)
3323         num_spatial_layers = svc->number_spatial_layers;
3324       for (sl = 0; sl < num_spatial_layers; ++sl) {
3325         for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
3326           const int layer =
3327               LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3328           LAYER_CONTEXT *lc = &svc->layer_context[layer];
3329           RATE_CONTROL *lrc = &lc->rc;
3330           lrc->avg_frame_qindex[INTER_FRAME] = *q;
3331           lrc->buffer_level = lrc->optimal_buffer_level;
3332           lrc->bits_off_target = lrc->optimal_buffer_level;
3333           lrc->rc_1_frame = 0;
3334           lrc->rc_2_frame = 0;
3335           lrc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
3336           lrc->force_max_q = 1;
3337         }
3338       }
3339     }
3340     return 1;
3341   } else {
3342     return 0;
3343   }
3344 }
3345