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 = (cpi->sf.recode_tolerance_low * frame_target) / 100;
1694     const int tol_high = (cpi->sf.recode_tolerance_high * frame_target) / 100;
1695     *frame_under_shoot_limit = VPXMAX(frame_target - tol_low - 100, 0);
1696     *frame_over_shoot_limit =
1697         VPXMIN(frame_target + tol_high + 100, cpi->rc.max_frame_bandwidth);
1698   }
1699 }
1700 
vp9_rc_set_frame_target(VP9_COMP * cpi,int target)1701 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
1702   const VP9_COMMON *const cm = &cpi->common;
1703   RATE_CONTROL *const rc = &cpi->rc;
1704 
1705   rc->this_frame_target = target;
1706 
1707   // Modify frame size target when down-scaling.
1708   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
1709       rc->frame_size_selector != UNSCALED)
1710     rc->this_frame_target = (int)(rc->this_frame_target *
1711                                   rate_thresh_mult[rc->frame_size_selector]);
1712 
1713   // Target rate per SB64 (including partial SB64s.
1714   rc->sb64_target_rate = (int)(((int64_t)rc->this_frame_target * 64 * 64) /
1715                                (cm->width * cm->height));
1716 }
1717 
update_alt_ref_frame_stats(VP9_COMP * cpi)1718 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
1719   // this frame refreshes means next frames don't unless specified by user
1720   RATE_CONTROL *const rc = &cpi->rc;
1721   rc->frames_since_golden = 0;
1722 
1723   // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1724   rc->source_alt_ref_pending = 0;
1725 
1726   // Set the alternate reference frame active flag
1727   rc->source_alt_ref_active = 1;
1728 }
1729 
update_golden_frame_stats(VP9_COMP * cpi)1730 static void update_golden_frame_stats(VP9_COMP *cpi) {
1731   RATE_CONTROL *const rc = &cpi->rc;
1732 
1733   // Update the Golden frame usage counts.
1734   if (cpi->refresh_golden_frame) {
1735     // this frame refreshes means next frames don't unless specified by user
1736     rc->frames_since_golden = 0;
1737 
1738     // If we are not using alt ref in the up and coming group clear the arf
1739     // active flag. In multi arf group case, if the index is not 0 then
1740     // we are overlaying a mid group arf so should not reset the flag.
1741     if (cpi->oxcf.pass == 2) {
1742       if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1743         rc->source_alt_ref_active = 0;
1744     } else if (!rc->source_alt_ref_pending) {
1745       rc->source_alt_ref_active = 0;
1746     }
1747 
1748     // Decrement count down till next gf
1749     if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1750 
1751   } else if (!cpi->refresh_alt_ref_frame) {
1752     // Decrement count down till next gf
1753     if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1754 
1755     rc->frames_since_golden++;
1756 
1757     if (rc->show_arf_as_gld) {
1758       rc->frames_since_golden = 0;
1759       // If we are not using alt ref in the up and coming group clear the arf
1760       // active flag. In multi arf group case, if the index is not 0 then
1761       // we are overlaying a mid group arf so should not reset the flag.
1762       if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1763         rc->source_alt_ref_active = 0;
1764     }
1765   }
1766 }
1767 
update_altref_usage(VP9_COMP * const cpi)1768 static void update_altref_usage(VP9_COMP *const cpi) {
1769   VP9_COMMON *const cm = &cpi->common;
1770   int sum_ref_frame_usage = 0;
1771   int arf_frame_usage = 0;
1772   int mi_row, mi_col;
1773   if (cpi->rc.alt_ref_gf_group && !cpi->rc.is_src_frame_alt_ref &&
1774       !cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame)
1775     for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8) {
1776       for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8) {
1777         int sboffset = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
1778         sum_ref_frame_usage += cpi->count_arf_frame_usage[sboffset] +
1779                                cpi->count_lastgolden_frame_usage[sboffset];
1780         arf_frame_usage += cpi->count_arf_frame_usage[sboffset];
1781       }
1782     }
1783   if (sum_ref_frame_usage > 0) {
1784     double altref_count = 100.0 * arf_frame_usage / sum_ref_frame_usage;
1785     cpi->rc.perc_arf_usage =
1786         0.75 * cpi->rc.perc_arf_usage + 0.25 * altref_count;
1787   }
1788 }
1789 
compute_frame_low_motion(VP9_COMP * const cpi)1790 static void compute_frame_low_motion(VP9_COMP *const cpi) {
1791   VP9_COMMON *const cm = &cpi->common;
1792   int mi_row, mi_col;
1793   MODE_INFO **mi = cm->mi_grid_visible;
1794   RATE_CONTROL *const rc = &cpi->rc;
1795   const int rows = cm->mi_rows, cols = cm->mi_cols;
1796   int cnt_zeromv = 0;
1797   for (mi_row = 0; mi_row < rows; mi_row++) {
1798     for (mi_col = 0; mi_col < cols; mi_col++) {
1799       if (mi[0]->ref_frame[0] == LAST_FRAME &&
1800           abs(mi[0]->mv[0].as_mv.row) < 16 && abs(mi[0]->mv[0].as_mv.col) < 16)
1801         cnt_zeromv++;
1802       mi++;
1803     }
1804     mi += 8;
1805   }
1806   cnt_zeromv = 100 * cnt_zeromv / (rows * cols);
1807   rc->avg_frame_low_motion = (3 * rc->avg_frame_low_motion + cnt_zeromv) >> 2;
1808 }
1809 
vp9_rc_postencode_update(VP9_COMP * cpi,uint64_t bytes_used)1810 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
1811   const VP9_COMMON *const cm = &cpi->common;
1812   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1813   RATE_CONTROL *const rc = &cpi->rc;
1814   SVC *const svc = &cpi->svc;
1815   const int qindex = cm->base_qindex;
1816   const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1817   const int gf_group_index = cpi->twopass.gf_group.index;
1818   const int layer_depth = gf_group->layer_depth[gf_group_index];
1819 
1820   // Update rate control heuristics
1821   rc->projected_frame_size = (int)(bytes_used << 3);
1822 
1823   // Post encode loop adjustment of Q prediction.
1824   vp9_rc_update_rate_correction_factors(cpi);
1825 
1826   // Keep a record of last Q and ambient average Q.
1827   if (frame_is_intra_only(cm)) {
1828     rc->last_q[KEY_FRAME] = qindex;
1829     rc->avg_frame_qindex[KEY_FRAME] =
1830         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1831     if (cpi->use_svc) {
1832       int i = 0;
1833       SVC *svc = &cpi->svc;
1834       for (i = 0; i < svc->number_temporal_layers; ++i) {
1835         const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
1836                                            svc->number_temporal_layers);
1837         LAYER_CONTEXT *lc = &svc->layer_context[layer];
1838         RATE_CONTROL *lrc = &lc->rc;
1839         lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME];
1840         lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME];
1841       }
1842     }
1843   } else {
1844     if ((cpi->use_svc && oxcf->rc_mode == VPX_CBR) ||
1845         (!rc->is_src_frame_alt_ref &&
1846          !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1847       rc->last_q[INTER_FRAME] = qindex;
1848       rc->avg_frame_qindex[INTER_FRAME] =
1849           ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1850       rc->ni_frames++;
1851       rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1852       rc->avg_q = rc->tot_q / rc->ni_frames;
1853       // Calculate the average Q for normal inter frames (not key or GFU
1854       // frames).
1855       rc->ni_tot_qi += qindex;
1856       rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1857     }
1858   }
1859 
1860   if (cpi->use_svc) vp9_svc_adjust_avg_frame_qindex(cpi);
1861 
1862   // Keep record of last boosted (KF/KF/ARF) Q value.
1863   // If the current frame is coded at a lower Q then we also update it.
1864   // If all mbs in this group are skipped only update if the Q value is
1865   // better than that already stored.
1866   // This is used to help set quality in forced key frames to reduce popping
1867   if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) ||
1868       (!rc->constrained_gf_group &&
1869        (cpi->refresh_alt_ref_frame ||
1870         (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1871     rc->last_boosted_qindex = qindex;
1872   }
1873 
1874   if ((qindex < cpi->twopass.last_qindex_of_arf_layer[layer_depth]) ||
1875       (cm->frame_type == KEY_FRAME) ||
1876       (!rc->constrained_gf_group &&
1877        (cpi->refresh_alt_ref_frame ||
1878         (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1879     cpi->twopass.last_qindex_of_arf_layer[layer_depth] = qindex;
1880   }
1881 
1882   if (frame_is_intra_only(cm)) rc->last_kf_qindex = qindex;
1883 
1884   update_buffer_level_postencode(cpi, rc->projected_frame_size);
1885 
1886   // Rolling monitors of whether we are over or underspending used to help
1887   // regulate min and Max Q in two pass.
1888   if (!frame_is_intra_only(cm)) {
1889     rc->rolling_target_bits = ROUND_POWER_OF_TWO(
1890         rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1891     rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
1892         rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1893     rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
1894         rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1895     rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
1896         rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1897   }
1898 
1899   // Actual bits spent
1900   rc->total_actual_bits += rc->projected_frame_size;
1901   rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1902 
1903   rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1904 
1905   if (!cpi->use_svc) {
1906     if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1907         (!frame_is_intra_only(cm)))
1908       // Update the alternate reference frame stats as appropriate.
1909       update_alt_ref_frame_stats(cpi);
1910     else
1911       // Update the Golden frame stats as appropriate.
1912       update_golden_frame_stats(cpi);
1913   }
1914 
1915   // If second (long term) temporal reference is used for SVC,
1916   // update the golden frame counter, only for base temporal layer.
1917   if (cpi->use_svc && svc->use_gf_temporal_ref_current_layer &&
1918       svc->temporal_layer_id == 0) {
1919     int i = 0;
1920     if (cpi->refresh_golden_frame)
1921       rc->frames_since_golden = 0;
1922     else
1923       rc->frames_since_golden++;
1924     // Decrement count down till next gf
1925     if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1926     // Update the frames_since_golden for all upper temporal layers.
1927     for (i = 1; i < svc->number_temporal_layers; ++i) {
1928       const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
1929                                          svc->number_temporal_layers);
1930       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1931       RATE_CONTROL *const lrc = &lc->rc;
1932       lrc->frames_since_golden = rc->frames_since_golden;
1933     }
1934   }
1935 
1936   if (frame_is_intra_only(cm)) rc->frames_since_key = 0;
1937   if (cm->show_frame) {
1938     rc->frames_since_key++;
1939     rc->frames_to_key--;
1940   }
1941 
1942   // Trigger the resizing of the next frame if it is scaled.
1943   if (oxcf->pass != 0) {
1944     cpi->resize_pending =
1945         rc->next_frame_size_selector != rc->frame_size_selector;
1946     rc->frame_size_selector = rc->next_frame_size_selector;
1947   }
1948 
1949   if (oxcf->pass == 0) {
1950     if (!frame_is_intra_only(cm) &&
1951         (!cpi->use_svc ||
1952          (cpi->use_svc &&
1953           !svc->layer_context[svc->temporal_layer_id].is_key_frame &&
1954           svc->spatial_layer_id == svc->number_spatial_layers - 1))) {
1955       compute_frame_low_motion(cpi);
1956       if (cpi->sf.use_altref_onepass) update_altref_usage(cpi);
1957     }
1958     // For SVC: set avg_frame_low_motion (only computed on top spatial layer)
1959     // to all lower spatial layers.
1960     if (cpi->use_svc &&
1961         svc->spatial_layer_id == svc->number_spatial_layers - 1) {
1962       int i;
1963       for (i = 0; i < svc->number_spatial_layers - 1; ++i) {
1964         const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
1965                                            svc->number_temporal_layers);
1966         LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1967         RATE_CONTROL *const lrc = &lc->rc;
1968         lrc->avg_frame_low_motion = rc->avg_frame_low_motion;
1969       }
1970     }
1971     cpi->rc.last_frame_is_src_altref = cpi->rc.is_src_frame_alt_ref;
1972   }
1973   if (!frame_is_intra_only(cm)) rc->reset_high_source_sad = 0;
1974 
1975   rc->last_avg_frame_bandwidth = rc->avg_frame_bandwidth;
1976   if (cpi->use_svc && svc->spatial_layer_id < svc->number_spatial_layers - 1)
1977     svc->lower_layer_qindex = cm->base_qindex;
1978 }
1979 
vp9_rc_postencode_update_drop_frame(VP9_COMP * cpi)1980 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
1981   cpi->common.current_video_frame++;
1982   cpi->rc.frames_since_key++;
1983   cpi->rc.frames_to_key--;
1984   cpi->rc.rc_2_frame = 0;
1985   cpi->rc.rc_1_frame = 0;
1986   cpi->rc.last_avg_frame_bandwidth = cpi->rc.avg_frame_bandwidth;
1987   // For SVC on dropped frame when framedrop_mode != LAYER_DROP:
1988   // in this mode the whole superframe may be dropped if only a single layer
1989   // has buffer underflow (below threshold). Since this can then lead to
1990   // increasing buffer levels/overflow for certain layers even though whole
1991   // superframe is dropped, we cap buffer level if its already stable.
1992   if (cpi->use_svc && cpi->svc.framedrop_mode != LAYER_DROP &&
1993       cpi->rc.buffer_level > cpi->rc.optimal_buffer_level) {
1994     cpi->rc.buffer_level = cpi->rc.optimal_buffer_level;
1995     cpi->rc.bits_off_target = cpi->rc.optimal_buffer_level;
1996   }
1997 }
1998 
calc_pframe_target_size_one_pass_vbr(const VP9_COMP * const cpi)1999 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
2000   const RATE_CONTROL *const rc = &cpi->rc;
2001   const int af_ratio = rc->af_ratio_onepass_vbr;
2002   int target =
2003       (!rc->is_src_frame_alt_ref &&
2004        (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
2005           ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
2006                 (rc->baseline_gf_interval + af_ratio - 1)
2007           : (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
2008                 (rc->baseline_gf_interval + af_ratio - 1);
2009   return vp9_rc_clamp_pframe_target_size(cpi, target);
2010 }
2011 
calc_iframe_target_size_one_pass_vbr(const VP9_COMP * const cpi)2012 static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
2013   static const int kf_ratio = 25;
2014   const RATE_CONTROL *rc = &cpi->rc;
2015   const int target = rc->avg_frame_bandwidth * kf_ratio;
2016   return vp9_rc_clamp_iframe_target_size(cpi, target);
2017 }
2018 
adjust_gfint_frame_constraint(VP9_COMP * cpi,int frame_constraint)2019 static void adjust_gfint_frame_constraint(VP9_COMP *cpi, int frame_constraint) {
2020   RATE_CONTROL *const rc = &cpi->rc;
2021   rc->constrained_gf_group = 0;
2022   // Reset gf interval to make more equal spacing for frame_constraint.
2023   if ((frame_constraint <= 7 * rc->baseline_gf_interval >> 2) &&
2024       (frame_constraint > rc->baseline_gf_interval)) {
2025     rc->baseline_gf_interval = frame_constraint >> 1;
2026     if (rc->baseline_gf_interval < 5)
2027       rc->baseline_gf_interval = frame_constraint;
2028     rc->constrained_gf_group = 1;
2029   } else {
2030     // Reset to keep gf_interval <= frame_constraint.
2031     if (rc->baseline_gf_interval > frame_constraint) {
2032       rc->baseline_gf_interval = frame_constraint;
2033       rc->constrained_gf_group = 1;
2034     }
2035   }
2036 }
2037 
vp9_rc_get_one_pass_vbr_params(VP9_COMP * cpi)2038 void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
2039   VP9_COMMON *const cm = &cpi->common;
2040   RATE_CONTROL *const rc = &cpi->rc;
2041   int target;
2042   if (!cpi->refresh_alt_ref_frame &&
2043       (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
2044        rc->frames_to_key == 0)) {
2045     cm->frame_type = KEY_FRAME;
2046     rc->this_key_frame_forced =
2047         cm->current_video_frame != 0 && rc->frames_to_key == 0;
2048     rc->frames_to_key = cpi->oxcf.key_freq;
2049     rc->kf_boost = DEFAULT_KF_BOOST;
2050     rc->source_alt_ref_active = 0;
2051   } else {
2052     cm->frame_type = INTER_FRAME;
2053   }
2054   if (rc->frames_till_gf_update_due == 0) {
2055     double rate_err = 1.0;
2056     rc->gfu_boost = DEFAULT_GF_BOOST;
2057     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0) {
2058       vp9_cyclic_refresh_set_golden_update(cpi);
2059     } else {
2060       rc->baseline_gf_interval = VPXMIN(
2061           20, VPXMAX(10, (rc->min_gf_interval + rc->max_gf_interval) / 2));
2062     }
2063     rc->af_ratio_onepass_vbr = 10;
2064     if (rc->rolling_target_bits > 0)
2065       rate_err =
2066           (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
2067     if (cm->current_video_frame > 30) {
2068       if (rc->avg_frame_qindex[INTER_FRAME] > (7 * rc->worst_quality) >> 3 &&
2069           rate_err > 3.5) {
2070         rc->baseline_gf_interval =
2071             VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
2072       } else if (rc->avg_frame_low_motion < 20) {
2073         // Decrease gf interval for high motion case.
2074         rc->baseline_gf_interval = VPXMAX(6, rc->baseline_gf_interval >> 1);
2075       }
2076       // Adjust boost and af_ratio based on avg_frame_low_motion, which varies
2077       // between 0 and 100 (stationary, 100% zero/small motion).
2078       rc->gfu_boost =
2079           VPXMAX(500, DEFAULT_GF_BOOST * (rc->avg_frame_low_motion << 1) /
2080                           (rc->avg_frame_low_motion + 100));
2081       rc->af_ratio_onepass_vbr = VPXMIN(15, VPXMAX(5, 3 * rc->gfu_boost / 400));
2082     }
2083     adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
2084     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2085     cpi->refresh_golden_frame = 1;
2086     rc->source_alt_ref_pending = 0;
2087     rc->alt_ref_gf_group = 0;
2088     if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
2089       rc->source_alt_ref_pending = 1;
2090       rc->alt_ref_gf_group = 1;
2091     }
2092   }
2093   if (cm->frame_type == KEY_FRAME)
2094     target = calc_iframe_target_size_one_pass_vbr(cpi);
2095   else
2096     target = calc_pframe_target_size_one_pass_vbr(cpi);
2097   vp9_rc_set_frame_target(cpi, target);
2098   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0)
2099     vp9_cyclic_refresh_update_parameters(cpi);
2100 }
2101 
calc_pframe_target_size_one_pass_cbr(const VP9_COMP * cpi)2102 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
2103   const VP9EncoderConfig *oxcf = &cpi->oxcf;
2104   const RATE_CONTROL *rc = &cpi->rc;
2105   const SVC *const svc = &cpi->svc;
2106   const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
2107   const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
2108   int min_frame_target =
2109       VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
2110   int target;
2111 
2112   if (oxcf->gf_cbr_boost_pct) {
2113     const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
2114     target = cpi->refresh_golden_frame
2115                  ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval *
2116                     af_ratio_pct) /
2117                        (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
2118                  : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
2119                        (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
2120   } else {
2121     target = rc->avg_frame_bandwidth;
2122   }
2123   if (is_one_pass_cbr_svc(cpi)) {
2124     // Note that for layers, avg_frame_bandwidth is the cumulative
2125     // per-frame-bandwidth. For the target size of this frame, use the
2126     // layer average frame size (i.e., non-cumulative per-frame-bw).
2127     int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2128                                  svc->number_temporal_layers);
2129     const LAYER_CONTEXT *lc = &svc->layer_context[layer];
2130     target = lc->avg_frame_size;
2131     min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
2132   }
2133   if (diff > 0) {
2134     // Lower the target bandwidth for this frame.
2135     const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
2136     target -= (target * pct_low) / 200;
2137   } else if (diff < 0) {
2138     // Increase the target bandwidth for this frame.
2139     const int pct_high =
2140         (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
2141     target += (target * pct_high) / 200;
2142   }
2143   if (oxcf->rc_max_inter_bitrate_pct) {
2144     const int max_rate =
2145         rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
2146     target = VPXMIN(target, max_rate);
2147   }
2148   return VPXMAX(min_frame_target, target);
2149 }
2150 
calc_iframe_target_size_one_pass_cbr(const VP9_COMP * cpi)2151 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
2152   const RATE_CONTROL *rc = &cpi->rc;
2153   const VP9EncoderConfig *oxcf = &cpi->oxcf;
2154   const SVC *const svc = &cpi->svc;
2155   int target;
2156   if (cpi->common.current_video_frame == 0) {
2157     target = ((rc->starting_buffer_level / 2) > INT_MAX)
2158                  ? INT_MAX
2159                  : (int)(rc->starting_buffer_level / 2);
2160   } else {
2161     int kf_boost = 32;
2162     double framerate = cpi->framerate;
2163     if (svc->number_temporal_layers > 1 && oxcf->rc_mode == VPX_CBR) {
2164       // Use the layer framerate for temporal layers CBR mode.
2165       const int layer =
2166           LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2167                            svc->number_temporal_layers);
2168       const LAYER_CONTEXT *lc = &svc->layer_context[layer];
2169       framerate = lc->framerate;
2170     }
2171     kf_boost = VPXMAX(kf_boost, (int)(2 * framerate - 16));
2172     if (rc->frames_since_key < framerate / 2) {
2173       kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
2174     }
2175     target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
2176   }
2177   return vp9_rc_clamp_iframe_target_size(cpi, target);
2178 }
2179 
set_intra_only_frame(VP9_COMP * cpi)2180 static void set_intra_only_frame(VP9_COMP *cpi) {
2181   VP9_COMMON *const cm = &cpi->common;
2182   SVC *const svc = &cpi->svc;
2183   // Don't allow intra_only frame for bypass/flexible SVC mode, or if number
2184   // of spatial layers is 1 or if number of spatial or temporal layers > 3.
2185   // Also if intra-only is inserted on very first frame, don't allow if
2186   // if number of temporal layers > 1. This is because on intra-only frame
2187   // only 3 reference buffers can be updated, but for temporal layers > 1
2188   // we generally need to use buffer slots 4 and 5.
2189   if ((cm->current_video_frame == 0 && svc->number_temporal_layers > 1) ||
2190       svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS ||
2191       svc->number_spatial_layers > 3 || svc->number_temporal_layers > 3 ||
2192       svc->number_spatial_layers == 1)
2193     return;
2194   cm->show_frame = 0;
2195   cm->intra_only = 1;
2196   cm->frame_type = INTER_FRAME;
2197   cpi->ext_refresh_frame_flags_pending = 1;
2198   cpi->ext_refresh_last_frame = 1;
2199   cpi->ext_refresh_golden_frame = 1;
2200   cpi->ext_refresh_alt_ref_frame = 1;
2201   if (cm->current_video_frame == 0) {
2202     cpi->lst_fb_idx = 0;
2203     cpi->gld_fb_idx = 1;
2204     cpi->alt_fb_idx = 2;
2205   } else {
2206     int i;
2207     int count = 0;
2208     cpi->lst_fb_idx = -1;
2209     cpi->gld_fb_idx = -1;
2210     cpi->alt_fb_idx = -1;
2211     // For intra-only frame we need to refresh all slots that were
2212     // being used for the base layer (fb_idx_base[i] == 1).
2213     // Start with assigning last first, then golden and then alt.
2214     for (i = 0; i < REF_FRAMES; ++i) {
2215       if (svc->fb_idx_base[i] == 1) count++;
2216       if (count == 1 && cpi->lst_fb_idx == -1) cpi->lst_fb_idx = i;
2217       if (count == 2 && cpi->gld_fb_idx == -1) cpi->gld_fb_idx = i;
2218       if (count == 3 && cpi->alt_fb_idx == -1) cpi->alt_fb_idx = i;
2219     }
2220     // If golden or alt is not being used for base layer, then set them
2221     // to the lst_fb_idx.
2222     if (cpi->gld_fb_idx == -1) cpi->gld_fb_idx = cpi->lst_fb_idx;
2223     if (cpi->alt_fb_idx == -1) cpi->alt_fb_idx = cpi->lst_fb_idx;
2224   }
2225 }
2226 
vp9_rc_get_svc_params(VP9_COMP * cpi)2227 void vp9_rc_get_svc_params(VP9_COMP *cpi) {
2228   VP9_COMMON *const cm = &cpi->common;
2229   RATE_CONTROL *const rc = &cpi->rc;
2230   SVC *const svc = &cpi->svc;
2231   int target = rc->avg_frame_bandwidth;
2232   int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2233                                svc->number_temporal_layers);
2234   if (svc->first_spatial_layer_to_encode)
2235     svc->layer_context[svc->temporal_layer_id].is_key_frame = 0;
2236   // Periodic key frames is based on the super-frame counter
2237   // (svc.current_superframe), also only base spatial layer is key frame.
2238   // Key frame is set for any of the following: very first frame, frame flags
2239   // indicates key, superframe counter hits key frequencey, or (non-intra) sync
2240   // flag is set for spatial layer 0.
2241   if ((cm->current_video_frame == 0 && !svc->previous_frame_is_intra_only) ||
2242       (cpi->frame_flags & FRAMEFLAGS_KEY) ||
2243       (cpi->oxcf.auto_key &&
2244        (svc->current_superframe % cpi->oxcf.key_freq == 0) &&
2245        !svc->previous_frame_is_intra_only && svc->spatial_layer_id == 0) ||
2246       (svc->spatial_layer_sync[0] == 1 && svc->spatial_layer_id == 0)) {
2247     cm->frame_type = KEY_FRAME;
2248     rc->source_alt_ref_active = 0;
2249     if (is_one_pass_cbr_svc(cpi)) {
2250       if (cm->current_video_frame > 0) vp9_svc_reset_temporal_layers(cpi, 1);
2251       layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2252                                svc->number_temporal_layers);
2253       svc->layer_context[layer].is_key_frame = 1;
2254       cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2255       // Assumption here is that LAST_FRAME is being updated for a keyframe.
2256       // Thus no change in update flags.
2257       target = calc_iframe_target_size_one_pass_cbr(cpi);
2258     }
2259   } else {
2260     cm->frame_type = INTER_FRAME;
2261     if (is_one_pass_cbr_svc(cpi)) {
2262       LAYER_CONTEXT *lc = &svc->layer_context[layer];
2263       // Add condition current_video_frame > 0 for the case where first frame
2264       // is intra only followed by overlay/copy frame. In this case we don't
2265       // want to reset is_key_frame to 0 on overlay/copy frame.
2266       lc->is_key_frame =
2267           (svc->spatial_layer_id == 0 && cm->current_video_frame > 0)
2268               ? 0
2269               : svc->layer_context[svc->temporal_layer_id].is_key_frame;
2270       target = calc_pframe_target_size_one_pass_cbr(cpi);
2271     }
2272   }
2273 
2274   if (svc->simulcast_mode) {
2275     if (svc->spatial_layer_id > 0 &&
2276         svc->layer_context[layer].is_key_frame == 1) {
2277       cm->frame_type = KEY_FRAME;
2278       cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2279       target = calc_iframe_target_size_one_pass_cbr(cpi);
2280     }
2281     // Set the buffer idx and refresh flags for key frames in simulcast mode.
2282     // Note the buffer slot for long-term reference is set below (line 2255),
2283     // and alt_ref is used for that on key frame. So use last and golden for
2284     // the other two normal slots.
2285     if (cm->frame_type == KEY_FRAME) {
2286       if (svc->number_spatial_layers == 2) {
2287         if (svc->spatial_layer_id == 0) {
2288           cpi->lst_fb_idx = 0;
2289           cpi->gld_fb_idx = 2;
2290           cpi->alt_fb_idx = 6;
2291         } else if (svc->spatial_layer_id == 1) {
2292           cpi->lst_fb_idx = 1;
2293           cpi->gld_fb_idx = 3;
2294           cpi->alt_fb_idx = 6;
2295         }
2296       } else if (svc->number_spatial_layers == 3) {
2297         if (svc->spatial_layer_id == 0) {
2298           cpi->lst_fb_idx = 0;
2299           cpi->gld_fb_idx = 3;
2300           cpi->alt_fb_idx = 6;
2301         } else if (svc->spatial_layer_id == 1) {
2302           cpi->lst_fb_idx = 1;
2303           cpi->gld_fb_idx = 4;
2304           cpi->alt_fb_idx = 6;
2305         } else if (svc->spatial_layer_id == 2) {
2306           cpi->lst_fb_idx = 2;
2307           cpi->gld_fb_idx = 5;
2308           cpi->alt_fb_idx = 7;
2309         }
2310       }
2311       cpi->ext_refresh_last_frame = 1;
2312       cpi->ext_refresh_golden_frame = 1;
2313       cpi->ext_refresh_alt_ref_frame = 1;
2314     }
2315   }
2316 
2317   // Check if superframe contains a sync layer request.
2318   vp9_svc_check_spatial_layer_sync(cpi);
2319 
2320   // If long term termporal feature is enabled, set the period of the update.
2321   // The update/refresh of this reference frame is always on base temporal
2322   // layer frame.
2323   if (svc->use_gf_temporal_ref_current_layer) {
2324     // Only use gf long-term prediction on non-key superframes.
2325     if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
2326       // Use golden for this reference, which will be used for prediction.
2327       int index = svc->spatial_layer_id;
2328       if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
2329       assert(index >= 0);
2330       cpi->gld_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
2331       // Enable prediction off LAST (last reference) and golden (which will
2332       // generally be further behind/long-term reference).
2333       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
2334     }
2335     // Check for update/refresh of reference: only refresh on base temporal
2336     // layer.
2337     if (svc->temporal_layer_id == 0) {
2338       if (svc->layer_context[svc->temporal_layer_id].is_key_frame) {
2339         // On key frame we update the buffer index used for long term reference.
2340         // Use the alt_ref since it is not used or updated on key frames.
2341         int index = svc->spatial_layer_id;
2342         if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
2343         assert(index >= 0);
2344         cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
2345         cpi->ext_refresh_alt_ref_frame = 1;
2346       } else if (rc->frames_till_gf_update_due == 0) {
2347         // Set perdiod of next update. Make it a multiple of 10, as the cyclic
2348         // refresh is typically ~10%, and we'd like the update to happen after
2349         // a few cylces of the refresh (so it better quality frame). Note the
2350         // cyclic refresh for SVC only operates on base temporal layer frames.
2351         // Choose 20 as perdiod for now (2 cycles).
2352         rc->baseline_gf_interval = 20;
2353         rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2354         cpi->ext_refresh_golden_frame = 1;
2355         rc->gfu_boost = DEFAULT_GF_BOOST;
2356       }
2357     }
2358   } else if (!svc->use_gf_temporal_ref) {
2359     rc->frames_till_gf_update_due = INT_MAX;
2360     rc->baseline_gf_interval = INT_MAX;
2361   }
2362   if (svc->set_intra_only_frame) {
2363     set_intra_only_frame(cpi);
2364     target = calc_iframe_target_size_one_pass_cbr(cpi);
2365   }
2366   // Any update/change of global cyclic refresh parameters (amount/delta-qp)
2367   // should be done here, before the frame qp is selected.
2368   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2369     vp9_cyclic_refresh_update_parameters(cpi);
2370 
2371   vp9_rc_set_frame_target(cpi, target);
2372   if (cm->show_frame) update_buffer_level_svc_preencode(cpi);
2373 }
2374 
vp9_rc_get_one_pass_cbr_params(VP9_COMP * cpi)2375 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
2376   VP9_COMMON *const cm = &cpi->common;
2377   RATE_CONTROL *const rc = &cpi->rc;
2378   int target;
2379   if ((cm->current_video_frame == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
2380       (cpi->oxcf.auto_key && rc->frames_to_key == 0)) {
2381     cm->frame_type = KEY_FRAME;
2382     rc->frames_to_key = cpi->oxcf.key_freq;
2383     rc->kf_boost = DEFAULT_KF_BOOST;
2384     rc->source_alt_ref_active = 0;
2385   } else {
2386     cm->frame_type = INTER_FRAME;
2387   }
2388   if (rc->frames_till_gf_update_due == 0) {
2389     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2390       vp9_cyclic_refresh_set_golden_update(cpi);
2391     else
2392       rc->baseline_gf_interval =
2393           (rc->min_gf_interval + rc->max_gf_interval) / 2;
2394     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2395     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
2396     if (rc->frames_till_gf_update_due > rc->frames_to_key)
2397       rc->frames_till_gf_update_due = rc->frames_to_key;
2398     cpi->refresh_golden_frame = 1;
2399     rc->gfu_boost = DEFAULT_GF_BOOST;
2400   }
2401 
2402   // Any update/change of global cyclic refresh parameters (amount/delta-qp)
2403   // should be done here, before the frame qp is selected.
2404   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2405     vp9_cyclic_refresh_update_parameters(cpi);
2406 
2407   if (frame_is_intra_only(cm))
2408     target = calc_iframe_target_size_one_pass_cbr(cpi);
2409   else
2410     target = calc_pframe_target_size_one_pass_cbr(cpi);
2411 
2412   vp9_rc_set_frame_target(cpi, target);
2413 
2414   if (cm->show_frame) update_buffer_level_preencode(cpi);
2415 
2416   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC)
2417     cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
2418   else
2419     cpi->resize_pending = 0;
2420 }
2421 
vp9_compute_qdelta(const RATE_CONTROL * rc,double qstart,double qtarget,vpx_bit_depth_t bit_depth)2422 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
2423                        vpx_bit_depth_t bit_depth) {
2424   int start_index = rc->worst_quality;
2425   int target_index = rc->worst_quality;
2426   int i;
2427 
2428   // Convert the average q value to an index.
2429   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
2430     start_index = i;
2431     if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart) break;
2432   }
2433 
2434   // Convert the q target to an index
2435   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
2436     target_index = i;
2437     if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
2438   }
2439 
2440   return target_index - start_index;
2441 }
2442 
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)2443 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
2444                                int qindex, double rate_target_ratio,
2445                                vpx_bit_depth_t bit_depth) {
2446   int target_index = rc->worst_quality;
2447   int i;
2448 
2449   // Look up the current projected bits per block for the base index
2450   const int base_bits_per_mb =
2451       vp9_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
2452 
2453   // Find the target bits per mb based on the base value and given ratio.
2454   const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
2455 
2456   // Convert the q target to an index
2457   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
2458     if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
2459         target_bits_per_mb) {
2460       target_index = i;
2461       break;
2462     }
2463   }
2464   return target_index - qindex;
2465 }
2466 
vp9_rc_set_gf_interval_range(const VP9_COMP * const cpi,RATE_CONTROL * const rc)2467 void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi,
2468                                   RATE_CONTROL *const rc) {
2469   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2470 
2471   // Special case code for 1 pass fixed Q mode tests
2472   if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
2473     rc->max_gf_interval = FIXED_GF_INTERVAL;
2474     rc->min_gf_interval = FIXED_GF_INTERVAL;
2475     rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
2476   } else {
2477     // Set Maximum gf/arf interval
2478     rc->max_gf_interval = oxcf->max_gf_interval;
2479     rc->min_gf_interval = oxcf->min_gf_interval;
2480 #if CONFIG_RATE_CTRL
2481     if (rc->min_gf_interval == 0) {
2482       rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
2483           oxcf->width, oxcf->height, oxcf->init_framerate);
2484     }
2485     if (rc->max_gf_interval == 0) {
2486       rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
2487           oxcf->init_framerate, rc->min_gf_interval);
2488     }
2489 #else
2490     if (rc->min_gf_interval == 0)
2491       rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
2492           oxcf->width, oxcf->height, cpi->framerate);
2493     if (rc->max_gf_interval == 0)
2494       rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
2495           cpi->framerate, rc->min_gf_interval);
2496 #endif
2497 
2498     // Extended max interval for genuinely static scenes like slide shows.
2499     rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH;
2500 
2501     if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
2502       rc->max_gf_interval = rc->static_scene_max_gf_interval;
2503 
2504     // Clamp min to max
2505     rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
2506 
2507     if (oxcf->target_level == LEVEL_AUTO) {
2508       const uint32_t pic_size = cpi->common.width * cpi->common.height;
2509       const uint32_t pic_breadth =
2510           VPXMAX(cpi->common.width, cpi->common.height);
2511       int i;
2512       for (i = LEVEL_1; i < LEVEL_MAX; ++i) {
2513         if (vp9_level_defs[i].max_luma_picture_size >= pic_size &&
2514             vp9_level_defs[i].max_luma_picture_breadth >= pic_breadth) {
2515           if (rc->min_gf_interval <=
2516               (int)vp9_level_defs[i].min_altref_distance) {
2517             rc->min_gf_interval =
2518                 (int)vp9_level_defs[i].min_altref_distance + 1;
2519             rc->max_gf_interval =
2520                 VPXMAX(rc->max_gf_interval, rc->min_gf_interval);
2521           }
2522           break;
2523         }
2524       }
2525     }
2526   }
2527 }
2528 
vp9_rc_update_framerate(VP9_COMP * cpi)2529 void vp9_rc_update_framerate(VP9_COMP *cpi) {
2530   const VP9_COMMON *const cm = &cpi->common;
2531   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2532   RATE_CONTROL *const rc = &cpi->rc;
2533   int vbr_max_bits;
2534 
2535   rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
2536   rc->min_frame_bandwidth =
2537       (int)(rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
2538 
2539   rc->min_frame_bandwidth =
2540       VPXMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
2541 
2542   // A maximum bitrate for a frame is defined.
2543   // However this limit is extended if a very high rate is given on the command
2544   // line or the the rate cannnot be acheived because of a user specificed max q
2545   // (e.g. when the user specifies lossless encode).
2546   //
2547   // If a level is specified that requires a lower maximum rate then the level
2548   // value take precedence.
2549   vbr_max_bits =
2550       (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section) /
2551             100);
2552   rc->max_frame_bandwidth =
2553       VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
2554 
2555   vp9_rc_set_gf_interval_range(cpi, rc);
2556 }
2557 
2558 #define VBR_PCT_ADJUSTMENT_LIMIT 50
2559 // For VBR...adjustment to the frame target based on error from previous frames
vbr_rate_correction(VP9_COMP * cpi,int * this_frame_target)2560 static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
2561   RATE_CONTROL *const rc = &cpi->rc;
2562   int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
2563   int max_delta;
2564   int frame_window = VPXMIN(16, ((int)cpi->twopass.total_stats.count -
2565                                  cpi->common.current_video_frame));
2566 
2567   // Calcluate the adjustment to rate for this frame.
2568   if (frame_window > 0) {
2569     max_delta = (vbr_bits_off_target > 0)
2570                     ? (int)(vbr_bits_off_target / frame_window)
2571                     : (int)(-vbr_bits_off_target / frame_window);
2572 
2573     max_delta = VPXMIN(max_delta,
2574                        ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
2575 
2576     // vbr_bits_off_target > 0 means we have extra bits to spend
2577     if (vbr_bits_off_target > 0) {
2578       *this_frame_target += (vbr_bits_off_target > max_delta)
2579                                 ? max_delta
2580                                 : (int)vbr_bits_off_target;
2581     } else {
2582       *this_frame_target -= (vbr_bits_off_target < -max_delta)
2583                                 ? max_delta
2584                                 : (int)-vbr_bits_off_target;
2585     }
2586   }
2587 
2588   // Fast redistribution of bits arising from massive local undershoot.
2589   // Dont do it for kf,arf,gf or overlay frames.
2590   if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
2591       rc->vbr_bits_off_target_fast) {
2592     int one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, *this_frame_target);
2593     int fast_extra_bits;
2594     fast_extra_bits = (int)VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
2595     fast_extra_bits = (int)VPXMIN(
2596         fast_extra_bits,
2597         VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
2598     *this_frame_target += (int)fast_extra_bits;
2599     rc->vbr_bits_off_target_fast -= fast_extra_bits;
2600   }
2601 }
2602 
vp9_set_target_rate(VP9_COMP * cpi)2603 void vp9_set_target_rate(VP9_COMP *cpi) {
2604   RATE_CONTROL *const rc = &cpi->rc;
2605   int target_rate = rc->base_frame_target;
2606 
2607   if (cpi->common.frame_type == KEY_FRAME)
2608     target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
2609   else
2610     target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2611 
2612   if (!cpi->oxcf.vbr_corpus_complexity) {
2613     // Correction to rate target based on prior over or under shoot.
2614     if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
2615       vbr_rate_correction(cpi, &target_rate);
2616   }
2617   vp9_rc_set_frame_target(cpi, target_rate);
2618 }
2619 
2620 // Check if we should resize, based on average QP from past x frames.
2621 // Only allow for resize at most one scale down for now, scaling factor is 2.
vp9_resize_one_pass_cbr(VP9_COMP * cpi)2622 int vp9_resize_one_pass_cbr(VP9_COMP *cpi) {
2623   const VP9_COMMON *const cm = &cpi->common;
2624   RATE_CONTROL *const rc = &cpi->rc;
2625   RESIZE_ACTION resize_action = NO_RESIZE;
2626   int avg_qp_thr1 = 70;
2627   int avg_qp_thr2 = 50;
2628   int min_width = 180;
2629   int min_height = 180;
2630   int down_size_on = 1;
2631   cpi->resize_scale_num = 1;
2632   cpi->resize_scale_den = 1;
2633   // Don't resize on key frame; reset the counters on key frame.
2634   if (cm->frame_type == KEY_FRAME) {
2635     cpi->resize_avg_qp = 0;
2636     cpi->resize_count = 0;
2637     return 0;
2638   }
2639   // Check current frame reslution to avoid generating frames smaller than
2640   // the minimum resolution.
2641   if (ONEHALFONLY_RESIZE) {
2642     if ((cm->width >> 1) < min_width || (cm->height >> 1) < min_height)
2643       down_size_on = 0;
2644   } else {
2645     if (cpi->resize_state == ORIG &&
2646         (cm->width * 3 / 4 < min_width || cm->height * 3 / 4 < min_height))
2647       return 0;
2648     else if (cpi->resize_state == THREE_QUARTER &&
2649              ((cpi->oxcf.width >> 1) < min_width ||
2650               (cpi->oxcf.height >> 1) < min_height))
2651       down_size_on = 0;
2652   }
2653 
2654 #if CONFIG_VP9_TEMPORAL_DENOISING
2655   // If denoiser is on, apply a smaller qp threshold.
2656   if (cpi->oxcf.noise_sensitivity > 0) {
2657     avg_qp_thr1 = 60;
2658     avg_qp_thr2 = 40;
2659   }
2660 #endif
2661 
2662   // Resize based on average buffer underflow and QP over some window.
2663   // Ignore samples close to key frame, since QP is usually high after key.
2664   if (cpi->rc.frames_since_key > 2 * cpi->framerate) {
2665     const int window = (int)(4 * cpi->framerate);
2666     cpi->resize_avg_qp += cm->base_qindex;
2667     if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100))
2668       ++cpi->resize_buffer_underflow;
2669     ++cpi->resize_count;
2670     // Check for resize action every "window" frames.
2671     if (cpi->resize_count >= window) {
2672       int avg_qp = cpi->resize_avg_qp / cpi->resize_count;
2673       // Resize down if buffer level has underflowed sufficient amount in past
2674       // window, and we are at original or 3/4 of original resolution.
2675       // Resize back up if average QP is low, and we are currently in a resized
2676       // down state, i.e. 1/2 or 3/4 of original resolution.
2677       // Currently, use a flag to turn 3/4 resizing feature on/off.
2678       if (cpi->resize_buffer_underflow > (cpi->resize_count >> 2)) {
2679         if (cpi->resize_state == THREE_QUARTER && down_size_on) {
2680           resize_action = DOWN_ONEHALF;
2681           cpi->resize_state = ONE_HALF;
2682         } else if (cpi->resize_state == ORIG) {
2683           resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
2684           cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
2685         }
2686       } else if (cpi->resize_state != ORIG &&
2687                  avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
2688         if (cpi->resize_state == THREE_QUARTER ||
2689             avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100 ||
2690             ONEHALFONLY_RESIZE) {
2691           resize_action = UP_ORIG;
2692           cpi->resize_state = ORIG;
2693         } else if (cpi->resize_state == ONE_HALF) {
2694           resize_action = UP_THREEFOUR;
2695           cpi->resize_state = THREE_QUARTER;
2696         }
2697       }
2698       // Reset for next window measurement.
2699       cpi->resize_avg_qp = 0;
2700       cpi->resize_count = 0;
2701       cpi->resize_buffer_underflow = 0;
2702     }
2703   }
2704   // If decision is to resize, reset some quantities, and check is we should
2705   // reduce rate correction factor,
2706   if (resize_action != NO_RESIZE) {
2707     int target_bits_per_frame;
2708     int active_worst_quality;
2709     int qindex;
2710     int tot_scale_change;
2711     if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
2712       cpi->resize_scale_num = 3;
2713       cpi->resize_scale_den = 4;
2714     } else if (resize_action == DOWN_ONEHALF) {
2715       cpi->resize_scale_num = 1;
2716       cpi->resize_scale_den = 2;
2717     } else {  // UP_ORIG or anything else
2718       cpi->resize_scale_num = 1;
2719       cpi->resize_scale_den = 1;
2720     }
2721     tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) /
2722                        (cpi->resize_scale_num * cpi->resize_scale_num);
2723     // Reset buffer level to optimal, update target size.
2724     rc->buffer_level = rc->optimal_buffer_level;
2725     rc->bits_off_target = rc->optimal_buffer_level;
2726     rc->this_frame_target = calc_pframe_target_size_one_pass_cbr(cpi);
2727     // Get the projected qindex, based on the scaled target frame size (scaled
2728     // so target_bits_per_mb in vp9_rc_regulate_q will be correct target).
2729     target_bits_per_frame = (resize_action >= 0)
2730                                 ? rc->this_frame_target * tot_scale_change
2731                                 : rc->this_frame_target / tot_scale_change;
2732     active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
2733     qindex = vp9_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality,
2734                                active_worst_quality);
2735     // If resize is down, check if projected q index is close to worst_quality,
2736     // and if so, reduce the rate correction factor (since likely can afford
2737     // lower q for resized frame).
2738     if (resize_action > 0 && qindex > 90 * cpi->rc.worst_quality / 100) {
2739       rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
2740     }
2741     // If resize is back up, check if projected q index is too much above the
2742     // current base_qindex, and if so, reduce the rate correction factor
2743     // (since prefer to keep q for resized frame at least close to previous q).
2744     if (resize_action < 0 && qindex > 130 * cm->base_qindex / 100) {
2745       rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
2746     }
2747   }
2748   return resize_action;
2749 }
2750 
adjust_gf_boost_lag_one_pass_vbr(VP9_COMP * cpi,uint64_t avg_sad_current)2751 static void adjust_gf_boost_lag_one_pass_vbr(VP9_COMP *cpi,
2752                                              uint64_t avg_sad_current) {
2753   VP9_COMMON *const cm = &cpi->common;
2754   RATE_CONTROL *const rc = &cpi->rc;
2755   int target;
2756   int found = 0;
2757   int found2 = 0;
2758   int frame;
2759   int i;
2760   uint64_t avg_source_sad_lag = avg_sad_current;
2761   int high_source_sad_lagindex = -1;
2762   int steady_sad_lagindex = -1;
2763   uint32_t sad_thresh1 = 70000;
2764   uint32_t sad_thresh2 = 120000;
2765   int low_content = 0;
2766   int high_content = 0;
2767   double rate_err = 1.0;
2768   // Get measure of complexity over the future frames, and get the first
2769   // future frame with high_source_sad/scene-change.
2770   int tot_frames = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
2771   for (frame = tot_frames; frame >= 1; --frame) {
2772     const int lagframe_idx = tot_frames - frame + 1;
2773     uint64_t reference_sad = rc->avg_source_sad[0];
2774     for (i = 1; i < lagframe_idx; ++i) {
2775       if (rc->avg_source_sad[i] > 0)
2776         reference_sad = (3 * reference_sad + rc->avg_source_sad[i]) >> 2;
2777     }
2778     // Detect up-coming scene change.
2779     if (!found &&
2780         (rc->avg_source_sad[lagframe_idx] >
2781              VPXMAX(sad_thresh1, (unsigned int)(reference_sad << 1)) ||
2782          rc->avg_source_sad[lagframe_idx] >
2783              VPXMAX(3 * sad_thresh1 >> 2,
2784                     (unsigned int)(reference_sad << 2)))) {
2785       high_source_sad_lagindex = lagframe_idx;
2786       found = 1;
2787     }
2788     // Detect change from motion to steady.
2789     if (!found2 && lagframe_idx > 1 && lagframe_idx < tot_frames &&
2790         rc->avg_source_sad[lagframe_idx - 1] > (sad_thresh1 >> 2)) {
2791       found2 = 1;
2792       for (i = lagframe_idx; i < tot_frames; ++i) {
2793         if (!(rc->avg_source_sad[i] > 0 &&
2794               rc->avg_source_sad[i] < (sad_thresh1 >> 2) &&
2795               rc->avg_source_sad[i] <
2796                   (rc->avg_source_sad[lagframe_idx - 1] >> 1))) {
2797           found2 = 0;
2798           i = tot_frames;
2799         }
2800       }
2801       if (found2) steady_sad_lagindex = lagframe_idx;
2802     }
2803     avg_source_sad_lag += rc->avg_source_sad[lagframe_idx];
2804   }
2805   if (tot_frames > 0) avg_source_sad_lag = avg_source_sad_lag / tot_frames;
2806   // Constrain distance between detected scene cuts.
2807   if (high_source_sad_lagindex != -1 &&
2808       high_source_sad_lagindex != rc->high_source_sad_lagindex - 1 &&
2809       abs(high_source_sad_lagindex - rc->high_source_sad_lagindex) < 4)
2810     rc->high_source_sad_lagindex = -1;
2811   else
2812     rc->high_source_sad_lagindex = high_source_sad_lagindex;
2813   // Adjust some factors for the next GF group, ignore initial key frame,
2814   // and only for lag_in_frames not too small.
2815   if (cpi->refresh_golden_frame == 1 && cm->current_video_frame > 30 &&
2816       cpi->oxcf.lag_in_frames > 8) {
2817     int frame_constraint;
2818     if (rc->rolling_target_bits > 0)
2819       rate_err =
2820           (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
2821     high_content = high_source_sad_lagindex != -1 ||
2822                    avg_source_sad_lag > (rc->prev_avg_source_sad_lag << 1) ||
2823                    avg_source_sad_lag > sad_thresh2;
2824     low_content = high_source_sad_lagindex == -1 &&
2825                   ((avg_source_sad_lag < (rc->prev_avg_source_sad_lag >> 1)) ||
2826                    (avg_source_sad_lag < sad_thresh1));
2827     if (low_content) {
2828       rc->gfu_boost = DEFAULT_GF_BOOST;
2829       rc->baseline_gf_interval =
2830           VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
2831     } else if (high_content) {
2832       rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
2833       rc->baseline_gf_interval = (rate_err > 3.0)
2834                                      ? VPXMAX(10, rc->baseline_gf_interval >> 1)
2835                                      : VPXMAX(6, rc->baseline_gf_interval >> 1);
2836     }
2837     if (rc->baseline_gf_interval > cpi->oxcf.lag_in_frames - 1)
2838       rc->baseline_gf_interval = cpi->oxcf.lag_in_frames - 1;
2839     // Check for constraining gf_interval for up-coming scene/content changes,
2840     // or for up-coming key frame, whichever is closer.
2841     frame_constraint = rc->frames_to_key;
2842     if (rc->high_source_sad_lagindex > 0 &&
2843         frame_constraint > rc->high_source_sad_lagindex)
2844       frame_constraint = rc->high_source_sad_lagindex;
2845     if (steady_sad_lagindex > 3 && frame_constraint > steady_sad_lagindex)
2846       frame_constraint = steady_sad_lagindex;
2847     adjust_gfint_frame_constraint(cpi, frame_constraint);
2848     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2849     // Adjust factors for active_worst setting & af_ratio for next gf interval.
2850     rc->fac_active_worst_inter = 150;  // corresponds to 3/2 (= 150 /100).
2851     rc->fac_active_worst_gf = 100;
2852     if (rate_err < 2.0 && !high_content) {
2853       rc->fac_active_worst_inter = 120;
2854       rc->fac_active_worst_gf = 90;
2855     } else if (rate_err > 8.0 && rc->avg_frame_qindex[INTER_FRAME] < 16) {
2856       // Increase active_worst faster at low Q if rate fluctuation is high.
2857       rc->fac_active_worst_inter = 200;
2858       if (rc->avg_frame_qindex[INTER_FRAME] < 8)
2859         rc->fac_active_worst_inter = 400;
2860     }
2861     if (low_content && rc->avg_frame_low_motion > 80) {
2862       rc->af_ratio_onepass_vbr = 15;
2863     } else if (high_content || rc->avg_frame_low_motion < 30) {
2864       rc->af_ratio_onepass_vbr = 5;
2865       rc->gfu_boost = DEFAULT_GF_BOOST >> 2;
2866     }
2867     if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
2868       // Flag to disable usage of ARF based on past usage, only allow this
2869       // disabling if current frame/group does not start with key frame or
2870       // scene cut. Note perc_arf_usage is only computed for speed >= 5.
2871       int arf_usage_low =
2872           (cm->frame_type != KEY_FRAME && !rc->high_source_sad &&
2873            cpi->rc.perc_arf_usage < 15 && cpi->oxcf.speed >= 5);
2874       // Don't use alt-ref for this group under certain conditions.
2875       if (arf_usage_low ||
2876           (rc->high_source_sad_lagindex > 0 &&
2877            rc->high_source_sad_lagindex <= rc->frames_till_gf_update_due) ||
2878           (avg_source_sad_lag > 3 * sad_thresh1 >> 3)) {
2879         rc->source_alt_ref_pending = 0;
2880         rc->alt_ref_gf_group = 0;
2881       } else {
2882         rc->source_alt_ref_pending = 1;
2883         rc->alt_ref_gf_group = 1;
2884         // If alt-ref is used for this gf group, limit the interval.
2885         if (rc->baseline_gf_interval > 12) {
2886           rc->baseline_gf_interval = 12;
2887           rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2888         }
2889       }
2890     }
2891     target = calc_pframe_target_size_one_pass_vbr(cpi);
2892     vp9_rc_set_frame_target(cpi, target);
2893   }
2894   rc->prev_avg_source_sad_lag = avg_source_sad_lag;
2895 }
2896 
2897 // Compute average source sad (temporal sad: between current source and
2898 // previous source) over a subset of superblocks. Use this is detect big changes
2899 // in content and allow rate control to react.
2900 // This function also handles special case of lag_in_frames, to measure content
2901 // level in #future frames set by the lag_in_frames.
vp9_scene_detection_onepass(VP9_COMP * cpi)2902 void vp9_scene_detection_onepass(VP9_COMP *cpi) {
2903   VP9_COMMON *const cm = &cpi->common;
2904   RATE_CONTROL *const rc = &cpi->rc;
2905   YV12_BUFFER_CONFIG const *unscaled_src = cpi->un_scaled_source;
2906   YV12_BUFFER_CONFIG const *unscaled_last_src = cpi->unscaled_last_source;
2907   uint8_t *src_y;
2908   int src_ystride;
2909   int src_width;
2910   int src_height;
2911   uint8_t *last_src_y;
2912   int last_src_ystride;
2913   int last_src_width;
2914   int last_src_height;
2915   if (cpi->un_scaled_source == NULL || cpi->unscaled_last_source == NULL ||
2916       (cpi->use_svc && cpi->svc.current_superframe == 0))
2917     return;
2918   src_y = unscaled_src->y_buffer;
2919   src_ystride = unscaled_src->y_stride;
2920   src_width = unscaled_src->y_width;
2921   src_height = unscaled_src->y_height;
2922   last_src_y = unscaled_last_src->y_buffer;
2923   last_src_ystride = unscaled_last_src->y_stride;
2924   last_src_width = unscaled_last_src->y_width;
2925   last_src_height = unscaled_last_src->y_height;
2926 #if CONFIG_VP9_HIGHBITDEPTH
2927   if (cm->use_highbitdepth) return;
2928 #endif
2929   rc->high_source_sad = 0;
2930   rc->high_num_blocks_with_motion = 0;
2931   // For SVC: scene detection is only checked on first spatial layer of
2932   // the superframe using the original/unscaled resolutions.
2933   if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode &&
2934       src_width == last_src_width && src_height == last_src_height) {
2935     YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = { NULL };
2936     int num_mi_cols = cm->mi_cols;
2937     int num_mi_rows = cm->mi_rows;
2938     int start_frame = 0;
2939     int frames_to_buffer = 1;
2940     int frame = 0;
2941     int scene_cut_force_key_frame = 0;
2942     int num_zero_temp_sad = 0;
2943     uint64_t avg_sad_current = 0;
2944     uint32_t min_thresh = 10000;
2945     float thresh = 8.0f;
2946     uint32_t thresh_key = 140000;
2947     if (cpi->oxcf.speed <= 5) thresh_key = 240000;
2948     if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) min_thresh = 65000;
2949     if (cpi->oxcf.rc_mode == VPX_VBR) thresh = 2.1f;
2950     if (cpi->use_svc && cpi->svc.number_spatial_layers > 1) {
2951       const int aligned_width = ALIGN_POWER_OF_TWO(src_width, MI_SIZE_LOG2);
2952       const int aligned_height = ALIGN_POWER_OF_TWO(src_height, MI_SIZE_LOG2);
2953       num_mi_cols = aligned_width >> MI_SIZE_LOG2;
2954       num_mi_rows = aligned_height >> MI_SIZE_LOG2;
2955     }
2956     if (cpi->oxcf.lag_in_frames > 0) {
2957       frames_to_buffer = (cm->current_video_frame == 1)
2958                              ? (int)vp9_lookahead_depth(cpi->lookahead) - 1
2959                              : 2;
2960       start_frame = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
2961       for (frame = 0; frame < frames_to_buffer; ++frame) {
2962         const int lagframe_idx = start_frame - frame;
2963         if (lagframe_idx >= 0) {
2964           struct lookahead_entry *buf =
2965               vp9_lookahead_peek(cpi->lookahead, lagframe_idx);
2966           frames[frame] = &buf->img;
2967         }
2968       }
2969       // The avg_sad for this current frame is the value of frame#1
2970       // (first future frame) from previous frame.
2971       avg_sad_current = rc->avg_source_sad[1];
2972       if (avg_sad_current >
2973               VPXMAX(min_thresh,
2974                      (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
2975           cm->current_video_frame > (unsigned int)cpi->oxcf.lag_in_frames)
2976         rc->high_source_sad = 1;
2977       else
2978         rc->high_source_sad = 0;
2979       if (rc->high_source_sad && avg_sad_current > thresh_key)
2980         scene_cut_force_key_frame = 1;
2981       // Update recursive average for current frame.
2982       if (avg_sad_current > 0)
2983         rc->avg_source_sad[0] =
2984             (3 * rc->avg_source_sad[0] + avg_sad_current) >> 2;
2985       // Shift back data, starting at frame#1.
2986       for (frame = 1; frame < cpi->oxcf.lag_in_frames - 1; ++frame)
2987         rc->avg_source_sad[frame] = rc->avg_source_sad[frame + 1];
2988     }
2989     for (frame = 0; frame < frames_to_buffer; ++frame) {
2990       if (cpi->oxcf.lag_in_frames == 0 ||
2991           (frames[frame] != NULL && frames[frame + 1] != NULL &&
2992            frames[frame]->y_width == frames[frame + 1]->y_width &&
2993            frames[frame]->y_height == frames[frame + 1]->y_height)) {
2994         int sbi_row, sbi_col;
2995         const int lagframe_idx =
2996             (cpi->oxcf.lag_in_frames == 0) ? 0 : start_frame - frame + 1;
2997         const BLOCK_SIZE bsize = BLOCK_64X64;
2998         // Loop over sub-sample of frame, compute average sad over 64x64 blocks.
2999         uint64_t avg_sad = 0;
3000         uint64_t tmp_sad = 0;
3001         int num_samples = 0;
3002         int sb_cols = (num_mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
3003         int sb_rows = (num_mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
3004         if (cpi->oxcf.lag_in_frames > 0) {
3005           src_y = frames[frame]->y_buffer;
3006           src_ystride = frames[frame]->y_stride;
3007           last_src_y = frames[frame + 1]->y_buffer;
3008           last_src_ystride = frames[frame + 1]->y_stride;
3009         }
3010         num_zero_temp_sad = 0;
3011         for (sbi_row = 0; sbi_row < sb_rows; ++sbi_row) {
3012           for (sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
3013             // Checker-board pattern, ignore boundary.
3014             if (((sbi_row > 0 && sbi_col > 0) &&
3015                  (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) &&
3016                  ((sbi_row % 2 == 0 && sbi_col % 2 == 0) ||
3017                   (sbi_row % 2 != 0 && sbi_col % 2 != 0)))) {
3018               tmp_sad = cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y,
3019                                                last_src_ystride);
3020               avg_sad += tmp_sad;
3021               num_samples++;
3022               if (tmp_sad == 0) num_zero_temp_sad++;
3023             }
3024             src_y += 64;
3025             last_src_y += 64;
3026           }
3027           src_y += (src_ystride << 6) - (sb_cols << 6);
3028           last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
3029         }
3030         if (num_samples > 0) avg_sad = avg_sad / num_samples;
3031         // Set high_source_sad flag if we detect very high increase in avg_sad
3032         // between current and previous frame value(s). Use minimum threshold
3033         // for cases where there is small change from content that is completely
3034         // static.
3035         if (lagframe_idx == 0) {
3036           if (avg_sad >
3037                   VPXMAX(min_thresh,
3038                          (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
3039               rc->frames_since_key > 1 + cpi->svc.number_spatial_layers &&
3040               num_zero_temp_sad < 3 * (num_samples >> 2))
3041             rc->high_source_sad = 1;
3042           else
3043             rc->high_source_sad = 0;
3044           if (rc->high_source_sad && avg_sad > thresh_key)
3045             scene_cut_force_key_frame = 1;
3046           if (avg_sad > 0 || cpi->oxcf.rc_mode == VPX_CBR)
3047             rc->avg_source_sad[0] = (3 * rc->avg_source_sad[0] + avg_sad) >> 2;
3048         } else {
3049           rc->avg_source_sad[lagframe_idx] = avg_sad;
3050         }
3051         if (num_zero_temp_sad < (3 * num_samples >> 2))
3052           rc->high_num_blocks_with_motion = 1;
3053       }
3054     }
3055     // For CBR non-screen content mode, check if we should reset the rate
3056     // control. Reset is done if high_source_sad is detected and the rate
3057     // control is at very low QP with rate correction factor at min level.
3058     if (cpi->oxcf.rc_mode == VPX_CBR &&
3059         cpi->oxcf.content != VP9E_CONTENT_SCREEN && !cpi->use_svc) {
3060       if (rc->high_source_sad && rc->last_q[INTER_FRAME] == rc->best_quality &&
3061           rc->avg_frame_qindex[INTER_FRAME] < (rc->best_quality << 1) &&
3062           rc->rate_correction_factors[INTER_NORMAL] == MIN_BPB_FACTOR) {
3063         rc->rate_correction_factors[INTER_NORMAL] = 0.5;
3064         rc->avg_frame_qindex[INTER_FRAME] = rc->worst_quality;
3065         rc->buffer_level = rc->optimal_buffer_level;
3066         rc->bits_off_target = rc->optimal_buffer_level;
3067         rc->reset_high_source_sad = 1;
3068       }
3069       if (cm->frame_type != KEY_FRAME && rc->reset_high_source_sad)
3070         rc->this_frame_target = rc->avg_frame_bandwidth;
3071     }
3072     // For SVC the new (updated) avg_source_sad[0] for the current superframe
3073     // updates the setting for all layers.
3074     if (cpi->use_svc) {
3075       int sl, tl;
3076       SVC *const svc = &cpi->svc;
3077       for (sl = 0; sl < svc->number_spatial_layers; ++sl)
3078         for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
3079           int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3080           LAYER_CONTEXT *const lc = &svc->layer_context[layer];
3081           RATE_CONTROL *const lrc = &lc->rc;
3082           lrc->avg_source_sad[0] = rc->avg_source_sad[0];
3083         }
3084     }
3085     // For VBR, under scene change/high content change, force golden refresh.
3086     if (cpi->oxcf.rc_mode == VPX_VBR && cm->frame_type != KEY_FRAME &&
3087         rc->high_source_sad && rc->frames_to_key > 3 &&
3088         rc->count_last_scene_change > 4 &&
3089         cpi->ext_refresh_frame_flags_pending == 0) {
3090       int target;
3091       cpi->refresh_golden_frame = 1;
3092       if (scene_cut_force_key_frame) cm->frame_type = KEY_FRAME;
3093       rc->source_alt_ref_pending = 0;
3094       if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf)
3095         rc->source_alt_ref_pending = 1;
3096       rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
3097       rc->baseline_gf_interval =
3098           VPXMIN(20, VPXMAX(10, rc->baseline_gf_interval));
3099       adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
3100       rc->frames_till_gf_update_due = rc->baseline_gf_interval;
3101       target = calc_pframe_target_size_one_pass_vbr(cpi);
3102       vp9_rc_set_frame_target(cpi, target);
3103       rc->count_last_scene_change = 0;
3104     } else {
3105       rc->count_last_scene_change++;
3106     }
3107     // If lag_in_frame is used, set the gf boost and interval.
3108     if (cpi->oxcf.lag_in_frames > 0)
3109       adjust_gf_boost_lag_one_pass_vbr(cpi, avg_sad_current);
3110   }
3111 }
3112 
3113 // Test if encoded frame will significantly overshoot the target bitrate, and
3114 // if so, set the QP, reset/adjust some rate control parameters, and return 1.
3115 // frame_size = -1 means frame has not been encoded.
vp9_encodedframe_overshoot(VP9_COMP * cpi,int frame_size,int * q)3116 int vp9_encodedframe_overshoot(VP9_COMP *cpi, int frame_size, int *q) {
3117   VP9_COMMON *const cm = &cpi->common;
3118   RATE_CONTROL *const rc = &cpi->rc;
3119   SPEED_FEATURES *const sf = &cpi->sf;
3120   int thresh_qp = 7 * (rc->worst_quality >> 3);
3121   int thresh_rate = rc->avg_frame_bandwidth << 3;
3122   // Lower thresh_qp for video (more overshoot at lower Q) to be
3123   // more conservative for video.
3124   if (cpi->oxcf.content != VP9E_CONTENT_SCREEN)
3125     thresh_qp = 3 * (rc->worst_quality >> 2);
3126   // If this decision is not based on an encoded frame size but just on
3127   // scene/slide change detection (i.e., re_encode_overshoot_cbr_rt ==
3128   // FAST_DETECTION_MAXQ), for now skip the (frame_size > thresh_rate)
3129   // condition in this case.
3130   // TODO(marpan): Use a better size/rate condition for this case and
3131   // adjust thresholds.
3132   if ((sf->overshoot_detection_cbr_rt == FAST_DETECTION_MAXQ ||
3133        frame_size > thresh_rate) &&
3134       cm->base_qindex < thresh_qp) {
3135     double rate_correction_factor =
3136         cpi->rc.rate_correction_factors[INTER_NORMAL];
3137     const int target_size = cpi->rc.avg_frame_bandwidth;
3138     double new_correction_factor;
3139     int target_bits_per_mb;
3140     double q2;
3141     int enumerator;
3142     // Force a re-encode, and for now use max-QP.
3143     *q = cpi->rc.worst_quality;
3144     cpi->cyclic_refresh->counter_encode_maxq_scene_change = 0;
3145     cpi->rc.re_encode_maxq_scene_change = 1;
3146     // If the frame_size is much larger than the threshold (big content change)
3147     // and the encoded frame used alot of Intra modes, then force hybrid_intra
3148     // encoding for the re-encode on this scene change. hybrid_intra will
3149     // use rd-based intra mode selection for small blocks.
3150     if (sf->overshoot_detection_cbr_rt == RE_ENCODE_MAXQ &&
3151         frame_size > (thresh_rate << 1) && cpi->svc.spatial_layer_id == 0) {
3152       MODE_INFO **mi = cm->mi_grid_visible;
3153       int sum_intra_usage = 0;
3154       int mi_row, mi_col;
3155       int tot = 0;
3156       for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
3157         for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
3158           if (mi[0]->ref_frame[0] == INTRA_FRAME) sum_intra_usage++;
3159           tot++;
3160           mi++;
3161         }
3162         mi += 8;
3163       }
3164       sum_intra_usage = 100 * sum_intra_usage / (cm->mi_rows * cm->mi_cols);
3165       if (sum_intra_usage > 60) cpi->rc.hybrid_intra_scene_change = 1;
3166     }
3167     // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
3168     // these parameters will affect QP selection for subsequent frames. If they
3169     // have settled down to a very different (low QP) state, then not adjusting
3170     // them may cause next frame to select low QP and overshoot again.
3171     cpi->rc.avg_frame_qindex[INTER_FRAME] = *q;
3172     rc->buffer_level = rc->optimal_buffer_level;
3173     rc->bits_off_target = rc->optimal_buffer_level;
3174     // Reset rate under/over-shoot flags.
3175     cpi->rc.rc_1_frame = 0;
3176     cpi->rc.rc_2_frame = 0;
3177     // Adjust rate correction factor.
3178     target_bits_per_mb =
3179         (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->MBs);
3180     // Rate correction factor based on target_bits_per_mb and qp (==max_QP).
3181     // This comes from the inverse computation of vp9_rc_bits_per_mb().
3182     q2 = vp9_convert_qindex_to_q(*q, cm->bit_depth);
3183     enumerator = 1800000;  // Factor for inter frame.
3184     enumerator += (int)(enumerator * q2) >> 12;
3185     new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
3186     if (new_correction_factor > rate_correction_factor) {
3187       rate_correction_factor =
3188           VPXMIN(2.0 * rate_correction_factor, new_correction_factor);
3189       if (rate_correction_factor > MAX_BPB_FACTOR)
3190         rate_correction_factor = MAX_BPB_FACTOR;
3191       cpi->rc.rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
3192     }
3193     // For temporal layers, reset the rate control parametes across all
3194     // temporal layers. If the first_spatial_layer_to_encode > 0, then this
3195     // superframe has skipped lower base layers. So in this case we should also
3196     // reset and force max-q for spatial layers < first_spatial_layer_to_encode.
3197     if (cpi->use_svc) {
3198       int tl = 0;
3199       int sl = 0;
3200       SVC *svc = &cpi->svc;
3201       for (sl = 0; sl < svc->first_spatial_layer_to_encode; ++sl) {
3202         for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
3203           const int layer =
3204               LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3205           LAYER_CONTEXT *lc = &svc->layer_context[layer];
3206           RATE_CONTROL *lrc = &lc->rc;
3207           lrc->avg_frame_qindex[INTER_FRAME] = *q;
3208           lrc->buffer_level = lrc->optimal_buffer_level;
3209           lrc->bits_off_target = lrc->optimal_buffer_level;
3210           lrc->rc_1_frame = 0;
3211           lrc->rc_2_frame = 0;
3212           lrc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
3213           lrc->force_max_q = 1;
3214         }
3215       }
3216     }
3217     return 1;
3218   } else {
3219     return 0;
3220   }
3221 }
3222