blob: 4acac73addd686822a13c5d8eb7e9505bdbc5084 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -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.
Yaowu Xuc27fc142016-08-22 16:08:15 -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
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "aom_dsp/aom_dsp_common.h"
20#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021#include "aom_ports/mem.h"
22#include "aom_ports/system_state.h"
23
24#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"
30
31#include "av1/encoder/encodemv.h"
Alex Converse9d068c12017-08-03 11:48:19 -070032#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033#include "av1/encoder/ratectrl.h"
34
35// Max rate target for 1080P and below encodes under normal circumstances
36// (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
37#define MAX_MB_RATE 250
38#define MAXRATE_1080P 2025000
39
40#define DEFAULT_KF_BOOST 2000
41#define DEFAULT_GF_BOOST 2000
42
43#define MIN_BPB_FACTOR 0.005
44#define MAX_BPB_FACTOR 50
45
46#define FRAME_OVERHEAD_BITS 200
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020047#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070048#define ASSIGN_MINQ_TABLE(bit_depth, name) \
49 do { \
50 switch (bit_depth) { \
Yaowu Xuf883b422016-08-30 14:01:10 -070051 case AOM_BITS_8: name = name##_8; break; \
52 case AOM_BITS_10: name = name##_10; break; \
53 case AOM_BITS_12: name = name##_12; break; \
Yaowu Xuc27fc142016-08-22 16:08:15 -070054 default: \
55 assert(0 && \
Yaowu Xuf883b422016-08-30 14:01:10 -070056 "bit_depth should be AOM_BITS_8, AOM_BITS_10" \
57 " or AOM_BITS_12"); \
Yaowu Xuc27fc142016-08-22 16:08:15 -070058 name = NULL; \
59 } \
60 } while (0)
61#else
62#define ASSIGN_MINQ_TABLE(bit_depth, name) \
63 do { \
clang-format67948d32016-09-07 22:40:40 -070064 (void)bit_depth; \
Yaowu Xuc27fc142016-08-22 16:08:15 -070065 name = name##_8; \
66 } while (0)
67#endif
68
69// Tables relating active max Q to active min Q
70static int kf_low_motion_minq_8[QINDEX_RANGE];
71static int kf_high_motion_minq_8[QINDEX_RANGE];
72static int arfgf_low_motion_minq_8[QINDEX_RANGE];
73static int arfgf_high_motion_minq_8[QINDEX_RANGE];
74static int inter_minq_8[QINDEX_RANGE];
75static int rtc_minq_8[QINDEX_RANGE];
76
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020077#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070078static int kf_low_motion_minq_10[QINDEX_RANGE];
79static int kf_high_motion_minq_10[QINDEX_RANGE];
80static int arfgf_low_motion_minq_10[QINDEX_RANGE];
81static int arfgf_high_motion_minq_10[QINDEX_RANGE];
82static int inter_minq_10[QINDEX_RANGE];
83static int rtc_minq_10[QINDEX_RANGE];
84static int kf_low_motion_minq_12[QINDEX_RANGE];
85static int kf_high_motion_minq_12[QINDEX_RANGE];
86static int arfgf_low_motion_minq_12[QINDEX_RANGE];
87static int arfgf_high_motion_minq_12[QINDEX_RANGE];
88static int inter_minq_12[QINDEX_RANGE];
89static int rtc_minq_12[QINDEX_RANGE];
90#endif
91
92static int gf_high = 2000;
93static int gf_low = 400;
94static int kf_high = 5000;
95static int kf_low = 400;
96
Fergus Simpsonddc846e2017-04-24 18:09:13 -070097double av1_resize_rate_factor(const AV1_COMP *cpi) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -070098 return (double)(cpi->oxcf.width * cpi->oxcf.height) /
99 (cpi->common.width * cpi->common.height);
Fergus Simpsonddc846e2017-04-24 18:09:13 -0700100}
101
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102// Functions to compute the active minq lookup table entries based on a
103// formulaic approach to facilitate easier adjustment of the Q tables.
104// The formulae were derived from computing a 3rd order polynomial best
105// fit to the original data (after plotting real maxq vs minq (not q index))
106static int get_minq_index(double maxq, double x3, double x2, double x1,
Yaowu Xuf883b422016-08-30 14:01:10 -0700107 aom_bit_depth_t bit_depth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700109 const double minqtarget = AOMMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700110
111 // Special case handling to deal with the step from q2.0
112 // down to lossless mode represented by q 1.0.
113 if (minqtarget <= 2.0) return 0;
114
115 for (i = 0; i < QINDEX_RANGE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700116 if (minqtarget <= av1_convert_qindex_to_q(i, bit_depth)) return i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700117 }
118
119 return QINDEX_RANGE - 1;
120}
121
122static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
123 int *arfgf_high, int *inter, int *rtc,
Yaowu Xuf883b422016-08-30 14:01:10 -0700124 aom_bit_depth_t bit_depth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 int i;
126 for (i = 0; i < QINDEX_RANGE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700127 const double maxq = av1_convert_qindex_to_q(i, bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
129 kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
130 arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
131 arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
132 inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth);
133 rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
134 }
135}
136
Yaowu Xuf883b422016-08-30 14:01:10 -0700137void av1_rc_init_minq_luts(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138 init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
139 arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
Yaowu Xuf883b422016-08-30 14:01:10 -0700140 inter_minq_8, rtc_minq_8, AOM_BITS_8);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200141#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700142 init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
143 arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
Yaowu Xuf883b422016-08-30 14:01:10 -0700144 inter_minq_10, rtc_minq_10, AOM_BITS_10);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145 init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
146 arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
Yaowu Xuf883b422016-08-30 14:01:10 -0700147 inter_minq_12, rtc_minq_12, AOM_BITS_12);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700148#endif
149}
150
151// These functions use formulaic calculations to make playing with the
152// quantizer tables easier. If necessary they can be replaced by lookup
153// tables if and when things settle down in the experimental bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -0700154double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155// Convert the index to a real Q value (scaled down to match old Q values)
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200156#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700157 switch (bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700158 case AOM_BITS_8: return av1_ac_quant(qindex, 0, bit_depth) / 4.0;
159 case AOM_BITS_10: return av1_ac_quant(qindex, 0, bit_depth) / 16.0;
160 case AOM_BITS_12: return av1_ac_quant(qindex, 0, bit_depth) / 64.0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700161 default:
Yaowu Xuf883b422016-08-30 14:01:10 -0700162 assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700163 return -1.0;
164 }
165#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700166 return av1_ac_quant(qindex, 0, bit_depth) / 4.0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700167#endif
168}
169
Yaowu Xuf883b422016-08-30 14:01:10 -0700170int av1_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
171 double correction_factor, aom_bit_depth_t bit_depth) {
172 const double q = av1_convert_qindex_to_q(qindex, bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700173 int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
174
175 assert(correction_factor <= MAX_BPB_FACTOR &&
176 correction_factor >= MIN_BPB_FACTOR);
177
178 // q based adjustment to baseline enumerator
179 enumerator += (int)(enumerator * q) >> 12;
180 return (int)(enumerator * correction_factor / q);
181}
182
Yaowu Xuf883b422016-08-30 14:01:10 -0700183int av1_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
184 double correction_factor,
185 aom_bit_depth_t bit_depth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 const int bpm =
Yaowu Xuf883b422016-08-30 14:01:10 -0700187 (int)(av1_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth));
188 return AOMMAX(FRAME_OVERHEAD_BITS,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
190}
191
Yaowu Xuf883b422016-08-30 14:01:10 -0700192int av1_rc_clamp_pframe_target_size(const AV1_COMP *const cpi, int target) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 const RATE_CONTROL *rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700194 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195 const int min_frame_target =
Yaowu Xuf883b422016-08-30 14:01:10 -0700196 AOMMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197// Clip the frame target to the minimum setup value.
198#if CONFIG_EXT_REFS
199 if (cpi->rc.is_src_frame_alt_ref) {
200#else
201 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
Zoe Liu6cfaff92016-10-18 17:12:11 -0700202#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 // If there is an active ARF at this location use the minimum
204 // bits on this frame even if it is a constructed arf.
205 // The active maximum quantizer insures that an appropriate
206 // number of bits will be spent if needed for constructed ARFs.
207 target = min_frame_target;
208 } else if (target < min_frame_target) {
209 target = min_frame_target;
210 }
211
212 // Clip the frame target to the maximum allowed value.
213 if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
214 if (oxcf->rc_max_inter_bitrate_pct) {
215 const int max_rate =
216 rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
Yaowu Xuf883b422016-08-30 14:01:10 -0700217 target = AOMMIN(target, max_rate);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218 }
219
220 return target;
221}
222
Yaowu Xuf883b422016-08-30 14:01:10 -0700223int av1_rc_clamp_iframe_target_size(const AV1_COMP *const cpi, int target) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224 const RATE_CONTROL *rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700225 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 if (oxcf->rc_max_intra_bitrate_pct) {
227 const int max_rate =
228 rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
Yaowu Xuf883b422016-08-30 14:01:10 -0700229 target = AOMMIN(target, max_rate);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700230 }
231 if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
232 return target;
233}
234
235// Update the buffer level: leaky bucket model.
Yaowu Xuf883b422016-08-30 14:01:10 -0700236static void update_buffer_level(AV1_COMP *cpi, int encoded_frame_size) {
237 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238 RATE_CONTROL *const rc = &cpi->rc;
239
240// Non-viewable frames are a special case and are treated as pure overhead.
241#if CONFIG_EXT_REFS
242 // TODO(zoeliu): To further explore whether we should treat BWDREF_FRAME
243 // differently, since it is a no-show frame.
244 if (!cm->show_frame && !rc->is_bwd_ref_frame)
245#else
246 if (!cm->show_frame)
247#endif // CONFIG_EXT_REFS
248 rc->bits_off_target -= encoded_frame_size;
249 else
250 rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
251
252 // Clip the buffer level to the maximum specified buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -0700253 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254 rc->buffer_level = rc->bits_off_target;
255}
256
Yaowu Xuf883b422016-08-30 14:01:10 -0700257int av1_rc_get_default_min_gf_interval(int width, int height,
258 double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259 // Assume we do not need any constraint lower than 4K 20 fps
260 static const double factor_safe = 3840 * 2160 * 20.0;
261 const double factor = width * height * framerate;
262 const int default_interval =
263 clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
264
265 if (factor <= factor_safe)
266 return default_interval;
267 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700268 return AOMMAX(default_interval,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
270 // Note this logic makes:
271 // 4K24: 5
272 // 4K30: 6
273 // 4K60: 12
274}
275
Yaowu Xuf883b422016-08-30 14:01:10 -0700276int av1_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
277 int interval = AOMMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700278 interval += (interval & 0x01); // Round to even value
Yaowu Xuf883b422016-08-30 14:01:10 -0700279 return AOMMAX(interval, min_gf_interval);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280}
281
Yaowu Xuf883b422016-08-30 14:01:10 -0700282void av1_rc_init(const AV1EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283 int i;
284
Yaowu Xuf883b422016-08-30 14:01:10 -0700285 if (pass == 0 && oxcf->rc_mode == AOM_CBR) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
287 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
288 } else {
289 rc->avg_frame_qindex[KEY_FRAME] =
290 (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
291 rc->avg_frame_qindex[INTER_FRAME] =
292 (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
293 }
294
295 rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
296 rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
297
298 rc->buffer_level = rc->starting_buffer_level;
299 rc->bits_off_target = rc->starting_buffer_level;
300
301 rc->rolling_target_bits = rc->avg_frame_bandwidth;
302 rc->rolling_actual_bits = rc->avg_frame_bandwidth;
303 rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
304 rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
305
306 rc->total_actual_bits = 0;
307 rc->total_target_bits = 0;
308 rc->total_target_vs_actual = 0;
309
310 rc->frames_since_key = 8; // Sensible default for first frame.
311 rc->this_key_frame_forced = 0;
312 rc->next_key_frame_forced = 0;
313 rc->source_alt_ref_pending = 0;
314 rc->source_alt_ref_active = 0;
315
316 rc->frames_till_gf_update_due = 0;
317 rc->ni_av_qi = oxcf->worst_allowed_q;
318 rc->ni_tot_qi = 0;
319 rc->ni_frames = 0;
320
321 rc->tot_q = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700322 rc->avg_q = av1_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323
324 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
325 rc->rate_correction_factors[i] = 1.0;
326 }
327
328 rc->min_gf_interval = oxcf->min_gf_interval;
329 rc->max_gf_interval = oxcf->max_gf_interval;
330 if (rc->min_gf_interval == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -0700331 rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700332 oxcf->width, oxcf->height, oxcf->init_framerate);
333 if (rc->max_gf_interval == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -0700334 rc->max_gf_interval = av1_rc_get_default_max_gf_interval(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335 oxcf->init_framerate, rc->min_gf_interval);
336 rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
337}
338
Yaowu Xuf883b422016-08-30 14:01:10 -0700339int av1_rc_drop_frame(AV1_COMP *cpi) {
340 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341 RATE_CONTROL *const rc = &cpi->rc;
342
343 if (!oxcf->drop_frames_water_mark) {
344 return 0;
345 } else {
346 if (rc->buffer_level < 0) {
347 // Always drop if buffer is below 0.
348 return 1;
349 } else {
350 // If buffer is below drop_mark, for now just drop every other frame
351 // (starting with the next frame) until it increases back over drop_mark.
352 int drop_mark =
353 (int)(oxcf->drop_frames_water_mark * rc->optimal_buffer_level / 100);
354 if ((rc->buffer_level > drop_mark) && (rc->decimation_factor > 0)) {
355 --rc->decimation_factor;
356 } else if (rc->buffer_level <= drop_mark && rc->decimation_factor == 0) {
357 rc->decimation_factor = 1;
358 }
359 if (rc->decimation_factor > 0) {
360 if (rc->decimation_count > 0) {
361 --rc->decimation_count;
362 return 1;
363 } else {
364 rc->decimation_count = rc->decimation_factor;
365 return 0;
366 }
367 } else {
368 rc->decimation_count = 0;
369 return 0;
370 }
371 }
372 }
373}
374
Yaowu Xuf883b422016-08-30 14:01:10 -0700375static double get_rate_correction_factor(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700376 const RATE_CONTROL *const rc = &cpi->rc;
377 double rcf;
378
379 if (cpi->common.frame_type == KEY_FRAME) {
380 rcf = rc->rate_correction_factors[KF_STD];
381 } else if (cpi->oxcf.pass == 2) {
382 RATE_FACTOR_LEVEL rf_lvl =
383 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
384 rcf = rc->rate_correction_factors[rf_lvl];
385 } else {
386 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
387 !rc->is_src_frame_alt_ref &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700388 (cpi->oxcf.rc_mode != AOM_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389 rcf = rc->rate_correction_factors[GF_ARF_STD];
390 else
391 rcf = rc->rate_correction_factors[INTER_NORMAL];
392 }
Fergus Simpsonddc846e2017-04-24 18:09:13 -0700393 rcf *= av1_resize_rate_factor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700394 return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
395}
396
Yaowu Xuf883b422016-08-30 14:01:10 -0700397static void set_rate_correction_factor(AV1_COMP *cpi, double factor) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700398 RATE_CONTROL *const rc = &cpi->rc;
399
400 // Normalize RCF to account for the size-dependent scaling factor.
Fergus Simpsonddc846e2017-04-24 18:09:13 -0700401 factor /= av1_resize_rate_factor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402
403 factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
404
405 if (cpi->common.frame_type == KEY_FRAME) {
406 rc->rate_correction_factors[KF_STD] = factor;
407 } else if (cpi->oxcf.pass == 2) {
408 RATE_FACTOR_LEVEL rf_lvl =
409 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
410 rc->rate_correction_factors[rf_lvl] = factor;
411 } else {
412 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
413 !rc->is_src_frame_alt_ref &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700414 (cpi->oxcf.rc_mode != AOM_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700415 rc->rate_correction_factors[GF_ARF_STD] = factor;
416 else
417 rc->rate_correction_factors[INTER_NORMAL] = factor;
418 }
419}
420
Yaowu Xuf883b422016-08-30 14:01:10 -0700421void av1_rc_update_rate_correction_factors(AV1_COMP *cpi) {
422 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700423 int correction_factor = 100;
424 double rate_correction_factor = get_rate_correction_factor(cpi);
425 double adjustment_limit;
426
427 int projected_size_based_on_q = 0;
428
429 // Do not update the rate factors for arf overlay frames.
430 if (cpi->rc.is_src_frame_alt_ref) return;
431
432 // Clear down mmx registers to allow floating point in what follows
Yaowu Xuf883b422016-08-30 14:01:10 -0700433 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700434
435 // Work out how big we would have expected the frame to be at this Q given
436 // the current correction factor.
437 // Stay in double to avoid int overflow when values are large
438 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
439 projected_size_based_on_q =
Yaowu Xuf883b422016-08-30 14:01:10 -0700440 av1_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700441 } else {
442 projected_size_based_on_q =
Yaowu Xuf883b422016-08-30 14:01:10 -0700443 av1_estimate_bits_at_q(cpi->common.frame_type, cm->base_qindex, cm->MBs,
444 rate_correction_factor, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700445 }
446 // Work out a size correction factor.
447 if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
448 correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
449 projected_size_based_on_q);
450
451 // More heavily damped adjustment used if we have been oscillating either side
452 // of target.
James Zernd2c42f02017-03-10 11:13:10 -0800453 if (correction_factor > 0) {
454 adjustment_limit =
455 0.25 + 0.5 * AOMMIN(1, fabs(log10(0.01 * correction_factor)));
456 } else {
457 adjustment_limit = 0.75;
458 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700459
460 cpi->rc.q_2_frame = cpi->rc.q_1_frame;
461 cpi->rc.q_1_frame = cm->base_qindex;
462 cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
463 if (correction_factor > 110)
464 cpi->rc.rc_1_frame = -1;
465 else if (correction_factor < 90)
466 cpi->rc.rc_1_frame = 1;
467 else
468 cpi->rc.rc_1_frame = 0;
469
470 if (correction_factor > 102) {
471 // We are not already at the worst allowable quality
472 correction_factor =
473 (int)(100 + ((correction_factor - 100) * adjustment_limit));
474 rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
475 // Keep rate_correction_factor within limits
476 if (rate_correction_factor > MAX_BPB_FACTOR)
477 rate_correction_factor = MAX_BPB_FACTOR;
478 } else if (correction_factor < 99) {
479 // We are not already at the best allowable quality
480 correction_factor =
481 (int)(100 - ((100 - correction_factor) * adjustment_limit));
482 rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
483
484 // Keep rate_correction_factor within limits
485 if (rate_correction_factor < MIN_BPB_FACTOR)
486 rate_correction_factor = MIN_BPB_FACTOR;
487 }
488
489 set_rate_correction_factor(cpi, rate_correction_factor);
490}
491
Yaowu Xuf883b422016-08-30 14:01:10 -0700492int av1_rc_regulate_q(const AV1_COMP *cpi, int target_bits_per_frame,
493 int active_best_quality, int active_worst_quality) {
494 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700495 int q = active_worst_quality;
496 int last_error = INT_MAX;
497 int i, target_bits_per_mb, bits_per_mb_at_this_q;
498 const double correction_factor = get_rate_correction_factor(cpi);
499
500 // Calculate required scaling factor based on target frame size and size of
501 // frame produced using previous Q.
502 target_bits_per_mb =
James Zernbe0bdd82017-04-20 15:48:47 -0700503 (int)((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504
505 i = active_best_quality;
506
507 do {
508 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
509 bits_per_mb_at_this_q =
Yaowu Xuf883b422016-08-30 14:01:10 -0700510 (int)av1_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700511 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700512 bits_per_mb_at_this_q = (int)av1_rc_bits_per_mb(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700513 cm->frame_type, i, correction_factor, cm->bit_depth);
514 }
515
516 if (bits_per_mb_at_this_q <= target_bits_per_mb) {
517 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
518 q = i;
519 else
520 q = i - 1;
521
522 break;
523 } else {
524 last_error = bits_per_mb_at_this_q - target_bits_per_mb;
525 }
526 } while (++i <= active_worst_quality);
527
528 // In CBR mode, this makes sure q is between oscillating Qs to prevent
529 // resonance.
Yaowu Xuf883b422016-08-30 14:01:10 -0700530 if (cpi->oxcf.rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531 (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
532 cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700533 q = clamp(q, AOMMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
534 AOMMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700535 }
536 return q;
537}
538
539static int get_active_quality(int q, int gfu_boost, int low, int high,
540 int *low_motion_minq, int *high_motion_minq) {
541 if (gfu_boost > high) {
542 return low_motion_minq[q];
543 } else if (gfu_boost < low) {
544 return high_motion_minq[q];
545 } else {
546 const int gap = high - low;
547 const int offset = high - gfu_boost;
548 const int qdiff = high_motion_minq[q] - low_motion_minq[q];
549 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
550 return low_motion_minq[q] + adjustment;
551 }
552}
553
554static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
Yaowu Xuf883b422016-08-30 14:01:10 -0700555 aom_bit_depth_t bit_depth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700556 int *kf_low_motion_minq;
557 int *kf_high_motion_minq;
558 ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
559 ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
560 return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
561 kf_low_motion_minq, kf_high_motion_minq);
562}
563
564static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
Yaowu Xuf883b422016-08-30 14:01:10 -0700565 aom_bit_depth_t bit_depth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700566 int *arfgf_low_motion_minq;
567 int *arfgf_high_motion_minq;
568 ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
569 ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
570 return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
571 arfgf_low_motion_minq, arfgf_high_motion_minq);
572}
573
Yaowu Xuf883b422016-08-30 14:01:10 -0700574static int calc_active_worst_quality_one_pass_vbr(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700575 const RATE_CONTROL *const rc = &cpi->rc;
576 const unsigned int curr_frame = cpi->common.current_video_frame;
577 int active_worst_quality;
578
579 if (cpi->common.frame_type == KEY_FRAME) {
580 active_worst_quality =
581 curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] * 2;
582 } else {
Zoe Liue9b15e22017-07-19 15:53:01 -0700583 if (!rc->is_src_frame_alt_ref && (cpi->refresh_golden_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -0700584#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -0700585 cpi->refresh_alt2_ref_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -0700586#endif // CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -0700587 cpi->refresh_alt_ref_frame)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700588 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
589 : rc->last_q[INTER_FRAME];
590 } else {
591 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
592 : rc->last_q[INTER_FRAME] * 2;
593 }
594 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700595 return AOMMIN(active_worst_quality, rc->worst_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596}
597
598// Adjust active_worst_quality level based on buffer level.
Yaowu Xuf883b422016-08-30 14:01:10 -0700599static int calc_active_worst_quality_one_pass_cbr(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700600 // Adjust active_worst_quality: If buffer is above the optimal/target level,
601 // bring active_worst_quality down depending on fullness of buffer.
602 // If buffer is below the optimal level, let the active_worst_quality go from
603 // ambient Q (at buffer = optimal level) to worst_quality level
604 // (at buffer = critical level).
Yaowu Xuf883b422016-08-30 14:01:10 -0700605 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 const RATE_CONTROL *rc = &cpi->rc;
607 // Buffer level below which we push active_worst to worst_quality.
608 int64_t critical_level = rc->optimal_buffer_level >> 3;
609 int64_t buff_lvl_step = 0;
610 int adjustment = 0;
611 int active_worst_quality;
612 int ambient_qp;
613 if (cm->frame_type == KEY_FRAME) return rc->worst_quality;
614 // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
615 // for the first few frames following key frame. These are both initialized
616 // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
617 // So for first few frames following key, the qp of that key frame is weighted
618 // into the active_worst_quality setting.
619 ambient_qp = (cm->current_video_frame < 5)
Yaowu Xuf883b422016-08-30 14:01:10 -0700620 ? AOMMIN(rc->avg_frame_qindex[INTER_FRAME],
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 rc->avg_frame_qindex[KEY_FRAME])
622 : rc->avg_frame_qindex[INTER_FRAME];
Yaowu Xuf883b422016-08-30 14:01:10 -0700623 active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp * 5 / 4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700624 if (rc->buffer_level > rc->optimal_buffer_level) {
625 // Adjust down.
626 // Maximum limit for down adjustment, ~30%.
627 int max_adjustment_down = active_worst_quality / 3;
628 if (max_adjustment_down) {
629 buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
630 max_adjustment_down);
631 if (buff_lvl_step)
632 adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
633 buff_lvl_step);
634 active_worst_quality -= adjustment;
635 }
636 } else if (rc->buffer_level > critical_level) {
637 // Adjust up from ambient Q.
638 if (critical_level) {
639 buff_lvl_step = (rc->optimal_buffer_level - critical_level);
640 if (buff_lvl_step) {
641 adjustment = (int)((rc->worst_quality - ambient_qp) *
642 (rc->optimal_buffer_level - rc->buffer_level) /
643 buff_lvl_step);
644 }
645 active_worst_quality = ambient_qp + adjustment;
646 }
647 } else {
648 // Set to worst_quality if buffer is below critical level.
649 active_worst_quality = rc->worst_quality;
650 }
651 return active_worst_quality;
652}
653
Yaowu Xuf883b422016-08-30 14:01:10 -0700654static int rc_pick_q_and_bounds_one_pass_cbr(const AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655 int *bottom_index,
656 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700657 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658 const RATE_CONTROL *const rc = &cpi->rc;
659 int active_best_quality;
660 int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
661 int q;
662 int *rtc_minq;
663 ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
664
665 if (frame_is_intra_only(cm)) {
666 active_best_quality = rc->best_quality;
667 // Handle the special case for key frames forced when we have reached
668 // the maximum key frame interval. Here force the Q to a range
669 // based on the ambient Q to reduce the risk of popping.
670 if (rc->this_key_frame_forced) {
671 int qindex = rc->last_boosted_qindex;
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 double last_boosted_q = av1_convert_qindex_to_q(qindex, cm->bit_depth);
673 int delta_qindex = av1_compute_qdelta(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700674 rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700676 } else if (cm->current_video_frame > 0) {
677 // not first frame of one pass and kf_boost is set
678 double q_adj_factor = 1.0;
679 double q_val;
680
681 active_best_quality = get_kf_active_quality(
682 rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
683
684 // Allow somewhat lower kf minq with small image formats.
685 if ((cm->width * cm->height) <= (352 * 288)) {
686 q_adj_factor -= 0.25;
687 }
688
689 // Convert the adjustment factor to a qindex delta
690 // on active_best_quality.
Yaowu Xuf883b422016-08-30 14:01:10 -0700691 q_val = av1_convert_qindex_to_q(active_best_quality, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700692 active_best_quality +=
Yaowu Xuf883b422016-08-30 14:01:10 -0700693 av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694 }
695 } else if (!rc->is_src_frame_alt_ref &&
696 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
697 // Use the lower of active_worst_quality and recent
698 // average Q as basis for GF/ARF best Q limit unless last frame was
699 // a key frame.
700 if (rc->frames_since_key > 1 &&
701 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
702 q = rc->avg_frame_qindex[INTER_FRAME];
703 } else {
704 q = active_worst_quality;
705 }
706 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
707 } else {
708 // Use the lower of active_worst_quality and recent/average Q.
709 if (cm->current_video_frame > 1) {
710 if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
711 active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
712 else
713 active_best_quality = rtc_minq[active_worst_quality];
714 } else {
715 if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
716 active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
717 else
718 active_best_quality = rtc_minq[active_worst_quality];
719 }
720 }
721
722 // Clip the active best and worst quality values to limits
723 active_best_quality =
724 clamp(active_best_quality, rc->best_quality, rc->worst_quality);
725 active_worst_quality =
726 clamp(active_worst_quality, active_best_quality, rc->worst_quality);
727
728 *top_index = active_worst_quality;
729 *bottom_index = active_best_quality;
730
731 // Limit Q range for the adaptive loop.
732 if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
733 !(cm->current_video_frame == 0)) {
734 int qdelta = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 aom_clear_system_state();
736 qdelta = av1_compute_qdelta_by_rate(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737 &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
738 *top_index = active_worst_quality + qdelta;
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 *top_index = AOMMAX(*top_index, *bottom_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740 }
741
742 // Special case code to try and match quality with forced key frames
743 if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
744 q = rc->last_boosted_qindex;
745 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700746 q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
747 active_worst_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700748 if (q > *top_index) {
749 // Special case when we are targeting the max allowed rate
750 if (rc->this_frame_target >= rc->max_frame_bandwidth)
751 *top_index = q;
752 else
753 q = *top_index;
754 }
755 }
756
757 assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
758 assert(*bottom_index <= rc->worst_quality &&
759 *bottom_index >= rc->best_quality);
760 assert(q <= rc->worst_quality && q >= rc->best_quality);
761 return q;
762}
763
764static int get_active_cq_level(const RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -0700765 const AV1EncoderConfig *const oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766 static const double cq_adjust_threshold = 0.1;
767 int active_cq_level = oxcf->cq_level;
Yaowu Xuf883b422016-08-30 14:01:10 -0700768 if (oxcf->rc_mode == AOM_CQ && rc->total_target_bits > 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700769 const double x = (double)rc->total_actual_bits / rc->total_target_bits;
770 if (x < cq_adjust_threshold) {
771 active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
772 }
773 }
774 return active_cq_level;
775}
776
Yaowu Xuf883b422016-08-30 14:01:10 -0700777static int rc_pick_q_and_bounds_one_pass_vbr(const AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 int *bottom_index,
779 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700780 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700781 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700782 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783 const int cq_level = get_active_cq_level(rc, oxcf);
784 int active_best_quality;
785 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
786 int q;
787 int *inter_minq;
788 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
789
790 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700791 if (oxcf->rc_mode == AOM_Q) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700792 const int qindex = cq_level;
793 const double q_val = av1_convert_qindex_to_q(qindex, cm->bit_depth);
794 const int delta_qindex =
795 av1_compute_qdelta(rc, q_val, q_val * 0.25, cm->bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700796 active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797 } else if (rc->this_key_frame_forced) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700798 const int qindex = rc->last_boosted_qindex;
799 const double last_boosted_q =
800 av1_convert_qindex_to_q(qindex, cm->bit_depth);
801 const int delta_qindex = av1_compute_qdelta(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802 rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700803 active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
Urvang Joshi454280d2016-10-14 16:51:44 -0700804 } else { // not first frame of one pass and kf_boost is set
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 double q_adj_factor = 1.0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806
807 active_best_quality = get_kf_active_quality(
808 rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
809
810 // Allow somewhat lower kf minq with small image formats.
811 if ((cm->width * cm->height) <= (352 * 288)) {
812 q_adj_factor -= 0.25;
813 }
814
Urvang Joshi454280d2016-10-14 16:51:44 -0700815 // Convert the adjustment factor to a qindex delta on active_best_quality.
816 {
817 const double q_val =
818 av1_convert_qindex_to_q(active_best_quality, cm->bit_depth);
819 active_best_quality +=
820 av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
821 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822 }
823 } else if (!rc->is_src_frame_alt_ref &&
824 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
825 // Use the lower of active_worst_quality and recent
826 // average Q as basis for GF/ARF best Q limit unless last frame was
827 // a key frame.
Urvang Joshi454280d2016-10-14 16:51:44 -0700828 q = (rc->frames_since_key > 1 &&
829 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
830 ? rc->avg_frame_qindex[INTER_FRAME]
831 : rc->avg_frame_qindex[KEY_FRAME];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832 // For constrained quality dont allow Q less than the cq level
Yaowu Xuf883b422016-08-30 14:01:10 -0700833 if (oxcf->rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700834 if (q < cq_level) q = cq_level;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700836 // Constrained quality use slightly lower active best.
837 active_best_quality = active_best_quality * 15 / 16;
Yaowu Xuf883b422016-08-30 14:01:10 -0700838 } else if (oxcf->rc_mode == AOM_Q) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700839 const int qindex = cq_level;
840 const double q_val = av1_convert_qindex_to_q(qindex, cm->bit_depth);
841 const int delta_qindex =
842 (cpi->refresh_alt_ref_frame)
843 ? av1_compute_qdelta(rc, q_val, q_val * 0.40, cm->bit_depth)
844 : av1_compute_qdelta(rc, q_val, q_val * 0.50, cm->bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700845 active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846 } else {
847 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
848 }
849 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700850 if (oxcf->rc_mode == AOM_Q) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700851 const int qindex = cq_level;
852 const double q_val = av1_convert_qindex_to_q(qindex, cm->bit_depth);
853 const double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
854 0.70, 1.0, 0.85, 1.0 };
855 const int delta_qindex = av1_compute_qdelta(
856 rc, q_val,
857 q_val * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL],
Yaowu Xuc27fc142016-08-22 16:08:15 -0700858 cm->bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700859 active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860 } else {
861 // Use the lower of active_worst_quality and recent/average Q.
Urvang Joshi454280d2016-10-14 16:51:44 -0700862 active_best_quality = (cm->current_video_frame > 1)
863 ? inter_minq[rc->avg_frame_qindex[INTER_FRAME]]
864 : inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865 // For the constrained quality mode we don't want
866 // q to fall below the cq level.
Yaowu Xuf883b422016-08-30 14:01:10 -0700867 if ((oxcf->rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 active_best_quality = cq_level;
869 }
870 }
871 }
872
873 // Clip the active best and worst quality values to limits
874 active_best_quality =
875 clamp(active_best_quality, rc->best_quality, rc->worst_quality);
876 active_worst_quality =
877 clamp(active_worst_quality, active_best_quality, rc->worst_quality);
878
879 *top_index = active_worst_quality;
880 *bottom_index = active_best_quality;
881
882 // Limit Q range for the adaptive loop.
883 {
884 int qdelta = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700885 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700886 if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
887 !(cm->current_video_frame == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700888 qdelta = av1_compute_qdelta_by_rate(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700889 &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
890 } else if (!rc->is_src_frame_alt_ref &&
891 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700892 qdelta = av1_compute_qdelta_by_rate(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth);
894 }
895 *top_index = active_worst_quality + qdelta;
Yaowu Xuf883b422016-08-30 14:01:10 -0700896 *top_index = AOMMAX(*top_index, *bottom_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897 }
898
Yaowu Xuf883b422016-08-30 14:01:10 -0700899 if (oxcf->rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900 q = active_best_quality;
901 // Special case code to try and match quality with forced key frames
902 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
903 q = rc->last_boosted_qindex;
904 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700905 q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
906 active_worst_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700907 if (q > *top_index) {
908 // Special case when we are targeting the max allowed rate
909 if (rc->this_frame_target >= rc->max_frame_bandwidth)
910 *top_index = q;
911 else
912 q = *top_index;
913 }
914 }
915
916 assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
917 assert(*bottom_index <= rc->worst_quality &&
918 *bottom_index >= rc->best_quality);
919 assert(q <= rc->worst_quality && q >= rc->best_quality);
920 return q;
921}
922
Yaowu Xuf883b422016-08-30 14:01:10 -0700923int av1_frame_type_qdelta(const AV1_COMP *cpi, int rf_level, int q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924 static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
925 1.00, // INTER_NORMAL
926#if CONFIG_EXT_REFS
927 0.80, // INTER_LOW
928 1.50, // INTER_HIGH
929 1.25, // GF_ARF_LOW
930#else
931 1.00, // INTER_HIGH
932 1.50, // GF_ARF_LOW
933#endif // CONFIG_EXT_REFS
934 2.00, // GF_ARF_STD
935 2.00, // KF_STD
936 };
937 static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] =
938#if CONFIG_EXT_REFS
939 { INTER_FRAME, INTER_FRAME, INTER_FRAME,
940 INTER_FRAME, INTER_FRAME, KEY_FRAME };
941#else
942 { INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME };
943#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -0700944 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945 int qdelta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700946 av1_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level], q,
947 rate_factor_deltas[rf_level], cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700948 return qdelta;
949}
950
951#define STATIC_MOTION_THRESH 95
Yaowu Xuf883b422016-08-30 14:01:10 -0700952static int rc_pick_q_and_bounds_two_pass(const AV1_COMP *cpi, int *bottom_index,
953 int *top_index) {
954 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700955 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700956 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
958 const int cq_level = get_active_cq_level(rc, oxcf);
959 int active_best_quality;
960 int active_worst_quality = cpi->twopass.active_worst_quality;
961 int q;
962 int *inter_minq;
963 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
964
965 if (frame_is_intra_only(cm)) {
966 // Handle the special case for key frames forced when we have reached
967 // the maximum key frame interval. Here force the Q to a range
968 // based on the ambient Q to reduce the risk of popping.
969 if (rc->this_key_frame_forced) {
970 double last_boosted_q;
971 int delta_qindex;
972 int qindex;
973
974 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700975 qindex = AOMMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700976 active_best_quality = qindex;
Yaowu Xuf883b422016-08-30 14:01:10 -0700977 last_boosted_q = av1_convert_qindex_to_q(qindex, cm->bit_depth);
978 delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
979 last_boosted_q * 1.25, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700980 active_worst_quality =
Yaowu Xuf883b422016-08-30 14:01:10 -0700981 AOMMIN(qindex + delta_qindex, active_worst_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700982 } else {
983 qindex = rc->last_boosted_qindex;
Yaowu Xuf883b422016-08-30 14:01:10 -0700984 last_boosted_q = av1_convert_qindex_to_q(qindex, cm->bit_depth);
985 delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
986 last_boosted_q * 0.75, cm->bit_depth);
987 active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700988 }
989 } else {
990 // Not forced keyframe.
991 double q_adj_factor = 1.0;
992 double q_val;
993
994 // Baseline value derived from cpi->active_worst_quality and kf boost.
995 active_best_quality =
996 get_kf_active_quality(rc, active_worst_quality, cm->bit_depth);
997
998 // Allow somewhat lower kf minq with small image formats.
999 if ((cm->width * cm->height) <= (352 * 288)) {
1000 q_adj_factor -= 0.25;
1001 }
1002
1003 // Make a further adjustment based on the kf zero motion measure.
1004 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
1005
1006 // Convert the adjustment factor to a qindex delta
1007 // on active_best_quality.
Yaowu Xuf883b422016-08-30 14:01:10 -07001008 q_val = av1_convert_qindex_to_q(active_best_quality, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001009 active_best_quality +=
Yaowu Xuf883b422016-08-30 14:01:10 -07001010 av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001011 }
Zoe Liue9b15e22017-07-19 15:53:01 -07001012 } else if (!rc->is_src_frame_alt_ref && (cpi->refresh_golden_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -07001013#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001014 cpi->refresh_alt2_ref_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -07001015#endif // CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001016 cpi->refresh_alt_ref_frame)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001017 // Use the lower of active_worst_quality and recent
1018 // average Q as basis for GF/ARF best Q limit unless last frame was
1019 // a key frame.
1020 if (rc->frames_since_key > 1 &&
1021 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1022 q = rc->avg_frame_qindex[INTER_FRAME];
1023 } else {
1024 q = active_worst_quality;
1025 }
1026 // For constrained quality dont allow Q less than the cq level
Yaowu Xuf883b422016-08-30 14:01:10 -07001027 if (oxcf->rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001028 if (q < cq_level) q = cq_level;
1029
1030 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1031
1032 // Constrained quality use slightly lower active best.
1033 active_best_quality = active_best_quality * 15 / 16;
1034
Yaowu Xuf883b422016-08-30 14:01:10 -07001035 } else if (oxcf->rc_mode == AOM_Q) {
Zoe Liu438b3ae2017-08-01 17:36:04 -07001036#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001037 if (!cpi->refresh_alt_ref_frame && !cpi->refresh_alt2_ref_frame) {
1038#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001039 if (!cpi->refresh_alt_ref_frame) {
Zoe Liu438b3ae2017-08-01 17:36:04 -07001040#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07001041 active_best_quality = cq_level;
1042 } else {
1043 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1044
Yaowu Xuf883b422016-08-30 14:01:10 -07001045 // Modify best quality for second level arfs. For mode AOM_Q this
Yaowu Xuc27fc142016-08-22 16:08:15 -07001046 // becomes the baseline frame q.
1047 if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
1048 active_best_quality = (active_best_quality + cq_level + 1) / 2;
1049 }
1050 } else {
1051 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1052 }
1053 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001054 if (oxcf->rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001055 active_best_quality = cq_level;
1056 } else {
1057 active_best_quality = inter_minq[active_worst_quality];
1058
1059 // For the constrained quality mode we don't want
1060 // q to fall below the cq level.
Yaowu Xuf883b422016-08-30 14:01:10 -07001061 if ((oxcf->rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001062 active_best_quality = cq_level;
1063 }
1064 }
1065 }
1066
1067 // Extension to max or min Q if undershoot or overshoot is outside
1068 // the permitted range.
Yaowu Xuf883b422016-08-30 14:01:10 -07001069 if ((cpi->oxcf.rc_mode != AOM_Q) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07001070 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) {
1071 if (frame_is_intra_only(cm) ||
Zoe Liue9b15e22017-07-19 15:53:01 -07001072 (!rc->is_src_frame_alt_ref && (cpi->refresh_golden_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -07001073#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001074 cpi->refresh_alt2_ref_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -07001075#endif // CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001076 cpi->refresh_alt_ref_frame))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001077 active_best_quality -=
1078 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
1079 active_worst_quality += (cpi->twopass.extend_maxq / 2);
1080 } else {
1081 active_best_quality -=
1082 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
1083 active_worst_quality += cpi->twopass.extend_maxq;
1084 }
1085 }
1086
Yaowu Xuf883b422016-08-30 14:01:10 -07001087 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07001088 // Static forced key frames Q restrictions dealt with elsewhere.
1089 if (!(frame_is_intra_only(cm)) || !rc->this_key_frame_forced ||
1090 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001091 int qdelta = av1_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index],
1092 active_worst_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001093 active_worst_quality =
Yaowu Xuf883b422016-08-30 14:01:10 -07001094 AOMMAX(active_worst_quality + qdelta, active_best_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001095 }
1096
1097 // Modify active_best_quality for downscaled normal frames.
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001098 if (!av1_frame_unscaled(cm) && !frame_is_kf_gf_arf(cpi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001099 int qdelta = av1_compute_qdelta_by_rate(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001100 rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth);
1101 active_best_quality =
Yaowu Xuf883b422016-08-30 14:01:10 -07001102 AOMMAX(active_best_quality + qdelta, rc->best_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001103 }
1104
1105 active_best_quality =
1106 clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1107 active_worst_quality =
1108 clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1109
Yaowu Xuf883b422016-08-30 14:01:10 -07001110 if (oxcf->rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001111 q = active_best_quality;
1112 // Special case code to try and match quality with forced key frames.
1113 } else if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
1114 // If static since last kf use better of last boosted and last kf q.
1115 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001116 q = AOMMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001117 } else {
1118 q = rc->last_boosted_qindex;
1119 }
1120 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001121 q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1122 active_worst_quality);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001123 if (q > active_worst_quality) {
1124 // Special case when we are targeting the max allowed rate.
1125 if (rc->this_frame_target >= rc->max_frame_bandwidth)
1126 active_worst_quality = q;
1127 else
1128 q = active_worst_quality;
1129 }
1130 }
1131 clamp(q, active_best_quality, active_worst_quality);
1132
1133 *top_index = active_worst_quality;
1134 *bottom_index = active_best_quality;
1135
1136 assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1137 assert(*bottom_index <= rc->worst_quality &&
1138 *bottom_index >= rc->best_quality);
1139 assert(q <= rc->worst_quality && q >= rc->best_quality);
1140 return q;
1141}
1142
Yaowu Xuf883b422016-08-30 14:01:10 -07001143int av1_rc_pick_q_and_bounds(const AV1_COMP *cpi, int *bottom_index,
1144 int *top_index) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001145 int q;
1146 if (cpi->oxcf.pass == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001147 if (cpi->oxcf.rc_mode == AOM_CBR)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001148 q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
1149 else
1150 q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
1151 } else {
1152 q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
1153 }
1154
1155 return q;
1156}
1157
Yaowu Xuf883b422016-08-30 14:01:10 -07001158void av1_rc_compute_frame_size_bounds(const AV1_COMP *cpi, int frame_target,
1159 int *frame_under_shoot_limit,
1160 int *frame_over_shoot_limit) {
1161 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001162 *frame_under_shoot_limit = 0;
1163 *frame_over_shoot_limit = INT_MAX;
1164 } else {
1165 // For very small rate targets where the fractional adjustment
1166 // may be tiny make sure there is at least a minimum range.
1167 const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100;
Yaowu Xuf883b422016-08-30 14:01:10 -07001168 *frame_under_shoot_limit = AOMMAX(frame_target - tolerance - 200, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001169 *frame_over_shoot_limit =
Yaowu Xuf883b422016-08-30 14:01:10 -07001170 AOMMIN(frame_target + tolerance + 200, cpi->rc.max_frame_bandwidth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001171 }
1172}
1173
Yaowu Xuf883b422016-08-30 14:01:10 -07001174void av1_rc_set_frame_target(AV1_COMP *cpi, int target) {
1175 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001176 RATE_CONTROL *const rc = &cpi->rc;
1177
1178 rc->this_frame_target = target;
1179
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07001180 // Modify frame size target when down-scaled.
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001181 if (!av1_frame_unscaled(cm))
Fergus Simpsonddc846e2017-04-24 18:09:13 -07001182 rc->this_frame_target =
1183 (int)(rc->this_frame_target * av1_resize_rate_factor(cpi));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001184
1185 // Target rate per SB64 (including partial SB64s.
James Zernbe0bdd82017-04-20 15:48:47 -07001186 rc->sb64_target_rate = (int)((int64_t)rc->this_frame_target * 64 * 64) /
1187 (cm->width * cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001188}
1189
Yaowu Xuf883b422016-08-30 14:01:10 -07001190static void update_alt_ref_frame_stats(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001191 // this frame refreshes means next frames don't unless specified by user
1192 RATE_CONTROL *const rc = &cpi->rc;
1193 rc->frames_since_golden = 0;
1194
1195 // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1196 rc->source_alt_ref_pending = 0;
1197
1198 // Set the alternate reference frame active flag
1199 rc->source_alt_ref_active = 1;
1200}
1201
Yaowu Xuf883b422016-08-30 14:01:10 -07001202static void update_golden_frame_stats(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001203 RATE_CONTROL *const rc = &cpi->rc;
1204
1205#if CONFIG_EXT_REFS
1206 // Update the Golden frame usage counts.
1207 // NOTE(weitinglin): If we use show_existing_frame for an OVERLAY frame,
1208 // only the virtual indices for the reference frame will be
1209 // updated and cpi->refresh_golden_frame will still be zero.
1210 if (cpi->refresh_golden_frame || rc->is_src_frame_alt_ref) {
Zoe Liue9b15e22017-07-19 15:53:01 -07001211#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001212 // Update the Golden frame usage counts.
1213 if (cpi->refresh_golden_frame) {
Zoe Liu6cfaff92016-10-18 17:12:11 -07001214#endif // CONFIG_EXT_REFS
1215
Yaowu Xuc27fc142016-08-22 16:08:15 -07001216#if CONFIG_EXT_REFS
1217 // We will not use internal overlay frames to replace the golden frame
1218 if (!rc->is_src_frame_ext_arf)
Zoe Liu6cfaff92016-10-18 17:12:11 -07001219#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220 // this frame refreshes means next frames don't unless specified by user
1221 rc->frames_since_golden = 0;
1222
1223 // If we are not using alt ref in the up and coming group clear the arf
1224 // active flag. In multi arf group case, if the index is not 0 then
1225 // we are overlaying a mid group arf so should not reset the flag.
1226 if (cpi->oxcf.pass == 2) {
1227 if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
1228 rc->source_alt_ref_active = 0;
1229 } else if (!rc->source_alt_ref_pending) {
1230 rc->source_alt_ref_active = 0;
1231 }
1232
1233 // Decrement count down till next gf
1234 if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1235
Zoe Liu438b3ae2017-08-01 17:36:04 -07001236#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001237 } else if (!cpi->refresh_alt_ref_frame && !cpi->refresh_alt2_ref_frame) {
1238#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001239 } else if (!cpi->refresh_alt_ref_frame) {
Zoe Liu438b3ae2017-08-01 17:36:04 -07001240#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07001241 // Decrement count down till next gf
1242 if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
1243
1244 rc->frames_since_golden++;
1245 }
1246}
1247
Yaowu Xuf883b422016-08-30 14:01:10 -07001248void av1_rc_postencode_update(AV1_COMP *cpi, uint64_t bytes_used) {
1249 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001250 RATE_CONTROL *const rc = &cpi->rc;
1251 const int qindex = cm->base_qindex;
1252
1253 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001254 av1_cyclic_refresh_postencode(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001255 }
1256
1257 // Update rate control heuristics
1258 rc->projected_frame_size = (int)(bytes_used << 3);
1259
1260 // Post encode loop adjustment of Q prediction.
Yaowu Xuf883b422016-08-30 14:01:10 -07001261 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001262
1263 // Keep a record of last Q and ambient average Q.
1264 if (cm->frame_type == KEY_FRAME) {
1265 rc->last_q[KEY_FRAME] = qindex;
1266 rc->avg_frame_qindex[KEY_FRAME] =
1267 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1268 } else {
1269 if (!rc->is_src_frame_alt_ref &&
Zoe Liue9b15e22017-07-19 15:53:01 -07001270 !(cpi->refresh_golden_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -07001271#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001272 cpi->refresh_alt2_ref_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -07001273#endif // CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001274 cpi->refresh_alt_ref_frame)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001275 rc->last_q[INTER_FRAME] = qindex;
1276 rc->avg_frame_qindex[INTER_FRAME] =
1277 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1278 rc->ni_frames++;
Yaowu Xuf883b422016-08-30 14:01:10 -07001279 rc->tot_q += av1_convert_qindex_to_q(qindex, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001280 rc->avg_q = rc->tot_q / rc->ni_frames;
1281 // Calculate the average Q for normal inter frames (not key or GFU
1282 // frames).
1283 rc->ni_tot_qi += qindex;
1284 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1285 }
1286 }
1287
1288 // Keep record of last boosted (KF/GF/ARF) Q value.
1289 // If the current frame is coded at a lower Q then we also update it.
1290 // If all mbs in this group are skipped only update if the Q value is
1291 // better than that already stored.
1292 // This is used to help set quality in forced key frames to reduce popping
1293 if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) ||
1294 (!rc->constrained_gf_group &&
1295 (cpi->refresh_alt_ref_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -07001296#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001297 cpi->refresh_alt2_ref_frame ||
Zoe Liu438b3ae2017-08-01 17:36:04 -07001298#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07001299 (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1300 rc->last_boosted_qindex = qindex;
1301 }
1302 if (cm->frame_type == KEY_FRAME) rc->last_kf_qindex = qindex;
1303
1304 update_buffer_level(cpi, rc->projected_frame_size);
1305
1306 // Rolling monitors of whether we are over or underspending used to help
1307 // regulate min and Max Q in two pass.
1308 if (cm->frame_type != KEY_FRAME) {
1309 rc->rolling_target_bits = ROUND_POWER_OF_TWO(
1310 rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1311 rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
1312 rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1313 rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
1314 rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1315 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
1316 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1317 }
1318
1319 // Actual bits spent
1320 rc->total_actual_bits += rc->projected_frame_size;
1321#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07001322 // TODO(zoeliu): To investigate whether we should treat BWDREF_FRAME
1323 // differently here for rc->avg_frame_bandwidth.
Yaowu Xuc27fc142016-08-22 16:08:15 -07001324 rc->total_target_bits +=
1325 (cm->show_frame || rc->is_bwd_ref_frame) ? rc->avg_frame_bandwidth : 0;
1326#else
1327 rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1328#endif // CONFIG_EXT_REFS
1329
1330 rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1331
1332 if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1333 (cm->frame_type != KEY_FRAME))
1334 // Update the alternate reference frame stats as appropriate.
1335 update_alt_ref_frame_stats(cpi);
1336 else
1337 // Update the Golden frame stats as appropriate.
1338 update_golden_frame_stats(cpi);
1339
1340 if (cm->frame_type == KEY_FRAME) rc->frames_since_key = 0;
1341
1342#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07001343 // TODO(zoeliu): To investigate whether we should treat BWDREF_FRAME
1344 // differently here for rc->avg_frame_bandwidth.
Yaowu Xuc27fc142016-08-22 16:08:15 -07001345 if (cm->show_frame || rc->is_bwd_ref_frame) {
1346#else
1347 if (cm->show_frame) {
1348#endif // CONFIG_EXT_REFS
1349 rc->frames_since_key++;
1350 rc->frames_to_key--;
1351 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001352}
1353
Yaowu Xuf883b422016-08-30 14:01:10 -07001354void av1_rc_postencode_update_drop_frame(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001355 // Update buffer level with zero size, update frame counters, and return.
1356 update_buffer_level(cpi, 0);
1357 cpi->rc.frames_since_key++;
1358 cpi->rc.frames_to_key--;
1359 cpi->rc.rc_2_frame = 0;
1360 cpi->rc.rc_1_frame = 0;
1361}
1362
1363// Use this macro to turn on/off use of alt-refs in one-pass mode.
1364#define USE_ALTREF_FOR_ONE_PASS 1
1365
Yaowu Xuf883b422016-08-30 14:01:10 -07001366static int calc_pframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001367 static const int af_ratio = 10;
1368 const RATE_CONTROL *const rc = &cpi->rc;
1369 int target;
1370#if USE_ALTREF_FOR_ONE_PASS
1371 target =
1372 (!rc->is_src_frame_alt_ref &&
1373 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
1374 ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
1375 (rc->baseline_gf_interval + af_ratio - 1)
1376 : (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
1377 (rc->baseline_gf_interval + af_ratio - 1);
1378#else
1379 target = rc->avg_frame_bandwidth;
1380#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001381 return av1_rc_clamp_pframe_target_size(cpi, target);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001382}
1383
Yaowu Xuf883b422016-08-30 14:01:10 -07001384static int calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001385 static const int kf_ratio = 25;
1386 const RATE_CONTROL *rc = &cpi->rc;
1387 const int target = rc->avg_frame_bandwidth * kf_ratio;
Yaowu Xuf883b422016-08-30 14:01:10 -07001388 return av1_rc_clamp_iframe_target_size(cpi, target);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001389}
1390
Yaowu Xuf883b422016-08-30 14:01:10 -07001391void av1_rc_get_one_pass_vbr_params(AV1_COMP *cpi) {
1392 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001393 RATE_CONTROL *const rc = &cpi->rc;
1394 int target;
1395 // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
1396 if (!cpi->refresh_alt_ref_frame &&
1397 (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1398 rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
1399 cm->frame_type = KEY_FRAME;
1400 rc->this_key_frame_forced =
1401 cm->current_video_frame != 0 && rc->frames_to_key == 0;
1402 rc->frames_to_key = cpi->oxcf.key_freq;
1403 rc->kf_boost = DEFAULT_KF_BOOST;
1404 rc->source_alt_ref_active = 0;
1405 } else {
1406 cm->frame_type = INTER_FRAME;
1407 }
1408 if (rc->frames_till_gf_update_due == 0) {
1409 rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
1410 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1411 // NOTE: frames_till_gf_update_due must be <= frames_to_key.
1412 if (rc->frames_till_gf_update_due > rc->frames_to_key) {
1413 rc->frames_till_gf_update_due = rc->frames_to_key;
1414 rc->constrained_gf_group = 1;
1415 } else {
1416 rc->constrained_gf_group = 0;
1417 }
1418 cpi->refresh_golden_frame = 1;
1419 rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
1420 rc->gfu_boost = DEFAULT_GF_BOOST;
1421 }
1422 if (cm->frame_type == KEY_FRAME)
1423 target = calc_iframe_target_size_one_pass_vbr(cpi);
1424 else
1425 target = calc_pframe_target_size_one_pass_vbr(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07001426 av1_rc_set_frame_target(cpi, target);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001427}
1428
Yaowu Xuf883b422016-08-30 14:01:10 -07001429static int calc_pframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
1430 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001431 const RATE_CONTROL *rc = &cpi->rc;
1432 const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
1433 const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
1434 int min_frame_target =
Yaowu Xuf883b422016-08-30 14:01:10 -07001435 AOMMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001436 int target;
1437
1438 if (oxcf->gf_cbr_boost_pct) {
1439 const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
1440 target = cpi->refresh_golden_frame
1441 ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval *
1442 af_ratio_pct) /
1443 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
1444 : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
1445 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
1446 } else {
1447 target = rc->avg_frame_bandwidth;
1448 }
1449
1450 if (diff > 0) {
1451 // Lower the target bandwidth for this frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07001452 const int pct_low = (int)AOMMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001453 target -= (target * pct_low) / 200;
1454 } else if (diff < 0) {
1455 // Increase the target bandwidth for this frame.
1456 const int pct_high =
Yaowu Xuf883b422016-08-30 14:01:10 -07001457 (int)AOMMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001458 target += (target * pct_high) / 200;
1459 }
1460 if (oxcf->rc_max_inter_bitrate_pct) {
1461 const int max_rate =
1462 rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
Yaowu Xuf883b422016-08-30 14:01:10 -07001463 target = AOMMIN(target, max_rate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001464 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001465 return AOMMAX(min_frame_target, target);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001466}
1467
Yaowu Xuf883b422016-08-30 14:01:10 -07001468static int calc_iframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001469 const RATE_CONTROL *rc = &cpi->rc;
1470 int target;
1471 if (cpi->common.current_video_frame == 0) {
1472 target = ((rc->starting_buffer_level / 2) > INT_MAX)
1473 ? INT_MAX
1474 : (int)(rc->starting_buffer_level / 2);
1475 } else {
1476 int kf_boost = 32;
1477 double framerate = cpi->framerate;
1478
Yaowu Xuf883b422016-08-30 14:01:10 -07001479 kf_boost = AOMMAX(kf_boost, (int)(2 * framerate - 16));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001480 if (rc->frames_since_key < framerate / 2) {
1481 kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
1482 }
1483 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
1484 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001485 return av1_rc_clamp_iframe_target_size(cpi, target);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001486}
1487
Yaowu Xuf883b422016-08-30 14:01:10 -07001488void av1_rc_get_one_pass_cbr_params(AV1_COMP *cpi) {
1489 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001490 RATE_CONTROL *const rc = &cpi->rc;
1491 int target;
1492 // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
1493 if ((cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1494 rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
1495 cm->frame_type = KEY_FRAME;
1496 rc->this_key_frame_forced =
1497 cm->current_video_frame != 0 && rc->frames_to_key == 0;
1498 rc->frames_to_key = cpi->oxcf.key_freq;
1499 rc->kf_boost = DEFAULT_KF_BOOST;
1500 rc->source_alt_ref_active = 0;
1501 } else {
1502 cm->frame_type = INTER_FRAME;
1503 }
1504 if (rc->frames_till_gf_update_due == 0) {
1505 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
Yaowu Xuf883b422016-08-30 14:01:10 -07001506 av1_cyclic_refresh_set_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001507 else
1508 rc->baseline_gf_interval =
1509 (rc->min_gf_interval + rc->max_gf_interval) / 2;
1510 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1511 // NOTE: frames_till_gf_update_due must be <= frames_to_key.
1512 if (rc->frames_till_gf_update_due > rc->frames_to_key)
1513 rc->frames_till_gf_update_due = rc->frames_to_key;
1514 cpi->refresh_golden_frame = 1;
1515 rc->gfu_boost = DEFAULT_GF_BOOST;
1516 }
1517
1518 // Any update/change of global cyclic refresh parameters (amount/delta-qp)
1519 // should be done here, before the frame qp is selected.
1520 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
Yaowu Xuf883b422016-08-30 14:01:10 -07001521 av1_cyclic_refresh_update_parameters(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001522
1523 if (cm->frame_type == KEY_FRAME)
1524 target = calc_iframe_target_size_one_pass_cbr(cpi);
1525 else
1526 target = calc_pframe_target_size_one_pass_cbr(cpi);
1527
Yaowu Xuf883b422016-08-30 14:01:10 -07001528 av1_rc_set_frame_target(cpi, target);
Fergus Simpson0757fd82017-04-28 20:14:27 -07001529 // TODO(afergs): Decide whether to scale up, down, or not at all
Yaowu Xuc27fc142016-08-22 16:08:15 -07001530}
1531
Yaowu Xuf883b422016-08-30 14:01:10 -07001532int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
1533 aom_bit_depth_t bit_depth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001534 int start_index = rc->worst_quality;
1535 int target_index = rc->worst_quality;
1536 int i;
1537
1538 // Convert the average q value to an index.
1539 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1540 start_index = i;
Yaowu Xuf883b422016-08-30 14:01:10 -07001541 if (av1_convert_qindex_to_q(i, bit_depth) >= qstart) break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001542 }
1543
1544 // Convert the q target to an index
1545 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1546 target_index = i;
Yaowu Xuf883b422016-08-30 14:01:10 -07001547 if (av1_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001548 }
1549
1550 return target_index - start_index;
1551}
1552
Yaowu Xuf883b422016-08-30 14:01:10 -07001553int av1_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
1554 int qindex, double rate_target_ratio,
1555 aom_bit_depth_t bit_depth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001556 int target_index = rc->worst_quality;
1557 int i;
1558
1559 // Look up the current projected bits per block for the base index
1560 const int base_bits_per_mb =
Yaowu Xuf883b422016-08-30 14:01:10 -07001561 av1_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001562
1563 // Find the target bits per mb based on the base value and given ratio.
1564 const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
1565
1566 // Convert the q target to an index
1567 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001568 if (av1_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
Yaowu Xuc27fc142016-08-22 16:08:15 -07001569 target_bits_per_mb) {
1570 target_index = i;
1571 break;
1572 }
1573 }
1574 return target_index - qindex;
1575}
1576
Yaowu Xuf883b422016-08-30 14:01:10 -07001577void av1_rc_set_gf_interval_range(const AV1_COMP *const cpi,
1578 RATE_CONTROL *const rc) {
1579 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001580
1581 // Special case code for 1 pass fixed Q mode tests
Yaowu Xuf883b422016-08-30 14:01:10 -07001582 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001583 rc->max_gf_interval = FIXED_GF_INTERVAL;
1584 rc->min_gf_interval = FIXED_GF_INTERVAL;
1585 rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
1586 } else {
1587 // Set Maximum gf/arf interval
1588 rc->max_gf_interval = oxcf->max_gf_interval;
1589 rc->min_gf_interval = oxcf->min_gf_interval;
1590 if (rc->min_gf_interval == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07001591 rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001592 oxcf->width, oxcf->height, cpi->framerate);
1593 if (rc->max_gf_interval == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07001594 rc->max_gf_interval = av1_rc_get_default_max_gf_interval(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001595 cpi->framerate, rc->min_gf_interval);
1596
1597 // Extended interval for genuinely static scenes
1598 rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
1599
1600 if (is_altref_enabled(cpi)) {
1601 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
1602 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
1603 }
1604
1605 if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1606 rc->max_gf_interval = rc->static_scene_max_gf_interval;
1607
1608 // Clamp min to max
Yaowu Xuf883b422016-08-30 14:01:10 -07001609 rc->min_gf_interval = AOMMIN(rc->min_gf_interval, rc->max_gf_interval);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001610 }
1611}
1612
Yaowu Xuf883b422016-08-30 14:01:10 -07001613void av1_rc_update_framerate(AV1_COMP *cpi) {
1614 const AV1_COMMON *const cm = &cpi->common;
1615 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001616 RATE_CONTROL *const rc = &cpi->rc;
1617 int vbr_max_bits;
1618
1619 rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
1620 rc->min_frame_bandwidth =
1621 (int)(rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
1622
1623 rc->min_frame_bandwidth =
Yaowu Xuf883b422016-08-30 14:01:10 -07001624 AOMMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001625
1626 // A maximum bitrate for a frame is defined.
1627 // The baseline for this aligns with HW implementations that
1628 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1629 // per 16x16 MB (averaged over a frame). However this limit is extended if
1630 // a very high rate is given on the command line or the the rate cannnot
1631 // be acheived because of a user specificed max q (e.g. when the user
1632 // specifies lossless encode.
1633 vbr_max_bits =
1634 (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section) /
1635 100);
1636 rc->max_frame_bandwidth =
Yaowu Xuf883b422016-08-30 14:01:10 -07001637 AOMMAX(AOMMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001638
Yaowu Xuf883b422016-08-30 14:01:10 -07001639 av1_rc_set_gf_interval_range(cpi, rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001640}
1641
1642#define VBR_PCT_ADJUSTMENT_LIMIT 50
1643// For VBR...adjustment to the frame target based on error from previous frames
Yaowu Xuf883b422016-08-30 14:01:10 -07001644static void vbr_rate_correction(AV1_COMP *cpi, int *this_frame_target) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001645 RATE_CONTROL *const rc = &cpi->rc;
1646 int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
1647 int max_delta;
1648 double position_factor = 1.0;
1649
1650 // How far through the clip are we.
1651 // This number is used to damp the per frame rate correction.
1652 // Range 0 - 1.0
1653 if (cpi->twopass.total_stats.count != 0.) {
1654 position_factor = sqrt((double)cpi->common.current_video_frame /
1655 cpi->twopass.total_stats.count);
1656 }
1657 max_delta = (int)(position_factor *
1658 ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
1659
1660 // vbr_bits_off_target > 0 means we have extra bits to spend
1661 if (vbr_bits_off_target > 0) {
1662 *this_frame_target += (vbr_bits_off_target > max_delta)
1663 ? max_delta
1664 : (int)vbr_bits_off_target;
1665 } else {
1666 *this_frame_target -= (vbr_bits_off_target < -max_delta)
1667 ? max_delta
1668 : (int)-vbr_bits_off_target;
1669 }
1670
1671 // Fast redistribution of bits arising from massive local undershoot.
1672 // Dont do it for kf,arf,gf or overlay frames.
1673 if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
1674 rc->vbr_bits_off_target_fast) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001675 int one_frame_bits = AOMMAX(rc->avg_frame_bandwidth, *this_frame_target);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001676 int fast_extra_bits;
Yaowu Xuf883b422016-08-30 14:01:10 -07001677 fast_extra_bits = (int)AOMMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
1678 fast_extra_bits = (int)AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001679 fast_extra_bits,
Yaowu Xuf883b422016-08-30 14:01:10 -07001680 AOMMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001681 *this_frame_target += (int)fast_extra_bits;
1682 rc->vbr_bits_off_target_fast -= fast_extra_bits;
1683 }
1684}
1685
Yaowu Xuf883b422016-08-30 14:01:10 -07001686void av1_set_target_rate(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001687 RATE_CONTROL *const rc = &cpi->rc;
1688 int target_rate = rc->base_frame_target;
1689
1690 // Correction to rate target based on prior over or under shoot.
Yaowu Xuf883b422016-08-30 14:01:10 -07001691 if (cpi->oxcf.rc_mode == AOM_VBR || cpi->oxcf.rc_mode == AOM_CQ)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001692 vbr_rate_correction(cpi, &target_rate);
Yaowu Xuf883b422016-08-30 14:01:10 -07001693 av1_rc_set_frame_target(cpi, target_rate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001694}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001695
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001696uint8_t av1_calculate_next_resize_scale(const AV1_COMP *cpi) {
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001697 static unsigned int seed = 56789;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001698 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07001699 if (oxcf->pass == 1) return SCALE_DENOMINATOR;
1700 uint8_t new_num = SCALE_DENOMINATOR;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001701
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001702 switch (oxcf->resize_mode) {
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07001703 case RESIZE_NONE: new_num = SCALE_DENOMINATOR; break;
Fergus Simpson87cf61b2017-06-15 00:50:34 -07001704 case RESIZE_FIXED:
1705 if (cpi->common.frame_type == KEY_FRAME)
1706 new_num = oxcf->resize_kf_scale_numerator;
1707 else
1708 new_num = oxcf->resize_scale_numerator;
1709 break;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001710 case RESIZE_DYNAMIC:
1711 // RESIZE_DYNAMIC: Just random for now.
1712 new_num = lcg_rand16(&seed) % 4 + 13;
1713 break;
1714 default: assert(0);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001715 }
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001716 return new_num;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001717}
1718
1719#if CONFIG_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001720// TODO(afergs): Rename av1_rc_update_superres_scale(...)?
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001721uint8_t av1_calculate_next_superres_scale(const AV1_COMP *cpi, int width,
1722 int height) {
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001723 static unsigned int seed = 34567;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001724 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07001725 if (oxcf->pass == 1) return SCALE_DENOMINATOR;
1726 uint8_t new_num = SCALE_DENOMINATOR;
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001727
1728 switch (oxcf->superres_mode) {
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07001729 case SUPERRES_NONE: new_num = SCALE_DENOMINATOR; break;
Fergus Simpson87cf61b2017-06-15 00:50:34 -07001730 case SUPERRES_FIXED:
1731 if (cpi->common.frame_type == KEY_FRAME)
1732 new_num = oxcf->superres_kf_scale_numerator;
1733 else
1734 new_num = oxcf->superres_scale_numerator;
1735 break;
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001736 case SUPERRES_DYNAMIC:
1737 // SUPERRES_DYNAMIC: Just random for now.
1738 new_num = lcg_rand16(&seed) % 9 + 8;
1739 break;
1740 default: assert(0);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001741 }
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001742
1743 // Make sure overall reduction is no more than 1/2 of the source size.
Fergus Simpsonb0157aa2017-06-15 00:16:32 -07001744 av1_calculate_scaled_size(&width, &height, new_num);
1745 if (width * 2 < oxcf->width || height * 2 < oxcf->height)
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07001746 new_num = SCALE_DENOMINATOR;
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001747
1748 return new_num;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001749}
1750#endif // CONFIG_FRAME_SUPERRES