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_