Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(enc-ffmpeg): cap the H.264 bitrate so a hostile bpp cannot overflow

Richie McIlroy8fb54becfd0a
diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rs
index eaeac3a98..2cdc2f610 100644
--- a/crates/enc-ffmpeg/src/video/h264.rs
+++ b/crates/enc-ffmpeg/src/video/h264.rs
@@ -1627,9 +1627,13 @@ fn get_bitrate(width: u32, height: u32, frame_rate: f32, bpp: f32) -> usize {
let area = (width as f64) * (height as f64);
let pixels_per_second = area * frame_rate_multiplier;
- (pixels_per_second * bpp as f64) as usize
+ // 1 Gbit/s is far past any real H.264 stream; the cap keeps a hostile
+ // bpp from overflowing the max-bitrate math below.
+ ((pixels_per_second * bpp as f64) as usize).min(MAX_BITRATE)
}
+const MAX_BITRATE: usize = 1_000_000_000;
+
#[cfg(test)]
mod self_test_tests {
use super::*;