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