diff --git a/crates/video/src/theora/encoder.rs b/crates/video/src/theora/encoder.rs index 7e1eb82d05..c02614ae3d 100644 --- a/crates/video/src/theora/encoder.rs +++ b/crates/video/src/theora/encoder.rs @@ -103,7 +103,11 @@ fn alloc_vec_exact(val: T, n: usize) -> Result> { impl Encoder { /// Create a new encoder with the given configuration. + /// The frame-rate numerator and denominator must both be nonzero. pub fn new(config: EncoderConfig) -> Result { + if config.fps_num == 0 || config.fps_den == 0 { + return Err(Error::InvalidHeader("framerate cannot be zero")); + } if config.width == 0 || config.height == 0 || config.pic_width == 0 || config.pic_height == 0 { return Err(Error::DimensionMismatch("dimensions cannot be zero")); } @@ -158,8 +162,8 @@ impl Encoder { pic_height: config.pic_height.min(frame_height), pic_x: config.pic_x.min(frame_width - config.pic_width.min(frame_width)), pic_y: config.pic_y.min(frame_height - config.pic_height.min(frame_height)), - fps_num: config.fps_num.max(1), - fps_den: config.fps_den.max(1), + fps_num: config.fps_num, + fps_den: config.fps_den, par_num: 1, par_den: 1, color_space: config.color_space, diff --git a/crates/video/tests/it/theora.rs b/crates/video/tests/it/theora.rs index eadc09d29d..0d0dcc394b 100644 --- a/crates/video/tests/it/theora.rs +++ b/crates/video/tests/it/theora.rs @@ -49,6 +49,20 @@ fn psnr(a: &[u8], b: &[u8]) -> f64 { if mse == 0.0 { 99.0 } else { 10.0 * (255.0f64 * 255.0 / mse).log10() } } +#[test] +fn test_theora_encoder_rejects_zero_frame_rate_components() { + let config = EncoderConfig { width: 16, height: 16, pic_width: 16, pic_height: 16, ..EncoderConfig::default() }; + for (fps_num, fps_den) in [(0, 1), (25, 0), (0, 0)] { + assert!(matches!(Encoder::new(EncoderConfig { fps_num, fps_den, ..config.clone() }), Err(Error::InvalidHeader(_)))); + } + for (fps_num, fps_den) in [(1, 1), (30_000, 1001), (u32::MAX, u32::MAX)] { + let encoder = Encoder::new(EncoderConfig { fps_num, fps_den, ..config.clone() }).unwrap(); + let header = IdentificationHeader::parse(&encoder.headers().0).unwrap(); + assert_eq!((header.info.fps_num, header.info.fps_den), (fps_num, fps_den)); + assert_eq!((encoder.config().fps_num, encoder.config().fps_den), (fps_num, fps_den)); + } +} + #[test] fn test_theora_headers_roundtrip() { let cfg = EncoderConfig {