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_mem/vpx_mem.h"
19 
20 #include "vp9/common/vp9_alloccommon.h"
21 #include "vp9/common/vp9_common.h"
22 #include "vp9/common/vp9_entropymode.h"
23 #include "vp9/common/vp9_quant_common.h"
24 #include "vp9/common/vp9_seg_common.h"
25 #include "vp9/common/vp9_systemdependent.h"
26 
27 #include "vp9/encoder/vp9_encodemv.h"
28 #include "vp9/encoder/vp9_ratectrl.h"
29 
30 // Max rate target for 1080P and below encodes under normal circumstances
31 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
32 #define MAX_MB_RATE 250
33 #define MAXRATE_1080P 2025000
34 
35 #define DEFAULT_KF_BOOST 2000
36 #define DEFAULT_GF_BOOST 2000
37 
38 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
39 
40 #define MIN_BPB_FACTOR 0.005
41 #define MAX_BPB_FACTOR 50
42 
43 #define FRAME_OVERHEAD_BITS 200
44 
45 // Tables relating active max Q to active min Q
46 static int kf_low_motion_minq[QINDEX_RANGE];
47 static int kf_high_motion_minq[QINDEX_RANGE];
48 static int arfgf_low_motion_minq[QINDEX_RANGE];
49 static int arfgf_high_motion_minq[QINDEX_RANGE];
50 static int inter_minq[QINDEX_RANGE];
51 static int rtc_minq[QINDEX_RANGE];
52 static int gf_high = 2000;
53 static int gf_low = 400;
54 static int kf_high = 5000;
55 static int kf_low = 400;
56 
57 // Functions to compute the active minq lookup table entries based on a
58 // formulaic approach to facilitate easier adjustment of the Q tables.
59 // The formulae were derived from computing a 3rd order polynomial best
60 // 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)61 static int get_minq_index(double maxq, double x3, double x2, double x1) {
62   int i;
63   const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq,
64                                 maxq);
65 
66   // Special case handling to deal with the step from q2.0
67   // down to lossless mode represented by q 1.0.
68   if (minqtarget <= 2.0)
69     return 0;
70 
71   for (i = 0; i < QINDEX_RANGE; i++)
72     if (minqtarget <= vp9_convert_qindex_to_q(i))
73       return i;
74 
75   return QINDEX_RANGE - 1;
76 }
77 
vp9_rc_init_minq_luts()78 void vp9_rc_init_minq_luts() {
79   int i;
80 
81   for (i = 0; i < QINDEX_RANGE; i++) {
82     const double maxq = vp9_convert_qindex_to_q(i);
83     kf_low_motion_minq[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.125);
84     kf_high_motion_minq[i] = get_minq_index(maxq, 0.000002, -0.0012, 0.50);
85     arfgf_low_motion_minq[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30);
86     arfgf_high_motion_minq[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.50);
87     inter_minq[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90);
88     rtc_minq[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70);
89   }
90 }
91 
92 // These functions use formulaic calculations to make playing with the
93 // quantizer tables easier. If necessary they can be replaced by lookup
94 // tables if and when things settle down in the experimental bitstream
vp9_convert_qindex_to_q(int qindex)95 double vp9_convert_qindex_to_q(int qindex) {
96   // Convert the index to a real Q value (scaled down to match old Q values)
97   return vp9_ac_quant(qindex, 0) / 4.0;
98 }
99 
vp9_rc_bits_per_mb(FRAME_TYPE frame_type,int qindex,double correction_factor)100 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
101                        double correction_factor) {
102   const double q = vp9_convert_qindex_to_q(qindex);
103   int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000;
104 
105   // q based adjustment to baseline enumerator
106   enumerator += (int)(enumerator * q) >> 12;
107   return (int)(enumerator * correction_factor / q);
108 }
109 
estimate_bits_at_q(FRAME_TYPE frame_type,int q,int mbs,double correction_factor)110 static int estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
111                               double correction_factor) {
112   const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor));
113   return ((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS;
114 }
115 
vp9_rc_clamp_pframe_target_size(const VP9_COMP * const cpi,int target)116 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
117   const RATE_CONTROL *rc = &cpi->rc;
118   const int min_frame_target = MAX(rc->min_frame_bandwidth,
119                                    rc->avg_frame_bandwidth >> 5);
120   if (target < min_frame_target)
121     target = min_frame_target;
122   if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
123     // If there is an active ARF at this location use the minimum
124     // bits on this frame even if it is a constructed arf.
125     // The active maximum quantizer insures that an appropriate
126     // number of bits will be spent if needed for constructed ARFs.
127     target = min_frame_target;
128   }
129   // Clip the frame target to the maximum allowed value.
130   if (target > rc->max_frame_bandwidth)
131     target = rc->max_frame_bandwidth;
132   return target;
133 }
134 
vp9_rc_clamp_iframe_target_size(const VP9_COMP * const cpi,int target)135 int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
136   const RATE_CONTROL *rc = &cpi->rc;
137   const VP9EncoderConfig *oxcf = &cpi->oxcf;
138   if (oxcf->rc_max_intra_bitrate_pct) {
139     const int max_rate = rc->avg_frame_bandwidth *
140                              oxcf->rc_max_intra_bitrate_pct / 100;
141     target = MIN(target, max_rate);
142   }
143   if (target > rc->max_frame_bandwidth)
144     target = rc->max_frame_bandwidth;
145   return target;
146 }
147 
148 
149 // Update the buffer level for higher layers, given the encoded current layer.
update_layer_buffer_level(SVC * svc,int encoded_frame_size)150 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
151   int temporal_layer = 0;
152   int current_temporal_layer = svc->temporal_layer_id;
153   for (temporal_layer = current_temporal_layer + 1;
154       temporal_layer < svc->number_temporal_layers; ++temporal_layer) {
155     LAYER_CONTEXT *lc = &svc->layer_context[temporal_layer];
156     RATE_CONTROL *lrc = &lc->rc;
157     int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
158         encoded_frame_size);
159     lrc->bits_off_target += bits_off_for_this_layer;
160 
161     // Clip buffer level to maximum buffer size for the layer.
162     lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size);
163     lrc->buffer_level = lrc->bits_off_target;
164   }
165 }
166 
167 // Update the buffer level: leaky bucket model.
update_buffer_level(VP9_COMP * cpi,int encoded_frame_size)168 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
169   const VP9_COMMON *const cm = &cpi->common;
170   RATE_CONTROL *const rc = &cpi->rc;
171 
172   // Non-viewable frames are a special case and are treated as pure overhead.
173   if (!cm->show_frame) {
174     rc->bits_off_target -= encoded_frame_size;
175   } else {
176     rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
177   }
178 
179   // Clip the buffer level to the maximum specified buffer size.
180   rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
181   rc->buffer_level = rc->bits_off_target;
182 
183   if (cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR) {
184     update_layer_buffer_level(&cpi->svc, encoded_frame_size);
185   }
186 }
187 
vp9_rc_init(const VP9EncoderConfig * oxcf,int pass,RATE_CONTROL * rc)188 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
189   int i;
190 
191   if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
192     rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
193     rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
194   } else {
195     rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
196                                            oxcf->best_allowed_q) / 2;
197     rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
198                                            oxcf->best_allowed_q) / 2;
199   }
200 
201   rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
202   rc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
203 
204   rc->buffer_level =    rc->starting_buffer_level;
205   rc->bits_off_target = rc->starting_buffer_level;
206 
207   rc->rolling_target_bits      = rc->avg_frame_bandwidth;
208   rc->rolling_actual_bits      = rc->avg_frame_bandwidth;
209   rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
210   rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
211 
212   rc->total_actual_bits = 0;
213   rc->total_target_bits = 0;
214   rc->total_target_vs_actual = 0;
215 
216   rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
217   rc->frames_since_key = 8;  // Sensible default for first frame.
218   rc->this_key_frame_forced = 0;
219   rc->next_key_frame_forced = 0;
220   rc->source_alt_ref_pending = 0;
221   rc->source_alt_ref_active = 0;
222 
223   rc->frames_till_gf_update_due = 0;
224 
225   rc->ni_av_qi = oxcf->worst_allowed_q;
226   rc->ni_tot_qi = 0;
227   rc->ni_frames = 0;
228 
229   rc->tot_q = 0.0;
230   rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q);
231 
232   for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
233     rc->rate_correction_factors[i] = 1.0;
234   }
235 }
236 
vp9_rc_drop_frame(VP9_COMP * cpi)237 int vp9_rc_drop_frame(VP9_COMP *cpi) {
238   const VP9EncoderConfig *oxcf = &cpi->oxcf;
239   RATE_CONTROL *const rc = &cpi->rc;
240 
241   if (!oxcf->drop_frames_water_mark) {
242     return 0;
243   } else {
244     if (rc->buffer_level < 0) {
245       // Always drop if buffer is below 0.
246       return 1;
247     } else {
248       // If buffer is below drop_mark, for now just drop every other frame
249       // (starting with the next frame) until it increases back over drop_mark.
250       int drop_mark = (int)(oxcf->drop_frames_water_mark *
251           rc->optimal_buffer_level / 100);
252       if ((rc->buffer_level > drop_mark) &&
253           (rc->decimation_factor > 0)) {
254         --rc->decimation_factor;
255       } else if (rc->buffer_level <= drop_mark &&
256           rc->decimation_factor == 0) {
257         rc->decimation_factor = 1;
258       }
259       if (rc->decimation_factor > 0) {
260         if (rc->decimation_count > 0) {
261           --rc->decimation_count;
262           return 1;
263         } else {
264           rc->decimation_count = rc->decimation_factor;
265           return 0;
266         }
267       } else {
268         rc->decimation_count = 0;
269         return 0;
270       }
271     }
272   }
273 }
274 
get_rate_correction_factor(const VP9_COMP * cpi)275 static double get_rate_correction_factor(const VP9_COMP *cpi) {
276   const RATE_CONTROL *const rc = &cpi->rc;
277 
278   if (cpi->common.frame_type == KEY_FRAME) {
279     return rc->rate_correction_factors[KF_STD];
280   } else if (cpi->oxcf.pass == 2) {
281     RATE_FACTOR_LEVEL rf_lvl =
282       cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
283     return rc->rate_correction_factors[rf_lvl];
284   } else {
285     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
286         !rc->is_src_frame_alt_ref &&
287         !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR))
288       return rc->rate_correction_factors[GF_ARF_STD];
289     else
290       return rc->rate_correction_factors[INTER_NORMAL];
291   }
292 }
293 
set_rate_correction_factor(VP9_COMP * cpi,double factor)294 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
295   RATE_CONTROL *const rc = &cpi->rc;
296 
297   if (cpi->common.frame_type == KEY_FRAME) {
298     rc->rate_correction_factors[KF_STD] = factor;
299   } else if (cpi->oxcf.pass == 2) {
300     RATE_FACTOR_LEVEL rf_lvl =
301       cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
302     rc->rate_correction_factors[rf_lvl] = factor;
303   } else {
304     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
305         !rc->is_src_frame_alt_ref &&
306         !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR))
307       rc->rate_correction_factors[GF_ARF_STD] = factor;
308     else
309       rc->rate_correction_factors[INTER_NORMAL] = factor;
310   }
311 }
312 
vp9_rc_update_rate_correction_factors(VP9_COMP * cpi,int damp_var)313 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) {
314   const VP9_COMMON *const cm = &cpi->common;
315   int correction_factor = 100;
316   double rate_correction_factor = get_rate_correction_factor(cpi);
317   double adjustment_limit;
318 
319   int projected_size_based_on_q = 0;
320 
321   // Do not update the rate factors for arf overlay frames.
322   if (cpi->rc.is_src_frame_alt_ref)
323     return;
324 
325   // Clear down mmx registers to allow floating point in what follows
326   vp9_clear_system_state();
327 
328   // Work out how big we would have expected the frame to be at this Q given
329   // the current correction factor.
330   // Stay in double to avoid int overflow when values are large
331   projected_size_based_on_q = estimate_bits_at_q(cm->frame_type,
332                                                  cm->base_qindex, cm->MBs,
333                                                  rate_correction_factor);
334   // Work out a size correction factor.
335   if (projected_size_based_on_q > 0)
336     correction_factor = (100 * cpi->rc.projected_frame_size) /
337                             projected_size_based_on_q;
338 
339   // More heavily damped adjustment used if we have been oscillating either side
340   // of target.
341   switch (damp_var) {
342     case 0:
343       adjustment_limit = 0.75;
344       break;
345     case 1:
346       adjustment_limit = 0.375;
347       break;
348     case 2:
349     default:
350       adjustment_limit = 0.25;
351       break;
352   }
353 
354   if (correction_factor > 102) {
355     // We are not already at the worst allowable quality
356     correction_factor = (int)(100 + ((correction_factor - 100) *
357                                   adjustment_limit));
358     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
359 
360     // Keep rate_correction_factor within limits
361     if (rate_correction_factor > MAX_BPB_FACTOR)
362       rate_correction_factor = MAX_BPB_FACTOR;
363   } else if (correction_factor < 99) {
364     // We are not already at the best allowable quality
365     correction_factor = (int)(100 - ((100 - correction_factor) *
366                                   adjustment_limit));
367     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
368 
369     // Keep rate_correction_factor within limits
370     if (rate_correction_factor < MIN_BPB_FACTOR)
371       rate_correction_factor = MIN_BPB_FACTOR;
372   }
373 
374   set_rate_correction_factor(cpi, rate_correction_factor);
375 }
376 
377 
vp9_rc_regulate_q(const VP9_COMP * cpi,int target_bits_per_frame,int active_best_quality,int active_worst_quality)378 int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
379                       int active_best_quality, int active_worst_quality) {
380   const VP9_COMMON *const cm = &cpi->common;
381   int q = active_worst_quality;
382   int last_error = INT_MAX;
383   int i, target_bits_per_mb;
384   const double correction_factor = get_rate_correction_factor(cpi);
385 
386   // Calculate required scaling factor based on target frame size and size of
387   // frame produced using previous Q.
388   target_bits_per_mb =
389       ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
390 
391   i = active_best_quality;
392 
393   do {
394     const int bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i,
395                                                              correction_factor);
396 
397     if (bits_per_mb_at_this_q <= target_bits_per_mb) {
398       if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
399         q = i;
400       else
401         q = i - 1;
402 
403       break;
404     } else {
405       last_error = bits_per_mb_at_this_q - target_bits_per_mb;
406     }
407   } while (++i <= active_worst_quality);
408 
409   return q;
410 }
411 
get_active_quality(int q,int gfu_boost,int low,int high,int * low_motion_minq,int * high_motion_minq)412 static int get_active_quality(int q, int gfu_boost, int low, int high,
413                               int *low_motion_minq, int *high_motion_minq) {
414   if (gfu_boost > high) {
415     return low_motion_minq[q];
416   } else if (gfu_boost < low) {
417     return high_motion_minq[q];
418   } else {
419     const int gap = high - low;
420     const int offset = high - gfu_boost;
421     const int qdiff = high_motion_minq[q] - low_motion_minq[q];
422     const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
423     return low_motion_minq[q] + adjustment;
424   }
425 }
426 
get_kf_active_quality(const RATE_CONTROL * const rc,int q)427 static int get_kf_active_quality(const RATE_CONTROL *const rc, int q) {
428   return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
429                             kf_low_motion_minq, kf_high_motion_minq);
430 }
431 
get_gf_active_quality(const RATE_CONTROL * const rc,int q)432 static int get_gf_active_quality(const RATE_CONTROL *const rc, int q) {
433   return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
434                             arfgf_low_motion_minq, arfgf_high_motion_minq);
435 }
436 
calc_active_worst_quality_one_pass_vbr(const VP9_COMP * cpi)437 static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
438   const RATE_CONTROL *const rc = &cpi->rc;
439   const unsigned int curr_frame = cpi->common.current_video_frame;
440   int active_worst_quality;
441 
442   if (cpi->common.frame_type == KEY_FRAME) {
443     active_worst_quality = curr_frame == 0 ? rc->worst_quality
444                                            : rc->last_q[KEY_FRAME] * 2;
445   } else {
446     if (!rc->is_src_frame_alt_ref &&
447         (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
448       active_worst_quality =  curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
449                                               : rc->last_q[INTER_FRAME];
450     } else {
451       active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
452                                              : rc->last_q[INTER_FRAME] * 2;
453     }
454   }
455   return MIN(active_worst_quality, rc->worst_quality);
456 }
457 
458 // Adjust active_worst_quality level based on buffer level.
calc_active_worst_quality_one_pass_cbr(const VP9_COMP * cpi)459 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
460   // Adjust active_worst_quality: If buffer is above the optimal/target level,
461   // bring active_worst_quality down depending on fullness of buffer.
462   // If buffer is below the optimal level, let the active_worst_quality go from
463   // ambient Q (at buffer = optimal level) to worst_quality level
464   // (at buffer = critical level).
465   const VP9_COMMON *const cm = &cpi->common;
466   const RATE_CONTROL *rc = &cpi->rc;
467   // Buffer level below which we push active_worst to worst_quality.
468   int64_t critical_level = rc->optimal_buffer_level >> 2;
469   int64_t buff_lvl_step = 0;
470   int adjustment = 0;
471   int active_worst_quality;
472   if (cm->frame_type == KEY_FRAME)
473     return rc->worst_quality;
474   if (cm->current_video_frame > 1)
475     active_worst_quality = MIN(rc->worst_quality,
476                                rc->avg_frame_qindex[INTER_FRAME] * 5 / 4);
477   else
478     active_worst_quality = MIN(rc->worst_quality,
479                                rc->avg_frame_qindex[KEY_FRAME] * 3 / 2);
480   if (rc->buffer_level > rc->optimal_buffer_level) {
481     // Adjust down.
482     // Maximum limit for down adjustment, ~30%.
483     int max_adjustment_down = active_worst_quality / 3;
484     if (max_adjustment_down) {
485       buff_lvl_step = ((rc->maximum_buffer_size -
486                         rc->optimal_buffer_level) / max_adjustment_down);
487       if (buff_lvl_step)
488         adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
489                             buff_lvl_step);
490       active_worst_quality -= adjustment;
491     }
492   } else if (rc->buffer_level > critical_level) {
493     // Adjust up from ambient Q.
494     if (critical_level) {
495       buff_lvl_step = (rc->optimal_buffer_level - critical_level);
496       if (buff_lvl_step) {
497         adjustment =
498             (int)((rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) *
499                   (rc->optimal_buffer_level - rc->buffer_level) /
500                   buff_lvl_step);
501       }
502       active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment;
503     }
504   } else {
505     // Set to worst_quality if buffer is below critical level.
506     active_worst_quality = rc->worst_quality;
507   }
508   return active_worst_quality;
509 }
510 
rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP * cpi,int * bottom_index,int * top_index)511 static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
512                                              int *bottom_index,
513                                              int *top_index) {
514   const VP9_COMMON *const cm = &cpi->common;
515   const RATE_CONTROL *const rc = &cpi->rc;
516   int active_best_quality;
517   int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
518   int q;
519 
520   if (frame_is_intra_only(cm)) {
521     active_best_quality = rc->best_quality;
522     // Handle the special case for key frames forced when we have75 reached
523     // the maximum key frame interval. Here force the Q to a range
524     // based on the ambient Q to reduce the risk of popping.
525     if (rc->this_key_frame_forced) {
526       int qindex = rc->last_boosted_qindex;
527       double last_boosted_q = vp9_convert_qindex_to_q(qindex);
528       int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
529                                             (last_boosted_q * 0.75));
530       active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
531     } else if (cm->current_video_frame > 0) {
532       // not first frame of one pass and kf_boost is set
533       double q_adj_factor = 1.0;
534       double q_val;
535 
536       active_best_quality =
537           get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME]);
538 
539       // Allow somewhat lower kf minq with small image formats.
540       if ((cm->width * cm->height) <= (352 * 288)) {
541         q_adj_factor -= 0.25;
542       }
543 
544       // Convert the adjustment factor to a qindex delta
545       // on active_best_quality.
546       q_val = vp9_convert_qindex_to_q(active_best_quality);
547       active_best_quality += vp9_compute_qdelta(rc, q_val,
548                                                 q_val * q_adj_factor);
549     }
550   } else if (!rc->is_src_frame_alt_ref &&
551              !cpi->use_svc &&
552              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
553     // Use the lower of active_worst_quality and recent
554     // average Q as basis for GF/ARF best Q limit unless last frame was
555     // a key frame.
556     if (rc->frames_since_key > 1 &&
557         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
558       q = rc->avg_frame_qindex[INTER_FRAME];
559     } else {
560       q = active_worst_quality;
561     }
562     active_best_quality = get_gf_active_quality(rc, q);
563   } else {
564     // Use the lower of active_worst_quality and recent/average Q.
565     if (cm->current_video_frame > 1) {
566       if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
567         active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
568       else
569         active_best_quality = rtc_minq[active_worst_quality];
570     } else {
571       if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
572         active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
573       else
574         active_best_quality = rtc_minq[active_worst_quality];
575     }
576   }
577 
578   // Clip the active best and worst quality values to limits
579   active_best_quality = clamp(active_best_quality,
580                               rc->best_quality, rc->worst_quality);
581   active_worst_quality = clamp(active_worst_quality,
582                                active_best_quality, rc->worst_quality);
583 
584   *top_index = active_worst_quality;
585   *bottom_index = active_best_quality;
586 
587 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
588   // Limit Q range for the adaptive loop.
589   if (cm->frame_type == KEY_FRAME &&
590       !rc->this_key_frame_forced  &&
591       !(cm->current_video_frame == 0)) {
592     int qdelta = 0;
593     vp9_clear_system_state();
594     qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
595                                         active_worst_quality, 2.0);
596     *top_index = active_worst_quality + qdelta;
597     *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
598   }
599 #endif
600 
601   // Special case code to try and match quality with forced key frames
602   if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
603     q = rc->last_boosted_qindex;
604   } else {
605     q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
606                           active_best_quality, active_worst_quality);
607     if (q > *top_index) {
608       // Special case when we are targeting the max allowed rate
609       if (rc->this_frame_target >= rc->max_frame_bandwidth)
610         *top_index = q;
611       else
612         q = *top_index;
613     }
614   }
615   assert(*top_index <= rc->worst_quality &&
616          *top_index >= rc->best_quality);
617   assert(*bottom_index <= rc->worst_quality &&
618          *bottom_index >= rc->best_quality);
619   assert(q <= rc->worst_quality && q >= rc->best_quality);
620   return q;
621 }
622 
get_active_cq_level(const RATE_CONTROL * rc,const VP9EncoderConfig * const oxcf)623 static int get_active_cq_level(const RATE_CONTROL *rc,
624                                const VP9EncoderConfig *const oxcf) {
625   static const double cq_adjust_threshold = 0.5;
626   int active_cq_level = oxcf->cq_level;
627   if (oxcf->rc_mode == VPX_CQ &&
628       rc->total_target_bits > 0) {
629     const double x = (double)rc->total_actual_bits / rc->total_target_bits;
630     if (x < cq_adjust_threshold) {
631       active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
632     }
633   }
634   return active_cq_level;
635 }
636 
rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP * cpi,int * bottom_index,int * top_index)637 static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
638                                              int *bottom_index,
639                                              int *top_index) {
640   const VP9_COMMON *const cm = &cpi->common;
641   const RATE_CONTROL *const rc = &cpi->rc;
642   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
643   const int cq_level = get_active_cq_level(rc, oxcf);
644   int active_best_quality;
645   int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
646   int q;
647 
648   if (frame_is_intra_only(cm)) {
649     active_best_quality = rc->best_quality;
650 
651     // Handle the special case for key frames forced when we have reached
652     // the maximum key frame interval. Here force the Q to a range
653     // based on the ambient Q to reduce the risk of popping.
654     if (rc->this_key_frame_forced) {
655       int qindex = rc->last_boosted_qindex;
656       double last_boosted_q = vp9_convert_qindex_to_q(qindex);
657       int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
658                                             last_boosted_q * 0.75);
659       active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
660     } else {
661       // not first frame of one pass and kf_boost is set
662       double q_adj_factor = 1.0;
663       double q_val;
664 
665       active_best_quality =
666           get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME]);
667 
668       // Allow somewhat lower kf minq with small image formats.
669       if ((cm->width * cm->height) <= (352 * 288)) {
670         q_adj_factor -= 0.25;
671       }
672 
673       // Convert the adjustment factor to a qindex delta
674       // on active_best_quality.
675       q_val = vp9_convert_qindex_to_q(active_best_quality);
676       active_best_quality += vp9_compute_qdelta(rc, q_val,
677                                                 q_val * q_adj_factor);
678     }
679   } else if (!rc->is_src_frame_alt_ref &&
680              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
681     // Use the lower of active_worst_quality and recent
682     // average Q as basis for GF/ARF best Q limit unless last frame was
683     // a key frame.
684     if (rc->frames_since_key > 1 &&
685         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
686       q = rc->avg_frame_qindex[INTER_FRAME];
687     } else {
688       q = rc->avg_frame_qindex[KEY_FRAME];
689     }
690     // For constrained quality dont allow Q less than the cq level
691     if (oxcf->rc_mode == VPX_CQ) {
692       if (q < cq_level)
693         q = cq_level;
694 
695       active_best_quality = get_gf_active_quality(rc, q);
696 
697       // Constrained quality use slightly lower active best.
698       active_best_quality = active_best_quality * 15 / 16;
699 
700     } else if (oxcf->rc_mode == VPX_Q) {
701       if (!cpi->refresh_alt_ref_frame) {
702         active_best_quality = cq_level;
703       } else {
704         active_best_quality = get_gf_active_quality(rc, q);
705       }
706     } else {
707       active_best_quality = get_gf_active_quality(rc, q);
708     }
709   } else {
710     if (oxcf->rc_mode == VPX_Q) {
711       active_best_quality = cq_level;
712     } else {
713       // Use the lower of active_worst_quality and recent/average Q.
714       if (cm->current_video_frame > 1)
715         active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
716       else
717         active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
718       // For the constrained quality mode we don't want
719       // q to fall below the cq level.
720       if ((oxcf->rc_mode == VPX_CQ) &&
721           (active_best_quality < cq_level)) {
722         active_best_quality = cq_level;
723       }
724     }
725   }
726 
727   // Clip the active best and worst quality values to limits
728   active_best_quality = clamp(active_best_quality,
729                               rc->best_quality, rc->worst_quality);
730   active_worst_quality = clamp(active_worst_quality,
731                                active_best_quality, rc->worst_quality);
732 
733   *top_index = active_worst_quality;
734   *bottom_index = active_best_quality;
735 
736 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
737   {
738     int qdelta = 0;
739     vp9_clear_system_state();
740 
741     // Limit Q range for the adaptive loop.
742     if (cm->frame_type == KEY_FRAME &&
743         !rc->this_key_frame_forced &&
744         !(cm->current_video_frame == 0)) {
745       qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
746                                           active_worst_quality, 2.0);
747     } else if (!rc->is_src_frame_alt_ref &&
748                (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
749       qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
750                                           active_worst_quality, 1.75);
751     }
752     *top_index = active_worst_quality + qdelta;
753     *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
754   }
755 #endif
756 
757   if (oxcf->rc_mode == VPX_Q) {
758     q = active_best_quality;
759   // Special case code to try and match quality with forced key frames
760   } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
761     q = rc->last_boosted_qindex;
762   } else {
763     q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
764                           active_best_quality, active_worst_quality);
765     if (q > *top_index) {
766       // Special case when we are targeting the max allowed rate
767       if (rc->this_frame_target >= rc->max_frame_bandwidth)
768         *top_index = q;
769       else
770         q = *top_index;
771     }
772   }
773 
774   assert(*top_index <= rc->worst_quality &&
775          *top_index >= rc->best_quality);
776   assert(*bottom_index <= rc->worst_quality &&
777          *bottom_index >= rc->best_quality);
778   assert(q <= rc->worst_quality && q >= rc->best_quality);
779   return q;
780 }
781 
rc_pick_q_and_bounds_two_pass(const VP9_COMP * cpi,int * bottom_index,int * top_index)782 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi,
783                                          int *bottom_index,
784                                          int *top_index) {
785   const VP9_COMMON *const cm = &cpi->common;
786   const RATE_CONTROL *const rc = &cpi->rc;
787   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
788   const int cq_level = get_active_cq_level(rc, oxcf);
789   int active_best_quality;
790   int active_worst_quality = cpi->twopass.active_worst_quality;
791   int q;
792 
793   if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
794     // Handle the special case for key frames forced when we have75 reached
795     // the maximum key frame interval. Here force the Q to a range
796     // based on the ambient Q to reduce the risk of popping.
797     if (rc->this_key_frame_forced) {
798       int qindex = rc->last_boosted_qindex;
799       double last_boosted_q = vp9_convert_qindex_to_q(qindex);
800       int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
801                                             last_boosted_q * 0.75);
802       active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
803     } else {
804       // Not forced keyframe.
805       double q_adj_factor = 1.0;
806       double q_val;
807       // Baseline value derived from cpi->active_worst_quality and kf boost.
808       active_best_quality = get_kf_active_quality(rc, active_worst_quality);
809 
810       // Allow somewhat lower kf minq with small image formats.
811       if ((cm->width * cm->height) <= (352 * 288)) {
812         q_adj_factor -= 0.25;
813       }
814 
815       // Make a further adjustment based on the kf zero motion measure.
816       q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
817 
818       // Convert the adjustment factor to a qindex delta
819       // on active_best_quality.
820       q_val = vp9_convert_qindex_to_q(active_best_quality);
821       active_best_quality += vp9_compute_qdelta(rc, q_val,
822                                                 q_val * q_adj_factor);
823     }
824   } else if (!rc->is_src_frame_alt_ref &&
825              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
826     // Use the lower of active_worst_quality and recent
827     // average Q as basis for GF/ARF best Q limit unless last frame was
828     // a key frame.
829     if (rc->frames_since_key > 1 &&
830         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
831       q = rc->avg_frame_qindex[INTER_FRAME];
832     } else {
833       q = active_worst_quality;
834     }
835     // For constrained quality dont allow Q less than the cq level
836     if (oxcf->rc_mode == VPX_CQ) {
837       if (q < cq_level)
838         q = cq_level;
839 
840       active_best_quality = get_gf_active_quality(rc, q);
841 
842       // Constrained quality use slightly lower active best.
843       active_best_quality = active_best_quality * 15 / 16;
844 
845     } else if (oxcf->rc_mode == VPX_Q) {
846       if (!cpi->refresh_alt_ref_frame) {
847         active_best_quality = cq_level;
848       } else {
849         active_best_quality = get_gf_active_quality(rc, q);
850       }
851     } else {
852       active_best_quality = get_gf_active_quality(rc, q);
853     }
854   } else {
855     if (oxcf->rc_mode == VPX_Q) {
856       active_best_quality = cq_level;
857     } else {
858       active_best_quality = inter_minq[active_worst_quality];
859 
860       // For the constrained quality mode we don't want
861       // q to fall below the cq level.
862       if ((oxcf->rc_mode == VPX_CQ) &&
863           (active_best_quality < cq_level)) {
864         active_best_quality = cq_level;
865       }
866     }
867   }
868 
869   // Clip the active best and worst quality values to limits.
870   active_best_quality = clamp(active_best_quality,
871                               rc->best_quality, rc->worst_quality);
872   active_worst_quality = clamp(active_worst_quality,
873                                active_best_quality, rc->worst_quality);
874 
875   *top_index = active_worst_quality;
876   *bottom_index = active_best_quality;
877 
878 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
879   vp9_clear_system_state();
880   {
881     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
882     const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
883       1.00,  // INTER_NORMAL
884       1.00,  // INTER_HIGH
885       1.50,  // GF_ARF_LOW
886       1.75,  // GF_ARF_STD
887       2.00,  // KF_STD
888     };
889     const double rate_factor =
890       rate_factor_deltas[gf_group->rf_level[gf_group->index]];
891     int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
892                                             active_worst_quality, rate_factor);
893     *top_index = active_worst_quality + qdelta;
894     *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
895   }
896 #endif
897 
898   if (oxcf->rc_mode == VPX_Q) {
899     q = active_best_quality;
900   // Special case code to try and match quality with forced key frames.
901   } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
902     q = rc->last_boosted_qindex;
903   } else {
904     q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
905                           active_best_quality, active_worst_quality);
906     if (q > *top_index) {
907       // Special case when we are targeting the max allowed rate.
908       if (rc->this_frame_target >= rc->max_frame_bandwidth)
909         *top_index = q;
910       else
911         q = *top_index;
912     }
913   }
914 
915   assert(*top_index <= rc->worst_quality &&
916          *top_index >= rc->best_quality);
917   assert(*bottom_index <= rc->worst_quality &&
918          *bottom_index >= rc->best_quality);
919   assert(q <= rc->worst_quality && q >= rc->best_quality);
920   return q;
921 }
922 
vp9_rc_pick_q_and_bounds(const VP9_COMP * cpi,int * bottom_index,int * top_index)923 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi,
924                              int *bottom_index, int *top_index) {
925   int q;
926   if (cpi->oxcf.pass == 0) {
927     if (cpi->oxcf.rc_mode == VPX_CBR)
928       q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
929     else
930       q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
931   } else {
932     q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
933   }
934   if (cpi->sf.use_nonrd_pick_mode) {
935     if (cpi->sf.force_frame_boost == 1)
936       q -= cpi->sf.max_delta_qindex;
937 
938     if (q < *bottom_index)
939       *bottom_index = q;
940     else if (q > *top_index)
941       *top_index = q;
942   }
943   return q;
944 }
945 
vp9_rc_compute_frame_size_bounds(const VP9_COMP * cpi,int frame_target,int * frame_under_shoot_limit,int * frame_over_shoot_limit)946 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi,
947                                       int frame_target,
948                                       int *frame_under_shoot_limit,
949                                       int *frame_over_shoot_limit) {
950   if (cpi->oxcf.rc_mode == VPX_Q) {
951     *frame_under_shoot_limit = 0;
952     *frame_over_shoot_limit  = INT_MAX;
953   } else {
954     // For very small rate targets where the fractional adjustment
955     // may be tiny make sure there is at least a minimum range.
956     const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100;
957     *frame_under_shoot_limit = MAX(frame_target - tolerance - 200, 0);
958     *frame_over_shoot_limit = MIN(frame_target + tolerance + 200,
959                                   cpi->rc.max_frame_bandwidth);
960   }
961 }
962 
vp9_rc_set_frame_target(VP9_COMP * cpi,int target)963 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
964   const VP9_COMMON *const cm = &cpi->common;
965   RATE_CONTROL *const rc = &cpi->rc;
966 
967   rc->this_frame_target = target;
968 
969   // Target rate per SB64 (including partial SB64s.
970   rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) /
971                              (cm->width * cm->height);
972 }
973 
update_alt_ref_frame_stats(VP9_COMP * cpi)974 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
975   // this frame refreshes means next frames don't unless specified by user
976   RATE_CONTROL *const rc = &cpi->rc;
977   rc->frames_since_golden = 0;
978 
979   // Mark the alt ref as done (setting to 0 means no further alt refs pending).
980   rc->source_alt_ref_pending = 0;
981 
982   // Set the alternate reference frame active flag
983   rc->source_alt_ref_active = 1;
984 }
985 
update_golden_frame_stats(VP9_COMP * cpi)986 static void update_golden_frame_stats(VP9_COMP *cpi) {
987   RATE_CONTROL *const rc = &cpi->rc;
988 
989   // Update the Golden frame usage counts.
990   if (cpi->refresh_golden_frame) {
991     // this frame refreshes means next frames don't unless specified by user
992     rc->frames_since_golden = 0;
993 
994     if (cpi->oxcf.pass == 2) {
995       if (!rc->source_alt_ref_pending &&
996           cpi->twopass.gf_group.rf_level[0] == GF_ARF_STD)
997       rc->source_alt_ref_active = 0;
998     } else if (!rc->source_alt_ref_pending) {
999       rc->source_alt_ref_active = 0;
1000     }
1001 
1002     // Decrement count down till next gf
1003     if (rc->frames_till_gf_update_due > 0)
1004       rc->frames_till_gf_update_due--;
1005 
1006   } else if (!cpi->refresh_alt_ref_frame) {
1007     // Decrement count down till next gf
1008     if (rc->frames_till_gf_update_due > 0)
1009       rc->frames_till_gf_update_due--;
1010 
1011     rc->frames_since_golden++;
1012   }
1013 }
1014 
vp9_rc_postencode_update(VP9_COMP * cpi,uint64_t bytes_used)1015 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
1016   const VP9_COMMON *const cm = &cpi->common;
1017   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1018   RATE_CONTROL *const rc = &cpi->rc;
1019   const int qindex = cm->base_qindex;
1020 
1021   // Update rate control heuristics
1022   rc->projected_frame_size = (int)(bytes_used << 3);
1023 
1024   // Post encode loop adjustment of Q prediction.
1025   vp9_rc_update_rate_correction_factors(
1026       cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF ||
1027             oxcf->rc_mode == VPX_CBR) ? 2 : 0);
1028 
1029   // Keep a record of last Q and ambient average Q.
1030   if (cm->frame_type == KEY_FRAME) {
1031     rc->last_q[KEY_FRAME] = qindex;
1032     rc->avg_frame_qindex[KEY_FRAME] =
1033         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1034   } else {
1035     if (rc->is_src_frame_alt_ref ||
1036         !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) ||
1037         (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) {
1038       rc->last_q[INTER_FRAME] = qindex;
1039       rc->avg_frame_qindex[INTER_FRAME] =
1040         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1041       rc->ni_frames++;
1042       rc->tot_q += vp9_convert_qindex_to_q(qindex);
1043       rc->avg_q = rc->tot_q / rc->ni_frames;
1044       // Calculate the average Q for normal inter frames (not key or GFU
1045       // frames).
1046       rc->ni_tot_qi += qindex;
1047       rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1048     }
1049   }
1050 
1051   // Keep record of last boosted (KF/KF/ARF) Q value.
1052   // If the current frame is coded at a lower Q then we also update it.
1053   // If all mbs in this group are skipped only update if the Q value is
1054   // better than that already stored.
1055   // This is used to help set quality in forced key frames to reduce popping
1056   if ((qindex < rc->last_boosted_qindex) ||
1057       ((cpi->static_mb_pct < 100) &&
1058        ((cm->frame_type == KEY_FRAME) || cpi->refresh_alt_ref_frame ||
1059         (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1060     rc->last_boosted_qindex = qindex;
1061   }
1062 
1063   update_buffer_level(cpi, rc->projected_frame_size);
1064 
1065   // Rolling monitors of whether we are over or underspending used to help
1066   // regulate min and Max Q in two pass.
1067   if (cm->frame_type != KEY_FRAME) {
1068     rc->rolling_target_bits = ROUND_POWER_OF_TWO(
1069         rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1070     rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
1071         rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1072     rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
1073         rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1074     rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
1075         rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1076   }
1077 
1078   // Actual bits spent
1079   rc->total_actual_bits += rc->projected_frame_size;
1080   rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1081 
1082   rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1083 
1084   if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1085       (cm->frame_type != KEY_FRAME))
1086     // Update the alternate reference frame stats as appropriate.
1087     update_alt_ref_frame_stats(cpi);
1088   else
1089     // Update the Golden frame stats as appropriate.
1090     update_golden_frame_stats(cpi);
1091 
1092   if (cm->frame_type == KEY_FRAME)
1093     rc->frames_since_key = 0;
1094   if (cm->show_frame) {
1095     rc->frames_since_key++;
1096     rc->frames_to_key--;
1097   }
1098 }
1099 
vp9_rc_postencode_update_drop_frame(VP9_COMP * cpi)1100 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
1101   // Update buffer level with zero size, update frame counters, and return.
1102   update_buffer_level(cpi, 0);
1103   cpi->common.last_frame_type = cpi->common.frame_type;
1104   cpi->rc.frames_since_key++;
1105   cpi->rc.frames_to_key--;
1106 }
1107 
1108 // Use this macro to turn on/off use of alt-refs in one-pass mode.
1109 #define USE_ALTREF_FOR_ONE_PASS   1
1110 
calc_pframe_target_size_one_pass_vbr(const VP9_COMP * const cpi)1111 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
1112   static const int af_ratio = 10;
1113   const RATE_CONTROL *const rc = &cpi->rc;
1114   int target;
1115 #if USE_ALTREF_FOR_ONE_PASS
1116   target = (!rc->is_src_frame_alt_ref &&
1117             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) ?
1118       (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
1119       (rc->baseline_gf_interval + af_ratio - 1) :
1120       (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
1121       (rc->baseline_gf_interval + af_ratio - 1);
1122 #else
1123   target = rc->avg_frame_bandwidth;
1124 #endif
1125   return vp9_rc_clamp_pframe_target_size(cpi, target);
1126 }
1127 
calc_iframe_target_size_one_pass_vbr(const VP9_COMP * const cpi)1128 static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
1129   static const int kf_ratio = 25;
1130   const RATE_CONTROL *rc = &cpi->rc;
1131   const int target = rc->avg_frame_bandwidth * kf_ratio;
1132   return vp9_rc_clamp_iframe_target_size(cpi, target);
1133 }
1134 
vp9_rc_get_one_pass_vbr_params(VP9_COMP * cpi)1135 void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
1136   VP9_COMMON *const cm = &cpi->common;
1137   RATE_CONTROL *const rc = &cpi->rc;
1138   int target;
1139   // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
1140   if (!cpi->refresh_alt_ref_frame &&
1141       (cm->current_video_frame == 0 ||
1142        (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1143        rc->frames_to_key == 0 ||
1144        (cpi->oxcf.auto_key && 0))) {
1145     cm->frame_type = KEY_FRAME;
1146     rc->this_key_frame_forced = cm->current_video_frame != 0 &&
1147                                 rc->frames_to_key == 0;
1148     rc->frames_to_key = cpi->oxcf.key_freq;
1149     rc->kf_boost = DEFAULT_KF_BOOST;
1150     rc->source_alt_ref_active = 0;
1151   } else {
1152     cm->frame_type = INTER_FRAME;
1153   }
1154   if (rc->frames_till_gf_update_due == 0) {
1155     rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1156     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1157     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
1158     if (rc->frames_till_gf_update_due > rc->frames_to_key)
1159       rc->frames_till_gf_update_due = rc->frames_to_key;
1160     cpi->refresh_golden_frame = 1;
1161     rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
1162     rc->gfu_boost = DEFAULT_GF_BOOST;
1163   }
1164   if (cm->frame_type == KEY_FRAME)
1165     target = calc_iframe_target_size_one_pass_vbr(cpi);
1166   else
1167     target = calc_pframe_target_size_one_pass_vbr(cpi);
1168   vp9_rc_set_frame_target(cpi, target);
1169 }
1170 
calc_pframe_target_size_one_pass_cbr(const VP9_COMP * cpi)1171 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1172   const VP9EncoderConfig *oxcf = &cpi->oxcf;
1173   const RATE_CONTROL *rc = &cpi->rc;
1174   const SVC *const svc = &cpi->svc;
1175   const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
1176   const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
1177   int min_frame_target = MAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
1178   int target = rc->avg_frame_bandwidth;
1179   if (svc->number_temporal_layers > 1 &&
1180       oxcf->rc_mode == VPX_CBR) {
1181     // Note that for layers, avg_frame_bandwidth is the cumulative
1182     // per-frame-bandwidth. For the target size of this frame, use the
1183     // layer average frame size (i.e., non-cumulative per-frame-bw).
1184     int current_temporal_layer = svc->temporal_layer_id;
1185     const LAYER_CONTEXT *lc = &svc->layer_context[current_temporal_layer];
1186     target = lc->avg_frame_size;
1187     min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
1188   }
1189   if (diff > 0) {
1190     // Lower the target bandwidth for this frame.
1191     const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct);
1192     target -= (target * pct_low) / 200;
1193   } else if (diff < 0) {
1194     // Increase the target bandwidth for this frame.
1195     const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
1196     target += (target * pct_high) / 200;
1197   }
1198   return MAX(min_frame_target, target);
1199 }
1200 
calc_iframe_target_size_one_pass_cbr(const VP9_COMP * cpi)1201 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1202   const RATE_CONTROL *rc = &cpi->rc;
1203   const VP9EncoderConfig *oxcf = &cpi->oxcf;
1204   const SVC *const svc = &cpi->svc;
1205   int target;
1206   if (cpi->common.current_video_frame == 0) {
1207     target = ((rc->starting_buffer_level / 2) > INT_MAX)
1208       ? INT_MAX : (int)(rc->starting_buffer_level / 2);
1209   } else {
1210     int kf_boost = 32;
1211     double framerate = oxcf->framerate;
1212     if (svc->number_temporal_layers > 1 &&
1213         oxcf->rc_mode == VPX_CBR) {
1214       // Use the layer framerate for temporal layers CBR mode.
1215       const LAYER_CONTEXT *lc = &svc->layer_context[svc->temporal_layer_id];
1216       framerate = lc->framerate;
1217     }
1218     kf_boost = MAX(kf_boost, (int)(2 * framerate - 16));
1219     if (rc->frames_since_key <  framerate / 2) {
1220       kf_boost = (int)(kf_boost * rc->frames_since_key /
1221                        (framerate / 2));
1222     }
1223     target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
1224   }
1225   return vp9_rc_clamp_iframe_target_size(cpi, target);
1226 }
1227 
vp9_rc_get_svc_params(VP9_COMP * cpi)1228 void vp9_rc_get_svc_params(VP9_COMP *cpi) {
1229   VP9_COMMON *const cm = &cpi->common;
1230   RATE_CONTROL *const rc = &cpi->rc;
1231   int target = rc->avg_frame_bandwidth;
1232   if ((cm->current_video_frame == 0) ||
1233       (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1234       (cpi->oxcf.auto_key && (rc->frames_since_key %
1235           cpi->oxcf.key_freq == 0))) {
1236     cm->frame_type = KEY_FRAME;
1237     rc->source_alt_ref_active = 0;
1238 
1239     if (is_spatial_svc(cpi)) {
1240       cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame = 1;
1241       cpi->ref_frame_flags &=
1242           (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
1243     }
1244 
1245     if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) {
1246       target = calc_iframe_target_size_one_pass_cbr(cpi);
1247     }
1248   } else {
1249     cm->frame_type = INTER_FRAME;
1250 
1251     if (is_spatial_svc(cpi)) {
1252       LAYER_CONTEXT *lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
1253       if (cpi->svc.spatial_layer_id == 0) {
1254         lc->is_key_frame = 0;
1255       } else {
1256         lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
1257         if (lc->is_key_frame)
1258           cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
1259       }
1260       cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
1261     }
1262 
1263     if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) {
1264       target = calc_pframe_target_size_one_pass_cbr(cpi);
1265     }
1266   }
1267   vp9_rc_set_frame_target(cpi, target);
1268   rc->frames_till_gf_update_due = INT_MAX;
1269   rc->baseline_gf_interval = INT_MAX;
1270 }
1271 
vp9_rc_get_one_pass_cbr_params(VP9_COMP * cpi)1272 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
1273   VP9_COMMON *const cm = &cpi->common;
1274   RATE_CONTROL *const rc = &cpi->rc;
1275   int target;
1276   // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
1277   if ((cm->current_video_frame == 0 ||
1278       (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1279       rc->frames_to_key == 0 ||
1280       (cpi->oxcf.auto_key && 0))) {
1281     cm->frame_type = KEY_FRAME;
1282     rc->this_key_frame_forced = cm->current_video_frame != 0 &&
1283                                 rc->frames_to_key == 0;
1284     rc->frames_to_key = cpi->oxcf.key_freq;
1285     rc->kf_boost = DEFAULT_KF_BOOST;
1286     rc->source_alt_ref_active = 0;
1287     target = calc_iframe_target_size_one_pass_cbr(cpi);
1288   } else {
1289     cm->frame_type = INTER_FRAME;
1290     target = calc_pframe_target_size_one_pass_cbr(cpi);
1291   }
1292   vp9_rc_set_frame_target(cpi, target);
1293   // Don't use gf_update by default in CBR mode.
1294   rc->frames_till_gf_update_due = INT_MAX;
1295   rc->baseline_gf_interval = INT_MAX;
1296 }
1297 
vp9_compute_qdelta(const RATE_CONTROL * rc,double qstart,double qtarget)1298 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget) {
1299   int start_index = rc->worst_quality;
1300   int target_index = rc->worst_quality;
1301   int i;
1302 
1303   // Convert the average q value to an index.
1304   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1305     start_index = i;
1306     if (vp9_convert_qindex_to_q(i) >= qstart)
1307       break;
1308   }
1309 
1310   // Convert the q target to an index
1311   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1312     target_index = i;
1313     if (vp9_convert_qindex_to_q(i) >= qtarget)
1314       break;
1315   }
1316 
1317   return target_index - start_index;
1318 }
1319 
vp9_compute_qdelta_by_rate(const RATE_CONTROL * rc,FRAME_TYPE frame_type,int qindex,double rate_target_ratio)1320 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
1321                                int qindex, double rate_target_ratio) {
1322   int target_index = rc->worst_quality;
1323   int i;
1324 
1325   // Look up the current projected bits per block for the base index
1326   const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0);
1327 
1328   // Find the target bits per mb based on the base value and given ratio.
1329   const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
1330 
1331   // Convert the q target to an index
1332   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1333     target_index = i;
1334     if (vp9_rc_bits_per_mb(frame_type, i, 1.0) <= target_bits_per_mb )
1335       break;
1336   }
1337 
1338   return target_index - qindex;
1339 }
1340 
vp9_rc_set_gf_max_interval(const VP9_COMP * const cpi,RATE_CONTROL * const rc)1341 void vp9_rc_set_gf_max_interval(const VP9_COMP *const cpi,
1342                                 RATE_CONTROL *const rc) {
1343   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1344   // Set Maximum gf/arf interval
1345   rc->max_gf_interval = 16;
1346 
1347   // Extended interval for genuinely static scenes
1348   rc->static_scene_max_gf_interval = oxcf->key_freq >> 1;
1349   if (rc->static_scene_max_gf_interval > (MAX_LAG_BUFFERS * 2))
1350     rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
1351 
1352   if (is_altref_enabled(cpi)) {
1353     if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
1354       rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
1355   }
1356 
1357   if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1358     rc->max_gf_interval = rc->static_scene_max_gf_interval;
1359 }
1360 
vp9_rc_update_framerate(VP9_COMP * cpi)1361 void vp9_rc_update_framerate(VP9_COMP *cpi) {
1362   const VP9_COMMON *const cm = &cpi->common;
1363   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1364   RATE_CONTROL *const rc = &cpi->rc;
1365   int vbr_max_bits;
1366 
1367   rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / oxcf->framerate);
1368   rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth *
1369                                 oxcf->two_pass_vbrmin_section / 100);
1370 
1371   rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
1372 
1373   // A maximum bitrate for a frame is defined.
1374   // The baseline for this aligns with HW implementations that
1375   // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1376   // per 16x16 MB (averaged over a frame). However this limit is extended if
1377   // a very high rate is given on the command line or the the rate cannnot
1378   // be acheived because of a user specificed max q (e.g. when the user
1379   // specifies lossless encode.
1380   vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth *
1381                      oxcf->two_pass_vbrmax_section) / 100);
1382   rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P),
1383                                     vbr_max_bits);
1384 
1385   vp9_rc_set_gf_max_interval(cpi, rc);
1386 }
1387