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