diff --git a/crates/media/src/gif_anim.rs b/crates/media/src/gif_anim.rs index e2cac5aa8..117f85d9b 100644 --- a/crates/media/src/gif_anim.rs +++ b/crates/media/src/gif_anim.rs @@ -24,6 +24,10 @@ pub(crate) const MAX_BYTES: usize = 256 << 20; /// Frames read from a GIF's or animated WebP's headers when probing. pub(crate) const MAX_FRAMES: usize = 100_000; +/// Frames kept when decoding a GIF or animated WebP: all of them. Only [`MAX_BYTES`] and the +/// canvas budget bound decoding, as before the shared frame readers; [`MAX_FRAMES`] bounds probing. +pub(crate) const DECODE_FRAMES: usize = usize::MAX; + /// Whether `f` is an animated GIF or WebP imported by [`probe`] or [`crate::webp_anim::probe`]: /// its frames come from an [`Anim`]. pub(crate) fn is_animated(f: &Footage) -> bool { @@ -85,7 +89,7 @@ impl Anim { return crate::webp_anim::load(bytes); } let mut limits = craft_gif::frames::FrameLimits::default(); - limits.max_frames = MAX_FRAMES; + limits.max_frames = DECODE_FRAMES; limits.max_bytes = MAX_BYTES as u64; limits.max_canvas_bytes = MAX_BYTES as u64; limits.max_decode_bytes = Some(MAX_BYTES as u64); @@ -121,6 +125,12 @@ impl Anim { Anim::new(frames, starts).ok_or_else(|| MediaError::Decode("GIF: no frames".into())) } + /// How many frames were decoded. + #[cfg(test)] + pub(crate) fn len(&self) -> usize { + self.frames.len() + } + /// The frame shown at source time `t` (the first one before the start, the last one past the /// end). pub(crate) fn frame_at(&self, t: Tick) -> Option<&DynamicImage> { @@ -322,4 +332,26 @@ mod tests { let e = super::Anim::load(&out).err().unwrap(); assert!(e.to_string().contains("too large"), "{e}"); } + + /// A 1×1 GIF of `n` frames. + fn tiny_gif(n: usize) -> Vec { + let mut out = Vec::new(); + { + let mut enc = gif::Encoder::new(&mut out, 1, 1, &[]).unwrap(); + let frame = gif::Frame::from_rgb(1, 1, &RED); + for _ in 0..n { + enc.write_frame(&frame).unwrap(); + } + } + out + } + + #[test] + fn decoding_keeps_every_frame_past_the_probe_frame_count() { + // 1×1 frames fit the byte budget however many there are: the frame count refuses none. + for n in [MAX_FRAMES, MAX_FRAMES + 1] { + let anim = super::Anim::load(&tiny_gif(n)).unwrap(); + assert_eq!(anim.len(), n); + } + } } diff --git a/crates/media/src/webp_anim.rs b/crates/media/src/webp_anim.rs index dbe664fac..bb2fdcc69 100644 --- a/crates/media/src/webp_anim.rs +++ b/crates/media/src/webp_anim.rs @@ -13,7 +13,7 @@ use effectcraft_project::{AlphaMode, Footage, FootageKind}; use effectcraft_time::{FrameRate, Tick}; use image::DynamicImage; -use crate::gif_anim::{Anim, MAX_BYTES, MAX_FRAMES, gcd}; +use crate::gif_anim::{Anim, DECODE_FRAMES, MAX_BYTES, MAX_FRAMES, gcd}; use crate::{MediaError, Result}; /// The `Footage::codec` of an animated WebP. @@ -120,7 +120,7 @@ pub(crate) fn load(bytes: &[u8]) -> Result { return Err(MediaError::Unsupported(format!("animated WebP of {} frames of {}×{} is too large to hold in memory", h.delays.len(), h.width, h.height))); } let mut limits = craft_webp::frames::FrameLimits::default(); - limits.max_frames = MAX_FRAMES; + limits.max_frames = DECODE_FRAMES; limits.max_bytes = MAX_BYTES as u64; limits.max_canvas_bytes = MAX_BYTES as u64; let decoded = craft_webp::frames::read_frames(bytes, &limits).map_err(|e| match e { @@ -381,4 +381,34 @@ mod tests { bytes[4..8].copy_from_slice(&riff.to_le_bytes()); assert!(super::load(&bytes).is_err()); } + + /// A 1×1 animated WebP of `n` identical frames. + fn tiny_webp(n: usize) -> Vec { + let mut anmf = vec![0; 6]; + anmf.extend_from_slice(&u24(0)); + anmf.extend_from_slice(&u24(0)); + anmf.extend_from_slice(&u24(50)); + anmf.push(0b10); + anmf.extend(vp8l(1, 1, RED)); + let frame = chunk(b"ANMF", &anmf); + let mut vp8x = vec![0x12, 0, 0, 0]; + vp8x.extend_from_slice(&u24(0)); + vp8x.extend_from_slice(&u24(0)); + let mut body = b"WEBP".to_vec(); + body.extend(chunk(b"VP8X", &vp8x)); + body.extend(chunk(b"ANIM", &[0; 6])); + for _ in 0..n { + body.extend_from_slice(&frame); + } + chunk(b"RIFF", &body) + } + + #[test] + fn decoding_keeps_every_frame_past_the_probe_frame_count() { + // 1×1 frames fit the byte budget however many there are: the frame count refuses none. + for n in [crate::gif_anim::MAX_FRAMES, crate::gif_anim::MAX_FRAMES + 1] { + let anim = super::load(&tiny_webp(n)).unwrap(); + assert_eq!(anim.len(), n); + } + } }