vp9_ratectrl.c 96.9 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10 11
 */

#include <assert.h>
12
#include <limits.h>
Dmitry Kovalev's avatar
Dmitry Kovalev committed
13
#include <math.h>
14 15 16 17
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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"
John Koleszar's avatar
John Koleszar committed
23

24
#include "vp9/common/vp9_alloccommon.h"
25
#include "vp9/encoder/vp9_aq_cyclicrefresh.h"
26 27 28
#include "vp9/common/vp9_common.h"
#include "vp9/common/vp9_entropymode.h"
#include "vp9/common/vp9_quant_common.h"
29
#include "vp9/common/vp9_seg_common.h"
30 31 32

#include "vp9/encoder/vp9_encodemv.h"
#include "vp9/encoder/vp9_ratectrl.h"
John Koleszar's avatar
John Koleszar committed
33

34 35 36 37 38
// Max rate target for 1080P and below encodes under normal circumstances
// (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
#define MAX_MB_RATE 250
#define MAXRATE_1080P 2025000

39 40 41
#define DEFAULT_KF_BOOST 2000
#define DEFAULT_GF_BOOST 2000

42
#define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
43

Dmitry Kovalev's avatar
Dmitry Kovalev committed
44 45
#define MIN_BPB_FACTOR 0.005
#define MAX_BPB_FACTOR 50
John Koleszar's avatar
John Koleszar committed
46

47 48
#define FRAME_OVERHEAD_BITS 200

49
// Use this macro to turn on/off use of alt-refs in one-pass vbr mode.
50 51
#define USE_ALTREF_FOR_ONE_PASS 0

52
#if CONFIG_VP9_HIGHBITDEPTH
53 54 55 56 57 58 59 60 61 62 63 64
#define ASSIGN_MINQ_TABLE(bit_depth, name)                   \
  do {                                                       \
    switch (bit_depth) {                                     \
      case VPX_BITS_8: name = name##_8; break;               \
      case VPX_BITS_10: name = name##_10; break;             \
      case VPX_BITS_12: name = name##_12; break;             \
      default:                                               \
        assert(0 &&                                          \
               "bit_depth should be VPX_BITS_8, VPX_BITS_10" \
               " or VPX_BITS_12");                           \
        name = NULL;                                         \
    }                                                        \
65 66 67
  } while (0)
#else
#define ASSIGN_MINQ_TABLE(bit_depth, name) \
68
  do {                                     \
clang-format's avatar
clang-format committed
69
    (void)bit_depth;                       \
70
    name = name##_8;                       \
71 72 73
  } while (0)
#endif

74
// Tables relating active max Q to active min Q
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static int kf_low_motion_minq_8[QINDEX_RANGE];
static int kf_high_motion_minq_8[QINDEX_RANGE];
static int arfgf_low_motion_minq_8[QINDEX_RANGE];
static int arfgf_high_motion_minq_8[QINDEX_RANGE];
static int inter_minq_8[QINDEX_RANGE];
static int rtc_minq_8[QINDEX_RANGE];

#if CONFIG_VP9_HIGHBITDEPTH
static int kf_low_motion_minq_10[QINDEX_RANGE];
static int kf_high_motion_minq_10[QINDEX_RANGE];
static int arfgf_low_motion_minq_10[QINDEX_RANGE];
static int arfgf_high_motion_minq_10[QINDEX_RANGE];
static int inter_minq_10[QINDEX_RANGE];
static int rtc_minq_10[QINDEX_RANGE];
static int kf_low_motion_minq_12[QINDEX_RANGE];
static int kf_high_motion_minq_12[QINDEX_RANGE];
static int arfgf_low_motion_minq_12[QINDEX_RANGE];
static int arfgf_high_motion_minq_12[QINDEX_RANGE];
static int inter_minq_12[QINDEX_RANGE];
static int rtc_minq_12[QINDEX_RANGE];
#endif

97
#ifdef AGGRESSIVE_VBR
paulwilkins's avatar
paulwilkins committed
98 99 100 101 102
static int gf_high = 2400;
static int gf_low = 400;
static int kf_high = 4000;
static int kf_low = 400;
#else
103 104 105 106
static int gf_high = 2000;
static int gf_low = 400;
static int kf_high = 5000;
static int kf_low = 400;
paulwilkins's avatar
paulwilkins committed
107
#endif
108 109 110 111 112

// Functions to compute the active minq lookup table entries based on a
// formulaic approach to facilitate easier adjustment of the Q tables.
// The formulae were derived from computing a 3rd order polynomial best
// fit to the original data (after plotting real maxq vs minq (not q index))
113 114
static int get_minq_index(double maxq, double x3, double x2, double x1,
                          vpx_bit_depth_t bit_depth) {
115
  int i;
116
  const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
117 118 119

  // Special case handling to deal with the step from q2.0
  // 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 127 128

  return QINDEX_RANGE - 1;
}

129 130 131
static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
                           int *arfgf_high, int *inter, int *rtc,
                           vpx_bit_depth_t bit_depth) {
132 133
  int i;
  for (i = 0; i < QINDEX_RANGE; i++) {
134
    const double maxq = vp9_convert_qindex_to_q(i, bit_depth);
135 136
    kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
    kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
137
#ifdef AGGRESSIVE_VBR
paulwilkins's avatar
paulwilkins committed
138 139 140
    arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.275, bit_depth);
    inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.80, bit_depth);
#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);
paulwilkins's avatar
paulwilkins committed
143 144
#endif
    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
  }
}

149
void vp9_rc_init_minq_luts(void) {
150 151 152 153 154 155 156 157 158 159 160 161 162
  init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
                 arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
                 inter_minq_8, rtc_minq_8, VPX_BITS_8);
#if CONFIG_VP9_HIGHBITDEPTH
  init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
                 arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
                 inter_minq_10, rtc_minq_10, VPX_BITS_10);
  init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
                 arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
                 inter_minq_12, rtc_minq_12, VPX_BITS_12);
#endif
}

Paul Wilkins's avatar
Paul Wilkins committed
163 164 165
// These functions use formulaic calculations to make playing with the
// quantizer tables easier. If necessary they can be replaced by lookup
// tables if and when things settle down in the experimental bitstream
166
double 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 169
#if CONFIG_VP9_HIGHBITDEPTH
  switch (bit_depth) {
170 171 172
    case VPX_BITS_8: return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
    case VPX_BITS_10: return vp9_ac_quant(qindex, 0, bit_depth) / 16.0;
    case VPX_BITS_12: return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
173 174 175 176 177 178 179
    default:
      assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
      return -1.0;
  }
#else
  return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
#endif
Paul Wilkins's avatar
Paul Wilkins committed
180 181
}

182 183 184 185
int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth) {
  int i;

  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 191 192

  return i;
}

193
int 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;
Paul Wilkins's avatar
Paul Wilkins committed
197

198 199 200
  assert(correction_factor <= MAX_BPB_FACTOR &&
         correction_factor >= MIN_BPB_FACTOR);

201
  // q based adjustment to baseline enumerator
202
  enumerator += (int)(enumerator * q) >> 12;
203
  return (int)(enumerator * correction_factor / q);
204
}
John Koleszar's avatar
John Koleszar committed
205

206 207 208
int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
                           double correction_factor,
                           vpx_bit_depth_t bit_depth) {
209 210
  const int bpm =
      (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

215 216
int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
  const RATE_CONTROL *rc = &cpi->rc;
217
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
218 219 220
  const int min_frame_target =
      VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
  if (target < min_frame_target) target = min_frame_target;
221
  if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
222 223 224 225
    // If there is an active ARF at this location use the minimum
    // bits on this frame even if it is a constructed arf.
    // The active maximum quantizer insures that an appropriate
    // 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 232
    const int max_rate =
        rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
233
    target = VPXMIN(target, max_rate);
234
  }
235 236
  return target;
}
237

238 239
int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
  const RATE_CONTROL *rc = &cpi->rc;
240
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
241
  if (oxcf->rc_max_intra_bitrate_pct) {
242 243
    const int max_rate =
        rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
244
    target = VPXMIN(target, max_rate);
John Koleszar's avatar
John Koleszar committed
245
  }
246
  if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
247
  return target;
John Koleszar's avatar
John Koleszar committed
248 249
}

250 251
// Update the buffer level for higher temporal layers, given the encoded current
// temporal layer.
252
static 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 256 257
  for (i = current_temporal_layer + 1; i < svc->number_temporal_layers; ++i) {
    const int layer =
        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 261
    int bits_off_for_this_layer =
        (int)(lc->target_bandwidth / lc->framerate - encoded_frame_size);
262 263 264
    lrc->bits_off_target += bits_off_for_this_layer;

    // Clip buffer level to maximum buffer size for the layer.
265 266
    lrc->bits_off_target =
        VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
267 268 269 270
    lrc->buffer_level = lrc->bits_off_target;
  }
}

271
// Update the buffer level: leaky bucket model.
272
static 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 277
  // Non-viewable frames are a special case and are treated as pure overhead.
  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 287 288 289 290 291

  // For screen-content mode, and if frame-dropper is off, don't let buffer
  // level go below threshold, given here as -rc->maximum_ buffer_size.
  if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
      cpi->oxcf.drop_frames_water_mark == 0)
    rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size);

292 293
  rc->buffer_level = rc->bits_off_target;

294
  if (is_one_pass_cbr_svc(cpi)) {
295
    update_layer_buffer_level(&cpi->svc, encoded_frame_size);
296
  }
297 298
}

299 300
int vp9_rc_get_default_min_gf_interval(int width, int height,
                                       double framerate) {
301 302 303
  // Assume we do not need any constraint lower than 4K 20 fps
  static const double factor_safe = 3840 * 2160 * 20.0;
  const double factor = width * height * framerate;
Alex Converse's avatar
Alex Converse committed
304 305
  const int default_interval =
      clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
306 307

  if (factor <= factor_safe)
Alex Converse's avatar
Alex Converse committed
308
    return default_interval;
309
  else
310 311
    return VPXMAX(default_interval,
                  (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
312 313 314 315 316 317 318
  // Note this logic makes:
  // 4K24: 5
  // 4K30: 6
  // 4K60: 12
}

int 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
}

324
void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
325 326
  int i;

327
  if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
328 329
    rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
    rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
330
  } else {
331 332 333 334
    rc->avg_frame_qindex[KEY_FRAME] =
        (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
    rc->avg_frame_qindex[INTER_FRAME] =
        (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 344
  rc->rolling_target_bits = rc->avg_frame_bandwidth;
  rc->rolling_actual_bits = rc->avg_frame_bandwidth;
345 346
  rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
  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 355
  rc->prev_avg_source_sad_lag = 0;
  rc->high_source_sad = 0;
356
  rc->reset_high_source_sad = 0;
357
  rc->high_source_sad_lagindex = -1;
358
  rc->alt_ref_gf_group = 0;
359 360 361
  rc->fac_active_worst_inter = 150;
  rc->fac_active_worst_gf = 100;
  rc->force_qpmin = 0;
362
  for (i = 0; i < MAX_LAG_BUFFERS; ++i) rc->avg_source_sad[i] = 0;
363 364 365 366 367 368 369 370 371 372 373 374
  rc->frames_since_key = 8;  // Sensible default for first frame.
  rc->this_key_frame_forced = 0;
  rc->next_key_frame_forced = 0;
  rc->source_alt_ref_pending = 0;
  rc->source_alt_ref_active = 0;

  rc->frames_till_gf_update_due = 0;
  rc->ni_av_qi = oxcf->worst_allowed_q;
  rc->ni_tot_qi = 0;
  rc->ni_frames = 0;

  rc->tot_q = 0.0;
375
  rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
376

377 378 379
  for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
    rc->rate_correction_factors[i] = 1.0;
  }
380 381 382 383 384 385 386 387 388 389

  rc->min_gf_interval = oxcf->min_gf_interval;
  rc->max_gf_interval = oxcf->max_gf_interval;
  if (rc->min_gf_interval == 0)
    rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
        oxcf->width, oxcf->height, oxcf->init_framerate);
  if (rc->max_gf_interval == 0)
    rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
        oxcf->init_framerate, rc->min_gf_interval);
  rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
390 391
}

392
int vp9_rc_drop_frame(VP9_COMP *cpi) {
393
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
394
  RATE_CONTROL *const rc = &cpi->rc;
395 396 397
  if (!oxcf->drop_frames_water_mark ||
      (is_one_pass_cbr_svc(cpi) &&
       cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode)) {
398 399
    return 0;
  } else {
400
    if (rc->buffer_level < 0) {
401 402 403 404 405
      // Always drop if buffer is below 0.
      return 1;
    } else {
      // If buffer is below drop_mark, for now just drop every other frame
      // (starting with the next frame) until it increases back over drop_mark.
406 407 408
      int drop_mark =
          (int)(oxcf->drop_frames_water_mark * rc->optimal_buffer_level / 100);
      if ((rc->buffer_level > drop_mark) && (rc->decimation_factor > 0)) {
409
        --rc->decimation_factor;
410
      } else if (rc->buffer_level <= drop_mark && rc->decimation_factor == 0) {
411
        rc->decimation_factor = 1;
412
      }
413 414 415
      if (rc->decimation_factor > 0) {
        if (rc->decimation_count > 0) {
          --rc->decimation_count;
416 417
          return 1;
        } else {
418
          rc->decimation_count = rc->decimation_factor;
419 420 421
          return 0;
        }
      } else {
422
        rc->decimation_count = 0;
423 424 425 426 427 428
        return 0;
      }
    }
  }
}

429
static double get_rate_correction_factor(const VP9_COMP *cpi) {
430
  const RATE_CONTROL *const rc = &cpi->rc;
431
  double rcf;
432

433
  if (cpi->common.frame_type == KEY_FRAME) {
434
    rcf = rc->rate_correction_factors[KF_STD];
435
  } else if (cpi->oxcf.pass == 2) {
436
    RATE_FACTOR_LEVEL rf_lvl =
437
        cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
438
    rcf = rc->rate_correction_factors[rf_lvl];
439
  } else {
440
    if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
441
        !rc->is_src_frame_alt_ref && !cpi->use_svc &&
442
        (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
443
      rcf = rc->rate_correction_factors[GF_ARF_STD];
444
    else
445
      rcf = rc->rate_correction_factors[INTER_NORMAL];
446
  }
447
  rcf *= rcf_mult[rc->frame_size_selector];
448
  return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
449 450 451
}

static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
452 453
  RATE_CONTROL *const rc = &cpi->rc;

454 455 456
  // Normalize RCF to account for the size-dependent scaling factor.
  factor /= rcf_mult[cpi->rc.frame_size_selector];

457 458
  factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);

459
  if (cpi->common.frame_type == KEY_FRAME) {
460
    rc->rate_correction_factors[KF_STD] = factor;
461
  } else if (cpi->oxcf.pass == 2) {
462
    RATE_FACTOR_LEVEL rf_lvl =
463
        cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
464
    rc->rate_correction_factors[rf_lvl] = factor;
465
  } else {
466
    if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
467
        !rc->is_src_frame_alt_ref && !cpi->use_svc &&
468
        (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
469
      rc->rate_correction_factors[GF_ARF_STD] = factor;
470
    else
471
      rc->rate_correction_factors[INTER_NORMAL] = factor;
472 473 474
  }
}

475
void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) {
476
  const VP9_COMMON *const cm = &cpi->common;
477
  int correction_factor = 100;
478
  double rate_correction_factor = get_rate_correction_factor(cpi);
John Koleszar's avatar
John Koleszar committed
479
  double adjustment_limit;
John Koleszar's avatar
John Koleszar committed
480

481
  int projected_size_based_on_q = 0;
John Koleszar's avatar
John Koleszar committed
482

Paul Wilkins's avatar
Paul Wilkins committed
483
  // Do not update the rate factors for arf overlay frames.
484
  if (cpi->rc.is_src_frame_alt_ref) return;
Paul Wilkins's avatar
Paul Wilkins committed
485

John Koleszar's avatar
John Koleszar committed
486
  // Clear down mmx registers to allow floating point in what follows
487
  vpx_clear_system_state();
John Koleszar's avatar
John Koleszar committed
488

489 490
  // Work out how big we would have expected the frame to be at this Q given
  // the current correction factor.
John Koleszar's avatar
John Koleszar committed
491
  // Stay in double to avoid int overflow when values are large
492 493 494 495
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
    projected_size_based_on_q =
        vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
  } else {
496 497 498
    projected_size_based_on_q =
        vp9_estimate_bits_at_q(cpi->common.frame_type, cm->base_qindex, cm->MBs,
                               rate_correction_factor, cm->bit_depth);
499
  }
John Koleszar's avatar
John Koleszar committed
500
  // Work out a size correction factor.
501
  if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
502
    correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
503
                              projected_size_based_on_q);
John Koleszar's avatar
John Koleszar committed
504

505 506
  // More heavily damped adjustment used if we have been oscillating either side
  // of target.
507 508
  adjustment_limit =
      0.25 + 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
John Koleszar's avatar
John Koleszar committed
509

510 511 512 513 514 515 516 517 518 519
  cpi->rc.q_2_frame = cpi->rc.q_1_frame;
  cpi->rc.q_1_frame = cm->base_qindex;
  cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
  if (correction_factor > 110)
    cpi->rc.rc_1_frame = -1;
  else if (correction_factor < 90)
    cpi->rc.rc_1_frame = 1;
  else
    cpi->rc.rc_1_frame = 0;

520 521 522 523 524 525
  // Turn off oscilation detection in the case of massive overshoot.
  if (cpi->rc.rc_1_frame == -1 && cpi->rc.rc_2_frame == 1 &&
      correction_factor > 1000) {
    cpi->rc.rc_2_frame = 0;
  }

John Koleszar's avatar
John Koleszar committed
526 527
  if (correction_factor > 102) {
    // We are not already at the worst allowable quality
528 529
    correction_factor =
        (int)(100 + ((correction_factor - 100) * adjustment_limit));
530
    rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
John Koleszar's avatar
John Koleszar committed
531 532 533
    // Keep rate_correction_factor within limits
    if (rate_correction_factor > MAX_BPB_FACTOR)
      rate_correction_factor = MAX_BPB_FACTOR;
534
  } else if (correction_factor < 99) {
John Koleszar's avatar
John Koleszar committed
535
    // We are not already at the best allowable quality
536 537
    correction_factor =
        (int)(100 - ((100 - correction_factor) * adjustment_limit));
538
    rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
John Koleszar's avatar
John Koleszar committed
539 540 541 542 543 544

    // Keep rate_correction_factor within limits
    if (rate_correction_factor < MIN_BPB_FACTOR)
      rate_correction_factor = MIN_BPB_FACTOR;
  }

545
  set_rate_correction_factor(cpi, rate_correction_factor);
John Koleszar's avatar
John Koleszar committed
546 547
}

548 549
int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
                      int active_best_quality, int active_worst_quality) {
550
  const VP9_COMMON *const cm = &cpi->common;
551
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
552
  int q = active_worst_quality;
John Koleszar's avatar
John Koleszar committed
553
  int last_error = INT_MAX;
554
  int i, target_bits_per_mb, bits_per_mb_at_this_q;
555
  const double correction_factor = get_rate_correction_factor(cpi);
John Koleszar's avatar
John Koleszar committed
556

557 558
  // Calculate required scaling factor based on target frame size and size of
  // frame produced using previous Q.
559
  target_bits_per_mb =
560
      (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs);
John Koleszar's avatar
John Koleszar committed
561

562
  i = active_best_quality;
John Koleszar's avatar
John Koleszar committed
563

John Koleszar's avatar
John Koleszar committed
564
  do {
565
    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
566
        cr->apply_cyclic_refresh &&
567
        (!cpi->oxcf.gf_cbr_boost_pct || !cpi->refresh_golden_frame)) {
568 569 570
      bits_per_mb_at_this_q =
          (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
    } else {
571 572
      bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(
          cm->frame_type, i, correction_factor, cm->bit_depth);
573
    }
John Koleszar's avatar
John Koleszar committed
574

John Koleszar's avatar
John Koleszar committed
575 576
    if (bits_per_mb_at_this_q <= target_bits_per_mb) {
      if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
577
        q = i;
John Koleszar's avatar
John Koleszar committed
578
      else
579
        q = i - 1;
John Koleszar's avatar
John Koleszar committed
580

John Koleszar's avatar
John Koleszar committed
581
      break;
582
    } else {
John Koleszar's avatar
John Koleszar committed
583
      last_error = bits_per_mb_at_this_q - target_bits_per_mb;
584
    }
585
  } while (++i <= active_worst_quality);
586

587 588
  // In CBR mode, this makes sure q is between oscillating Qs to prevent
  // resonance.
589
  if (cpi->oxcf.rc_mode == VPX_CBR && !cpi->rc.reset_high_source_sad &&
590 591
      (!cpi->oxcf.gf_cbr_boost_pct ||
       !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)) &&
592 593
      (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
      cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
594 595
    q = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
              VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
596
  }
597
#if USE_ALTREF_FOR_ONE_PASS
598 599 600
  if (cpi->oxcf.enable_auto_arf && cpi->oxcf.pass == 0 &&
      cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
      cpi->rc.is_src_frame_alt_ref && !cpi->rc.alt_ref_gf_group) {
601 602 603
    q = VPXMIN(q, (q + cpi->rc.last_boosted_qindex) >> 1);
  }
#endif
604 605 606
  return q;
}

607 608
static int get_active_quality(int q, int gfu_boost, int low, int high,
                              int *low_motion_minq, int *high_motion_minq) {
609
  if (gfu_boost > high) {
610
    return low_motion_minq[q];
611
  } else if (gfu_boost < low) {
612
    return high_motion_minq[q];
613 614 615 616 617
  } else {
    const int gap = high - low;
    const int offset = high - gfu_boost;
    const int qdiff = high_motion_minq[q] - low_motion_minq[q];
    const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
618
    return low_motion_minq[q] + adjustment;
619 620 621
  }
}

622 623 624 625 626 627
static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
                                 vpx_bit_depth_t bit_depth) {
  int *kf_low_motion_minq;
  int *kf_high_motion_minq;
  ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
  ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
628 629 630 631
  return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
                            kf_low_motion_minq, kf_high_motion_minq);
}

632 633 634 635 636 637
static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
                                 vpx_bit_depth_t bit_depth) {
  int *arfgf_low_motion_minq;
  int *arfgf_high_motion_minq;
  ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
  ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
638 639 640 641
  return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
                            arfgf_low_motion_minq, arfgf_high_motion_minq);
}

642
static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
643 644
  const RATE_CONTROL *const rc = &cpi->rc;
  const unsigned int curr_frame = cpi->common.current_video_frame;
645
  int active_worst_quality;
646

647
  if (cpi->common.frame_type == KEY_FRAME) {
648 649
    active_worst_quality =
        curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] << 1;
650
  } else {
651 652
    if (!rc->is_src_frame_alt_ref &&
        (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
653 654 655 656
      active_worst_quality =
          curr_frame == 1
              ? rc->last_q[KEY_FRAME] * 5 >> 2
              : rc->last_q[INTER_FRAME] * rc->fac_active_worst_gf / 100;
657
    } else {
658 659 660 661
      active_worst_quality = curr_frame == 1
                                 ? rc->last_q[KEY_FRAME] << 1
                                 : rc->avg_frame_qindex[INTER_FRAME] *
                                       rc->fac_active_worst_inter / 100;
662 663
    }
  }
664
  return VPXMIN(active_worst_quality, rc->worst_quality);
665 666 667 668 669 670 671 672 673
}

// Adjust active_worst_quality level based on buffer level.
static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
  // Adjust active_worst_quality: If buffer is above the optimal/target level,
  // bring active_worst_quality down depending on fullness of buffer.
  // If buffer is below the optimal level, let the active_worst_quality go from
  // ambient Q (at buffer = optimal level) to worst_quality level
  // (at buffer = critical level).
674
  const VP9_COMMON *const cm = &cpi->common;
675 676
  const RATE_CONTROL *rc = &cpi->rc;
  // Buffer level below which we push active_worst to worst_quality.
677
  int64_t critical_level = rc->optimal_buffer_level >> 3;
678
  int64_t buff_lvl_step = 0;
679 680
  int adjustment = 0;
  int active_worst_quality;
681
  int ambient_qp;
682
  unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
683 684
  if (cm->frame_type == KEY_FRAME || rc->reset_high_source_sad)
    return rc->worst_quality;
685 686 687 688 689
  // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
  // for the first few frames following key frame. These are both initialized
  // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
  // So for first few frames following key, the qp of that key frame is weighted
  // into the active_worst_quality setting.
690 691 692 693
  ambient_qp = (cm->current_video_frame < num_frames_weight_key)
                   ? VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
                            rc->avg_frame_qindex[KEY_FRAME])
                   : rc->avg_frame_qindex[INTER_FRAME];
694 695 696 697 698 699 700 701 702 703 704
  // For SVC if the current base spatial layer was key frame, use the QP from
  // that base layer for ambient_qp.
  if (cpi->use_svc && cpi->svc.spatial_layer_id > 0) {
    int layer = LAYER_IDS_TO_IDX(0, cpi->svc.temporal_layer_id,
                                 cpi->svc.number_temporal_layers);
    const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
    if (lc->is_key_frame) {
      const RATE_CONTROL *lrc = &lc->rc;
      ambient_qp = VPXMIN(ambient_qp, lrc->last_q[KEY_FRAME]);
    }
  }
705
  active_worst_quality = VPXMIN(rc->worst_quality, ambient_qp * 5 >> 2);
706
  if (rc->buffer_level > rc->optimal_buffer_level) {
707
    // Adjust down.
708
    // Maximum limit for down adjustment, ~30%.
709 710
    int max_adjustment_down = active_worst_quality / 3;
    if (max_adjustment_down) {
711 712
      buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
                       max_adjustment_down);
713
      if (buff_lvl_step)
714
        adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
715
                           buff_lvl_step);
716 717 718 719 720
      active_worst_quality -= adjustment;
    }
  } else if (rc->buffer_level > critical_level) {
    // Adjust up from ambient Q.
    if (critical_level) {
721
      buff_lvl_step = (rc->optimal_buffer_level - critical_level);
722
      if (buff_lvl_step) {
723 724 725
        adjustment = (int)((rc->worst_quality - ambient_qp) *
                           (rc->optimal_buffer_level - rc->buffer_level) /
                           buff_lvl_step);
726
      }
727
      active_worst_quality = ambient_qp + adjustment;
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
    }
  } else {
    // Set to worst_quality if buffer is below critical level.
    active_worst_quality = rc->worst_quality;
  }
  return active_worst_quality;
}

static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
                                             int *bottom_index,
                                             int *top_index) {
  const VP9_COMMON *const cm = &cpi->common;
  const RATE_CONTROL *const rc = &cpi->rc;
  int active_best_quality;
  int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
  int q;
744 745
  int *rtc_minq;
  ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
746 747 748

  if (frame_is_intra_only(cm)) {
    active_best_quality = rc->best_quality;
749
    // Handle the special case for key frames forced when we have reached
750 751 752 753
    // the maximum key frame interval. Here force the Q to a range
    // based on the ambient Q to reduce the risk of popping.
    if (rc->this_key_frame_forced) {
      int qindex = rc->last_boosted_qindex;
754
      double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
755 756
      int delta_qindex = vp9_compute_qdelta(
          rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth);
757
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
758 759 760 761 762
    } else if (cm->current_video_frame > 0) {
      // not first frame of one pass and kf_boost is set
      double q_adj_factor = 1.0;
      double q_val;

763 764
      active_best_quality = get_kf_active_quality(
          rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
765 766 767 768 769 770 771 772

      // Allow somewhat lower kf minq with small image formats.
      if ((cm->width * cm->height) <= (352 * 288)) {
        q_adj_factor -= 0.25;
      }

      // Convert the adjustment factor to a qindex delta
      // on active_best_quality.
773
      q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
774 775
      active_best_quality +=
          vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
776
    }
777
  } else if (!rc->is_src_frame_alt_ref && !cpi->use_svc &&
778 779 780 781 782 783 784 785 786 787
             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
    // Use the lower of active_worst_quality and recent
    // average Q as basis for GF/ARF best Q limit unless last frame was
    // a key frame.
    if (rc->frames_since_key > 1 &&
        rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
      q = rc->avg_frame_qindex[INTER_FRAME];
    } else {
      q = active_worst_quality;
    }
788
    active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
789 790 791 792
  } else {
    // Use the lower of active_worst_quality and recent/average Q.
    if (cm->current_video_frame > 1) {
      if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
793
        active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
794
      else
795
        active_best_quality = rtc_minq[active_worst_quality];
796 797
    } else {
      if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
798
        active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
799
      else
800
        active_best_quality = rtc_minq[active_worst_quality];
801 802 803 804
    }
  }

  // Clip the active best and worst quality values to limits
805 806 807 808
  active_best_quality =
      clamp(active_best_quality, rc->best_quality, rc->worst_quality);
  active_worst_quality =
      clamp(active_worst_quality, active_best_quality, rc->worst_quality);
809 810 811 812 813 814

  *top_index = active_worst_quality;
  *bottom_index = active_best_quality;

#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
  // Limit Q range for the adaptive loop.
815
  if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
Paul Wilkins's avatar
Paul Wilkins committed
816 817
      !(cm->current_video_frame == 0)) {
    int qdelta = 0;
818
    vpx_clear_system_state();
819 820
    qdelta = vp9_compute_qdelta_by_rate(
        &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
Paul Wilkins's avatar
Paul Wilkins committed
821 822
    *top_index = active_worst_quality + qdelta;
    *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
823 824
  }
#endif
Paul Wilkins's avatar
Paul Wilkins committed
825

826 827 828 829
  // Special case code to try and match quality with forced key frames
  if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
    q = rc->last_boosted_qindex;
  } else {
830 831
    q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
                          active_worst_quality);
832 833
    if (q > *top_index) {
      // Special case when we are targeting the max allowed rate
834
      if (rc->this_frame_target >= rc->max_frame_bandwidth)
835 836 837 838 839
        *top_index = q;
      else
        q = *top_index;
    }
  }
840
  assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
841 842 843 844 845 846
  assert(*bottom_index <= rc->worst_quality &&
         *bottom_index >= rc->best_quality);
  assert(q <= rc->worst_quality && q >= rc->best_quality);
  return q;
}

847 848
static int get_active_cq_level_one_pass(const RATE_CONTROL *rc,
                                        const VP9EncoderConfig *const oxcf) {
849
  static const double cq_adjust_threshold = 0.1;
850
  int active_cq_level = oxcf->cq_level;
851
  if (oxcf->rc_mode == VPX_CQ && rc->total_target_bits > 0) {
852
    const double x = (double)rc->total_actual_bits / rc->total_target_bits;
853
    if (x < cq_adjust_threshold) {
854
      active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
855 856 857 858 859
    }
  }
  return active_cq_level;
}

860 861 862 863 864
#define SMOOTH_PCT_MIN 0.1
#define SMOOTH_PCT_DIV 0.05
static int get_active_cq_level_two_pass(const TWO_PASS *twopass,
                                        const RATE_CONTROL *rc,
                                        const VP9EncoderConfig *const oxcf) {
865 866 867 868
  static const double cq_adjust_threshold = 0.1;
  int active_cq_level = oxcf->cq_level;
  if (oxcf->rc_mode == VPX_CQ) {
    if (twopass->mb_smooth_pct > SMOOTH_PCT_MIN) {
869 870
      active_cq_level -=
          (int)((twopass->mb_smooth_pct - SMOOTH_PCT_MIN) / SMOOTH_PCT_DIV);
871 872 873 874 875 876 877 878 879 880 881 882
      active_cq_level = VPXMAX(active_cq_level, 0);
    }
    if (rc->total_target_bits > 0) {
      const double x = (double)rc->total_actual_bits / rc->total_target_bits;
      if (x < cq_adjust_threshold) {
        active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
      }
    }
  }
  return active_cq_level;
}

883 884 885
static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
                                             int *bottom_index,
                                             int *top_index) {
886
  const VP9_COMMON *const cm = &cpi->common;
887
  const RATE_CONTROL *const rc = &cpi->rc;
888
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
889
  const int cq_level = get_active_cq_level_one_pass(rc, oxcf);
890
  int active_best_quality;
891
  int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
892
  int q;
893 894
  int *inter_minq;
  ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
895 896

  if (frame_is_intra_only(cm)) {
897 898 899
    if (oxcf->rc_mode == VPX_Q) {
      int qindex = cq_level;
      double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
900
      int delta_qindex = vp9_compute_qdelta(rc, q, q * 0.25, cm->bit_depth);
901 902 903 904 905
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
    } else if (rc->this_key_frame_forced) {
      // Handle the special case for key frames forced when we have reached
      // the maximum key frame interval. Here force the Q to a range
      // based on the ambient Q to reduce the risk of popping.
906
      int qindex = rc->last_boosted_qindex;
907
      double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
908 909
      int delta_qindex = vp9_compute_qdelta(
          rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
910
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
911
    } else {
912
      // not first frame of one pass and kf_boost is set
913 914 915
      double q_adj_factor = 1.0;
      double q_val;

916 917
      active_best_quality = get_kf_active_quality(
          rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
918 919 920 921 922 923 924 925

      // Allow somewhat lower kf minq with small image formats.
      if ((cm->width * cm->height) <= (352 * 288)) {
        q_adj_factor -= 0.25;
      }

      // Convert the adjustment factor to a qindex delta
      // on active_best_quality.
926
      q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
927 928
      active_best_quality +=
          vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
929
    }
930
  } else if (!rc->is_src_frame_alt_ref &&
931
             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
932
    // Use the lower of active_worst_quality and recent
933 934
    // average Q as basis for GF/ARF best Q limit unless last frame was
    // a key frame.
935 936 937 938 939 940
    if (rc->frames_since_key > 1) {
      if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
        q = rc->avg_frame_qindex[INTER_FRAME];
      } else {
        q = active_worst_quality;
      }
941
    } else {
942
      q = rc->avg_frame_qindex[KEY_FRAME];
943 944
    }
    // For constrained quality dont allow Q less than the cq level
945
    if (oxcf->rc_mode == VPX_CQ) {
946
      if (q < cq_level) q = cq_level;
947

948
      active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
949

950
      // Constrained quality use slightly lower active best.
951
      active_best_quality = active_best_quality * 15 / 16;
952

953
    } else if (oxcf->rc_mode == VPX_Q) {
954 955 956 957 958 959 960 961
      int qindex = cq_level;
      double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
      int delta_qindex;
      if (cpi->refresh_alt_ref_frame)
        delta_qindex = vp9_compute_qdelta(rc, q, q * 0.40, cm->bit_depth);
      else
        delta_qindex = vp9_compute_qdelta(rc, q, q * 0.50, cm->bit_depth);
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
962
    } else {
963
      active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
964 965
    }
  } else {
966
    if (oxcf->rc_mode == VPX_Q) {
967 968
      int qindex = cq_level;
      double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
969 970 971 972 973
      double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
                                               0.70, 1.0, 0.85, 1.0 };
      int delta_qindex = vp9_compute_qdelta(
          rc, q, q * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL],
          cm->bit_depth);
974
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
975
    } else {
976 977
      // Use the min of the average Q and active_worst_quality as basis for
      // active_best.
978
      if (cm->current_video_frame > 1) {
979
        q = VPXMIN(rc->avg_frame_qindex[INTER_FRAME], active_worst_quality);
980 981
        active_best_quality = inter_minq[q];
      } else {
982
        active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
983
      }
984 985
      // For the constrained quality mode we don't want
      // q to fall below the cq level.
986
      if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
987
        active_best_quality = cq_level;
988 989 990 991 992
      }
    }
  }

  // Clip the active best and worst quality values to limits
993 994 995 996
  active_best_quality =
      clamp(active_best_quality, rc->best_quality, rc->worst_quality);
  active_worst_quality =
      clamp(active_worst_quality, active_best_quality, rc->worst_quality);
997

998 999
  *top_index = active_worst_quality;
  *bottom_index = active_best_quality;
1000 1001

#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
Paul Wilkins's avatar
Paul Wilkins committed
1002 1003
  {
    int qdelta = 0;
1004
    vpx_clear_system_state();
Paul Wilkins's avatar
Paul Wilkins committed
1005 1006

    // Limit Q range for the adaptive loop.
1007
    if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
Paul Wilkins's avatar
Paul Wilkins committed
1008
        !(cm->current_video_frame == 0)) {
1009 1010
      qdelta = vp9_compute_qdelta_by_rate(
          &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
Paul Wilkins's avatar
Paul Wilkins committed
1011 1012
    } else if (!rc->is_src_frame_alt_ref &&
               (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1013 1014
      qdelta = vp9_compute_qdelta_by_rate(
          &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth);
Paul Wilkins's avatar
Paul Wilkins committed
1015 1016 1017
    }
    *top_index = active_worst_quality + qdelta;
    *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
1018
  }
1019
#endif
Paul Wilkins's avatar
Paul Wilkins committed
1020

1021
  if (oxcf->rc_mode == VPX_Q) {
1022
    q = active_best_quality;
1023
    // Special case code to try and match quality with forced key frames
1024 1025
  } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
    q = rc->last_boosted_qindex;
1026
  } else {
1027 1028
    q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
                          active_worst_quality);
1029 1030
    if (q > *top_index) {
      // Special case when we are targeting the max allowed rate
1031
      if (rc->this_frame_target >= rc->max_frame_bandwidth)
1032 1033 1034 1035
        *top_index = q;
      else
        q = *top_index;
    }
1036
  }
1037

1038
  assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1039 1040 1041
  assert(*bottom_index <= rc->worst_quality &&
         *bottom_index >= rc->best_quality);
  assert(q <= rc->worst_quality && q >= rc->best_quality);
1042
  return q;
John Koleszar's avatar
John Koleszar committed
1043 1044
}

1045 1046 1047 1048 1049 1050 1051 1052
int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) {
  static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
    1.00,  // INTER_NORMAL
    1.00,  // INTER_HIGH
    1.50,  // GF_ARF_LOW
    1.75,  // GF_ARF_STD
    2.00,  // KF_STD
  };
1053 1054 1055
  static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] = {
    INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME
  };
1056
  const VP9_COMMON *const cm = &cpi->common;
1057 1058 1059
  int qdelta =
      vp9_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level], q,
                                 rate_factor_deltas[rf_level], cm->bit_depth);
1060 1061 1062
  return qdelta;
}

1063
#define STATIC_MOTION_THRESH 95
1064
static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, int *bottom_index,
1065
                                         int *top_index) {
1066 1067
  const VP9_COMMON *const cm = &cpi->common;
  const RATE_CONTROL *const rc = &cpi->rc;
1068
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1069
  const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1070
  const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf);
Deb Mukherjee's avatar