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