12#ifndef AOM_AV1_ENCODER_RC_UTILS_H_
13#define AOM_AV1_ENCODER_RC_UTILS_H_
16#include "aom_dsp/psnr.h"
22static inline void check_reset_rc_flag(
AV1_COMP *cpi) {
26 (
unsigned int)cpi->
svc.number_spatial_layers) {
28 av1_svc_check_reset_layer_rc_flag(cpi);
30 if (rc->avg_frame_bandwidth / 3 > (rc->prev_avg_frame_bandwidth >> 1) ||
31 rc->avg_frame_bandwidth < (rc->prev_avg_frame_bandwidth >> 1)) {
53 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
55 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
64static inline void config_target_level(
AV1_COMP *
const cpi,
65 AV1_LEVEL target_level,
int tier) {
68 TileConfig *
const tile_cfg = &oxcf->tile_cfg;
72 const BITSTREAM_PROFILE profile = seq_params->profile;
73 const double level_bitrate_limit =
74 av1_get_max_bitrate_for_level(target_level, tier, profile);
75 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
91 int max_tiles, max_tile_cols;
92 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
93 while (tile_cfg->tile_columns > 0 &&
94 (1 << tile_cfg->tile_columns) > max_tile_cols) {
95 --tile_cfg->tile_columns;
97 const int tile_cols = (1 << tile_cfg->tile_columns);
98 while (tile_cfg->tile_rows > 0 &&
99 tile_cols * (1 << tile_cfg->tile_rows) > max_tiles) {
100 --tile_cfg->tile_rows;
104 const int still_picture = seq_params->still_picture;
105 const double min_cr =
106 av1_get_min_cr_for_level(target_level, tier, still_picture);
107 rc_cfg->
min_cr = AOMMAX(rc_cfg->
min_cr, (
unsigned int)(min_cr * 100));
110#if !CONFIG_REALTIME_ONLY
129 int q,
int maxq,
int minq) {
132 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
133 int force_recode = 0;
156static inline double av1_get_gfu_boost_projection_factor(
double min_factor,
159 double factor = sqrt((
double)frame_count);
160 factor = AOMMIN(factor, max_factor);
161 factor = AOMMAX(factor, min_factor);
162 factor = (200.0 + 10.0 * factor);
166static inline int get_gfu_boost_from_r0_lap(
double min_factor,
167 double max_factor,
double r0,
169 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
171 const int boost = (int)rint(factor / r0);
175static inline double av1_get_kf_boost_projection_factor(
int frame_count) {
176 double factor = sqrt((
double)frame_count);
177 factor = AOMMIN(factor, 10.0);
178 factor = AOMMAX(factor, 4.0);
179 factor = (75.0 + 14.0 * factor);
183static inline int get_regulated_q_overshoot(
AV1_COMP *
const cpi,
184 int is_encode_stage,
int q_low,
185 int q_high,
int top_index,
198 while (q_regulated < q_low && retries < 10) {
209static inline int get_regulated_q_undershoot(
AV1_COMP *
const cpi,
210 int is_encode_stage,
int q_high,
211 int top_index,
int bottom_index) {
221 while (q_regulated > q_high && retries < 10) {
254 AV1_COMP *
const cpi,
int *
const loop,
int *
const q,
int *
const q_low,
255 int *
const q_high,
const int top_index,
const int bottom_index,
256 int *
const undershoot_seen,
int *
const overshoot_seen,
257 int *
const low_cr_seen,
const int loop_count) {
265 if (rc->is_src_frame_alt_ref &&
269 const int min_cr = rc_cfg->
min_cr;
271 const double compression_ratio =
273 const double target_cr = min_cr / 100.0;
274 if (compression_ratio < target_cr) {
276 if (*q < rc->worst_quality) {
277 const double cr_ratio = target_cr / compression_ratio;
278 const int projected_q = AOMMAX(*q + 1, (
int)(*q * cr_ratio * cr_ratio));
279 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->
worst_quality);
280 *q_low = AOMMAX(*q, *q_low);
281 *q_high = AOMMAX(*q, *q_high);
285 if (*low_cr_seen)
return;
289 !is_stat_generation_stage(cpi)) {
293 av1_init_level_info(cpi);
297 const AV1LevelInfo *
const level_info = level_params->level_info[0];
298 const DECODER_MODEL *
const decoder_models = level_info->decoder_models;
299 const AV1_LEVEL target_level = level_params->target_seq_level_idx[0];
301 if (target_level < SEQ_LEVELS &&
302 decoder_models[target_level].status == DECODER_MODEL_OK) {
303 DECODER_MODEL_STATUS status = av1_decoder_model_try_smooth_buf(
306 if ((status == SMOOTHING_BUFFER_UNDERFLOW ||
307 status == SMOOTHING_BUFFER_OVERFLOW) &&
308 *q < rc->worst_quality) {
310 *q_low = AOMMAX(*q, *q_low);
311 *q_high = AOMMAX(*q, *q_high);
320 const int last_q = *q;
321 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
323 &frame_under_shoot_limit,
324 &frame_over_shoot_limit);
325 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
328 p_rc->this_key_frame_forced &&
332 const int64_t low_err_target = cpi->
ambient_err >> 1;
334#if CONFIG_AV1_HIGHBITDEPTH
348 if ((kf_err > high_err_target &&
350 (kf_err > low_err_target &&
353 *q_high = AOMMAX(*q - 1, *q_low);
356 *q = (int)((*q * high_err_target) / kf_err);
357 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
358 }
else if (kf_err < low_err_target &&
362 *q_low = AOMMIN(*q + 1, *q_high);
365 *q = (int)((*q * low_err_target) / kf_err);
366 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
370 *q = clamp(*q, *q_low, *q_high);
371 *loop = (*q != last_q);
375 if (
recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
376 AOMMAX(*q_high, top_index), bottom_index)) {
387 const double q_val_high_current =
388 av1_convert_qindex_to_q(*q_high, cm->
seq_params->bit_depth);
389 const double q_val_high_new =
392 *q_high = av1_find_qindex(q_val_high_new, cm->
seq_params->bit_depth,
397 *q_low = AOMMIN(*q + 1, *q_high);
399 if (*undershoot_seen || loop_count > 2 ||
400 (loop_count == 2 && !frame_is_intra_only(cm))) {
403 *q = (*q_high + *q_low + 1) / 2;
404 }
else if (loop_count == 2 && frame_is_intra_only(cm)) {
405 const int q_mid = (*q_high + *q_low + 1) / 2;
406 const int q_regulated = get_regulated_q_overshoot(
407 cpi, 1, *q_low, *q_high, top_index, bottom_index);
410 *q = (q_mid + q_regulated + 1) / 2;
412 *q = get_regulated_q_overshoot(cpi, 1, *q_low, *q_high, top_index,
419 *q_high = AOMMAX(*q - 1, *q_low);
421 if (*overshoot_seen || loop_count > 2 ||
422 (loop_count == 2 && !frame_is_intra_only(cm))) {
424 *q = (*q_high + *q_low) / 2;
425 }
else if (loop_count == 2 && frame_is_intra_only(cm)) {
426 const int q_mid = (*q_high + *q_low) / 2;
427 const int q_regulated = get_regulated_q_undershoot(
428 cpi, 1, *q_high, top_index, bottom_index);
431 *q = (q_mid + q_regulated) / 2;
437 if (rc_cfg->
mode ==
AOM_CQ && q_regulated < *q_low) {
441 *q = get_regulated_q_undershoot(cpi, 1, *q_high, top_index,
453 *undershoot_seen = 1;
457 *q = clamp(*q, *q_low, *q_high);
460 *loop = (*q != last_q);
Declares top-level encoder structures and functions.
@ AOM_CQ
Definition aom_encoder.h:188
@ AOM_Q
Definition aom_encoder.h:189
void av1_rc_update_rate_correction_factors(AV1_COMP *cpi, int is_encode_stage, int width, int height)
Updates the rate correction factor linking Q to output bits.
Definition ratectrl.c:881
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:128
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:253
Top level common structure used by both encoder and decoder.
Definition av1_common_int.h:757
SequenceHeader * seq_params
Definition av1_common_int.h:983
int width
Definition av1_common_int.h:782
RefCntBuffer * cur_frame
Definition av1_common_int.h:839
CurrentFrame current_frame
Definition av1_common_int.h:761
int height
Definition av1_common_int.h:783
Main encoder configuration data structure.
Definition encoder.h:925
RateControlCfg rc_cfg
Definition encoder.h:947
Top level encoder structure.
Definition encoder.h:2870
RATE_CONTROL rc
Definition encoder.h:3077
int64_t ambient_err
Definition encoder.h:3046
SPEED_FEATURES sf
Definition encoder.h:3097
unsigned char gf_frame_index
Definition encoder.h:3128
AV1EncoderConfig oxcf
Definition encoder.h:2918
AV1_COMMON common
Definition encoder.h:2913
AV1_PRIMARY * ppi
Definition encoder.h:2874
YV12_BUFFER_CONFIG * source
Definition encoder.h:2931
SVC svc
Definition encoder.h:3411
Top level primary encoder structure.
Definition encoder.h:2574
AV1LevelParams level_params
Definition encoder.h:2705
TWO_PASS twopass
Definition encoder.h:2720
PRIMARY_RATE_CONTROL p_rc
Definition encoder.h:2725
int use_svc
Definition encoder.h:2740
GF_GROUP gf_group
Definition encoder.h:2690
The stucture of acummulated frame stats in the first pass.
Definition firstpass.h:43
double duration
Definition firstpass.h:144
RECODE_LOOP_TYPE recode_loop
Definition speed_features.h:414
Primary Rate Control parameters and status.
Definition ratectrl.h:300
int64_t bits_off_target
Definition ratectrl.h:555
int64_t maximum_buffer_size
Definition ratectrl.h:357
int64_t starting_buffer_level
Definition ratectrl.h:347
int64_t buffer_level
Definition ratectrl.h:534
int64_t optimal_buffer_level
Definition ratectrl.h:352
Rate Control parameters and status.
Definition ratectrl.h:134
int best_quality
Definition ratectrl.h:234
int this_frame_target
Definition ratectrl.h:145
int projected_frame_size
Definition ratectrl.h:150
int worst_quality
Definition ratectrl.h:230
Encoder rate control configuration parameters.
Definition encoder.h:523
int worst_allowed_q
Definition encoder.h:593
int over_shoot_pct
Definition encoder.h:588
int64_t maximum_buffer_size_ms
Definition encoder.h:542
unsigned int min_cr
Definition encoder.h:572
enum aom_rc_mode mode
Definition encoder.h:607
int64_t starting_buffer_level_ms
Definition encoder.h:532
int64_t target_bandwidth
Definition encoder.h:547
int64_t optimal_buffer_level_ms
Definition encoder.h:537
int cq_level
Definition encoder.h:602
HIGH_LEVEL_SPEED_FEATURES hl_sf
Definition speed_features.h:1943
Two pass status and control data.
Definition firstpass.h:416