blob: bcb5ab8c741f2802af2f9c4b7a33209c23c74406 [file] [log] [blame]
Jingning Han3ee6db62015-08-05 19:00:31 -07001/*
Adrian Grangea872b062016-03-24 11:38:32 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han3ee6db62015-08-05 19:00:31 -07003 *
Adrian Grangea872b062016-03-24 11:38:32 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han3ee6db62015-08-05 19:00:31 -070010 */
11
12#include <assert.h>
13#include <limits.h>
14#include <math.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
Adrian Grangecebe6f02016-03-25 12:11:05 -070019#include "aom_dsp/aom_dsp_common.h"
20#include "aom_mem/aom_mem.h"
Yaowu Xubf4202e2016-03-21 15:15:19 -070021#include "aom_ports/mem.h"
22#include "aom_ports/system_state.h"
Jingning Han3ee6db62015-08-05 19:00:31 -070023
Yaowu Xucfea7dd2016-03-22 09:52:13 -070024#include "av1/common/alloccommon.h"
25#include "av1/encoder/aq_cyclicrefresh.h"
26#include "av1/common/common.h"
27#include "av1/common/entropymode.h"
28#include "av1/common/quant_common.h"
29#include "av1/common/seg_common.h"
Jingning Han3ee6db62015-08-05 19:00:31 -070030
Yaowu Xucfea7dd2016-03-22 09:52:13 -070031#include "av1/encoder/encodemv.h"
32#include "av1/encoder/ratectrl.h"
Jingning Han3ee6db62015-08-05 19:00:31 -070033
34// Max rate target for 1080P and below encodes under normal circumstances
35// (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
36#define MAX_MB_RATE 250
37#define MAXRATE_1080P 2025000
38
39#define DEFAULT_KF_BOOST 2000
40#define DEFAULT_GF_BOOST 2000
41
42#define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
43
44#define MIN_BPB_FACTOR 0.005
45#define MAX_BPB_FACTOR 50
46
47#define FRAME_OVERHEAD_BITS 200
48
Yaowu Xu3246fc02016-01-20 16:13:04 -080049#if CONFIG_VPX_HIGHBITDEPTH
clang-format99e28b82016-01-27 12:42:45 -080050#define ASSIGN_MINQ_TABLE(bit_depth, name) \
51 do { \
52 switch (bit_depth) { \
53 case VPX_BITS_8: name = name##_8; break; \
54 case VPX_BITS_10: name = name##_10; break; \
55 case VPX_BITS_12: name = name##_12; break; \
56 default: \
57 assert(0 && \
58 "bit_depth should be VPX_BITS_8, VPX_BITS_10" \
59 " or VPX_BITS_12"); \
60 name = NULL; \
61 } \
Jingning Han3ee6db62015-08-05 19:00:31 -070062 } while (0)
63#else
64#define ASSIGN_MINQ_TABLE(bit_depth, name) \
clang-format99e28b82016-01-27 12:42:45 -080065 do { \
66 (void) bit_depth; \
67 name = name##_8; \
Jingning Han3ee6db62015-08-05 19:00:31 -070068 } while (0)
69#endif
70
71// Tables relating active max Q to active min Q
72static int kf_low_motion_minq_8[QINDEX_RANGE];
73static int kf_high_motion_minq_8[QINDEX_RANGE];
74static int arfgf_low_motion_minq_8[QINDEX_RANGE];
75static int arfgf_high_motion_minq_8[QINDEX_RANGE];
76static int inter_minq_8[QINDEX_RANGE];
77static int rtc_minq_8[QINDEX_RANGE];
78
Yaowu Xu3246fc02016-01-20 16:13:04 -080079#if CONFIG_VPX_HIGHBITDEPTH
Jingning Han3ee6db62015-08-05 19:00:31 -070080static int kf_low_motion_minq_10[QINDEX_RANGE];
81static int kf_high_motion_minq_10[QINDEX_RANGE];
82static int arfgf_low_motion_minq_10[QINDEX_RANGE];
83static int arfgf_high_motion_minq_10[QINDEX_RANGE];
84static int inter_minq_10[QINDEX_RANGE];
85static int rtc_minq_10[QINDEX_RANGE];
86static int kf_low_motion_minq_12[QINDEX_RANGE];
87static int kf_high_motion_minq_12[QINDEX_RANGE];
88static int arfgf_low_motion_minq_12[QINDEX_RANGE];
89static int arfgf_high_motion_minq_12[QINDEX_RANGE];
90static int inter_minq_12[QINDEX_RANGE];
91static int rtc_minq_12[QINDEX_RANGE];
92#endif
93
94static int gf_high = 2000;
95static int gf_low = 400;
96static int kf_high = 5000;
97static int kf_low = 400;
98
99// Functions to compute the active minq lookup table entries based on a
100// formulaic approach to facilitate easier adjustment of the Q tables.
101// The formulae were derived from computing a 3rd order polynomial best
102// fit to the original data (after plotting real maxq vs minq (not q index))
103static int get_minq_index(double maxq, double x3, double x2, double x1,
Adrian Grangecebe6f02016-03-25 12:11:05 -0700104 aom_bit_depth_t bit_depth) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700105 int i;
James Zern5e16d392015-08-17 18:19:22 -0700106 const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
Jingning Han3ee6db62015-08-05 19:00:31 -0700107
108 // Special case handling to deal with the step from q2.0
109 // down to lossless mode represented by q 1.0.
clang-format99e28b82016-01-27 12:42:45 -0800110 if (minqtarget <= 2.0) return 0;
Jingning Han3ee6db62015-08-05 19:00:31 -0700111
112 for (i = 0; i < QINDEX_RANGE; i++) {
clang-format99e28b82016-01-27 12:42:45 -0800113 if (minqtarget <= vp10_convert_qindex_to_q(i, bit_depth)) return i;
Jingning Han3ee6db62015-08-05 19:00:31 -0700114 }
115
116 return QINDEX_RANGE - 1;
117}
118
clang-format99e28b82016-01-27 12:42:45 -0800119static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
120 int *arfgf_high, int *inter, int *rtc,
Adrian Grangecebe6f02016-03-25 12:11:05 -0700121 aom_bit_depth_t bit_depth) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700122 int i;
123 for (i = 0; i < QINDEX_RANGE; i++) {
124 const double maxq = vp10_convert_qindex_to_q(i, bit_depth);
125 kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
126 kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
127 arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
128 arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
129 inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth);
130 rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
131 }
132}
133
134void vp10_rc_init_minq_luts(void) {
135 init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
136 arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
137 inter_minq_8, rtc_minq_8, VPX_BITS_8);
Yaowu Xu3246fc02016-01-20 16:13:04 -0800138#if CONFIG_VPX_HIGHBITDEPTH
Jingning Han3ee6db62015-08-05 19:00:31 -0700139 init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
140 arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
141 inter_minq_10, rtc_minq_10, VPX_BITS_10);
142 init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
143 arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
144 inter_minq_12, rtc_minq_12, VPX_BITS_12);
145#endif
146}
147
148// These functions use formulaic calculations to make playing with the
149// quantizer tables easier. If necessary they can be replaced by lookup
150// tables if and when things settle down in the experimental bitstream
Adrian Grangecebe6f02016-03-25 12:11:05 -0700151double vp10_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth) {
clang-format99e28b82016-01-27 12:42:45 -0800152// Convert the index to a real Q value (scaled down to match old Q values)
Yaowu Xu3246fc02016-01-20 16:13:04 -0800153#if CONFIG_VPX_HIGHBITDEPTH
Jingning Han3ee6db62015-08-05 19:00:31 -0700154 switch (bit_depth) {
clang-format99e28b82016-01-27 12:42:45 -0800155 case VPX_BITS_8: return vp10_ac_quant(qindex, 0, bit_depth) / 4.0;
156 case VPX_BITS_10: return vp10_ac_quant(qindex, 0, bit_depth) / 16.0;
157 case VPX_BITS_12: return vp10_ac_quant(qindex, 0, bit_depth) / 64.0;
Jingning Han3ee6db62015-08-05 19:00:31 -0700158 default:
159 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
160 return -1.0;
161 }
162#else
163 return vp10_ac_quant(qindex, 0, bit_depth) / 4.0;
164#endif
165}
166
167int vp10_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
Adrian Grangecebe6f02016-03-25 12:11:05 -0700168 double correction_factor, aom_bit_depth_t bit_depth) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700169 const double q = vp10_convert_qindex_to_q(qindex, bit_depth);
170 int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
171
172 assert(correction_factor <= MAX_BPB_FACTOR &&
173 correction_factor >= MIN_BPB_FACTOR);
174
175 // q based adjustment to baseline enumerator
176 enumerator += (int)(enumerator * q) >> 12;
177 return (int)(enumerator * correction_factor / q);
178}
179
180int vp10_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
clang-format99e28b82016-01-27 12:42:45 -0800181 double correction_factor,
Adrian Grangecebe6f02016-03-25 12:11:05 -0700182 aom_bit_depth_t bit_depth) {
clang-format99e28b82016-01-27 12:42:45 -0800183 const int bpm =
184 (int)(vp10_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth));
James Zern5e16d392015-08-17 18:19:22 -0700185 return VPXMAX(FRAME_OVERHEAD_BITS,
186 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
Jingning Han3ee6db62015-08-05 19:00:31 -0700187}
188
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700189int vp10_rc_clamp_pframe_target_size(const VP10_COMP *const cpi, int target) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700190 const RATE_CONTROL *rc = &cpi->rc;
hui sua4c7e922015-08-16 17:24:35 -0700191 const VP10EncoderConfig *oxcf = &cpi->oxcf;
clang-format99e28b82016-01-27 12:42:45 -0800192 const int min_frame_target =
193 VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
194 if (target < min_frame_target) target = min_frame_target;
Jingning Han3ee6db62015-08-05 19:00:31 -0700195 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
196 // If there is an active ARF at this location use the minimum
197 // bits on this frame even if it is a constructed arf.
198 // The active maximum quantizer insures that an appropriate
199 // number of bits will be spent if needed for constructed ARFs.
200 target = min_frame_target;
201 }
202 // Clip the frame target to the maximum allowed value.
clang-format99e28b82016-01-27 12:42:45 -0800203 if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
Jingning Han3ee6db62015-08-05 19:00:31 -0700204 if (oxcf->rc_max_inter_bitrate_pct) {
clang-format99e28b82016-01-27 12:42:45 -0800205 const int max_rate =
206 rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
James Zern5e16d392015-08-17 18:19:22 -0700207 target = VPXMIN(target, max_rate);
Jingning Han3ee6db62015-08-05 19:00:31 -0700208 }
209 return target;
210}
211
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700212int vp10_rc_clamp_iframe_target_size(const VP10_COMP *const cpi, int target) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700213 const RATE_CONTROL *rc = &cpi->rc;
hui sua4c7e922015-08-16 17:24:35 -0700214 const VP10EncoderConfig *oxcf = &cpi->oxcf;
Jingning Han3ee6db62015-08-05 19:00:31 -0700215 if (oxcf->rc_max_intra_bitrate_pct) {
clang-format99e28b82016-01-27 12:42:45 -0800216 const int max_rate =
217 rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
James Zern5e16d392015-08-17 18:19:22 -0700218 target = VPXMIN(target, max_rate);
Jingning Han3ee6db62015-08-05 19:00:31 -0700219 }
clang-format99e28b82016-01-27 12:42:45 -0800220 if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
Jingning Han3ee6db62015-08-05 19:00:31 -0700221 return target;
222}
223
Jingning Han3ee6db62015-08-05 19:00:31 -0700224// Update the buffer level: leaky bucket model.
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700225static void update_buffer_level(VP10_COMP *cpi, int encoded_frame_size) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700226 const VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -0700227 RATE_CONTROL *const rc = &cpi->rc;
228
229 // Non-viewable frames are a special case and are treated as pure overhead.
230 if (!cm->show_frame) {
231 rc->bits_off_target -= encoded_frame_size;
232 } else {
233 rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
234 }
235
236 // Clip the buffer level to the maximum specified buffer size.
James Zern5e16d392015-08-17 18:19:22 -0700237 rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
Jingning Han3ee6db62015-08-05 19:00:31 -0700238 rc->buffer_level = rc->bits_off_target;
Jingning Han3ee6db62015-08-05 19:00:31 -0700239}
240
clang-format99e28b82016-01-27 12:42:45 -0800241int vp10_rc_get_default_min_gf_interval(int width, int height,
242 double framerate) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700243 // Assume we do not need any constraint lower than 4K 20 fps
244 static const double factor_safe = 3840 * 2160 * 20.0;
245 const double factor = width * height * framerate;
246 const int default_interval =
247 clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
248
249 if (factor <= factor_safe)
250 return default_interval;
251 else
James Zern5e16d392015-08-17 18:19:22 -0700252 return VPXMAX(default_interval,
253 (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
Jingning Han3ee6db62015-08-05 19:00:31 -0700254 // Note this logic makes:
255 // 4K24: 5
256 // 4K30: 6
257 // 4K60: 12
258}
259
260int vp10_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
James Zern5e16d392015-08-17 18:19:22 -0700261 int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
Jingning Han3ee6db62015-08-05 19:00:31 -0700262 interval += (interval & 0x01); // Round to even value
James Zern5e16d392015-08-17 18:19:22 -0700263 return VPXMAX(interval, min_gf_interval);
Jingning Han3ee6db62015-08-05 19:00:31 -0700264}
265
hui sua4c7e922015-08-16 17:24:35 -0700266void vp10_rc_init(const VP10EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700267 int i;
268
269 if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
270 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
271 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
272 } else {
clang-format99e28b82016-01-27 12:42:45 -0800273 rc->avg_frame_qindex[KEY_FRAME] =
274 (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
275 rc->avg_frame_qindex[INTER_FRAME] =
276 (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
Jingning Han3ee6db62015-08-05 19:00:31 -0700277 }
278
279 rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
280 rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
281
clang-format99e28b82016-01-27 12:42:45 -0800282 rc->buffer_level = rc->starting_buffer_level;
Jingning Han3ee6db62015-08-05 19:00:31 -0700283 rc->bits_off_target = rc->starting_buffer_level;
284
clang-format99e28b82016-01-27 12:42:45 -0800285 rc->rolling_target_bits = rc->avg_frame_bandwidth;
286 rc->rolling_actual_bits = rc->avg_frame_bandwidth;
Jingning Han3ee6db62015-08-05 19:00:31 -0700287 rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
288 rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
289
290 rc->total_actual_bits = 0;
291 rc->total_target_bits = 0;
292 rc->total_target_vs_actual = 0;
293
294 rc->frames_since_key = 8; // Sensible default for first frame.
295 rc->this_key_frame_forced = 0;
296 rc->next_key_frame_forced = 0;
297 rc->source_alt_ref_pending = 0;
298 rc->source_alt_ref_active = 0;
299
300 rc->frames_till_gf_update_due = 0;
301 rc->ni_av_qi = oxcf->worst_allowed_q;
302 rc->ni_tot_qi = 0;
303 rc->ni_frames = 0;
304
305 rc->tot_q = 0.0;
306 rc->avg_q = vp10_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
307
308 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
309 rc->rate_correction_factors[i] = 1.0;
310 }
311
312 rc->min_gf_interval = oxcf->min_gf_interval;
313 rc->max_gf_interval = oxcf->max_gf_interval;
314 if (rc->min_gf_interval == 0)
315 rc->min_gf_interval = vp10_rc_get_default_min_gf_interval(
316 oxcf->width, oxcf->height, oxcf->init_framerate);
317 if (rc->max_gf_interval == 0)
318 rc->max_gf_interval = vp10_rc_get_default_max_gf_interval(
319 oxcf->init_framerate, rc->min_gf_interval);
320 rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
321}
322
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700323int vp10_rc_drop_frame(VP10_COMP *cpi) {
hui sua4c7e922015-08-16 17:24:35 -0700324 const VP10EncoderConfig *oxcf = &cpi->oxcf;
Jingning Han3ee6db62015-08-05 19:00:31 -0700325 RATE_CONTROL *const rc = &cpi->rc;
326
327 if (!oxcf->drop_frames_water_mark) {
328 return 0;
329 } else {
330 if (rc->buffer_level < 0) {
331 // Always drop if buffer is below 0.
332 return 1;
333 } else {
334 // If buffer is below drop_mark, for now just drop every other frame
335 // (starting with the next frame) until it increases back over drop_mark.
clang-format99e28b82016-01-27 12:42:45 -0800336 int drop_mark =
337 (int)(oxcf->drop_frames_water_mark * rc->optimal_buffer_level / 100);
338 if ((rc->buffer_level > drop_mark) && (rc->decimation_factor > 0)) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700339 --rc->decimation_factor;
clang-format99e28b82016-01-27 12:42:45 -0800340 } else if (rc->buffer_level <= drop_mark && rc->decimation_factor == 0) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700341 rc->decimation_factor = 1;
342 }
343 if (rc->decimation_factor > 0) {
344 if (rc->decimation_count > 0) {
345 --rc->decimation_count;
346 return 1;
347 } else {
348 rc->decimation_count = rc->decimation_factor;
349 return 0;
350 }
351 } else {
352 rc->decimation_count = 0;
353 return 0;
354 }
355 }
356 }
357}
358
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700359static double get_rate_correction_factor(const VP10_COMP *cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700360 const RATE_CONTROL *const rc = &cpi->rc;
361 double rcf;
362
363 if (cpi->common.frame_type == KEY_FRAME) {
364 rcf = rc->rate_correction_factors[KF_STD];
365 } else if (cpi->oxcf.pass == 2) {
366 RATE_FACTOR_LEVEL rf_lvl =
clang-format99e28b82016-01-27 12:42:45 -0800367 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
Jingning Han3ee6db62015-08-05 19:00:31 -0700368 rcf = rc->rate_correction_factors[rf_lvl];
369 } else {
370 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
Yunqing Wangc147c4d2015-08-27 15:11:38 -0700371 !rc->is_src_frame_alt_ref &&
Jingning Han3ee6db62015-08-05 19:00:31 -0700372 (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
373 rcf = rc->rate_correction_factors[GF_ARF_STD];
374 else
375 rcf = rc->rate_correction_factors[INTER_NORMAL];
376 }
377 rcf *= rcf_mult[rc->frame_size_selector];
378 return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
379}
380
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700381static void set_rate_correction_factor(VP10_COMP *cpi, double factor) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700382 RATE_CONTROL *const rc = &cpi->rc;
383
384 // Normalize RCF to account for the size-dependent scaling factor.
385 factor /= rcf_mult[cpi->rc.frame_size_selector];
386
387 factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
388
389 if (cpi->common.frame_type == KEY_FRAME) {
390 rc->rate_correction_factors[KF_STD] = factor;
391 } else if (cpi->oxcf.pass == 2) {
392 RATE_FACTOR_LEVEL rf_lvl =
clang-format99e28b82016-01-27 12:42:45 -0800393 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
Jingning Han3ee6db62015-08-05 19:00:31 -0700394 rc->rate_correction_factors[rf_lvl] = factor;
395 } else {
396 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
Yunqing Wangc147c4d2015-08-27 15:11:38 -0700397 !rc->is_src_frame_alt_ref &&
Jingning Han3ee6db62015-08-05 19:00:31 -0700398 (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
399 rc->rate_correction_factors[GF_ARF_STD] = factor;
400 else
401 rc->rate_correction_factors[INTER_NORMAL] = factor;
402 }
403}
404
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700405void vp10_rc_update_rate_correction_factors(VP10_COMP *cpi) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700406 const VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -0700407 int correction_factor = 100;
408 double rate_correction_factor = get_rate_correction_factor(cpi);
409 double adjustment_limit;
410
411 int projected_size_based_on_q = 0;
412
413 // Do not update the rate factors for arf overlay frames.
clang-format99e28b82016-01-27 12:42:45 -0800414 if (cpi->rc.is_src_frame_alt_ref) return;
Jingning Han3ee6db62015-08-05 19:00:31 -0700415
416 // Clear down mmx registers to allow floating point in what follows
Adrian Grangecebe6f02016-03-25 12:11:05 -0700417 aom_clear_system_state();
Jingning Han3ee6db62015-08-05 19:00:31 -0700418
419 // Work out how big we would have expected the frame to be at this Q given
420 // the current correction factor.
421 // Stay in double to avoid int overflow when values are large
422 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
423 projected_size_based_on_q =
424 vp10_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
425 } else {
clang-format99e28b82016-01-27 12:42:45 -0800426 projected_size_based_on_q =
427 vp10_estimate_bits_at_q(cpi->common.frame_type, cm->base_qindex,
428 cm->MBs, rate_correction_factor, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700429 }
430 // Work out a size correction factor.
431 if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
432 correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
clang-format99e28b82016-01-27 12:42:45 -0800433 projected_size_based_on_q);
Jingning Han3ee6db62015-08-05 19:00:31 -0700434
435 // More heavily damped adjustment used if we have been oscillating either side
436 // of target.
clang-format99e28b82016-01-27 12:42:45 -0800437 adjustment_limit =
438 0.25 + 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
Jingning Han3ee6db62015-08-05 19:00:31 -0700439
440 cpi->rc.q_2_frame = cpi->rc.q_1_frame;
441 cpi->rc.q_1_frame = cm->base_qindex;
442 cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
443 if (correction_factor > 110)
444 cpi->rc.rc_1_frame = -1;
445 else if (correction_factor < 90)
446 cpi->rc.rc_1_frame = 1;
447 else
448 cpi->rc.rc_1_frame = 0;
449
450 if (correction_factor > 102) {
451 // We are not already at the worst allowable quality
clang-format99e28b82016-01-27 12:42:45 -0800452 correction_factor =
453 (int)(100 + ((correction_factor - 100) * adjustment_limit));
Jingning Han3ee6db62015-08-05 19:00:31 -0700454 rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
455 // Keep rate_correction_factor within limits
456 if (rate_correction_factor > MAX_BPB_FACTOR)
457 rate_correction_factor = MAX_BPB_FACTOR;
458 } else if (correction_factor < 99) {
459 // We are not already at the best allowable quality
clang-format99e28b82016-01-27 12:42:45 -0800460 correction_factor =
461 (int)(100 - ((100 - correction_factor) * adjustment_limit));
Jingning Han3ee6db62015-08-05 19:00:31 -0700462 rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
463
464 // Keep rate_correction_factor within limits
465 if (rate_correction_factor < MIN_BPB_FACTOR)
466 rate_correction_factor = MIN_BPB_FACTOR;
467 }
468
469 set_rate_correction_factor(cpi, rate_correction_factor);
470}
471
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700472int vp10_rc_regulate_q(const VP10_COMP *cpi, int target_bits_per_frame,
clang-format99e28b82016-01-27 12:42:45 -0800473 int active_best_quality, int active_worst_quality) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700474 const VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -0700475 int q = active_worst_quality;
476 int last_error = INT_MAX;
477 int i, target_bits_per_mb, bits_per_mb_at_this_q;
478 const double correction_factor = get_rate_correction_factor(cpi);
479
480 // Calculate required scaling factor based on target frame size and size of
481 // frame produced using previous Q.
482 target_bits_per_mb =
483 ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
484
485 i = active_best_quality;
486
487 do {
Yunqing Wangc147c4d2015-08-27 15:11:38 -0700488 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700489 bits_per_mb_at_this_q =
490 (int)vp10_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
491 } else {
clang-format99e28b82016-01-27 12:42:45 -0800492 bits_per_mb_at_this_q = (int)vp10_rc_bits_per_mb(
493 cm->frame_type, i, correction_factor, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700494 }
495
496 if (bits_per_mb_at_this_q <= target_bits_per_mb) {
497 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
498 q = i;
499 else
500 q = i - 1;
501
502 break;
503 } else {
504 last_error = bits_per_mb_at_this_q - target_bits_per_mb;
505 }
506 } while (++i <= active_worst_quality);
507
508 // In CBR mode, this makes sure q is between oscillating Qs to prevent
509 // resonance.
510 if (cpi->oxcf.rc_mode == VPX_CBR &&
511 (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
512 cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
James Zern5e16d392015-08-17 18:19:22 -0700513 q = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
514 VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
Jingning Han3ee6db62015-08-05 19:00:31 -0700515 }
516 return q;
517}
518
519static int get_active_quality(int q, int gfu_boost, int low, int high,
520 int *low_motion_minq, int *high_motion_minq) {
521 if (gfu_boost > high) {
522 return low_motion_minq[q];
523 } else if (gfu_boost < low) {
524 return high_motion_minq[q];
525 } else {
526 const int gap = high - low;
527 const int offset = high - gfu_boost;
528 const int qdiff = high_motion_minq[q] - low_motion_minq[q];
529 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
530 return low_motion_minq[q] + adjustment;
531 }
532}
533
534static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
Adrian Grangecebe6f02016-03-25 12:11:05 -0700535 aom_bit_depth_t bit_depth) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700536 int *kf_low_motion_minq;
537 int *kf_high_motion_minq;
538 ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
539 ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
540 return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
541 kf_low_motion_minq, kf_high_motion_minq);
542}
543
544static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
Adrian Grangecebe6f02016-03-25 12:11:05 -0700545 aom_bit_depth_t bit_depth) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700546 int *arfgf_low_motion_minq;
547 int *arfgf_high_motion_minq;
548 ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
549 ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
550 return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
551 arfgf_low_motion_minq, arfgf_high_motion_minq);
552}
553
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700554static int calc_active_worst_quality_one_pass_vbr(const VP10_COMP *cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700555 const RATE_CONTROL *const rc = &cpi->rc;
556 const unsigned int curr_frame = cpi->common.current_video_frame;
557 int active_worst_quality;
558
559 if (cpi->common.frame_type == KEY_FRAME) {
clang-format99e28b82016-01-27 12:42:45 -0800560 active_worst_quality =
561 curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] * 2;
Jingning Han3ee6db62015-08-05 19:00:31 -0700562 } else {
563 if (!rc->is_src_frame_alt_ref &&
564 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
clang-format99e28b82016-01-27 12:42:45 -0800565 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
566 : rc->last_q[INTER_FRAME];
Jingning Han3ee6db62015-08-05 19:00:31 -0700567 } else {
568 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
569 : rc->last_q[INTER_FRAME] * 2;
570 }
571 }
James Zern5e16d392015-08-17 18:19:22 -0700572 return VPXMIN(active_worst_quality, rc->worst_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700573}
574
575// Adjust active_worst_quality level based on buffer level.
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700576static int calc_active_worst_quality_one_pass_cbr(const VP10_COMP *cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700577 // Adjust active_worst_quality: If buffer is above the optimal/target level,
578 // bring active_worst_quality down depending on fullness of buffer.
579 // If buffer is below the optimal level, let the active_worst_quality go from
580 // ambient Q (at buffer = optimal level) to worst_quality level
581 // (at buffer = critical level).
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700582 const VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -0700583 const RATE_CONTROL *rc = &cpi->rc;
584 // Buffer level below which we push active_worst to worst_quality.
585 int64_t critical_level = rc->optimal_buffer_level >> 3;
586 int64_t buff_lvl_step = 0;
587 int adjustment = 0;
588 int active_worst_quality;
589 int ambient_qp;
clang-format99e28b82016-01-27 12:42:45 -0800590 if (cm->frame_type == KEY_FRAME) return rc->worst_quality;
Jingning Han3ee6db62015-08-05 19:00:31 -0700591 // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
592 // for the first few frames following key frame. These are both initialized
593 // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
594 // So for first few frames following key, the qp of that key frame is weighted
595 // into the active_worst_quality setting.
clang-format99e28b82016-01-27 12:42:45 -0800596 ambient_qp = (cm->current_video_frame < 5)
597 ? VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
598 rc->avg_frame_qindex[KEY_FRAME])
599 : rc->avg_frame_qindex[INTER_FRAME];
James Zern5e16d392015-08-17 18:19:22 -0700600 active_worst_quality = VPXMIN(rc->worst_quality, ambient_qp * 5 / 4);
Jingning Han3ee6db62015-08-05 19:00:31 -0700601 if (rc->buffer_level > rc->optimal_buffer_level) {
602 // Adjust down.
603 // Maximum limit for down adjustment, ~30%.
604 int max_adjustment_down = active_worst_quality / 3;
605 if (max_adjustment_down) {
clang-format99e28b82016-01-27 12:42:45 -0800606 buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
607 max_adjustment_down);
Jingning Han3ee6db62015-08-05 19:00:31 -0700608 if (buff_lvl_step)
609 adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
clang-format99e28b82016-01-27 12:42:45 -0800610 buff_lvl_step);
Jingning Han3ee6db62015-08-05 19:00:31 -0700611 active_worst_quality -= adjustment;
612 }
613 } else if (rc->buffer_level > critical_level) {
614 // Adjust up from ambient Q.
615 if (critical_level) {
616 buff_lvl_step = (rc->optimal_buffer_level - critical_level);
617 if (buff_lvl_step) {
618 adjustment = (int)((rc->worst_quality - ambient_qp) *
619 (rc->optimal_buffer_level - rc->buffer_level) /
620 buff_lvl_step);
621 }
622 active_worst_quality = ambient_qp + adjustment;
623 }
624 } else {
625 // Set to worst_quality if buffer is below critical level.
626 active_worst_quality = rc->worst_quality;
627 }
628 return active_worst_quality;
629}
630
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700631static int rc_pick_q_and_bounds_one_pass_cbr(const VP10_COMP *cpi,
Jingning Han3ee6db62015-08-05 19:00:31 -0700632 int *bottom_index,
633 int *top_index) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700634 const VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -0700635 const RATE_CONTROL *const rc = &cpi->rc;
636 int active_best_quality;
637 int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
638 int q;
639 int *rtc_minq;
640 ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
641
642 if (frame_is_intra_only(cm)) {
643 active_best_quality = rc->best_quality;
644 // Handle the special case for key frames forced when we have reached
645 // the maximum key frame interval. Here force the Q to a range
646 // based on the ambient Q to reduce the risk of popping.
647 if (rc->this_key_frame_forced) {
648 int qindex = rc->last_boosted_qindex;
649 double last_boosted_q = vp10_convert_qindex_to_q(qindex, cm->bit_depth);
clang-format99e28b82016-01-27 12:42:45 -0800650 int delta_qindex = vp10_compute_qdelta(
651 rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth);
James Zern5e16d392015-08-17 18:19:22 -0700652 active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700653 } else if (cm->current_video_frame > 0) {
654 // not first frame of one pass and kf_boost is set
655 double q_adj_factor = 1.0;
656 double q_val;
657
clang-format99e28b82016-01-27 12:42:45 -0800658 active_best_quality = get_kf_active_quality(
659 rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700660
661 // Allow somewhat lower kf minq with small image formats.
662 if ((cm->width * cm->height) <= (352 * 288)) {
663 q_adj_factor -= 0.25;
664 }
665
666 // Convert the adjustment factor to a qindex delta
667 // on active_best_quality.
668 q_val = vp10_convert_qindex_to_q(active_best_quality, cm->bit_depth);
clang-format99e28b82016-01-27 12:42:45 -0800669 active_best_quality +=
670 vp10_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700671 }
672 } else if (!rc->is_src_frame_alt_ref &&
Jingning Han3ee6db62015-08-05 19:00:31 -0700673 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
674 // Use the lower of active_worst_quality and recent
675 // average Q as basis for GF/ARF best Q limit unless last frame was
676 // a key frame.
677 if (rc->frames_since_key > 1 &&
678 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
679 q = rc->avg_frame_qindex[INTER_FRAME];
680 } else {
681 q = active_worst_quality;
682 }
683 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
684 } else {
685 // Use the lower of active_worst_quality and recent/average Q.
686 if (cm->current_video_frame > 1) {
687 if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
688 active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
689 else
690 active_best_quality = rtc_minq[active_worst_quality];
691 } else {
692 if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
693 active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
694 else
695 active_best_quality = rtc_minq[active_worst_quality];
696 }
697 }
698
699 // Clip the active best and worst quality values to limits
clang-format99e28b82016-01-27 12:42:45 -0800700 active_best_quality =
701 clamp(active_best_quality, rc->best_quality, rc->worst_quality);
702 active_worst_quality =
703 clamp(active_worst_quality, active_best_quality, rc->worst_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700704
705 *top_index = active_worst_quality;
706 *bottom_index = active_best_quality;
707
708#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
709 // Limit Q range for the adaptive loop.
clang-format99e28b82016-01-27 12:42:45 -0800710 if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
Jingning Han3ee6db62015-08-05 19:00:31 -0700711 !(cm->current_video_frame == 0)) {
712 int qdelta = 0;
Adrian Grangecebe6f02016-03-25 12:11:05 -0700713 aom_clear_system_state();
clang-format99e28b82016-01-27 12:42:45 -0800714 qdelta = vp10_compute_qdelta_by_rate(
715 &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700716 *top_index = active_worst_quality + qdelta;
717 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
718 }
719#endif
720
721 // Special case code to try and match quality with forced key frames
722 if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
723 q = rc->last_boosted_qindex;
724 } else {
clang-format99e28b82016-01-27 12:42:45 -0800725 q = vp10_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
726 active_worst_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700727 if (q > *top_index) {
728 // Special case when we are targeting the max allowed rate
729 if (rc->this_frame_target >= rc->max_frame_bandwidth)
730 *top_index = q;
731 else
732 q = *top_index;
733 }
734 }
clang-format99e28b82016-01-27 12:42:45 -0800735 assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700736 assert(*bottom_index <= rc->worst_quality &&
737 *bottom_index >= rc->best_quality);
738 assert(q <= rc->worst_quality && q >= rc->best_quality);
739 return q;
740}
741
742static int get_active_cq_level(const RATE_CONTROL *rc,
hui sua4c7e922015-08-16 17:24:35 -0700743 const VP10EncoderConfig *const oxcf) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700744 static const double cq_adjust_threshold = 0.1;
745 int active_cq_level = oxcf->cq_level;
clang-format99e28b82016-01-27 12:42:45 -0800746 if (oxcf->rc_mode == VPX_CQ && rc->total_target_bits > 0) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700747 const double x = (double)rc->total_actual_bits / rc->total_target_bits;
748 if (x < cq_adjust_threshold) {
749 active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
750 }
751 }
752 return active_cq_level;
753}
754
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700755static int rc_pick_q_and_bounds_one_pass_vbr(const VP10_COMP *cpi,
Jingning Han3ee6db62015-08-05 19:00:31 -0700756 int *bottom_index,
757 int *top_index) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700758 const VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -0700759 const RATE_CONTROL *const rc = &cpi->rc;
hui sua4c7e922015-08-16 17:24:35 -0700760 const VP10EncoderConfig *const oxcf = &cpi->oxcf;
Jingning Han3ee6db62015-08-05 19:00:31 -0700761 const int cq_level = get_active_cq_level(rc, oxcf);
762 int active_best_quality;
763 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
764 int q;
765 int *inter_minq;
766 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
767
768 if (frame_is_intra_only(cm)) {
paulwilkins99309002015-12-15 15:23:47 +0000769 if (oxcf->rc_mode == VPX_Q) {
770 int qindex = cq_level;
771 double q = vp10_convert_qindex_to_q(qindex, cm->bit_depth);
clang-format99e28b82016-01-27 12:42:45 -0800772 int delta_qindex = vp10_compute_qdelta(rc, q, q * 0.25, cm->bit_depth);
paulwilkins99309002015-12-15 15:23:47 +0000773 active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
774 } else if (rc->this_key_frame_forced) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700775 int qindex = rc->last_boosted_qindex;
776 double last_boosted_q = vp10_convert_qindex_to_q(qindex, cm->bit_depth);
clang-format99e28b82016-01-27 12:42:45 -0800777 int delta_qindex = vp10_compute_qdelta(
778 rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
James Zern5e16d392015-08-17 18:19:22 -0700779 active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700780 } else {
781 // not first frame of one pass and kf_boost is set
782 double q_adj_factor = 1.0;
783 double q_val;
784
clang-format99e28b82016-01-27 12:42:45 -0800785 active_best_quality = get_kf_active_quality(
786 rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700787
788 // Allow somewhat lower kf minq with small image formats.
789 if ((cm->width * cm->height) <= (352 * 288)) {
790 q_adj_factor -= 0.25;
791 }
792
793 // Convert the adjustment factor to a qindex delta
794 // on active_best_quality.
795 q_val = vp10_convert_qindex_to_q(active_best_quality, cm->bit_depth);
clang-format99e28b82016-01-27 12:42:45 -0800796 active_best_quality +=
797 vp10_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700798 }
799 } else if (!rc->is_src_frame_alt_ref &&
800 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
801 // Use the lower of active_worst_quality and recent
802 // average Q as basis for GF/ARF best Q limit unless last frame was
803 // a key frame.
804 if (rc->frames_since_key > 1 &&
805 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
806 q = rc->avg_frame_qindex[INTER_FRAME];
807 } else {
808 q = rc->avg_frame_qindex[KEY_FRAME];
809 }
810 // For constrained quality dont allow Q less than the cq level
811 if (oxcf->rc_mode == VPX_CQ) {
clang-format99e28b82016-01-27 12:42:45 -0800812 if (q < cq_level) q = cq_level;
Jingning Han3ee6db62015-08-05 19:00:31 -0700813
814 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
815
816 // Constrained quality use slightly lower active best.
817 active_best_quality = active_best_quality * 15 / 16;
818
819 } else if (oxcf->rc_mode == VPX_Q) {
paulwilkins99309002015-12-15 15:23:47 +0000820 int qindex = cq_level;
821 double q = vp10_convert_qindex_to_q(qindex, cm->bit_depth);
822 int delta_qindex;
823 if (cpi->refresh_alt_ref_frame)
824 delta_qindex = vp10_compute_qdelta(rc, q, q * 0.40, cm->bit_depth);
825 else
826 delta_qindex = vp10_compute_qdelta(rc, q, q * 0.50, cm->bit_depth);
827 active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700828 } else {
829 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
830 }
831 } else {
832 if (oxcf->rc_mode == VPX_Q) {
paulwilkins99309002015-12-15 15:23:47 +0000833 int qindex = cq_level;
834 double q = vp10_convert_qindex_to_q(qindex, cm->bit_depth);
clang-format99e28b82016-01-27 12:42:45 -0800835 double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
836 0.70, 1.0, 0.85, 1.0 };
837 int delta_qindex = vp10_compute_qdelta(
838 rc, q, q * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL],
839 cm->bit_depth);
paulwilkins99309002015-12-15 15:23:47 +0000840 active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700841 } else {
842 // Use the lower of active_worst_quality and recent/average Q.
843 if (cm->current_video_frame > 1)
844 active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
845 else
846 active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
847 // For the constrained quality mode we don't want
848 // q to fall below the cq level.
clang-format99e28b82016-01-27 12:42:45 -0800849 if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700850 active_best_quality = cq_level;
851 }
852 }
853 }
854
855 // Clip the active best and worst quality values to limits
clang-format99e28b82016-01-27 12:42:45 -0800856 active_best_quality =
857 clamp(active_best_quality, rc->best_quality, rc->worst_quality);
858 active_worst_quality =
859 clamp(active_worst_quality, active_best_quality, rc->worst_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700860
861 *top_index = active_worst_quality;
862 *bottom_index = active_best_quality;
863
864#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
865 {
866 int qdelta = 0;
Adrian Grangecebe6f02016-03-25 12:11:05 -0700867 aom_clear_system_state();
Jingning Han3ee6db62015-08-05 19:00:31 -0700868
869 // Limit Q range for the adaptive loop.
clang-format99e28b82016-01-27 12:42:45 -0800870 if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
Jingning Han3ee6db62015-08-05 19:00:31 -0700871 !(cm->current_video_frame == 0)) {
clang-format99e28b82016-01-27 12:42:45 -0800872 qdelta = vp10_compute_qdelta_by_rate(
873 &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700874 } else if (!rc->is_src_frame_alt_ref &&
875 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
clang-format99e28b82016-01-27 12:42:45 -0800876 qdelta = vp10_compute_qdelta_by_rate(
877 &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700878 }
879 *top_index = active_worst_quality + qdelta;
880 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
881 }
882#endif
883
884 if (oxcf->rc_mode == VPX_Q) {
885 q = active_best_quality;
clang-format99e28b82016-01-27 12:42:45 -0800886 // Special case code to try and match quality with forced key frames
Jingning Han3ee6db62015-08-05 19:00:31 -0700887 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
888 q = rc->last_boosted_qindex;
889 } else {
clang-format99e28b82016-01-27 12:42:45 -0800890 q = vp10_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
891 active_worst_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700892 if (q > *top_index) {
893 // Special case when we are targeting the max allowed rate
894 if (rc->this_frame_target >= rc->max_frame_bandwidth)
895 *top_index = q;
896 else
897 q = *top_index;
898 }
899 }
900
clang-format99e28b82016-01-27 12:42:45 -0800901 assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700902 assert(*bottom_index <= rc->worst_quality &&
903 *bottom_index >= rc->best_quality);
904 assert(q <= rc->worst_quality && q >= rc->best_quality);
905 return q;
906}
907
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700908int vp10_frame_type_qdelta(const VP10_COMP *cpi, int rf_level, int q) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700909 static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
910 1.00, // INTER_NORMAL
911 1.00, // INTER_HIGH
912 1.50, // GF_ARF_LOW
913 1.75, // GF_ARF_STD
914 2.00, // KF_STD
915 };
clang-format99e28b82016-01-27 12:42:45 -0800916 static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] = {
917 INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME
918 };
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700919 const VP10_COMMON *const cm = &cpi->common;
clang-format99e28b82016-01-27 12:42:45 -0800920 int qdelta =
921 vp10_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level], q,
922 rate_factor_deltas[rf_level], cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700923 return qdelta;
924}
925
926#define STATIC_MOTION_THRESH 95
Yaowu Xu26a9afc2015-08-13 09:42:27 -0700927static int rc_pick_q_and_bounds_two_pass(const VP10_COMP *cpi,
clang-format99e28b82016-01-27 12:42:45 -0800928 int *bottom_index, int *top_index) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700929 const VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -0700930 const RATE_CONTROL *const rc = &cpi->rc;
hui sua4c7e922015-08-16 17:24:35 -0700931 const VP10EncoderConfig *const oxcf = &cpi->oxcf;
Jingning Han3ee6db62015-08-05 19:00:31 -0700932 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
933 const int cq_level = get_active_cq_level(rc, oxcf);
934 int active_best_quality;
935 int active_worst_quality = cpi->twopass.active_worst_quality;
936 int q;
937 int *inter_minq;
938 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
939
Yunqing Wangc147c4d2015-08-27 15:11:38 -0700940 if (frame_is_intra_only(cm)) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700941 // Handle the special case for key frames forced when we have reached
942 // the maximum key frame interval. Here force the Q to a range
943 // based on the ambient Q to reduce the risk of popping.
944 if (rc->this_key_frame_forced) {
945 double last_boosted_q;
946 int delta_qindex;
947 int qindex;
948
949 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
James Zern5e16d392015-08-17 18:19:22 -0700950 qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
Jingning Han3ee6db62015-08-05 19:00:31 -0700951 active_best_quality = qindex;
952 last_boosted_q = vp10_convert_qindex_to_q(qindex, cm->bit_depth);
clang-format99e28b82016-01-27 12:42:45 -0800953 delta_qindex = vp10_compute_qdelta(
954 rc, last_boosted_q, last_boosted_q * 1.25, cm->bit_depth);
James Zern5e16d392015-08-17 18:19:22 -0700955 active_worst_quality =
956 VPXMIN(qindex + delta_qindex, active_worst_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700957 } else {
958 qindex = rc->last_boosted_qindex;
959 last_boosted_q = vp10_convert_qindex_to_q(qindex, cm->bit_depth);
clang-format99e28b82016-01-27 12:42:45 -0800960 delta_qindex = vp10_compute_qdelta(
961 rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
James Zern5e16d392015-08-17 18:19:22 -0700962 active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -0700963 }
964 } else {
965 // Not forced keyframe.
966 double q_adj_factor = 1.0;
967 double q_val;
968 // Baseline value derived from cpi->active_worst_quality and kf boost.
clang-format99e28b82016-01-27 12:42:45 -0800969 active_best_quality =
970 get_kf_active_quality(rc, active_worst_quality, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700971
972 // Allow somewhat lower kf minq with small image formats.
973 if ((cm->width * cm->height) <= (352 * 288)) {
974 q_adj_factor -= 0.25;
975 }
976
977 // Make a further adjustment based on the kf zero motion measure.
978 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
979
980 // Convert the adjustment factor to a qindex delta
981 // on active_best_quality.
982 q_val = vp10_convert_qindex_to_q(active_best_quality, cm->bit_depth);
clang-format99e28b82016-01-27 12:42:45 -0800983 active_best_quality +=
984 vp10_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -0700985 }
986 } else if (!rc->is_src_frame_alt_ref &&
987 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
988 // Use the lower of active_worst_quality and recent
989 // average Q as basis for GF/ARF best Q limit unless last frame was
990 // a key frame.
991 if (rc->frames_since_key > 1 &&
992 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
993 q = rc->avg_frame_qindex[INTER_FRAME];
994 } else {
995 q = active_worst_quality;
996 }
997 // For constrained quality dont allow Q less than the cq level
998 if (oxcf->rc_mode == VPX_CQ) {
clang-format99e28b82016-01-27 12:42:45 -0800999 if (q < cq_level) q = cq_level;
Jingning Han3ee6db62015-08-05 19:00:31 -07001000
1001 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1002
1003 // Constrained quality use slightly lower active best.
1004 active_best_quality = active_best_quality * 15 / 16;
1005
1006 } else if (oxcf->rc_mode == VPX_Q) {
1007 if (!cpi->refresh_alt_ref_frame) {
1008 active_best_quality = cq_level;
1009 } else {
clang-format99e28b82016-01-27 12:42:45 -08001010 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -07001011
1012 // Modify best quality for second level arfs. For mode VPX_Q this
1013 // becomes the baseline frame q.
1014 if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
1015 active_best_quality = (active_best_quality + cq_level + 1) / 2;
1016 }
1017 } else {
1018 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1019 }
1020 } else {
1021 if (oxcf->rc_mode == VPX_Q) {
1022 active_best_quality = cq_level;
1023 } else {
1024 active_best_quality = inter_minq[active_worst_quality];
1025
1026 // For the constrained quality mode we don't want
1027 // q to fall below the cq level.
clang-format99e28b82016-01-27 12:42:45 -08001028 if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001029 active_best_quality = cq_level;
1030 }
1031 }
1032 }
1033
1034 // Extension to max or min Q if undershoot or overshoot is outside
1035 // the permitted range.
1036 if ((cpi->oxcf.rc_mode != VPX_Q) &&
1037 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) {
1038 if (frame_is_intra_only(cm) ||
1039 (!rc->is_src_frame_alt_ref &&
1040 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1041 active_best_quality -=
clang-format99e28b82016-01-27 12:42:45 -08001042 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
Jingning Han3ee6db62015-08-05 19:00:31 -07001043 active_worst_quality += (cpi->twopass.extend_maxq / 2);
1044 } else {
1045 active_best_quality -=
clang-format99e28b82016-01-27 12:42:45 -08001046 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
Jingning Han3ee6db62015-08-05 19:00:31 -07001047 active_worst_quality += cpi->twopass.extend_maxq;
1048 }
1049 }
1050
1051#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
Adrian Grangecebe6f02016-03-25 12:11:05 -07001052 aom_clear_system_state();
Jingning Han3ee6db62015-08-05 19:00:31 -07001053 // Static forced key frames Q restrictions dealt with elsewhere.
clang-format99e28b82016-01-27 12:42:45 -08001054 if (!(frame_is_intra_only(cm)) || !rc->this_key_frame_forced ||
Jingning Han3ee6db62015-08-05 19:00:31 -07001055 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
clang-format99e28b82016-01-27 12:42:45 -08001056 int qdelta = vp10_frame_type_qdelta(
1057 cpi, gf_group->rf_level[gf_group->index], active_worst_quality);
1058 active_worst_quality =
1059 VPXMAX(active_worst_quality + qdelta, active_best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -07001060 }
1061#endif
1062
1063 // Modify active_best_quality for downscaled normal frames.
1064 if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
clang-format99e28b82016-01-27 12:42:45 -08001065 int qdelta = vp10_compute_qdelta_by_rate(
1066 rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth);
James Zern5e16d392015-08-17 18:19:22 -07001067 active_best_quality =
1068 VPXMAX(active_best_quality + qdelta, rc->best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -07001069 }
1070
clang-format99e28b82016-01-27 12:42:45 -08001071 active_best_quality =
1072 clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1073 active_worst_quality =
1074 clamp(active_worst_quality, active_best_quality, rc->worst_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -07001075
1076 if (oxcf->rc_mode == VPX_Q) {
1077 q = active_best_quality;
clang-format99e28b82016-01-27 12:42:45 -08001078 // Special case code to try and match quality with forced key frames.
Yunqing Wangc147c4d2015-08-27 15:11:38 -07001079 } else if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001080 // If static since last kf use better of last boosted and last kf q.
1081 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
James Zern5e16d392015-08-17 18:19:22 -07001082 q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
Jingning Han3ee6db62015-08-05 19:00:31 -07001083 } else {
1084 q = rc->last_boosted_qindex;
1085 }
1086 } else {
clang-format99e28b82016-01-27 12:42:45 -08001087 q = vp10_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1088 active_worst_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -07001089 if (q > active_worst_quality) {
1090 // Special case when we are targeting the max allowed rate.
1091 if (rc->this_frame_target >= rc->max_frame_bandwidth)
1092 active_worst_quality = q;
1093 else
1094 q = active_worst_quality;
1095 }
1096 }
1097 clamp(q, active_best_quality, active_worst_quality);
1098
1099 *top_index = active_worst_quality;
1100 *bottom_index = active_best_quality;
1101
clang-format99e28b82016-01-27 12:42:45 -08001102 assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -07001103 assert(*bottom_index <= rc->worst_quality &&
1104 *bottom_index >= rc->best_quality);
1105 assert(q <= rc->worst_quality && q >= rc->best_quality);
1106 return q;
1107}
1108
clang-format99e28b82016-01-27 12:42:45 -08001109int vp10_rc_pick_q_and_bounds(const VP10_COMP *cpi, int *bottom_index,
1110 int *top_index) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001111 int q;
1112 if (cpi->oxcf.pass == 0) {
1113 if (cpi->oxcf.rc_mode == VPX_CBR)
1114 q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
1115 else
1116 q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
1117 } else {
1118 q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
1119 }
Jingning Han3ee6db62015-08-05 19:00:31 -07001120
Jingning Han3ee6db62015-08-05 19:00:31 -07001121 return q;
1122}
1123
clang-format99e28b82016-01-27 12:42:45 -08001124void vp10_rc_compute_frame_size_bounds(const VP10_COMP *cpi, int frame_target,
1125 int *frame_under_shoot_limit,
1126 int *frame_over_shoot_limit) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001127 if (cpi->oxcf.rc_mode == VPX_Q) {
1128 *frame_under_shoot_limit = 0;
clang-format99e28b82016-01-27 12:42:45 -08001129 *frame_over_shoot_limit = INT_MAX;
Jingning Han3ee6db62015-08-05 19:00:31 -07001130 } else {
1131 // For very small rate targets where the fractional adjustment
1132 // may be tiny make sure there is at least a minimum range.
1133 const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100;
James Zern5e16d392015-08-17 18:19:22 -07001134 *frame_under_shoot_limit = VPXMAX(frame_target - tolerance - 200, 0);
clang-format99e28b82016-01-27 12:42:45 -08001135 *frame_over_shoot_limit =
1136 VPXMIN(frame_target + tolerance + 200, cpi->rc.max_frame_bandwidth);
Jingning Han3ee6db62015-08-05 19:00:31 -07001137 }
1138}
1139
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001140void vp10_rc_set_frame_target(VP10_COMP *cpi, int target) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -07001141 const VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -07001142 RATE_CONTROL *const rc = &cpi->rc;
1143
1144 rc->this_frame_target = target;
1145
1146 // Modify frame size target when down-scaling.
1147 if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
1148 rc->frame_size_selector != UNSCALED)
clang-format99e28b82016-01-27 12:42:45 -08001149 rc->this_frame_target = (int)(rc->this_frame_target *
1150 rate_thresh_mult[rc->frame_size_selector]);
Jingning Han3ee6db62015-08-05 19:00:31 -07001151
1152 // Target rate per SB64 (including partial SB64s.
clang-format99e28b82016-01-27 12:42:45 -08001153 rc->sb64_target_rate =
1154 ((int64_t)rc->this_frame_target * 64 * 64) / (cm->width * cm->height);
Jingning Han3ee6db62015-08-05 19:00:31 -07001155}
1156
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001157static void update_alt_ref_frame_stats(VP10_COMP *cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001158 // this frame refreshes means next frames don't unless specified by user
1159 RATE_CONTROL *const rc = &cpi->rc;
1160 rc->frames_since_golden = 0;
1161
1162 // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1163 rc->source_alt_ref_pending = 0;
1164
1165 // Set the alternate reference frame active flag
1166 rc->source_alt_ref_active = 1;
1167}
1168
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001169static void update_golden_frame_stats(VP10_COMP *cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001170 RATE_CONTROL *const rc = &cpi->rc;
1171
1172 // Update the Golden frame usage counts.
1173 if (cpi->refresh_golden_frame) {
1174 // this frame refreshes means next frames don't unless specified by user
1175 rc->frames_since_golden = 0;
1176
1177 // If we are not using alt ref in the up and coming group clear the arf
paulwilkins9d85ce82015-12-07 15:23:46 +00001178 // active flag. In multi arf group case, if the index is not 0 then
1179 // we are overlaying a mid group arf so should not reset the flag.
1180 if (cpi->oxcf.pass == 2) {
1181 if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1182 rc->source_alt_ref_active = 0;
1183 } else if (!rc->source_alt_ref_pending) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001184 rc->source_alt_ref_active = 0;
1185 }
1186
1187 // Decrement count down till next gf
clang-format99e28b82016-01-27 12:42:45 -08001188 if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
Jingning Han3ee6db62015-08-05 19:00:31 -07001189
1190 } else if (!cpi->refresh_alt_ref_frame) {
1191 // Decrement count down till next gf
clang-format99e28b82016-01-27 12:42:45 -08001192 if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
Jingning Han3ee6db62015-08-05 19:00:31 -07001193
1194 rc->frames_since_golden++;
1195 }
1196}
1197
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001198void vp10_rc_postencode_update(VP10_COMP *cpi, uint64_t bytes_used) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -07001199 const VP10_COMMON *const cm = &cpi->common;
hui sua4c7e922015-08-16 17:24:35 -07001200 const VP10EncoderConfig *const oxcf = &cpi->oxcf;
Jingning Han3ee6db62015-08-05 19:00:31 -07001201 RATE_CONTROL *const rc = &cpi->rc;
1202 const int qindex = cm->base_qindex;
1203
1204 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
1205 vp10_cyclic_refresh_postencode(cpi);
1206 }
1207
1208 // Update rate control heuristics
1209 rc->projected_frame_size = (int)(bytes_used << 3);
1210
1211 // Post encode loop adjustment of Q prediction.
1212 vp10_rc_update_rate_correction_factors(cpi);
1213
1214 // Keep a record of last Q and ambient average Q.
1215 if (cm->frame_type == KEY_FRAME) {
1216 rc->last_q[KEY_FRAME] = qindex;
1217 rc->avg_frame_qindex[KEY_FRAME] =
1218 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1219 } else {
paulwilkins9ce611a2015-12-15 14:53:44 +00001220 if (!rc->is_src_frame_alt_ref &&
Yunqing Wangc147c4d2015-08-27 15:11:38 -07001221 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001222 rc->last_q[INTER_FRAME] = qindex;
1223 rc->avg_frame_qindex[INTER_FRAME] =
clang-format99e28b82016-01-27 12:42:45 -08001224 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
Jingning Han3ee6db62015-08-05 19:00:31 -07001225 rc->ni_frames++;
1226 rc->tot_q += vp10_convert_qindex_to_q(qindex, cm->bit_depth);
1227 rc->avg_q = rc->tot_q / rc->ni_frames;
1228 // Calculate the average Q for normal inter frames (not key or GFU
1229 // frames).
1230 rc->ni_tot_qi += qindex;
1231 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1232 }
1233 }
1234
1235 // Keep record of last boosted (KF/KF/ARF) Q value.
1236 // If the current frame is coded at a lower Q then we also update it.
1237 // If all mbs in this group are skipped only update if the Q value is
1238 // better than that already stored.
1239 // This is used to help set quality in forced key frames to reduce popping
clang-format99e28b82016-01-27 12:42:45 -08001240 if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) ||
Jingning Han3ee6db62015-08-05 19:00:31 -07001241 (!rc->constrained_gf_group &&
1242 (cpi->refresh_alt_ref_frame ||
1243 (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1244 rc->last_boosted_qindex = qindex;
1245 }
clang-format99e28b82016-01-27 12:42:45 -08001246 if (cm->frame_type == KEY_FRAME) rc->last_kf_qindex = qindex;
Jingning Han3ee6db62015-08-05 19:00:31 -07001247
1248 update_buffer_level(cpi, rc->projected_frame_size);
1249
1250 // Rolling monitors of whether we are over or underspending used to help
1251 // regulate min and Max Q in two pass.
1252 if (cm->frame_type != KEY_FRAME) {
1253 rc->rolling_target_bits = ROUND_POWER_OF_TWO(
1254 rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1255 rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
1256 rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1257 rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
1258 rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1259 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
1260 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1261 }
1262
1263 // Actual bits spent
1264 rc->total_actual_bits += rc->projected_frame_size;
1265 rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1266
1267 rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1268
1269 if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1270 (cm->frame_type != KEY_FRAME))
1271 // Update the alternate reference frame stats as appropriate.
1272 update_alt_ref_frame_stats(cpi);
1273 else
1274 // Update the Golden frame stats as appropriate.
1275 update_golden_frame_stats(cpi);
1276
clang-format99e28b82016-01-27 12:42:45 -08001277 if (cm->frame_type == KEY_FRAME) rc->frames_since_key = 0;
Jingning Han3ee6db62015-08-05 19:00:31 -07001278 if (cm->show_frame) {
1279 rc->frames_since_key++;
1280 rc->frames_to_key--;
1281 }
1282
1283 // Trigger the resizing of the next frame if it is scaled.
1284 if (oxcf->pass != 0) {
1285 cpi->resize_pending =
1286 rc->next_frame_size_selector != rc->frame_size_selector;
1287 rc->frame_size_selector = rc->next_frame_size_selector;
1288 }
1289}
1290
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001291void vp10_rc_postencode_update_drop_frame(VP10_COMP *cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001292 // Update buffer level with zero size, update frame counters, and return.
1293 update_buffer_level(cpi, 0);
1294 cpi->rc.frames_since_key++;
1295 cpi->rc.frames_to_key--;
1296 cpi->rc.rc_2_frame = 0;
1297 cpi->rc.rc_1_frame = 0;
1298}
1299
1300// Use this macro to turn on/off use of alt-refs in one-pass mode.
clang-format99e28b82016-01-27 12:42:45 -08001301#define USE_ALTREF_FOR_ONE_PASS 1
Jingning Han3ee6db62015-08-05 19:00:31 -07001302
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001303static int calc_pframe_target_size_one_pass_vbr(const VP10_COMP *const cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001304 static const int af_ratio = 10;
1305 const RATE_CONTROL *const rc = &cpi->rc;
1306 int target;
1307#if USE_ALTREF_FOR_ONE_PASS
clang-format99e28b82016-01-27 12:42:45 -08001308 target =
1309 (!rc->is_src_frame_alt_ref &&
1310 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
1311 ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
1312 (rc->baseline_gf_interval + af_ratio - 1)
1313 : (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
1314 (rc->baseline_gf_interval + af_ratio - 1);
Jingning Han3ee6db62015-08-05 19:00:31 -07001315#else
1316 target = rc->avg_frame_bandwidth;
1317#endif
1318 return vp10_rc_clamp_pframe_target_size(cpi, target);
1319}
1320
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001321static int calc_iframe_target_size_one_pass_vbr(const VP10_COMP *const cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001322 static const int kf_ratio = 25;
1323 const RATE_CONTROL *rc = &cpi->rc;
1324 const int target = rc->avg_frame_bandwidth * kf_ratio;
1325 return vp10_rc_clamp_iframe_target_size(cpi, target);
1326}
1327
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001328void vp10_rc_get_one_pass_vbr_params(VP10_COMP *cpi) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -07001329 VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -07001330 RATE_CONTROL *const rc = &cpi->rc;
1331 int target;
1332 // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
1333 if (!cpi->refresh_alt_ref_frame &&
clang-format99e28b82016-01-27 12:42:45 -08001334 (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1335 rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001336 cm->frame_type = KEY_FRAME;
clang-format99e28b82016-01-27 12:42:45 -08001337 rc->this_key_frame_forced =
1338 cm->current_video_frame != 0 && rc->frames_to_key == 0;
Jingning Han3ee6db62015-08-05 19:00:31 -07001339 rc->frames_to_key = cpi->oxcf.key_freq;
1340 rc->kf_boost = DEFAULT_KF_BOOST;
1341 rc->source_alt_ref_active = 0;
1342 } else {
1343 cm->frame_type = INTER_FRAME;
1344 }
1345 if (rc->frames_till_gf_update_due == 0) {
1346 rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
1347 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1348 // NOTE: frames_till_gf_update_due must be <= frames_to_key.
1349 if (rc->frames_till_gf_update_due > rc->frames_to_key) {
1350 rc->frames_till_gf_update_due = rc->frames_to_key;
1351 rc->constrained_gf_group = 1;
1352 } else {
1353 rc->constrained_gf_group = 0;
1354 }
1355 cpi->refresh_golden_frame = 1;
1356 rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
1357 rc->gfu_boost = DEFAULT_GF_BOOST;
1358 }
1359 if (cm->frame_type == KEY_FRAME)
1360 target = calc_iframe_target_size_one_pass_vbr(cpi);
1361 else
1362 target = calc_pframe_target_size_one_pass_vbr(cpi);
1363 vp10_rc_set_frame_target(cpi, target);
1364}
1365
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001366static int calc_pframe_target_size_one_pass_cbr(const VP10_COMP *cpi) {
hui sua4c7e922015-08-16 17:24:35 -07001367 const VP10EncoderConfig *oxcf = &cpi->oxcf;
Jingning Han3ee6db62015-08-05 19:00:31 -07001368 const RATE_CONTROL *rc = &cpi->rc;
Jingning Han3ee6db62015-08-05 19:00:31 -07001369 const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
1370 const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
James Zern5e16d392015-08-17 18:19:22 -07001371 int min_frame_target =
1372 VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
Jingning Han3ee6db62015-08-05 19:00:31 -07001373 int target;
1374
1375 if (oxcf->gf_cbr_boost_pct) {
1376 const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
clang-format99e28b82016-01-27 12:42:45 -08001377 target = cpi->refresh_golden_frame
1378 ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval *
1379 af_ratio_pct) /
1380 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
1381 : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
1382 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
Jingning Han3ee6db62015-08-05 19:00:31 -07001383 } else {
1384 target = rc->avg_frame_bandwidth;
1385 }
Yunqing Wangc147c4d2015-08-27 15:11:38 -07001386
Jingning Han3ee6db62015-08-05 19:00:31 -07001387 if (diff > 0) {
1388 // Lower the target bandwidth for this frame.
James Zern5e16d392015-08-17 18:19:22 -07001389 const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
Jingning Han3ee6db62015-08-05 19:00:31 -07001390 target -= (target * pct_low) / 200;
1391 } else if (diff < 0) {
1392 // Increase the target bandwidth for this frame.
James Zern5e16d392015-08-17 18:19:22 -07001393 const int pct_high =
1394 (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
Jingning Han3ee6db62015-08-05 19:00:31 -07001395 target += (target * pct_high) / 200;
1396 }
1397 if (oxcf->rc_max_inter_bitrate_pct) {
clang-format99e28b82016-01-27 12:42:45 -08001398 const int max_rate =
1399 rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
James Zern5e16d392015-08-17 18:19:22 -07001400 target = VPXMIN(target, max_rate);
Jingning Han3ee6db62015-08-05 19:00:31 -07001401 }
James Zern5e16d392015-08-17 18:19:22 -07001402 return VPXMAX(min_frame_target, target);
Jingning Han3ee6db62015-08-05 19:00:31 -07001403}
1404
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001405static int calc_iframe_target_size_one_pass_cbr(const VP10_COMP *cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001406 const RATE_CONTROL *rc = &cpi->rc;
Jingning Han3ee6db62015-08-05 19:00:31 -07001407 int target;
1408 if (cpi->common.current_video_frame == 0) {
1409 target = ((rc->starting_buffer_level / 2) > INT_MAX)
clang-format99e28b82016-01-27 12:42:45 -08001410 ? INT_MAX
1411 : (int)(rc->starting_buffer_level / 2);
Jingning Han3ee6db62015-08-05 19:00:31 -07001412 } else {
1413 int kf_boost = 32;
1414 double framerate = cpi->framerate;
Yunqing Wangc147c4d2015-08-27 15:11:38 -07001415
James Zern5e16d392015-08-17 18:19:22 -07001416 kf_boost = VPXMAX(kf_boost, (int)(2 * framerate - 16));
clang-format99e28b82016-01-27 12:42:45 -08001417 if (rc->frames_since_key < framerate / 2) {
1418 kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
Jingning Han3ee6db62015-08-05 19:00:31 -07001419 }
1420 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
1421 }
1422 return vp10_rc_clamp_iframe_target_size(cpi, target);
1423}
1424
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001425void vp10_rc_get_one_pass_cbr_params(VP10_COMP *cpi) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -07001426 VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -07001427 RATE_CONTROL *const rc = &cpi->rc;
1428 int target;
1429 // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
clang-format99e28b82016-01-27 12:42:45 -08001430 if ((cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1431 rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001432 cm->frame_type = KEY_FRAME;
clang-format99e28b82016-01-27 12:42:45 -08001433 rc->this_key_frame_forced =
1434 cm->current_video_frame != 0 && rc->frames_to_key == 0;
Jingning Han3ee6db62015-08-05 19:00:31 -07001435 rc->frames_to_key = cpi->oxcf.key_freq;
1436 rc->kf_boost = DEFAULT_KF_BOOST;
1437 rc->source_alt_ref_active = 0;
1438 } else {
1439 cm->frame_type = INTER_FRAME;
1440 }
1441 if (rc->frames_till_gf_update_due == 0) {
1442 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1443 vp10_cyclic_refresh_set_golden_update(cpi);
1444 else
1445 rc->baseline_gf_interval =
1446 (rc->min_gf_interval + rc->max_gf_interval) / 2;
1447 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1448 // NOTE: frames_till_gf_update_due must be <= frames_to_key.
1449 if (rc->frames_till_gf_update_due > rc->frames_to_key)
1450 rc->frames_till_gf_update_due = rc->frames_to_key;
1451 cpi->refresh_golden_frame = 1;
1452 rc->gfu_boost = DEFAULT_GF_BOOST;
1453 }
1454
1455 // Any update/change of global cyclic refresh parameters (amount/delta-qp)
1456 // should be done here, before the frame qp is selected.
1457 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1458 vp10_cyclic_refresh_update_parameters(cpi);
1459
1460 if (cm->frame_type == KEY_FRAME)
1461 target = calc_iframe_target_size_one_pass_cbr(cpi);
1462 else
1463 target = calc_pframe_target_size_one_pass_cbr(cpi);
1464
1465 vp10_rc_set_frame_target(cpi, target);
1466 if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC)
1467 cpi->resize_pending = vp10_resize_one_pass_cbr(cpi);
1468 else
1469 cpi->resize_pending = 0;
1470}
1471
1472int vp10_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
Adrian Grangecebe6f02016-03-25 12:11:05 -07001473 aom_bit_depth_t bit_depth) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001474 int start_index = rc->worst_quality;
1475 int target_index = rc->worst_quality;
1476 int i;
1477
1478 // Convert the average q value to an index.
1479 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1480 start_index = i;
clang-format99e28b82016-01-27 12:42:45 -08001481 if (vp10_convert_qindex_to_q(i, bit_depth) >= qstart) break;
Jingning Han3ee6db62015-08-05 19:00:31 -07001482 }
1483
1484 // Convert the q target to an index
1485 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1486 target_index = i;
clang-format99e28b82016-01-27 12:42:45 -08001487 if (vp10_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
Jingning Han3ee6db62015-08-05 19:00:31 -07001488 }
1489
1490 return target_index - start_index;
1491}
1492
1493int vp10_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
clang-format99e28b82016-01-27 12:42:45 -08001494 int qindex, double rate_target_ratio,
Adrian Grangecebe6f02016-03-25 12:11:05 -07001495 aom_bit_depth_t bit_depth) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001496 int target_index = rc->worst_quality;
1497 int i;
1498
1499 // Look up the current projected bits per block for the base index
clang-format99e28b82016-01-27 12:42:45 -08001500 const int base_bits_per_mb =
1501 vp10_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
Jingning Han3ee6db62015-08-05 19:00:31 -07001502
1503 // Find the target bits per mb based on the base value and given ratio.
1504 const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
1505
1506 // Convert the q target to an index
1507 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1508 if (vp10_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
1509 target_bits_per_mb) {
1510 target_index = i;
1511 break;
1512 }
1513 }
1514 return target_index - qindex;
1515}
1516
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001517void vp10_rc_set_gf_interval_range(const VP10_COMP *const cpi,
clang-format99e28b82016-01-27 12:42:45 -08001518 RATE_CONTROL *const rc) {
hui sua4c7e922015-08-16 17:24:35 -07001519 const VP10EncoderConfig *const oxcf = &cpi->oxcf;
Jingning Han3ee6db62015-08-05 19:00:31 -07001520
paulwilkins99309002015-12-15 15:23:47 +00001521 // Special case code for 1 pass fixed Q mode tests
1522 if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
1523 rc->max_gf_interval = FIXED_GF_INTERVAL;
1524 rc->min_gf_interval = FIXED_GF_INTERVAL;
1525 rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
1526 } else {
1527 // Set Maximum gf/arf interval
1528 rc->max_gf_interval = oxcf->max_gf_interval;
1529 rc->min_gf_interval = oxcf->min_gf_interval;
1530 if (rc->min_gf_interval == 0)
1531 rc->min_gf_interval = vp10_rc_get_default_min_gf_interval(
1532 oxcf->width, oxcf->height, cpi->framerate);
1533 if (rc->max_gf_interval == 0)
1534 rc->max_gf_interval = vp10_rc_get_default_max_gf_interval(
1535 cpi->framerate, rc->min_gf_interval);
Jingning Han3ee6db62015-08-05 19:00:31 -07001536
paulwilkins99309002015-12-15 15:23:47 +00001537 // Extended interval for genuinely static scenes
1538 rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
Jingning Han3ee6db62015-08-05 19:00:31 -07001539
paulwilkins99309002015-12-15 15:23:47 +00001540 if (is_altref_enabled(cpi)) {
1541 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
1542 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
1543 }
1544
1545 if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1546 rc->max_gf_interval = rc->static_scene_max_gf_interval;
1547
1548 // Clamp min to max
1549 rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
Jingning Han3ee6db62015-08-05 19:00:31 -07001550 }
Jingning Han3ee6db62015-08-05 19:00:31 -07001551}
1552
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001553void vp10_rc_update_framerate(VP10_COMP *cpi) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -07001554 const VP10_COMMON *const cm = &cpi->common;
hui sua4c7e922015-08-16 17:24:35 -07001555 const VP10EncoderConfig *const oxcf = &cpi->oxcf;
Jingning Han3ee6db62015-08-05 19:00:31 -07001556 RATE_CONTROL *const rc = &cpi->rc;
1557 int vbr_max_bits;
1558
1559 rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
clang-format99e28b82016-01-27 12:42:45 -08001560 rc->min_frame_bandwidth =
1561 (int)(rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
Jingning Han3ee6db62015-08-05 19:00:31 -07001562
James Zern5e16d392015-08-17 18:19:22 -07001563 rc->min_frame_bandwidth =
1564 VPXMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
Jingning Han3ee6db62015-08-05 19:00:31 -07001565
1566 // A maximum bitrate for a frame is defined.
1567 // The baseline for this aligns with HW implementations that
1568 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1569 // per 16x16 MB (averaged over a frame). However this limit is extended if
1570 // a very high rate is given on the command line or the the rate cannnot
1571 // be acheived because of a user specificed max q (e.g. when the user
1572 // specifies lossless encode.
clang-format99e28b82016-01-27 12:42:45 -08001573 vbr_max_bits =
1574 (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section) /
1575 100);
James Zern5e16d392015-08-17 18:19:22 -07001576 rc->max_frame_bandwidth =
1577 VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
Jingning Han3ee6db62015-08-05 19:00:31 -07001578
1579 vp10_rc_set_gf_interval_range(cpi, rc);
1580}
1581
1582#define VBR_PCT_ADJUSTMENT_LIMIT 50
1583// For VBR...adjustment to the frame target based on error from previous frames
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001584static void vbr_rate_correction(VP10_COMP *cpi, int *this_frame_target) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001585 RATE_CONTROL *const rc = &cpi->rc;
1586 int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
1587 int max_delta;
1588 double position_factor = 1.0;
1589
1590 // How far through the clip are we.
1591 // This number is used to damp the per frame rate correction.
1592 // Range 0 - 1.0
1593 if (cpi->twopass.total_stats.count) {
1594 position_factor = sqrt((double)cpi->common.current_video_frame /
1595 cpi->twopass.total_stats.count);
1596 }
1597 max_delta = (int)(position_factor *
1598 ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
1599
1600 // vbr_bits_off_target > 0 means we have extra bits to spend
1601 if (vbr_bits_off_target > 0) {
clang-format99e28b82016-01-27 12:42:45 -08001602 *this_frame_target += (vbr_bits_off_target > max_delta)
1603 ? max_delta
1604 : (int)vbr_bits_off_target;
Jingning Han3ee6db62015-08-05 19:00:31 -07001605 } else {
clang-format99e28b82016-01-27 12:42:45 -08001606 *this_frame_target -= (vbr_bits_off_target < -max_delta)
1607 ? max_delta
1608 : (int)-vbr_bits_off_target;
Jingning Han3ee6db62015-08-05 19:00:31 -07001609 }
1610
1611 // Fast redistribution of bits arising from massive local undershoot.
1612 // Dont do it for kf,arf,gf or overlay frames.
1613 if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
1614 rc->vbr_bits_off_target_fast) {
James Zern5e16d392015-08-17 18:19:22 -07001615 int one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, *this_frame_target);
Jingning Han3ee6db62015-08-05 19:00:31 -07001616 int fast_extra_bits;
James Zern5e16d392015-08-17 18:19:22 -07001617 fast_extra_bits = (int)VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
1618 fast_extra_bits = (int)VPXMIN(
1619 fast_extra_bits,
1620 VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
Jingning Han3ee6db62015-08-05 19:00:31 -07001621 *this_frame_target += (int)fast_extra_bits;
1622 rc->vbr_bits_off_target_fast -= fast_extra_bits;
1623 }
1624}
1625
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001626void vp10_set_target_rate(VP10_COMP *cpi) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001627 RATE_CONTROL *const rc = &cpi->rc;
1628 int target_rate = rc->base_frame_target;
1629
1630 // Correction to rate target based on prior over or under shoot.
1631 if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
1632 vbr_rate_correction(cpi, &target_rate);
1633 vp10_rc_set_frame_target(cpi, target_rate);
1634}
1635
1636// Check if we should resize, based on average QP from past x frames.
1637// Only allow for resize at most one scale down for now, scaling factor is 2.
Yaowu Xu26a9afc2015-08-13 09:42:27 -07001638int vp10_resize_one_pass_cbr(VP10_COMP *cpi) {
Yaowu Xufc7cbd12015-08-13 09:36:53 -07001639 const VP10_COMMON *const cm = &cpi->common;
Jingning Han3ee6db62015-08-05 19:00:31 -07001640 RATE_CONTROL *const rc = &cpi->rc;
1641 int resize_now = 0;
1642 cpi->resize_scale_num = 1;
1643 cpi->resize_scale_den = 1;
1644 // Don't resize on key frame; reset the counters on key frame.
1645 if (cm->frame_type == KEY_FRAME) {
1646 cpi->resize_avg_qp = 0;
1647 cpi->resize_count = 0;
1648 return 0;
1649 }
1650 // Resize based on average buffer underflow and QP over some window.
1651 // Ignore samples close to key frame, since QP is usually high after key.
1652 if (cpi->rc.frames_since_key > 2 * cpi->framerate) {
1653 const int window = (int)(5 * cpi->framerate);
1654 cpi->resize_avg_qp += cm->base_qindex;
1655 if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100))
1656 ++cpi->resize_buffer_underflow;
1657 ++cpi->resize_count;
1658 // Check for resize action every "window" frames.
1659 if (cpi->resize_count >= window) {
1660 int avg_qp = cpi->resize_avg_qp / cpi->resize_count;
1661 // Resize down if buffer level has underflowed sufficent amount in past
1662 // window, and we are at original resolution.
1663 // Resize back up if average QP is low, and we are currently in a resized
1664 // down state.
1665 if (cpi->resize_state == 0 &&
1666 cpi->resize_buffer_underflow > (cpi->resize_count >> 2)) {
1667 resize_now = 1;
1668 cpi->resize_state = 1;
1669 } else if (cpi->resize_state == 1 &&
1670 avg_qp < 40 * cpi->rc.worst_quality / 100) {
1671 resize_now = -1;
1672 cpi->resize_state = 0;
1673 }
1674 // Reset for next window measurement.
1675 cpi->resize_avg_qp = 0;
1676 cpi->resize_count = 0;
1677 cpi->resize_buffer_underflow = 0;
1678 }
1679 }
1680 // If decision is to resize, reset some quantities, and check is we should
1681 // reduce rate correction factor,
1682 if (resize_now != 0) {
1683 int target_bits_per_frame;
1684 int active_worst_quality;
1685 int qindex;
1686 int tot_scale_change;
1687 // For now, resize is by 1/2 x 1/2.
1688 cpi->resize_scale_num = 1;
1689 cpi->resize_scale_den = 2;
1690 tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) /
clang-format99e28b82016-01-27 12:42:45 -08001691 (cpi->resize_scale_num * cpi->resize_scale_num);
Jingning Han3ee6db62015-08-05 19:00:31 -07001692 // Reset buffer level to optimal, update target size.
1693 rc->buffer_level = rc->optimal_buffer_level;
1694 rc->bits_off_target = rc->optimal_buffer_level;
1695 rc->this_frame_target = calc_pframe_target_size_one_pass_cbr(cpi);
1696 // Reset cyclic refresh parameters.
1697 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled)
1698 vp10_cyclic_refresh_reset_resize(cpi);
1699 // Get the projected qindex, based on the scaled target frame size (scaled
1700 // so target_bits_per_mb in vp10_rc_regulate_q will be correct target).
clang-format99e28b82016-01-27 12:42:45 -08001701 target_bits_per_frame = (resize_now == 1)
1702 ? rc->this_frame_target * tot_scale_change
1703 : rc->this_frame_target / tot_scale_change;
Jingning Han3ee6db62015-08-05 19:00:31 -07001704 active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
clang-format99e28b82016-01-27 12:42:45 -08001705 qindex = vp10_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality,
1706 active_worst_quality);
Jingning Han3ee6db62015-08-05 19:00:31 -07001707 // If resize is down, check if projected q index is close to worst_quality,
1708 // and if so, reduce the rate correction factor (since likely can afford
1709 // lower q for resized frame).
clang-format99e28b82016-01-27 12:42:45 -08001710 if (resize_now == 1 && qindex > 90 * cpi->rc.worst_quality / 100) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001711 rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
1712 }
1713 // If resize is back up, check if projected q index is too much above the
1714 // current base_qindex, and if so, reduce the rate correction factor
1715 // (since prefer to keep q for resized frame at least close to previous q).
clang-format99e28b82016-01-27 12:42:45 -08001716 if (resize_now == -1 && qindex > 130 * cm->base_qindex / 100) {
Jingning Han3ee6db62015-08-05 19:00:31 -07001717 rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
1718 }
1719 }
1720 return resize_now;
1721}