#ifndef AOM_AV1_ENCODER_RC_UTILS_H_
#define AOM_AV1_ENCODER_RC_UTILS_H_
#include "av1/encoder/encoder.h"
#include "aom_dsp/psnr.h"
#ifdef __cplusplus
extern "C" {
#endif
static inline void check_reset_rc_flag(AV1_COMP *cpi) { … }
static inline void set_primary_rc_buffer_sizes(const AV1EncoderConfig *oxcf,
AV1_PRIMARY *ppi) { … }
static inline void config_target_level(AV1_COMP *const cpi,
AV1_LEVEL target_level, int tier) { … }
#if !CONFIG_REALTIME_ONLY
static inline int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit,
int q, int maxq, int minq) {
const RATE_CONTROL *const rc = &cpi->rc;
const AV1EncoderConfig *const oxcf = &cpi->oxcf;
const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
int force_recode = 0;
if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
(cpi->sf.hl_sf.recode_loop == ALLOW_RECODE) ||
(frame_is_kfgfarf &&
(cpi->sf.hl_sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
if ((rc->projected_frame_size > high_limit && q < maxq) ||
(rc->projected_frame_size < low_limit && q > minq)) {
force_recode = 1;
} else if (cpi->oxcf.rc_cfg.mode == AOM_CQ) {
if (q > oxcf->rc_cfg.cq_level &&
rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
force_recode = 1;
}
}
}
return force_recode;
}
static inline double av1_get_gfu_boost_projection_factor(double min_factor,
double max_factor,
int frame_count) {
double factor = sqrt((double)frame_count);
factor = AOMMIN(factor, max_factor);
factor = AOMMAX(factor, min_factor);
factor = (200.0 + 10.0 * factor);
return factor;
}
static inline int get_gfu_boost_from_r0_lap(double min_factor,
double max_factor, double r0,
int frames_to_key) {
double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
frames_to_key);
const int boost = (int)rint(factor / r0);
return boost;
}
static inline double av1_get_kf_boost_projection_factor(int frame_count) {
double factor = sqrt((double)frame_count);
factor = AOMMIN(factor, 10.0);
factor = AOMMAX(factor, 4.0);
factor = (75.0 + 14.0 * factor);
return factor;
}
static inline int get_regulated_q_overshoot(AV1_COMP *const cpi,
int is_encode_stage, int q_low,
int q_high, int top_index,
int bottom_index) {
const AV1_COMMON *const cm = &cpi->common;
const RATE_CONTROL *const rc = &cpi->rc;
av1_rc_update_rate_correction_factors(cpi, is_encode_stage, cm->width,
cm->height);
int q_regulated =
av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
AOMMAX(q_high, top_index), cm->width, cm->height);
int retries = 0;
while (q_regulated < q_low && retries < 10) {
av1_rc_update_rate_correction_factors(cpi, is_encode_stage, cm->width,
cm->height);
q_regulated =
av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
AOMMAX(q_high, top_index), cm->width, cm->height);
retries++;
}
return q_regulated;
}
static inline int get_regulated_q_undershoot(AV1_COMP *const cpi,
int is_encode_stage, int q_high,
int top_index, int bottom_index) {
const AV1_COMMON *const cm = &cpi->common;
const RATE_CONTROL *const rc = &cpi->rc;
av1_rc_update_rate_correction_factors(cpi, is_encode_stage, cm->width,
cm->height);
int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
top_index, cm->width, cm->height);
int retries = 0;
while (q_regulated > q_high && retries < 10) {
av1_rc_update_rate_correction_factors(cpi, is_encode_stage, cm->width,
cm->height);
q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
top_index, cm->width, cm->height);
retries++;
}
return q_regulated;
}
static inline 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) {
AV1_COMMON *const cm = &cpi->common;
RATE_CONTROL *const rc = &cpi->rc;
PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg;
*loop = 0;
if (rc->is_src_frame_alt_ref &&
rc->projected_frame_size < rc->max_frame_bandwidth)
return;
const int min_cr = rc_cfg->min_cr;
if (min_cr > 0) {
const double compression_ratio =
av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
const double target_cr = min_cr / 100.0;
if (compression_ratio < target_cr) {
*low_cr_seen = 1;
if (*q < rc->worst_quality) {
const double cr_ratio = target_cr / compression_ratio;
const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
*q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
*q_low = AOMMAX(*q, *q_low);
*q_high = AOMMAX(*q, *q_high);
*loop = 1;
}
}
if (*low_cr_seen) return;
}
if (cpi->ppi->level_params.keep_level_stats &&
!is_stat_generation_stage(cpi)) {
if (cm->current_frame.frame_type == KEY_FRAME &&
cpi->ppi->gf_group.refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) {
av1_init_level_info(cpi);
}
const AV1LevelParams *const level_params = &cpi->ppi->level_params;
const AV1LevelInfo *const level_info = level_params->level_info[0];
const DECODER_MODEL *const decoder_models = level_info->decoder_models;
const AV1_LEVEL target_level = level_params->target_seq_level_idx[0];
if (target_level < SEQ_LEVELS &&
decoder_models[target_level].status == DECODER_MODEL_OK) {
DECODER_MODEL_STATUS status = av1_decoder_model_try_smooth_buf(
cpi, rc->projected_frame_size, &decoder_models[target_level]);
if ((status == SMOOTHING_BUFFER_UNDERFLOW ||
status == SMOOTHING_BUFFER_OVERFLOW) &&
*q < rc->worst_quality) {
*q = AOMMIN(*q + 10, rc->worst_quality);
*q_low = AOMMAX(*q, *q_low);
*q_high = AOMMAX(*q, *q_high);
*loop = 1;
return;
}
}
}
if (rc_cfg->mode == AOM_Q) return;
const int last_q = *q;
int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
&frame_under_shoot_limit,
&frame_over_shoot_limit);
if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
if (cm->current_frame.frame_type == KEY_FRAME &&
p_rc->this_key_frame_forced &&
rc->projected_frame_size < rc->max_frame_bandwidth) {
int64_t kf_err;
const int64_t high_err_target = cpi->ambient_err;
const int64_t low_err_target = cpi->ambient_err >> 1;
#if CONFIG_AV1_HIGHBITDEPTH
if (cm->seq_params->use_highbitdepth) {
kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
} else {
kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
}
#else
kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
#endif
kf_err += !kf_err;
if ((kf_err > high_err_target &&
rc->projected_frame_size <= frame_over_shoot_limit) ||
(kf_err > low_err_target &&
rc->projected_frame_size <= frame_under_shoot_limit)) {
*q_high = AOMMAX(*q - 1, *q_low);
*q = (int)((*q * high_err_target) / kf_err);
*q = AOMMIN(*q, (*q_high + *q_low) >> 1);
} else if (kf_err < low_err_target &&
rc->projected_frame_size >= frame_under_shoot_limit) {
*q_low = AOMMIN(*q + 1, *q_high);
*q = (int)((*q * low_err_target) / kf_err);
*q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
}
*q = clamp(*q, *q_low, *q_high);
*loop = (*q != last_q);
return;
}
if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
AOMMAX(*q_high, top_index), bottom_index)) {
if (rc->projected_frame_size > rc->this_frame_target) {
if (*q == *q_high &&
rc->projected_frame_size >= rc->max_frame_bandwidth) {
const double q_val_high_current =
av1_convert_qindex_to_q(*q_high, cm->seq_params->bit_depth);
const double q_val_high_new =
q_val_high_current *
((double)rc->projected_frame_size / rc->max_frame_bandwidth);
*q_high = av1_find_qindex(q_val_high_new, cm->seq_params->bit_depth,
rc->best_quality, rc->worst_quality);
}
*q_low = AOMMIN(*q + 1, *q_high);
if (*undershoot_seen || loop_count > 2 ||
(loop_count == 2 && !frame_is_intra_only(cm))) {
av1_rc_update_rate_correction_factors(cpi, 1, cm->width, cm->height);
*q = (*q_high + *q_low + 1) / 2;
} else if (loop_count == 2 && frame_is_intra_only(cm)) {
const int q_mid = (*q_high + *q_low + 1) / 2;
const int q_regulated = get_regulated_q_overshoot(
cpi, 1, *q_low, *q_high, top_index, bottom_index);
*q = (q_mid + q_regulated + 1) / 2;
} else {
*q = get_regulated_q_overshoot(cpi, 1, *q_low, *q_high, top_index,
bottom_index);
}
*overshoot_seen = 1;
} else {
*q_high = AOMMAX(*q - 1, *q_low);
if (*overshoot_seen || loop_count > 2 ||
(loop_count == 2 && !frame_is_intra_only(cm))) {
av1_rc_update_rate_correction_factors(cpi, 1, cm->width, cm->height);
*q = (*q_high + *q_low) / 2;
} else if (loop_count == 2 && frame_is_intra_only(cm)) {
const int q_mid = (*q_high + *q_low) / 2;
const int q_regulated = get_regulated_q_undershoot(
cpi, 1, *q_high, top_index, bottom_index);
*q = (q_mid + q_regulated) / 2;
if (rc_cfg->mode == AOM_CQ && q_regulated < *q_low) {
*q_low = *q;
}
} else {
*q = get_regulated_q_undershoot(cpi, 1, *q_high, top_index,
bottom_index);
if (rc_cfg->mode == AOM_CQ && *q < *q_low) {
*q_low = *q;
}
}
*undershoot_seen = 1;
}
*q = clamp(*q, *q_low, *q_high);
}
*loop = (*q != last_q);
}
#endif
#ifdef __cplusplus
}
#endif
#endif