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