1 /*
2 * Copyright (c) 2020, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #ifndef AOM_AV1_ENCODER_RC_UTILS_H_
13 #define AOM_AV1_ENCODER_RC_UTILS_H_
14
15 #include "av1/encoder/encoder.h"
16 #include "aom_dsp/psnr.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
check_reset_rc_flag(AV1_COMP * cpi)22 static AOM_INLINE void check_reset_rc_flag(AV1_COMP *cpi) {
23 RATE_CONTROL *rc = &cpi->rc;
24 PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
25 if (cpi->common.current_frame.frame_number >
26 (unsigned int)cpi->svc.number_spatial_layers) {
27 if (cpi->ppi->use_svc) {
28 av1_svc_check_reset_layer_rc_flag(cpi);
29 } else {
30 if (rc->avg_frame_bandwidth > (3 * rc->prev_avg_frame_bandwidth >> 1) ||
31 rc->avg_frame_bandwidth < (rc->prev_avg_frame_bandwidth >> 1)) {
32 rc->rc_1_frame = 0;
33 rc->rc_2_frame = 0;
34 p_rc->bits_off_target = p_rc->optimal_buffer_level;
35 p_rc->buffer_level = p_rc->optimal_buffer_level;
36 }
37 }
38 }
39 }
40
set_primary_rc_buffer_sizes(const AV1EncoderConfig * oxcf,AV1_PRIMARY * ppi)41 static AOM_INLINE void set_primary_rc_buffer_sizes(const AV1EncoderConfig *oxcf,
42 AV1_PRIMARY *ppi) {
43 PRIMARY_RATE_CONTROL *p_rc = &ppi->p_rc;
44 const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
45
46 const int64_t bandwidth = rc_cfg->target_bandwidth;
47 const int64_t starting = rc_cfg->starting_buffer_level_ms;
48 const int64_t optimal = rc_cfg->optimal_buffer_level_ms;
49 const int64_t maximum = rc_cfg->maximum_buffer_size_ms;
50
51 p_rc->starting_buffer_level = starting * bandwidth / 1000;
52 p_rc->optimal_buffer_level =
53 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
54 p_rc->maximum_buffer_size =
55 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
56
57 // Under a configuration change, where maximum_buffer_size may change,
58 // keep buffer level clipped to the maximum allowed buffer size.
59 p_rc->bits_off_target =
60 AOMMIN(p_rc->bits_off_target, p_rc->maximum_buffer_size);
61 p_rc->buffer_level = AOMMIN(p_rc->buffer_level, p_rc->maximum_buffer_size);
62 }
63
config_target_level(AV1_COMP * const cpi,AV1_LEVEL target_level,int tier)64 static AOM_INLINE void config_target_level(AV1_COMP *const cpi,
65 AV1_LEVEL target_level, int tier) {
66 AV1EncoderConfig *const oxcf = &cpi->oxcf;
67 SequenceHeader *const seq_params = cpi->common.seq_params;
68 TileConfig *const tile_cfg = &oxcf->tile_cfg;
69 RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
70
71 // Adjust target bitrate to be no larger than 70% of level limit.
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);
76 rc_cfg->target_bandwidth = AOMMIN(rc_cfg->target_bandwidth, max_bitrate);
77 // Also need to update cpi->ppi->twopass.bits_left.
78 TWO_PASS *const twopass = &cpi->ppi->twopass;
79 FIRSTPASS_STATS *stats = twopass->stats_buf_ctx->total_stats;
80 if (stats != NULL)
81 cpi->ppi->twopass.bits_left =
82 (int64_t)(stats->duration * rc_cfg->target_bandwidth / 10000000.0);
83
84 // Adjust max over-shoot percentage.
85 rc_cfg->over_shoot_pct = 0;
86
87 // Adjust max quantizer.
88 rc_cfg->worst_allowed_q = 255;
89
90 // Adjust number of tiles and tile columns to be under level limit.
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;
96 }
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;
101 }
102
103 // Adjust min compression ratio.
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));
108 }
109
110 #if !CONFIG_REALTIME_ONLY
111
112 /*!\brief Function to test for conditions that indicate we should loop
113 * back and recode a frame.
114 *
115 * \ingroup rate_control
116 *
117 * \param[in] cpi Top-level encoder structure
118 * \param[in] high_limit Upper rate threshold
119 * \param[in] low_limit Lower rate threshold
120 * \param[in] q Current q index
121 * \param[in] maxq Maximum allowed q index
122 * \param[in] minq Minimum allowed q index
123 *
124 * \return Indicates if a recode is required.
125 * \retval 1 Recode Required
126 * \retval 0 No Recode required
127 */
recode_loop_test(AV1_COMP * cpi,int high_limit,int low_limit,int q,int maxq,int minq)128 static AOM_INLINE int recode_loop_test(AV1_COMP *cpi, int high_limit,
129 int low_limit, int q, int maxq,
130 int minq) {
131 const RATE_CONTROL *const rc = &cpi->rc;
132 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
133 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
134 int force_recode = 0;
135
136 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
137 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE) ||
138 (frame_is_kfgfarf &&
139 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
140 // TODO(agrange) high_limit could be greater than the scale-down threshold.
141 if ((rc->projected_frame_size > high_limit && q < maxq) ||
142 (rc->projected_frame_size < low_limit && q > minq)) {
143 force_recode = 1;
144 } else if (cpi->oxcf.rc_cfg.mode == AOM_CQ) {
145 // Deal with frame undershoot and whether or not we are
146 // below the automatically set cq level.
147 if (q > oxcf->rc_cfg.cq_level &&
148 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
149 force_recode = 1;
150 }
151 }
152 }
153 return force_recode;
154 }
155
av1_get_gfu_boost_projection_factor(double min_factor,double max_factor,int frame_count)156 static AOM_INLINE double av1_get_gfu_boost_projection_factor(double min_factor,
157 double max_factor,
158 int frame_count) {
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);
163 return factor;
164 }
165
get_gfu_boost_from_r0_lap(double min_factor,double max_factor,double r0,int frames_to_key)166 static AOM_INLINE int get_gfu_boost_from_r0_lap(double min_factor,
167 double max_factor, double r0,
168 int frames_to_key) {
169 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
170 frames_to_key);
171 const int boost = (int)rint(factor / r0);
172 return boost;
173 }
174
av1_get_kf_boost_projection_factor(int frame_count)175 static AOM_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);
180 return factor;
181 }
182
get_regulated_q_overshoot(AV1_COMP * const cpi,int is_encode_stage,int q_low,int q_high,int top_index,int bottom_index)183 static AOM_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,
186 int bottom_index) {
187 const AV1_COMMON *const cm = &cpi->common;
188 const RATE_CONTROL *const rc = &cpi->rc;
189
190 av1_rc_update_rate_correction_factors(cpi, is_encode_stage, cm->width,
191 cm->height);
192
193 int q_regulated =
194 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
195 AOMMAX(q_high, top_index), cm->width, cm->height);
196
197 int retries = 0;
198 while (q_regulated < q_low && retries < 10) {
199 av1_rc_update_rate_correction_factors(cpi, is_encode_stage, cm->width,
200 cm->height);
201 q_regulated =
202 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
203 AOMMAX(q_high, top_index), cm->width, cm->height);
204 retries++;
205 }
206 return q_regulated;
207 }
208
get_regulated_q_undershoot(AV1_COMP * const cpi,int is_encode_stage,int q_high,int top_index,int bottom_index)209 static AOM_INLINE int get_regulated_q_undershoot(AV1_COMP *const cpi,
210 int is_encode_stage,
211 int q_high, int top_index,
212 int bottom_index) {
213 const AV1_COMMON *const cm = &cpi->common;
214 const RATE_CONTROL *const rc = &cpi->rc;
215
216 av1_rc_update_rate_correction_factors(cpi, is_encode_stage, cm->width,
217 cm->height);
218 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
219 top_index, cm->width, cm->height);
220
221 int retries = 0;
222 while (q_regulated > q_high && retries < 10) {
223 av1_rc_update_rate_correction_factors(cpi, is_encode_stage, cm->width,
224 cm->height);
225 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
226 top_index, cm->width, cm->height);
227 retries++;
228 }
229 return q_regulated;
230 }
231
232 /*!\brief Called after encode_with_recode_loop() has just encoded a frame.
233 * This function works out whether we undershot or overshot our bitrate
234 * target and adjusts q as appropriate. It also decides whether or not
235 * we need to recode the frame to get closer to the target rate.
236 *
237 * \ingroup rate_control
238 *
239 * \param[in] cpi Top-level encoder structure
240 * \param[out] loop Should we go around the recode loop again
241 * \param[in,out] q New q index value
242 * \param[in,out] q_low Low q index limit for this loop itteration
243 * \param[in,out] q_high High q index limit for this loop itteration
244 * \param[in] top_index Max permited new value for q index
245 * \param[in] bottom_index Min permited new value for q index
246 * \param[in,out] undershoot_seen Have we seen undershoot on this frame
247 * \param[in,out] overshoot_seen Have we seen overshoot on this frame
248 * \param[in,out] low_cr_seen Have we previously trriggered recode
249 * because the compression ration was less
250 * than a given minimum threshold.
251 * \param[in] loop_count Loop itterations so far.
252 *
253 */
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)254 static AOM_INLINE void recode_loop_update_q(
255 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
256 int *const q_high, const int top_index, const int bottom_index,
257 int *const undershoot_seen, int *const overshoot_seen,
258 int *const low_cr_seen, const int loop_count) {
259 AV1_COMMON *const cm = &cpi->common;
260 RATE_CONTROL *const rc = &cpi->rc;
261 PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
262 const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg;
263 *loop = 0;
264
265 // Special case for overlay frame.
266 if (rc->is_src_frame_alt_ref &&
267 rc->projected_frame_size < rc->max_frame_bandwidth)
268 return;
269
270 const int min_cr = rc_cfg->min_cr;
271 if (min_cr > 0) {
272 const double compression_ratio =
273 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
274 const double target_cr = min_cr / 100.0;
275 if (compression_ratio < target_cr) {
276 *low_cr_seen = 1;
277 if (*q < rc->worst_quality) {
278 const double cr_ratio = target_cr / compression_ratio;
279 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
280 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
281 *q_low = AOMMAX(*q, *q_low);
282 *q_high = AOMMAX(*q, *q_high);
283 *loop = 1;
284 }
285 }
286 if (*low_cr_seen) return;
287 }
288
289 if (cpi->ppi->level_params.keep_level_stats &&
290 !is_stat_generation_stage(cpi)) {
291 // Initialize level info. at the beginning of each sequence.
292 if (cm->current_frame.frame_type == KEY_FRAME &&
293 cpi->ppi->gf_group.refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) {
294 av1_init_level_info(cpi);
295 }
296 const AV1LevelParams *const level_params = &cpi->ppi->level_params;
297 // TODO(any): currently only checking operating point 0
298 const AV1LevelInfo *const level_info = level_params->level_info[0];
299 const DECODER_MODEL *const decoder_models = level_info->decoder_models;
300 const AV1_LEVEL target_level = level_params->target_seq_level_idx[0];
301
302 if (target_level < SEQ_LEVELS &&
303 decoder_models[target_level].status == DECODER_MODEL_OK) {
304 DECODER_MODEL_STATUS status = av1_decoder_model_try_smooth_buf(
305 cpi, rc->projected_frame_size, &decoder_models[target_level]);
306
307 if ((status == SMOOTHING_BUFFER_UNDERFLOW ||
308 status == SMOOTHING_BUFFER_OVERFLOW) &&
309 *q < rc->worst_quality) {
310 *q = AOMMIN(*q + 10, rc->worst_quality);
311 *q_low = AOMMAX(*q, *q_low);
312 *q_high = AOMMAX(*q, *q_high);
313 *loop = 1;
314 return;
315 }
316 }
317 }
318
319 if (rc_cfg->mode == AOM_Q) return;
320
321 const int last_q = *q;
322 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
323 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
324 &frame_under_shoot_limit,
325 &frame_over_shoot_limit);
326 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
327
328 if (cm->current_frame.frame_type == KEY_FRAME &&
329 p_rc->this_key_frame_forced &&
330 rc->projected_frame_size < rc->max_frame_bandwidth) {
331 int64_t kf_err;
332 const int64_t high_err_target = cpi->ambient_err;
333 const int64_t low_err_target = cpi->ambient_err >> 1;
334
335 #if CONFIG_AV1_HIGHBITDEPTH
336 if (cm->seq_params->use_highbitdepth) {
337 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
338 } else {
339 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
340 }
341 #else
342 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
343 #endif
344 // Prevent possible divide by zero error below for perfect KF
345 kf_err += !kf_err;
346
347 // The key frame is not good enough or we can afford
348 // to make it better without undue risk of popping.
349 if ((kf_err > high_err_target &&
350 rc->projected_frame_size <= frame_over_shoot_limit) ||
351 (kf_err > low_err_target &&
352 rc->projected_frame_size <= frame_under_shoot_limit)) {
353 // Lower q_high
354 *q_high = AOMMAX(*q - 1, *q_low);
355
356 // Adjust Q
357 *q = (int)((*q * high_err_target) / kf_err);
358 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
359 } else if (kf_err < low_err_target &&
360 rc->projected_frame_size >= frame_under_shoot_limit) {
361 // The key frame is much better than the previous frame
362 // Raise q_low
363 *q_low = AOMMIN(*q + 1, *q_high);
364
365 // Adjust Q
366 *q = (int)((*q * low_err_target) / kf_err);
367 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
368 }
369
370 // Clamp Q to upper and lower limits:
371 *q = clamp(*q, *q_low, *q_high);
372 *loop = (*q != last_q);
373 return;
374 }
375
376 if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
377 AOMMAX(*q_high, top_index), bottom_index)) {
378 // Is the projected frame size out of range and are we allowed
379 // to attempt to recode.
380
381 // Frame size out of permitted range:
382 // Update correction factor & compute new Q to try...
383 // Frame is too large
384 if (rc->projected_frame_size > rc->this_frame_target) {
385 // Special case if the projected size is > the max allowed.
386 if (*q == *q_high &&
387 rc->projected_frame_size >= rc->max_frame_bandwidth) {
388 const double q_val_high_current =
389 av1_convert_qindex_to_q(*q_high, cm->seq_params->bit_depth);
390 const double q_val_high_new =
391 q_val_high_current *
392 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
393 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params->bit_depth,
394 rc->best_quality, rc->worst_quality);
395 }
396
397 // Raise Qlow as to at least the current value
398 *q_low = AOMMIN(*q + 1, *q_high);
399
400 if (*undershoot_seen || loop_count > 2 ||
401 (loop_count == 2 && !frame_is_intra_only(cm))) {
402 av1_rc_update_rate_correction_factors(cpi, 1, cm->width, cm->height);
403
404 *q = (*q_high + *q_low + 1) / 2;
405 } else if (loop_count == 2 && frame_is_intra_only(cm)) {
406 const int q_mid = (*q_high + *q_low + 1) / 2;
407 const int q_regulated = get_regulated_q_overshoot(
408 cpi, 1, *q_low, *q_high, top_index, bottom_index);
409 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
410 // transition between loop_count < 2 and loop_count > 2.
411 *q = (q_mid + q_regulated + 1) / 2;
412 } else {
413 *q = get_regulated_q_overshoot(cpi, 1, *q_low, *q_high, top_index,
414 bottom_index);
415 }
416
417 *overshoot_seen = 1;
418 } else {
419 // Frame is too small
420 *q_high = AOMMAX(*q - 1, *q_low);
421
422 if (*overshoot_seen || loop_count > 2 ||
423 (loop_count == 2 && !frame_is_intra_only(cm))) {
424 av1_rc_update_rate_correction_factors(cpi, 1, cm->width, cm->height);
425 *q = (*q_high + *q_low) / 2;
426 } else if (loop_count == 2 && frame_is_intra_only(cm)) {
427 const int q_mid = (*q_high + *q_low) / 2;
428 const int q_regulated = get_regulated_q_undershoot(
429 cpi, 1, *q_high, top_index, bottom_index);
430 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
431 // transition between loop_count < 2 and loop_count > 2.
432 *q = (q_mid + q_regulated) / 2;
433
434 // Special case reset for qlow for constrained quality.
435 // This should only trigger where there is very substantial
436 // undershoot on a frame and the auto cq level is above
437 // the user passsed in value.
438 if (rc_cfg->mode == AOM_CQ && q_regulated < *q_low) {
439 *q_low = *q;
440 }
441 } else {
442 *q = get_regulated_q_undershoot(cpi, 1, *q_high, top_index,
443 bottom_index);
444
445 // Special case reset for qlow for constrained quality.
446 // This should only trigger where there is very substantial
447 // undershoot on a frame and the auto cq level is above
448 // the user passsed in value.
449 if (rc_cfg->mode == AOM_CQ && *q < *q_low) {
450 *q_low = *q;
451 }
452 }
453
454 *undershoot_seen = 1;
455 }
456
457 // Clamp Q to upper and lower limits:
458 *q = clamp(*q, *q_low, *q_high);
459 }
460
461 *loop = (*q != last_q);
462 }
463 #endif
464
465 #ifdef __cplusplus
466 } // extern "C"
467 #endif
468
469 #endif // AOM_AV1_ENCODER_RC_UTILS_H_
470