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