12#ifndef AOM_AV1_ENCODER_RC_UTILS_H_
13#define AOM_AV1_ENCODER_RC_UTILS_H_
16#include "aom_dsp/psnr.h"
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;
31 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
33 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
36static AOM_INLINE
void config_target_level(AV1_COMP *
const cpi,
37 AV1_LEVEL target_level,
int tier) {
38 aom_clear_system_state();
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;
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);
52 TWO_PASS *
const twopass = &cpi->twopass;
55 cpi->twopass.bits_left =
56 (int64_t)(stats->
duration * rc_cfg->target_bandwidth / 10000000.0);
59 rc_cfg->over_shoot_pct = 0;
62 rc_cfg->worst_allowed_q = 255;
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;
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;
78 const int still_picture = seq_params->still_picture;
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));
84#if !CONFIG_REALTIME_ONLY
103 int low_limit,
int q,
int maxq,
106 const AV1EncoderConfig *
const oxcf = &cpi->oxcf;
107 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
108 int force_recode = 0;
111 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE) ||
113 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
118 }
else if (cpi->oxcf.rc_cfg.mode ==
AOM_CQ) {
121 if (q > oxcf->rc_cfg.cq_level &&
130static AOM_INLINE
double av1_get_gfu_boost_projection_factor(
double min_factor,
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);
140static AOM_INLINE
int get_gfu_boost_from_r0_lap(
double min_factor,
141 double max_factor,
double r0,
143 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
145 const int boost = (int)rint(factor / r0);
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);
157static AOM_INLINE
int get_regulated_q_overshoot(AV1_COMP *
const cpi,
int q_low,
158 int q_high,
int top_index,
160 const AV1_COMMON *
const cm = &cpi->common;
170 while (q_regulated < q_low && retries < 10) {
180static AOM_INLINE
int get_regulated_q_undershoot(AV1_COMP *
const cpi,
181 int q_high,
int top_index,
183 const AV1_COMMON *
const cm = &cpi->common;
191 while (q_regulated > q_high && retries < 10) {
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;
229 const RateControlCfg *
const rc_cfg = &cpi->oxcf.rc_cfg;
233 if (rc->is_src_frame_alt_ref &&
237 const int min_cr = rc_cfg->min_cr;
239 aom_clear_system_state();
240 const double compression_ratio =
242 const double target_cr = min_cr / 100.0;
243 if (compression_ratio < target_cr) {
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);
254 if (*low_cr_seen)
return;
257 if (rc_cfg->mode ==
AOM_Q)
return;
259 const int last_q = *q;
260 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
262 &frame_under_shoot_limit,
263 &frame_over_shoot_limit);
264 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
266 if (cm->
current_frame.frame_type == KEY_FRAME && rc->this_key_frame_forced &&
269 const int64_t high_err_target = cpi->ambient_err;
270 const int64_t low_err_target = cpi->ambient_err >> 1;
272#if CONFIG_AV1_HIGHBITDEPTH
274 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->
cur_frame->buf);
276 kf_err = aom_get_y_sse(cpi->source, &cm->
cur_frame->buf);
279 kf_err = aom_get_y_sse(cpi->source, &cm->
cur_frame->buf);
286 if ((kf_err > high_err_target &&
288 (kf_err > low_err_target &&
291 *q_high = AOMMAX(*q - 1, *q_low);
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 &&
300 *q_low = AOMMIN(*q + 1, *q_high);
303 *q = (int)((*q * low_err_target) / kf_err);
304 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
308 *q = clamp(*q, *q_low, *q_high);
309 *loop = (*q != last_q);
313 if (
recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
314 AOMMAX(*q_high, top_index), bottom_index)) {
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 =
330 *q_high = av1_find_qindex(q_val_high_new, cm->
seq_params.bit_depth,
335 *q_low = AOMMIN(*q + 1, *q_high);
337 if (*undershoot_seen || loop_count > 2 ||
338 (loop_count == 2 && !frame_is_intra_only(cm))) {
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);
348 *q = (q_mid + q_regulated + 1) / 2;
350 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
357 *q_high = AOMMAX(*q - 1, *q_low);
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);
369 *q = (q_mid + q_regulated) / 2;
375 if (rc_cfg->mode ==
AOM_CQ && q_regulated < *q_low) {
379 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
385 if (rc_cfg->mode ==
AOM_CQ && *q < *q_low) {
390 *undershoot_seen = 1;
394 *q = clamp(*q, *q_low, *q_high);
397 *loop = (*q != last_q);
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
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
int width
Definition av1_common_int.h:748
RefCntBuffer * cur_frame
Definition av1_common_int.h:809
CurrentFrame current_frame
Definition av1_common_int.h:727
SequenceHeader seq_params
Definition av1_common_int.h:953
int height
Definition av1_common_int.h:749
The stucture of acummulated frame stats in the first pass.
Definition firstpass.h:36
double duration
Definition firstpass.h:145
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
Two pass status and control data.
Definition firstpass.h:214