1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <assert.h>
12#include <limits.h>
13#include <math.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include "./vpx_dsp_rtcd.h"
19#include "vpx_dsp/vpx_dsp_common.h"
20#include "vpx_mem/vpx_mem.h"
21#include "vpx_ports/mem.h"
22#include "vpx_ports/system_state.h"
23
24#include "vp9/common/vp9_alloccommon.h"
25#include "vp9/encoder/vp9_aq_cyclicrefresh.h"
26#include "vp9/common/vp9_common.h"
27#include "vp9/common/vp9_entropymode.h"
28#include "vp9/common/vp9_quant_common.h"
29#include "vp9/common/vp9_seg_common.h"
30
31#include "vp9/encoder/vp9_encodemv.h"
32#include "vp9/encoder/vp9_ratectrl.h"
33
34// Max rate target for 1080P and below encodes under normal circumstances
35// (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
36#define MAX_MB_RATE 250
37#define MAXRATE_1080P 2025000
38
39#define DEFAULT_KF_BOOST 2000
40#define DEFAULT_GF_BOOST 2000
41
42#define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
43
44#define MIN_BPB_FACTOR 0.005
45#define MAX_BPB_FACTOR 50
46
47#define FRAME_OVERHEAD_BITS 200
48
49// Use this macro to turn on/off use of alt-refs in one-pass vbr mode.
50#define USE_ALTREF_FOR_ONE_PASS 0
51
52#if CONFIG_VP9_HIGHBITDEPTH
53#define ASSIGN_MINQ_TABLE(bit_depth, name)                   \
54  do {                                                       \
55    switch (bit_depth) {                                     \
56      case VPX_BITS_8: name = name##_8; break;               \
57      case VPX_BITS_10: name = name##_10; break;             \
58      case VPX_BITS_12: name = name##_12; break;             \
59      default:                                               \
60        assert(0 &&                                          \
61               "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
97#ifdef AGGRESSIVE_VBR
98static int gf_high = 2400;
99static int gf_low = 400;
100static int kf_high = 4000;
101static int kf_low = 400;
102#else
103static int gf_high = 2000;
104static int gf_low = 400;
105static int kf_high = 5000;
106static int kf_low = 400;
107#endif
108
109// Functions to compute the active minq lookup table entries based on a
110// formulaic approach to facilitate easier adjustment of the Q tables.
111// The formulae were derived from computing a 3rd order polynomial best
112// fit to the original data (after plotting real maxq vs minq (not q index))
113static int get_minq_index(double maxq, double x3, double x2, double x1,
114                          vpx_bit_depth_t bit_depth) {
115  int i;
116  const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
117
118  // Special case handling to deal with the step from q2.0
119  // down to lossless mode represented by q 1.0.
120  if (minqtarget <= 2.0) return 0;
121
122  for (i = 0; i < QINDEX_RANGE; i++) {
123    if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth)) return i;
124  }
125
126  return QINDEX_RANGE - 1;
127}
128
129static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
130                           int *arfgf_high, int *inter, int *rtc,
131                           vpx_bit_depth_t bit_depth) {
132  int i;
133  for (i = 0; i < QINDEX_RANGE; i++) {
134    const double maxq = vp9_convert_qindex_to_q(i, bit_depth);
135    kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
136    kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
137#ifdef AGGRESSIVE_VBR
138    arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.275, bit_depth);
139    inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.80, bit_depth);
140#else
141    arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
142    inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
143#endif
144    arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
145    rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
146  }
147}
148
149void vp9_rc_init_minq_luts(void) {
150  init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
151                 arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
152                 inter_minq_8, rtc_minq_8, VPX_BITS_8);
153#if CONFIG_VP9_HIGHBITDEPTH
154  init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
155                 arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
156                 inter_minq_10, rtc_minq_10, VPX_BITS_10);
157  init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
158                 arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
159                 inter_minq_12, rtc_minq_12, VPX_BITS_12);
160#endif
161}
162
163// These functions use formulaic calculations to make playing with the
164// quantizer tables easier. If necessary they can be replaced by lookup
165// tables if and when things settle down in the experimental bitstream
166double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) {
167// Convert the index to a real Q value (scaled down to match old Q values)
168#if CONFIG_VP9_HIGHBITDEPTH
169  switch (bit_depth) {
170    case VPX_BITS_8: return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
171    case VPX_BITS_10: return vp9_ac_quant(qindex, 0, bit_depth) / 16.0;
172    case VPX_BITS_12: return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
173    default:
174      assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
175      return -1.0;
176  }
177#else
178  return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
179#endif
180}
181
182int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth) {
183  int i;
184
185  for (i = 0; i < QINDEX_RANGE; ++i)
186    if (vp9_convert_qindex_to_q(i, bit_depth) >= q_val) break;
187
188  if (i == QINDEX_RANGE) i--;
189
190  return i;
191}
192
193int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
194                       double correction_factor, vpx_bit_depth_t bit_depth) {
195  const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
196  int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
197
198  assert(correction_factor <= MAX_BPB_FACTOR &&
199         correction_factor >= MIN_BPB_FACTOR);
200
201  // q based adjustment to baseline enumerator
202  enumerator += (int)(enumerator * q) >> 12;
203  return (int)(enumerator * correction_factor / q);
204}
205
206int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
207                           double correction_factor,
208                           vpx_bit_depth_t bit_depth) {
209  const int bpm =
210      (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth));
211  return VPXMAX(FRAME_OVERHEAD_BITS,
212                (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
213}
214
215int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
216  const RATE_CONTROL *rc = &cpi->rc;
217  const VP9EncoderConfig *oxcf = &cpi->oxcf;
218  const int min_frame_target =
219      VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
220  if (target < min_frame_target) target = min_frame_target;
221  if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
222    // If there is an active ARF at this location use the minimum
223    // bits on this frame even if it is a constructed arf.
224    // The active maximum quantizer insures that an appropriate
225    // number of bits will be spent if needed for constructed ARFs.
226    target = min_frame_target;
227  }
228  // Clip the frame target to the maximum allowed value.
229  if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
230  if (oxcf->rc_max_inter_bitrate_pct) {
231    const int max_rate =
232        rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
233    target = VPXMIN(target, max_rate);
234  }
235  return target;
236}
237
238int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
239  const RATE_CONTROL *rc = &cpi->rc;
240  const VP9EncoderConfig *oxcf = &cpi->oxcf;
241  if (oxcf->rc_max_intra_bitrate_pct) {
242    const int max_rate =
243        rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
244    target = VPXMIN(target, max_rate);
245  }
246  if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
247  return target;
248}
249
250// Update the buffer level for higher temporal layers, given the encoded current
251// temporal layer.
252static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
253  int i = 0;
254  int current_temporal_layer = svc->temporal_layer_id;
255  for (i = current_temporal_layer + 1; i < svc->number_temporal_layers; ++i) {
256    const int layer =
257        LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
258    LAYER_CONTEXT *lc = &svc->layer_context[layer];
259    RATE_CONTROL *lrc = &lc->rc;
260    int bits_off_for_this_layer =
261        (int)(lc->target_bandwidth / lc->framerate - encoded_frame_size);
262    lrc->bits_off_target += bits_off_for_this_layer;
263
264    // Clip buffer level to maximum buffer size for the layer.
265    lrc->bits_off_target =
266        VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
267    lrc->buffer_level = lrc->bits_off_target;
268  }
269}
270
271// Update the buffer level: leaky bucket model.
272static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
273  const VP9_COMMON *const cm = &cpi->common;
274  RATE_CONTROL *const rc = &cpi->rc;
275
276  // Non-viewable frames are a special case and are treated as pure overhead.
277  if (!cm->show_frame) {
278    rc->bits_off_target -= encoded_frame_size;
279  } else {
280    rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
281  }
282
283  // Clip the buffer level to the maximum specified buffer size.
284  rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
285
286  // For screen-content mode, and if frame-dropper is off, don't let buffer
287  // level go below threshold, given here as -rc->maximum_ buffer_size.
288  if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
289      cpi->oxcf.drop_frames_water_mark == 0)
290    rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size);
291
292  rc->buffer_level = rc->bits_off_target;
293
294  if (is_one_pass_cbr_svc(cpi)) {
295    update_layer_buffer_level(&cpi->svc, encoded_frame_size);
296  }
297}
298
299int vp9_rc_get_default_min_gf_interval(int width, int height,
300                                       double framerate) {
301  // Assume we do not need any constraint lower than 4K 20 fps
302  static const double factor_safe = 3840 * 2160 * 20.0;
303  const double factor = width * height * framerate;
304  const int default_interval =
305      clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
306
307  if (factor <= factor_safe)
308    return default_interval;
309  else
310    return VPXMAX(default_interval,
311                  (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
312  // Note this logic makes:
313  // 4K24: 5
314  // 4K30: 6
315  // 4K60: 12
316}
317
318int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
319  int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
320  interval += (interval & 0x01);  // Round to even value
321  return VPXMAX(interval, min_gf_interval);
322}
323
324void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
325  int i;
326
327  if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
328    rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
329    rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
330  } else {
331    rc->avg_frame_qindex[KEY_FRAME] =
332        (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
333    rc->avg_frame_qindex[INTER_FRAME] =
334        (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
335  }
336
337  rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
338  rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
339
340  rc->buffer_level = rc->starting_buffer_level;
341  rc->bits_off_target = rc->starting_buffer_level;
342
343  rc->rolling_target_bits = rc->avg_frame_bandwidth;
344  rc->rolling_actual_bits = rc->avg_frame_bandwidth;
345  rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
346  rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
347
348  rc->total_actual_bits = 0;
349  rc->total_target_bits = 0;
350  rc->total_target_vs_actual = 0;
351  rc->avg_frame_low_motion = 0;
352  rc->count_last_scene_change = 0;
353  rc->af_ratio_onepass_vbr = 10;
354  rc->prev_avg_source_sad_lag = 0;
355  rc->high_source_sad = 0;
356  rc->high_source_sad_lagindex = -1;
357  rc->alt_ref_gf_group = 0;
358  rc->fac_active_worst_inter = 150;
359  rc->fac_active_worst_gf = 100;
360  rc->force_qpmin = 0;
361  for (i = 0; i < MAX_LAG_BUFFERS; ++i) rc->avg_source_sad[i] = 0;
362  rc->frames_since_key = 8;  // Sensible default for first frame.
363  rc->this_key_frame_forced = 0;
364  rc->next_key_frame_forced = 0;
365  rc->source_alt_ref_pending = 0;
366  rc->source_alt_ref_active = 0;
367
368  rc->frames_till_gf_update_due = 0;
369  rc->ni_av_qi = oxcf->worst_allowed_q;
370  rc->ni_tot_qi = 0;
371  rc->ni_frames = 0;
372
373  rc->tot_q = 0.0;
374  rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
375
376  for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
377    rc->rate_correction_factors[i] = 1.0;
378  }
379
380  rc->min_gf_interval = oxcf->min_gf_interval;
381  rc->max_gf_interval = oxcf->max_gf_interval;
382  if (rc->min_gf_interval == 0)
383    rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
384        oxcf->width, oxcf->height, oxcf->init_framerate);
385  if (rc->max_gf_interval == 0)
386    rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
387        oxcf->init_framerate, rc->min_gf_interval);
388  rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
389}
390
391int vp9_rc_drop_frame(VP9_COMP *cpi) {
392  const VP9EncoderConfig *oxcf = &cpi->oxcf;
393  RATE_CONTROL *const rc = &cpi->rc;
394  if (!oxcf->drop_frames_water_mark ||
395      (is_one_pass_cbr_svc(cpi) &&
396       cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode)) {
397    return 0;
398  } else {
399    if (rc->buffer_level < 0) {
400      // Always drop if buffer is below 0.
401      return 1;
402    } else {
403      // If buffer is below drop_mark, for now just drop every other frame
404      // (starting with the next frame) until it increases back over drop_mark.
405      int drop_mark =
406          (int)(oxcf->drop_frames_water_mark * rc->optimal_buffer_level / 100);
407      if ((rc->buffer_level > drop_mark) && (rc->decimation_factor > 0)) {
408        --rc->decimation_factor;
409      } else if (rc->buffer_level <= drop_mark && rc->decimation_factor == 0) {
410        rc->decimation_factor = 1;
411      }
412      if (rc->decimation_factor > 0) {
413        if (rc->decimation_count > 0) {
414          --rc->decimation_count;
415          return 1;
416        } else {
417          rc->decimation_count = rc->decimation_factor;
418          return 0;
419        }
420      } else {
421        rc->decimation_count = 0;
422        return 0;
423      }
424    }
425  }
426}
427
428static double get_rate_correction_factor(const VP9_COMP *cpi) {
429  const RATE_CONTROL *const rc = &cpi->rc;
430  double rcf;
431
432  if (cpi->common.frame_type == KEY_FRAME) {
433    rcf = rc->rate_correction_factors[KF_STD];
434  } else if (cpi->oxcf.pass == 2) {
435    RATE_FACTOR_LEVEL rf_lvl =
436        cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
437    rcf = rc->rate_correction_factors[rf_lvl];
438  } else {
439    if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
440        !rc->is_src_frame_alt_ref && !cpi->use_svc &&
441        (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
442      rcf = rc->rate_correction_factors[GF_ARF_STD];
443    else
444      rcf = rc->rate_correction_factors[INTER_NORMAL];
445  }
446  rcf *= rcf_mult[rc->frame_size_selector];
447  return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
448}
449
450static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
451  RATE_CONTROL *const rc = &cpi->rc;
452
453  // Normalize RCF to account for the size-dependent scaling factor.
454  factor /= rcf_mult[cpi->rc.frame_size_selector];
455
456  factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
457
458  if (cpi->common.frame_type == KEY_FRAME) {
459    rc->rate_correction_factors[KF_STD] = factor;
460  } else if (cpi->oxcf.pass == 2) {
461    RATE_FACTOR_LEVEL rf_lvl =
462        cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
463    rc->rate_correction_factors[rf_lvl] = factor;
464  } else {
465    if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
466        !rc->is_src_frame_alt_ref && !cpi->use_svc &&
467        (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
468      rc->rate_correction_factors[GF_ARF_STD] = factor;
469    else
470      rc->rate_correction_factors[INTER_NORMAL] = factor;
471  }
472}
473
474void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) {
475  const VP9_COMMON *const cm = &cpi->common;
476  int correction_factor = 100;
477  double rate_correction_factor = get_rate_correction_factor(cpi);
478  double adjustment_limit;
479
480  int projected_size_based_on_q = 0;
481
482  // Do not update the rate factors for arf overlay frames.
483  if (cpi->rc.is_src_frame_alt_ref) return;
484
485  // Clear down mmx registers to allow floating point in what follows
486  vpx_clear_system_state();
487
488  // Work out how big we would have expected the frame to be at this Q given
489  // the current correction factor.
490  // Stay in double to avoid int overflow when values are large
491  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
492    projected_size_based_on_q =
493        vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
494  } else {
495    projected_size_based_on_q =
496        vp9_estimate_bits_at_q(cpi->common.frame_type, cm->base_qindex, cm->MBs,
497                               rate_correction_factor, cm->bit_depth);
498  }
499  // Work out a size correction factor.
500  if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
501    correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
502                              projected_size_based_on_q);
503
504  // More heavily damped adjustment used if we have been oscillating either side
505  // of target.
506  adjustment_limit =
507      0.25 + 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
508
509  cpi->rc.q_2_frame = cpi->rc.q_1_frame;
510  cpi->rc.q_1_frame = cm->base_qindex;
511  cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
512  if (correction_factor > 110)
513    cpi->rc.rc_1_frame = -1;
514  else if (correction_factor < 90)
515    cpi->rc.rc_1_frame = 1;
516  else
517    cpi->rc.rc_1_frame = 0;
518
519  // Turn off oscilation detection in the case of massive overshoot.
520  if (cpi->rc.rc_1_frame == -1 && cpi->rc.rc_2_frame == 1 &&
521      correction_factor > 1000) {
522    cpi->rc.rc_2_frame = 0;
523  }
524
525  if (correction_factor > 102) {
526    // We are not already at the worst allowable quality
527    correction_factor =
528        (int)(100 + ((correction_factor - 100) * adjustment_limit));
529    rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
530    // Keep rate_correction_factor within limits
531    if (rate_correction_factor > MAX_BPB_FACTOR)
532      rate_correction_factor = MAX_BPB_FACTOR;
533  } else if (correction_factor < 99) {
534    // We are not already at the best allowable quality
535    correction_factor =
536        (int)(100 - ((100 - correction_factor) * adjustment_limit));
537    rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
538
539    // Keep rate_correction_factor within limits
540    if (rate_correction_factor < MIN_BPB_FACTOR)
541      rate_correction_factor = MIN_BPB_FACTOR;
542  }
543
544  set_rate_correction_factor(cpi, rate_correction_factor);
545}
546
547int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
548                      int active_best_quality, int active_worst_quality) {
549  const VP9_COMMON *const cm = &cpi->common;
550  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
551  int q = active_worst_quality;
552  int last_error = INT_MAX;
553  int i, target_bits_per_mb, bits_per_mb_at_this_q;
554  const double correction_factor = get_rate_correction_factor(cpi);
555
556  // Calculate required scaling factor based on target frame size and size of
557  // frame produced using previous Q.
558  target_bits_per_mb =
559      (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs);
560
561  i = active_best_quality;
562
563  do {
564    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
565        cr->apply_cyclic_refresh &&
566        (!cpi->oxcf.gf_cbr_boost_pct || !cpi->refresh_golden_frame)) {
567      bits_per_mb_at_this_q =
568          (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
569    } else {
570      bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(
571          cm->frame_type, i, correction_factor, cm->bit_depth);
572    }
573
574    if (bits_per_mb_at_this_q <= target_bits_per_mb) {
575      if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
576        q = i;
577      else
578        q = i - 1;
579
580      break;
581    } else {
582      last_error = bits_per_mb_at_this_q - target_bits_per_mb;
583    }
584  } while (++i <= active_worst_quality);
585
586  // In CBR mode, this makes sure q is between oscillating Qs to prevent
587  // resonance.
588  if (cpi->oxcf.rc_mode == VPX_CBR &&
589      (!cpi->oxcf.gf_cbr_boost_pct ||
590       !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)) &&
591      (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
592      cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
593    q = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
594              VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
595  }
596#if USE_ALTREF_FOR_ONE_PASS
597  if (cpi->oxcf.enable_auto_arf && cpi->oxcf.pass == 0 &&
598      cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
599      cpi->rc.is_src_frame_alt_ref && !cpi->rc.alt_ref_gf_group) {
600    q = VPXMIN(q, (q + cpi->rc.last_boosted_qindex) >> 1);
601  }
602#endif
603  return q;
604}
605
606static int get_active_quality(int q, int gfu_boost, int low, int high,
607                              int *low_motion_minq, int *high_motion_minq) {
608  if (gfu_boost > high) {
609    return low_motion_minq[q];
610  } else if (gfu_boost < low) {
611    return high_motion_minq[q];
612  } else {
613    const int gap = high - low;
614    const int offset = high - gfu_boost;
615    const int qdiff = high_motion_minq[q] - low_motion_minq[q];
616    const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
617    return low_motion_minq[q] + adjustment;
618  }
619}
620
621static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
622                                 vpx_bit_depth_t bit_depth) {
623  int *kf_low_motion_minq;
624  int *kf_high_motion_minq;
625  ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
626  ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
627  return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
628                            kf_low_motion_minq, kf_high_motion_minq);
629}
630
631static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
632                                 vpx_bit_depth_t bit_depth) {
633  int *arfgf_low_motion_minq;
634  int *arfgf_high_motion_minq;
635  ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
636  ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
637  return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
638                            arfgf_low_motion_minq, arfgf_high_motion_minq);
639}
640
641static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
642  const RATE_CONTROL *const rc = &cpi->rc;
643  const unsigned int curr_frame = cpi->common.current_video_frame;
644  int active_worst_quality;
645
646  if (cpi->common.frame_type == KEY_FRAME) {
647    active_worst_quality =
648        curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] << 1;
649  } else {
650    if (!rc->is_src_frame_alt_ref &&
651        (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
652      active_worst_quality =
653          curr_frame == 1
654              ? rc->last_q[KEY_FRAME] * 5 >> 2
655              : rc->last_q[INTER_FRAME] * rc->fac_active_worst_gf / 100;
656    } else {
657      active_worst_quality = curr_frame == 1
658                                 ? rc->last_q[KEY_FRAME] << 1
659                                 : rc->avg_frame_qindex[INTER_FRAME] *
660                                       rc->fac_active_worst_inter / 100;
661    }
662  }
663  return VPXMIN(active_worst_quality, rc->worst_quality);
664}
665
666// Adjust active_worst_quality level based on buffer level.
667static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
668  // Adjust active_worst_quality: If buffer is above the optimal/target level,
669  // bring active_worst_quality down depending on fullness of buffer.
670  // If buffer is below the optimal level, let the active_worst_quality go from
671  // ambient Q (at buffer = optimal level) to worst_quality level
672  // (at buffer = critical level).
673  const VP9_COMMON *const cm = &cpi->common;
674  const RATE_CONTROL *rc = &cpi->rc;
675  // Buffer level below which we push active_worst to worst_quality.
676  int64_t critical_level = rc->optimal_buffer_level >> 3;
677  int64_t buff_lvl_step = 0;
678  int adjustment = 0;
679  int active_worst_quality;
680  int ambient_qp;
681  unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
682  if (cm->frame_type == KEY_FRAME) return rc->worst_quality;
683  // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
684  // for the first few frames following key frame. These are both initialized
685  // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
686  // So for first few frames following key, the qp of that key frame is weighted
687  // into the active_worst_quality setting.
688  ambient_qp = (cm->current_video_frame < num_frames_weight_key)
689                   ? VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
690                            rc->avg_frame_qindex[KEY_FRAME])
691                   : rc->avg_frame_qindex[INTER_FRAME];
692  // For SVC if the current base spatial layer was key frame, use the QP from
693  // that base layer for ambient_qp.
694  if (cpi->use_svc && cpi->svc.spatial_layer_id > 0) {
695    int layer = LAYER_IDS_TO_IDX(0, cpi->svc.temporal_layer_id,
696                                 cpi->svc.number_temporal_layers);
697    const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
698    if (lc->is_key_frame) {
699      const RATE_CONTROL *lrc = &lc->rc;
700      ambient_qp = VPXMIN(ambient_qp, lrc->last_q[KEY_FRAME]);
701    }
702  }
703  active_worst_quality = VPXMIN(rc->worst_quality, ambient_qp * 5 >> 2);
704  if (rc->buffer_level > rc->optimal_buffer_level) {
705    // Adjust down.
706    // Maximum limit for down adjustment, ~30%.
707    int max_adjustment_down = active_worst_quality / 3;
708    if (max_adjustment_down) {
709      buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
710                       max_adjustment_down);
711      if (buff_lvl_step)
712        adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
713                           buff_lvl_step);
714      active_worst_quality -= adjustment;
715    }
716  } else if (rc->buffer_level > critical_level) {
717    // Adjust up from ambient Q.
718    if (critical_level) {
719      buff_lvl_step = (rc->optimal_buffer_level - critical_level);
720      if (buff_lvl_step) {
721        adjustment = (int)((rc->worst_quality - ambient_qp) *
722                           (rc->optimal_buffer_level - rc->buffer_level) /
723                           buff_lvl_step);
724      }
725      active_worst_quality = ambient_qp + adjustment;
726    }
727  } else {
728    // Set to worst_quality if buffer is below critical level.
729    active_worst_quality = rc->worst_quality;
730  }
731  return active_worst_quality;
732}
733
734static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
735                                             int *bottom_index,
736                                             int *top_index) {
737  const VP9_COMMON *const cm = &cpi->common;
738  const RATE_CONTROL *const rc = &cpi->rc;
739  int active_best_quality;
740  int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
741  int q;
742  int *rtc_minq;
743  ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
744
745  if (frame_is_intra_only(cm)) {
746    active_best_quality = rc->best_quality;
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(
754          rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth);
755      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
756    } else if (cm->current_video_frame > 0) {
757      // not first frame of one pass and kf_boost is set
758      double q_adj_factor = 1.0;
759      double q_val;
760
761      active_best_quality = get_kf_active_quality(
762          rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
763
764      // Allow somewhat lower kf minq with small image formats.
765      if ((cm->width * cm->height) <= (352 * 288)) {
766        q_adj_factor -= 0.25;
767      }
768
769      // Convert the adjustment factor to a qindex delta
770      // on active_best_quality.
771      q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
772      active_best_quality +=
773          vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
774    }
775  } else if (!rc->is_src_frame_alt_ref && !cpi->use_svc &&
776             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
777    // Use the lower of active_worst_quality and recent
778    // average Q as basis for GF/ARF best Q limit unless last frame was
779    // a key frame.
780    if (rc->frames_since_key > 1 &&
781        rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
782      q = rc->avg_frame_qindex[INTER_FRAME];
783    } else {
784      q = active_worst_quality;
785    }
786    active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
787  } else {
788    // Use the lower of active_worst_quality and recent/average Q.
789    if (cm->current_video_frame > 1) {
790      if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
791        active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
792      else
793        active_best_quality = rtc_minq[active_worst_quality];
794    } else {
795      if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
796        active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
797      else
798        active_best_quality = rtc_minq[active_worst_quality];
799    }
800  }
801
802  // Clip the active best and worst quality values to limits
803  active_best_quality =
804      clamp(active_best_quality, rc->best_quality, rc->worst_quality);
805  active_worst_quality =
806      clamp(active_worst_quality, active_best_quality, rc->worst_quality);
807
808  *top_index = active_worst_quality;
809  *bottom_index = active_best_quality;
810
811#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
812  // Limit Q range for the adaptive loop.
813  if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
814      !(cm->current_video_frame == 0)) {
815    int qdelta = 0;
816    vpx_clear_system_state();
817    qdelta = vp9_compute_qdelta_by_rate(
818        &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
819    *top_index = active_worst_quality + qdelta;
820    *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
821  }
822#endif
823
824  // Special case code to try and match quality with forced key frames
825  if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
826    q = rc->last_boosted_qindex;
827  } else {
828    q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
829                          active_worst_quality);
830    if (q > *top_index) {
831      // Special case when we are targeting the max allowed rate
832      if (rc->this_frame_target >= rc->max_frame_bandwidth)
833        *top_index = q;
834      else
835        q = *top_index;
836    }
837  }
838  assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
839  assert(*bottom_index <= rc->worst_quality &&
840         *bottom_index >= rc->best_quality);
841  assert(q <= rc->worst_quality && q >= rc->best_quality);
842  return q;
843}
844
845static int get_active_cq_level_one_pass(const RATE_CONTROL *rc,
846                                        const VP9EncoderConfig *const oxcf) {
847  static const double cq_adjust_threshold = 0.1;
848  int active_cq_level = oxcf->cq_level;
849  if (oxcf->rc_mode == VPX_CQ && rc->total_target_bits > 0) {
850    const double x = (double)rc->total_actual_bits / rc->total_target_bits;
851    if (x < cq_adjust_threshold) {
852      active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
853    }
854  }
855  return active_cq_level;
856}
857
858#define SMOOTH_PCT_MIN 0.1
859#define SMOOTH_PCT_DIV 0.05
860static int get_active_cq_level_two_pass(const TWO_PASS *twopass,
861                                        const RATE_CONTROL *rc,
862                                        const VP9EncoderConfig *const oxcf) {
863  static const double cq_adjust_threshold = 0.1;
864  int active_cq_level = oxcf->cq_level;
865  if (oxcf->rc_mode == VPX_CQ) {
866    if (twopass->mb_smooth_pct > SMOOTH_PCT_MIN) {
867      active_cq_level -=
868          (int)((twopass->mb_smooth_pct - SMOOTH_PCT_MIN) / SMOOTH_PCT_DIV);
869      active_cq_level = VPXMAX(active_cq_level, 0);
870    }
871    if (rc->total_target_bits > 0) {
872      const double x = (double)rc->total_actual_bits / rc->total_target_bits;
873      if (x < cq_adjust_threshold) {
874        active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
875      }
876    }
877  }
878  return active_cq_level;
879}
880
881static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
882                                             int *bottom_index,
883                                             int *top_index) {
884  const VP9_COMMON *const cm = &cpi->common;
885  const RATE_CONTROL *const rc = &cpi->rc;
886  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
887  const int cq_level = get_active_cq_level_one_pass(rc, oxcf);
888  int active_best_quality;
889  int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
890  int q;
891  int *inter_minq;
892  ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
893
894  if (frame_is_intra_only(cm)) {
895    if (oxcf->rc_mode == VPX_Q) {
896      int qindex = cq_level;
897      double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
898      int delta_qindex = vp9_compute_qdelta(rc, q, q * 0.25, cm->bit_depth);
899      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
900    } else if (rc->this_key_frame_forced) {
901      // Handle the special case for key frames forced when we have reached
902      // the maximum key frame interval. Here force the Q to a range
903      // based on the ambient Q to reduce the risk of popping.
904      int qindex = rc->last_boosted_qindex;
905      double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
906      int delta_qindex = vp9_compute_qdelta(
907          rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
908      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
909    } else {
910      // not first frame of one pass and kf_boost is set
911      double q_adj_factor = 1.0;
912      double q_val;
913
914      active_best_quality = get_kf_active_quality(
915          rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
916
917      // Allow somewhat lower kf minq with small image formats.
918      if ((cm->width * cm->height) <= (352 * 288)) {
919        q_adj_factor -= 0.25;
920      }
921
922      // Convert the adjustment factor to a qindex delta
923      // on active_best_quality.
924      q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
925      active_best_quality +=
926          vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
927    }
928  } else if (!rc->is_src_frame_alt_ref &&
929             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
930    // Use the lower of active_worst_quality and recent
931    // average Q as basis for GF/ARF best Q limit unless last frame was
932    // a key frame.
933    if (rc->frames_since_key > 1) {
934      if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
935        q = rc->avg_frame_qindex[INTER_FRAME];
936      } else {
937        q = active_worst_quality;
938      }
939    } else {
940      q = rc->avg_frame_qindex[KEY_FRAME];
941    }
942    // For constrained quality dont allow Q less than the cq level
943    if (oxcf->rc_mode == VPX_CQ) {
944      if (q < cq_level) q = cq_level;
945
946      active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
947
948      // Constrained quality use slightly lower active best.
949      active_best_quality = active_best_quality * 15 / 16;
950
951    } else if (oxcf->rc_mode == VPX_Q) {
952      int qindex = cq_level;
953      double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
954      int delta_qindex;
955      if (cpi->refresh_alt_ref_frame)
956        delta_qindex = vp9_compute_qdelta(rc, q, q * 0.40, cm->bit_depth);
957      else
958        delta_qindex = vp9_compute_qdelta(rc, q, q * 0.50, cm->bit_depth);
959      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
960    } else {
961      active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
962    }
963  } else {
964    if (oxcf->rc_mode == VPX_Q) {
965      int qindex = cq_level;
966      double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
967      double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
968                                               0.70, 1.0, 0.85, 1.0 };
969      int delta_qindex = vp9_compute_qdelta(
970          rc, q, q * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL],
971          cm->bit_depth);
972      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
973    } else {
974      // Use the min of the average Q and active_worst_quality as basis for
975      // active_best.
976      if (cm->current_video_frame > 1) {
977        q = VPXMIN(rc->avg_frame_qindex[INTER_FRAME], active_worst_quality);
978        active_best_quality = inter_minq[q];
979      } else {
980        active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
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) && (active_best_quality < cq_level)) {
985        active_best_quality = cq_level;
986      }
987    }
988  }
989
990  // Clip the active best and worst quality values to limits
991  active_best_quality =
992      clamp(active_best_quality, rc->best_quality, rc->worst_quality);
993  active_worst_quality =
994      clamp(active_worst_quality, active_best_quality, rc->worst_quality);
995
996  *top_index = active_worst_quality;
997  *bottom_index = active_best_quality;
998
999#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
1000  {
1001    int qdelta = 0;
1002    vpx_clear_system_state();
1003
1004    // Limit Q range for the adaptive loop.
1005    if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
1006        !(cm->current_video_frame == 0)) {
1007      qdelta = vp9_compute_qdelta_by_rate(
1008          &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
1009    } else if (!rc->is_src_frame_alt_ref &&
1010               (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1011      qdelta = vp9_compute_qdelta_by_rate(
1012          &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth);
1013    }
1014    *top_index = active_worst_quality + qdelta;
1015    *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
1016  }
1017#endif
1018
1019  if (oxcf->rc_mode == VPX_Q) {
1020    q = active_best_quality;
1021    // Special case code to try and match quality with forced key frames
1022  } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
1023    q = rc->last_boosted_qindex;
1024  } else {
1025    q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1026                          active_worst_quality);
1027    if (q > *top_index) {
1028      // Special case when we are targeting the max allowed rate
1029      if (rc->this_frame_target >= rc->max_frame_bandwidth)
1030        *top_index = q;
1031      else
1032        q = *top_index;
1033    }
1034  }
1035
1036  assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1037  assert(*bottom_index <= rc->worst_quality &&
1038         *bottom_index >= rc->best_quality);
1039  assert(q <= rc->worst_quality && q >= rc->best_quality);
1040  return q;
1041}
1042
1043int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) {
1044  static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
1045    1.00,  // INTER_NORMAL
1046    1.00,  // INTER_HIGH
1047    1.50,  // GF_ARF_LOW
1048    1.75,  // GF_ARF_STD
1049    2.00,  // KF_STD
1050  };
1051  static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] = {
1052    INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME
1053  };
1054  const VP9_COMMON *const cm = &cpi->common;
1055  int qdelta =
1056      vp9_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level], q,
1057                                 rate_factor_deltas[rf_level], cm->bit_depth);
1058  return qdelta;
1059}
1060
1061#define STATIC_MOTION_THRESH 95
1062static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, int *bottom_index,
1063                                         int *top_index) {
1064  const VP9_COMMON *const cm = &cpi->common;
1065  const RATE_CONTROL *const rc = &cpi->rc;
1066  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1067  const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1068  const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf);
1069  int active_best_quality;
1070  int active_worst_quality = cpi->twopass.active_worst_quality;
1071  int q;
1072  int *inter_minq;
1073  ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
1074
1075  if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
1076    // Handle the special case for key frames forced when we have reached
1077    // the maximum key frame interval. Here force the Q to a range
1078    // based on the ambient Q to reduce the risk of popping.
1079    if (rc->this_key_frame_forced) {
1080      double last_boosted_q;
1081      int delta_qindex;
1082      int qindex;
1083
1084      if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1085        qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1086        active_best_quality = qindex;
1087        last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1088        delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1089                                          last_boosted_q * 1.25, cm->bit_depth);
1090        active_worst_quality =
1091            VPXMIN(qindex + delta_qindex, active_worst_quality);
1092      } else {
1093        qindex = rc->last_boosted_qindex;
1094        last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1095        delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1096                                          last_boosted_q * 0.75, cm->bit_depth);
1097        active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1098      }
1099    } else {
1100      // Not forced keyframe.
1101      double q_adj_factor = 1.0;
1102      double q_val;
1103      // Baseline value derived from cpi->active_worst_quality and kf boost.
1104      active_best_quality =
1105          get_kf_active_quality(rc, active_worst_quality, cm->bit_depth);
1106
1107      // Allow somewhat lower kf minq with small image formats.
1108      if ((cm->width * cm->height) <= (352 * 288)) {
1109        q_adj_factor -= 0.25;
1110      }
1111
1112      // Make a further adjustment based on the kf zero motion measure.
1113      q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
1114
1115      // Convert the adjustment factor to a qindex delta
1116      // on active_best_quality.
1117      q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
1118      active_best_quality +=
1119          vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
1120    }
1121  } else if (!rc->is_src_frame_alt_ref &&
1122             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1123    // Use the lower of active_worst_quality and recent
1124    // average Q as basis for GF/ARF best Q limit unless last frame was
1125    // a key frame.
1126    if (rc->frames_since_key > 1 &&
1127        rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1128      q = rc->avg_frame_qindex[INTER_FRAME];
1129    } else {
1130      q = active_worst_quality;
1131    }
1132    // For constrained quality dont allow Q less than the cq level
1133    if (oxcf->rc_mode == VPX_CQ) {
1134      if (q < cq_level) q = cq_level;
1135
1136      active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1137
1138      // Constrained quality use slightly lower active best.
1139      active_best_quality = active_best_quality * 15 / 16;
1140
1141    } else if (oxcf->rc_mode == VPX_Q) {
1142      if (!cpi->refresh_alt_ref_frame) {
1143        active_best_quality = cq_level;
1144      } else {
1145        active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1146
1147        // Modify best quality for second level arfs. For mode VPX_Q this
1148        // becomes the baseline frame q.
1149        if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
1150          active_best_quality = (active_best_quality + cq_level + 1) / 2;
1151      }
1152    } else {
1153      active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1154    }
1155  } else {
1156    if (oxcf->rc_mode == VPX_Q) {
1157      active_best_quality = cq_level;
1158    } else {
1159      active_best_quality = inter_minq[active_worst_quality];
1160
1161      // For the constrained quality mode we don't want
1162      // q to fall below the cq level.
1163      if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
1164        active_best_quality = cq_level;
1165      }
1166    }
1167  }
1168
1169  // Extension to max or min Q if undershoot or overshoot is outside
1170  // the permitted range.
1171  if (cpi->oxcf.rc_mode != VPX_Q) {
1172    if (frame_is_intra_only(cm) ||
1173        (!rc->is_src_frame_alt_ref &&
1174         (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1175      active_best_quality -=
1176          (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
1177      active_worst_quality += (cpi->twopass.extend_maxq / 2);
1178    } else {
1179      active_best_quality -=
1180          (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
1181      active_worst_quality += cpi->twopass.extend_maxq;
1182    }
1183  }
1184
1185#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
1186  vpx_clear_system_state();
1187  // Static forced key frames Q restrictions dealt with elsewhere.
1188  if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) ||
1189      !rc->this_key_frame_forced ||
1190      (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
1191    int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index],
1192                                       active_worst_quality);
1193    active_worst_quality =
1194        VPXMAX(active_worst_quality + qdelta, active_best_quality);
1195  }
1196#endif
1197
1198  // Modify active_best_quality for downscaled normal frames.
1199  if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
1200    int qdelta = vp9_compute_qdelta_by_rate(
1201        rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth);
1202    active_best_quality =
1203        VPXMAX(active_best_quality + qdelta, rc->best_quality);
1204  }
1205
1206  active_best_quality =
1207      clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1208  active_worst_quality =
1209      clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1210
1211  if (oxcf->rc_mode == VPX_Q) {
1212    q = active_best_quality;
1213    // Special case code to try and match quality with forced key frames.
1214  } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) &&
1215             rc->this_key_frame_forced) {
1216    // If static since last kf use better of last boosted and last kf q.
1217    if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1218      q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1219    } else {
1220      q = rc->last_boosted_qindex;
1221    }
1222  } else {
1223    q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1224                          active_worst_quality);
1225    if (q > active_worst_quality) {
1226      // Special case when we are targeting the max allowed rate.
1227      if (rc->this_frame_target >= rc->max_frame_bandwidth)
1228        active_worst_quality = q;
1229      else
1230        q = active_worst_quality;
1231    }
1232  }
1233  clamp(q, active_best_quality, active_worst_quality);
1234
1235  *top_index = active_worst_quality;
1236  *bottom_index = active_best_quality;
1237
1238  assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1239  assert(*bottom_index <= rc->worst_quality &&
1240         *bottom_index >= rc->best_quality);
1241  assert(q <= rc->worst_quality && q >= rc->best_quality);
1242  return q;
1243}
1244
1245int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, int *bottom_index,
1246                             int *top_index) {
1247  int q;
1248  if (cpi->oxcf.pass == 0) {
1249    if (cpi->oxcf.rc_mode == VPX_CBR)
1250      q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
1251    else
1252      q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
1253  } else {
1254    q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
1255  }
1256  if (cpi->sf.use_nonrd_pick_mode) {
1257    if (cpi->sf.force_frame_boost == 1) q -= cpi->sf.max_delta_qindex;
1258
1259    if (q < *bottom_index)
1260      *bottom_index = q;
1261    else if (q > *top_index)
1262      *top_index = q;
1263  }
1264  return q;
1265}
1266
1267void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, int frame_target,
1268                                      int *frame_under_shoot_limit,
1269                                      int *frame_over_shoot_limit) {
1270  if (cpi->oxcf.rc_mode == VPX_Q) {
1271    *frame_under_shoot_limit = 0;
1272    *frame_over_shoot_limit = INT_MAX;
1273  } else {
1274    // For very small rate targets where the fractional adjustment
1275    // may be tiny make sure there is at least a minimum range.
1276    const int tol_low = (cpi->sf.recode_tolerance_low * frame_target) / 100;
1277    const int tol_high = (cpi->sf.recode_tolerance_high * frame_target) / 100;
1278    *frame_under_shoot_limit = VPXMAX(frame_target - tol_low - 100, 0);
1279    *frame_over_shoot_limit =
1280        VPXMIN(frame_target + tol_high + 100, cpi->rc.max_frame_bandwidth);
1281  }
1282}
1283
1284void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
1285  const VP9_COMMON *const cm = &cpi->common;
1286  RATE_CONTROL *const rc = &cpi->rc;
1287
1288  rc->this_frame_target = target;
1289
1290  // Modify frame size target when down-scaling.
1291  if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
1292      rc->frame_size_selector != UNSCALED)
1293    rc->this_frame_target = (int)(rc->this_frame_target *
1294                                  rate_thresh_mult[rc->frame_size_selector]);
1295
1296  // Target rate per SB64 (including partial SB64s.
1297  rc->sb64_target_rate = (int)(((int64_t)rc->this_frame_target * 64 * 64) /
1298                               (cm->width * cm->height));
1299}
1300
1301static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
1302  // this frame refreshes means next frames don't unless specified by user
1303  RATE_CONTROL *const rc = &cpi->rc;
1304  rc->frames_since_golden = 0;
1305
1306  // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1307  rc->source_alt_ref_pending = 0;
1308
1309  // Set the alternate reference frame active flag
1310  rc->source_alt_ref_active = 1;
1311}
1312
1313static void update_golden_frame_stats(VP9_COMP *cpi) {
1314  RATE_CONTROL *const rc = &cpi->rc;
1315
1316  // Update the Golden frame usage counts.
1317  if (cpi->refresh_golden_frame) {
1318    // this frame refreshes means next frames don't unless specified by user
1319    rc->frames_since_golden = 0;
1320
1321    // If we are not using alt ref in the up and coming group clear the arf
1322    // active flag. In multi arf group case, if the index is not 0 then
1323    // we are overlaying a mid group arf so should not reset the flag.
1324    if (cpi->oxcf.pass == 2) {
1325      if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1326        rc->source_alt_ref_active = 0;
1327    } else if (!rc->source_alt_ref_pending) {
1328      rc->source_alt_ref_active = 0;
1329    }
1330
1331    // Decrement count down till next gf
1332    if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1333
1334  } else if (!cpi->refresh_alt_ref_frame) {
1335    // Decrement count down till next gf
1336    if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1337
1338    rc->frames_since_golden++;
1339  }
1340}
1341
1342static void compute_frame_low_motion(VP9_COMP *const cpi) {
1343  VP9_COMMON *const cm = &cpi->common;
1344  int mi_row, mi_col;
1345  MODE_INFO **mi = cm->mi_grid_visible;
1346  RATE_CONTROL *const rc = &cpi->rc;
1347  const int rows = cm->mi_rows, cols = cm->mi_cols;
1348  int cnt_zeromv = 0;
1349  for (mi_row = 0; mi_row < rows; mi_row++) {
1350    for (mi_col = 0; mi_col < cols; mi_col++) {
1351      if (abs(mi[0]->mv[0].as_mv.row) < 16 && abs(mi[0]->mv[0].as_mv.col) < 16)
1352        cnt_zeromv++;
1353      mi++;
1354    }
1355    mi += 8;
1356  }
1357  cnt_zeromv = 100 * cnt_zeromv / (rows * cols);
1358  rc->avg_frame_low_motion = (3 * rc->avg_frame_low_motion + cnt_zeromv) >> 2;
1359}
1360
1361void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
1362  const VP9_COMMON *const cm = &cpi->common;
1363  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1364  RATE_CONTROL *const rc = &cpi->rc;
1365  const int qindex = cm->base_qindex;
1366
1367  // Update rate control heuristics
1368  rc->projected_frame_size = (int)(bytes_used << 3);
1369
1370  // Post encode loop adjustment of Q prediction.
1371  vp9_rc_update_rate_correction_factors(cpi);
1372
1373  // Keep a record of last Q and ambient average Q.
1374  if (cm->frame_type == KEY_FRAME) {
1375    rc->last_q[KEY_FRAME] = qindex;
1376    rc->avg_frame_qindex[KEY_FRAME] =
1377        ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1378    if (cpi->use_svc) {
1379      int i = 0;
1380      SVC *svc = &cpi->svc;
1381      for (i = 0; i < svc->number_temporal_layers; ++i) {
1382        const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
1383                                           svc->number_temporal_layers);
1384        LAYER_CONTEXT *lc = &svc->layer_context[layer];
1385        RATE_CONTROL *lrc = &lc->rc;
1386        lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME];
1387        lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME];
1388      }
1389    }
1390  } else {
1391    if ((cpi->use_svc && oxcf->rc_mode == VPX_CBR) ||
1392        (!rc->is_src_frame_alt_ref &&
1393         !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1394      rc->last_q[INTER_FRAME] = qindex;
1395      rc->avg_frame_qindex[INTER_FRAME] =
1396          ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1397      rc->ni_frames++;
1398      rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1399      rc->avg_q = rc->tot_q / rc->ni_frames;
1400      // Calculate the average Q for normal inter frames (not key or GFU
1401      // frames).
1402      rc->ni_tot_qi += qindex;
1403      rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1404    }
1405  }
1406
1407  // Keep record of last boosted (KF/KF/ARF) Q value.
1408  // If the current frame is coded at a lower Q then we also update it.
1409  // If all mbs in this group are skipped only update if the Q value is
1410  // better than that already stored.
1411  // This is used to help set quality in forced key frames to reduce popping
1412  if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) ||
1413      (!rc->constrained_gf_group &&
1414       (cpi->refresh_alt_ref_frame ||
1415        (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1416    rc->last_boosted_qindex = qindex;
1417  }
1418  if (cm->frame_type == KEY_FRAME) rc->last_kf_qindex = qindex;
1419
1420  update_buffer_level(cpi, rc->projected_frame_size);
1421
1422  // Rolling monitors of whether we are over or underspending used to help
1423  // regulate min and Max Q in two pass.
1424  if (cm->frame_type != KEY_FRAME) {
1425    rc->rolling_target_bits = ROUND_POWER_OF_TWO(
1426        rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1427    rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
1428        rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1429    rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
1430        rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1431    rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
1432        rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1433  }
1434
1435  // Actual bits spent
1436  rc->total_actual_bits += rc->projected_frame_size;
1437  rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1438
1439  rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1440
1441  if (!cpi->use_svc || is_two_pass_svc(cpi)) {
1442    if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1443        (cm->frame_type != KEY_FRAME))
1444      // Update the alternate reference frame stats as appropriate.
1445      update_alt_ref_frame_stats(cpi);
1446    else
1447      // Update the Golden frame stats as appropriate.
1448      update_golden_frame_stats(cpi);
1449  }
1450
1451  if (cm->frame_type == KEY_FRAME) rc->frames_since_key = 0;
1452  if (cm->show_frame) {
1453    rc->frames_since_key++;
1454    rc->frames_to_key--;
1455  }
1456
1457  // Trigger the resizing of the next frame if it is scaled.
1458  if (oxcf->pass != 0) {
1459    cpi->resize_pending =
1460        rc->next_frame_size_selector != rc->frame_size_selector;
1461    rc->frame_size_selector = rc->next_frame_size_selector;
1462  }
1463
1464  if (oxcf->pass == 0) {
1465    if (cm->frame_type != KEY_FRAME) compute_frame_low_motion(cpi);
1466  }
1467}
1468
1469void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
1470  // Update buffer level with zero size, update frame counters, and return.
1471  update_buffer_level(cpi, 0);
1472  cpi->rc.frames_since_key++;
1473  cpi->rc.frames_to_key--;
1474  cpi->rc.rc_2_frame = 0;
1475  cpi->rc.rc_1_frame = 0;
1476}
1477
1478static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
1479  const RATE_CONTROL *const rc = &cpi->rc;
1480  const int af_ratio = rc->af_ratio_onepass_vbr;
1481  int target =
1482      (!rc->is_src_frame_alt_ref &&
1483       (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
1484          ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
1485                (rc->baseline_gf_interval + af_ratio - 1)
1486          : (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
1487                (rc->baseline_gf_interval + af_ratio - 1);
1488  return vp9_rc_clamp_pframe_target_size(cpi, target);
1489}
1490
1491static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
1492  static const int kf_ratio = 25;
1493  const RATE_CONTROL *rc = &cpi->rc;
1494  const int target = rc->avg_frame_bandwidth * kf_ratio;
1495  return vp9_rc_clamp_iframe_target_size(cpi, target);
1496}
1497
1498static void adjust_gfint_frame_constraint(VP9_COMP *cpi, int frame_constraint) {
1499  RATE_CONTROL *const rc = &cpi->rc;
1500  rc->constrained_gf_group = 0;
1501  // Reset gf interval to make more equal spacing for frame_constraint.
1502  if ((frame_constraint <= 7 * rc->baseline_gf_interval >> 2) &&
1503      (frame_constraint > rc->baseline_gf_interval)) {
1504    rc->baseline_gf_interval = frame_constraint >> 1;
1505    if (rc->baseline_gf_interval < 5)
1506      rc->baseline_gf_interval = frame_constraint;
1507    rc->constrained_gf_group = 1;
1508  } else {
1509    // Reset to keep gf_interval <= frame_constraint.
1510    if (rc->baseline_gf_interval > frame_constraint) {
1511      rc->baseline_gf_interval = frame_constraint;
1512      rc->constrained_gf_group = 1;
1513    }
1514  }
1515}
1516
1517void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
1518  VP9_COMMON *const cm = &cpi->common;
1519  RATE_CONTROL *const rc = &cpi->rc;
1520  int target;
1521  // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
1522  if (!cpi->refresh_alt_ref_frame &&
1523      (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1524       rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
1525    cm->frame_type = KEY_FRAME;
1526    rc->this_key_frame_forced =
1527        cm->current_video_frame != 0 && rc->frames_to_key == 0;
1528    rc->frames_to_key = cpi->oxcf.key_freq;
1529    rc->kf_boost = DEFAULT_KF_BOOST;
1530    rc->source_alt_ref_active = 0;
1531  } else {
1532    cm->frame_type = INTER_FRAME;
1533  }
1534  if (rc->frames_till_gf_update_due == 0) {
1535    double rate_err = 1.0;
1536    rc->gfu_boost = DEFAULT_GF_BOOST;
1537    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0) {
1538      vp9_cyclic_refresh_set_golden_update(cpi);
1539    } else {
1540      rc->baseline_gf_interval = VPXMIN(
1541          20, VPXMAX(10, (rc->min_gf_interval + rc->max_gf_interval) / 2));
1542    }
1543    rc->af_ratio_onepass_vbr = 10;
1544    if (rc->rolling_target_bits > 0)
1545      rate_err =
1546          (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
1547    if (cm->current_video_frame > 30) {
1548      if (rc->avg_frame_qindex[INTER_FRAME] > (7 * rc->worst_quality) >> 3 &&
1549          rate_err > 3.5) {
1550        rc->baseline_gf_interval =
1551            VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
1552      } else if (rc->avg_frame_low_motion < 20) {
1553        // Decrease gf interval for high motion case.
1554        rc->baseline_gf_interval = VPXMAX(6, rc->baseline_gf_interval >> 1);
1555      }
1556      // Adjust boost and af_ratio based on avg_frame_low_motion, which varies
1557      // between 0 and 100 (stationary, 100% zero/small motion).
1558      rc->gfu_boost =
1559          VPXMAX(500, DEFAULT_GF_BOOST * (rc->avg_frame_low_motion << 1) /
1560                          (rc->avg_frame_low_motion + 100));
1561      rc->af_ratio_onepass_vbr = VPXMIN(15, VPXMAX(5, 3 * rc->gfu_boost / 400));
1562    }
1563    adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
1564    rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1565    cpi->refresh_golden_frame = 1;
1566    rc->source_alt_ref_pending = 0;
1567    rc->alt_ref_gf_group = 0;
1568#if USE_ALTREF_FOR_ONE_PASS
1569    if (cpi->oxcf.enable_auto_arf) {
1570      rc->source_alt_ref_pending = 1;
1571      rc->alt_ref_gf_group = 1;
1572    }
1573#endif
1574  }
1575  if (cm->frame_type == KEY_FRAME)
1576    target = calc_iframe_target_size_one_pass_vbr(cpi);
1577  else
1578    target = calc_pframe_target_size_one_pass_vbr(cpi);
1579  vp9_rc_set_frame_target(cpi, target);
1580  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0)
1581    vp9_cyclic_refresh_update_parameters(cpi);
1582}
1583
1584static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1585  const VP9EncoderConfig *oxcf = &cpi->oxcf;
1586  const RATE_CONTROL *rc = &cpi->rc;
1587  const SVC *const svc = &cpi->svc;
1588  const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
1589  const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
1590  int min_frame_target =
1591      VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
1592  int target;
1593
1594  if (oxcf->gf_cbr_boost_pct) {
1595    const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
1596    target = cpi->refresh_golden_frame
1597                 ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval *
1598                    af_ratio_pct) /
1599                       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
1600                 : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
1601                       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
1602  } else {
1603    target = rc->avg_frame_bandwidth;
1604  }
1605  if (is_one_pass_cbr_svc(cpi)) {
1606    // Note that for layers, avg_frame_bandwidth is the cumulative
1607    // per-frame-bandwidth. For the target size of this frame, use the
1608    // layer average frame size (i.e., non-cumulative per-frame-bw).
1609    int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
1610                                 svc->number_temporal_layers);
1611    const LAYER_CONTEXT *lc = &svc->layer_context[layer];
1612    target = lc->avg_frame_size;
1613    min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
1614  }
1615  if (diff > 0) {
1616    // Lower the target bandwidth for this frame.
1617    const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
1618    target -= (target * pct_low) / 200;
1619  } else if (diff < 0) {
1620    // Increase the target bandwidth for this frame.
1621    const int pct_high =
1622        (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
1623    target += (target * pct_high) / 200;
1624  }
1625  if (oxcf->rc_max_inter_bitrate_pct) {
1626    const int max_rate =
1627        rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
1628    target = VPXMIN(target, max_rate);
1629  }
1630  return VPXMAX(min_frame_target, target);
1631}
1632
1633static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1634  const RATE_CONTROL *rc = &cpi->rc;
1635  const VP9EncoderConfig *oxcf = &cpi->oxcf;
1636  const SVC *const svc = &cpi->svc;
1637  int target;
1638  if (cpi->common.current_video_frame == 0) {
1639    target = ((rc->starting_buffer_level / 2) > INT_MAX)
1640                 ? INT_MAX
1641                 : (int)(rc->starting_buffer_level / 2);
1642  } else {
1643    int kf_boost = 32;
1644    double framerate = cpi->framerate;
1645    if (svc->number_temporal_layers > 1 && oxcf->rc_mode == VPX_CBR) {
1646      // Use the layer framerate for temporal layers CBR mode.
1647      const int layer =
1648          LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
1649                           svc->number_temporal_layers);
1650      const LAYER_CONTEXT *lc = &svc->layer_context[layer];
1651      framerate = lc->framerate;
1652    }
1653    kf_boost = VPXMAX(kf_boost, (int)(2 * framerate - 16));
1654    if (rc->frames_since_key < framerate / 2) {
1655      kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
1656    }
1657    target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
1658  }
1659  return vp9_rc_clamp_iframe_target_size(cpi, target);
1660}
1661
1662void vp9_rc_get_svc_params(VP9_COMP *cpi) {
1663  VP9_COMMON *const cm = &cpi->common;
1664  RATE_CONTROL *const rc = &cpi->rc;
1665  int target = rc->avg_frame_bandwidth;
1666  int layer =
1667      LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
1668                       cpi->svc.number_temporal_layers);
1669  // Periodic key frames is based on the super-frame counter
1670  // (svc.current_superframe), also only base spatial layer is key frame.
1671  if ((cm->current_video_frame == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1672      (cpi->oxcf.auto_key &&
1673       (cpi->svc.current_superframe % cpi->oxcf.key_freq == 0) &&
1674       cpi->svc.spatial_layer_id == 0)) {
1675    cm->frame_type = KEY_FRAME;
1676    rc->source_alt_ref_active = 0;
1677    if (is_two_pass_svc(cpi)) {
1678      cpi->svc.layer_context[layer].is_key_frame = 1;
1679      cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
1680    } else if (is_one_pass_cbr_svc(cpi)) {
1681      if (cm->current_video_frame > 0) vp9_svc_reset_key_frame(cpi);
1682      layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
1683                               cpi->svc.temporal_layer_id,
1684                               cpi->svc.number_temporal_layers);
1685      cpi->svc.layer_context[layer].is_key_frame = 1;
1686      cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
1687      // Assumption here is that LAST_FRAME is being updated for a keyframe.
1688      // Thus no change in update flags.
1689      target = calc_iframe_target_size_one_pass_cbr(cpi);
1690    }
1691  } else {
1692    cm->frame_type = INTER_FRAME;
1693    if (is_two_pass_svc(cpi)) {
1694      LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1695      if (cpi->svc.spatial_layer_id == 0) {
1696        lc->is_key_frame = 0;
1697      } else {
1698        lc->is_key_frame =
1699            cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
1700        if (lc->is_key_frame) cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
1701      }
1702      cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
1703    } else if (is_one_pass_cbr_svc(cpi)) {
1704      LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1705      if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode) {
1706        lc->is_key_frame = 0;
1707      } else {
1708        lc->is_key_frame =
1709            cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
1710      }
1711      target = calc_pframe_target_size_one_pass_cbr(cpi);
1712    }
1713  }
1714
1715  // Any update/change of global cyclic refresh parameters (amount/delta-qp)
1716  // should be done here, before the frame qp is selected.
1717  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1718    vp9_cyclic_refresh_update_parameters(cpi);
1719
1720  vp9_rc_set_frame_target(cpi, target);
1721  rc->frames_till_gf_update_due = INT_MAX;
1722  rc->baseline_gf_interval = INT_MAX;
1723}
1724
1725void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
1726  VP9_COMMON *const cm = &cpi->common;
1727  RATE_CONTROL *const rc = &cpi->rc;
1728  int target;
1729  // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
1730  if ((cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1731       rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
1732    cm->frame_type = KEY_FRAME;
1733    rc->this_key_frame_forced =
1734        cm->current_video_frame != 0 && rc->frames_to_key == 0;
1735    rc->frames_to_key = cpi->oxcf.key_freq;
1736    rc->kf_boost = DEFAULT_KF_BOOST;
1737    rc->source_alt_ref_active = 0;
1738  } else {
1739    cm->frame_type = INTER_FRAME;
1740  }
1741  if (rc->frames_till_gf_update_due == 0) {
1742    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1743      vp9_cyclic_refresh_set_golden_update(cpi);
1744    else
1745      rc->baseline_gf_interval =
1746          (rc->min_gf_interval + rc->max_gf_interval) / 2;
1747    rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1748    // NOTE: frames_till_gf_update_due must be <= frames_to_key.
1749    if (rc->frames_till_gf_update_due > rc->frames_to_key)
1750      rc->frames_till_gf_update_due = rc->frames_to_key;
1751    cpi->refresh_golden_frame = 1;
1752    rc->gfu_boost = DEFAULT_GF_BOOST;
1753  }
1754
1755  // Any update/change of global cyclic refresh parameters (amount/delta-qp)
1756  // should be done here, before the frame qp is selected.
1757  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1758    vp9_cyclic_refresh_update_parameters(cpi);
1759
1760  if (cm->frame_type == KEY_FRAME)
1761    target = calc_iframe_target_size_one_pass_cbr(cpi);
1762  else
1763    target = calc_pframe_target_size_one_pass_cbr(cpi);
1764
1765  vp9_rc_set_frame_target(cpi, target);
1766  if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC)
1767    cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
1768  else
1769    cpi->resize_pending = 0;
1770}
1771
1772int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
1773                       vpx_bit_depth_t bit_depth) {
1774  int start_index = rc->worst_quality;
1775  int target_index = rc->worst_quality;
1776  int i;
1777
1778  // Convert the average q value to an index.
1779  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1780    start_index = i;
1781    if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart) break;
1782  }
1783
1784  // Convert the q target to an index
1785  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1786    target_index = i;
1787    if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
1788  }
1789
1790  return target_index - start_index;
1791}
1792
1793int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
1794                               int qindex, double rate_target_ratio,
1795                               vpx_bit_depth_t bit_depth) {
1796  int target_index = rc->worst_quality;
1797  int i;
1798
1799  // Look up the current projected bits per block for the base index
1800  const int base_bits_per_mb =
1801      vp9_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
1802
1803  // Find the target bits per mb based on the base value and given ratio.
1804  const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
1805
1806  // Convert the q target to an index
1807  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1808    if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
1809        target_bits_per_mb) {
1810      target_index = i;
1811      break;
1812    }
1813  }
1814  return target_index - qindex;
1815}
1816
1817void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi,
1818                                  RATE_CONTROL *const rc) {
1819  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1820
1821  // Special case code for 1 pass fixed Q mode tests
1822  if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
1823    rc->max_gf_interval = FIXED_GF_INTERVAL;
1824    rc->min_gf_interval = FIXED_GF_INTERVAL;
1825    rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
1826  } else {
1827    // Set Maximum gf/arf interval
1828    rc->max_gf_interval = oxcf->max_gf_interval;
1829    rc->min_gf_interval = oxcf->min_gf_interval;
1830    if (rc->min_gf_interval == 0)
1831      rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
1832          oxcf->width, oxcf->height, cpi->framerate);
1833    if (rc->max_gf_interval == 0)
1834      rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
1835          cpi->framerate, rc->min_gf_interval);
1836
1837    // Extended interval for genuinely static scenes
1838    rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
1839
1840    if (is_altref_enabled(cpi)) {
1841      if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
1842        rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
1843    }
1844
1845    if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1846      rc->max_gf_interval = rc->static_scene_max_gf_interval;
1847
1848    // Clamp min to max
1849    rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
1850  }
1851}
1852
1853void vp9_rc_update_framerate(VP9_COMP *cpi) {
1854  const VP9_COMMON *const cm = &cpi->common;
1855  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1856  RATE_CONTROL *const rc = &cpi->rc;
1857  int vbr_max_bits;
1858
1859  rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
1860  rc->min_frame_bandwidth =
1861      (int)(rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
1862
1863  rc->min_frame_bandwidth =
1864      VPXMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
1865
1866  // A maximum bitrate for a frame is defined.
1867  // The baseline for this aligns with HW implementations that
1868  // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1869  // per 16x16 MB (averaged over a frame). However this limit is extended if
1870  // a very high rate is given on the command line or the the rate cannnot
1871  // be acheived because of a user specificed max q (e.g. when the user
1872  // specifies lossless encode.
1873  vbr_max_bits =
1874      (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section) /
1875            100);
1876  rc->max_frame_bandwidth =
1877      VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
1878
1879  vp9_rc_set_gf_interval_range(cpi, rc);
1880}
1881
1882#define VBR_PCT_ADJUSTMENT_LIMIT 50
1883// For VBR...adjustment to the frame target based on error from previous frames
1884static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
1885  RATE_CONTROL *const rc = &cpi->rc;
1886  int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
1887  int max_delta;
1888  int frame_window = VPXMIN(16, ((int)cpi->twopass.total_stats.count -
1889                                 cpi->common.current_video_frame));
1890
1891  // Calcluate the adjustment to rate for this frame.
1892  if (frame_window > 0) {
1893    max_delta = (vbr_bits_off_target > 0)
1894                    ? (int)(vbr_bits_off_target / frame_window)
1895                    : (int)(-vbr_bits_off_target / frame_window);
1896
1897    max_delta = VPXMIN(max_delta,
1898                       ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
1899
1900    // vbr_bits_off_target > 0 means we have extra bits to spend
1901    if (vbr_bits_off_target > 0) {
1902      *this_frame_target += (vbr_bits_off_target > max_delta)
1903                                ? max_delta
1904                                : (int)vbr_bits_off_target;
1905    } else {
1906      *this_frame_target -= (vbr_bits_off_target < -max_delta)
1907                                ? max_delta
1908                                : (int)-vbr_bits_off_target;
1909    }
1910  }
1911
1912  // Fast redistribution of bits arising from massive local undershoot.
1913  // Dont do it for kf,arf,gf or overlay frames.
1914  if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
1915      rc->vbr_bits_off_target_fast) {
1916    int one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, *this_frame_target);
1917    int fast_extra_bits;
1918    fast_extra_bits = (int)VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
1919    fast_extra_bits = (int)VPXMIN(
1920        fast_extra_bits,
1921        VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
1922    *this_frame_target += (int)fast_extra_bits;
1923    rc->vbr_bits_off_target_fast -= fast_extra_bits;
1924  }
1925}
1926
1927void vp9_set_target_rate(VP9_COMP *cpi) {
1928  RATE_CONTROL *const rc = &cpi->rc;
1929  int target_rate = rc->base_frame_target;
1930
1931  if (cpi->common.frame_type == KEY_FRAME)
1932    target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
1933  else
1934    target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
1935
1936  // Correction to rate target based on prior over or under shoot.
1937  if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
1938    vbr_rate_correction(cpi, &target_rate);
1939  vp9_rc_set_frame_target(cpi, target_rate);
1940}
1941
1942// Check if we should resize, based on average QP from past x frames.
1943// Only allow for resize at most one scale down for now, scaling factor is 2.
1944int vp9_resize_one_pass_cbr(VP9_COMP *cpi) {
1945  const VP9_COMMON *const cm = &cpi->common;
1946  RATE_CONTROL *const rc = &cpi->rc;
1947  RESIZE_ACTION resize_action = NO_RESIZE;
1948  int avg_qp_thr1 = 70;
1949  int avg_qp_thr2 = 50;
1950  int min_width = 180;
1951  int min_height = 180;
1952  int down_size_on = 1;
1953  cpi->resize_scale_num = 1;
1954  cpi->resize_scale_den = 1;
1955  // Don't resize on key frame; reset the counters on key frame.
1956  if (cm->frame_type == KEY_FRAME) {
1957    cpi->resize_avg_qp = 0;
1958    cpi->resize_count = 0;
1959    return 0;
1960  }
1961  // Check current frame reslution to avoid generating frames smaller than
1962  // the minimum resolution.
1963  if (ONEHALFONLY_RESIZE) {
1964    if ((cm->width >> 1) < min_width || (cm->height >> 1) < min_height)
1965      down_size_on = 0;
1966  } else {
1967    if (cpi->resize_state == ORIG &&
1968        (cm->width * 3 / 4 < min_width || cm->height * 3 / 4 < min_height))
1969      return 0;
1970    else if (cpi->resize_state == THREE_QUARTER &&
1971             ((cpi->oxcf.width >> 1) < min_width ||
1972              (cpi->oxcf.height >> 1) < min_height))
1973      down_size_on = 0;
1974  }
1975
1976#if CONFIG_VP9_TEMPORAL_DENOISING
1977  // If denoiser is on, apply a smaller qp threshold.
1978  if (cpi->oxcf.noise_sensitivity > 0) {
1979    avg_qp_thr1 = 60;
1980    avg_qp_thr2 = 40;
1981  }
1982#endif
1983
1984  // Resize based on average buffer underflow and QP over some window.
1985  // Ignore samples close to key frame, since QP is usually high after key.
1986  if (cpi->rc.frames_since_key > 2 * cpi->framerate) {
1987    const int window = (int)(4 * cpi->framerate);
1988    cpi->resize_avg_qp += cm->base_qindex;
1989    if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100))
1990      ++cpi->resize_buffer_underflow;
1991    ++cpi->resize_count;
1992    // Check for resize action every "window" frames.
1993    if (cpi->resize_count >= window) {
1994      int avg_qp = cpi->resize_avg_qp / cpi->resize_count;
1995      // Resize down if buffer level has underflowed sufficient amount in past
1996      // window, and we are at original or 3/4 of original resolution.
1997      // Resize back up if average QP is low, and we are currently in a resized
1998      // down state, i.e. 1/2 or 3/4 of original resolution.
1999      // Currently, use a flag to turn 3/4 resizing feature on/off.
2000      if (cpi->resize_buffer_underflow > (cpi->resize_count >> 2)) {
2001        if (cpi->resize_state == THREE_QUARTER && down_size_on) {
2002          resize_action = DOWN_ONEHALF;
2003          cpi->resize_state = ONE_HALF;
2004        } else if (cpi->resize_state == ORIG) {
2005          resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
2006          cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
2007        }
2008      } else if (cpi->resize_state != ORIG &&
2009                 avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
2010        if (cpi->resize_state == THREE_QUARTER ||
2011            avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100 ||
2012            ONEHALFONLY_RESIZE) {
2013          resize_action = UP_ORIG;
2014          cpi->resize_state = ORIG;
2015        } else if (cpi->resize_state == ONE_HALF) {
2016          resize_action = UP_THREEFOUR;
2017          cpi->resize_state = THREE_QUARTER;
2018        }
2019      }
2020      // Reset for next window measurement.
2021      cpi->resize_avg_qp = 0;
2022      cpi->resize_count = 0;
2023      cpi->resize_buffer_underflow = 0;
2024    }
2025  }
2026  // If decision is to resize, reset some quantities, and check is we should
2027  // reduce rate correction factor,
2028  if (resize_action != NO_RESIZE) {
2029    int target_bits_per_frame;
2030    int active_worst_quality;
2031    int qindex;
2032    int tot_scale_change;
2033    if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
2034      cpi->resize_scale_num = 3;
2035      cpi->resize_scale_den = 4;
2036    } else if (resize_action == DOWN_ONEHALF) {
2037      cpi->resize_scale_num = 1;
2038      cpi->resize_scale_den = 2;
2039    } else {  // UP_ORIG or anything else
2040      cpi->resize_scale_num = 1;
2041      cpi->resize_scale_den = 1;
2042    }
2043    tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) /
2044                       (cpi->resize_scale_num * cpi->resize_scale_num);
2045    // Reset buffer level to optimal, update target size.
2046    rc->buffer_level = rc->optimal_buffer_level;
2047    rc->bits_off_target = rc->optimal_buffer_level;
2048    rc->this_frame_target = calc_pframe_target_size_one_pass_cbr(cpi);
2049    // Get the projected qindex, based on the scaled target frame size (scaled
2050    // so target_bits_per_mb in vp9_rc_regulate_q will be correct target).
2051    target_bits_per_frame = (resize_action >= 0)
2052                                ? rc->this_frame_target * tot_scale_change
2053                                : rc->this_frame_target / tot_scale_change;
2054    active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
2055    qindex = vp9_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality,
2056                               active_worst_quality);
2057    // If resize is down, check if projected q index is close to worst_quality,
2058    // and if so, reduce the rate correction factor (since likely can afford
2059    // lower q for resized frame).
2060    if (resize_action > 0 && qindex > 90 * cpi->rc.worst_quality / 100) {
2061      rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
2062    }
2063    // If resize is back up, check if projected q index is too much above the
2064    // current base_qindex, and if so, reduce the rate correction factor
2065    // (since prefer to keep q for resized frame at least close to previous q).
2066    if (resize_action < 0 && qindex > 130 * cm->base_qindex / 100) {
2067      rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
2068    }
2069  }
2070  return resize_action;
2071}
2072
2073void adjust_gf_boost_lag_one_pass_vbr(VP9_COMP *cpi, uint64_t avg_sad_current) {
2074  VP9_COMMON *const cm = &cpi->common;
2075  RATE_CONTROL *const rc = &cpi->rc;
2076  int target;
2077  int found = 0;
2078  int found2 = 0;
2079  int frame;
2080  int i;
2081  uint64_t avg_source_sad_lag = avg_sad_current;
2082  int high_source_sad_lagindex = -1;
2083  int steady_sad_lagindex = -1;
2084  uint32_t sad_thresh1 = 60000;
2085  uint32_t sad_thresh2 = 120000;
2086  int low_content = 0;
2087  int high_content = 0;
2088  double rate_err = 1.0;
2089  // Get measure of complexity over the future frames, and get the first
2090  // future frame with high_source_sad/scene-change.
2091  int tot_frames = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
2092  for (frame = tot_frames; frame >= 1; --frame) {
2093    const int lagframe_idx = tot_frames - frame + 1;
2094    uint64_t reference_sad = rc->avg_source_sad[0];
2095    for (i = 1; i < lagframe_idx; ++i) {
2096      if (rc->avg_source_sad[i] > 0)
2097        reference_sad = (3 * reference_sad + rc->avg_source_sad[i]) >> 2;
2098    }
2099    // Detect up-coming scene change.
2100    if (!found &&
2101        (rc->avg_source_sad[lagframe_idx] >
2102             VPXMAX(sad_thresh1, (unsigned int)(reference_sad << 1)) ||
2103         rc->avg_source_sad[lagframe_idx] >
2104             VPXMAX(3 * sad_thresh1 >> 2,
2105                    (unsigned int)(reference_sad << 2)))) {
2106      high_source_sad_lagindex = lagframe_idx;
2107      found = 1;
2108    }
2109    // Detect change from motion to steady.
2110    if (!found2 && lagframe_idx > 1 && lagframe_idx < tot_frames &&
2111        rc->avg_source_sad[lagframe_idx - 1] > (sad_thresh1 >> 2)) {
2112      found2 = 1;
2113      for (i = lagframe_idx; i < tot_frames; ++i) {
2114        if (!(rc->avg_source_sad[i] > 0 &&
2115              rc->avg_source_sad[i] < (sad_thresh1 >> 2) &&
2116              rc->avg_source_sad[i] <
2117                  (rc->avg_source_sad[lagframe_idx - 1] >> 1))) {
2118          found2 = 0;
2119          i = tot_frames;
2120        }
2121      }
2122      if (found2) steady_sad_lagindex = lagframe_idx;
2123    }
2124    avg_source_sad_lag += rc->avg_source_sad[lagframe_idx];
2125  }
2126  if (tot_frames > 0) avg_source_sad_lag = avg_source_sad_lag / tot_frames;
2127  // Constrain distance between detected scene cuts.
2128  if (high_source_sad_lagindex != -1 &&
2129      high_source_sad_lagindex != rc->high_source_sad_lagindex - 1 &&
2130      abs(high_source_sad_lagindex - rc->high_source_sad_lagindex) < 4)
2131    rc->high_source_sad_lagindex = -1;
2132  else
2133    rc->high_source_sad_lagindex = high_source_sad_lagindex;
2134  // Adjust some factors for the next GF group, ignore initial key frame,
2135  // and only for lag_in_frames not too small.
2136  if (cpi->refresh_golden_frame == 1 && cm->current_video_frame > 30 &&
2137      cpi->oxcf.lag_in_frames > 8) {
2138    int frame_constraint;
2139    if (rc->rolling_target_bits > 0)
2140      rate_err =
2141          (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
2142    high_content = high_source_sad_lagindex != -1 ||
2143                   avg_source_sad_lag > (rc->prev_avg_source_sad_lag << 1) ||
2144                   avg_source_sad_lag > sad_thresh2;
2145    low_content = high_source_sad_lagindex == -1 &&
2146                  ((avg_source_sad_lag < (rc->prev_avg_source_sad_lag >> 1)) ||
2147                   (avg_source_sad_lag < sad_thresh1));
2148    if (low_content) {
2149      rc->gfu_boost = DEFAULT_GF_BOOST;
2150      rc->baseline_gf_interval =
2151          VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
2152    } else if (high_content) {
2153      rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
2154      rc->baseline_gf_interval = (rate_err > 3.0)
2155                                     ? VPXMAX(10, rc->baseline_gf_interval >> 1)
2156                                     : VPXMAX(6, rc->baseline_gf_interval >> 1);
2157    }
2158    if (rc->baseline_gf_interval > cpi->oxcf.lag_in_frames - 1)
2159      rc->baseline_gf_interval = cpi->oxcf.lag_in_frames - 1;
2160    // Check for constraining gf_interval for up-coming scene/content changes,
2161    // or for up-coming key frame, whichever is closer.
2162    frame_constraint = rc->frames_to_key;
2163    if (rc->high_source_sad_lagindex > 0 &&
2164        frame_constraint > rc->high_source_sad_lagindex)
2165      frame_constraint = rc->high_source_sad_lagindex;
2166    if (steady_sad_lagindex > 3 && frame_constraint > steady_sad_lagindex)
2167      frame_constraint = steady_sad_lagindex;
2168    adjust_gfint_frame_constraint(cpi, frame_constraint);
2169    rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2170    // Adjust factors for active_worst setting & af_ratio for next gf interval.
2171    rc->fac_active_worst_inter = 150;  // corresponds to 3/2 (= 150 /100).
2172    rc->fac_active_worst_gf = 100;
2173    if (rate_err < 2.0 && !high_content) {
2174      rc->fac_active_worst_inter = 120;
2175      rc->fac_active_worst_gf = 90;
2176    } else if (rate_err > 8.0 && rc->avg_frame_qindex[INTER_FRAME] < 16) {
2177      // Increase active_worst faster at low Q if rate fluctuation is high.
2178      rc->fac_active_worst_inter = 200;
2179      if (rc->avg_frame_qindex[INTER_FRAME] < 8)
2180        rc->fac_active_worst_inter = 400;
2181    }
2182    if (low_content && rc->avg_frame_low_motion > 80) {
2183      rc->af_ratio_onepass_vbr = 15;
2184    } else if (high_content || rc->avg_frame_low_motion < 30) {
2185      rc->af_ratio_onepass_vbr = 5;
2186      rc->gfu_boost = DEFAULT_GF_BOOST >> 2;
2187    }
2188#if USE_ALTREF_FOR_ONE_PASS
2189    if (cpi->oxcf.enable_auto_arf) {
2190      // Don't use alt-ref if there is a scene cut within the group,
2191      // or content is not low.
2192      if ((rc->high_source_sad_lagindex > 0 &&
2193           rc->high_source_sad_lagindex <= rc->frames_till_gf_update_due) ||
2194          (avg_source_sad_lag > 3 * sad_thresh1 >> 3)) {
2195        rc->source_alt_ref_pending = 0;
2196        rc->alt_ref_gf_group = 0;
2197      } else {
2198        rc->source_alt_ref_pending = 1;
2199        rc->alt_ref_gf_group = 1;
2200        // If alt-ref is used for this gf group, limit the interval.
2201        if (rc->baseline_gf_interval > 10 &&
2202            rc->baseline_gf_interval < rc->frames_to_key)
2203          rc->baseline_gf_interval = 10;
2204      }
2205    }
2206#endif
2207    target = calc_pframe_target_size_one_pass_vbr(cpi);
2208    vp9_rc_set_frame_target(cpi, target);
2209  }
2210  rc->prev_avg_source_sad_lag = avg_source_sad_lag;
2211}
2212
2213// Compute average source sad (temporal sad: between current source and
2214// previous source) over a subset of superblocks. Use this is detect big changes
2215// in content and allow rate control to react.
2216// This function also handles special case of lag_in_frames, to measure content
2217// level in #future frames set by the lag_in_frames.
2218void vp9_scene_detection_onepass(VP9_COMP *cpi) {
2219  VP9_COMMON *const cm = &cpi->common;
2220  RATE_CONTROL *const rc = &cpi->rc;
2221#if CONFIG_VP9_HIGHBITDEPTH
2222  if (cm->use_highbitdepth) return;
2223#endif
2224  rc->high_source_sad = 0;
2225  if (cpi->Last_Source != NULL &&
2226      cpi->Last_Source->y_width == cpi->Source->y_width &&
2227      cpi->Last_Source->y_height == cpi->Source->y_height) {
2228    YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = { NULL };
2229    uint8_t *src_y = cpi->Source->y_buffer;
2230    int src_ystride = cpi->Source->y_stride;
2231    uint8_t *last_src_y = cpi->Last_Source->y_buffer;
2232    int last_src_ystride = cpi->Last_Source->y_stride;
2233    int start_frame = 0;
2234    int frames_to_buffer = 1;
2235    int frame = 0;
2236    uint64_t avg_sad_current = 0;
2237    uint32_t min_thresh = 4000;
2238    float thresh = 8.0f;
2239    if (cpi->oxcf.rc_mode == VPX_VBR) {
2240      min_thresh = 60000;
2241      thresh = 2.1f;
2242    }
2243    if (cpi->oxcf.lag_in_frames > 0) {
2244      frames_to_buffer = (cm->current_video_frame == 1)
2245                             ? (int)vp9_lookahead_depth(cpi->lookahead) - 1
2246                             : 2;
2247      start_frame = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
2248      for (frame = 0; frame < frames_to_buffer; ++frame) {
2249        const int lagframe_idx = start_frame - frame;
2250        if (lagframe_idx >= 0) {
2251          struct lookahead_entry *buf =
2252              vp9_lookahead_peek(cpi->lookahead, lagframe_idx);
2253          frames[frame] = &buf->img;
2254        }
2255      }
2256      // The avg_sad for this current frame is the value of frame#1
2257      // (first future frame) from previous frame.
2258      avg_sad_current = rc->avg_source_sad[1];
2259      if (avg_sad_current >
2260              VPXMAX(min_thresh,
2261                     (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
2262          cm->current_video_frame > (unsigned int)cpi->oxcf.lag_in_frames)
2263        rc->high_source_sad = 1;
2264      else
2265        rc->high_source_sad = 0;
2266      // Update recursive average for current frame.
2267      if (avg_sad_current > 0)
2268        rc->avg_source_sad[0] =
2269            (3 * rc->avg_source_sad[0] + avg_sad_current) >> 2;
2270      // Shift back data, starting at frame#1.
2271      for (frame = 1; frame < cpi->oxcf.lag_in_frames - 1; ++frame)
2272        rc->avg_source_sad[frame] = rc->avg_source_sad[frame + 1];
2273    }
2274    for (frame = 0; frame < frames_to_buffer; ++frame) {
2275      if (cpi->oxcf.lag_in_frames == 0 ||
2276          (frames[frame] != NULL && frames[frame + 1] != NULL &&
2277           frames[frame]->y_width == frames[frame + 1]->y_width &&
2278           frames[frame]->y_height == frames[frame + 1]->y_height)) {
2279        int sbi_row, sbi_col;
2280        const int lagframe_idx =
2281            (cpi->oxcf.lag_in_frames == 0) ? 0 : start_frame - frame + 1;
2282        const BLOCK_SIZE bsize = BLOCK_64X64;
2283        // Loop over sub-sample of frame, compute average sad over 64x64 blocks.
2284        uint64_t avg_sad = 0;
2285        uint64_t tmp_sad = 0;
2286        int num_samples = 0;
2287        int sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
2288        int sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
2289        if (cpi->oxcf.lag_in_frames > 0) {
2290          src_y = frames[frame]->y_buffer;
2291          src_ystride = frames[frame]->y_stride;
2292          last_src_y = frames[frame + 1]->y_buffer;
2293          last_src_ystride = frames[frame + 1]->y_stride;
2294        }
2295        for (sbi_row = 0; sbi_row < sb_rows; ++sbi_row) {
2296          for (sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
2297            // Checker-board pattern, ignore boundary.
2298            if (((sbi_row > 0 && sbi_col > 0) &&
2299                 (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) &&
2300                 ((sbi_row % 2 == 0 && sbi_col % 2 == 0) ||
2301                  (sbi_row % 2 != 0 && sbi_col % 2 != 0)))) {
2302              tmp_sad = cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y,
2303                                               last_src_ystride);
2304              avg_sad += tmp_sad;
2305              num_samples++;
2306            }
2307            src_y += 64;
2308            last_src_y += 64;
2309          }
2310          src_y += (src_ystride << 6) - (sb_cols << 6);
2311          last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
2312        }
2313        if (num_samples > 0) avg_sad = avg_sad / num_samples;
2314        // Set high_source_sad flag if we detect very high increase in avg_sad
2315        // between current and previous frame value(s). Use minimum threshold
2316        // for cases where there is small change from content that is completely
2317        // static.
2318        if (lagframe_idx == 0) {
2319          if (avg_sad >
2320                  VPXMAX(min_thresh,
2321                         (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
2322              rc->frames_since_key > 1)
2323            rc->high_source_sad = 1;
2324          else
2325            rc->high_source_sad = 0;
2326          if (avg_sad > 0 || cpi->oxcf.rc_mode == VPX_CBR)
2327            rc->avg_source_sad[0] = (3 * rc->avg_source_sad[0] + avg_sad) >> 2;
2328        } else {
2329          rc->avg_source_sad[lagframe_idx] = avg_sad;
2330        }
2331      }
2332    }
2333    // For VBR, under scene change/high content change, force golden refresh.
2334    if (cpi->oxcf.rc_mode == VPX_VBR && cm->frame_type != KEY_FRAME &&
2335        rc->high_source_sad && rc->frames_to_key > 3 &&
2336        rc->count_last_scene_change > 4 &&
2337        cpi->ext_refresh_frame_flags_pending == 0) {
2338      int target;
2339      cpi->refresh_golden_frame = 1;
2340      rc->source_alt_ref_pending = 0;
2341#if USE_ALTREF_FOR_ONE_PASS
2342      if (cpi->oxcf.enable_auto_arf) rc->source_alt_ref_pending = 1;
2343#endif
2344      rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
2345      rc->baseline_gf_interval =
2346          VPXMIN(20, VPXMAX(10, rc->baseline_gf_interval));
2347      adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
2348      rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2349      target = calc_pframe_target_size_one_pass_vbr(cpi);
2350      vp9_rc_set_frame_target(cpi, target);
2351      rc->count_last_scene_change = 0;
2352    } else {
2353      rc->count_last_scene_change++;
2354    }
2355    // If lag_in_frame is used, set the gf boost and interval.
2356    if (cpi->oxcf.lag_in_frames > 0)
2357      adjust_gf_boost_lag_one_pass_vbr(cpi, avg_sad_current);
2358  }
2359}
2360
2361// Test if encoded frame will significantly overshoot the target bitrate, and
2362// if so, set the QP, reset/adjust some rate control parameters, and return 1.
2363int vp9_encodedframe_overshoot(VP9_COMP *cpi, int frame_size, int *q) {
2364  VP9_COMMON *const cm = &cpi->common;
2365  RATE_CONTROL *const rc = &cpi->rc;
2366  int thresh_qp = 3 * (rc->worst_quality >> 2);
2367  int thresh_rate = rc->avg_frame_bandwidth * 10;
2368  if (cm->base_qindex < thresh_qp && frame_size > thresh_rate) {
2369    double rate_correction_factor =
2370        cpi->rc.rate_correction_factors[INTER_NORMAL];
2371    const int target_size = cpi->rc.avg_frame_bandwidth;
2372    double new_correction_factor;
2373    int target_bits_per_mb;
2374    double q2;
2375    int enumerator;
2376    // Force a re-encode, and for now use max-QP.
2377    *q = cpi->rc.worst_quality;
2378    // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
2379    // these parameters will affect QP selection for subsequent frames. If they
2380    // have settled down to a very different (low QP) state, then not adjusting
2381    // them may cause next frame to select low QP and overshoot again.
2382    cpi->rc.avg_frame_qindex[INTER_FRAME] = *q;
2383    rc->buffer_level = rc->optimal_buffer_level;
2384    rc->bits_off_target = rc->optimal_buffer_level;
2385    // Reset rate under/over-shoot flags.
2386    cpi->rc.rc_1_frame = 0;
2387    cpi->rc.rc_2_frame = 0;
2388    // Adjust rate correction factor.
2389    target_bits_per_mb =
2390        (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->MBs);
2391    // Rate correction factor based on target_bits_per_mb and qp (==max_QP).
2392    // This comes from the inverse computation of vp9_rc_bits_per_mb().
2393    q2 = vp9_convert_qindex_to_q(*q, cm->bit_depth);
2394    enumerator = 1800000;  // Factor for inter frame.
2395    enumerator += (int)(enumerator * q2) >> 12;
2396    new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
2397    if (new_correction_factor > rate_correction_factor) {
2398      rate_correction_factor =
2399          VPXMIN(2.0 * rate_correction_factor, new_correction_factor);
2400      if (rate_correction_factor > MAX_BPB_FACTOR)
2401        rate_correction_factor = MAX_BPB_FACTOR;
2402      cpi->rc.rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
2403    }
2404    // For temporal layers, reset the rate control parametes across all
2405    // temporal layers.
2406    if (cpi->use_svc) {
2407      int i = 0;
2408      SVC *svc = &cpi->svc;
2409      for (i = 0; i < svc->number_temporal_layers; ++i) {
2410        const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
2411                                           svc->number_temporal_layers);
2412        LAYER_CONTEXT *lc = &svc->layer_context[layer];
2413        RATE_CONTROL *lrc = &lc->rc;
2414        lrc->avg_frame_qindex[INTER_FRAME] = *q;
2415        lrc->buffer_level = rc->optimal_buffer_level;
2416        lrc->bits_off_target = rc->optimal_buffer_level;
2417        lrc->rc_1_frame = 0;
2418        lrc->rc_2_frame = 0;
2419        lrc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
2420      }
2421    }
2422    return 1;
2423  } else {
2424    return 0;
2425  }
2426}
2427