Commit 922614d6fe for aom
commit 922614d6fe92530fc11c188077c4c456405e09d2
Author: Marvin Scholz <marvinscholz@xwf.google.com>
Date: Tue Nov 24 20:10:03 2026 +0000
Fix integer overflows in av1_twopass_postencode_update()
A high target bitrate combined with a low frame rate makes the average
per-frame bit budget exceed INT_MAX / 2. The rate control drift
adjustment in av1_twopass_postencode_update() then overflows int when
it doubles rc->avg_frame_bandwidth and rc->base_frame_target, and when
it multiplies the difference between the rolling target and actual bits
by 100.
For example, a target bitrate of 1185941 kbps with a timebase of
65529831/50529027 (about 0.77 fps) gives an avg_frame_bandwidth of
1538017116. Both values pass the encoder config range checks, which
validate the bitrate and the timebase separately. UBSan reports:
pass2_strategy.c: runtime error: signed integer overflow:
2 * 1538017116 cannot be represented in type 'int'
Do these calculations in int64_t, as is already done for
other variables later in the same function.
Change-Id: I645ff5b27b579440f81c379e5e7a2dd53d7e358a
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index b3a7ccf432..e516d6d7dd 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -4594,8 +4594,9 @@ void av1_twopass_postencode_update(AV1_COMP *cpi) {
// Undershoot
if ((rc_cfg->under_shoot_pct < 100) &&
(p_rc->rolling_actual_bits < p_rc->rolling_target_bits)) {
- int pct_error =
- ((p_rc->rolling_target_bits - p_rc->rolling_actual_bits) * 100) /
+ const int64_t pct_error =
+ (((int64_t)p_rc->rolling_target_bits - p_rc->rolling_actual_bits) *
+ 100) /
p_rc->rolling_target_bits;
if ((pct_error >= rc_cfg->under_shoot_pct) &&
@@ -4606,8 +4607,9 @@ void av1_twopass_postencode_update(AV1_COMP *cpi) {
// Overshoot
} else if ((rc_cfg->over_shoot_pct <= 100) &&
(p_rc->rolling_actual_bits > p_rc->rolling_target_bits)) {
- int pct_error =
- ((p_rc->rolling_actual_bits - p_rc->rolling_target_bits) * 100) /
+ const int64_t pct_error =
+ (((int64_t)p_rc->rolling_actual_bits - p_rc->rolling_target_bits) *
+ 100) /
p_rc->rolling_target_bits;
if ((pct_error >= rc_cfg->over_shoot_pct) &&
@@ -4619,8 +4621,8 @@ void av1_twopass_postencode_update(AV1_COMP *cpi) {
// Adjustment for extreme local overshoot.
// Only applies when normal adjustment above is not used (e.g.
// when threshold is set to 100).
- if (rc->projected_frame_size > (2 * rc->base_frame_target) &&
- rc->projected_frame_size > (2 * rc->avg_frame_bandwidth))
+ if (rc->projected_frame_size > (2 * (int64_t)rc->base_frame_target) &&
+ rc->projected_frame_size > (2 * (int64_t)rc->avg_frame_bandwidth))
++twopass->extend_maxq;
// Unwind extreme overshoot adjustment.
else if (p_rc->rolling_target_bits > p_rc->rolling_actual_bits)