Coverage Report

Created: 2025-08-28 07:12

/src/libvpx/vp9/encoder/vp9_ratectrl.c
Line
Count
Source (jump to first uncovered line)
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 <stdint.h>
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include <string.h>
18
19
#include "./vpx_dsp_rtcd.h"
20
#include "vpx_dsp/vpx_dsp_common.h"
21
#include "vpx_mem/vpx_mem.h"
22
#include "vpx_ports/mem.h"
23
#include "vpx_ports/system_state.h"
24
25
#include "vp9/common/vp9_alloccommon.h"
26
#include "vp9/common/vp9_blockd.h"
27
#include "vp9/common/vp9_common.h"
28
#include "vp9/common/vp9_entropymode.h"
29
#include "vp9/common/vp9_onyxc_int.h"
30
#include "vp9/common/vp9_quant_common.h"
31
#include "vp9/common/vp9_seg_common.h"
32
33
#include "vp9/encoder/vp9_aq_cyclicrefresh.h"
34
#include "vp9/encoder/vp9_encodemv.h"
35
#include "vp9/encoder/vp9_encoder.h"
36
#include "vp9/encoder/vp9_ext_ratectrl.h"
37
#include "vp9/encoder/vp9_firstpass.h"
38
#include "vp9/encoder/vp9_ratectrl.h"
39
#include "vp9/encoder/vp9_svc_layercontext.h"
40
41
#include "vpx/vpx_codec.h"
42
#include "vpx/vpx_ext_ratectrl.h"
43
#include "vpx/internal/vpx_codec_internal.h"
44
45
// Max rate per frame for 1080P and below encodes if no level requirement given.
46
// For larger formats limit to MAX_MB_RATE bits per MB
47
// 4Mbits is derived from the level requirement for level 4 (1080P 30) which
48
// requires that HW can sustain a rate of 16Mbits over a 4 frame group.
49
// If a lower level requirement is specified then this may over ride this value.
50
#define MAX_MB_RATE 250
51
#define MAXRATE_1080P 4000000
52
53
#define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
54
55
176k
#define MIN_BPB_FACTOR 0.005
56
171k
#define MAX_BPB_FACTOR 50
57
58
#if CONFIG_VP9_HIGHBITDEPTH
59
#define ASSIGN_MINQ_TABLE(bit_depth, name)       \
60
74.6k
  do {                                           \
61
74.6k
    switch (bit_depth) {                         \
62
74.6k
      case VPX_BITS_8: name = name##_8; break;   \
63
0
      case VPX_BITS_10: name = name##_10; break; \
64
0
      default:                                   \
65
0
        assert(bit_depth == VPX_BITS_12);        \
66
0
        name = name##_12;                        \
67
0
        break;                                   \
68
74.6k
    }                                            \
69
74.6k
  } while (0)
70
#else
71
#define ASSIGN_MINQ_TABLE(bit_depth, name) \
72
  do {                                     \
73
    (void)bit_depth;                       \
74
    name = name##_8;                       \
75
  } while (0)
76
#endif
77
78
// Tables relating active max Q to active min Q
79
static int kf_low_motion_minq_8[QINDEX_RANGE];
80
static int kf_high_motion_minq_8[QINDEX_RANGE];
81
static int arfgf_low_motion_minq_8[QINDEX_RANGE];
82
static int arfgf_high_motion_minq_8[QINDEX_RANGE];
83
static int inter_minq_8[QINDEX_RANGE];
84
static int rtc_minq_8[QINDEX_RANGE];
85
86
#if CONFIG_VP9_HIGHBITDEPTH
87
static int kf_low_motion_minq_10[QINDEX_RANGE];
88
static int kf_high_motion_minq_10[QINDEX_RANGE];
89
static int arfgf_low_motion_minq_10[QINDEX_RANGE];
90
static int arfgf_high_motion_minq_10[QINDEX_RANGE];
91
static int inter_minq_10[QINDEX_RANGE];
92
static int rtc_minq_10[QINDEX_RANGE];
93
static int kf_low_motion_minq_12[QINDEX_RANGE];
94
static int kf_high_motion_minq_12[QINDEX_RANGE];
95
static int arfgf_low_motion_minq_12[QINDEX_RANGE];
96
static int arfgf_high_motion_minq_12[QINDEX_RANGE];
97
static int inter_minq_12[QINDEX_RANGE];
98
static int rtc_minq_12[QINDEX_RANGE];
99
#endif
100
101
#ifdef AGGRESSIVE_VBR
102
static int gf_high = 2400;
103
static int gf_low = 400;
104
static int kf_high = 4000;
105
static int kf_low = 400;
106
#else
107
static int gf_high = 2000;
108
static int gf_low = 400;
109
static int kf_high = 4800;
110
static int kf_low = 300;
111
#endif
112
113
// Functions to compute the active minq lookup table entries based on a
114
// formulaic approach to facilitate easier adjustment of the Q tables.
115
// The formulae were derived from computing a 3rd order polynomial best
116
// fit to the original data (after plotting real maxq vs minq (not q index))
117
static int get_minq_index(double maxq, double x3, double x2, double x1,
118
4.60k
                          vpx_bit_depth_t bit_depth) {
119
4.60k
  int i;
120
4.60k
  const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
121
122
  // Special case handling to deal with the step from q2.0
123
  // down to lossless mode represented by q 1.0.
124
4.60k
  if (minqtarget <= 2.0) return 0;
125
126
371k
  for (i = 0; i < QINDEX_RANGE; i++) {
127
371k
    if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth)) return i;
128
371k
  }
129
130
0
  return QINDEX_RANGE - 1;
131
4.19k
}
132
133
static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
134
                           int *arfgf_high, int *inter, int *rtc,
135
3
                           vpx_bit_depth_t bit_depth) {
136
3
  int i;
137
771
  for (i = 0; i < QINDEX_RANGE; i++) {
138
768
    const double maxq = vp9_convert_qindex_to_q(i, bit_depth);
139
768
    kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
140
768
    kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth);
141
#ifdef AGGRESSIVE_VBR
142
    arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.275, bit_depth);
143
    inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.80, bit_depth);
144
#else
145
768
    arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
146
768
    inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
147
768
#endif
148
768
    arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
149
768
    rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
150
768
  }
151
3
}
152
153
1
void vp9_rc_init_minq_luts(void) {
154
1
  init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
155
1
                 arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
156
1
                 inter_minq_8, rtc_minq_8, VPX_BITS_8);
157
1
#if CONFIG_VP9_HIGHBITDEPTH
158
1
  init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
159
1
                 arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
160
1
                 inter_minq_10, rtc_minq_10, VPX_BITS_10);
161
1
  init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
162
1
                 arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
163
1
                 inter_minq_12, rtc_minq_12, VPX_BITS_12);
164
1
#endif
165
1
}
166
167
// These functions use formulaic calculations to make playing with the
168
// quantizer tables easier. If necessary they can be replaced by lookup
169
// tables if and when things settle down in the experimental bitstream
170
5.35M
double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) {
171
// Convert the index to a real Q value (scaled down to match old Q values)
172
5.35M
#if CONFIG_VP9_HIGHBITDEPTH
173
5.35M
  switch (bit_depth) {
174
5.10M
    case VPX_BITS_8: return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
175
125k
    case VPX_BITS_10: return vp9_ac_quant(qindex, 0, bit_depth) / 16.0;
176
126k
    default:
177
126k
      assert(bit_depth == VPX_BITS_12);
178
126k
      return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
179
5.35M
  }
180
#else
181
  return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
182
#endif
183
5.35M
}
184
185
0
int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth) {
186
0
  int i;
187
188
0
  for (i = 0; i < QINDEX_RANGE; ++i)
189
0
    if (vp9_convert_qindex_to_q(i, bit_depth) >= q_val) break;
190
191
0
  if (i == QINDEX_RANGE) i--;
192
193
0
  return i;
194
0
}
195
196
int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
197
3.14M
                       double correction_factor, vpx_bit_depth_t bit_depth) {
198
3.14M
  const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
199
3.14M
  int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
200
201
3.14M
  assert(correction_factor <= MAX_BPB_FACTOR &&
202
3.14M
         correction_factor >= MIN_BPB_FACTOR);
203
204
  // q based adjustment to baseline enumerator
205
3.14M
  enumerator += (int)(enumerator * q) >> 12;
206
3.14M
  return (int)(enumerator * correction_factor / q);
207
3.14M
}
208
209
int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
210
                           double correction_factor,
211
53.0k
                           vpx_bit_depth_t bit_depth) {
212
53.0k
  const int bpm =
213
53.0k
      (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth));
214
53.0k
  return VPXMAX(FRAME_OVERHEAD_BITS,
215
53.0k
                (int)(((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS));
216
53.0k
}
217
218
43.6k
int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
219
43.6k
  const RATE_CONTROL *rc = &cpi->rc;
220
43.6k
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
221
222
43.6k
  const int min_frame_target =
223
43.6k
      VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
224
43.6k
  if (target < min_frame_target) target = min_frame_target;
225
43.6k
  if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
226
    // If there is an active ARF at this location use the minimum
227
    // bits on this frame even if it is a constructed arf.
228
    // The active maximum quantizer insures that an appropriate
229
    // number of bits will be spent if needed for constructed ARFs.
230
0
    target = min_frame_target;
231
0
  }
232
233
  // Clip the frame target to the maximum allowed value.
234
43.6k
  if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
235
236
43.6k
  if (oxcf->rc_max_inter_bitrate_pct) {
237
0
    const int64_t max_rate =
238
0
        (int64_t)rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
239
    // target is of type int and VPXMIN cannot evaluate to larger than target
240
0
    target = (int)VPXMIN(target, max_rate);
241
0
  }
242
43.6k
  return target;
243
43.6k
}
244
245
12.4k
int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
246
12.4k
  const RATE_CONTROL *rc = &cpi->rc;
247
12.4k
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
248
12.4k
  if (oxcf->rc_max_intra_bitrate_pct) {
249
0
    const int64_t max_rate =
250
0
        (int64_t)rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
251
0
    target = (int)VPXMIN(target, max_rate);
252
0
  }
253
12.4k
  if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
254
12.4k
  return target;
255
12.4k
}
256
257
// TODO(marpan/jianj): bits_off_target and buffer_level are used in the same
258
// way for CBR mode, for the buffering updates below. Look into removing one
259
// of these (i.e., bits_off_target).
260
// Update the buffer level before encoding with the per-frame-bandwidth,
261
53.1k
void vp9_update_buffer_level_preencode(VP9_COMP *cpi) {
262
53.1k
  RATE_CONTROL *const rc = &cpi->rc;
263
53.1k
  rc->bits_off_target += rc->avg_frame_bandwidth;
264
  // Clip the buffer level to the maximum specified buffer size.
265
53.1k
  rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
266
53.1k
  rc->buffer_level = rc->bits_off_target;
267
53.1k
}
268
269
// Update the buffer level before encoding with the per-frame-bandwidth
270
// for SVC. The current and all upper temporal layers are updated, needed
271
// for the layered rate control which involves cumulative buffer levels for
272
// the temporal layers. Allow for using the timestamp(pts) delta for the
273
// framerate when the set_ref_frame_config is used.
274
0
void vp9_update_buffer_level_svc_preencode(VP9_COMP *cpi) {
275
0
  SVC *const svc = &cpi->svc;
276
0
  int i;
277
  // Set this to 1 to use timestamp delta for "framerate" under
278
  // ref_frame_config usage.
279
0
  int use_timestamp = 1;
280
0
  const int64_t ts_delta =
281
0
      svc->time_stamp_superframe - svc->time_stamp_prev[svc->spatial_layer_id];
282
0
  for (i = svc->temporal_layer_id; i < svc->number_temporal_layers; ++i) {
283
0
    const int layer =
284
0
        LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
285
0
    LAYER_CONTEXT *const lc = &svc->layer_context[layer];
286
0
    RATE_CONTROL *const lrc = &lc->rc;
287
0
    if (use_timestamp && cpi->svc.use_set_ref_frame_config &&
288
0
        svc->number_temporal_layers == 1 && ts_delta > 0 &&
289
0
        svc->current_superframe > 0) {
290
      // TODO(marpan): This may need to be modified for temporal layers.
291
0
      const double framerate_pts = 10000000.0 / ts_delta;
292
0
      lrc->bits_off_target += saturate_cast_double_to_int(
293
0
          round(lc->target_bandwidth / framerate_pts));
294
0
    } else {
295
0
      lrc->bits_off_target += saturate_cast_double_to_int(
296
0
          round(lc->target_bandwidth / lc->framerate));
297
0
    }
298
    // Clip buffer level to maximum buffer size for the layer.
299
0
    lrc->bits_off_target =
300
0
        VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
301
0
    lrc->buffer_level = lrc->bits_off_target;
302
0
    if (i == svc->temporal_layer_id) {
303
0
      cpi->rc.bits_off_target = lrc->bits_off_target;
304
0
      cpi->rc.buffer_level = lrc->buffer_level;
305
0
    }
306
0
  }
307
0
}
308
309
// Update the buffer level for higher temporal layers, given the encoded current
310
// temporal layer.
311
static void update_layer_buffer_level_postencode(SVC *svc,
312
0
                                                 int encoded_frame_size) {
313
0
  int i = 0;
314
0
  const int current_temporal_layer = svc->temporal_layer_id;
315
0
  for (i = current_temporal_layer + 1; i < svc->number_temporal_layers; ++i) {
316
0
    const int layer =
317
0
        LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
318
0
    LAYER_CONTEXT *lc = &svc->layer_context[layer];
319
0
    RATE_CONTROL *lrc = &lc->rc;
320
0
    lrc->bits_off_target -= encoded_frame_size;
321
    // Clip buffer level to maximum buffer size for the layer.
322
0
    lrc->bits_off_target =
323
0
        VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
324
0
    lrc->buffer_level = lrc->bits_off_target;
325
0
  }
326
0
}
327
328
// Update the buffer level after encoding with encoded frame size.
329
static void update_buffer_level_postencode(VP9_COMP *cpi,
330
53.0k
                                           int encoded_frame_size) {
331
53.0k
  RATE_CONTROL *const rc = &cpi->rc;
332
53.0k
  rc->bits_off_target -= encoded_frame_size;
333
  // Clip the buffer level to the maximum specified buffer size.
334
53.0k
  rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
335
  // For screen-content mode, and if frame-dropper is off, don't let buffer
336
  // level go below threshold, given here as -rc->maximum_ buffer_size.
337
53.0k
  if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
338
53.0k
      cpi->oxcf.drop_frames_water_mark == 0)
339
0
    rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size);
340
341
53.0k
  rc->buffer_level = rc->bits_off_target;
342
343
53.0k
  if (is_one_pass_svc(cpi)) {
344
0
    update_layer_buffer_level_postencode(&cpi->svc, encoded_frame_size);
345
0
  }
346
53.0k
}
347
348
int vp9_rc_get_default_min_gf_interval(int width, int height,
349
86.3k
                                       double framerate) {
350
  // Assume we do not need any constraint lower than 4K 20 fps
351
86.3k
  static const double factor_safe = 3840 * 2160 * 20.0;
352
86.3k
  const double factor = width * height * framerate;
353
86.3k
  const int default_interval =
354
86.3k
      clamp((int)round(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
355
356
86.3k
  if (factor <= factor_safe)
357
84.2k
    return default_interval;
358
2.07k
  else
359
2.07k
    return VPXMAX(default_interval,
360
86.3k
                  (int)round(MIN_GF_INTERVAL * factor / factor_safe));
361
  // Note this logic makes:
362
  // 4K24: 5
363
  // 4K30: 6
364
  // 4K60: 12
365
86.3k
}
366
367
86.3k
int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
368
86.3k
  int interval = VPXMIN(MAX_GF_INTERVAL, (int)round(framerate * 0.75));
369
86.3k
  interval += (interval & 0x01);  // Round to even value
370
86.3k
  return VPXMAX(interval, min_gf_interval);
371
86.3k
}
372
373
3.99k
void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
374
3.99k
  int i;
375
376
3.99k
  if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
377
0
    rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
378
0
    rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
379
3.99k
  } else {
380
3.99k
    rc->avg_frame_qindex[KEY_FRAME] =
381
3.99k
        (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
382
3.99k
    rc->avg_frame_qindex[INTER_FRAME] =
383
3.99k
        (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
384
3.99k
  }
385
386
3.99k
  rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
387
3.99k
  rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
388
389
3.99k
  rc->buffer_level = rc->starting_buffer_level;
390
3.99k
  rc->bits_off_target = rc->starting_buffer_level;
391
392
3.99k
  rc->rolling_target_bits = rc->avg_frame_bandwidth;
393
3.99k
  rc->rolling_actual_bits = rc->avg_frame_bandwidth;
394
3.99k
  rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
395
3.99k
  rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
396
397
3.99k
  rc->total_actual_bits = 0;
398
3.99k
  rc->total_target_bits = 0;
399
3.99k
  rc->total_target_vs_actual = 0;
400
3.99k
  rc->avg_frame_low_motion = 0;
401
3.99k
  rc->count_last_scene_change = 0;
402
3.99k
  rc->af_ratio_onepass_vbr = 10;
403
3.99k
  rc->prev_avg_source_sad_lag = 0;
404
3.99k
  rc->high_source_sad = 0;
405
3.99k
  rc->reset_high_source_sad = 0;
406
3.99k
  rc->high_source_sad_lagindex = -1;
407
3.99k
  rc->high_num_blocks_with_motion = 0;
408
3.99k
  rc->hybrid_intra_scene_change = 0;
409
3.99k
  rc->re_encode_maxq_scene_change = 0;
410
3.99k
  rc->alt_ref_gf_group = 0;
411
3.99k
  rc->last_frame_is_src_altref = 0;
412
3.99k
  rc->fac_active_worst_inter = 150;
413
3.99k
  rc->fac_active_worst_gf = 100;
414
3.99k
  rc->force_qpmin = 0;
415
103k
  for (i = 0; i < MAX_LAG_BUFFERS; ++i) rc->avg_source_sad[i] = 0;
416
3.99k
  rc->frames_to_key = 0;
417
3.99k
  rc->frames_since_key = 8;  // Sensible default for first frame.
418
3.99k
  rc->this_key_frame_forced = 0;
419
3.99k
  rc->next_key_frame_forced = 0;
420
3.99k
  rc->source_alt_ref_pending = 0;
421
3.99k
  rc->source_alt_ref_active = 0;
422
423
3.99k
  rc->frames_till_gf_update_due = 0;
424
3.99k
  rc->constrain_gf_key_freq_onepass_vbr = 1;
425
3.99k
  rc->ni_av_qi = oxcf->worst_allowed_q;
426
3.99k
  rc->ni_tot_qi = 0;
427
3.99k
  rc->ni_frames = 0;
428
429
3.99k
  rc->tot_q = 0.0;
430
3.99k
  rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
431
432
23.9k
  for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
433
19.9k
    rc->rate_correction_factors[i] = 1.0;
434
19.9k
    rc->damped_adjustment[i] = 0;
435
19.9k
  }
436
437
3.99k
  rc->min_gf_interval = oxcf->min_gf_interval;
438
3.99k
  rc->max_gf_interval = oxcf->max_gf_interval;
439
3.99k
  if (rc->min_gf_interval == 0)
440
3.99k
    rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
441
3.99k
        oxcf->width, oxcf->height, oxcf->init_framerate);
442
3.99k
  if (rc->max_gf_interval == 0)
443
3.99k
    rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
444
3.99k
        oxcf->init_framerate, rc->min_gf_interval);
445
3.99k
  rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
446
3.99k
  if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
447
191
    rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
448
3.80k
  } else {
449
3.80k
    rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH;
450
3.80k
  }
451
452
3.99k
  rc->force_max_q = 0;
453
3.99k
  rc->last_post_encode_dropped_scene_change = 0;
454
3.99k
  rc->use_post_encode_drop = 0;
455
3.99k
  rc->ext_use_post_encode_drop = 0;
456
3.99k
  rc->disable_overshoot_maxq_cbr = 0;
457
3.99k
  rc->arf_active_best_quality_adjustment_factor = 1.0;
458
3.99k
  rc->arf_increase_active_best_quality = 0;
459
3.99k
  rc->preserve_arf_as_gld = 0;
460
3.99k
  rc->preserve_next_arf_as_gld = 0;
461
3.99k
  rc->show_arf_as_gld = 0;
462
3.99k
}
463
464
0
static int check_buffer_above_thresh(VP9_COMP *cpi, int drop_mark) {
465
0
  SVC *svc = &cpi->svc;
466
0
  if (!cpi->use_svc || cpi->svc.framedrop_mode != FULL_SUPERFRAME_DROP) {
467
0
    RATE_CONTROL *const rc = &cpi->rc;
468
0
    return (rc->buffer_level > drop_mark);
469
0
  } else {
470
0
    int i;
471
    // For SVC in the FULL_SUPERFRAME_DROP): the condition on
472
    // buffer (if its above threshold, so no drop) is checked on current and
473
    // upper spatial layers. If any spatial layer is not above threshold then
474
    // we return 0.
475
0
    for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) {
476
0
      const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
477
0
                                         svc->number_temporal_layers);
478
0
      LAYER_CONTEXT *lc = &svc->layer_context[layer];
479
0
      RATE_CONTROL *lrc = &lc->rc;
480
      // Exclude check for layer whose bitrate is 0.
481
0
      if (lc->target_bandwidth > 0) {
482
0
        const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] *
483
0
                                          lrc->optimal_buffer_level / 100);
484
0
        if (!(lrc->buffer_level > drop_mark_layer)) return 0;
485
0
      }
486
0
    }
487
0
    return 1;
488
0
  }
489
0
}
490
491
0
static int check_buffer_below_thresh(VP9_COMP *cpi, int drop_mark) {
492
0
  SVC *svc = &cpi->svc;
493
0
  if (!cpi->use_svc || cpi->svc.framedrop_mode == LAYER_DROP) {
494
0
    RATE_CONTROL *const rc = &cpi->rc;
495
0
    return (rc->buffer_level <= drop_mark);
496
0
  } else {
497
0
    int i;
498
    // For SVC in the constrained framedrop mode (svc->framedrop_mode =
499
    // CONSTRAINED_LAYER_DROP or FULL_SUPERFRAME_DROP): the condition on
500
    // buffer (if its below threshold, so drop frame) is checked on current
501
    // and upper spatial layers. For FULL_SUPERFRAME_DROP mode if any
502
    // spatial layer is <= threshold, then we return 1 (drop).
503
0
    for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) {
504
0
      const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
505
0
                                         svc->number_temporal_layers);
506
0
      LAYER_CONTEXT *lc = &svc->layer_context[layer];
507
0
      RATE_CONTROL *lrc = &lc->rc;
508
      // Exclude check for layer whose bitrate is 0.
509
0
      if (lc->target_bandwidth > 0) {
510
0
        const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] *
511
0
                                          lrc->optimal_buffer_level / 100);
512
0
        if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP) {
513
0
          if (lrc->buffer_level <= drop_mark_layer) return 1;
514
0
        } else {
515
0
          if (!(lrc->buffer_level <= drop_mark_layer)) return 0;
516
0
        }
517
0
      }
518
0
    }
519
0
    if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP)
520
0
      return 0;
521
0
    else
522
0
      return 1;
523
0
  }
524
0
}
525
526
0
int vp9_test_drop(VP9_COMP *cpi) {
527
0
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
528
0
  RATE_CONTROL *const rc = &cpi->rc;
529
0
  SVC *svc = &cpi->svc;
530
0
  int drop_frames_water_mark = oxcf->drop_frames_water_mark;
531
0
  if (cpi->use_svc) {
532
    // If we have dropped max_consec_drop frames, then we don't
533
    // drop this spatial layer, and reset counter to 0.
534
0
    if (svc->drop_count[svc->spatial_layer_id] == svc->max_consec_drop) {
535
0
      svc->drop_count[svc->spatial_layer_id] = 0;
536
0
      return 0;
537
0
    } else {
538
0
      drop_frames_water_mark = svc->framedrop_thresh[svc->spatial_layer_id];
539
0
    }
540
0
  }
541
0
  if (!drop_frames_water_mark ||
542
0
      (svc->spatial_layer_id > 0 &&
543
0
       svc->framedrop_mode == FULL_SUPERFRAME_DROP)) {
544
0
    return 0;
545
0
  } else {
546
0
    if ((rc->buffer_level < 0 && svc->framedrop_mode != FULL_SUPERFRAME_DROP) ||
547
0
        (check_buffer_below_thresh(cpi, -1) &&
548
0
         svc->framedrop_mode == FULL_SUPERFRAME_DROP)) {
549
      // Always drop if buffer is below 0.
550
0
      return 1;
551
0
    } else {
552
      // If buffer is below drop_mark, for now just drop every other frame
553
      // (starting with the next frame) until it increases back over drop_mark.
554
0
      int drop_mark =
555
0
          (int)(drop_frames_water_mark * rc->optimal_buffer_level / 100);
556
0
      if (check_buffer_above_thresh(cpi, drop_mark) &&
557
0
          (rc->decimation_factor > 0)) {
558
0
        --rc->decimation_factor;
559
0
      } else if (check_buffer_below_thresh(cpi, drop_mark) &&
560
0
                 rc->decimation_factor == 0) {
561
0
        rc->decimation_factor = 1;
562
0
      }
563
0
      if (rc->decimation_factor > 0) {
564
0
        if (rc->decimation_count > 0) {
565
0
          --rc->decimation_count;
566
0
          return 1;
567
0
        } else {
568
0
          rc->decimation_count = rc->decimation_factor;
569
0
          return 0;
570
0
        }
571
0
      } else {
572
0
        rc->decimation_count = 0;
573
0
        return 0;
574
0
      }
575
0
    }
576
0
  }
577
0
}
578
579
0
int post_encode_drop_cbr(VP9_COMP *cpi, size_t *size) {
580
0
  size_t frame_size = *size << 3;
581
0
  int64_t new_buffer_level =
582
0
      cpi->rc.buffer_level + cpi->rc.avg_frame_bandwidth - (int64_t)frame_size;
583
584
  // For now we drop if new buffer level (given the encoded frame size) goes
585
  // below 0.
586
0
  if (new_buffer_level < 0) {
587
0
    *size = 0;
588
0
    vp9_rc_postencode_update_drop_frame(cpi);
589
    // Update flag to use for next frame.
590
0
    if (cpi->rc.high_source_sad ||
591
0
        (cpi->use_svc && cpi->svc.high_source_sad_superframe))
592
0
      cpi->rc.last_post_encode_dropped_scene_change = 1;
593
    // Force max_q on next fame.
594
0
    cpi->rc.force_max_q = 1;
595
0
    cpi->rc.avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality;
596
0
    cpi->last_frame_dropped = 1;
597
0
    cpi->ext_refresh_frame_flags_pending = 0;
598
0
    if (cpi->use_svc) {
599
0
      SVC *svc = &cpi->svc;
600
0
      int sl = 0;
601
0
      int tl = 0;
602
0
      svc->last_layer_dropped[svc->spatial_layer_id] = 1;
603
0
      svc->drop_spatial_layer[svc->spatial_layer_id] = 1;
604
0
      svc->drop_count[svc->spatial_layer_id]++;
605
0
      svc->skip_enhancement_layer = 1;
606
      // Postencode drop is only checked on base spatial layer,
607
      // for now if max-q is set on base we force it on all layers.
608
0
      for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
609
0
        for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
610
0
          const int layer =
611
0
              LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
612
0
          LAYER_CONTEXT *lc = &svc->layer_context[layer];
613
0
          RATE_CONTROL *lrc = &lc->rc;
614
0
          lrc->force_max_q = 1;
615
0
          lrc->avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality;
616
0
        }
617
0
      }
618
0
    }
619
0
    return 1;
620
0
  }
621
622
0
  cpi->rc.force_max_q = 0;
623
0
  cpi->rc.last_post_encode_dropped_scene_change = 0;
624
0
  return 0;
625
0
}
626
627
0
int vp9_rc_drop_frame(VP9_COMP *cpi) {
628
0
  SVC *svc = &cpi->svc;
629
0
  int svc_prev_layer_dropped = 0;
630
  // In the constrained or full_superframe framedrop mode for svc
631
  // (framedrop_mode != (LAYER_DROP && CONSTRAINED_FROM_ABOVE)),
632
  // if the previous spatial layer was dropped, drop the current spatial layer.
633
0
  if (cpi->use_svc && svc->spatial_layer_id > 0 &&
634
0
      svc->drop_spatial_layer[svc->spatial_layer_id - 1])
635
0
    svc_prev_layer_dropped = 1;
636
0
  if ((svc_prev_layer_dropped && svc->framedrop_mode != LAYER_DROP &&
637
0
       svc->framedrop_mode != CONSTRAINED_FROM_ABOVE_DROP) ||
638
0
      svc->force_drop_constrained_from_above[svc->spatial_layer_id] ||
639
0
      vp9_test_drop(cpi)) {
640
0
    vp9_rc_postencode_update_drop_frame(cpi);
641
0
    cpi->ext_refresh_frame_flags_pending = 0;
642
0
    cpi->last_frame_dropped = 1;
643
0
    if (cpi->use_svc) {
644
0
      svc->last_layer_dropped[svc->spatial_layer_id] = 1;
645
0
      svc->drop_spatial_layer[svc->spatial_layer_id] = 1;
646
0
      svc->drop_count[svc->spatial_layer_id]++;
647
0
      svc->skip_enhancement_layer = 1;
648
0
      if (svc->framedrop_mode == LAYER_DROP ||
649
0
          (svc->framedrop_mode == CONSTRAINED_FROM_ABOVE_DROP &&
650
0
           svc->force_drop_constrained_from_above[svc->number_spatial_layers -
651
0
                                                  1] == 0) ||
652
0
          svc->drop_spatial_layer[0] == 0) {
653
        // For the case of constrained drop mode where full superframe is
654
        // dropped, we don't increment the svc frame counters.
655
        // In particular temporal layer counter (which is incremented in
656
        // vp9_inc_frame_in_layer()) won't be incremented, so on a dropped
657
        // frame we try the same temporal_layer_id on next incoming frame.
658
        // This is to avoid an issue with temporal alignment with full
659
        // superframe dropping.
660
0
        vp9_inc_frame_in_layer(cpi);
661
0
      }
662
0
      if (svc->spatial_layer_id == svc->number_spatial_layers - 1) {
663
0
        int i;
664
0
        int all_layers_drop = 1;
665
0
        for (i = 0; i < svc->spatial_layer_id; i++) {
666
0
          if (svc->drop_spatial_layer[i] == 0) {
667
0
            all_layers_drop = 0;
668
0
            break;
669
0
          }
670
0
        }
671
0
        if (all_layers_drop == 1) svc->skip_enhancement_layer = 0;
672
0
      }
673
0
    }
674
0
    return 1;
675
0
  }
676
0
  return 0;
677
0
}
678
679
0
static int adjust_q_cbr(const VP9_COMP *cpi, int q) {
680
  // This makes sure q is between oscillating Qs to prevent resonance.
681
0
  if (!cpi->rc.reset_high_source_sad &&
682
0
      (!cpi->oxcf.gf_cbr_boost_pct ||
683
0
       !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)) &&
684
0
      (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
685
0
      cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
686
0
    int qclamp = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
687
0
                       VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
688
    // If the previous frame had overshoot and the current q needs to increase
689
    // above the clamped value, reduce the clamp for faster reaction to
690
    // overshoot.
691
0
    if (cpi->rc.rc_1_frame == -1 && q > qclamp)
692
0
      q = (q + qclamp) >> 1;
693
0
    else
694
0
      q = qclamp;
695
0
  }
696
0
  if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
697
0
      cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
698
0
    vp9_cyclic_refresh_limit_q(cpi, &q);
699
0
  return VPXMAX(VPXMIN(q, cpi->rc.worst_quality), cpi->rc.best_quality);
700
0
}
701
702
100k
static double get_rate_correction_factor(const VP9_COMP *cpi) {
703
100k
  const RATE_CONTROL *const rc = &cpi->rc;
704
100k
  const VP9_COMMON *const cm = &cpi->common;
705
100k
  double rcf;
706
707
100k
  if (frame_is_intra_only(cm)) {
708
20.4k
    rcf = rc->rate_correction_factors[KF_STD];
709
80.3k
  } else if (cpi->oxcf.pass == 2) {
710
0
    RATE_FACTOR_LEVEL rf_lvl =
711
0
        cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
712
0
    rcf = rc->rate_correction_factors[rf_lvl];
713
80.3k
  } else {
714
80.3k
    if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
715
80.3k
        !rc->is_src_frame_alt_ref && !cpi->use_svc &&
716
80.3k
        (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
717
5.48k
      rcf = rc->rate_correction_factors[GF_ARF_STD];
718
74.8k
    else
719
74.8k
      rcf = rc->rate_correction_factors[INTER_NORMAL];
720
80.3k
  }
721
100k
  rcf *= rcf_mult[rc->frame_size_selector];
722
100k
  return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
723
100k
}
724
725
53.0k
static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
726
53.0k
  RATE_CONTROL *const rc = &cpi->rc;
727
53.0k
  const VP9_COMMON *const cm = &cpi->common;
728
729
  // Normalize RCF to account for the size-dependent scaling factor.
730
53.0k
  factor /= rcf_mult[cpi->rc.frame_size_selector];
731
732
53.0k
  factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
733
734
53.0k
  if (frame_is_intra_only(cm)) {
735
12.4k
    rc->rate_correction_factors[KF_STD] = factor;
736
40.6k
  } else if (cpi->oxcf.pass == 2) {
737
0
    RATE_FACTOR_LEVEL rf_lvl =
738
0
        cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
739
0
    rc->rate_correction_factors[rf_lvl] = factor;
740
40.6k
  } else {
741
40.6k
    if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
742
40.6k
        !rc->is_src_frame_alt_ref && !cpi->use_svc &&
743
40.6k
        (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
744
2.76k
      rc->rate_correction_factors[GF_ARF_STD] = factor;
745
37.8k
    else
746
37.8k
      rc->rate_correction_factors[INTER_NORMAL] = factor;
747
40.6k
  }
748
53.0k
}
749
750
53.0k
void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) {
751
53.0k
  const VP9_COMMON *const cm = &cpi->common;
752
53.0k
  int correction_factor = 100;
753
53.0k
  double rate_correction_factor = get_rate_correction_factor(cpi);
754
53.0k
  double adjustment_limit;
755
53.0k
  RATE_FACTOR_LEVEL rf_lvl =
756
53.0k
      cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
757
758
53.0k
  int projected_size_based_on_q = 0;
759
760
  // Do not update the rate factors for arf overlay frames.
761
53.0k
  if (cpi->rc.is_src_frame_alt_ref) return;
762
763
  // Clear down mmx registers to allow floating point in what follows
764
53.0k
  vpx_clear_system_state();
765
766
  // Work out how big we would have expected the frame to be at this Q given
767
  // the current correction factor.
768
  // Stay in double to avoid int overflow when values are large
769
53.0k
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
770
0
    projected_size_based_on_q =
771
0
        vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
772
53.0k
  } else {
773
53.0k
    FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type;
774
53.0k
    projected_size_based_on_q =
775
53.0k
        vp9_estimate_bits_at_q(frame_type, cm->base_qindex, cm->MBs,
776
53.0k
                               rate_correction_factor, cm->bit_depth);
777
53.0k
  }
778
  // Work out a size correction factor.
779
53.0k
  if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
780
42.3k
    correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
781
42.3k
                              projected_size_based_on_q);
782
783
  // Do not use damped adjustment for the first frame of each frame type
784
53.0k
  if (!cpi->rc.damped_adjustment[rf_lvl]) {
785
3.87k
    adjustment_limit = 1.0;
786
3.87k
    cpi->rc.damped_adjustment[rf_lvl] = 1;
787
49.1k
  } else {
788
    // More heavily damped adjustment used if we have been oscillating either
789
    // side of target.
790
49.1k
    adjustment_limit =
791
49.1k
        0.25 + 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
792
49.1k
  }
793
794
53.0k
  cpi->rc.q_2_frame = cpi->rc.q_1_frame;
795
53.0k
  cpi->rc.q_1_frame = cm->base_qindex;
796
53.0k
  cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
797
53.0k
  if (correction_factor > 110)
798
13.9k
    cpi->rc.rc_1_frame = -1;
799
39.0k
  else if (correction_factor < 90)
800
17.4k
    cpi->rc.rc_1_frame = 1;
801
21.6k
  else
802
21.6k
    cpi->rc.rc_1_frame = 0;
803
804
  // Turn off oscilation detection in the case of massive overshoot.
805
53.0k
  if (cpi->rc.rc_1_frame == -1 && cpi->rc.rc_2_frame == 1 &&
806
53.0k
      correction_factor > 1000) {
807
194
    cpi->rc.rc_2_frame = 0;
808
194
  }
809
810
53.0k
  if (correction_factor > 102) {
811
    // We are not already at the worst allowable quality
812
17.5k
    correction_factor =
813
17.5k
        (int)(100 + ((correction_factor - 100) * adjustment_limit));
814
17.5k
    rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
815
    // Keep rate_correction_factor within limits
816
17.5k
    if (rate_correction_factor > MAX_BPB_FACTOR)
817
0
      rate_correction_factor = MAX_BPB_FACTOR;
818
35.4k
  } else if (correction_factor < 99) {
819
    // We are not already at the best allowable quality
820
22.1k
    correction_factor =
821
22.1k
        (int)(100 - ((100 - correction_factor) * adjustment_limit));
822
22.1k
    rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
823
824
    // Keep rate_correction_factor within limits
825
22.1k
    if (rate_correction_factor < MIN_BPB_FACTOR)
826
231
      rate_correction_factor = MIN_BPB_FACTOR;
827
22.1k
  }
828
829
53.0k
  set_rate_correction_factor(cpi, rate_correction_factor);
830
53.0k
}
831
832
int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
833
47.7k
                      int active_best_quality, int active_worst_quality) {
834
47.7k
  const VP9_COMMON *const cm = &cpi->common;
835
47.7k
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
836
47.7k
  int q = active_worst_quality;
837
47.7k
  int last_error = INT_MAX;
838
47.7k
  int i, target_bits_per_mb, bits_per_mb_at_this_q;
839
47.7k
  const double correction_factor = get_rate_correction_factor(cpi);
840
841
  // Calculate required scaling factor based on target frame size and size of
842
  // frame produced using previous Q.
843
47.7k
  target_bits_per_mb =
844
47.7k
      (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs);
845
846
47.7k
  i = active_best_quality;
847
848
1.19M
  do {
849
1.19M
    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cr->apply_cyclic_refresh &&
850
1.19M
        (!cpi->oxcf.gf_cbr_boost_pct || !cpi->refresh_golden_frame)) {
851
0
      bits_per_mb_at_this_q =
852
0
          (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
853
1.19M
    } else {
854
1.19M
      FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type;
855
1.19M
      bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(
856
1.19M
          frame_type, i, correction_factor, cm->bit_depth);
857
1.19M
    }
858
859
1.19M
    if (bits_per_mb_at_this_q <= target_bits_per_mb) {
860
36.5k
      if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
861
33.1k
        q = i;
862
3.41k
      else
863
3.41k
        q = i - 1;
864
865
36.5k
      break;
866
1.15M
    } else {
867
1.15M
      last_error = bits_per_mb_at_this_q - target_bits_per_mb;
868
1.15M
    }
869
1.19M
  } while (++i <= active_worst_quality);
870
871
  // Adjustment to q for CBR mode.
872
47.7k
  if (cpi->oxcf.rc_mode == VPX_CBR) return adjust_q_cbr(cpi, q);
873
874
47.7k
  return q;
875
47.7k
}
876
877
static int get_active_quality(int q, int gfu_boost, int low, int high,
878
10.7k
                              int *low_motion_minq, int *high_motion_minq) {
879
10.7k
  if (gfu_boost > high) {
880
0
    return low_motion_minq[q];
881
10.7k
  } else if (gfu_boost < low) {
882
0
    return high_motion_minq[q];
883
10.7k
  } else {
884
10.7k
    const int gap = high - low;
885
10.7k
    const int offset = high - gfu_boost;
886
10.7k
    const int qdiff = high_motion_minq[q] - low_motion_minq[q];
887
10.7k
    const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
888
10.7k
    return low_motion_minq[q] + adjustment;
889
10.7k
  }
890
10.7k
}
891
892
static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
893
8.06k
                                 vpx_bit_depth_t bit_depth) {
894
8.06k
  int *kf_low_motion_minq;
895
8.06k
  int *kf_high_motion_minq;
896
8.06k
  ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
897
8.06k
  ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
898
8.06k
  return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
899
8.06k
                            kf_low_motion_minq, kf_high_motion_minq);
900
8.06k
}
901
902
static int get_gf_active_quality(const VP9_COMP *const cpi, int q,
903
2.71k
                                 vpx_bit_depth_t bit_depth) {
904
2.71k
  const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
905
2.71k
  const RATE_CONTROL *const rc = &cpi->rc;
906
907
2.71k
  int *arfgf_low_motion_minq;
908
2.71k
  int *arfgf_high_motion_minq;
909
2.71k
  const int gfu_boost = cpi->multi_layer_arf
910
2.71k
                            ? gf_group->gfu_boost[gf_group->index]
911
2.71k
                            : rc->gfu_boost;
912
2.71k
  ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
913
2.71k
  ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
914
2.71k
  return get_active_quality(q, gfu_boost, gf_low, gf_high,
915
2.71k
                            arfgf_low_motion_minq, arfgf_high_motion_minq);
916
2.71k
}
917
918
53.1k
static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
919
53.1k
  const RATE_CONTROL *const rc = &cpi->rc;
920
53.1k
  const unsigned int curr_frame = cpi->common.current_video_frame;
921
53.1k
  int active_worst_quality;
922
923
53.1k
  if (cpi->common.frame_type == KEY_FRAME) {
924
12.4k
    active_worst_quality =
925
12.4k
        curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] << 1;
926
40.6k
  } else {
927
40.6k
    if (!rc->is_src_frame_alt_ref && !cpi->use_svc &&
928
40.6k
        (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
929
2.78k
      active_worst_quality =
930
2.78k
          curr_frame == 1
931
2.78k
              ? rc->last_q[KEY_FRAME] * 5 >> 2
932
2.78k
              : rc->last_q[INTER_FRAME] * rc->fac_active_worst_gf / 100;
933
37.8k
    } else {
934
37.8k
      active_worst_quality = curr_frame == 1
935
37.8k
                                 ? rc->last_q[KEY_FRAME] << 1
936
37.8k
                                 : rc->avg_frame_qindex[INTER_FRAME] *
937
35.6k
                                       rc->fac_active_worst_inter / 100;
938
37.8k
    }
939
40.6k
  }
940
53.1k
  return VPXMIN(active_worst_quality, rc->worst_quality);
941
53.1k
}
942
943
// Adjust active_worst_quality level based on buffer level.
944
0
static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
945
  // Adjust active_worst_quality: If buffer is above the optimal/target level,
946
  // bring active_worst_quality down depending on fullness of buffer.
947
  // If buffer is below the optimal level, let the active_worst_quality go from
948
  // ambient Q (at buffer = optimal level) to worst_quality level
949
  // (at buffer = critical level).
950
0
  const VP9_COMMON *const cm = &cpi->common;
951
0
  const RATE_CONTROL *rc = &cpi->rc;
952
  // Buffer level below which we push active_worst to worst_quality.
953
0
  int64_t critical_level = rc->optimal_buffer_level >> 3;
954
0
  int64_t buff_lvl_step = 0;
955
0
  int adjustment = 0;
956
0
  int active_worst_quality;
957
0
  int ambient_qp;
958
0
  unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
959
0
  if (frame_is_intra_only(cm) || rc->reset_high_source_sad || rc->force_max_q)
960
0
    return rc->worst_quality;
961
  // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
962
  // for the first few frames following key frame. These are both initialized
963
  // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
964
  // So for first few frames following key, the qp of that key frame is weighted
965
  // into the active_worst_quality setting.
966
0
  ambient_qp = (cm->current_video_frame < num_frames_weight_key)
967
0
                   ? VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
968
0
                            rc->avg_frame_qindex[KEY_FRAME])
969
0
                   : rc->avg_frame_qindex[INTER_FRAME];
970
0
  active_worst_quality = VPXMIN(rc->worst_quality, (ambient_qp * 5) >> 2);
971
  // For SVC if the current base spatial layer was key frame, use the QP from
972
  // that base layer for ambient_qp.
973
0
  if (cpi->use_svc && cpi->svc.spatial_layer_id > 0) {
974
0
    int layer = LAYER_IDS_TO_IDX(0, cpi->svc.temporal_layer_id,
975
0
                                 cpi->svc.number_temporal_layers);
976
0
    const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
977
0
    if (lc->is_key_frame) {
978
0
      const RATE_CONTROL *lrc = &lc->rc;
979
0
      ambient_qp = VPXMIN(ambient_qp, lrc->last_q[KEY_FRAME]);
980
0
      active_worst_quality = VPXMIN(rc->worst_quality, (ambient_qp * 9) >> 3);
981
0
    }
982
0
  }
983
0
  if (rc->buffer_level > rc->optimal_buffer_level) {
984
    // Adjust down.
985
    // Maximum limit for down adjustment ~30%; make it lower for screen content.
986
0
    int max_adjustment_down = active_worst_quality / 3;
987
0
    if (cpi->oxcf.content == VP9E_CONTENT_SCREEN)
988
0
      max_adjustment_down = active_worst_quality >> 3;
989
0
    if (max_adjustment_down) {
990
0
      buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
991
0
                       max_adjustment_down);
992
0
      if (buff_lvl_step)
993
0
        adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
994
0
                           buff_lvl_step);
995
0
      active_worst_quality -= adjustment;
996
0
    }
997
0
  } else if (rc->buffer_level > critical_level) {
998
    // Adjust up from ambient Q.
999
0
    if (critical_level) {
1000
0
      buff_lvl_step = (rc->optimal_buffer_level - critical_level);
1001
0
      if (buff_lvl_step) {
1002
0
        adjustment = (int)((rc->worst_quality - ambient_qp) *
1003
0
                           (rc->optimal_buffer_level - rc->buffer_level) /
1004
0
                           buff_lvl_step);
1005
0
      }
1006
0
      active_worst_quality = ambient_qp + adjustment;
1007
0
    }
1008
0
  } else {
1009
    // Set to worst_quality if buffer is below critical level.
1010
0
    active_worst_quality = rc->worst_quality;
1011
0
  }
1012
0
  return active_worst_quality;
1013
0
}
1014
1015
static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
1016
                                             int *bottom_index,
1017
0
                                             int *top_index) {
1018
0
  const VP9_COMMON *const cm = &cpi->common;
1019
0
  const RATE_CONTROL *const rc = &cpi->rc;
1020
0
  int active_best_quality;
1021
0
  int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
1022
0
  int q;
1023
0
  int *rtc_minq;
1024
0
  ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
1025
1026
0
  if (frame_is_intra_only(cm)) {
1027
0
    active_best_quality = rc->best_quality;
1028
    // Handle the special case for key frames forced when we have reached
1029
    // the maximum key frame interval. Here force the Q to a range
1030
    // based on the ambient Q to reduce the risk of popping.
1031
0
    if (rc->this_key_frame_forced) {
1032
0
      int qindex = rc->last_boosted_qindex;
1033
0
      double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1034
0
      int delta_qindex = vp9_compute_qdelta(
1035
0
          rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth);
1036
0
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1037
0
    } else if (cm->current_video_frame > 0) {
1038
      // not first frame of one pass and kf_boost is set
1039
0
      double q_adj_factor = 1.0;
1040
0
      double q_val;
1041
1042
0
      active_best_quality = get_kf_active_quality(
1043
0
          rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
1044
1045
      // Allow somewhat lower kf minq with small image formats.
1046
0
      if ((cm->width * cm->height) <= (352 * 288)) {
1047
0
        q_adj_factor -= 0.25;
1048
0
      }
1049
1050
      // Convert the adjustment factor to a qindex delta
1051
      // on active_best_quality.
1052
0
      q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
1053
0
      active_best_quality +=
1054
0
          vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
1055
0
    }
1056
0
  } else if (!rc->is_src_frame_alt_ref && !cpi->use_svc &&
1057
0
             cpi->oxcf.gf_cbr_boost_pct &&
1058
0
             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1059
    // Use the lower of active_worst_quality and recent
1060
    // average Q as basis for GF/ARF best Q limit unless last frame was
1061
    // a key frame.
1062
0
    if (rc->frames_since_key > 1 &&
1063
0
        rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1064
0
      q = rc->avg_frame_qindex[INTER_FRAME];
1065
0
    } else {
1066
0
      q = active_worst_quality;
1067
0
    }
1068
0
    active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1069
0
  } else {
1070
    // Use the lower of active_worst_quality and recent/average Q.
1071
0
    if (cm->current_video_frame > 1) {
1072
0
      if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
1073
0
        active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
1074
0
      else
1075
0
        active_best_quality = rtc_minq[active_worst_quality];
1076
0
    } else {
1077
0
      if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
1078
0
        active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
1079
0
      else
1080
0
        active_best_quality = rtc_minq[active_worst_quality];
1081
0
    }
1082
0
  }
1083
1084
  // Clip the active best and worst quality values to limits
1085
0
  active_best_quality =
1086
0
      clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1087
0
  active_worst_quality =
1088
0
      clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1089
1090
0
  *top_index = active_worst_quality;
1091
0
  *bottom_index = active_best_quality;
1092
1093
  // Special case code to try and match quality with forced key frames
1094
0
  if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
1095
0
    q = rc->last_boosted_qindex;
1096
0
  } else {
1097
0
    q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1098
0
                          active_worst_quality);
1099
0
    if (q > *top_index) {
1100
      // Special case when we are targeting the max allowed rate
1101
0
      if (rc->this_frame_target >= rc->max_frame_bandwidth)
1102
0
        *top_index = q;
1103
0
      else
1104
0
        q = *top_index;
1105
0
    }
1106
0
  }
1107
1108
0
  assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1109
0
  assert(*bottom_index <= rc->worst_quality &&
1110
0
         *bottom_index >= rc->best_quality);
1111
0
  assert(q <= rc->worst_quality && q >= rc->best_quality);
1112
0
  return q;
1113
0
}
1114
1115
static int get_active_cq_level_one_pass(const RATE_CONTROL *rc,
1116
53.1k
                                        const VP9EncoderConfig *const oxcf) {
1117
53.1k
  static const double cq_adjust_threshold = 0.1;
1118
53.1k
  int active_cq_level = oxcf->cq_level;
1119
53.1k
  if (oxcf->rc_mode == VPX_CQ && rc->total_target_bits > 0) {
1120
0
    const double x = (double)rc->total_actual_bits / rc->total_target_bits;
1121
0
    if (x < cq_adjust_threshold) {
1122
0
      active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
1123
0
    }
1124
0
  }
1125
53.1k
  return active_cq_level;
1126
53.1k
}
1127
1128
0
#define SMOOTH_PCT_MIN 0.1
1129
0
#define SMOOTH_PCT_DIV 0.05
1130
static int get_active_cq_level_two_pass(const TWO_PASS *twopass,
1131
                                        const RATE_CONTROL *rc,
1132
0
                                        const VP9EncoderConfig *const oxcf) {
1133
0
  static const double cq_adjust_threshold = 0.1;
1134
0
  int active_cq_level = oxcf->cq_level;
1135
0
  if (oxcf->rc_mode == VPX_CQ) {
1136
0
    if (twopass->mb_smooth_pct > SMOOTH_PCT_MIN) {
1137
0
      active_cq_level -=
1138
0
          (int)((twopass->mb_smooth_pct - SMOOTH_PCT_MIN) / SMOOTH_PCT_DIV);
1139
0
      active_cq_level = VPXMAX(active_cq_level, 0);
1140
0
    }
1141
0
    if (rc->total_target_bits > 0) {
1142
0
      const double x = (double)rc->total_actual_bits / rc->total_target_bits;
1143
0
      if (x < cq_adjust_threshold) {
1144
0
        active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
1145
0
      }
1146
0
    }
1147
0
  }
1148
0
  return active_cq_level;
1149
0
}
1150
1151
static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
1152
                                             int *bottom_index,
1153
53.1k
                                             int *top_index) {
1154
53.1k
  const VP9_COMMON *const cm = &cpi->common;
1155
53.1k
  const RATE_CONTROL *const rc = &cpi->rc;
1156
53.1k
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1157
53.1k
  const int cq_level = get_active_cq_level_one_pass(rc, oxcf);
1158
53.1k
  int active_best_quality;
1159
53.1k
  int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
1160
53.1k
  int q;
1161
53.1k
  int *inter_minq;
1162
53.1k
  ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
1163
1164
53.1k
  if (frame_is_intra_only(cm)) {
1165
12.4k
    if (oxcf->rc_mode == VPX_Q) {
1166
2.00k
      int qindex = cq_level;
1167
2.00k
      double qstart = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1168
2.00k
      int delta_qindex =
1169
2.00k
          vp9_compute_qdelta(rc, qstart, qstart * 0.25, cm->bit_depth);
1170
2.00k
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1171
10.4k
    } else if (rc->this_key_frame_forced) {
1172
      // Handle the special case for key frames forced when we have reached
1173
      // the maximum key frame interval. Here force the Q to a range
1174
      // based on the ambient Q to reduce the risk of popping.
1175
2.39k
      int qindex = rc->last_boosted_qindex;
1176
2.39k
      double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1177
2.39k
      int delta_qindex = vp9_compute_qdelta(
1178
2.39k
          rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
1179
2.39k
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1180
8.06k
    } else {
1181
      // not first frame of one pass and kf_boost is set
1182
8.06k
      double q_adj_factor = 1.0;
1183
8.06k
      double q_val;
1184
1185
8.06k
      active_best_quality = get_kf_active_quality(
1186
8.06k
          rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
1187
1188
      // Allow somewhat lower kf minq with small image formats.
1189
8.06k
      if ((cm->width * cm->height) <= (352 * 288)) {
1190
7.76k
        q_adj_factor -= 0.25;
1191
7.76k
      }
1192
1193
      // Convert the adjustment factor to a qindex delta
1194
      // on active_best_quality.
1195
8.06k
      q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
1196
8.06k
      active_best_quality +=
1197
8.06k
          vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
1198
8.06k
    }
1199
40.6k
  } else if (!rc->is_src_frame_alt_ref &&
1200
40.6k
             (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1201
    // Use the lower of active_worst_quality and recent
1202
    // average Q as basis for GF/ARF best Q limit unless last frame was
1203
    // a key frame.
1204
2.78k
    if (rc->frames_since_key > 1) {
1205
2.78k
      if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1206
666
        q = rc->avg_frame_qindex[INTER_FRAME];
1207
2.11k
      } else {
1208
2.11k
        q = active_worst_quality;
1209
2.11k
      }
1210
2.78k
    } else {
1211
0
      q = rc->avg_frame_qindex[KEY_FRAME];
1212
0
    }
1213
    // For constrained quality don't allow Q less than the cq level
1214
2.78k
    if (oxcf->rc_mode == VPX_CQ) {
1215
0
      if (q < cq_level) q = cq_level;
1216
1217
0
      active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1218
1219
      // Constrained quality use slightly lower active best.
1220
0
      active_best_quality = active_best_quality * 15 / 16;
1221
1222
2.78k
    } else if (oxcf->rc_mode == VPX_Q) {
1223
72
      int qindex = cq_level;
1224
72
      double qstart = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1225
72
      int delta_qindex;
1226
72
      if (cpi->refresh_alt_ref_frame)
1227
0
        delta_qindex =
1228
0
            vp9_compute_qdelta(rc, qstart, qstart * 0.40, cm->bit_depth);
1229
72
      else
1230
72
        delta_qindex =
1231
72
            vp9_compute_qdelta(rc, qstart, qstart * 0.50, cm->bit_depth);
1232
72
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1233
2.71k
    } else {
1234
2.71k
      active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1235
2.71k
    }
1236
37.8k
  } else {
1237
37.8k
    if (oxcf->rc_mode == VPX_Q) {
1238
874
      int qindex = cq_level;
1239
874
      double qstart = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1240
874
      double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
1241
874
                                               0.70, 1.0, 0.85, 1.0 };
1242
874
      int delta_qindex = vp9_compute_qdelta(
1243
874
          rc, qstart,
1244
874
          qstart * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL],
1245
874
          cm->bit_depth);
1246
874
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1247
37.0k
    } else {
1248
      // Use the min of the average Q and active_worst_quality as basis for
1249
      // active_best.
1250
37.0k
      if (cm->current_video_frame > 1) {
1251
34.8k
        q = VPXMIN(rc->avg_frame_qindex[INTER_FRAME], active_worst_quality);
1252
34.8k
        active_best_quality = inter_minq[q];
1253
34.8k
      } else {
1254
2.17k
        active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
1255
2.17k
      }
1256
      // For the constrained quality mode we don't want
1257
      // q to fall below the cq level.
1258
37.0k
      if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
1259
0
        active_best_quality = cq_level;
1260
0
      }
1261
37.0k
    }
1262
37.8k
  }
1263
1264
  // Clip the active best and worst quality values to limits
1265
53.1k
  active_best_quality =
1266
53.1k
      clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1267
53.1k
  active_worst_quality =
1268
53.1k
      clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1269
1270
53.1k
  *top_index = active_worst_quality;
1271
53.1k
  *bottom_index = active_best_quality;
1272
1273
53.1k
#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
1274
53.1k
  {
1275
53.1k
    int qdelta = 0;
1276
53.1k
    vpx_clear_system_state();
1277
1278
    // Limit Q range for the adaptive loop.
1279
53.1k
    if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
1280
53.1k
        !(cm->current_video_frame == 0)) {
1281
6.14k
      qdelta = vp9_compute_qdelta_by_rate(
1282
6.14k
          &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
1283
46.9k
    } else if (!rc->is_src_frame_alt_ref &&
1284
46.9k
               (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1285
9.10k
      qdelta = vp9_compute_qdelta_by_rate(
1286
9.10k
          &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth);
1287
9.10k
    }
1288
53.1k
    if (rc->high_source_sad && cpi->sf.use_altref_onepass) qdelta = 0;
1289
53.1k
    *top_index = active_worst_quality + qdelta;
1290
53.1k
    *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
1291
53.1k
  }
1292
53.1k
#endif
1293
1294
53.1k
  if (oxcf->rc_mode == VPX_Q) {
1295
2.95k
    q = active_best_quality;
1296
    // Special case code to try and match quality with forced key frames
1297
50.1k
  } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
1298
2.39k
    q = rc->last_boosted_qindex;
1299
47.7k
  } else {
1300
47.7k
    q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1301
47.7k
                          active_worst_quality);
1302
1303
    // For no lookahead: if buffer_level indicates overshoot, then avoid going
1304
    // to very low QP. This reduces overshoot observed in Issue: 376707227.
1305
    // Note the buffer_level is updated for every encoded frame as:
1306
    // buffer_level - starting_buffer_level += (avg_frame_bandwidth -
1307
    // encoded_frame_size). So normalizing this with framerate and #encoded
1308
    // frames (current_video_frame) gives the difference/error between target
1309
    // and encoding bitrate. The additional avg_frame_bandwidth term is to
1310
    // compensate for the pre-encoded buffer update (in
1311
    // vp9_rc_get_one_pass_vbr_params).
1312
47.7k
    const int qp_thresh = 32;
1313
47.7k
    const int64_t bitrate_err =
1314
47.7k
        (int64_t)(cpi->framerate *
1315
47.7k
                  (rc->buffer_level - rc->starting_buffer_level -
1316
47.7k
                   rc->avg_frame_bandwidth) /
1317
47.7k
                  (cm->current_video_frame + 1));
1318
    // Threshold may be tuned, but for now condition this on low QP.
1319
47.7k
    if (cpi->oxcf.lag_in_frames == 0 && bitrate_err / 1000 < -10 &&
1320
47.7k
        qp_thresh < rc->worst_quality &&
1321
47.7k
        (q < qp_thresh || *top_index < qp_thresh)) {
1322
0
      q = qp_thresh;
1323
0
      *top_index = VPXMAX(*top_index, q);
1324
0
    }
1325
1326
47.7k
    if (q > *top_index) {
1327
      // Special case when we are targeting the max allowed rate
1328
3.30k
      if (rc->this_frame_target >= rc->max_frame_bandwidth)
1329
318
        *top_index = q;
1330
2.98k
      else
1331
2.98k
        q = *top_index;
1332
3.30k
    }
1333
47.7k
  }
1334
1335
53.1k
  assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1336
53.1k
  assert(*bottom_index <= rc->worst_quality &&
1337
53.1k
         *bottom_index >= rc->best_quality);
1338
53.1k
  assert(q <= rc->worst_quality && q >= rc->best_quality);
1339
53.1k
  return q;
1340
53.1k
}
1341
1342
0
int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) {
1343
0
  static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
1344
0
    1.00,  // INTER_NORMAL
1345
0
    1.00,  // INTER_HIGH
1346
0
    1.50,  // GF_ARF_LOW
1347
0
    1.75,  // GF_ARF_STD
1348
0
    2.00,  // KF_STD
1349
0
  };
1350
0
  const VP9_COMMON *const cm = &cpi->common;
1351
1352
0
  int qdelta = vp9_compute_qdelta_by_rate(
1353
0
      &cpi->rc, cm->frame_type, q, rate_factor_deltas[rf_level], cm->bit_depth);
1354
0
  return qdelta;
1355
0
}
1356
1357
0
#define STATIC_MOTION_THRESH 95
1358
1359
static void pick_kf_q_bound_two_pass(const VP9_COMP *cpi, int *bottom_index,
1360
0
                                     int *top_index) {
1361
0
  const VP9_COMMON *const cm = &cpi->common;
1362
0
  const RATE_CONTROL *const rc = &cpi->rc;
1363
0
  int active_best_quality;
1364
0
  int active_worst_quality = cpi->twopass.active_worst_quality;
1365
1366
0
  if (rc->this_key_frame_forced) {
1367
    // Handle the special case for key frames forced when we have reached
1368
    // the maximum key frame interval. Here force the Q to a range
1369
    // based on the ambient Q to reduce the risk of popping.
1370
0
    double last_boosted_q;
1371
0
    int delta_qindex;
1372
0
    int qindex;
1373
1374
0
    if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1375
0
      qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1376
0
      active_best_quality = qindex;
1377
0
      last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1378
0
      delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1379
0
                                        last_boosted_q * 1.25, cm->bit_depth);
1380
0
      active_worst_quality =
1381
0
          VPXMIN(qindex + delta_qindex, active_worst_quality);
1382
0
    } else {
1383
0
      qindex = rc->last_boosted_qindex;
1384
0
      last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1385
0
      delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1386
0
                                        last_boosted_q * 0.75, cm->bit_depth);
1387
0
      active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1388
0
    }
1389
0
  } else {
1390
    // Not forced keyframe.
1391
0
    double q_adj_factor = 1.0;
1392
0
    double q_val;
1393
    // Baseline value derived from cpi->active_worst_quality and kf boost.
1394
0
    active_best_quality =
1395
0
        get_kf_active_quality(rc, active_worst_quality, cm->bit_depth);
1396
0
    if (cpi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH) {
1397
0
      active_best_quality /= 4;
1398
0
    }
1399
1400
    // Don't allow the active min to be lossless (q0) unlesss the max q
1401
    // already indicates lossless.
1402
0
    active_best_quality =
1403
0
        VPXMIN(active_worst_quality, VPXMAX(1, active_best_quality));
1404
1405
    // Allow somewhat lower kf minq with small image formats.
1406
0
    if ((cm->width * cm->height) <= (352 * 288)) {
1407
0
      q_adj_factor -= 0.25;
1408
0
    }
1409
1410
    // Make a further adjustment based on the kf zero motion measure.
1411
0
    q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
1412
1413
    // Convert the adjustment factor to a qindex delta
1414
    // on active_best_quality.
1415
0
    q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
1416
0
    active_best_quality +=
1417
0
        vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
1418
0
  }
1419
0
  *top_index = active_worst_quality;
1420
0
  *bottom_index = active_best_quality;
1421
0
}
1422
1423
static int rc_constant_q(const VP9_COMP *cpi, int *bottom_index, int *top_index,
1424
0
                         int gf_group_index) {
1425
0
  const VP9_COMMON *const cm = &cpi->common;
1426
0
  const RATE_CONTROL *const rc = &cpi->rc;
1427
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1428
0
  const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1429
0
  const int is_intra_frame = frame_is_intra_only(cm);
1430
1431
0
  const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf);
1432
1433
0
  int q = cq_level;
1434
0
  int active_best_quality = cq_level;
1435
0
  int active_worst_quality = cq_level;
1436
1437
  // Key frame qp decision
1438
0
  if (is_intra_frame && rc->frames_to_key > 1)
1439
0
    pick_kf_q_bound_two_pass(cpi, &active_best_quality, &active_worst_quality);
1440
1441
  // ARF / GF qp decision
1442
0
  if (!is_intra_frame && !rc->is_src_frame_alt_ref &&
1443
0
      cpi->refresh_alt_ref_frame) {
1444
0
    active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1445
1446
    // Modify best quality for second level arfs. For mode VPX_Q this
1447
    // becomes the baseline frame q.
1448
0
    if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) {
1449
0
      const int layer_depth = gf_group->layer_depth[gf_group_index];
1450
      // linearly fit the frame q depending on the layer depth index from
1451
      // the base layer ARF.
1452
0
      active_best_quality = ((layer_depth - 1) * cq_level +
1453
0
                             active_best_quality + layer_depth / 2) /
1454
0
                            layer_depth;
1455
0
    }
1456
0
  }
1457
1458
0
  q = active_best_quality;
1459
0
  *top_index = active_worst_quality;
1460
0
  *bottom_index = active_best_quality;
1461
0
  return q;
1462
0
}
1463
1464
int vp9_rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, int *bottom_index,
1465
0
                                      int *top_index, int gf_group_index) {
1466
0
  const VP9_COMMON *const cm = &cpi->common;
1467
0
  const RATE_CONTROL *const rc = &cpi->rc;
1468
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1469
0
  const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1470
0
  const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf);
1471
0
  int active_best_quality;
1472
0
  int active_worst_quality = cpi->twopass.active_worst_quality;
1473
0
  int q;
1474
0
  int *inter_minq;
1475
0
  int arf_active_best_quality_hl;
1476
0
  int *arfgf_high_motion_minq, *arfgf_low_motion_minq;
1477
0
  const int boost_frame =
1478
0
      !rc->is_src_frame_alt_ref &&
1479
0
      (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
1480
1481
0
  ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
1482
1483
0
  if (oxcf->rc_mode == VPX_Q)
1484
0
    return rc_constant_q(cpi, bottom_index, top_index, gf_group_index);
1485
1486
0
  if (frame_is_intra_only(cm)) {
1487
0
    pick_kf_q_bound_two_pass(cpi, &active_best_quality, &active_worst_quality);
1488
0
  } else if (boost_frame) {
1489
    // Use the lower of active_worst_quality and recent
1490
    // average Q as basis for GF/ARF best Q limit unless last frame was
1491
    // a key frame.
1492
0
    if (rc->frames_since_key > 1 &&
1493
0
        rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1494
0
      q = rc->avg_frame_qindex[INTER_FRAME];
1495
0
    } else {
1496
0
      q = active_worst_quality;
1497
0
    }
1498
    // For constrained quality don't allow Q less than the cq level
1499
0
    if (oxcf->rc_mode == VPX_CQ) {
1500
0
      if (q < cq_level) q = cq_level;
1501
0
    }
1502
0
    active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth);
1503
0
    arf_active_best_quality_hl = active_best_quality;
1504
1505
0
    if (rc->arf_increase_active_best_quality == 1) {
1506
0
      ASSIGN_MINQ_TABLE(cm->bit_depth, arfgf_high_motion_minq);
1507
0
      arf_active_best_quality_hl = arfgf_high_motion_minq[q];
1508
0
    } else if (rc->arf_increase_active_best_quality == -1) {
1509
0
      ASSIGN_MINQ_TABLE(cm->bit_depth, arfgf_low_motion_minq);
1510
0
      arf_active_best_quality_hl = arfgf_low_motion_minq[q];
1511
0
    }
1512
0
    active_best_quality =
1513
0
        (int)((double)active_best_quality *
1514
0
                  rc->arf_active_best_quality_adjustment_factor +
1515
0
              (double)arf_active_best_quality_hl *
1516
0
                  (1.0 - rc->arf_active_best_quality_adjustment_factor));
1517
1518
    // Modify best quality for second level arfs. For mode VPX_Q this
1519
    // becomes the baseline frame q.
1520
0
    if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) {
1521
0
      const int layer_depth = gf_group->layer_depth[gf_group_index];
1522
      // linearly fit the frame q depending on the layer depth index from
1523
      // the base layer ARF.
1524
0
      active_best_quality =
1525
0
          ((layer_depth - 1) * q + active_best_quality + layer_depth / 2) /
1526
0
          layer_depth;
1527
0
    }
1528
0
  } else {
1529
0
    active_best_quality = inter_minq[active_worst_quality];
1530
1531
    // For the constrained quality mode we don't want
1532
    // q to fall below the cq level.
1533
0
    if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
1534
0
      active_best_quality = cq_level;
1535
0
    }
1536
0
  }
1537
1538
  // Extension to max or min Q if undershoot or overshoot is outside
1539
  // the permitted range.
1540
0
  if (frame_is_intra_only(cm) || boost_frame) {
1541
0
    const int layer_depth = gf_group->layer_depth[gf_group_index];
1542
0
    active_best_quality -=
1543
0
        (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
1544
0
    active_worst_quality += (cpi->twopass.extend_maxq / 2);
1545
1546
0
    if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) {
1547
0
      assert(layer_depth > 1);
1548
0
      active_best_quality =
1549
0
          VPXMAX(active_best_quality,
1550
0
                 cpi->twopass.last_qindex_of_arf_layer[layer_depth - 1]);
1551
0
    }
1552
0
  } else {
1553
0
    const int max_layer_depth = gf_group->max_layer_depth;
1554
0
    assert(max_layer_depth > 0);
1555
1556
0
    active_best_quality -=
1557
0
        (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
1558
0
    active_worst_quality += cpi->twopass.extend_maxq;
1559
1560
    // For normal frames do not allow an active minq lower than the q used for
1561
    // the last boosted frame.
1562
0
    active_best_quality =
1563
0
        VPXMAX(active_best_quality,
1564
0
               cpi->twopass.last_qindex_of_arf_layer[max_layer_depth - 1]);
1565
0
  }
1566
1567
0
#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
1568
0
  vpx_clear_system_state();
1569
  // Static forced key frames Q restrictions dealt with elsewhere.
1570
0
  if (!frame_is_intra_only(cm) || !rc->this_key_frame_forced ||
1571
0
      cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH) {
1572
0
    int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group_index],
1573
0
                                       active_worst_quality);
1574
0
    active_worst_quality =
1575
0
        VPXMAX(active_worst_quality + qdelta, active_best_quality);
1576
0
  }
1577
0
#endif
1578
1579
  // Modify active_best_quality for downscaled normal frames.
1580
0
  if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
1581
0
    int qdelta = vp9_compute_qdelta_by_rate(
1582
0
        rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth);
1583
0
    active_best_quality =
1584
0
        VPXMAX(active_best_quality + qdelta, rc->best_quality);
1585
0
  }
1586
1587
0
  active_best_quality =
1588
0
      clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1589
0
  active_worst_quality =
1590
0
      clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1591
1592
0
  if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
1593
    // If static since last kf use better of last boosted and last kf q.
1594
0
    if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1595
0
      q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1596
0
    } else {
1597
0
      q = rc->last_boosted_qindex;
1598
0
    }
1599
0
  } else if (frame_is_intra_only(cm) && !rc->this_key_frame_forced) {
1600
0
    q = active_best_quality;
1601
0
  } else {
1602
0
    q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1603
0
                          active_worst_quality);
1604
0
    if (q > active_worst_quality) {
1605
      // Special case when we are targeting the max allowed rate.
1606
0
      if (rc->this_frame_target >= rc->max_frame_bandwidth)
1607
0
        active_worst_quality = q;
1608
0
      else
1609
0
        q = active_worst_quality;
1610
0
    }
1611
0
  }
1612
1613
0
  *top_index = active_worst_quality;
1614
0
  *bottom_index = active_best_quality;
1615
1616
0
  assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1617
0
  assert(*bottom_index <= rc->worst_quality &&
1618
0
         *bottom_index >= rc->best_quality);
1619
0
  assert(q <= rc->worst_quality && q >= rc->best_quality);
1620
0
  return q;
1621
0
}
1622
1623
int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, int *bottom_index,
1624
53.1k
                             int *top_index) {
1625
53.1k
  int q;
1626
53.1k
  const int gf_group_index = cpi->twopass.gf_group.index;
1627
53.1k
  if (cpi->oxcf.pass == 0) {
1628
53.1k
    if (cpi->oxcf.rc_mode == VPX_CBR)
1629
0
      q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
1630
53.1k
    else
1631
53.1k
      q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
1632
53.1k
  } else {
1633
0
    q = vp9_rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index,
1634
0
                                          gf_group_index);
1635
0
  }
1636
53.1k
  if (cpi->sf.use_nonrd_pick_mode) {
1637
0
    if (cpi->sf.force_frame_boost == 1) q -= cpi->sf.max_delta_qindex;
1638
1639
0
    if (q < *bottom_index)
1640
0
      *bottom_index = q;
1641
0
    else if (q > *top_index)
1642
0
      *top_index = q;
1643
0
  }
1644
53.1k
  return q;
1645
53.1k
}
1646
1647
0
void vp9_configure_buffer_updates(VP9_COMP *cpi, int gf_group_index) {
1648
0
  VP9_COMMON *cm = &cpi->common;
1649
0
  TWO_PASS *const twopass = &cpi->twopass;
1650
1651
0
  cpi->rc.is_src_frame_alt_ref = 0;
1652
0
  cm->show_existing_frame = 0;
1653
0
  cpi->rc.show_arf_as_gld = 0;
1654
0
  switch (twopass->gf_group.update_type[gf_group_index]) {
1655
0
    case KF_UPDATE:
1656
0
      cpi->refresh_last_frame = 1;
1657
0
      cpi->refresh_golden_frame = 1;
1658
0
      cpi->refresh_alt_ref_frame = 1;
1659
0
      break;
1660
0
    case LF_UPDATE:
1661
0
      cpi->refresh_last_frame = 1;
1662
0
      cpi->refresh_golden_frame = 0;
1663
0
      cpi->refresh_alt_ref_frame = 0;
1664
0
      break;
1665
0
    case GF_UPDATE:
1666
0
      cpi->refresh_last_frame = 1;
1667
0
      cpi->refresh_golden_frame = 1;
1668
0
      cpi->refresh_alt_ref_frame = 0;
1669
0
      break;
1670
0
    case OVERLAY_UPDATE:
1671
0
      cpi->refresh_last_frame = 0;
1672
0
      cpi->refresh_golden_frame = 1;
1673
0
      cpi->refresh_alt_ref_frame = 0;
1674
0
      cpi->rc.is_src_frame_alt_ref = 1;
1675
0
      if (cpi->rc.preserve_arf_as_gld) {
1676
0
        cpi->rc.show_arf_as_gld = 1;
1677
0
        cpi->refresh_golden_frame = 0;
1678
0
        cm->show_existing_frame = 1;
1679
0
        cm->refresh_frame_context = 0;
1680
0
      }
1681
0
      break;
1682
0
    case MID_OVERLAY_UPDATE:
1683
0
      cpi->refresh_last_frame = 1;
1684
0
      cpi->refresh_golden_frame = 0;
1685
0
      cpi->refresh_alt_ref_frame = 0;
1686
0
      cpi->rc.is_src_frame_alt_ref = 1;
1687
0
      break;
1688
0
    case USE_BUF_FRAME:
1689
0
      cpi->refresh_last_frame = 0;
1690
0
      cpi->refresh_golden_frame = 0;
1691
0
      cpi->refresh_alt_ref_frame = 0;
1692
0
      cpi->rc.is_src_frame_alt_ref = 1;
1693
0
      cm->show_existing_frame = 1;
1694
0
      cm->refresh_frame_context = 0;
1695
0
      break;
1696
0
    default:
1697
0
      assert(twopass->gf_group.update_type[gf_group_index] == ARF_UPDATE);
1698
0
      cpi->refresh_last_frame = 0;
1699
0
      cpi->refresh_golden_frame = 0;
1700
0
      cpi->refresh_alt_ref_frame = 1;
1701
0
      break;
1702
0
  }
1703
0
}
1704
1705
void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, int frame_target,
1706
                                      int *frame_under_shoot_limit,
1707
0
                                      int *frame_over_shoot_limit) {
1708
0
  if (cpi->oxcf.rc_mode == VPX_Q) {
1709
0
    *frame_under_shoot_limit = 0;
1710
0
    *frame_over_shoot_limit = INT_MAX;
1711
0
  } else {
1712
    // For very small rate targets where the fractional adjustment
1713
    // may be tiny make sure there is at least a minimum range.
1714
0
    const int tol_low =
1715
0
        (int)(((int64_t)cpi->sf.recode_tolerance_low * frame_target) / 100);
1716
0
    const int tol_high =
1717
0
        (int)(((int64_t)cpi->sf.recode_tolerance_high * frame_target) / 100);
1718
0
    *frame_under_shoot_limit = VPXMAX(frame_target - tol_low - 100, 0);
1719
0
    *frame_over_shoot_limit =
1720
0
        VPXMIN(frame_target + tol_high + 100, cpi->rc.max_frame_bandwidth);
1721
0
  }
1722
0
}
1723
1724
56.1k
void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
1725
56.1k
  const VP9_COMMON *const cm = &cpi->common;
1726
56.1k
  RATE_CONTROL *const rc = &cpi->rc;
1727
1728
56.1k
  rc->this_frame_target = target;
1729
1730
  // Modify frame size target when down-scaling.
1731
56.1k
  if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
1732
56.1k
      rc->frame_size_selector != UNSCALED) {
1733
0
    rc->this_frame_target = (int)(rc->this_frame_target *
1734
0
                                  rate_thresh_mult[rc->frame_size_selector]);
1735
0
  }
1736
1737
  // Target rate per SB64 (including partial SB64s.
1738
56.1k
  const int64_t sb64_target_rate =
1739
56.1k
      ((int64_t)rc->this_frame_target * 64 * 64) / (cm->width * cm->height);
1740
56.1k
  rc->sb64_target_rate = (int)VPXMIN(sb64_target_rate, INT_MAX);
1741
56.1k
}
1742
1743
0
static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
1744
  // this frame refreshes means next frames don't unless specified by user
1745
0
  RATE_CONTROL *const rc = &cpi->rc;
1746
0
  rc->frames_since_golden = 0;
1747
1748
  // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1749
0
  rc->source_alt_ref_pending = 0;
1750
1751
  // Set the alternate reference frame active flag
1752
0
  rc->source_alt_ref_active = 1;
1753
0
}
1754
1755
53.0k
static void update_golden_frame_stats(VP9_COMP *cpi) {
1756
53.0k
  RATE_CONTROL *const rc = &cpi->rc;
1757
1758
  // Update the Golden frame usage counts.
1759
53.0k
  if (cpi->refresh_golden_frame) {
1760
    // this frame refreshes means next frames don't unless specified by user
1761
15.1k
    rc->frames_since_golden = 0;
1762
1763
    // If we are not using alt ref in the up and coming group clear the arf
1764
    // active flag. In multi arf group case, if the index is not 0 then
1765
    // we are overlaying a mid group arf so should not reset the flag.
1766
15.1k
    if (cpi->oxcf.pass == 2) {
1767
0
      if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1768
0
        rc->source_alt_ref_active = 0;
1769
15.1k
    } else if (!rc->source_alt_ref_pending) {
1770
15.1k
      rc->source_alt_ref_active = 0;
1771
15.1k
    }
1772
1773
    // Decrement count down till next gf
1774
15.1k
    if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1775
1776
37.8k
  } else if (!cpi->refresh_alt_ref_frame) {
1777
    // Decrement count down till next gf
1778
37.8k
    if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1779
1780
37.8k
    rc->frames_since_golden++;
1781
1782
37.8k
    if (rc->show_arf_as_gld) {
1783
0
      rc->frames_since_golden = 0;
1784
      // If we are not using alt ref in the up and coming group clear the arf
1785
      // active flag. In multi arf group case, if the index is not 0 then
1786
      // we are overlaying a mid group arf so should not reset the flag.
1787
0
      if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1788
0
        rc->source_alt_ref_active = 0;
1789
0
    }
1790
37.8k
  }
1791
53.0k
}
1792
1793
0
static void update_altref_usage(VP9_COMP *const cpi) {
1794
0
  VP9_COMMON *const cm = &cpi->common;
1795
0
  int sum_ref_frame_usage = 0;
1796
0
  int arf_frame_usage = 0;
1797
0
  int mi_row, mi_col;
1798
0
  if (cpi->rc.alt_ref_gf_group && !cpi->rc.is_src_frame_alt_ref &&
1799
0
      !cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame)
1800
0
    for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8) {
1801
0
      for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8) {
1802
0
        int sboffset = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
1803
0
        sum_ref_frame_usage += cpi->count_arf_frame_usage[sboffset] +
1804
0
                               cpi->count_lastgolden_frame_usage[sboffset];
1805
0
        arf_frame_usage += cpi->count_arf_frame_usage[sboffset];
1806
0
      }
1807
0
    }
1808
0
  if (sum_ref_frame_usage > 0) {
1809
0
    double altref_count = 100.0 * arf_frame_usage / sum_ref_frame_usage;
1810
0
    cpi->rc.perc_arf_usage =
1811
0
        0.75 * cpi->rc.perc_arf_usage + 0.25 * altref_count;
1812
0
  }
1813
0
}
1814
1815
40.6k
void vp9_compute_frame_low_motion(VP9_COMP *const cpi) {
1816
40.6k
  VP9_COMMON *const cm = &cpi->common;
1817
40.6k
  SVC *const svc = &cpi->svc;
1818
40.6k
  int mi_row, mi_col;
1819
40.6k
  MODE_INFO **mi = cm->mi_grid_visible;
1820
40.6k
  RATE_CONTROL *const rc = &cpi->rc;
1821
40.6k
  const int rows = cm->mi_rows, cols = cm->mi_cols;
1822
40.6k
  int cnt_zeromv = 0;
1823
290k
  for (mi_row = 0; mi_row < rows; mi_row++) {
1824
2.42M
    for (mi_col = 0; mi_col < cols; mi_col++) {
1825
2.17M
      if (mi[0]->ref_frame[0] == LAST_FRAME &&
1826
2.17M
          abs(mi[0]->mv[0].as_mv.row) < 16 && abs(mi[0]->mv[0].as_mv.col) < 16)
1827
174k
        cnt_zeromv++;
1828
2.17M
      mi++;
1829
2.17M
    }
1830
249k
    mi += 8;
1831
249k
  }
1832
40.6k
  cnt_zeromv = 100 * cnt_zeromv / (rows * cols);
1833
40.6k
  rc->avg_frame_low_motion = (3 * rc->avg_frame_low_motion + cnt_zeromv) >> 2;
1834
1835
  // For SVC: set avg_frame_low_motion (only computed on top spatial layer)
1836
  // to all lower spatial layers.
1837
40.6k
  if (cpi->use_svc && svc->spatial_layer_id == svc->number_spatial_layers - 1) {
1838
0
    int i;
1839
0
    for (i = 0; i < svc->number_spatial_layers - 1; ++i) {
1840
0
      const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
1841
0
                                         svc->number_temporal_layers);
1842
0
      LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1843
0
      RATE_CONTROL *const lrc = &lc->rc;
1844
0
      lrc->avg_frame_low_motion = rc->avg_frame_low_motion;
1845
0
    }
1846
0
  }
1847
40.6k
}
1848
1849
53.0k
void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
1850
53.0k
  const VP9_COMMON *const cm = &cpi->common;
1851
53.0k
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1852
53.0k
  RATE_CONTROL *const rc = &cpi->rc;
1853
53.0k
  SVC *const svc = &cpi->svc;
1854
53.0k
  const int qindex = cm->base_qindex;
1855
53.0k
  const GF_GROUP *gf_group = &cpi->twopass.gf_group;
1856
53.0k
  const int gf_group_index = cpi->twopass.gf_group.index;
1857
53.0k
  const int layer_depth = gf_group->layer_depth[gf_group_index];
1858
1859
  // Update rate control heuristics
1860
53.0k
  rc->projected_frame_size = (int)(bytes_used << 3);
1861
1862
  // Post encode loop adjustment of Q prediction.
1863
53.0k
  vp9_rc_update_rate_correction_factors(cpi);
1864
1865
  // Keep a record of last Q and ambient average Q.
1866
53.0k
  if (frame_is_intra_only(cm)) {
1867
12.4k
    rc->last_q[KEY_FRAME] = qindex;
1868
12.4k
    rc->avg_frame_qindex[KEY_FRAME] =
1869
12.4k
        ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1870
12.4k
    if (cpi->use_svc) {
1871
0
      int i;
1872
0
      for (i = 0; i < svc->number_temporal_layers; ++i) {
1873
0
        const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
1874
0
                                           svc->number_temporal_layers);
1875
0
        LAYER_CONTEXT *lc = &svc->layer_context[layer];
1876
0
        RATE_CONTROL *lrc = &lc->rc;
1877
0
        lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME];
1878
0
        lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME];
1879
0
      }
1880
0
    }
1881
40.6k
  } else {
1882
40.6k
    if ((cpi->use_svc) ||
1883
40.6k
        (!rc->is_src_frame_alt_ref &&
1884
40.6k
         !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1885
37.8k
      rc->last_q[INTER_FRAME] = qindex;
1886
37.8k
      rc->avg_frame_qindex[INTER_FRAME] =
1887
37.8k
          ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1888
37.8k
      rc->ni_frames++;
1889
37.8k
      rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1890
37.8k
      rc->avg_q = rc->tot_q / rc->ni_frames;
1891
      // Calculate the average Q for normal inter frames (not key or GFU
1892
      // frames).
1893
37.8k
      rc->ni_tot_qi += qindex;
1894
37.8k
      rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1895
37.8k
    }
1896
40.6k
  }
1897
1898
53.0k
  if (cpi->use_svc) vp9_svc_adjust_avg_frame_qindex(cpi);
1899
1900
  // Keep record of last boosted (KF/KF/ARF) Q value.
1901
  // If the current frame is coded at a lower Q then we also update it.
1902
  // If all mbs in this group are skipped only update if the Q value is
1903
  // better than that already stored.
1904
  // This is used to help set quality in forced key frames to reduce popping
1905
53.0k
  if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) ||
1906
53.0k
      (!rc->constrained_gf_group &&
1907
38.8k
       (cpi->refresh_alt_ref_frame ||
1908
34.5k
        (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1909
15.9k
    rc->last_boosted_qindex = qindex;
1910
15.9k
  }
1911
1912
53.0k
  if ((qindex < cpi->twopass.last_qindex_of_arf_layer[layer_depth]) ||
1913
53.0k
      (cm->frame_type == KEY_FRAME) ||
1914
53.0k
      (!rc->constrained_gf_group &&
1915
38.8k
       (cpi->refresh_alt_ref_frame ||
1916
34.5k
        (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1917
15.9k
    cpi->twopass.last_qindex_of_arf_layer[layer_depth] = qindex;
1918
15.9k
  }
1919
1920
53.0k
  if (frame_is_intra_only(cm)) rc->last_kf_qindex = qindex;
1921
1922
53.0k
  update_buffer_level_postencode(cpi, rc->projected_frame_size);
1923
1924
  // Rolling monitors of whether we are over or underspending used to help
1925
  // regulate min and Max Q in two pass.
1926
53.0k
  if (!frame_is_intra_only(cm)) {
1927
40.6k
    rc->rolling_target_bits = (int)ROUND64_POWER_OF_TWO(
1928
40.6k
        (int64_t)rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1929
40.6k
    rc->rolling_actual_bits = (int)ROUND64_POWER_OF_TWO(
1930
40.6k
        (int64_t)rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1931
40.6k
    rc->long_rolling_target_bits = (int)ROUND64_POWER_OF_TWO(
1932
40.6k
        (int64_t)rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1933
40.6k
    rc->long_rolling_actual_bits = (int)ROUND64_POWER_OF_TWO(
1934
40.6k
        (int64_t)rc->long_rolling_actual_bits * 31 + rc->projected_frame_size,
1935
40.6k
        5);
1936
40.6k
  }
1937
1938
  // Actual bits spent
1939
53.0k
  rc->total_actual_bits += rc->projected_frame_size;
1940
53.0k
  rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1941
1942
53.0k
  rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1943
1944
53.0k
  if (!cpi->use_svc) {
1945
53.0k
    if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1946
53.0k
        (!frame_is_intra_only(cm)))
1947
      // Update the alternate reference frame stats as appropriate.
1948
0
      update_alt_ref_frame_stats(cpi);
1949
53.0k
    else
1950
      // Update the Golden frame stats as appropriate.
1951
53.0k
      update_golden_frame_stats(cpi);
1952
53.0k
  }
1953
1954
  // If second (long term) temporal reference is used for SVC,
1955
  // update the golden frame counter, only for base temporal layer.
1956
53.0k
  if (cpi->use_svc && svc->use_gf_temporal_ref_current_layer &&
1957
53.0k
      svc->temporal_layer_id == 0) {
1958
0
    int i = 0;
1959
0
    if (cpi->refresh_golden_frame)
1960
0
      rc->frames_since_golden = 0;
1961
0
    else
1962
0
      rc->frames_since_golden++;
1963
    // Decrement count down till next gf
1964
0
    if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1965
    // Update the frames_since_golden for all upper temporal layers.
1966
0
    for (i = 1; i < svc->number_temporal_layers; ++i) {
1967
0
      const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
1968
0
                                         svc->number_temporal_layers);
1969
0
      LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1970
0
      RATE_CONTROL *const lrc = &lc->rc;
1971
0
      lrc->frames_since_golden = rc->frames_since_golden;
1972
0
    }
1973
0
  }
1974
1975
53.0k
  if (frame_is_intra_only(cm)) rc->frames_since_key = 0;
1976
53.0k
  if (cm->show_frame) {
1977
53.0k
    rc->frames_since_key++;
1978
53.0k
    rc->frames_to_key--;
1979
53.0k
  }
1980
1981
  // Trigger the resizing of the next frame if it is scaled.
1982
53.0k
  if (oxcf->pass != 0) {
1983
0
    cpi->resize_pending =
1984
0
        rc->next_frame_size_selector != rc->frame_size_selector;
1985
0
    rc->frame_size_selector = rc->next_frame_size_selector;
1986
0
  }
1987
1988
53.0k
  if (oxcf->pass == 0) {
1989
53.0k
    if (!frame_is_intra_only(cm))
1990
40.6k
      if (cpi->sf.use_altref_onepass) update_altref_usage(cpi);
1991
53.0k
    cpi->rc.last_frame_is_src_altref = cpi->rc.is_src_frame_alt_ref;
1992
53.0k
  }
1993
1994
53.0k
  if (!frame_is_intra_only(cm)) rc->reset_high_source_sad = 0;
1995
1996
53.0k
  rc->last_avg_frame_bandwidth = rc->avg_frame_bandwidth;
1997
53.0k
  if (cpi->use_svc && svc->spatial_layer_id < svc->number_spatial_layers - 1)
1998
0
    svc->lower_layer_qindex = cm->base_qindex;
1999
53.0k
  cpi->deadline_mode_previous_frame = cpi->oxcf.mode;
2000
53.0k
}
2001
2002
0
void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
2003
0
  cpi->common.current_video_frame++;
2004
0
  cpi->rc.frames_since_key++;
2005
0
  cpi->rc.frames_to_key--;
2006
0
  cpi->rc.rc_2_frame = 0;
2007
0
  cpi->rc.rc_1_frame = 0;
2008
0
  cpi->rc.last_avg_frame_bandwidth = cpi->rc.avg_frame_bandwidth;
2009
0
  cpi->rc.last_q[INTER_FRAME] = cpi->common.base_qindex;
2010
  // For SVC on dropped frame when framedrop_mode != LAYER_DROP:
2011
  // in this mode the whole superframe may be dropped if only a single layer
2012
  // has buffer underflow (below threshold). Since this can then lead to
2013
  // increasing buffer levels/overflow for certain layers even though whole
2014
  // superframe is dropped, we cap buffer level if its already stable.
2015
0
  if (cpi->use_svc && cpi->svc.framedrop_mode != LAYER_DROP &&
2016
0
      cpi->rc.buffer_level > cpi->rc.optimal_buffer_level) {
2017
0
    cpi->rc.buffer_level = cpi->rc.optimal_buffer_level;
2018
0
    cpi->rc.bits_off_target = cpi->rc.optimal_buffer_level;
2019
0
  }
2020
0
  cpi->deadline_mode_previous_frame = cpi->oxcf.mode;
2021
0
}
2022
2023
43.6k
int vp9_calc_pframe_target_size_one_pass_vbr(const VP9_COMP *cpi) {
2024
43.6k
  const RATE_CONTROL *const rc = &cpi->rc;
2025
43.6k
  const int af_ratio = rc->af_ratio_onepass_vbr;
2026
43.6k
  int64_t target =
2027
43.6k
      (!rc->is_src_frame_alt_ref &&
2028
43.6k
       (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
2029
43.6k
          ? ((int64_t)rc->avg_frame_bandwidth * rc->baseline_gf_interval *
2030
5.80k
             af_ratio) /
2031
5.80k
                (rc->baseline_gf_interval + af_ratio - 1)
2032
43.6k
          : ((int64_t)rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
2033
37.8k
                (rc->baseline_gf_interval + af_ratio - 1);
2034
  // For SVC: refresh flags are used to define the pattern, so we can't
2035
  // use that for boosting the target size here.
2036
  // TODO(marpan): Consider adding internal boost on TL0 for VBR-SVC.
2037
  // For now just use the CBR logic for setting target size.
2038
43.6k
  if (cpi->use_svc) target = vp9_calc_pframe_target_size_one_pass_cbr(cpi);
2039
43.6k
  if (target > INT_MAX) target = INT_MAX;
2040
43.6k
  return vp9_rc_clamp_pframe_target_size(cpi, (int)target);
2041
43.6k
}
2042
2043
12.4k
int vp9_calc_iframe_target_size_one_pass_vbr(const VP9_COMP *cpi) {
2044
12.4k
  static const int kf_ratio = 25;
2045
12.4k
  const RATE_CONTROL *rc = &cpi->rc;
2046
12.4k
  int target = rc->avg_frame_bandwidth;
2047
12.4k
  if (target > INT_MAX / kf_ratio)
2048
49
    target = INT_MAX;
2049
12.4k
  else
2050
12.4k
    target = rc->avg_frame_bandwidth * kf_ratio;
2051
12.4k
  return vp9_rc_clamp_iframe_target_size(cpi, target);
2052
12.4k
}
2053
2054
18.2k
static void adjust_gfint_frame_constraint(VP9_COMP *cpi, int frame_constraint) {
2055
18.2k
  RATE_CONTROL *const rc = &cpi->rc;
2056
18.2k
  rc->constrained_gf_group = 0;
2057
  // Reset gf interval to make more equal spacing for frame_constraint.
2058
18.2k
  if ((frame_constraint <= 7 * rc->baseline_gf_interval >> 2) &&
2059
18.2k
      (frame_constraint > rc->baseline_gf_interval)) {
2060
202
    rc->baseline_gf_interval = frame_constraint >> 1;
2061
202
    if (rc->baseline_gf_interval < 5)
2062
51
      rc->baseline_gf_interval = frame_constraint;
2063
202
    rc->constrained_gf_group = 1;
2064
18.0k
  } else {
2065
    // Reset to keep gf_interval <= frame_constraint.
2066
18.0k
    if (rc->baseline_gf_interval > frame_constraint) {
2067
9.93k
      rc->baseline_gf_interval = frame_constraint;
2068
9.93k
      rc->constrained_gf_group = 1;
2069
9.93k
    }
2070
18.0k
  }
2071
18.2k
}
2072
2073
53.1k
void vp9_set_gf_update_one_pass_vbr(VP9_COMP *const cpi) {
2074
53.1k
  RATE_CONTROL *const rc = &cpi->rc;
2075
53.1k
  VP9_COMMON *const cm = &cpi->common;
2076
53.1k
  if (rc->frames_till_gf_update_due == 0) {
2077
15.2k
    double rate_err = 1.0;
2078
15.2k
    rc->gfu_boost = DEFAULT_GF_BOOST;
2079
15.2k
    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0) {
2080
0
      vp9_cyclic_refresh_set_golden_update(cpi);
2081
15.2k
    } else {
2082
15.2k
      rc->baseline_gf_interval = VPXMIN(
2083
15.2k
          20, VPXMAX(10, (rc->min_gf_interval + rc->max_gf_interval) / 2));
2084
15.2k
    }
2085
15.2k
    rc->af_ratio_onepass_vbr = 10;
2086
15.2k
    if (rc->rolling_target_bits > 0)
2087
11.5k
      rate_err =
2088
11.5k
          (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
2089
15.2k
    if (cm->current_video_frame > 30) {
2090
5.68k
      if (rc->avg_frame_qindex[INTER_FRAME] > (7 * rc->worst_quality) >> 3 &&
2091
5.68k
          rate_err > 3.5) {
2092
129
        rc->baseline_gf_interval =
2093
129
            VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
2094
5.56k
      } else if (rc->avg_frame_low_motion > 0 &&
2095
5.56k
                 rc->avg_frame_low_motion < 20) {
2096
        // Decrease gf interval for high motion case.
2097
804
        rc->baseline_gf_interval = VPXMAX(6, rc->baseline_gf_interval >> 1);
2098
804
      }
2099
      // Adjust boost and af_ratio based on avg_frame_low_motion, which
2100
      // varies between 0 and 100 (stationary, 100% zero/small motion).
2101
5.68k
      if (rc->avg_frame_low_motion > 0)
2102
1.37k
        rc->gfu_boost =
2103
1.37k
            VPXMAX(500, DEFAULT_GF_BOOST * (rc->avg_frame_low_motion << 1) /
2104
5.68k
                            (rc->avg_frame_low_motion + 100));
2105
4.31k
      else if (rc->avg_frame_low_motion == 0 && rate_err > 1.0)
2106
380
        rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
2107
5.68k
      rc->af_ratio_onepass_vbr = VPXMIN(15, VPXMAX(5, 3 * rc->gfu_boost / 400));
2108
5.68k
    }
2109
15.2k
    if (rc->constrain_gf_key_freq_onepass_vbr)
2110
15.2k
      adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
2111
15.2k
    rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2112
15.2k
    cpi->refresh_golden_frame = 1;
2113
15.2k
    rc->source_alt_ref_pending = 0;
2114
15.2k
    rc->alt_ref_gf_group = 0;
2115
15.2k
    if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
2116
0
      rc->source_alt_ref_pending = 1;
2117
0
      rc->alt_ref_gf_group = 1;
2118
0
    }
2119
15.2k
  }
2120
53.1k
}
2121
2122
53.1k
void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
2123
53.1k
  VP9_COMMON *const cm = &cpi->common;
2124
53.1k
  RATE_CONTROL *const rc = &cpi->rc;
2125
53.1k
  int target;
2126
53.1k
  if (!cpi->refresh_alt_ref_frame &&
2127
53.1k
      (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
2128
53.1k
       rc->frames_to_key == 0 ||
2129
53.1k
       (cpi->oxcf.mode != cpi->deadline_mode_previous_frame))) {
2130
12.4k
    cm->frame_type = KEY_FRAME;
2131
12.4k
    rc->this_key_frame_forced =
2132
12.4k
        cm->current_video_frame != 0 && rc->frames_to_key == 0;
2133
12.4k
    rc->frames_to_key = cpi->oxcf.key_freq;
2134
12.4k
    rc->kf_boost = DEFAULT_KF_BOOST;
2135
12.4k
    rc->source_alt_ref_active = 0;
2136
40.6k
  } else {
2137
40.6k
    cm->frame_type = INTER_FRAME;
2138
40.6k
  }
2139
53.1k
  vp9_set_gf_update_one_pass_vbr(cpi);
2140
53.1k
  if (cm->frame_type == KEY_FRAME)
2141
12.4k
    target = vp9_calc_iframe_target_size_one_pass_vbr(cpi);
2142
40.6k
  else
2143
40.6k
    target = vp9_calc_pframe_target_size_one_pass_vbr(cpi);
2144
53.1k
  vp9_rc_set_frame_target(cpi, target);
2145
53.1k
  if (cm->show_frame) vp9_update_buffer_level_preencode(cpi);
2146
53.1k
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0)
2147
0
    vp9_cyclic_refresh_update_parameters(cpi);
2148
53.1k
}
2149
2150
0
int vp9_calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
2151
0
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
2152
0
  const RATE_CONTROL *rc = &cpi->rc;
2153
0
  const SVC *const svc = &cpi->svc;
2154
0
  const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
2155
0
  const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
2156
0
  int min_frame_target =
2157
0
      VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
2158
0
  int64_t target;
2159
2160
0
  if (oxcf->gf_cbr_boost_pct) {
2161
0
    const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
2162
0
    target = cpi->refresh_golden_frame
2163
0
                 ? ((int64_t)rc->avg_frame_bandwidth *
2164
0
                    rc->baseline_gf_interval * af_ratio_pct) /
2165
0
                       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
2166
0
                 : ((int64_t)rc->avg_frame_bandwidth *
2167
0
                    rc->baseline_gf_interval * 100) /
2168
0
                       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
2169
0
  } else {
2170
0
    target = rc->avg_frame_bandwidth;
2171
0
  }
2172
0
  if (is_one_pass_svc(cpi)) {
2173
    // Note that for layers, avg_frame_bandwidth is the cumulative
2174
    // per-frame-bandwidth. For the target size of this frame, use the
2175
    // layer average frame size (i.e., non-cumulative per-frame-bw).
2176
0
    int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2177
0
                                 svc->number_temporal_layers);
2178
0
    const LAYER_CONTEXT *lc = &svc->layer_context[layer];
2179
0
    target = lc->avg_frame_size;
2180
0
    min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
2181
0
  }
2182
0
  if (diff > 0) {
2183
    // Lower the target bandwidth for this frame.
2184
0
    const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
2185
0
    target -= (target * pct_low) / 200;
2186
0
  } else if (diff < 0) {
2187
    // Increase the target bandwidth for this frame.
2188
0
    const int pct_high =
2189
0
        (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
2190
0
    target += (target * pct_high) / 200;
2191
0
  }
2192
0
  if (oxcf->rc_max_inter_bitrate_pct) {
2193
0
    const int64_t max_rate =
2194
0
        (int64_t)rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
2195
0
    target = VPXMIN(target, max_rate);
2196
0
  }
2197
0
  if (target > INT_MAX) target = INT_MAX;
2198
0
  return VPXMAX(min_frame_target, (int)target);
2199
0
}
2200
2201
0
int vp9_calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
2202
0
  const RATE_CONTROL *rc = &cpi->rc;
2203
0
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
2204
0
  const SVC *const svc = &cpi->svc;
2205
0
  int64_t target;
2206
0
  if (cpi->common.current_video_frame == 0) {
2207
0
    target = rc->starting_buffer_level / 2;
2208
0
  } else {
2209
0
    int kf_boost = 32;
2210
0
    double framerate = cpi->framerate;
2211
0
    if (svc->number_temporal_layers > 1 && oxcf->rc_mode == VPX_CBR) {
2212
      // Use the layer framerate for temporal layers CBR mode.
2213
0
      const int layer =
2214
0
          LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2215
0
                           svc->number_temporal_layers);
2216
0
      const LAYER_CONTEXT *lc = &svc->layer_context[layer];
2217
0
      framerate = lc->framerate;
2218
0
    }
2219
0
    kf_boost = VPXMAX(kf_boost, (int)round(2 * framerate - 16));
2220
0
    if (rc->frames_since_key < framerate / 2) {
2221
0
      kf_boost = (int)round(kf_boost * rc->frames_since_key / (framerate / 2));
2222
0
    }
2223
2224
0
    target = ((int64_t)(16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
2225
0
  }
2226
0
  target = VPXMIN(INT_MAX, target);
2227
0
  return vp9_rc_clamp_iframe_target_size(cpi, (int)target);
2228
0
}
2229
2230
0
static void set_intra_only_frame(VP9_COMP *cpi) {
2231
0
  VP9_COMMON *const cm = &cpi->common;
2232
0
  SVC *const svc = &cpi->svc;
2233
  // Don't allow intra_only frame for bypass/flexible SVC mode, or if number
2234
  // of spatial layers is 1 or if number of spatial or temporal layers > 3.
2235
  // Also if intra-only is inserted on very first frame, don't allow if
2236
  // if number of temporal layers > 1. This is because on intra-only frame
2237
  // only 3 reference buffers can be updated, but for temporal layers > 1
2238
  // we generally need to use buffer slots 4 and 5.
2239
0
  if ((cm->current_video_frame == 0 && svc->number_temporal_layers > 1) ||
2240
0
      svc->number_spatial_layers > 3 || svc->number_temporal_layers > 3 ||
2241
0
      svc->number_spatial_layers == 1)
2242
0
    return;
2243
0
  cm->show_frame = 0;
2244
0
  cm->intra_only = 1;
2245
0
  cm->frame_type = INTER_FRAME;
2246
0
  cpi->ext_refresh_frame_flags_pending = 1;
2247
0
  cpi->ext_refresh_last_frame = 1;
2248
0
  cpi->ext_refresh_golden_frame = 1;
2249
0
  cpi->ext_refresh_alt_ref_frame = 1;
2250
0
  if (cm->current_video_frame == 0) {
2251
0
    cpi->lst_fb_idx = 0;
2252
0
    cpi->gld_fb_idx = 1;
2253
0
    cpi->alt_fb_idx = 2;
2254
0
  } else {
2255
0
    int i;
2256
0
    int count = 0;
2257
0
    cpi->lst_fb_idx = -1;
2258
0
    cpi->gld_fb_idx = -1;
2259
0
    cpi->alt_fb_idx = -1;
2260
0
    svc->update_buffer_slot[0] = 0;
2261
    // For intra-only frame we need to refresh all slots that were
2262
    // being used for the base layer (fb_idx_base[i] == 1).
2263
    // Start with assigning last first, then golden and then alt.
2264
0
    for (i = 0; i < REF_FRAMES; ++i) {
2265
0
      if (svc->fb_idx_base[i] == 1) {
2266
0
        svc->update_buffer_slot[0] |= 1 << i;
2267
0
        count++;
2268
0
      }
2269
0
      if (count == 1 && cpi->lst_fb_idx == -1) cpi->lst_fb_idx = i;
2270
0
      if (count == 2 && cpi->gld_fb_idx == -1) cpi->gld_fb_idx = i;
2271
0
      if (count == 3 && cpi->alt_fb_idx == -1) cpi->alt_fb_idx = i;
2272
0
    }
2273
    // If golden or alt is not being used for base layer, then set them
2274
    // to the lst_fb_idx.
2275
0
    if (cpi->gld_fb_idx == -1) cpi->gld_fb_idx = cpi->lst_fb_idx;
2276
0
    if (cpi->alt_fb_idx == -1) cpi->alt_fb_idx = cpi->lst_fb_idx;
2277
0
    if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
2278
0
      cpi->ext_refresh_last_frame = 0;
2279
0
      cpi->ext_refresh_golden_frame = 0;
2280
0
      cpi->ext_refresh_alt_ref_frame = 0;
2281
0
      cpi->ref_frame_flags = 0;
2282
0
    }
2283
0
  }
2284
0
}
2285
2286
0
void vp9_rc_get_svc_params(VP9_COMP *cpi) {
2287
0
  VP9_COMMON *const cm = &cpi->common;
2288
0
  RATE_CONTROL *const rc = &cpi->rc;
2289
0
  SVC *const svc = &cpi->svc;
2290
0
  int target = rc->avg_frame_bandwidth;
2291
0
  int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2292
0
                               svc->number_temporal_layers);
2293
0
  if (svc->first_spatial_layer_to_encode)
2294
0
    svc->layer_context[svc->temporal_layer_id].is_key_frame = 0;
2295
  // Periodic key frames is based on the super-frame counter
2296
  // (svc.current_superframe), also only base spatial layer is key frame.
2297
  // Key frame is set for any of the following: very first frame, frame flags
2298
  // indicates key, superframe counter hits key frequency,(non-intra) sync
2299
  // flag is set for spatial layer 0, or deadline mode changes.
2300
0
  if ((cm->current_video_frame == 0 && !svc->previous_frame_is_intra_only) ||
2301
0
      (cpi->frame_flags & FRAMEFLAGS_KEY) ||
2302
0
      (cpi->oxcf.auto_key &&
2303
0
       (svc->current_superframe % cpi->oxcf.key_freq == 0) &&
2304
0
       !svc->previous_frame_is_intra_only && svc->spatial_layer_id == 0) ||
2305
0
      (svc->spatial_layer_sync[0] == 1 && svc->spatial_layer_id == 0) ||
2306
0
      (cpi->oxcf.mode != cpi->deadline_mode_previous_frame)) {
2307
0
    cm->frame_type = KEY_FRAME;
2308
0
    rc->source_alt_ref_active = 0;
2309
0
    if (is_one_pass_svc(cpi)) {
2310
0
      if (cm->current_video_frame > 0) vp9_svc_reset_temporal_layers(cpi, 1);
2311
0
      layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
2312
0
                               svc->number_temporal_layers);
2313
0
      svc->layer_context[layer].is_key_frame = 1;
2314
0
      cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2315
      // Assumption here is that LAST_FRAME is being updated for a keyframe.
2316
      // Thus no change in update flags.
2317
0
      if (cpi->oxcf.rc_mode == VPX_CBR)
2318
0
        target = vp9_calc_iframe_target_size_one_pass_cbr(cpi);
2319
0
      else
2320
0
        target = vp9_calc_iframe_target_size_one_pass_vbr(cpi);
2321
0
    }
2322
0
  } else {
2323
0
    cm->frame_type = INTER_FRAME;
2324
0
    if (is_one_pass_svc(cpi)) {
2325
0
      LAYER_CONTEXT *lc = &svc->layer_context[layer];
2326
      // Add condition current_video_frame > 0 for the case where first frame
2327
      // is intra only followed by overlay/copy frame. In this case we don't
2328
      // want to reset is_key_frame to 0 on overlay/copy frame.
2329
0
      lc->is_key_frame =
2330
0
          (svc->spatial_layer_id == 0 && cm->current_video_frame > 0)
2331
0
              ? 0
2332
0
              : svc->layer_context[svc->temporal_layer_id].is_key_frame;
2333
0
      if (cpi->oxcf.rc_mode == VPX_CBR) {
2334
0
        target = vp9_calc_pframe_target_size_one_pass_cbr(cpi);
2335
0
      } else {
2336
0
        double rate_err = 0.0;
2337
0
        rc->fac_active_worst_inter = 140;
2338
0
        rc->fac_active_worst_gf = 100;
2339
0
        if (rc->rolling_target_bits > 0) {
2340
0
          rate_err =
2341
0
              (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
2342
0
          if (rate_err < 1.0)
2343
0
            rc->fac_active_worst_inter = 120;
2344
0
          else if (rate_err > 2.0)
2345
            // Increase active_worst faster if rate fluctuation is high.
2346
0
            rc->fac_active_worst_inter = 160;
2347
0
        }
2348
0
        target = vp9_calc_pframe_target_size_one_pass_vbr(cpi);
2349
0
      }
2350
0
    }
2351
0
  }
2352
2353
0
  if (svc->simulcast_mode) {
2354
0
    if (svc->spatial_layer_id > 0 &&
2355
0
        svc->layer_context[layer].is_key_frame == 1) {
2356
0
      cm->frame_type = KEY_FRAME;
2357
0
      cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2358
0
      if (cpi->oxcf.rc_mode == VPX_CBR)
2359
0
        target = vp9_calc_iframe_target_size_one_pass_cbr(cpi);
2360
0
      else
2361
0
        target = vp9_calc_iframe_target_size_one_pass_vbr(cpi);
2362
0
    }
2363
    // Set the buffer idx and refresh flags for key frames in simulcast mode.
2364
    // Note the buffer slot for long-term reference is set below (line 2255),
2365
    // and alt_ref is used for that on key frame. So use last and golden for
2366
    // the other two normal slots.
2367
0
    if (cm->frame_type == KEY_FRAME) {
2368
0
      if (svc->number_spatial_layers == 2) {
2369
0
        if (svc->spatial_layer_id == 0) {
2370
0
          cpi->lst_fb_idx = 0;
2371
0
          cpi->gld_fb_idx = 2;
2372
0
          cpi->alt_fb_idx = 6;
2373
0
        } else if (svc->spatial_layer_id == 1) {
2374
0
          cpi->lst_fb_idx = 1;
2375
0
          cpi->gld_fb_idx = 3;
2376
0
          cpi->alt_fb_idx = 6;
2377
0
        }
2378
0
      } else if (svc->number_spatial_layers == 3) {
2379
0
        if (svc->spatial_layer_id == 0) {
2380
0
          cpi->lst_fb_idx = 0;
2381
0
          cpi->gld_fb_idx = 3;
2382
0
          cpi->alt_fb_idx = 6;
2383
0
        } else if (svc->spatial_layer_id == 1) {
2384
0
          cpi->lst_fb_idx = 1;
2385
0
          cpi->gld_fb_idx = 4;
2386
0
          cpi->alt_fb_idx = 6;
2387
0
        } else if (svc->spatial_layer_id == 2) {
2388
0
          cpi->lst_fb_idx = 2;
2389
0
          cpi->gld_fb_idx = 5;
2390
0
          cpi->alt_fb_idx = 7;
2391
0
        }
2392
0
      }
2393
0
      cpi->ext_refresh_last_frame = 1;
2394
0
      cpi->ext_refresh_golden_frame = 1;
2395
0
      cpi->ext_refresh_alt_ref_frame = 1;
2396
0
    }
2397
0
  }
2398
2399
  // Check if superframe contains a sync layer request.
2400
0
  vp9_svc_check_spatial_layer_sync(cpi);
2401
2402
  // If long term termporal feature is enabled, set the period of the update.
2403
  // The update/refresh of this reference frame is always on base temporal
2404
  // layer frame.
2405
0
  if (svc->use_gf_temporal_ref_current_layer) {
2406
    // Only use gf long-term prediction on non-key superframes.
2407
0
    if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
2408
      // Use golden for this reference, which will be used for prediction.
2409
0
      int index = svc->spatial_layer_id;
2410
0
      if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
2411
0
      assert(index >= 0);
2412
0
      cpi->gld_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
2413
      // Enable prediction off LAST (last reference) and golden (which will
2414
      // generally be further behind/long-term reference).
2415
0
      cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
2416
0
    }
2417
    // Check for update/refresh of reference: only refresh on base temporal
2418
    // layer.
2419
0
    if (svc->temporal_layer_id == 0) {
2420
0
      if (svc->layer_context[svc->temporal_layer_id].is_key_frame) {
2421
        // On key frame we update the buffer index used for long term reference.
2422
        // Use the alt_ref since it is not used or updated on key frames.
2423
0
        int index = svc->spatial_layer_id;
2424
0
        if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
2425
0
        assert(index >= 0);
2426
0
        cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
2427
0
        cpi->ext_refresh_alt_ref_frame = 1;
2428
0
      } else if (rc->frames_till_gf_update_due == 0) {
2429
        // Set perdiod of next update. Make it a multiple of 10, as the cyclic
2430
        // refresh is typically ~10%, and we'd like the update to happen after
2431
        // a few cylces of the refresh (so it better quality frame). Note the
2432
        // cyclic refresh for SVC only operates on base temporal layer frames.
2433
        // Choose 20 as perdiod for now (2 cycles).
2434
0
        rc->baseline_gf_interval = 20;
2435
0
        rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2436
0
        cpi->ext_refresh_golden_frame = 1;
2437
0
        rc->gfu_boost = DEFAULT_GF_BOOST;
2438
0
      }
2439
0
    }
2440
0
  } else if (!svc->use_gf_temporal_ref) {
2441
0
    rc->frames_till_gf_update_due = INT_MAX;
2442
0
    rc->baseline_gf_interval = INT_MAX;
2443
0
  }
2444
0
  if (svc->set_intra_only_frame) {
2445
0
    set_intra_only_frame(cpi);
2446
0
    if (cpi->oxcf.rc_mode == VPX_CBR)
2447
0
      target = vp9_calc_iframe_target_size_one_pass_cbr(cpi);
2448
0
    else
2449
0
      target = vp9_calc_iframe_target_size_one_pass_vbr(cpi);
2450
0
  }
2451
  // Overlay frame predicts from LAST (intra-only)
2452
0
  if (svc->previous_frame_is_intra_only) cpi->ref_frame_flags |= VP9_LAST_FLAG;
2453
2454
  // Any update/change of global cyclic refresh parameters (amount/delta-qp)
2455
  // should be done here, before the frame qp is selected.
2456
0
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2457
0
    vp9_cyclic_refresh_update_parameters(cpi);
2458
2459
0
  vp9_rc_set_frame_target(cpi, target);
2460
0
  if (cm->show_frame) vp9_update_buffer_level_svc_preencode(cpi);
2461
2462
0
  if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && svc->single_layer_svc == 1 &&
2463
0
      svc->spatial_layer_id == svc->first_spatial_layer_to_encode &&
2464
0
      svc->temporal_layer_id == 0) {
2465
0
    LAYER_CONTEXT *lc = NULL;
2466
0
    cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
2467
0
    if (cpi->resize_pending) {
2468
0
      int tl, width, height;
2469
      // Apply the same scale to all temporal layers.
2470
0
      for (tl = 0; tl < svc->number_temporal_layers; tl++) {
2471
0
        lc = &svc->layer_context[svc->spatial_layer_id *
2472
0
                                     svc->number_temporal_layers +
2473
0
                                 tl];
2474
0
        lc->scaling_factor_num_resize =
2475
0
            cpi->resize_scale_num * lc->scaling_factor_num;
2476
0
        lc->scaling_factor_den_resize =
2477
0
            cpi->resize_scale_den * lc->scaling_factor_den;
2478
        // Reset rate control for all temporal layers.
2479
0
        lc->rc.buffer_level = lc->rc.optimal_buffer_level;
2480
0
        lc->rc.bits_off_target = lc->rc.optimal_buffer_level;
2481
0
        lc->rc.rate_correction_factors[INTER_FRAME] =
2482
0
            rc->rate_correction_factors[INTER_FRAME];
2483
0
      }
2484
      // Set the size for this current temporal layer.
2485
0
      lc = &svc->layer_context[svc->spatial_layer_id *
2486
0
                                   svc->number_temporal_layers +
2487
0
                               svc->temporal_layer_id];
2488
0
      get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
2489
0
                           lc->scaling_factor_num_resize,
2490
0
                           lc->scaling_factor_den_resize, &width, &height);
2491
0
      vp9_set_size_literal(cpi, width, height);
2492
0
      svc->resize_set = 1;
2493
0
    }
2494
0
  } else {
2495
0
    cpi->resize_pending = 0;
2496
0
    svc->resize_set = 0;
2497
0
  }
2498
0
}
2499
2500
0
void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
2501
0
  VP9_COMMON *const cm = &cpi->common;
2502
0
  RATE_CONTROL *const rc = &cpi->rc;
2503
0
  int target;
2504
0
  if ((cm->current_video_frame == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
2505
0
      (cpi->oxcf.auto_key && rc->frames_to_key == 0) ||
2506
0
      (cpi->oxcf.mode != cpi->deadline_mode_previous_frame)) {
2507
0
    cm->frame_type = KEY_FRAME;
2508
0
    rc->frames_to_key = cpi->oxcf.key_freq;
2509
0
    rc->kf_boost = DEFAULT_KF_BOOST;
2510
0
    rc->source_alt_ref_active = 0;
2511
0
  } else {
2512
0
    cm->frame_type = INTER_FRAME;
2513
0
  }
2514
0
  if (rc->frames_till_gf_update_due == 0) {
2515
0
    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2516
0
      vp9_cyclic_refresh_set_golden_update(cpi);
2517
0
    else
2518
0
      rc->baseline_gf_interval =
2519
0
          (rc->min_gf_interval + rc->max_gf_interval) / 2;
2520
0
    rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2521
    // NOTE: frames_till_gf_update_due must be <= frames_to_key.
2522
0
    if (rc->frames_till_gf_update_due > rc->frames_to_key)
2523
0
      rc->frames_till_gf_update_due = rc->frames_to_key;
2524
0
    cpi->refresh_golden_frame = 1;
2525
0
    rc->gfu_boost = DEFAULT_GF_BOOST;
2526
0
  }
2527
2528
  // Any update/change of global cyclic refresh parameters (amount/delta-qp)
2529
  // should be done here, before the frame qp is selected.
2530
0
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2531
0
    vp9_cyclic_refresh_update_parameters(cpi);
2532
2533
0
  if (frame_is_intra_only(cm))
2534
0
    target = vp9_calc_iframe_target_size_one_pass_cbr(cpi);
2535
0
  else
2536
0
    target = vp9_calc_pframe_target_size_one_pass_cbr(cpi);
2537
2538
0
  vp9_rc_set_frame_target(cpi, target);
2539
2540
0
  if (cm->show_frame) vp9_update_buffer_level_preencode(cpi);
2541
2542
0
  if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC)
2543
0
    cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
2544
0
  else
2545
0
    cpi->resize_pending = 0;
2546
0
}
2547
2548
int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
2549
13.4k
                       vpx_bit_depth_t bit_depth) {
2550
13.4k
  int start_index = rc->worst_quality;
2551
13.4k
  int target_index = rc->worst_quality;
2552
13.4k
  int i;
2553
2554
  // Convert the average q value to an index.
2555
1.04M
  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
2556
1.04M
    start_index = i;
2557
1.04M
    if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart) break;
2558
1.04M
  }
2559
2560
  // Convert the q target to an index
2561
737k
  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
2562
737k
    target_index = i;
2563
737k
    if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
2564
737k
  }
2565
2566
13.4k
  return target_index - start_index;
2567
13.4k
}
2568
2569
int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
2570
                               int qindex, double rate_target_ratio,
2571
15.2k
                               vpx_bit_depth_t bit_depth) {
2572
15.2k
  int target_index = rc->worst_quality;
2573
15.2k
  int i;
2574
2575
  // Look up the current projected bits per block for the base index
2576
15.2k
  const int base_bits_per_mb =
2577
15.2k
      vp9_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
2578
2579
  // Find the target bits per mb based on the base value and given ratio.
2580
15.2k
  const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
2581
2582
  // Convert the q target to an index
2583
1.88M
  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
2584
1.88M
    if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
2585
1.88M
        target_bits_per_mb) {
2586
15.2k
      target_index = i;
2587
15.2k
      break;
2588
15.2k
    }
2589
1.88M
  }
2590
15.2k
  return target_index - qindex;
2591
15.2k
}
2592
2593
void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi,
2594
87.1k
                                  RATE_CONTROL *const rc) {
2595
87.1k
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2596
2597
  // Special case code for 1 pass fixed Q mode tests
2598
87.1k
  if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
2599
4.78k
    rc->max_gf_interval = FIXED_GF_INTERVAL;
2600
4.78k
    rc->min_gf_interval = FIXED_GF_INTERVAL;
2601
4.78k
    rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
2602
82.3k
  } else {
2603
82.3k
    double framerate = cpi->framerate;
2604
    // Set Maximum gf/arf interval
2605
82.3k
    rc->max_gf_interval = oxcf->max_gf_interval;
2606
82.3k
    rc->min_gf_interval = oxcf->min_gf_interval;
2607
82.3k
    if (rc->min_gf_interval == 0) {
2608
82.3k
      rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
2609
82.3k
          oxcf->width, oxcf->height, framerate);
2610
82.3k
    }
2611
82.3k
    if (rc->max_gf_interval == 0) {
2612
82.3k
      rc->max_gf_interval =
2613
82.3k
          vp9_rc_get_default_max_gf_interval(framerate, rc->min_gf_interval);
2614
82.3k
    }
2615
2616
    // Extended max interval for genuinely static scenes like slide shows.
2617
82.3k
    rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH;
2618
2619
82.3k
    if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
2620
176
      rc->max_gf_interval = rc->static_scene_max_gf_interval;
2621
2622
    // Clamp min to max
2623
82.3k
    rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
2624
2625
82.3k
    if (oxcf->target_level == LEVEL_AUTO) {
2626
0
      const uint32_t pic_size = cpi->common.width * cpi->common.height;
2627
0
      const uint32_t pic_breadth =
2628
0
          VPXMAX(cpi->common.width, cpi->common.height);
2629
0
      int i;
2630
0
      for (i = 0; i < VP9_LEVELS; ++i) {
2631
0
        if (vp9_level_defs[i].max_luma_picture_size >= pic_size &&
2632
0
            vp9_level_defs[i].max_luma_picture_breadth >= pic_breadth) {
2633
0
          if (rc->min_gf_interval <=
2634
0
              (int)vp9_level_defs[i].min_altref_distance) {
2635
0
            rc->min_gf_interval = (int)vp9_level_defs[i].min_altref_distance;
2636
0
            rc->max_gf_interval =
2637
0
                VPXMAX(rc->max_gf_interval, rc->min_gf_interval);
2638
0
          }
2639
0
          break;
2640
0
        }
2641
0
      }
2642
0
    }
2643
82.3k
  }
2644
87.1k
}
2645
2646
87.1k
void vp9_rc_update_framerate(VP9_COMP *cpi) {
2647
87.1k
  const VP9_COMMON *const cm = &cpi->common;
2648
87.1k
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2649
87.1k
  RATE_CONTROL *const rc = &cpi->rc;
2650
2651
87.1k
  rc->avg_frame_bandwidth = saturate_cast_double_to_int(
2652
87.1k
      round(oxcf->target_bandwidth / cpi->framerate));
2653
2654
87.1k
  int64_t vbr_min_bits =
2655
87.1k
      (int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100;
2656
87.1k
  vbr_min_bits = VPXMIN(vbr_min_bits, INT_MAX);
2657
2658
87.1k
  rc->min_frame_bandwidth = VPXMAX((int)vbr_min_bits, FRAME_OVERHEAD_BITS);
2659
2660
  // A maximum bitrate for a frame is defined.
2661
  // However this limit is extended if a very high rate is given on the command
2662
  // line or the rate can not be achieved because of a user specified max q
2663
  // (e.g. when the user specifies lossless encode).
2664
  //
2665
  // If a level is specified that requires a lower maximum rate then the level
2666
  // value take precedence.
2667
87.1k
  int64_t vbr_max_bits =
2668
87.1k
      (int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section / 100;
2669
87.1k
  vbr_max_bits = VPXMIN(vbr_max_bits, INT_MAX);
2670
2671
87.1k
  rc->max_frame_bandwidth =
2672
87.1k
      VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), (int)vbr_max_bits);
2673
2674
87.1k
  vp9_rc_set_gf_interval_range(cpi, rc);
2675
87.1k
}
2676
2677
#define VBR_PCT_ADJUSTMENT_LIMIT 50
2678
// For VBR...adjustment to the frame target based on error from previous frames
2679
0
static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
2680
0
  RATE_CONTROL *const rc = &cpi->rc;
2681
0
  int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
2682
0
  int64_t frame_target = *this_frame_target;
2683
0
  int frame_window = (int)VPXMIN(
2684
0
      16, cpi->twopass.total_stats.count - cpi->common.current_video_frame);
2685
2686
  // Calcluate the adjustment to rate for this frame.
2687
0
  if (frame_window > 0) {
2688
0
    int64_t max_delta = (vbr_bits_off_target > 0)
2689
0
                            ? (vbr_bits_off_target / frame_window)
2690
0
                            : (-vbr_bits_off_target / frame_window);
2691
2692
0
    max_delta =
2693
0
        VPXMIN(max_delta, ((frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
2694
2695
    // vbr_bits_off_target > 0 means we have extra bits to spend
2696
0
    if (vbr_bits_off_target > 0) {
2697
0
      frame_target += VPXMIN(vbr_bits_off_target, max_delta);
2698
0
    } else {
2699
0
      frame_target -= VPXMIN(-vbr_bits_off_target, max_delta);
2700
0
    }
2701
0
  }
2702
2703
  // Fast redistribution of bits arising from massive local undershoot.
2704
  // Don't do it for kf,arf,gf or overlay frames.
2705
0
  if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
2706
0
      rc->vbr_bits_off_target_fast) {
2707
0
    int64_t one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, frame_target);
2708
0
    int64_t fast_extra_bits =
2709
0
        VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
2710
0
    fast_extra_bits =
2711
0
        VPXMIN(fast_extra_bits,
2712
0
               VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
2713
0
    frame_target += fast_extra_bits;
2714
0
    rc->vbr_bits_off_target_fast -= fast_extra_bits;
2715
0
  }
2716
2717
  // Clamp the target for the frame to the maximum allowed for one frame.
2718
0
  *this_frame_target = (int)VPXMIN(frame_target, INT_MAX);
2719
0
}
2720
2721
0
void vp9_set_target_rate(VP9_COMP *cpi) {
2722
0
  RATE_CONTROL *const rc = &cpi->rc;
2723
0
  int target_rate = rc->base_frame_target;
2724
2725
0
  if (cpi->common.frame_type == KEY_FRAME)
2726
0
    target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
2727
0
  else
2728
0
    target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2729
2730
0
  if (!cpi->oxcf.vbr_corpus_complexity) {
2731
    // Correction to rate target based on prior over or under shoot.
2732
0
    if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
2733
0
      vbr_rate_correction(cpi, &target_rate);
2734
0
  }
2735
0
  vp9_rc_set_frame_target(cpi, target_rate);
2736
0
}
2737
2738
// Check if we should resize, based on average QP from past x frames.
2739
// Only allow for resize at most one scale down for now, scaling factor is 2.
2740
0
int vp9_resize_one_pass_cbr(VP9_COMP *cpi) {
2741
0
  const VP9_COMMON *const cm = &cpi->common;
2742
0
  RATE_CONTROL *const rc = &cpi->rc;
2743
0
  RESIZE_ACTION resize_action = NO_RESIZE;
2744
0
  int avg_qp_thr1 = 70;
2745
0
  int avg_qp_thr2 = 50;
2746
  // Don't allow for resized frame to go below 320x180, resize in steps of 3/4.
2747
0
  int min_width = (320 * 4) / 3;
2748
0
  int min_height = (180 * 4) / 3;
2749
0
  int down_size_on = 1;
2750
0
  int force_downsize_rate = 0;
2751
0
  cpi->resize_scale_num = 1;
2752
0
  cpi->resize_scale_den = 1;
2753
  // Don't resize on key frame; reset the counters on key frame.
2754
0
  if (cm->frame_type == KEY_FRAME) {
2755
0
    cpi->resize_avg_qp = 0;
2756
0
    cpi->resize_count = 0;
2757
0
    return 0;
2758
0
  }
2759
2760
  // No resizing down if frame size is below some limit.
2761
0
  if ((cm->width * cm->height) < min_width * min_height) down_size_on = 0;
2762
2763
#if CONFIG_VP9_TEMPORAL_DENOISING
2764
  // If denoiser is on, apply a smaller qp threshold.
2765
  if (cpi->oxcf.noise_sensitivity > 0) {
2766
    avg_qp_thr1 = 60;
2767
    avg_qp_thr2 = 40;
2768
  }
2769
#endif
2770
2771
  // Force downsize based on per-frame-bandwidth, for extreme case,
2772
  // for HD input.
2773
0
  if (cpi->resize_state == ORIG && cm->width * cm->height >= 1280 * 720) {
2774
0
    if (rc->avg_frame_bandwidth < 300000 / 30) {
2775
0
      resize_action = DOWN_ONEHALF;
2776
0
      cpi->resize_state = ONE_HALF;
2777
0
      force_downsize_rate = 1;
2778
0
    } else if (rc->avg_frame_bandwidth < 400000 / 30) {
2779
0
      resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
2780
0
      cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
2781
0
      force_downsize_rate = 1;
2782
0
    }
2783
0
  } else if (cpi->resize_state == THREE_QUARTER &&
2784
0
             cm->width * cm->height >= 960 * 540) {
2785
0
    if (rc->avg_frame_bandwidth < 300000 / 30) {
2786
0
      resize_action = DOWN_ONEHALF;
2787
0
      cpi->resize_state = ONE_HALF;
2788
0
      force_downsize_rate = 1;
2789
0
    }
2790
0
  }
2791
2792
  // Resize based on average buffer underflow and QP over some window.
2793
  // Ignore samples close to key frame, since QP is usually high after key.
2794
0
  if (!force_downsize_rate && cpi->rc.frames_since_key > cpi->framerate) {
2795
0
    const int window = VPXMIN(30, (int)round(2 * cpi->framerate));
2796
0
    cpi->resize_avg_qp += rc->last_q[INTER_FRAME];
2797
0
    if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100))
2798
0
      ++cpi->resize_buffer_underflow;
2799
0
    ++cpi->resize_count;
2800
    // Check for resize action every "window" frames.
2801
0
    if (cpi->resize_count >= window) {
2802
0
      int avg_qp = cpi->resize_avg_qp / cpi->resize_count;
2803
      // Resize down if buffer level has underflowed sufficient amount in past
2804
      // window, and we are at original or 3/4 of original resolution.
2805
      // Resize back up if average QP is low, and we are currently in a resized
2806
      // down state, i.e. 1/2 or 3/4 of original resolution.
2807
      // Currently, use a flag to turn 3/4 resizing feature on/off.
2808
0
      if (cpi->resize_buffer_underflow > (cpi->resize_count >> 2) &&
2809
0
          down_size_on) {
2810
0
        if (cpi->resize_state == THREE_QUARTER) {
2811
0
          resize_action = DOWN_ONEHALF;
2812
0
          cpi->resize_state = ONE_HALF;
2813
0
        } else if (cpi->resize_state == ORIG) {
2814
0
          resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
2815
0
          cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
2816
0
        }
2817
0
      } else if (cpi->resize_state != ORIG &&
2818
0
                 avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
2819
0
        if (cpi->resize_state == THREE_QUARTER ||
2820
0
            avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100 ||
2821
0
            ONEHALFONLY_RESIZE) {
2822
0
          resize_action = UP_ORIG;
2823
0
          cpi->resize_state = ORIG;
2824
0
        } else if (cpi->resize_state == ONE_HALF) {
2825
0
          resize_action = UP_THREEFOUR;
2826
0
          cpi->resize_state = THREE_QUARTER;
2827
0
        }
2828
0
      }
2829
      // Reset for next window measurement.
2830
0
      cpi->resize_avg_qp = 0;
2831
0
      cpi->resize_count = 0;
2832
0
      cpi->resize_buffer_underflow = 0;
2833
0
    }
2834
0
  }
2835
  // If decision is to resize, reset some quantities, and check is we should
2836
  // reduce rate correction factor,
2837
0
  if (resize_action != NO_RESIZE) {
2838
0
    int target_bits_per_frame;
2839
0
    int active_worst_quality;
2840
0
    int qindex;
2841
0
    int tot_scale_change;
2842
0
    if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
2843
0
      cpi->resize_scale_num = 3;
2844
0
      cpi->resize_scale_den = 4;
2845
0
    } else if (resize_action == DOWN_ONEHALF) {
2846
0
      cpi->resize_scale_num = 1;
2847
0
      cpi->resize_scale_den = 2;
2848
0
    } else {  // UP_ORIG or anything else
2849
0
      cpi->resize_scale_num = 1;
2850
0
      cpi->resize_scale_den = 1;
2851
0
    }
2852
0
    tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) /
2853
0
                       (cpi->resize_scale_num * cpi->resize_scale_num);
2854
    // Reset buffer level to optimal, update target size.
2855
0
    rc->buffer_level = rc->optimal_buffer_level;
2856
0
    rc->bits_off_target = rc->optimal_buffer_level;
2857
0
    rc->this_frame_target = vp9_calc_pframe_target_size_one_pass_cbr(cpi);
2858
    // Get the projected qindex, based on the scaled target frame size (scaled
2859
    // so target_bits_per_mb in vp9_rc_regulate_q will be correct target).
2860
0
    target_bits_per_frame = (resize_action >= 0)
2861
0
                                ? rc->this_frame_target * tot_scale_change
2862
0
                                : rc->this_frame_target / tot_scale_change;
2863
0
    active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
2864
0
    qindex = vp9_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality,
2865
0
                               active_worst_quality);
2866
    // If resize is down, check if projected q index is close to worst_quality,
2867
    // and if so, reduce the rate correction factor (since likely can afford
2868
    // lower q for resized frame).
2869
0
    if (resize_action > 0 && qindex > 90 * cpi->rc.worst_quality / 100) {
2870
0
      rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
2871
0
    }
2872
    // If resize is back up, check if projected q index is too much above the
2873
    // current base_qindex, and if so, reduce the rate correction factor
2874
    // (since prefer to keep q for resized frame at least close to previous q).
2875
0
    if (resize_action < 0 && qindex > 130 * cm->base_qindex / 100) {
2876
0
      rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
2877
0
    }
2878
0
  }
2879
0
  return resize_action;
2880
0
}
2881
2882
static void adjust_gf_boost_lag_one_pass_vbr(VP9_COMP *cpi,
2883
27.1k
                                             uint64_t avg_sad_current) {
2884
27.1k
  VP9_COMMON *const cm = &cpi->common;
2885
27.1k
  RATE_CONTROL *const rc = &cpi->rc;
2886
27.1k
  int target;
2887
27.1k
  int found = 0;
2888
27.1k
  int found2 = 0;
2889
27.1k
  int frame;
2890
27.1k
  int i;
2891
27.1k
  uint64_t avg_source_sad_lag = avg_sad_current;
2892
27.1k
  int high_source_sad_lagindex = -1;
2893
27.1k
  int steady_sad_lagindex = -1;
2894
27.1k
  uint32_t sad_thresh1 = 70000;
2895
27.1k
  uint32_t sad_thresh2 = 120000;
2896
27.1k
  int low_content = 0;
2897
27.1k
  int high_content = 0;
2898
27.1k
  double rate_err = 1.0;
2899
  // Get measure of complexity over the future frames, and get the first
2900
  // future frame with high_source_sad/scene-change.
2901
27.1k
  int tot_frames = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
2902
447k
  for (frame = tot_frames; frame >= 1; --frame) {
2903
420k
    const int lagframe_idx = tot_frames - frame + 1;
2904
420k
    uint64_t reference_sad = rc->avg_source_sad[0];
2905
4.56M
    for (i = 1; i < lagframe_idx; ++i) {
2906
4.14M
      if (rc->avg_source_sad[i] > 0)
2907
1.35k
        reference_sad = (3 * reference_sad + rc->avg_source_sad[i]) >> 2;
2908
4.14M
    }
2909
    // Detect up-coming scene change.
2910
420k
    if (!found &&
2911
420k
        (rc->avg_source_sad[lagframe_idx] >
2912
419k
             VPXMAX(sad_thresh1, (unsigned int)(reference_sad << 1)) ||
2913
419k
         rc->avg_source_sad[lagframe_idx] >
2914
419k
             VPXMAX(3 * sad_thresh1 >> 2,
2915
419k
                    (unsigned int)(reference_sad << 2)))) {
2916
160
      high_source_sad_lagindex = lagframe_idx;
2917
160
      found = 1;
2918
160
    }
2919
    // Detect change from motion to steady.
2920
420k
    if (!found2 && lagframe_idx > 1 && lagframe_idx < tot_frames &&
2921
420k
        rc->avg_source_sad[lagframe_idx - 1] > (sad_thresh1 >> 2)) {
2922
267
      found2 = 1;
2923
543
      for (i = lagframe_idx; i < tot_frames; ++i) {
2924
276
        if (!(rc->avg_source_sad[i] > 0 &&
2925
276
              rc->avg_source_sad[i] < (sad_thresh1 >> 2) &&
2926
276
              rc->avg_source_sad[i] <
2927
253
                  (rc->avg_source_sad[lagframe_idx - 1] >> 1))) {
2928
253
          found2 = 0;
2929
253
          i = tot_frames;
2930
253
        }
2931
276
      }
2932
267
      if (found2) steady_sad_lagindex = lagframe_idx;
2933
267
    }
2934
420k
    avg_source_sad_lag += rc->avg_source_sad[lagframe_idx];
2935
420k
  }
2936
27.1k
  if (tot_frames > 0) avg_source_sad_lag = avg_source_sad_lag / tot_frames;
2937
  // Constrain distance between detected scene cuts.
2938
27.1k
  if (high_source_sad_lagindex != -1 &&
2939
27.1k
      high_source_sad_lagindex != rc->high_source_sad_lagindex - 1 &&
2940
27.1k
      abs(high_source_sad_lagindex - rc->high_source_sad_lagindex) < 4)
2941
48
    rc->high_source_sad_lagindex = -1;
2942
27.0k
  else
2943
27.0k
    rc->high_source_sad_lagindex = high_source_sad_lagindex;
2944
  // Adjust some factors for the next GF group, ignore initial key frame,
2945
  // and only for lag_in_frames not too small.
2946
27.1k
  if (cpi->refresh_golden_frame == 1 && cm->current_video_frame > 30 &&
2947
27.1k
      cpi->oxcf.lag_in_frames > 8) {
2948
3.01k
    int frame_constraint;
2949
3.01k
    if (rc->rolling_target_bits > 0)
2950
1.65k
      rate_err =
2951
1.65k
          (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
2952
3.01k
    high_content = high_source_sad_lagindex != -1 ||
2953
3.01k
                   avg_source_sad_lag > (rc->prev_avg_source_sad_lag << 1) ||
2954
3.01k
                   avg_source_sad_lag > sad_thresh2;
2955
3.01k
    low_content = high_source_sad_lagindex == -1 &&
2956
3.01k
                  ((avg_source_sad_lag < (rc->prev_avg_source_sad_lag >> 1)) ||
2957
3.01k
                   (avg_source_sad_lag < sad_thresh1));
2958
3.01k
    if (low_content) {
2959
3.01k
      rc->gfu_boost = DEFAULT_GF_BOOST;
2960
3.01k
      rc->baseline_gf_interval =
2961
3.01k
          VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
2962
3.01k
    } else if (high_content) {
2963
0
      rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
2964
0
      rc->baseline_gf_interval = (rate_err > 3.0)
2965
0
                                     ? VPXMAX(10, rc->baseline_gf_interval >> 1)
2966
0
                                     : VPXMAX(6, rc->baseline_gf_interval >> 1);
2967
0
    }
2968
3.01k
    if (rc->baseline_gf_interval > cpi->oxcf.lag_in_frames - 1)
2969
0
      rc->baseline_gf_interval = cpi->oxcf.lag_in_frames - 1;
2970
    // Check for constraining gf_interval for up-coming scene/content changes,
2971
    // or for up-coming key frame, whichever is closer.
2972
3.01k
    frame_constraint = rc->frames_to_key;
2973
3.01k
    if (rc->high_source_sad_lagindex > 0 &&
2974
3.01k
        frame_constraint > rc->high_source_sad_lagindex)
2975
0
      frame_constraint = rc->high_source_sad_lagindex;
2976
3.01k
    if (steady_sad_lagindex > 3 && frame_constraint > steady_sad_lagindex)
2977
0
      frame_constraint = steady_sad_lagindex;
2978
3.01k
    adjust_gfint_frame_constraint(cpi, frame_constraint);
2979
3.01k
    rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2980
    // Adjust factors for active_worst setting & af_ratio for next gf interval.
2981
3.01k
    rc->fac_active_worst_inter = 150;  // corresponds to 3/2 (= 150 /100).
2982
3.01k
    rc->fac_active_worst_gf = 100;
2983
3.01k
    if (rate_err < 2.0 && !high_content) {
2984
2.75k
      rc->fac_active_worst_inter = 120;
2985
2.75k
      rc->fac_active_worst_gf = 90;
2986
2.75k
    } else if (rate_err > 8.0 && rc->avg_frame_qindex[INTER_FRAME] < 16) {
2987
      // Increase active_worst faster at low Q if rate fluctuation is high.
2988
0
      rc->fac_active_worst_inter = 200;
2989
0
      if (rc->avg_frame_qindex[INTER_FRAME] < 8)
2990
0
        rc->fac_active_worst_inter = 400;
2991
0
    }
2992
3.01k
    if (low_content && rc->avg_frame_low_motion > 80) {
2993
119
      rc->af_ratio_onepass_vbr = 15;
2994
2.89k
    } else if (high_content || rc->avg_frame_low_motion < 30) {
2995
2.71k
      rc->af_ratio_onepass_vbr = 5;
2996
2.71k
      rc->gfu_boost = DEFAULT_GF_BOOST >> 2;
2997
2.71k
    }
2998
3.01k
    if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
2999
      // Flag to disable usage of ARF based on past usage, only allow this
3000
      // disabling if current frame/group does not start with key frame or
3001
      // scene cut. Note perc_arf_usage is only computed for speed >= 5.
3002
0
      int arf_usage_low =
3003
0
          (cm->frame_type != KEY_FRAME && !rc->high_source_sad &&
3004
0
           cpi->rc.perc_arf_usage < 15 && cpi->oxcf.speed >= 5);
3005
      // Don't use alt-ref for this group under certain conditions.
3006
0
      if (arf_usage_low ||
3007
0
          (rc->high_source_sad_lagindex > 0 &&
3008
0
           rc->high_source_sad_lagindex <= rc->frames_till_gf_update_due) ||
3009
0
          (avg_source_sad_lag > 3 * sad_thresh1 >> 3)) {
3010
0
        rc->source_alt_ref_pending = 0;
3011
0
        rc->alt_ref_gf_group = 0;
3012
0
      } else {
3013
0
        rc->source_alt_ref_pending = 1;
3014
0
        rc->alt_ref_gf_group = 1;
3015
        // If alt-ref is used for this gf group, limit the interval.
3016
0
        if (rc->baseline_gf_interval > 12) {
3017
0
          rc->baseline_gf_interval = 12;
3018
0
          rc->frames_till_gf_update_due = rc->baseline_gf_interval;
3019
0
        }
3020
0
      }
3021
0
    }
3022
3.01k
    target = vp9_calc_pframe_target_size_one_pass_vbr(cpi);
3023
3.01k
    vp9_rc_set_frame_target(cpi, target);
3024
3.01k
  }
3025
27.1k
  rc->prev_avg_source_sad_lag = avg_source_sad_lag;
3026
27.1k
}
3027
3028
// Compute average source sad (temporal sad: between current source and
3029
// previous source) over a subset of superblocks. Use this is detect big changes
3030
// in content and allow rate control to react.
3031
// This function also handles special case of lag_in_frames, to measure content
3032
// level in #future frames set by the lag_in_frames.
3033
29.3k
void vp9_scene_detection_onepass(VP9_COMP *cpi) {
3034
29.3k
  VP9_COMMON *const cm = &cpi->common;
3035
29.3k
  RATE_CONTROL *const rc = &cpi->rc;
3036
29.3k
  YV12_BUFFER_CONFIG const *unscaled_src = cpi->un_scaled_source;
3037
29.3k
  YV12_BUFFER_CONFIG const *unscaled_last_src = cpi->unscaled_last_source;
3038
29.3k
  uint8_t *src_y;
3039
29.3k
  int src_ystride;
3040
29.3k
  int src_width;
3041
29.3k
  int src_height;
3042
29.3k
  uint8_t *last_src_y;
3043
29.3k
  int last_src_ystride;
3044
29.3k
  int last_src_width;
3045
29.3k
  int last_src_height;
3046
29.3k
  if (cpi->un_scaled_source == NULL || cpi->unscaled_last_source == NULL ||
3047
29.3k
      (cpi->use_svc && cpi->svc.current_superframe == 0))
3048
2.25k
    return;
3049
27.1k
  src_y = unscaled_src->y_buffer;
3050
27.1k
  src_ystride = unscaled_src->y_stride;
3051
27.1k
  src_width = unscaled_src->y_width;
3052
27.1k
  src_height = unscaled_src->y_height;
3053
27.1k
  last_src_y = unscaled_last_src->y_buffer;
3054
27.1k
  last_src_ystride = unscaled_last_src->y_stride;
3055
27.1k
  last_src_width = unscaled_last_src->y_width;
3056
27.1k
  last_src_height = unscaled_last_src->y_height;
3057
27.1k
#if CONFIG_VP9_HIGHBITDEPTH
3058
27.1k
  if (cm->use_highbitdepth) return;
3059
27.1k
#endif
3060
27.1k
  rc->high_source_sad = 0;
3061
27.1k
  rc->high_num_blocks_with_motion = 0;
3062
  // For SVC: scene detection is only checked on first spatial layer of
3063
  // the superframe using the original/unscaled resolutions.
3064
27.1k
  if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode &&
3065
27.1k
      src_width == last_src_width && src_height == last_src_height) {
3066
27.1k
    YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = { NULL };
3067
27.1k
    int num_mi_cols = cm->mi_cols;
3068
27.1k
    int num_mi_rows = cm->mi_rows;
3069
27.1k
    int start_frame = 0;
3070
27.1k
    int frames_to_buffer = 1;
3071
27.1k
    int frame = 0;
3072
27.1k
    int scene_cut_force_key_frame = 0;
3073
27.1k
    int num_zero_temp_sad = 0;
3074
27.1k
    uint64_t avg_sad_current = 0;
3075
27.1k
    uint32_t min_thresh = 20000;  // ~5 * 64 * 64
3076
27.1k
    float thresh = 8.0f;
3077
27.1k
    uint32_t thresh_key = 140000;
3078
27.1k
    if (cpi->oxcf.speed <= 5) thresh_key = 240000;
3079
27.1k
    if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) min_thresh = 65000;
3080
27.1k
    if (cpi->oxcf.rc_mode == VPX_VBR) thresh = 2.1f;
3081
27.1k
    if (cpi->use_svc && cpi->svc.number_spatial_layers > 1) {
3082
0
      const int aligned_width = ALIGN_POWER_OF_TWO(src_width, MI_SIZE_LOG2);
3083
0
      const int aligned_height = ALIGN_POWER_OF_TWO(src_height, MI_SIZE_LOG2);
3084
0
      num_mi_cols = aligned_width >> MI_SIZE_LOG2;
3085
0
      num_mi_rows = aligned_height >> MI_SIZE_LOG2;
3086
0
    }
3087
27.1k
    if (cpi->oxcf.lag_in_frames > 0) {
3088
27.1k
      frames_to_buffer = (cm->current_video_frame == 1)
3089
27.1k
                             ? (int)vp9_lookahead_depth(cpi->lookahead) - 1
3090
27.1k
                             : 2;
3091
27.1k
      start_frame = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
3092
90.0k
      for (frame = 0; frame < frames_to_buffer; ++frame) {
3093
62.9k
        const int lagframe_idx = start_frame - frame;
3094
62.9k
        if (lagframe_idx >= 0) {
3095
59.8k
          struct lookahead_entry *buf =
3096
59.8k
              vp9_lookahead_peek(cpi->lookahead, lagframe_idx);
3097
59.8k
          frames[frame] = &buf->img;
3098
59.8k
        }
3099
62.9k
      }
3100
      // The avg_sad for this current frame is the value of frame#1
3101
      // (first future frame) from previous frame.
3102
27.1k
      avg_sad_current = rc->avg_source_sad[1];
3103
27.1k
      if (avg_sad_current >
3104
27.1k
              VPXMAX(min_thresh,
3105
27.1k
                     (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
3106
27.1k
          cm->current_video_frame > (unsigned int)cpi->oxcf.lag_in_frames)
3107
0
        rc->high_source_sad = 1;
3108
27.1k
      else
3109
27.1k
        rc->high_source_sad = 0;
3110
27.1k
      if (rc->high_source_sad && avg_sad_current > thresh_key)
3111
0
        scene_cut_force_key_frame = 1;
3112
      // Update recursive average for current frame.
3113
27.1k
      if (avg_sad_current > 0)
3114
166
        rc->avg_source_sad[0] =
3115
166
            (3 * rc->avg_source_sad[0] + avg_sad_current) >> 2;
3116
      // Shift back data, starting at frame#1.
3117
650k
      for (frame = 1; frame < cpi->oxcf.lag_in_frames - 1; ++frame)
3118
623k
        rc->avg_source_sad[frame] = rc->avg_source_sad[frame + 1];
3119
27.1k
    }
3120
90.0k
    for (frame = 0; frame < frames_to_buffer; ++frame) {
3121
62.9k
      if (cpi->oxcf.lag_in_frames == 0 ||
3122
62.9k
          (frames[frame] != NULL && frames[frame + 1] != NULL &&
3123
62.9k
           frames[frame]->y_width == frames[frame + 1]->y_width &&
3124
62.9k
           frames[frame]->y_height == frames[frame + 1]->y_height)) {
3125
34.2k
        int sbi_row, sbi_col;
3126
34.2k
        const int lagframe_idx =
3127
34.2k
            (cpi->oxcf.lag_in_frames == 0) ? 0 : start_frame - frame + 1;
3128
34.2k
        const BLOCK_SIZE bsize = BLOCK_64X64;
3129
        // Loop over sub-sample of frame, compute average sad over 64x64 blocks.
3130
34.2k
        uint64_t avg_sad = 0;
3131
34.2k
        uint64_t tmp_sad = 0;
3132
34.2k
        int num_samples = 0;
3133
34.2k
        int sb_cols = (num_mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
3134
34.2k
        int sb_rows = (num_mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
3135
34.2k
        if (cpi->oxcf.lag_in_frames > 0) {
3136
34.2k
          src_y = frames[frame]->y_buffer;
3137
34.2k
          src_ystride = frames[frame]->y_stride;
3138
34.2k
          last_src_y = frames[frame + 1]->y_buffer;
3139
34.2k
          last_src_ystride = frames[frame + 1]->y_stride;
3140
34.2k
        }
3141
34.2k
        num_zero_temp_sad = 0;
3142
70.6k
        for (sbi_row = 0; sbi_row < sb_rows; ++sbi_row) {
3143
80.8k
          for (sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
3144
            // Checker-board pattern, ignore boundary.
3145
44.4k
            if (((sbi_row > 0 && sbi_col > 0) &&
3146
44.4k
                 (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) &&
3147
44.4k
                 ((sbi_row % 2 == 0 && sbi_col % 2 == 0) ||
3148
1.24k
                  (sbi_row % 2 != 0 && sbi_col % 2 != 0)))) {
3149
724
              tmp_sad = cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y,
3150
724
                                               last_src_ystride);
3151
724
              avg_sad += tmp_sad;
3152
724
              num_samples++;
3153
724
              if (tmp_sad == 0) num_zero_temp_sad++;
3154
724
            }
3155
44.4k
            src_y += 64;
3156
44.4k
            last_src_y += 64;
3157
44.4k
          }
3158
36.4k
          src_y += (src_ystride << 6) - (sb_cols << 6);
3159
36.4k
          last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
3160
36.4k
        }
3161
34.2k
        if (num_samples > 0) avg_sad = avg_sad / num_samples;
3162
        // Set high_source_sad flag if we detect very high increase in avg_sad
3163
        // between current and previous frame value(s). Use minimum threshold
3164
        // for cases where there is small change from content that is completely
3165
        // static.
3166
34.2k
        if (lagframe_idx == 0) {
3167
0
          if (avg_sad >
3168
0
                  VPXMAX(min_thresh,
3169
0
                         (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
3170
0
              rc->frames_since_key > 1 + cpi->svc.number_spatial_layers &&
3171
0
              num_zero_temp_sad < 3 * (num_samples >> 2))
3172
0
            rc->high_source_sad = 1;
3173
0
          else
3174
0
            rc->high_source_sad = 0;
3175
0
          if (rc->high_source_sad && avg_sad > thresh_key)
3176
0
            scene_cut_force_key_frame = 1;
3177
0
          if (avg_sad > 0 || cpi->oxcf.rc_mode == VPX_CBR)
3178
0
            rc->avg_source_sad[0] = (3 * rc->avg_source_sad[0] + avg_sad) >> 2;
3179
34.2k
        } else {
3180
34.2k
          rc->avg_source_sad[lagframe_idx] = avg_sad;
3181
34.2k
        }
3182
34.2k
        if (num_zero_temp_sad < (3 * num_samples >> 2))
3183
109
          rc->high_num_blocks_with_motion = 1;
3184
34.2k
      }
3185
62.9k
    }
3186
    // For CBR non-screen content mode, check if we should reset the rate
3187
    // control. Reset is done if high_source_sad is detected and the rate
3188
    // control is at very low QP with rate correction factor at min level.
3189
27.1k
    if (cpi->oxcf.rc_mode == VPX_CBR &&
3190
27.1k
        cpi->oxcf.content != VP9E_CONTENT_SCREEN && !cpi->use_svc) {
3191
0
      if (rc->high_source_sad && rc->last_q[INTER_FRAME] == rc->best_quality &&
3192
0
          rc->avg_frame_qindex[INTER_FRAME] < (rc->best_quality << 1) &&
3193
0
          rc->rate_correction_factors[INTER_NORMAL] == MIN_BPB_FACTOR) {
3194
0
        rc->rate_correction_factors[INTER_NORMAL] = 0.5;
3195
0
        rc->avg_frame_qindex[INTER_FRAME] = rc->worst_quality;
3196
0
        rc->buffer_level = rc->optimal_buffer_level;
3197
0
        rc->bits_off_target = rc->optimal_buffer_level;
3198
0
        rc->reset_high_source_sad = 1;
3199
0
      }
3200
0
      if (cm->frame_type != KEY_FRAME && rc->reset_high_source_sad)
3201
0
        rc->this_frame_target = rc->avg_frame_bandwidth;
3202
0
    }
3203
    // For SVC the new (updated) avg_source_sad[0] for the current superframe
3204
    // updates the setting for all layers.
3205
27.1k
    if (cpi->use_svc) {
3206
0
      int sl, tl;
3207
0
      SVC *const svc = &cpi->svc;
3208
0
      for (sl = 0; sl < svc->number_spatial_layers; ++sl)
3209
0
        for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
3210
0
          int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3211
0
          LAYER_CONTEXT *const lc = &svc->layer_context[layer];
3212
0
          RATE_CONTROL *const lrc = &lc->rc;
3213
0
          lrc->avg_source_sad[0] = rc->avg_source_sad[0];
3214
0
        }
3215
0
    }
3216
    // For VBR, under scene change/high content change, force golden refresh.
3217
27.1k
    if (cpi->oxcf.rc_mode == VPX_VBR && cm->frame_type != KEY_FRAME &&
3218
27.1k
        rc->high_source_sad && rc->frames_to_key > 3 &&
3219
27.1k
        rc->count_last_scene_change > 4 &&
3220
27.1k
        cpi->ext_refresh_frame_flags_pending == 0) {
3221
0
      int target;
3222
0
      cpi->refresh_golden_frame = 1;
3223
0
      if (scene_cut_force_key_frame) cm->frame_type = KEY_FRAME;
3224
0
      rc->source_alt_ref_pending = 0;
3225
0
      if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf)
3226
0
        rc->source_alt_ref_pending = 1;
3227
0
      rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
3228
0
      rc->baseline_gf_interval =
3229
0
          VPXMIN(20, VPXMAX(10, rc->baseline_gf_interval));
3230
0
      adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
3231
0
      rc->frames_till_gf_update_due = rc->baseline_gf_interval;
3232
0
      target = vp9_calc_pframe_target_size_one_pass_vbr(cpi);
3233
0
      vp9_rc_set_frame_target(cpi, target);
3234
0
      rc->count_last_scene_change = 0;
3235
27.1k
    } else {
3236
27.1k
      rc->count_last_scene_change++;
3237
27.1k
    }
3238
    // If lag_in_frame is used, set the gf boost and interval.
3239
27.1k
    if (cpi->oxcf.lag_in_frames > 0)
3240
27.1k
      adjust_gf_boost_lag_one_pass_vbr(cpi, avg_sad_current);
3241
27.1k
  }
3242
27.1k
}
3243
3244
// Test if encoded frame will significantly overshoot the target bitrate, and
3245
// if so, set the QP, reset/adjust some rate control parameters, and return 1.
3246
// frame_size = -1 means frame has not been encoded.
3247
0
int vp9_encodedframe_overshoot(VP9_COMP *cpi, int frame_size, int *q) {
3248
0
  VP9_COMMON *const cm = &cpi->common;
3249
0
  RATE_CONTROL *const rc = &cpi->rc;
3250
0
  SPEED_FEATURES *const sf = &cpi->sf;
3251
0
  int thresh_qp = 7 * (rc->worst_quality >> 3);
3252
0
  int thresh_rate = rc->avg_frame_bandwidth << 3;
3253
  // Lower thresh_qp for video (more overshoot at lower Q) to be
3254
  // more conservative for video.
3255
0
  if (cpi->oxcf.content != VP9E_CONTENT_SCREEN)
3256
0
    thresh_qp = 3 * (rc->worst_quality >> 2);
3257
  // If this decision is not based on an encoded frame size but just on
3258
  // scene/slide change detection (i.e., re_encode_overshoot_cbr_rt ==
3259
  // FAST_DETECTION_MAXQ), for now skip the (frame_size > thresh_rate)
3260
  // condition in this case.
3261
  // TODO(marpan): Use a better size/rate condition for this case and
3262
  // adjust thresholds.
3263
0
  if ((sf->overshoot_detection_cbr_rt == FAST_DETECTION_MAXQ ||
3264
0
       frame_size > thresh_rate) &&
3265
0
      cm->base_qindex < thresh_qp) {
3266
0
    double rate_correction_factor =
3267
0
        cpi->rc.rate_correction_factors[INTER_NORMAL];
3268
0
    const int target_size = cpi->rc.avg_frame_bandwidth;
3269
0
    const uint64_t sad_thr = 64 * 64 * 32;
3270
0
    int force_maxqp = 1;
3271
0
    double new_correction_factor;
3272
0
    int target_bits_per_mb;
3273
0
    double q2;
3274
0
    int enumerator;
3275
    // Set a larger QP.
3276
0
    if (cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
3277
0
        (rc->buffer_level > (3 * rc->optimal_buffer_level) >> 2) &&
3278
0
        (cpi->rc.avg_source_sad[0] < sad_thr)) {
3279
0
      *q = (*q + cpi->rc.worst_quality) >> 1;
3280
0
      force_maxqp = 0;
3281
0
    } else {
3282
0
      *q = cpi->rc.worst_quality;
3283
0
    }
3284
0
    cpi->cyclic_refresh->counter_encode_maxq_scene_change = 0;
3285
0
    cpi->rc.re_encode_maxq_scene_change = 1;
3286
    // If the frame_size is much larger than the threshold (big content change)
3287
    // and the encoded frame used alot of Intra modes, then force hybrid_intra
3288
    // encoding for the re-encode on this scene change. hybrid_intra will
3289
    // use rd-based intra mode selection for small blocks.
3290
0
    if (sf->overshoot_detection_cbr_rt == RE_ENCODE_MAXQ &&
3291
0
        frame_size > (thresh_rate << 1) && cpi->svc.spatial_layer_id == 0) {
3292
0
      MODE_INFO **mi = cm->mi_grid_visible;
3293
0
      int sum_intra_usage = 0;
3294
0
      int mi_row, mi_col;
3295
0
      for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
3296
0
        for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
3297
0
          if (mi[0]->ref_frame[0] == INTRA_FRAME) sum_intra_usage++;
3298
0
          mi++;
3299
0
        }
3300
0
        mi += 8;
3301
0
      }
3302
0
      sum_intra_usage = 100 * sum_intra_usage / (cm->mi_rows * cm->mi_cols);
3303
0
      if (sum_intra_usage > 60) cpi->rc.hybrid_intra_scene_change = 1;
3304
0
    }
3305
    // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
3306
    // these parameters will affect QP selection for subsequent frames. If they
3307
    // have settled down to a very different (low QP) state, then not adjusting
3308
    // them may cause next frame to select low QP and overshoot again.
3309
0
    cpi->rc.avg_frame_qindex[INTER_FRAME] = *q;
3310
0
    rc->buffer_level = rc->optimal_buffer_level;
3311
0
    rc->bits_off_target = rc->optimal_buffer_level;
3312
    // Reset rate under/over-shoot flags.
3313
0
    cpi->rc.rc_1_frame = 0;
3314
0
    cpi->rc.rc_2_frame = 0;
3315
    // Adjust rate correction factor.
3316
0
    target_bits_per_mb =
3317
0
        (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->MBs);
3318
    // Rate correction factor based on target_bits_per_mb and qp (==max_QP).
3319
    // This comes from the inverse computation of vp9_rc_bits_per_mb().
3320
0
    q2 = vp9_convert_qindex_to_q(*q, cm->bit_depth);
3321
0
    enumerator = 1800000;  // Factor for inter frame.
3322
0
    enumerator += (int)(enumerator * q2) >> 12;
3323
0
    new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
3324
0
    if (new_correction_factor > rate_correction_factor) {
3325
0
      rate_correction_factor =
3326
0
          VPXMIN(2.0 * rate_correction_factor, new_correction_factor);
3327
0
      if (rate_correction_factor > MAX_BPB_FACTOR)
3328
0
        rate_correction_factor = MAX_BPB_FACTOR;
3329
0
      cpi->rc.rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
3330
0
    }
3331
    // For temporal layers, reset the rate control parametes across all
3332
    // temporal layers.
3333
    // If the first_spatial_layer_to_encode > 0, then this superframe has
3334
    // skipped lower base layers. So in this case we should also reset and
3335
    // force max-q for spatial layers < first_spatial_layer_to_encode.
3336
    // For the case of no inter-layer prediction on delta frames: reset and
3337
    // force max-q for all spatial layers, to avoid excessive frame drops.
3338
0
    if (cpi->use_svc) {
3339
0
      int tl = 0;
3340
0
      int sl = 0;
3341
0
      SVC *svc = &cpi->svc;
3342
0
      int num_spatial_layers = VPXMAX(1, svc->first_spatial_layer_to_encode);
3343
0
      if (svc->disable_inter_layer_pred != INTER_LAYER_PRED_ON)
3344
0
        num_spatial_layers = svc->number_spatial_layers;
3345
0
      for (sl = 0; sl < num_spatial_layers; ++sl) {
3346
0
        for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
3347
0
          const int layer =
3348
0
              LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3349
0
          LAYER_CONTEXT *lc = &svc->layer_context[layer];
3350
0
          RATE_CONTROL *lrc = &lc->rc;
3351
0
          lrc->avg_frame_qindex[INTER_FRAME] = *q;
3352
0
          lrc->buffer_level = lrc->optimal_buffer_level;
3353
0
          lrc->bits_off_target = lrc->optimal_buffer_level;
3354
0
          lrc->rc_1_frame = 0;
3355
0
          lrc->rc_2_frame = 0;
3356
0
          lrc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
3357
0
          lrc->force_max_q = force_maxqp;
3358
0
        }
3359
0
      }
3360
0
    }
3361
0
    return 1;
3362
0
  } else {
3363
0
    return 0;
3364
0
  }
3365
0
}