AOMedia AV1 Codec
rc_utils.h
1/*
2 * Copyright (c) 2020, Alliance for Open Media. All rights reserved
3 *
4 * 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.
10 */
11
12#ifndef AOM_AV1_ENCODER_RC_UTILS_H_
13#define AOM_AV1_ENCODER_RC_UTILS_H_
14
15#include "av1/encoder/encoder.h"
16#include "aom_dsp/psnr.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22static AOM_INLINE void set_rc_buffer_sizes(RATE_CONTROL *rc,
23 const RateControlCfg *rc_cfg) {
24 const int64_t bandwidth = rc_cfg->target_bandwidth;
25 const int64_t starting = rc_cfg->starting_buffer_level_ms;
26 const int64_t optimal = rc_cfg->optimal_buffer_level_ms;
27 const int64_t maximum = rc_cfg->maximum_buffer_size_ms;
28
29 rc->starting_buffer_level = starting * bandwidth / 1000;
31 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
33 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
34}
35
36static AOM_INLINE void config_target_level(AV1_COMP *const cpi,
37 AV1_LEVEL target_level, int tier) {
38 aom_clear_system_state();
39
40 AV1EncoderConfig *const oxcf = &cpi->oxcf;
41 SequenceHeader *const seq_params = &cpi->common.seq_params;
42 TileConfig *const tile_cfg = &oxcf->tile_cfg;
43 RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
44
45 // Adjust target bitrate to be no larger than 70% of level limit.
46 const BITSTREAM_PROFILE profile = seq_params->profile;
47 const double level_bitrate_limit =
48 av1_get_max_bitrate_for_level(target_level, tier, profile);
49 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
50 rc_cfg->target_bandwidth = AOMMIN(rc_cfg->target_bandwidth, max_bitrate);
51 // Also need to update cpi->twopass.bits_left.
52 TWO_PASS *const twopass = &cpi->twopass;
53 FIRSTPASS_STATS *stats = twopass->stats_buf_ctx->total_stats;
54 if (stats != NULL)
55 cpi->twopass.bits_left =
56 (int64_t)(stats->duration * rc_cfg->target_bandwidth / 10000000.0);
57
58 // Adjust max over-shoot percentage.
59 rc_cfg->over_shoot_pct = 0;
60
61 // Adjust max quantizer.
62 rc_cfg->worst_allowed_q = 255;
63
64 // Adjust number of tiles and tile columns to be under level limit.
65 int max_tiles, max_tile_cols;
66 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
67 while (tile_cfg->tile_columns > 0 &&
68 (1 << tile_cfg->tile_columns) > max_tile_cols) {
69 --tile_cfg->tile_columns;
70 }
71 const int tile_cols = (1 << tile_cfg->tile_columns);
72 while (tile_cfg->tile_rows > 0 &&
73 tile_cols * (1 << tile_cfg->tile_rows) > max_tiles) {
74 --tile_cfg->tile_rows;
75 }
76
77 // Adjust min compression ratio.
78 const int still_picture = seq_params->still_picture;
79 const double min_cr =
80 av1_get_min_cr_for_level(target_level, tier, still_picture);
81 rc_cfg->min_cr = AOMMAX(rc_cfg->min_cr, (unsigned int)(min_cr * 100));
82}
83
84#if !CONFIG_REALTIME_ONLY
85
102static AOM_INLINE int recode_loop_test(AV1_COMP *cpi, int high_limit,
103 int low_limit, int q, int maxq,
104 int minq) {
105 const RATE_CONTROL *const rc = &cpi->rc;
106 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
107 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
108 int force_recode = 0;
109
110 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
111 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE) ||
112 (frame_is_kfgfarf &&
113 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
114 // TODO(agrange) high_limit could be greater than the scale-down threshold.
115 if ((rc->projected_frame_size > high_limit && q < maxq) ||
116 (rc->projected_frame_size < low_limit && q > minq)) {
117 force_recode = 1;
118 } else if (cpi->oxcf.rc_cfg.mode == AOM_CQ) {
119 // Deal with frame undershoot and whether or not we are
120 // below the automatically set cq level.
121 if (q > oxcf->rc_cfg.cq_level &&
122 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
123 force_recode = 1;
124 }
125 }
126 }
127 return force_recode;
128}
129
130static AOM_INLINE double av1_get_gfu_boost_projection_factor(double min_factor,
131 double max_factor,
132 int frame_count) {
133 double factor = sqrt((double)frame_count);
134 factor = AOMMIN(factor, max_factor);
135 factor = AOMMAX(factor, min_factor);
136 factor = (200.0 + 10.0 * factor);
137 return factor;
138}
139
140static AOM_INLINE int get_gfu_boost_from_r0_lap(double min_factor,
141 double max_factor, double r0,
142 int frames_to_key) {
143 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
144 frames_to_key);
145 const int boost = (int)rint(factor / r0);
146 return boost;
147}
148
149static AOM_INLINE double av1_get_kf_boost_projection_factor(int frame_count) {
150 double factor = sqrt((double)frame_count);
151 factor = AOMMIN(factor, 10.0);
152 factor = AOMMAX(factor, 4.0);
153 factor = (75.0 + 14.0 * factor);
154 return factor;
155}
156
157static AOM_INLINE int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low,
158 int q_high, int top_index,
159 int bottom_index) {
160 const AV1_COMMON *const cm = &cpi->common;
161 const RATE_CONTROL *const rc = &cpi->rc;
162
164
165 int q_regulated =
166 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
167 AOMMAX(q_high, top_index), cm->width, cm->height);
168
169 int retries = 0;
170 while (q_regulated < q_low && retries < 10) {
172 q_regulated =
173 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
174 AOMMAX(q_high, top_index), cm->width, cm->height);
175 retries++;
176 }
177 return q_regulated;
178}
179
180static AOM_INLINE int get_regulated_q_undershoot(AV1_COMP *const cpi,
181 int q_high, int top_index,
182 int bottom_index) {
183 const AV1_COMMON *const cm = &cpi->common;
184 const RATE_CONTROL *const rc = &cpi->rc;
185
187 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
188 top_index, cm->width, cm->height);
189
190 int retries = 0;
191 while (q_regulated > q_high && retries < 10) {
193 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
194 top_index, cm->width, cm->height);
195 retries++;
196 }
197 return q_regulated;
198}
199
222static AOM_INLINE void recode_loop_update_q(
223 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
224 int *const q_high, const int top_index, const int bottom_index,
225 int *const undershoot_seen, int *const overshoot_seen,
226 int *const low_cr_seen, const int loop_count) {
227 AV1_COMMON *const cm = &cpi->common;
228 RATE_CONTROL *const rc = &cpi->rc;
229 const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg;
230 *loop = 0;
231
232 // Special case for overlay frame.
233 if (rc->is_src_frame_alt_ref &&
234 rc->projected_frame_size < rc->max_frame_bandwidth)
235 return;
236
237 const int min_cr = rc_cfg->min_cr;
238 if (min_cr > 0) {
239 aom_clear_system_state();
240 const double compression_ratio =
241 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
242 const double target_cr = min_cr / 100.0;
243 if (compression_ratio < target_cr) {
244 *low_cr_seen = 1;
245 if (*q < rc->worst_quality) {
246 const double cr_ratio = target_cr / compression_ratio;
247 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
248 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
249 *q_low = AOMMAX(*q, *q_low);
250 *q_high = AOMMAX(*q, *q_high);
251 *loop = 1;
252 }
253 }
254 if (*low_cr_seen) return;
255 }
256
257 if (rc_cfg->mode == AOM_Q) return;
258
259 const int last_q = *q;
260 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
261 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
262 &frame_under_shoot_limit,
263 &frame_over_shoot_limit);
264 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
265
266 if (cm->current_frame.frame_type == KEY_FRAME && rc->this_key_frame_forced &&
267 rc->projected_frame_size < rc->max_frame_bandwidth) {
268 int64_t kf_err;
269 const int64_t high_err_target = cpi->ambient_err;
270 const int64_t low_err_target = cpi->ambient_err >> 1;
271
272#if CONFIG_AV1_HIGHBITDEPTH
273 if (cm->seq_params.use_highbitdepth) {
274 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
275 } else {
276 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
277 }
278#else
279 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
280#endif
281 // Prevent possible divide by zero error below for perfect KF
282 kf_err += !kf_err;
283
284 // The key frame is not good enough or we can afford
285 // to make it better without undue risk of popping.
286 if ((kf_err > high_err_target &&
287 rc->projected_frame_size <= frame_over_shoot_limit) ||
288 (kf_err > low_err_target &&
289 rc->projected_frame_size <= frame_under_shoot_limit)) {
290 // Lower q_high
291 *q_high = AOMMAX(*q - 1, *q_low);
292
293 // Adjust Q
294 *q = (int)((*q * high_err_target) / kf_err);
295 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
296 } else if (kf_err < low_err_target &&
297 rc->projected_frame_size >= frame_under_shoot_limit) {
298 // The key frame is much better than the previous frame
299 // Raise q_low
300 *q_low = AOMMIN(*q + 1, *q_high);
301
302 // Adjust Q
303 *q = (int)((*q * low_err_target) / kf_err);
304 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
305 }
306
307 // Clamp Q to upper and lower limits:
308 *q = clamp(*q, *q_low, *q_high);
309 *loop = (*q != last_q);
310 return;
311 }
312
313 if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
314 AOMMAX(*q_high, top_index), bottom_index)) {
315 // Is the projected frame size out of range and are we allowed
316 // to attempt to recode.
317
318 // Frame size out of permitted range:
319 // Update correction factor & compute new Q to try...
320 // Frame is too large
322 // Special case if the projected size is > the max allowed.
323 if (*q == *q_high &&
324 rc->projected_frame_size >= rc->max_frame_bandwidth) {
325 const double q_val_high_current =
326 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
327 const double q_val_high_new =
328 q_val_high_current *
329 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
330 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
331 rc->best_quality, rc->worst_quality);
332 }
333
334 // Raise Qlow as to at least the current value
335 *q_low = AOMMIN(*q + 1, *q_high);
336
337 if (*undershoot_seen || loop_count > 2 ||
338 (loop_count == 2 && !frame_is_intra_only(cm))) {
340
341 *q = (*q_high + *q_low + 1) / 2;
342 } else if (loop_count == 2 && frame_is_intra_only(cm)) {
343 const int q_mid = (*q_high + *q_low + 1) / 2;
344 const int q_regulated = get_regulated_q_overshoot(
345 cpi, *q_low, *q_high, top_index, bottom_index);
346 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
347 // transition between loop_count < 2 and loop_count > 2.
348 *q = (q_mid + q_regulated + 1) / 2;
349 } else {
350 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
351 bottom_index);
352 }
353
354 *overshoot_seen = 1;
355 } else {
356 // Frame is too small
357 *q_high = AOMMAX(*q - 1, *q_low);
358
359 if (*overshoot_seen || loop_count > 2 ||
360 (loop_count == 2 && !frame_is_intra_only(cm))) {
362 *q = (*q_high + *q_low) / 2;
363 } else if (loop_count == 2 && frame_is_intra_only(cm)) {
364 const int q_mid = (*q_high + *q_low) / 2;
365 const int q_regulated =
366 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
367 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
368 // transition between loop_count < 2 and loop_count > 2.
369 *q = (q_mid + q_regulated) / 2;
370
371 // Special case reset for qlow for constrained quality.
372 // This should only trigger where there is very substantial
373 // undershoot on a frame and the auto cq level is above
374 // the user passsed in value.
375 if (rc_cfg->mode == AOM_CQ && q_regulated < *q_low) {
376 *q_low = *q;
377 }
378 } else {
379 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
380
381 // Special case reset for qlow for constrained quality.
382 // This should only trigger where there is very substantial
383 // undershoot on a frame and the auto cq level is above
384 // the user passsed in value.
385 if (rc_cfg->mode == AOM_CQ && *q < *q_low) {
386 *q_low = *q;
387 }
388 }
389
390 *undershoot_seen = 1;
391 }
392
393 // Clamp Q to upper and lower limits:
394 *q = clamp(*q, *q_low, *q_high);
395 }
396
397 *loop = (*q != last_q);
398}
399#endif
400
401#ifdef __cplusplus
402} // extern "C"
403#endif
404
405#endif // AOM_AV1_ENCODER_RC_UTILS_H_
Declares top-level encoder structures and functions.
@ AOM_CQ
Definition: aom_encoder.h:168
@ AOM_Q
Definition: aom_encoder.h:169
static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q, int maxq, int minq)
Function to test for conditions that indicate we should loop back and recode a frame.
Definition: rc_utils.h:102
static void recode_loop_update_q(AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low, int *const q_high, const int top_index, const int bottom_index, int *const undershoot_seen, int *const overshoot_seen, int *const low_cr_seen, const int loop_count)
Called after encode_with_recode_loop() has just encoded a frame. This function works out whether we u...
Definition: rc_utils.h:222
int av1_rc_regulate_q(const struct AV1_COMP *cpi, int target_bits_per_frame, int active_best_quality, int active_worst_quality, int width, int height)
Estimates q to achieve a target bits per frame.
void av1_rc_update_rate_correction_factors(AV1_COMP *cpi, int width, int height)
Updates the rate correction factor linking Q to output bits.
Definition: ratectrl.c:544
Top level common structure used by both encoder and decoder.
Definition: av1_common_int.h:725
int width
Definition: av1_common_int.h:750
RefCntBuffer * cur_frame
Definition: av1_common_int.h:811
CurrentFrame current_frame
Definition: av1_common_int.h:729
SequenceHeader seq_params
Definition: av1_common_int.h:955
int height
Definition: av1_common_int.h:751
Main encoder configuration data structure.
Definition: encoder.h:805
RateControlCfg rc_cfg
Definition: encoder.h:827
Top level encoder structure.
Definition: encoder.h:2095
RATE_CONTROL rc
Definition: encoder.h:2294
int64_t ambient_err
Definition: encoder.h:2263
SPEED_FEATURES sf
Definition: encoder.h:2314
AV1EncoderConfig oxcf
Definition: encoder.h:2143
AV1_COMMON common
Definition: encoder.h:2138
TWO_PASS twopass
Definition: encoder.h:2352
YV12_BUFFER_CONFIG * source
Definition: encoder.h:2161
The stucture of acummulated frame stats in the first pass.
Definition: firstpass.h:36
double duration
Definition: firstpass.h:145
RECODE_LOOP_TYPE recode_loop
Definition: speed_features.h:319
Rate Control parameters and status.
Definition: ratectrl.h:118
int best_quality
Definition: ratectrl.h:271
int64_t starting_buffer_level
Definition: ratectrl.h:276
int this_frame_target
Definition: ratectrl.h:129
int projected_frame_size
Definition: ratectrl.h:139
int worst_quality
Definition: ratectrl.h:267
int64_t maximum_buffer_size
Definition: ratectrl.h:284
int64_t optimal_buffer_level
Definition: ratectrl.h:280
Encoder rate control configuration parameters.
Definition: encoder.h:430
int worst_allowed_q
Definition: encoder.h:500
int over_shoot_pct
Definition: encoder.h:495
int64_t maximum_buffer_size_ms
Definition: encoder.h:449
unsigned int min_cr
Definition: encoder.h:479
enum aom_rc_mode mode
Definition: encoder.h:514
int64_t starting_buffer_level_ms
Definition: encoder.h:439
int64_t target_bandwidth
Definition: encoder.h:454
int64_t optimal_buffer_level_ms
Definition: encoder.h:444
int cq_level
Definition: encoder.h:509
HIGH_LEVEL_SPEED_FEATURES hl_sf
Definition: speed_features.h:1124
Two pass status and control data.
Definition: firstpass.h:214