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