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;
155static inline double av1_get_gfu_boost_projection_factor(
double min_factor,
158 double factor = sqrt((
double)frame_count);
159 factor = AOMMIN(factor, max_factor);
160 factor = AOMMAX(factor, min_factor);
161 factor = (200.0 + 10.0 * factor);
165static inline int get_gfu_boost_from_r0_lap(
double min_factor,
166 double max_factor,
double r0,
168 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
170 const int boost = (int)rint(factor / r0);
174static inline double av1_get_kf_boost_projection_factor(
int frame_count) {
175 double factor = sqrt((
double)frame_count);
176 factor = AOMMIN(factor, 10.0);
177 factor = AOMMAX(factor, 4.0);
178 factor = (75.0 + 14.0 * factor);
182static inline int get_regulated_q_overshoot(
AV1_COMP *
const cpi,
183 int is_encode_stage,
int q_low,
184 int q_high,
int top_index,
197 while (q_regulated < q_low && retries < 10) {
208static inline int get_regulated_q_undershoot(
AV1_COMP *
const cpi,
209 int is_encode_stage,
int q_high,
210 int top_index,
int bottom_index) {
220 while (q_regulated > q_high && retries < 10) {
253 AV1_COMP *
const cpi,
int *
const loop,
int *
const q,
int *
const q_low,
254 int *
const q_high,
const int top_index,
const int bottom_index,
255 int *
const undershoot_seen,
int *
const overshoot_seen,
256 int *
const low_cr_seen,
const int loop_count) {
264 if (rc->is_src_frame_alt_ref &&
268 const int min_cr = rc_cfg->
min_cr;
270 const double compression_ratio =
272 const double target_cr = min_cr / 100.0;
273 if (compression_ratio < target_cr) {
275 if (*q < rc->worst_quality) {
276 const double cr_ratio = target_cr / compression_ratio;
277 const int projected_q = AOMMAX(*q + 1, (
int)(*q * cr_ratio * cr_ratio));
278 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->
worst_quality);
279 *q_low = AOMMAX(*q, *q_low);
280 *q_high = AOMMAX(*q, *q_high);
284 if (*low_cr_seen)
return;
288 !is_stat_generation_stage(cpi)) {
292 av1_init_level_info(cpi);
296 const AV1LevelInfo *
const level_info = level_params->level_info[0];
297 const DECODER_MODEL *
const decoder_models = level_info->decoder_models;
298 const AV1_LEVEL target_level = level_params->target_seq_level_idx[0];
300 if (target_level < SEQ_LEVELS &&
301 decoder_models[target_level].status == DECODER_MODEL_OK) {
302 DECODER_MODEL_STATUS status = av1_decoder_model_try_smooth_buf(
305 if ((status == SMOOTHING_BUFFER_UNDERFLOW ||
306 status == SMOOTHING_BUFFER_OVERFLOW) &&
307 *q < rc->worst_quality) {
309 *q_low = AOMMAX(*q, *q_low);
310 *q_high = AOMMAX(*q, *q_high);
319 const int last_q = *q;
320 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
322 &frame_under_shoot_limit,
323 &frame_over_shoot_limit);
324 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
327 p_rc->this_key_frame_forced &&
331 const int64_t low_err_target = cpi->
ambient_err >> 1;
333#if CONFIG_AV1_HIGHBITDEPTH
347 if ((kf_err > high_err_target &&
349 (kf_err > low_err_target &&
352 *q_high = AOMMAX(*q - 1, *q_low);
355 *q = (int)((*q * high_err_target) / kf_err);
356 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
357 }
else if (kf_err < low_err_target &&
361 *q_low = AOMMIN(*q + 1, *q_high);
364 *q = (int)((*q * low_err_target) / kf_err);
365 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
369 *q = clamp(*q, *q_low, *q_high);
370 *loop = (*q != last_q);
374 if (
recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
375 AOMMAX(*q_high, top_index), bottom_index)) {
386 const double q_val_high_current =
387 av1_convert_qindex_to_q(*q_high, cm->
seq_params->bit_depth);
388 const double q_val_high_new =
391 *q_high = av1_find_qindex(q_val_high_new, cm->
seq_params->bit_depth,
396 *q_low = AOMMIN(*q + 1, *q_high);
398 if (*undershoot_seen || loop_count > 2 ||
399 (loop_count == 2 && !frame_is_intra_only(cm))) {
402 *q = (*q_high + *q_low + 1) / 2;
403 }
else if (loop_count == 2 && frame_is_intra_only(cm)) {
404 const int q_mid = (*q_high + *q_low + 1) / 2;
405 const int q_regulated = get_regulated_q_overshoot(
406 cpi, 1, *q_low, *q_high, top_index, bottom_index);
409 *q = (q_mid + q_regulated + 1) / 2;
411 *q = get_regulated_q_overshoot(cpi, 1, *q_low, *q_high, top_index,
418 *q_high = AOMMAX(*q - 1, *q_low);
420 if (*overshoot_seen || loop_count > 2 ||
421 (loop_count == 2 && !frame_is_intra_only(cm))) {
423 *q = (*q_high + *q_low) / 2;
424 }
else if (loop_count == 2 && frame_is_intra_only(cm)) {
425 const int q_mid = (*q_high + *q_low) / 2;
426 const int q_regulated = get_regulated_q_undershoot(
427 cpi, 1, *q_high, top_index, bottom_index);
430 *q = (q_mid + q_regulated) / 2;
436 if (rc_cfg->
mode ==
AOM_CQ && q_regulated < *q_low) {
440 *q = get_regulated_q_undershoot(cpi, 1, *q_high, top_index,
452 *undershoot_seen = 1;
456 *q = clamp(*q, *q_low, *q_high);
459 *loop = (*q != last_q);
Declares top-level encoder structures and functions.
@ AOM_CQ
Definition aom_encoder.h:186
@ AOM_Q
Definition aom_encoder.h:187
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:858
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:252
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:923
RateControlCfg rc_cfg
Definition encoder.h:945
Top level encoder structure.
Definition encoder.h:2873
RATE_CONTROL rc
Definition encoder.h:3080
int64_t ambient_err
Definition encoder.h:3049
SPEED_FEATURES sf
Definition encoder.h:3100
unsigned char gf_frame_index
Definition encoder.h:3131
AV1EncoderConfig oxcf
Definition encoder.h:2921
AV1_COMMON common
Definition encoder.h:2916
AV1_PRIMARY * ppi
Definition encoder.h:2877
YV12_BUFFER_CONFIG * source
Definition encoder.h:2934
SVC svc
Definition encoder.h:3414
Top level primary encoder structure.
Definition encoder.h:2577
AV1LevelParams level_params
Definition encoder.h:2708
TWO_PASS twopass
Definition encoder.h:2723
PRIMARY_RATE_CONTROL p_rc
Definition encoder.h:2728
int use_svc
Definition encoder.h:2743
GF_GROUP gf_group
Definition encoder.h:2693
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:299
int64_t bits_off_target
Definition ratectrl.h:554
int64_t maximum_buffer_size
Definition ratectrl.h:356
int64_t starting_buffer_level
Definition ratectrl.h:346
int64_t buffer_level
Definition ratectrl.h:533
int64_t optimal_buffer_level
Definition ratectrl.h:351
Rate Control parameters and status.
Definition ratectrl.h:134
int best_quality
Definition ratectrl.h:233
int this_frame_target
Definition ratectrl.h:145
int projected_frame_size
Definition ratectrl.h:150
int worst_quality
Definition ratectrl.h:229
Encoder rate control configuration parameters.
Definition encoder.h:521
int worst_allowed_q
Definition encoder.h:591
int over_shoot_pct
Definition encoder.h:586
int64_t maximum_buffer_size_ms
Definition encoder.h:540
unsigned int min_cr
Definition encoder.h:570
enum aom_rc_mode mode
Definition encoder.h:605
int64_t starting_buffer_level_ms
Definition encoder.h:530
int64_t target_bandwidth
Definition encoder.h:545
int64_t optimal_buffer_level_ms
Definition encoder.h:535
int cq_level
Definition encoder.h:600
HIGH_LEVEL_SPEED_FEATURES hl_sf
Definition speed_features.h:1936
Two pass status and control data.
Definition firstpass.h:416