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