Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

h264: dequantisation wraps instead of overflowing on huge levels

Brandon Thomas0f678aa1055b
diff --git a/crates/h264/src/transform.rs b/crates/h264/src/transform.rs
index ec1f91650..4380fe2aa 100644
--- a/crates/h264/src/transform.rs
+++ b/crates/h264/src/transform.rs
@@ -32,13 +32,13 @@ impl LevelScale {
/// Scale one 4x4 AC coefficient (8.5.12.1, not the DC of Intra16x16 / chroma blocks).
#[inline(always)]
pub fn scale4(c: i32, ls: i32, qp: i32) -> i32 {
- if qp >= 24 { (c * ls) << (qp / 6 - 4) } else { (c * ls + (1 << (3 - qp / 6))) >> (4 - qp / 6) }
+ if qp >= 24 { c.wrapping_mul(ls) << (qp / 6 - 4) } else { c.wrapping_mul(ls).wrapping_add(1 << (3 - qp / 6)) >> (4 - qp / 6) }
}
/// Scale one 8x8 coefficient (8.5.13.1).
#[inline(always)]
pub fn scale8(c: i32, ls: i32, qp: i32) -> i32 {
- if qp >= 36 { (c * ls) << (qp / 6 - 6) } else { (c * ls + (1 << (5 - qp / 6))) >> (6 - qp / 6) }
+ if qp >= 36 { c.wrapping_mul(ls) << (qp / 6 - 6) } else { c.wrapping_mul(ls).wrapping_add(1 << (5 - qp / 6)) >> (6 - qp / 6) }
}
/// Intra16x16 luma DC: inverse Hadamard + scaling (8.5.10). `c` is raster order (row*4+col);
@@ -65,20 +65,21 @@ pub fn luma_dc_dequant(c: &mut [i32; 16], qp: i32, ls00: i32) {
let d23 = t[8 + j] - t[12 + j];
let f = [s01 + s23, s01 - s23, d01 - d23, d01 + d23];
for i in 0..4 {
- c[i * 4 + j] = if qp >= 36 { (f[i] * ls00) << (qp / 6 - 6) } else { (f[i] * ls00 + (1 << (5 - qp / 6))) >> (6 - qp / 6) };
+ c[i * 4 + j] =
+ if qp >= 36 { f[i].wrapping_mul(ls00) << (qp / 6 - 6) } else { f[i].wrapping_mul(ls00).wrapping_add(1 << (5 - qp / 6)) >> (6 - qp / 6) };
}
}
}
/// 4:2:0 chroma DC: 2x2 transform + scaling (8.5.11). `c` = [c00, c01, c10, c11].
pub fn chroma_dc_dequant_420(c: &mut [i32; 4], qp: i32, ls00: i32) {
- let s0 = c[0] + c[1];
- let d0 = c[0] - c[1];
- let s1 = c[2] + c[3];
- let d1 = c[2] - c[3];
- let f = [s0 + s1, d0 + d1, s0 - s1, d0 - d1];
+ let s0 = c[0].wrapping_add(c[1]);
+ let d0 = c[0].wrapping_sub(c[1]);
+ let s1 = c[2].wrapping_add(c[3]);
+ let d1 = c[2].wrapping_sub(c[3]);
+ let f = [s0.wrapping_add(s1), d0.wrapping_add(d1), s0.wrapping_sub(s1), d0.wrapping_sub(d1)];
for k in 0..4 {
- c[k] = ((f[k] * ls00) << (qp / 6)) >> 5;
+ c[k] = (f[k].wrapping_mul(ls00) << (qp / 6)) >> 5;
}
}
@@ -287,4 +288,17 @@ mod tests {
assert_eq!(scale8(1, 320, 36), 320);
assert_eq!(scale8(1, 320, 30), (320 + 1) >> 1);
}
+
+ /// Corrupt streams can carry huge coefficient levels: dequantisation wraps (as release
+ /// builds always did) instead of panicking with "attempt to multiply with overflow" in
+ /// debug builds (found by mutation fuzzing an H.264 transport stream).
+ #[test]
+ fn huge_levels_dequantise_without_overflow_panics() {
+ let mut c = [i32::MAX / 2, i32::MAX / 3, -i32::MAX / 2, 7];
+ chroma_dc_dequant_420(&mut c, 51, 16 * 25);
+ let _ = scale4(i32::MAX, 16 * 25, 51);
+ let _ = scale4(i32::MAX, 16 * 25, 0);
+ let _ = scale8(i32::MIN, 16 * 25, 51);
+ let _ = scale8(i32::MIN, 16 * 25, 0);
+ }
}