12 #ifndef AOM_AV1_ENCODER_RC_UTILS_H_ 13 #define AOM_AV1_ENCODER_RC_UTILS_H_ 16 #include "aom_dsp/psnr.h" 22 static AOM_INLINE
void set_rc_buffer_sizes(
RATE_CONTROL *rc,
31 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
33 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
36 static AOM_INLINE
void config_target_level(
AV1_COMP *
const cpi,
37 AV1_LEVEL target_level,
int tier) {
38 aom_clear_system_state();
42 TileConfig *
const tile_cfg = &oxcf->tile_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);
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,
107 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
108 int force_recode = 0;
130 static 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);
140 static 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);
149 static 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);
157 static AOM_INLINE
int get_regulated_q_overshoot(
AV1_COMP *
const cpi,
int q_low,
158 int q_high,
int top_index,
170 while (q_regulated < q_low && retries < 10) {
180 static AOM_INLINE
int get_regulated_q_undershoot(
AV1_COMP *
const cpi,
181 int q_high,
int top_index,
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) {
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;
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 &&
270 const int64_t low_err_target = cpi->
ambient_err >> 1;
272 #if CONFIG_AV1_HIGHBITDEPTH 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);
390 *undershoot_seen = 1;
394 *q = clamp(*q, *q_low, *q_high);
397 *loop = (*q != last_q);
405 #endif // AOM_AV1_ENCODER_RC_UTILS_H_ RATE_CONTROL rc
Definition: encoder.h:2294
int over_shoot_pct
Definition: encoder.h:495
AV1_COMMON common
Definition: encoder.h:2138
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 cq_level
Definition: encoder.h:509
RateControlCfg rc_cfg
Definition: encoder.h:827
Rate Control parameters and status.
Definition: ratectrl.h:118
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.
Encoder rate control configuration parameters.
Definition: encoder.h:430
AV1EncoderConfig oxcf
Definition: encoder.h:2143
enum aom_rc_mode mode
Definition: encoder.h:514
YV12_BUFFER_CONFIG * source
Definition: encoder.h:2161
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
int64_t starting_buffer_level_ms
Definition: encoder.h:439
Two pass status and control data.
Definition: firstpass.h:214
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
SPEED_FEATURES sf
Definition: encoder.h:2314
int width
Definition: av1_common_int.h:748
int64_t ambient_err
Definition: encoder.h:2263
int worst_allowed_q
Definition: encoder.h:500
TWO_PASS twopass
Definition: encoder.h:2352
int64_t optimal_buffer_level
Definition: ratectrl.h:280
HIGH_LEVEL_SPEED_FEATURES hl_sf
Definition: speed_features.h:1124
Definition: aom_encoder.h:168
CurrentFrame current_frame
Definition: av1_common_int.h:727
int projected_frame_size
Definition: ratectrl.h:139
The stucture of acummulated frame stats in the first pass.
Definition: firstpass.h:36
int64_t maximum_buffer_size
Definition: ratectrl.h:284
SequenceHeader seq_params
Definition: av1_common_int.h:953
unsigned int min_cr
Definition: encoder.h:479
RefCntBuffer * cur_frame
Definition: av1_common_int.h:809
int64_t maximum_buffer_size_ms
Definition: encoder.h:449
int best_quality
Definition: ratectrl.h:271
int64_t target_bandwidth
Definition: encoder.h:454
double duration
Definition: firstpass.h:145
Top level encoder structure.
Definition: encoder.h:2095
Declares top-level encoder structures and functions.
Top level common structure used by both encoder and decoder.
Definition: av1_common_int.h:723
RECODE_LOOP_TYPE recode_loop
Definition: speed_features.h:319
Definition: aom_encoder.h:169
int64_t starting_buffer_level
Definition: ratectrl.h:276
int this_frame_target
Definition: ratectrl.h:129
int64_t optimal_buffer_level_ms
Definition: encoder.h:444
Main encoder configuration data structure.
Definition: encoder.h:805
int height
Definition: av1_common_int.h:749
int worst_quality
Definition: ratectrl.h:267