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