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