mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
Merge pull request #3783 from dotCooCoo/fix/theora-frame-rate-validation
craft-video: reject zero Theora frame-rate components
This commit is contained in:
2 files changed
+20
-2
No files matched your search
@@ -103,7 +103,11 @@ fn alloc_vec_exact<T: Clone>(val: T, n: usize) -> Result<Vec<T>> {
|
||||
|
||||
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<Self> {
|
||||
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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user