]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_ratectrl.c
vp9: Update to scene/content change detection.
[libvpx] / vp9 / encoder / vp9_ratectrl.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <assert.h>
12 #include <limits.h>
13 #include <math.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "vpx_dsp/vpx_dsp_common.h"
19 #include "vpx_mem/vpx_mem.h"
20 #include "vpx_ports/mem.h"
21 #include "vpx_ports/system_state.h"
22
23 #include "vp9/common/vp9_alloccommon.h"
24 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
25 #include "vp9/common/vp9_common.h"
26 #include "vp9/common/vp9_entropymode.h"
27 #include "vp9/common/vp9_quant_common.h"
28 #include "vp9/common/vp9_seg_common.h"
29
30 #include "vp9/encoder/vp9_encodemv.h"
31 #include "vp9/encoder/vp9_ratectrl.h"
32
33 // Max rate target for 1080P and below encodes under normal circumstances
34 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
35 #define MAX_MB_RATE 250
36 #define MAXRATE_1080P 2025000
37
38 #define DEFAULT_KF_BOOST 2000
39 #define DEFAULT_GF_BOOST 2000
40
41 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
42
43 #define MIN_BPB_FACTOR 0.005
44 #define MAX_BPB_FACTOR 50
45
46 #define FRAME_OVERHEAD_BITS 200
47
48 #if CONFIG_VP9_HIGHBITDEPTH
49 #define ASSIGN_MINQ_TABLE(bit_depth, name) \
50   do { \
51     switch (bit_depth) { \
52       case VPX_BITS_8: \
53         name = name##_8; \
54         break; \
55       case VPX_BITS_10: \
56         name = name##_10; \
57         break; \
58       case VPX_BITS_12: \
59         name = name##_12; \
60         break; \
61       default: \
62         assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10" \
63                     " or VPX_BITS_12"); \
64         name = NULL; \
65     } \
66   } while (0)
67 #else
68 #define ASSIGN_MINQ_TABLE(bit_depth, name) \
69   do { \
70     (void) bit_depth; \
71     name = name##_8; \
72   } while (0)
73 #endif
74
75 // Tables relating active max Q to active min Q
76 static int kf_low_motion_minq_8[QINDEX_RANGE];
77 static int kf_high_motion_minq_8[QINDEX_RANGE];
78 static int arfgf_low_motion_minq_8[QINDEX_RANGE];
79 static int arfgf_high_motion_minq_8[QINDEX_RANGE];
80 static int inter_minq_8[QINDEX_RANGE];
81 static int rtc_minq_8[QINDEX_RANGE];
82
83 #if CONFIG_VP9_HIGHBITDEPTH
84 static int kf_low_motion_minq_10[QINDEX_RANGE];
85 static int kf_high_motion_minq_10[QINDEX_RANGE];
86 static int arfgf_low_motion_minq_10[QINDEX_RANGE];
87 static int arfgf_high_motion_minq_10[QINDEX_RANGE];
88 static int inter_minq_10[QINDEX_RANGE];
89 static int rtc_minq_10[QINDEX_RANGE];
90 static int kf_low_motion_minq_12[QINDEX_RANGE];
91 static int kf_high_motion_minq_12[QINDEX_RANGE];
92 static int arfgf_low_motion_minq_12[QINDEX_RANGE];
93 static int arfgf_high_motion_minq_12[QINDEX_RANGE];
94 static int inter_minq_12[QINDEX_RANGE];
95 static int rtc_minq_12[QINDEX_RANGE];
96 #endif
97
98 static int gf_high = 2000;
99 static int gf_low = 400;
100 static int kf_high = 5000;
101 static int kf_low = 400;
102
103 // Functions to compute the active minq lookup table entries based on a
104 // formulaic approach to facilitate easier adjustment of the Q tables.
105 // The formulae were derived from computing a 3rd order polynomial best
106 // fit to the original data (after plotting real maxq vs minq (not q index))
107 static int get_minq_index(double maxq, double x3, double x2, double x1,
108                           vpx_bit_depth_t bit_depth) {
109   int i;
110   const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq,
111                                    maxq);
112
113   // Special case handling to deal with the step from q2.0
114   // down to lossless mode represented by q 1.0.
115   if (minqtarget <= 2.0)
116     return 0;
117
118   for (i = 0; i < QINDEX_RANGE; i++) {
119     if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth))
120       return i;
121   }
122
123   return QINDEX_RANGE - 1;
124 }
125
126 static void init_minq_luts(int *kf_low_m, int *kf_high_m,
127                            int *arfgf_low, int *arfgf_high,
128                            int *inter, int *rtc, vpx_bit_depth_t bit_depth) {
129   int i;
130   for (i = 0; i < QINDEX_RANGE; i++) {
131     const double maxq = vp9_convert_qindex_to_q(i, bit_depth);
132     kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
133     kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
134     arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
135     arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
136     inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth);
137     rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
138   }
139 }
140
141 void vp9_rc_init_minq_luts(void) {
142   init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
143                  arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
144                  inter_minq_8, rtc_minq_8, VPX_BITS_8);
145 #if CONFIG_VP9_HIGHBITDEPTH
146   init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
147                  arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
148                  inter_minq_10, rtc_minq_10, VPX_BITS_10);
149   init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
150                  arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
151                  inter_minq_12, rtc_minq_12, VPX_BITS_12);
152 #endif
153 }
154
155 // These functions use formulaic calculations to make playing with the
156 // quantizer tables easier. If necessary they can be replaced by lookup
157 // tables if and when things settle down in the experimental bitstream
158 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) {
159   // Convert the index to a real Q value (scaled down to match old Q values)
160 #if CONFIG_VP9_HIGHBITDEPTH
161   switch (bit_depth) {
162     case VPX_BITS_8:
163       return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
164     case VPX_BITS_10:
165       return vp9_ac_quant(qindex, 0, bit_depth) / 16.0;
166     case VPX_BITS_12:
167       return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
168     default:
169       assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
170       return -1.0;
171   }
172 #else
173   return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
174 #endif
175 }
176
177 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
178                        double correction_factor,
179                        vpx_bit_depth_t bit_depth) {
180   const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
181   int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
182
183   assert(correction_factor <= MAX_BPB_FACTOR &&
184          correction_factor >= MIN_BPB_FACTOR);
185
186   // q based adjustment to baseline enumerator
187   enumerator += (int)(enumerator * q) >> 12;
188   return (int)(enumerator * correction_factor / q);
189 }
190
191 int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
192                            double correction_factor,
193                            vpx_bit_depth_t bit_depth) {
194   const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor,
195                                            bit_depth));
196   return VPXMAX(FRAME_OVERHEAD_BITS,
197                 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
198 }
199
200 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
201   const RATE_CONTROL *rc = &cpi->rc;
202   const VP9EncoderConfig *oxcf = &cpi->oxcf;
203   const int min_frame_target = VPXMAX(rc->min_frame_bandwidth,
204                                       rc->avg_frame_bandwidth >> 5);
205   if (target < min_frame_target)
206     target = min_frame_target;
207   if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
208     // If there is an active ARF at this location use the minimum
209     // bits on this frame even if it is a constructed arf.
210     // The active maximum quantizer insures that an appropriate
211     // number of bits will be spent if needed for constructed ARFs.
212     target = min_frame_target;
213   }
214   // Clip the frame target to the maximum allowed value.
215   if (target > rc->max_frame_bandwidth)
216     target = rc->max_frame_bandwidth;
217   if (oxcf->rc_max_inter_bitrate_pct) {
218     const int max_rate = rc->avg_frame_bandwidth *
219                          oxcf->rc_max_inter_bitrate_pct / 100;
220     target = VPXMIN(target, max_rate);
221   }
222   return target;
223 }
224
225 int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
226   const RATE_CONTROL *rc = &cpi->rc;
227   const VP9EncoderConfig *oxcf = &cpi->oxcf;
228   if (oxcf->rc_max_intra_bitrate_pct) {
229     const int max_rate = rc->avg_frame_bandwidth *
230                              oxcf->rc_max_intra_bitrate_pct / 100;
231     target = VPXMIN(target, max_rate);
232   }
233   if (target > rc->max_frame_bandwidth)
234     target = rc->max_frame_bandwidth;
235   return target;
236 }
237
238 // Update the buffer level for higher temporal layers, given the encoded current
239 // temporal layer.
240 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
241   int i = 0;
242   int current_temporal_layer = svc->temporal_layer_id;
243   for (i = current_temporal_layer + 1;
244       i < svc->number_temporal_layers; ++i) {
245     const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
246                                        svc->number_temporal_layers);
247     LAYER_CONTEXT *lc = &svc->layer_context[layer];
248     RATE_CONTROL *lrc = &lc->rc;
249     int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
250         encoded_frame_size);
251     lrc->bits_off_target += bits_off_for_this_layer;
252
253     // Clip buffer level to maximum buffer size for the layer.
254     lrc->bits_off_target =
255         VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
256     lrc->buffer_level = lrc->bits_off_target;
257   }
258 }
259
260 // Update the buffer level: leaky bucket model.
261 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
262   const VP9_COMMON *const cm = &cpi->common;
263   RATE_CONTROL *const rc = &cpi->rc;
264
265   // Non-viewable frames are a special case and are treated as pure overhead.
266   if (!cm->show_frame) {
267     rc->bits_off_target -= encoded_frame_size;
268   } else {
269     rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
270   }
271
272   // Clip the buffer level to the maximum specified buffer size.
273   rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
274
275   // For screen-content mode, and if frame-dropper is off, don't let buffer
276   // level go below threshold, given here as -rc->maximum_ buffer_size.
277   if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
278       cpi->oxcf.drop_frames_water_mark == 0)
279     rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size);
280
281   rc->buffer_level = rc->bits_off_target;
282
283   if (is_one_pass_cbr_svc(cpi)) {
284     update_layer_buffer_level(&cpi->svc, encoded_frame_size);
285   }
286 }
287
288 int vp9_rc_get_default_min_gf_interval(
289     int width, int height, double framerate) {
290   // Assume we do not need any constraint lower than 4K 20 fps
291   static const double factor_safe = 3840 * 2160 * 20.0;
292   const double factor = width * height * framerate;
293   const int default_interval =
294       clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
295
296   if (factor <= factor_safe)
297     return default_interval;
298   else
299     return VPXMAX(default_interval,
300                   (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
301   // Note this logic makes:
302   // 4K24: 5
303   // 4K30: 6
304   // 4K60: 12
305 }
306
307 int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
308   int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
309   interval += (interval & 0x01);  // Round to even value
310   return VPXMAX(interval, min_gf_interval);
311 }
312
313 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
314   int i;
315
316   if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
317     rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
318     rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
319   } else {
320     rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
321                                        oxcf->best_allowed_q) / 2;
322     rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
323                                          oxcf->best_allowed_q) / 2;
324   }
325
326   rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
327   rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
328
329   rc->buffer_level =    rc->starting_buffer_level;
330   rc->bits_off_target = rc->starting_buffer_level;
331
332   rc->rolling_target_bits      = rc->avg_frame_bandwidth;
333   rc->rolling_actual_bits      = rc->avg_frame_bandwidth;
334   rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
335   rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
336
337   rc->total_actual_bits = 0;
338   rc->total_target_bits = 0;
339   rc->total_target_vs_actual = 0;
340
341   rc->frames_since_key = 8;  // Sensible default for first frame.
342   rc->this_key_frame_forced = 0;
343   rc->next_key_frame_forced = 0;
344   rc->source_alt_ref_pending = 0;
345   rc->source_alt_ref_active = 0;
346
347   rc->frames_till_gf_update_due = 0;
348   rc->ni_av_qi = oxcf->worst_allowed_q;
349   rc->ni_tot_qi = 0;
350   rc->ni_frames = 0;
351
352   rc->tot_q = 0.0;
353   rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
354
355   for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
356     rc->rate_correction_factors[i] = 1.0;
357   }
358
359   rc->min_gf_interval = oxcf->min_gf_interval;
360   rc->max_gf_interval = oxcf->max_gf_interval;
361   if (rc->min_gf_interval == 0)
362     rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
363         oxcf->width, oxcf->height, oxcf->init_framerate);
364   if (rc->max_gf_interval == 0)
365     rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
366         oxcf->init_framerate, rc->min_gf_interval);
367   rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
368 }
369
370 int vp9_rc_drop_frame(VP9_COMP *cpi) {
371   const VP9EncoderConfig *oxcf = &cpi->oxcf;
372   RATE_CONTROL *const rc = &cpi->rc;
373   if (!oxcf->drop_frames_water_mark ||
374       (is_one_pass_cbr_svc(cpi) &&
375        cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode)) {
376     return 0;
377   } else {
378     if (rc->buffer_level < 0) {
379       // Always drop if buffer is below 0.
380       return 1;
381     } else {
382       // If buffer is below drop_mark, for now just drop every other frame
383       // (starting with the next frame) until it increases back over drop_mark.
384       int drop_mark = (int)(oxcf->drop_frames_water_mark *
385           rc->optimal_buffer_level / 100);
386       if ((rc->buffer_level > drop_mark) &&
387           (rc->decimation_factor > 0)) {
388         --rc->decimation_factor;
389       } else if (rc->buffer_level <= drop_mark &&
390           rc->decimation_factor == 0) {
391         rc->decimation_factor = 1;
392       }
393       if (rc->decimation_factor > 0) {
394         if (rc->decimation_count > 0) {
395           --rc->decimation_count;
396           return 1;
397         } else {
398           rc->decimation_count = rc->decimation_factor;
399           return 0;
400         }
401       } else {
402         rc->decimation_count = 0;
403         return 0;
404       }
405     }
406   }
407 }
408
409 static double get_rate_correction_factor(const VP9_COMP *cpi) {
410   const RATE_CONTROL *const rc = &cpi->rc;
411   double rcf;
412
413   if (cpi->common.frame_type == KEY_FRAME) {
414     rcf = rc->rate_correction_factors[KF_STD];
415   } else if (cpi->oxcf.pass == 2) {
416     RATE_FACTOR_LEVEL rf_lvl =
417       cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
418     rcf = rc->rate_correction_factors[rf_lvl];
419   } else {
420     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
421         !rc->is_src_frame_alt_ref && !cpi->use_svc &&
422         (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
423       rcf = rc->rate_correction_factors[GF_ARF_STD];
424     else
425       rcf = rc->rate_correction_factors[INTER_NORMAL];
426   }
427   rcf *= rcf_mult[rc->frame_size_selector];
428   return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
429 }
430
431 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
432   RATE_CONTROL *const rc = &cpi->rc;
433
434   // Normalize RCF to account for the size-dependent scaling factor.
435   factor /= rcf_mult[cpi->rc.frame_size_selector];
436
437   factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
438
439   if (cpi->common.frame_type == KEY_FRAME) {
440     rc->rate_correction_factors[KF_STD] = factor;
441   } else if (cpi->oxcf.pass == 2) {
442     RATE_FACTOR_LEVEL rf_lvl =
443       cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
444     rc->rate_correction_factors[rf_lvl] = factor;
445   } else {
446     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
447         !rc->is_src_frame_alt_ref && !cpi->use_svc &&
448         (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
449       rc->rate_correction_factors[GF_ARF_STD] = factor;
450     else
451       rc->rate_correction_factors[INTER_NORMAL] = factor;
452   }
453 }
454
455 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) {
456   const VP9_COMMON *const cm = &cpi->common;
457   int correction_factor = 100;
458   double rate_correction_factor = get_rate_correction_factor(cpi);
459   double adjustment_limit;
460
461   int projected_size_based_on_q = 0;
462
463   // Do not update the rate factors for arf overlay frames.
464   if (cpi->rc.is_src_frame_alt_ref)
465     return;
466
467   // Clear down mmx registers to allow floating point in what follows
468   vpx_clear_system_state();
469
470   // Work out how big we would have expected the frame to be at this Q given
471   // the current correction factor.
472   // Stay in double to avoid int overflow when values are large
473   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
474     projected_size_based_on_q =
475         vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
476   } else {
477     projected_size_based_on_q = vp9_estimate_bits_at_q(cpi->common.frame_type,
478                                                        cm->base_qindex,
479                                                        cm->MBs,
480                                                        rate_correction_factor,
481                                                        cm->bit_depth);
482   }
483   // Work out a size correction factor.
484   if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
485     correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
486                         projected_size_based_on_q);
487
488   // More heavily damped adjustment used if we have been oscillating either side
489   // of target.
490   adjustment_limit = 0.25 +
491       0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
492
493   cpi->rc.q_2_frame = cpi->rc.q_1_frame;
494   cpi->rc.q_1_frame = cm->base_qindex;
495   cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
496   if (correction_factor > 110)
497     cpi->rc.rc_1_frame = -1;
498   else if (correction_factor < 90)
499     cpi->rc.rc_1_frame = 1;
500   else
501     cpi->rc.rc_1_frame = 0;
502
503   // Turn off oscilation detection in the case of massive overshoot.
504   if (cpi->rc.rc_1_frame == -1 && cpi->rc.rc_2_frame == 1 &&
505       correction_factor > 1000) {
506     cpi->rc.rc_2_frame = 0;
507   }
508
509   if (correction_factor > 102) {
510     // We are not already at the worst allowable quality
511     correction_factor = (int)(100 + ((correction_factor - 100) *
512                                   adjustment_limit));
513     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
514     // Keep rate_correction_factor within limits
515     if (rate_correction_factor > MAX_BPB_FACTOR)
516       rate_correction_factor = MAX_BPB_FACTOR;
517   } else if (correction_factor < 99) {
518     // We are not already at the best allowable quality
519     correction_factor = (int)(100 - ((100 - correction_factor) *
520                                   adjustment_limit));
521     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
522
523     // Keep rate_correction_factor within limits
524     if (rate_correction_factor < MIN_BPB_FACTOR)
525       rate_correction_factor = MIN_BPB_FACTOR;
526   }
527
528   set_rate_correction_factor(cpi, rate_correction_factor);
529 }
530
531
532 int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
533                       int active_best_quality, int active_worst_quality) {
534   const VP9_COMMON *const cm = &cpi->common;
535   int q = active_worst_quality;
536   int last_error = INT_MAX;
537   int i, target_bits_per_mb, bits_per_mb_at_this_q;
538   const double correction_factor = get_rate_correction_factor(cpi);
539
540   // Calculate required scaling factor based on target frame size and size of
541   // frame produced using previous Q.
542   target_bits_per_mb =
543       ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
544
545   i = active_best_quality;
546
547   do {
548     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
549         cm->seg.enabled &&
550         cpi->svc.temporal_layer_id == 0) {
551       bits_per_mb_at_this_q =
552           (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
553     } else {
554       bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i,
555                                                       correction_factor,
556                                                       cm->bit_depth);
557     }
558
559     if (bits_per_mb_at_this_q <= target_bits_per_mb) {
560       if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
561         q = i;
562       else
563         q = i - 1;
564
565       break;
566     } else {
567       last_error = bits_per_mb_at_this_q - target_bits_per_mb;
568     }
569   } while (++i <= active_worst_quality);
570
571   // In CBR mode, this makes sure q is between oscillating Qs to prevent
572   // resonance.
573   if (cpi->oxcf.rc_mode == VPX_CBR &&
574       (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
575       cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
576     q = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
577               VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
578   }
579   return q;
580 }
581
582 static int get_active_quality(int q, int gfu_boost, int low, int high,
583                               int *low_motion_minq, int *high_motion_minq) {
584   if (gfu_boost > high) {
585     return low_motion_minq[q];
586   } else if (gfu_boost < low) {
587     return high_motion_minq[q];
588   } else {
589     const int gap = high - low;
590     const int offset = high - gfu_boost;
591     const int qdiff = high_motion_minq[q] - low_motion_minq[q];
592     const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
593     return low_motion_minq[q] + adjustment;
594   }
595 }
596
597 static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
598                                  vpx_bit_depth_t bit_depth) {
599   int *kf_low_motion_minq;
600   int *kf_high_motion_minq;
601   ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
602   ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
603   return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
604                             kf_low_motion_minq, kf_high_motion_minq);
605 }
606
607 static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
608                                  vpx_bit_depth_t bit_depth) {
609   int *arfgf_low_motion_minq;
610   int *arfgf_high_motion_minq;
611   ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
612   ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
613   return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
614                             arfgf_low_motion_minq, arfgf_high_motion_minq);
615 }
616
617 static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
618   const RATE_CONTROL *const rc = &cpi->rc;
619   const unsigned int curr_frame = cpi->common.current_video_frame;
620   int active_worst_quality;
621
622   if (cpi->common.frame_type == KEY_FRAME) {
623     active_worst_quality = curr_frame == 0 ? rc->worst_quality
624                                            : rc->last_q[KEY_FRAME] * 2;
625   } else {
626     if (!rc->is_src_frame_alt_ref &&
627         (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
628       active_worst_quality =  curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
629                                               : rc->last_q[INTER_FRAME];
630     } else {
631       active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
632                                              : rc->last_q[INTER_FRAME] * 2;
633     }
634   }
635   return VPXMIN(active_worst_quality, rc->worst_quality);
636 }
637
638 // Adjust active_worst_quality level based on buffer level.
639 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
640   // Adjust active_worst_quality: If buffer is above the optimal/target level,
641   // bring active_worst_quality down depending on fullness of buffer.
642   // If buffer is below the optimal level, let the active_worst_quality go from
643   // ambient Q (at buffer = optimal level) to worst_quality level
644   // (at buffer = critical level).
645   const VP9_COMMON *const cm = &cpi->common;
646   const RATE_CONTROL *rc = &cpi->rc;
647   // Buffer level below which we push active_worst to worst_quality.
648   int64_t critical_level = rc->optimal_buffer_level >> 3;
649   int64_t buff_lvl_step = 0;
650   int adjustment = 0;
651   int active_worst_quality;
652   int ambient_qp;
653   unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
654   if (cm->frame_type == KEY_FRAME)
655     return rc->worst_quality;
656   // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
657   // for the first few frames following key frame. These are both initialized
658   // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
659   // So for first few frames following key, the qp of that key frame is weighted
660   // into the active_worst_quality setting.
661   ambient_qp = (cm->current_video_frame < num_frames_weight_key) ?
662                    VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
663                           rc->avg_frame_qindex[KEY_FRAME]) :
664                    rc->avg_frame_qindex[INTER_FRAME];
665   active_worst_quality = VPXMIN(rc->worst_quality, ambient_qp * 5 / 4);
666   if (rc->buffer_level > rc->optimal_buffer_level) {
667     // Adjust down.
668     // Maximum limit for down adjustment, ~30%.
669     int max_adjustment_down = active_worst_quality / 3;
670     if (max_adjustment_down) {
671       buff_lvl_step = ((rc->maximum_buffer_size -
672                         rc->optimal_buffer_level) / max_adjustment_down);
673       if (buff_lvl_step)
674         adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
675                             buff_lvl_step);
676       active_worst_quality -= adjustment;
677     }
678   } else if (rc->buffer_level > critical_level) {
679     // Adjust up from ambient Q.
680     if (critical_level) {
681       buff_lvl_step = (rc->optimal_buffer_level - critical_level);
682       if (buff_lvl_step) {
683         adjustment = (int)((rc->worst_quality - ambient_qp) *
684                            (rc->optimal_buffer_level - rc->buffer_level) /
685                            buff_lvl_step);
686       }
687       active_worst_quality = ambient_qp + adjustment;
688     }
689   } else {
690     // Set to worst_quality if buffer is below critical level.
691     active_worst_quality = rc->worst_quality;
692   }
693   return active_worst_quality;
694 }
695
696 static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
697                                              int *bottom_index,
698                                              int *top_index) {
699   const VP9_COMMON *const cm = &cpi->common;
700   const RATE_CONTROL *const rc = &cpi->rc;
701   int active_best_quality;
702   int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
703   int q;
704   int *rtc_minq;
705   ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
706
707   if (frame_is_intra_only(cm)) {
708     active_best_quality = rc->best_quality;
709     // Handle the special case for key frames forced when we have reached
710     // the maximum key frame interval. Here force the Q to a range
711     // based on the ambient Q to reduce the risk of popping.
712     if (rc->this_key_frame_forced) {
713       int qindex = rc->last_boosted_qindex;
714       double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
715       int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
716                                             (last_boosted_q * 0.75),
717                                             cm->bit_depth);
718       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
719     } else if (cm->current_video_frame > 0) {
720       // not first frame of one pass and kf_boost is set
721       double q_adj_factor = 1.0;
722       double q_val;
723
724       active_best_quality =
725           get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
726                                 cm->bit_depth);
727
728       // Allow somewhat lower kf minq with small image formats.
729       if ((cm->width * cm->height) <= (352 * 288)) {
730         q_adj_factor -= 0.25;
731       }
732
733       // Convert the adjustment factor to a qindex delta
734       // on active_best_quality.
735       q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
736       active_best_quality += vp9_compute_qdelta(rc, q_val,
737                                                 q_val * q_adj_factor,
738                                                 cm->bit_depth);
739     }
740   } else if (!rc->is_src_frame_alt_ref &&
741              !cpi->use_svc &&
742              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
743     // Use the lower of active_worst_quality and recent
744     // average Q as basis for GF/ARF best Q limit unless last frame was
745     // a key frame.
746     if (rc->frames_since_key > 1 &&
747         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
748       q = rc->avg_frame_qindex[INTER_FRAME];
749     } else {
750       q = active_worst_quality;
751     }
752     active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
753   } else {
754     // Use the lower of active_worst_quality and recent/average Q.
755     if (cm->current_video_frame > 1) {
756       if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
757         active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
758       else
759         active_best_quality = rtc_minq[active_worst_quality];
760     } else {
761       if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
762         active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
763       else
764         active_best_quality = rtc_minq[active_worst_quality];
765     }
766   }
767
768   // Clip the active best and worst quality values to limits
769   active_best_quality = clamp(active_best_quality,
770                               rc->best_quality, rc->worst_quality);
771   active_worst_quality = clamp(active_worst_quality,
772                                active_best_quality, rc->worst_quality);
773
774   *top_index = active_worst_quality;
775   *bottom_index = active_best_quality;
776
777 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
778   // Limit Q range for the adaptive loop.
779   if (cm->frame_type == KEY_FRAME &&
780       !rc->this_key_frame_forced  &&
781       !(cm->current_video_frame == 0)) {
782     int qdelta = 0;
783     vpx_clear_system_state();
784     qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
785                                         active_worst_quality, 2.0,
786                                         cm->bit_depth);
787     *top_index = active_worst_quality + qdelta;
788     *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
789   }
790 #endif
791
792   // Special case code to try and match quality with forced key frames
793   if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
794     q = rc->last_boosted_qindex;
795   } else {
796     q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
797                           active_best_quality, active_worst_quality);
798     if (q > *top_index) {
799       // Special case when we are targeting the max allowed rate
800       if (rc->this_frame_target >= rc->max_frame_bandwidth)
801         *top_index = q;
802       else
803         q = *top_index;
804     }
805   }
806   assert(*top_index <= rc->worst_quality &&
807          *top_index >= rc->best_quality);
808   assert(*bottom_index <= rc->worst_quality &&
809          *bottom_index >= rc->best_quality);
810   assert(q <= rc->worst_quality && q >= rc->best_quality);
811   return q;
812 }
813
814 static int get_active_cq_level(const RATE_CONTROL *rc,
815                                const VP9EncoderConfig *const oxcf) {
816   static const double cq_adjust_threshold = 0.1;
817   int active_cq_level = oxcf->cq_level;
818   if (oxcf->rc_mode == VPX_CQ &&
819       rc->total_target_bits > 0) {
820     const double x = (double)rc->total_actual_bits / rc->total_target_bits;
821     if (x < cq_adjust_threshold) {
822       active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
823     }
824   }
825   return active_cq_level;
826 }
827
828 static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
829                                              int *bottom_index,
830                                              int *top_index) {
831   const VP9_COMMON *const cm = &cpi->common;
832   const RATE_CONTROL *const rc = &cpi->rc;
833   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
834   const int cq_level = get_active_cq_level(rc, oxcf);
835   int active_best_quality;
836   int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
837   int q;
838   int *inter_minq;
839   ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
840
841   if (frame_is_intra_only(cm)) {
842     if (oxcf->rc_mode == VPX_Q) {
843       int qindex = cq_level;
844       double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
845       int delta_qindex = vp9_compute_qdelta(rc, q, q * 0.25,
846                                             cm->bit_depth);
847       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
848     } else if (rc->this_key_frame_forced) {
849       // Handle the special case for key frames forced when we have reached
850       // the maximum key frame interval. Here force the Q to a range
851       // based on the ambient Q to reduce the risk of popping.
852       int qindex = rc->last_boosted_qindex;
853       double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
854       int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
855                                             last_boosted_q * 0.75,
856                                             cm->bit_depth);
857       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
858     } else {
859       // not first frame of one pass and kf_boost is set
860       double q_adj_factor = 1.0;
861       double q_val;
862
863       active_best_quality =
864           get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
865                                 cm->bit_depth);
866
867       // Allow somewhat lower kf minq with small image formats.
868       if ((cm->width * cm->height) <= (352 * 288)) {
869         q_adj_factor -= 0.25;
870       }
871
872       // Convert the adjustment factor to a qindex delta
873       // on active_best_quality.
874       q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
875       active_best_quality += vp9_compute_qdelta(rc, q_val,
876                                                 q_val * q_adj_factor,
877                                                 cm->bit_depth);
878     }
879   } else if (!rc->is_src_frame_alt_ref &&
880              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
881     // Use the lower of active_worst_quality and recent
882     // average Q as basis for GF/ARF best Q limit unless last frame was
883     // a key frame.
884     if (rc->frames_since_key > 1 &&
885         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
886       q = rc->avg_frame_qindex[INTER_FRAME];
887     } else {
888       q = rc->avg_frame_qindex[KEY_FRAME];
889     }
890     // For constrained quality dont allow Q less than the cq level
891     if (oxcf->rc_mode == VPX_CQ) {
892       if (q < cq_level)
893         q = cq_level;
894
895       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
896
897       // Constrained quality use slightly lower active best.
898       active_best_quality = active_best_quality * 15 / 16;
899
900     } else if (oxcf->rc_mode == VPX_Q) {
901       int qindex = cq_level;
902       double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
903       int delta_qindex;
904       if (cpi->refresh_alt_ref_frame)
905         delta_qindex = vp9_compute_qdelta(rc, q, q * 0.40, cm->bit_depth);
906       else
907         delta_qindex = vp9_compute_qdelta(rc, q, q * 0.50, cm->bit_depth);
908       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
909     } else {
910       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
911     }
912   } else {
913     if (oxcf->rc_mode == VPX_Q) {
914       int qindex = cq_level;
915       double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
916       double delta_rate[FIXED_GF_INTERVAL] =
917           {0.50, 1.0, 0.85, 1.0, 0.70, 1.0, 0.85, 1.0};
918       int delta_qindex =
919           vp9_compute_qdelta(rc, q,
920                              q * delta_rate[cm->current_video_frame %
921                              FIXED_GF_INTERVAL], cm->bit_depth);
922       active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
923     } else {
924       // Use the lower of active_worst_quality and recent/average Q.
925       if (cm->current_video_frame > 1)
926         active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
927       else
928         active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
929       // For the constrained quality mode we don't want
930       // q to fall below the cq level.
931       if ((oxcf->rc_mode == VPX_CQ) &&
932           (active_best_quality < cq_level)) {
933         active_best_quality = cq_level;
934       }
935     }
936   }
937
938   // Clip the active best and worst quality values to limits
939   active_best_quality = clamp(active_best_quality,
940                               rc->best_quality, rc->worst_quality);
941   active_worst_quality = clamp(active_worst_quality,
942                                active_best_quality, rc->worst_quality);
943
944   *top_index = active_worst_quality;
945   *bottom_index = active_best_quality;
946
947 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
948   {
949     int qdelta = 0;
950     vpx_clear_system_state();
951
952     // Limit Q range for the adaptive loop.
953     if (cm->frame_type == KEY_FRAME &&
954         !rc->this_key_frame_forced &&
955         !(cm->current_video_frame == 0)) {
956       qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
957                                           active_worst_quality, 2.0,
958                                           cm->bit_depth);
959     } else if (!rc->is_src_frame_alt_ref &&
960                (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
961       qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
962                                           active_worst_quality, 1.75,
963                                           cm->bit_depth);
964     }
965     *top_index = active_worst_quality + qdelta;
966     *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
967   }
968 #endif
969
970   if (oxcf->rc_mode == VPX_Q) {
971     q = active_best_quality;
972   // Special case code to try and match quality with forced key frames
973   } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
974     q = rc->last_boosted_qindex;
975   } else {
976     q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
977                           active_best_quality, active_worst_quality);
978     if (q > *top_index) {
979       // Special case when we are targeting the max allowed rate
980       if (rc->this_frame_target >= rc->max_frame_bandwidth)
981         *top_index = q;
982       else
983         q = *top_index;
984     }
985   }
986
987   assert(*top_index <= rc->worst_quality &&
988          *top_index >= rc->best_quality);
989   assert(*bottom_index <= rc->worst_quality &&
990          *bottom_index >= rc->best_quality);
991   assert(q <= rc->worst_quality && q >= rc->best_quality);
992   return q;
993 }
994
995 int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) {
996   static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
997     1.00,  // INTER_NORMAL
998     1.00,  // INTER_HIGH
999     1.50,  // GF_ARF_LOW
1000     1.75,  // GF_ARF_STD
1001     2.00,  // KF_STD
1002   };
1003   static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] =
1004       {INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME};
1005   const VP9_COMMON *const cm = &cpi->common;
1006   int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level],
1007                                           q, rate_factor_deltas[rf_level],
1008                                           cm->bit_depth);
1009   return qdelta;
1010 }
1011
1012 #define STATIC_MOTION_THRESH 95
1013 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi,
1014                                          int *bottom_index,
1015                                          int *top_index) {
1016   const VP9_COMMON *const cm = &cpi->common;
1017   const RATE_CONTROL *const rc = &cpi->rc;
1018   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1019   const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1020   const int cq_level = get_active_cq_level(rc, oxcf);
1021   int active_best_quality;
1022   int active_worst_quality = cpi->twopass.active_worst_quality;
1023   int q;
1024   int *inter_minq;
1025   ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
1026
1027   if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
1028     // Handle the special case for key frames forced when we have reached
1029     // the maximum key frame interval. Here force the Q to a range
1030     // based on the ambient Q to reduce the risk of popping.
1031     if (rc->this_key_frame_forced) {
1032       double last_boosted_q;
1033       int delta_qindex;
1034       int qindex;
1035
1036       if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1037         qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1038         active_best_quality = qindex;
1039         last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1040         delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1041                                               last_boosted_q * 1.25,
1042                                               cm->bit_depth);
1043         active_worst_quality =
1044             VPXMIN(qindex + delta_qindex, active_worst_quality);
1045       } else {
1046         qindex = rc->last_boosted_qindex;
1047         last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1048         delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1049                                               last_boosted_q * 0.75,
1050                                               cm->bit_depth);
1051         active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1052       }
1053     } else {
1054       // Not forced keyframe.
1055       double q_adj_factor = 1.0;
1056       double q_val;
1057       // Baseline value derived from cpi->active_worst_quality and kf boost.
1058       active_best_quality = get_kf_active_quality(rc, active_worst_quality,
1059                                                   cm->bit_depth);
1060
1061       // Allow somewhat lower kf minq with small image formats.
1062       if ((cm->width * cm->height) <= (352 * 288)) {
1063         q_adj_factor -= 0.25;
1064       }
1065
1066       // Make a further adjustment based on the kf zero motion measure.
1067       q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
1068
1069       // Convert the adjustment factor to a qindex delta
1070       // on active_best_quality.
1071       q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
1072       active_best_quality += vp9_compute_qdelta(rc, q_val,
1073                                                 q_val * q_adj_factor,
1074                                                 cm->bit_depth);
1075     }
1076   } else if (!rc->is_src_frame_alt_ref &&
1077              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1078     // Use the lower of active_worst_quality and recent
1079     // average Q as basis for GF/ARF best Q limit unless last frame was
1080     // a key frame.
1081     if (rc->frames_since_key > 1 &&
1082         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1083       q = rc->avg_frame_qindex[INTER_FRAME];
1084     } else {
1085       q = active_worst_quality;
1086     }
1087     // For constrained quality dont allow Q less than the cq level
1088     if (oxcf->rc_mode == VPX_CQ) {
1089       if (q < cq_level)
1090         q = cq_level;
1091
1092       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1093
1094       // Constrained quality use slightly lower active best.
1095       active_best_quality = active_best_quality * 15 / 16;
1096
1097     } else if (oxcf->rc_mode == VPX_Q) {
1098       if (!cpi->refresh_alt_ref_frame) {
1099         active_best_quality = cq_level;
1100       } else {
1101         active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1102
1103         // Modify best quality for second level arfs. For mode VPX_Q this
1104         // becomes the baseline frame q.
1105         if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
1106           active_best_quality = (active_best_quality + cq_level + 1) / 2;
1107       }
1108     } else {
1109       active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1110     }
1111   } else {
1112     if (oxcf->rc_mode == VPX_Q) {
1113       active_best_quality = cq_level;
1114     } else {
1115       active_best_quality = inter_minq[active_worst_quality];
1116
1117       // For the constrained quality mode we don't want
1118       // q to fall below the cq level.
1119       if ((oxcf->rc_mode == VPX_CQ) &&
1120           (active_best_quality < cq_level)) {
1121         active_best_quality = cq_level;
1122       }
1123     }
1124   }
1125
1126   // Extension to max or min Q if undershoot or overshoot is outside
1127   // the permitted range.
1128   if ((cpi->oxcf.rc_mode != VPX_Q) &&
1129       (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) {
1130     if (frame_is_intra_only(cm) ||
1131         (!rc->is_src_frame_alt_ref &&
1132          (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1133       active_best_quality -=
1134         (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
1135       active_worst_quality += (cpi->twopass.extend_maxq / 2);
1136     } else {
1137       active_best_quality -=
1138         (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
1139       active_worst_quality += cpi->twopass.extend_maxq;
1140     }
1141   }
1142
1143 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
1144   vpx_clear_system_state();
1145   // Static forced key frames Q restrictions dealt with elsewhere.
1146   if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) ||
1147       !rc->this_key_frame_forced ||
1148       (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
1149     int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index],
1150                                        active_worst_quality);
1151     active_worst_quality = VPXMAX(active_worst_quality + qdelta,
1152                                   active_best_quality);
1153   }
1154 #endif
1155
1156   // Modify active_best_quality for downscaled normal frames.
1157   if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
1158     int qdelta = vp9_compute_qdelta_by_rate(rc, cm->frame_type,
1159                                             active_best_quality, 2.0,
1160                                             cm->bit_depth);
1161     active_best_quality =
1162         VPXMAX(active_best_quality + qdelta, rc->best_quality);
1163   }
1164
1165   active_best_quality = clamp(active_best_quality,
1166                               rc->best_quality, rc->worst_quality);
1167   active_worst_quality = clamp(active_worst_quality,
1168                                active_best_quality, rc->worst_quality);
1169
1170   if (oxcf->rc_mode == VPX_Q) {
1171     q = active_best_quality;
1172   // Special case code to try and match quality with forced key frames.
1173   } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) &&
1174              rc->this_key_frame_forced) {
1175     // If static since last kf use better of last boosted and last kf q.
1176     if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1177       q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1178     } else {
1179       q = rc->last_boosted_qindex;
1180     }
1181   } else {
1182     q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1183                           active_best_quality, active_worst_quality);
1184     if (q > active_worst_quality) {
1185       // Special case when we are targeting the max allowed rate.
1186       if (rc->this_frame_target >= rc->max_frame_bandwidth)
1187         active_worst_quality = q;
1188       else
1189         q = active_worst_quality;
1190     }
1191   }
1192   clamp(q, active_best_quality, active_worst_quality);
1193
1194   *top_index = active_worst_quality;
1195   *bottom_index = active_best_quality;
1196
1197   assert(*top_index <= rc->worst_quality &&
1198          *top_index >= rc->best_quality);
1199   assert(*bottom_index <= rc->worst_quality &&
1200          *bottom_index >= rc->best_quality);
1201   assert(q <= rc->worst_quality && q >= rc->best_quality);
1202   return q;
1203 }
1204
1205 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi,
1206                              int *bottom_index, int *top_index) {
1207   int q;
1208   if (cpi->oxcf.pass == 0) {
1209     if (cpi->oxcf.rc_mode == VPX_CBR)
1210       q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
1211     else
1212       q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
1213   } else {
1214     q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
1215   }
1216   if (cpi->sf.use_nonrd_pick_mode) {
1217     if (cpi->sf.force_frame_boost == 1)
1218       q -= cpi->sf.max_delta_qindex;
1219
1220     if (q < *bottom_index)
1221       *bottom_index = q;
1222     else if (q > *top_index)
1223       *top_index = q;
1224   }
1225   return q;
1226 }
1227
1228 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi,
1229                                       int frame_target,
1230                                       int *frame_under_shoot_limit,
1231                                       int *frame_over_shoot_limit) {
1232   if (cpi->oxcf.rc_mode == VPX_Q) {
1233     *frame_under_shoot_limit = 0;
1234     *frame_over_shoot_limit  = INT_MAX;
1235   } else {
1236     // For very small rate targets where the fractional adjustment
1237     // may be tiny make sure there is at least a minimum range.
1238     const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100;
1239     *frame_under_shoot_limit = VPXMAX(frame_target - tolerance - 200, 0);
1240     *frame_over_shoot_limit = VPXMIN(frame_target + tolerance + 200,
1241                                      cpi->rc.max_frame_bandwidth);
1242   }
1243 }
1244
1245 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
1246   const VP9_COMMON *const cm = &cpi->common;
1247   RATE_CONTROL *const rc = &cpi->rc;
1248
1249   rc->this_frame_target = target;
1250
1251   // Modify frame size target when down-scaling.
1252   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
1253       rc->frame_size_selector != UNSCALED)
1254     rc->this_frame_target = (int)(rc->this_frame_target
1255         * rate_thresh_mult[rc->frame_size_selector]);
1256
1257   // Target rate per SB64 (including partial SB64s.
1258   rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) /
1259                              (cm->width * cm->height);
1260 }
1261
1262 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
1263   // this frame refreshes means next frames don't unless specified by user
1264   RATE_CONTROL *const rc = &cpi->rc;
1265   rc->frames_since_golden = 0;
1266
1267   // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1268   rc->source_alt_ref_pending = 0;
1269
1270   // Set the alternate reference frame active flag
1271   rc->source_alt_ref_active = 1;
1272 }
1273
1274 static void update_golden_frame_stats(VP9_COMP *cpi) {
1275   RATE_CONTROL *const rc = &cpi->rc;
1276
1277   // Update the Golden frame usage counts.
1278   if (cpi->refresh_golden_frame) {
1279     // this frame refreshes means next frames don't unless specified by user
1280     rc->frames_since_golden = 0;
1281
1282     // If we are not using alt ref in the up and coming group clear the arf
1283     // active flag. In multi arf group case, if the index is not 0 then
1284     // we are overlaying a mid group arf so should not reset the flag.
1285     if (cpi->oxcf.pass == 2) {
1286       if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1287         rc->source_alt_ref_active = 0;
1288     } else if (!rc->source_alt_ref_pending) {
1289       rc->source_alt_ref_active = 0;
1290     }
1291
1292     // Decrement count down till next gf
1293     if (rc->frames_till_gf_update_due > 0)
1294       rc->frames_till_gf_update_due--;
1295
1296   } else if (!cpi->refresh_alt_ref_frame) {
1297     // Decrement count down till next gf
1298     if (rc->frames_till_gf_update_due > 0)
1299       rc->frames_till_gf_update_due--;
1300
1301     rc->frames_since_golden++;
1302   }
1303 }
1304
1305 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
1306   const VP9_COMMON *const cm = &cpi->common;
1307   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1308   RATE_CONTROL *const rc = &cpi->rc;
1309   const int qindex = cm->base_qindex;
1310
1311   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
1312     vp9_cyclic_refresh_postencode(cpi);
1313   }
1314
1315   // Update rate control heuristics
1316   rc->projected_frame_size = (int)(bytes_used << 3);
1317
1318   // Post encode loop adjustment of Q prediction.
1319   vp9_rc_update_rate_correction_factors(cpi);
1320
1321   // Keep a record of last Q and ambient average Q.
1322   if (cm->frame_type == KEY_FRAME) {
1323     rc->last_q[KEY_FRAME] = qindex;
1324     rc->avg_frame_qindex[KEY_FRAME] =
1325         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1326     if (cpi->use_svc) {
1327       int i = 0;
1328       SVC *svc = &cpi->svc;
1329       for (i = 0; i < svc->number_temporal_layers; ++i) {
1330         const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
1331                                            svc->number_temporal_layers);
1332         LAYER_CONTEXT *lc = &svc->layer_context[layer];
1333         RATE_CONTROL *lrc = &lc->rc;
1334         lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME];
1335         lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME];
1336       }
1337     }
1338   } else {
1339     if ((cpi->use_svc && oxcf->rc_mode == VPX_CBR) ||
1340         (!rc->is_src_frame_alt_ref &&
1341          !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1342       rc->last_q[INTER_FRAME] = qindex;
1343       rc->avg_frame_qindex[INTER_FRAME] =
1344         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1345       rc->ni_frames++;
1346       rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1347       rc->avg_q = rc->tot_q / rc->ni_frames;
1348       // Calculate the average Q for normal inter frames (not key or GFU
1349       // frames).
1350       rc->ni_tot_qi += qindex;
1351       rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1352     }
1353   }
1354
1355   // Keep record of last boosted (KF/KF/ARF) Q value.
1356   // If the current frame is coded at a lower Q then we also update it.
1357   // If all mbs in this group are skipped only update if the Q value is
1358   // better than that already stored.
1359   // This is used to help set quality in forced key frames to reduce popping
1360   if ((qindex < rc->last_boosted_qindex) ||
1361       (cm->frame_type == KEY_FRAME) ||
1362       (!rc->constrained_gf_group &&
1363        (cpi->refresh_alt_ref_frame ||
1364         (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1365     rc->last_boosted_qindex = qindex;
1366   }
1367   if (cm->frame_type == KEY_FRAME)
1368     rc->last_kf_qindex = qindex;
1369
1370   update_buffer_level(cpi, rc->projected_frame_size);
1371
1372   // Rolling monitors of whether we are over or underspending used to help
1373   // regulate min and Max Q in two pass.
1374   if (cm->frame_type != KEY_FRAME) {
1375     rc->rolling_target_bits = ROUND_POWER_OF_TWO(
1376         rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1377     rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
1378         rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1379     rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
1380         rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1381     rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
1382         rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1383   }
1384
1385   // Actual bits spent
1386   rc->total_actual_bits += rc->projected_frame_size;
1387   rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1388
1389   rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1390
1391   if (!cpi->use_svc || is_two_pass_svc(cpi)) {
1392     if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1393         (cm->frame_type != KEY_FRAME))
1394       // Update the alternate reference frame stats as appropriate.
1395       update_alt_ref_frame_stats(cpi);
1396     else
1397       // Update the Golden frame stats as appropriate.
1398       update_golden_frame_stats(cpi);
1399   }
1400
1401   if (cm->frame_type == KEY_FRAME)
1402     rc->frames_since_key = 0;
1403   if (cm->show_frame) {
1404     rc->frames_since_key++;
1405     rc->frames_to_key--;
1406   }
1407
1408   // Trigger the resizing of the next frame if it is scaled.
1409   if (oxcf->pass != 0) {
1410     cpi->resize_pending =
1411         rc->next_frame_size_selector != rc->frame_size_selector;
1412     rc->frame_size_selector = rc->next_frame_size_selector;
1413   }
1414 }
1415
1416 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
1417   // Update buffer level with zero size, update frame counters, and return.
1418   update_buffer_level(cpi, 0);
1419   cpi->rc.frames_since_key++;
1420   cpi->rc.frames_to_key--;
1421   cpi->rc.rc_2_frame = 0;
1422   cpi->rc.rc_1_frame = 0;
1423 }
1424
1425 // Use this macro to turn on/off use of alt-refs in one-pass mode.
1426 #define USE_ALTREF_FOR_ONE_PASS   1
1427
1428 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
1429   static const int af_ratio = 10;
1430   const RATE_CONTROL *const rc = &cpi->rc;
1431   int target;
1432 #if USE_ALTREF_FOR_ONE_PASS
1433   target = (!rc->is_src_frame_alt_ref &&
1434             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) ?
1435       (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
1436       (rc->baseline_gf_interval + af_ratio - 1) :
1437       (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
1438       (rc->baseline_gf_interval + af_ratio - 1);
1439 #else
1440   target = rc->avg_frame_bandwidth;
1441 #endif
1442   return vp9_rc_clamp_pframe_target_size(cpi, target);
1443 }
1444
1445 static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
1446   static const int kf_ratio = 25;
1447   const RATE_CONTROL *rc = &cpi->rc;
1448   const int target = rc->avg_frame_bandwidth * kf_ratio;
1449   return vp9_rc_clamp_iframe_target_size(cpi, target);
1450 }
1451
1452 void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
1453   VP9_COMMON *const cm = &cpi->common;
1454   RATE_CONTROL *const rc = &cpi->rc;
1455   int target;
1456   // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
1457   if (!cpi->refresh_alt_ref_frame &&
1458       (cm->current_video_frame == 0 ||
1459        (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1460        rc->frames_to_key == 0 ||
1461        (cpi->oxcf.auto_key && 0))) {
1462     cm->frame_type = KEY_FRAME;
1463     rc->this_key_frame_forced = cm->current_video_frame != 0 &&
1464                                 rc->frames_to_key == 0;
1465     rc->frames_to_key = cpi->oxcf.key_freq;
1466     rc->kf_boost = DEFAULT_KF_BOOST;
1467     rc->source_alt_ref_active = 0;
1468   } else {
1469     cm->frame_type = INTER_FRAME;
1470   }
1471   if (rc->frames_till_gf_update_due == 0) {
1472     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0) {
1473       vp9_cyclic_refresh_set_golden_update(cpi);
1474     } else {
1475       rc->baseline_gf_interval =
1476           (rc->min_gf_interval + rc->max_gf_interval) / 2;
1477     }
1478     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1479     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
1480     if (rc->frames_till_gf_update_due > rc->frames_to_key) {
1481       rc->frames_till_gf_update_due = rc->frames_to_key;
1482       rc->constrained_gf_group = 1;
1483     } else {
1484       rc->constrained_gf_group = 0;
1485     }
1486     cpi->refresh_golden_frame = 1;
1487     rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
1488     rc->gfu_boost = DEFAULT_GF_BOOST;
1489   }
1490   if (cm->frame_type == KEY_FRAME)
1491     target = calc_iframe_target_size_one_pass_vbr(cpi);
1492   else
1493     target = calc_pframe_target_size_one_pass_vbr(cpi);
1494   vp9_rc_set_frame_target(cpi, target);
1495   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0)
1496     vp9_cyclic_refresh_update_parameters(cpi);
1497 }
1498
1499 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1500   const VP9EncoderConfig *oxcf = &cpi->oxcf;
1501   const RATE_CONTROL *rc = &cpi->rc;
1502   const SVC *const svc = &cpi->svc;
1503   const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
1504   const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
1505   int min_frame_target =
1506       VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
1507   int target;
1508
1509   if (oxcf->gf_cbr_boost_pct) {
1510     const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
1511     target =  cpi->refresh_golden_frame ?
1512       (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) /
1513       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) :
1514       (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
1515       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
1516   } else {
1517     target = rc->avg_frame_bandwidth;
1518   }
1519   if (is_one_pass_cbr_svc(cpi)) {
1520     // Note that for layers, avg_frame_bandwidth is the cumulative
1521     // per-frame-bandwidth. For the target size of this frame, use the
1522     // layer average frame size (i.e., non-cumulative per-frame-bw).
1523     int layer =
1524         LAYER_IDS_TO_IDX(svc->spatial_layer_id,
1525             svc->temporal_layer_id, svc->number_temporal_layers);
1526     const LAYER_CONTEXT *lc = &svc->layer_context[layer];
1527     target = lc->avg_frame_size;
1528     min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
1529   }
1530   if (diff > 0) {
1531     // Lower the target bandwidth for this frame.
1532     const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
1533     target -= (target * pct_low) / 200;
1534   } else if (diff < 0) {
1535     // Increase the target bandwidth for this frame.
1536     const int pct_high =
1537         (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
1538     target += (target * pct_high) / 200;
1539   }
1540   if (oxcf->rc_max_inter_bitrate_pct) {
1541     const int max_rate = rc->avg_frame_bandwidth *
1542                          oxcf->rc_max_inter_bitrate_pct / 100;
1543     target = VPXMIN(target, max_rate);
1544   }
1545   return VPXMAX(min_frame_target, target);
1546 }
1547
1548 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1549   const RATE_CONTROL *rc = &cpi->rc;
1550   const VP9EncoderConfig *oxcf = &cpi->oxcf;
1551   const SVC *const svc = &cpi->svc;
1552   int target;
1553   if (cpi->common.current_video_frame == 0) {
1554     target = ((rc->starting_buffer_level / 2) > INT_MAX)
1555       ? INT_MAX : (int)(rc->starting_buffer_level / 2);
1556   } else {
1557     int kf_boost = 32;
1558     double framerate = cpi->framerate;
1559     if (svc->number_temporal_layers > 1 &&
1560         oxcf->rc_mode == VPX_CBR) {
1561       // Use the layer framerate for temporal layers CBR mode.
1562       const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id,
1563           svc->temporal_layer_id, svc->number_temporal_layers);
1564       const LAYER_CONTEXT *lc = &svc->layer_context[layer];
1565       framerate = lc->framerate;
1566     }
1567     kf_boost = VPXMAX(kf_boost, (int)(2 * framerate - 16));
1568     if (rc->frames_since_key <  framerate / 2) {
1569       kf_boost = (int)(kf_boost * rc->frames_since_key /
1570                        (framerate / 2));
1571     }
1572     target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
1573   }
1574   return vp9_rc_clamp_iframe_target_size(cpi, target);
1575 }
1576
1577 // Reset information needed to set proper reference frames and buffer updates
1578 // for temporal layering. This is called when a key frame is encoded.
1579 static void reset_temporal_layer_to_zero(VP9_COMP *cpi) {
1580   int sl;
1581   LAYER_CONTEXT *lc = NULL;
1582   cpi->svc.temporal_layer_id = 0;
1583
1584   for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1585     lc = &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers];
1586     lc->current_video_frame_in_layer = 0;
1587     lc->frames_from_key_frame = 0;
1588   }
1589 }
1590
1591 void vp9_rc_get_svc_params(VP9_COMP *cpi) {
1592   VP9_COMMON *const cm = &cpi->common;
1593   RATE_CONTROL *const rc = &cpi->rc;
1594   int target = rc->avg_frame_bandwidth;
1595   int layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
1596       cpi->svc.temporal_layer_id, cpi->svc.number_temporal_layers);
1597
1598   if ((cm->current_video_frame == 0) ||
1599       (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1600       (cpi->oxcf.auto_key && (rc->frames_since_key %
1601           cpi->oxcf.key_freq == 0))) {
1602     cm->frame_type = KEY_FRAME;
1603     rc->source_alt_ref_active = 0;
1604
1605     if (is_two_pass_svc(cpi)) {
1606       cpi->svc.layer_context[layer].is_key_frame = 1;
1607       cpi->ref_frame_flags &=
1608           (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
1609     } else if (is_one_pass_cbr_svc(cpi)) {
1610       reset_temporal_layer_to_zero(cpi);
1611       layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
1612            cpi->svc.temporal_layer_id, cpi->svc.number_temporal_layers);
1613       cpi->svc.layer_context[layer].is_key_frame = 1;
1614       cpi->ref_frame_flags &=
1615                 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
1616       // Assumption here is that LAST_FRAME is being updated for a keyframe.
1617       // Thus no change in update flags.
1618       target = calc_iframe_target_size_one_pass_cbr(cpi);
1619     }
1620   } else {
1621     cm->frame_type = INTER_FRAME;
1622     if (is_two_pass_svc(cpi)) {
1623       LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1624       if (cpi->svc.spatial_layer_id == 0) {
1625         lc->is_key_frame = 0;
1626       } else {
1627         lc->is_key_frame =
1628             cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
1629         if (lc->is_key_frame)
1630           cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
1631       }
1632       cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
1633     } else if (is_one_pass_cbr_svc(cpi)) {
1634       LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1635       if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode) {
1636         lc->is_key_frame = 0;
1637       } else {
1638         lc->is_key_frame =
1639             cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
1640       }
1641       target = calc_pframe_target_size_one_pass_cbr(cpi);
1642     }
1643   }
1644
1645   // Any update/change of global cyclic refresh parameters (amount/delta-qp)
1646   // should be done here, before the frame qp is selected.
1647   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1648     vp9_cyclic_refresh_update_parameters(cpi);
1649
1650   vp9_rc_set_frame_target(cpi, target);
1651   rc->frames_till_gf_update_due = INT_MAX;
1652   rc->baseline_gf_interval = INT_MAX;
1653 }
1654
1655 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
1656   VP9_COMMON *const cm = &cpi->common;
1657   RATE_CONTROL *const rc = &cpi->rc;
1658   int target;
1659   // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
1660   if ((cm->current_video_frame == 0 ||
1661       (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1662       rc->frames_to_key == 0 ||
1663       (cpi->oxcf.auto_key && 0))) {
1664     cm->frame_type = KEY_FRAME;
1665     rc->this_key_frame_forced = cm->current_video_frame != 0 &&
1666                                 rc->frames_to_key == 0;
1667     rc->frames_to_key = cpi->oxcf.key_freq;
1668     rc->kf_boost = DEFAULT_KF_BOOST;
1669     rc->source_alt_ref_active = 0;
1670   } else {
1671     cm->frame_type = INTER_FRAME;
1672   }
1673   if (rc->frames_till_gf_update_due == 0) {
1674     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1675       vp9_cyclic_refresh_set_golden_update(cpi);
1676     else
1677       rc->baseline_gf_interval =
1678           (rc->min_gf_interval + rc->max_gf_interval) / 2;
1679     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1680     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
1681     if (rc->frames_till_gf_update_due > rc->frames_to_key)
1682       rc->frames_till_gf_update_due = rc->frames_to_key;
1683     cpi->refresh_golden_frame = 1;
1684     rc->gfu_boost = DEFAULT_GF_BOOST;
1685   }
1686
1687   // Any update/change of global cyclic refresh parameters (amount/delta-qp)
1688   // should be done here, before the frame qp is selected.
1689   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1690     vp9_cyclic_refresh_update_parameters(cpi);
1691
1692   if (cm->frame_type == KEY_FRAME)
1693     target = calc_iframe_target_size_one_pass_cbr(cpi);
1694   else
1695     target = calc_pframe_target_size_one_pass_cbr(cpi);
1696
1697   vp9_rc_set_frame_target(cpi, target);
1698   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC)
1699     cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
1700   else
1701     cpi->resize_pending = 0;
1702 }
1703
1704 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
1705                        vpx_bit_depth_t bit_depth) {
1706   int start_index = rc->worst_quality;
1707   int target_index = rc->worst_quality;
1708   int i;
1709
1710   // Convert the average q value to an index.
1711   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1712     start_index = i;
1713     if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart)
1714       break;
1715   }
1716
1717   // Convert the q target to an index
1718   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1719     target_index = i;
1720     if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget)
1721       break;
1722   }
1723
1724   return target_index - start_index;
1725 }
1726
1727 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
1728                                int qindex, double rate_target_ratio,
1729                                vpx_bit_depth_t bit_depth) {
1730   int target_index = rc->worst_quality;
1731   int i;
1732
1733   // Look up the current projected bits per block for the base index
1734   const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0,
1735                                                   bit_depth);
1736
1737   // Find the target bits per mb based on the base value and given ratio.
1738   const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
1739
1740   // Convert the q target to an index
1741   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1742     if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
1743         target_bits_per_mb) {
1744       target_index = i;
1745       break;
1746     }
1747   }
1748   return target_index - qindex;
1749 }
1750
1751 void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi,
1752                                   RATE_CONTROL *const rc) {
1753   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1754
1755   // Special case code for 1 pass fixed Q mode tests
1756   if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
1757     rc->max_gf_interval = FIXED_GF_INTERVAL;
1758     rc->min_gf_interval = FIXED_GF_INTERVAL;
1759     rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
1760   } else {
1761     // Set Maximum gf/arf interval
1762     rc->max_gf_interval = oxcf->max_gf_interval;
1763     rc->min_gf_interval = oxcf->min_gf_interval;
1764     if (rc->min_gf_interval == 0)
1765       rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
1766           oxcf->width, oxcf->height, cpi->framerate);
1767     if (rc->max_gf_interval == 0)
1768       rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
1769           cpi->framerate, rc->min_gf_interval);
1770
1771     // Extended interval for genuinely static scenes
1772     rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
1773
1774     if (is_altref_enabled(cpi)) {
1775       if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
1776         rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
1777     }
1778
1779     if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1780       rc->max_gf_interval = rc->static_scene_max_gf_interval;
1781
1782     // Clamp min to max
1783     rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
1784   }
1785 }
1786
1787 void vp9_rc_update_framerate(VP9_COMP *cpi) {
1788   const VP9_COMMON *const cm = &cpi->common;
1789   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1790   RATE_CONTROL *const rc = &cpi->rc;
1791   int vbr_max_bits;
1792
1793   rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
1794   rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth *
1795                                 oxcf->two_pass_vbrmin_section / 100);
1796
1797   rc->min_frame_bandwidth =
1798       VPXMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
1799
1800   // A maximum bitrate for a frame is defined.
1801   // The baseline for this aligns with HW implementations that
1802   // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1803   // per 16x16 MB (averaged over a frame). However this limit is extended if
1804   // a very high rate is given on the command line or the the rate cannnot
1805   // be acheived because of a user specificed max q (e.g. when the user
1806   // specifies lossless encode.
1807   vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth *
1808                      oxcf->two_pass_vbrmax_section) / 100);
1809   rc->max_frame_bandwidth =
1810       VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
1811
1812   vp9_rc_set_gf_interval_range(cpi, rc);
1813 }
1814
1815 #define VBR_PCT_ADJUSTMENT_LIMIT 50
1816 // For VBR...adjustment to the frame target based on error from previous frames
1817 static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
1818   RATE_CONTROL *const rc = &cpi->rc;
1819   int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
1820   int max_delta;
1821   double position_factor = 1.0;
1822
1823   // How far through the clip are we.
1824   // This number is used to damp the per frame rate correction.
1825   // Range 0 - 1.0
1826   if (cpi->twopass.total_stats.count) {
1827     position_factor = sqrt((double)cpi->common.current_video_frame /
1828                            cpi->twopass.total_stats.count);
1829   }
1830   max_delta = (int)(position_factor *
1831                     ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
1832
1833   // vbr_bits_off_target > 0 means we have extra bits to spend
1834   if (vbr_bits_off_target > 0) {
1835     *this_frame_target +=
1836       (vbr_bits_off_target > max_delta) ? max_delta
1837                                         : (int)vbr_bits_off_target;
1838   } else {
1839     *this_frame_target -=
1840       (vbr_bits_off_target < -max_delta) ? max_delta
1841                                          : (int)-vbr_bits_off_target;
1842   }
1843
1844   // Fast redistribution of bits arising from massive local undershoot.
1845   // Dont do it for kf,arf,gf or overlay frames.
1846   if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
1847       rc->vbr_bits_off_target_fast) {
1848     int one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, *this_frame_target);
1849     int fast_extra_bits;
1850     fast_extra_bits = (int)VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
1851     fast_extra_bits = (int)VPXMIN(
1852         fast_extra_bits,
1853         VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
1854     *this_frame_target += (int)fast_extra_bits;
1855     rc->vbr_bits_off_target_fast -= fast_extra_bits;
1856   }
1857 }
1858
1859 void vp9_set_target_rate(VP9_COMP *cpi) {
1860   RATE_CONTROL *const rc = &cpi->rc;
1861   int target_rate = rc->base_frame_target;
1862
1863   if (cpi->common.frame_type == KEY_FRAME)
1864     target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
1865   else
1866     target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
1867
1868   // Correction to rate target based on prior over or under shoot.
1869   if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
1870     vbr_rate_correction(cpi, &target_rate);
1871   vp9_rc_set_frame_target(cpi, target_rate);
1872 }
1873
1874 // Check if we should resize, based on average QP from past x frames.
1875 // Only allow for resize at most one scale down for now, scaling factor is 2.
1876 int vp9_resize_one_pass_cbr(VP9_COMP *cpi) {
1877   const VP9_COMMON *const cm = &cpi->common;
1878   RATE_CONTROL *const rc = &cpi->rc;
1879   RESIZE_ACTION resize_action = NO_RESIZE;
1880   int avg_qp_thr1 = 70;
1881   int avg_qp_thr2 = 50;
1882   int min_width = 180;
1883   int min_height = 180;
1884   int down_size_on = 1;
1885   cpi->resize_scale_num = 1;
1886   cpi->resize_scale_den = 1;
1887   // Don't resize on key frame; reset the counters on key frame.
1888   if (cm->frame_type == KEY_FRAME) {
1889     cpi->resize_avg_qp = 0;
1890     cpi->resize_count = 0;
1891     return 0;
1892   }
1893   // Check current frame reslution to avoid generating frames smaller than
1894   // the minimum resolution.
1895   if (ONEHALFONLY_RESIZE) {
1896     if ((cm->width >> 1) < min_width || (cm->height >> 1) < min_height)
1897       down_size_on = 0;
1898   } else {
1899     if (cpi->resize_state == ORIG &&
1900         (cm->width * 3 / 4 < min_width ||
1901          cm->height * 3 / 4 < min_height))
1902       return 0;
1903     else if (cpi->resize_state == THREE_QUARTER &&
1904              ((cpi->oxcf.width >> 1) < min_width ||
1905               (cpi->oxcf.height >> 1) < min_height))
1906       down_size_on = 0;
1907   }
1908
1909 #if CONFIG_VP9_TEMPORAL_DENOISING
1910   // If denoiser is on, apply a smaller qp threshold.
1911   if (cpi->oxcf.noise_sensitivity > 0) {
1912     avg_qp_thr1 = 60;
1913     avg_qp_thr2 = 40;
1914   }
1915 #endif
1916
1917   // Resize based on average buffer underflow and QP over some window.
1918   // Ignore samples close to key frame, since QP is usually high after key.
1919   if (cpi->rc.frames_since_key > 2 * cpi->framerate) {
1920     const int window = (int)(4 * cpi->framerate);
1921     cpi->resize_avg_qp += cm->base_qindex;
1922     if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100))
1923       ++cpi->resize_buffer_underflow;
1924     ++cpi->resize_count;
1925     // Check for resize action every "window" frames.
1926     if (cpi->resize_count >= window) {
1927       int avg_qp = cpi->resize_avg_qp / cpi->resize_count;
1928       // Resize down if buffer level has underflowed sufficient amount in past
1929       // window, and we are at original or 3/4 of original resolution.
1930       // Resize back up if average QP is low, and we are currently in a resized
1931       // down state, i.e. 1/2 or 3/4 of original resolution.
1932       // Currently, use a flag to turn 3/4 resizing feature on/off.
1933       if (cpi->resize_buffer_underflow > (cpi->resize_count >> 2)) {
1934         if (cpi->resize_state == THREE_QUARTER && down_size_on) {
1935           resize_action = DOWN_ONEHALF;
1936           cpi->resize_state = ONE_HALF;
1937         } else if (cpi->resize_state == ORIG) {
1938           resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
1939           cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
1940         }
1941       } else if (cpi->resize_state != ORIG &&
1942                  avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
1943         if (cpi->resize_state == THREE_QUARTER ||
1944             avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100 ||
1945             ONEHALFONLY_RESIZE) {
1946           resize_action = UP_ORIG;
1947           cpi->resize_state = ORIG;
1948         } else if (cpi->resize_state == ONE_HALF) {
1949           resize_action = UP_THREEFOUR;
1950           cpi->resize_state = THREE_QUARTER;
1951         }
1952       }
1953       // Reset for next window measurement.
1954       cpi->resize_avg_qp = 0;
1955       cpi->resize_count = 0;
1956       cpi->resize_buffer_underflow = 0;
1957     }
1958   }
1959   // If decision is to resize, reset some quantities, and check is we should
1960   // reduce rate correction factor,
1961   if (resize_action != NO_RESIZE) {
1962     int target_bits_per_frame;
1963     int active_worst_quality;
1964     int qindex;
1965     int tot_scale_change;
1966     if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
1967       cpi->resize_scale_num = 3;
1968       cpi->resize_scale_den = 4;
1969     } else if (resize_action == DOWN_ONEHALF) {
1970       cpi->resize_scale_num = 1;
1971       cpi->resize_scale_den = 2;
1972     } else {  // UP_ORIG or anything else
1973       cpi->resize_scale_num = 1;
1974       cpi->resize_scale_den = 1;
1975     }
1976     tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) /
1977         (cpi->resize_scale_num * cpi->resize_scale_num);
1978     // Reset buffer level to optimal, update target size.
1979     rc->buffer_level = rc->optimal_buffer_level;
1980     rc->bits_off_target = rc->optimal_buffer_level;
1981     rc->this_frame_target = calc_pframe_target_size_one_pass_cbr(cpi);
1982     // Get the projected qindex, based on the scaled target frame size (scaled
1983     // so target_bits_per_mb in vp9_rc_regulate_q will be correct target).
1984     target_bits_per_frame = (resize_action >= 0) ?
1985         rc->this_frame_target * tot_scale_change :
1986         rc->this_frame_target / tot_scale_change;
1987     active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
1988     qindex = vp9_rc_regulate_q(cpi,
1989                                target_bits_per_frame,
1990                                rc->best_quality,
1991                                active_worst_quality);
1992     // If resize is down, check if projected q index is close to worst_quality,
1993     // and if so, reduce the rate correction factor (since likely can afford
1994     // lower q for resized frame).
1995     if (resize_action > 0 &&
1996         qindex > 90 * cpi->rc.worst_quality / 100) {
1997       rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
1998     }
1999     // If resize is back up, check if projected q index is too much above the
2000     // current base_qindex, and if so, reduce the rate correction factor
2001     // (since prefer to keep q for resized frame at least close to previous q).
2002     if (resize_action < 0 &&
2003        qindex > 130 * cm->base_qindex / 100) {
2004       rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
2005     }
2006   }
2007   return resize_action;
2008 }
2009
2010 // Compute average source sad (temporal sad: between current source and
2011 // previous source) over a subset of superblocks. Use this is detect big changes
2012 // in content and allow rate control to react.
2013 // TODO(marpan): Superblock sad is computed again in variance partition for
2014 // non-rd mode (but based on last reconstructed frame). Should try to reuse
2015 // these computations.
2016 void vp9_avg_source_sad(VP9_COMP *cpi) {
2017   VP9_COMMON * const cm = &cpi->common;
2018   RATE_CONTROL *const rc = &cpi->rc;
2019   rc->high_source_sad = 0;
2020   if (cpi->Last_Source != NULL &&
2021       cpi->Last_Source->y_width == cm->width &&
2022       cpi->Last_Source->y_height == cm->height) {
2023     const uint8_t *src_y = cpi->Source->y_buffer;
2024     const int src_ystride = cpi->Source->y_stride;
2025     const uint8_t *last_src_y = cpi->Last_Source->y_buffer;
2026     const int last_src_ystride = cpi->Last_Source->y_stride;
2027     int sbi_row, sbi_col;
2028     const BLOCK_SIZE bsize = BLOCK_64X64;
2029     uint32_t min_thresh = 4000;
2030     float thresh = 8.0f;
2031     // Loop over sub-sample of frame, and compute average sad over 64x64 blocks.
2032     uint64_t avg_sad = 0;
2033     int num_samples = 0;
2034     int sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
2035     int sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
2036     for (sbi_row = 0; sbi_row < sb_rows; sbi_row ++) {
2037       for (sbi_col = 0; sbi_col < sb_cols; sbi_col ++) {
2038         // Checker-board pattern, ignore boundary.
2039         if ((sbi_row > 0 && sbi_col > 0) &&
2040             (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) &&
2041             ((sbi_row % 2 == 0 && sbi_col % 2 == 0) ||
2042             (sbi_row % 2 != 0 && sbi_col % 2 != 0))) {
2043           num_samples++;
2044           avg_sad += cpi->fn_ptr[bsize].sdf(src_y,
2045                                             src_ystride,
2046                                             last_src_y,
2047                                             last_src_ystride);
2048         }
2049         src_y += 64;
2050         last_src_y += 64;
2051       }
2052       src_y += (src_ystride << 6) - (sb_cols << 6);
2053       last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
2054     }
2055     if (num_samples > 0)
2056       avg_sad = avg_sad / num_samples;
2057     // Set high_source_sad flag if we detect very high increase in avg_sad
2058     // between current and the previous frame value(s). Use a minimum threshold
2059     // for cases where there is small change from content that is completely
2060     // static.
2061     if (cpi->oxcf.rc_mode == VPX_VBR) {
2062       min_thresh = 20000;
2063       thresh = 2.0f;
2064     }
2065     if (avg_sad >
2066         VPXMAX(min_thresh, (unsigned int)(rc->avg_source_sad  * thresh)) &&
2067         rc->frames_since_key > 1)
2068       rc->high_source_sad = 1;
2069     else
2070       rc->high_source_sad = 0;
2071     if (avg_sad > 0)
2072       rc->avg_source_sad = (rc->avg_source_sad + avg_sad) >> 1;
2073     // For VBR, under scene change/high content change, force golden refresh.
2074     if (cpi->oxcf.rc_mode == VPX_VBR &&
2075         rc->high_source_sad &&
2076         cpi->refresh_golden_frame == 0 &&
2077         cpi->ext_refresh_frame_flags_pending == 0) {
2078       int target;
2079       cpi->refresh_golden_frame = 1;
2080       rc->frames_till_gf_update_due = rc->baseline_gf_interval >> 1;
2081       if (rc->frames_till_gf_update_due > rc->frames_to_key)
2082         rc->frames_till_gf_update_due = rc->frames_to_key;
2083       rc->gfu_boost = DEFAULT_GF_BOOST;
2084       target = calc_pframe_target_size_one_pass_vbr(cpi);
2085       vp9_rc_set_frame_target(cpi, target);
2086     }
2087   }
2088 }
2089
2090 // Test if encoded frame will significantly overshoot the target bitrate, and
2091 // if so, set the QP, reset/adjust some rate control parameters, and return 1.
2092 int vp9_encodedframe_overshoot(VP9_COMP *cpi,
2093                                int frame_size,
2094                                int *q) {
2095   VP9_COMMON * const cm = &cpi->common;
2096   RATE_CONTROL *const rc = &cpi->rc;
2097   int thresh_qp = 3 * (rc->worst_quality >> 2);
2098   int thresh_rate = rc->avg_frame_bandwidth * 10;
2099   if (cm->base_qindex < thresh_qp &&
2100       frame_size > thresh_rate) {
2101     double rate_correction_factor =
2102         cpi->rc.rate_correction_factors[INTER_NORMAL];
2103     const int target_size = cpi->rc.avg_frame_bandwidth;
2104     double new_correction_factor;
2105     int target_bits_per_mb;
2106     double q2;
2107     int enumerator;
2108     // Force a re-encode, and for now use max-QP.
2109     *q = cpi->rc.worst_quality;
2110     // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
2111     // these parameters will affect QP selection for subsequent frames. If they
2112     // have settled down to a very different (low QP) state, then not adjusting
2113     // them may cause next frame to select low QP and overshoot again.
2114     cpi->rc.avg_frame_qindex[INTER_FRAME] = *q;
2115     rc->buffer_level = rc->optimal_buffer_level;
2116     rc->bits_off_target = rc->optimal_buffer_level;
2117     // Reset rate under/over-shoot flags.
2118     cpi->rc.rc_1_frame = 0;
2119     cpi->rc.rc_2_frame = 0;
2120     // Adjust rate correction factor.
2121     target_bits_per_mb = ((uint64_t)target_size << BPER_MB_NORMBITS) / cm->MBs;
2122     // Rate correction factor based on target_bits_per_mb and qp (==max_QP).
2123     // This comes from the inverse computation of vp9_rc_bits_per_mb().
2124     q2 = vp9_convert_qindex_to_q(*q, cm->bit_depth);
2125     enumerator = 1800000;  // Factor for inter frame.
2126     enumerator += (int)(enumerator * q2) >> 12;
2127     new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
2128     if (new_correction_factor > rate_correction_factor) {
2129       rate_correction_factor =
2130           VPXMIN(2.0 * rate_correction_factor, new_correction_factor);
2131       if (rate_correction_factor > MAX_BPB_FACTOR)
2132         rate_correction_factor = MAX_BPB_FACTOR;
2133       cpi->rc.rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
2134     }
2135     // For temporal layers, reset the rate control parametes across all
2136     // temporal layers.
2137     if (cpi->use_svc) {
2138       int i = 0;
2139       SVC *svc = &cpi->svc;
2140       for (i = 0; i < svc->number_temporal_layers; ++i) {
2141         const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
2142                                            svc->number_temporal_layers);
2143         LAYER_CONTEXT *lc = &svc->layer_context[layer];
2144         RATE_CONTROL *lrc = &lc->rc;
2145         lrc->avg_frame_qindex[INTER_FRAME] = *q;
2146         lrc->buffer_level = rc->optimal_buffer_level;
2147         lrc->bits_off_target = rc->optimal_buffer_level;
2148         lrc->rc_1_frame = 0;
2149         lrc->rc_2_frame = 0;
2150         lrc->rate_correction_factors[INTER_NORMAL] =
2151             rate_correction_factor;
2152       }
2153     }
2154     return 1;
2155   } else {
2156     return 0;
2157   }
2158 }