mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-16 21:15:29 +00:00
Merge pull request #3763 from storytold/codex/effectcraft-gif-webp-decode-shared
media: decode animated GIF and WebP through craft-gif and craft-webp (#778)
This commit is contained in:
Generated
+3
-1
@@ -1182,6 +1182,7 @@ version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=948b5c23c12dc8c87c8b1828892b1462113dd5a7#948b5c23c12dc8c87c8b1828892b1462113dd5a7"
|
||||
dependencies = [
|
||||
"gif",
|
||||
"image",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1390,8 +1391,9 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-webp"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=948b5c23c12dc8c87c8b1828892b1462113dd5a7#948b5c23c12dc8c87c8b1828892b1462113dd5a7"
|
||||
dependencies = [
|
||||
"image",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs"
|
||||
craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "948b5c23c12dc8c87c8b1828892b1462113dd5a7" }
|
||||
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "948b5c23c12dc8c87c8b1828892b1462113dd5a7" }
|
||||
craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f" }
|
||||
craft-mesh = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", default-features = false, features = ["stl", "obj", "gltf", "fbx"] }
|
||||
craft-aep = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
|
||||
@@ -24,8 +24,8 @@ filmcraft-codecs = { workspace = true }
|
||||
filmcraft-media = { workspace = true }
|
||||
craft-dpx = { workspace = true }
|
||||
craft-exr = { workspace = true, features = ["decode"] }
|
||||
craft-gif = { workspace = true }
|
||||
craft-webp = { workspace = true }
|
||||
craft-gif = { workspace = true, features = ["frames"] }
|
||||
craft-webp = { workspace = true, features = ["frames"] }
|
||||
craft-audio = { workspace = true }
|
||||
craft-media = { workspace = true }
|
||||
filmcraft-mpegts = { workspace = true }
|
||||
|
||||
+155
-34
@@ -5,11 +5,9 @@
|
||||
//! delays), so uneven timing is kept exactly; [`Anim::frame_at`] looks the frame up by time.
|
||||
//! Animated WebP ([`crate::webp_anim`]) shares [`Anim`] and this decode path.
|
||||
|
||||
use std::io::Cursor;
|
||||
|
||||
use effectcraft_project::{AlphaMode, Footage, FootageKind};
|
||||
use effectcraft_time::{FrameRate, Tick};
|
||||
use image::{AnimationDecoder, DynamicImage, codecs::gif::GifDecoder};
|
||||
use image::DynamicImage;
|
||||
|
||||
use crate::{MediaError, Result};
|
||||
|
||||
@@ -26,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 {
|
||||
@@ -86,43 +88,57 @@ impl Anim {
|
||||
if crate::webp_anim::is_webp(bytes) {
|
||||
return crate::webp_anim::load(bytes);
|
||||
}
|
||||
let mut dec = GifDecoder::new(Cursor::new(bytes)).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?;
|
||||
// A frame may be far larger than the canvas, and the decoder allocates its buffer before
|
||||
// decoding it: no single buffer may pass what all the frames may hold.
|
||||
let mut limits = image::Limits::no_limits();
|
||||
limits.max_alloc = Some(MAX_BYTES as u64);
|
||||
image::ImageDecoder::set_limits(&mut dec, limits).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?;
|
||||
// The decoder allocates the full logical canvas for the first frame, however small the
|
||||
// frames themselves are: check it before decoding any.
|
||||
let (w, h) = image::ImageDecoder::dimensions(&dec);
|
||||
if u64::from(w) * u64::from(h) * 4 > MAX_BYTES as u64 {
|
||||
return Err(MediaError::Unsupported(format!("GIF canvas of {w}×{h} is too large to hold in memory")));
|
||||
}
|
||||
let (mut frames, mut starts) = (Vec::new(), Vec::new());
|
||||
let (mut at, mut size) = (0i64, 0usize);
|
||||
for frame in dec.into_frames() {
|
||||
let frame = match frame {
|
||||
Ok(f) => f,
|
||||
Err(e) if frames.is_empty() => return Err(MediaError::Decode(format!("GIF: {e}"))),
|
||||
Err(e) => {
|
||||
log::warn!("media: GIF stops after {} frames: {e}", frames.len());
|
||||
break;
|
||||
}
|
||||
};
|
||||
let (numer, denom) = frame.delay().numer_denom_ms();
|
||||
let cs = delay_cs(numer.checked_div(denom).unwrap_or(0) / 10);
|
||||
let buf = frame.into_buffer();
|
||||
size = size.saturating_add(buf.as_raw().len());
|
||||
if size > MAX_BYTES {
|
||||
return Err(MediaError::Unsupported("GIF is too large to hold in memory".into()));
|
||||
// The frame reader scans every image descriptor before it checks the canvas: refuse a
|
||||
// logical screen too large to hold first, as the former decoder did.
|
||||
if let Some(&[w0, w1, h0, h1]) = bytes.get(6..10).filter(|_| bytes.starts_with(b"GIF8")) {
|
||||
let (w, h) = (u16::from_le_bytes([w0, w1]), u16::from_le_bytes([h0, h1]));
|
||||
if u64::from(w) * u64::from(h) * 4 > MAX_BYTES as u64 {
|
||||
return Err(MediaError::Unsupported(format!("GIF canvas of {w}×{h} is too large to hold in memory")));
|
||||
}
|
||||
}
|
||||
let mut limits = craft_gif::frames::FrameLimits::default();
|
||||
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);
|
||||
let decoded = craft_gif::frames::read_frames(bytes, &limits).map_err(|e| match e {
|
||||
craft_gif::frames::FrameError::Malformed(m) => MediaError::Decode(format!("GIF: {m}")),
|
||||
craft_gif::frames::FrameError::LimitExceeded(m)
|
||||
if m.contains("cannot hold") || m.contains("decode-allocation limit") || m.to_ascii_lowercase().contains("memory limit") =>
|
||||
{
|
||||
MediaError::Unsupported("GIF is too large to hold in memory".into())
|
||||
}
|
||||
craft_gif::frames::FrameError::LimitExceeded(m) if m.contains("canvas is over") => {
|
||||
MediaError::Unsupported("GIF canvas is too large to hold in memory".into())
|
||||
}
|
||||
craft_gif::frames::FrameError::LimitExceeded(m) => MediaError::Unsupported(format!("GIF: {m}")),
|
||||
})?;
|
||||
if matches!(&decoded.stop, craft_gif::frames::Stop::ByteLimit | craft_gif::frames::Stop::FrameLimit) {
|
||||
return Err(MediaError::Unsupported("GIF is too large to hold in memory".into()));
|
||||
}
|
||||
if let craft_gif::frames::Stop::Damaged(ref e) = decoded.stop {
|
||||
log::warn!("media: GIF stops after {} frames: {e}", decoded.frames.len());
|
||||
}
|
||||
let mut frames = Vec::with_capacity(decoded.frames.len());
|
||||
let mut starts = Vec::with_capacity(decoded.frames.len());
|
||||
let mut at = 0i64;
|
||||
for frame in decoded.frames {
|
||||
let cs = delay_cs(frame.delay_ms / 10);
|
||||
let image = image::RgbaImage::from_raw(decoded.width, decoded.height, frame.rgba)
|
||||
.ok_or_else(|| MediaError::Decode("GIF: invalid decoded frame dimensions".into()))?;
|
||||
starts.push(Tick::from_units(at, 100));
|
||||
at = at.saturating_add(i64::from(cs));
|
||||
frames.push(DynamicImage::ImageRgba8(buf));
|
||||
frames.push(DynamicImage::ImageRgba8(image));
|
||||
}
|
||||
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> {
|
||||
@@ -133,9 +149,13 @@ impl Anim {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Cursor;
|
||||
|
||||
use effectcraft_project::{Footage, FootageKind};
|
||||
use effectcraft_time::{FrameRate, Tick};
|
||||
use image::AnimationDecoder;
|
||||
|
||||
use super::MAX_FRAMES;
|
||||
use crate::{MediaPool, probe_bytes};
|
||||
|
||||
/// A 2×2 GIF with one solid-colour frame per `(rgb, delay)`.
|
||||
@@ -152,6 +172,15 @@ mod tests {
|
||||
out
|
||||
}
|
||||
|
||||
/// Preserve the old consumer path as a test oracle while the frame API is shared.
|
||||
fn legacy_gif_frames(bytes: &[u8]) -> Vec<image::RgbaImage> {
|
||||
image::codecs::gif::GifDecoder::new(Cursor::new(bytes)).unwrap().into_frames().map(|frame| frame.unwrap().into_buffer()).collect()
|
||||
}
|
||||
|
||||
fn legacy_gif_frames_until_error(bytes: &[u8]) -> Vec<image::RgbaImage> {
|
||||
image::codecs::gif::GifDecoder::new(Cursor::new(bytes)).unwrap().into_frames().map_while(|frame| frame.ok()).map(|frame| frame.into_buffer()).collect()
|
||||
}
|
||||
|
||||
const RED: [u8; 3] = [255, 0, 0];
|
||||
const GREEN: [u8; 3] = [0, 255, 0];
|
||||
const BLUE: [u8; 3] = [0, 0, 255];
|
||||
@@ -201,6 +230,62 @@ mod tests {
|
||||
assert_eq!(f.duration, Tick::from_units(20, 100));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_gif_frames_match_legacy_compositing_and_disposal() {
|
||||
let mut bytes = Vec::new();
|
||||
{
|
||||
let palette = [255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0];
|
||||
let mut enc = gif::Encoder::new(&mut bytes, 3, 2, &palette).unwrap();
|
||||
let first = gif::Frame {
|
||||
width: 3,
|
||||
height: 2,
|
||||
buffer: vec![0; 6].into(), // red background
|
||||
delay: 5,
|
||||
dispose: gif::DisposalMethod::Keep,
|
||||
..gif::Frame::default()
|
||||
};
|
||||
enc.write_frame(&first).unwrap();
|
||||
|
||||
let second = gif::Frame {
|
||||
left: 1,
|
||||
width: 1,
|
||||
height: 1,
|
||||
buffer: vec![1].into(), // green patch
|
||||
delay: 7,
|
||||
dispose: gif::DisposalMethod::Background,
|
||||
..gif::Frame::default()
|
||||
};
|
||||
enc.write_frame(&second).unwrap();
|
||||
|
||||
let third = gif::Frame {
|
||||
left: 2,
|
||||
width: 1,
|
||||
height: 1,
|
||||
buffer: vec![2].into(), // blue patch after disposal
|
||||
delay: 9,
|
||||
dispose: gif::DisposalMethod::Previous,
|
||||
..gif::Frame::default()
|
||||
};
|
||||
enc.write_frame(&third).unwrap();
|
||||
|
||||
let fourth = gif::Frame {
|
||||
width: 1,
|
||||
height: 1,
|
||||
buffer: vec![3].into(), // black patch after restoring the previous canvas
|
||||
delay: 11,
|
||||
..gif::Frame::default()
|
||||
};
|
||||
enc.write_frame(&fourth).unwrap();
|
||||
}
|
||||
let expected = legacy_gif_frames(&bytes);
|
||||
let actual = super::Anim::load(&bytes).unwrap();
|
||||
assert_eq!(actual.frames.len(), expected.len());
|
||||
for (frame, old) in actual.frames.iter().zip(expected) {
|
||||
assert_eq!(frame.to_rgba8(), old);
|
||||
}
|
||||
assert_eq!(actual.starts, vec![Tick::from_units(0, 100), Tick::from_units(5, 100), Tick::from_units(12, 100), Tick::from_units(21, 100)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_single_frame_gif_stays_a_still() {
|
||||
let f = probe_bytes("a.gif", gif(&[(RED, 10)]).into()).unwrap();
|
||||
@@ -228,6 +313,20 @@ mod tests {
|
||||
let _ = probe_bytes("a.gif", bytes.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_damaged_gif_tail_keeps_frames_already_decoded() {
|
||||
let mut bytes = gif(&[(RED, 5), (GREEN, 5), (BLUE, 5)]);
|
||||
let info = craft_gif::scan(&bytes, MAX_FRAMES).unwrap();
|
||||
assert_eq!(info.frames.len(), 3);
|
||||
bytes.truncate(info.frames[1].offset);
|
||||
|
||||
let legacy = legacy_gif_frames_until_error(&bytes);
|
||||
assert_eq!(legacy.len(), 1);
|
||||
let actual = super::Anim::load(&bytes).unwrap();
|
||||
assert_eq!(actual.frames.len(), legacy.len());
|
||||
assert_eq!(actual.frames[0].to_rgba8(), legacy[0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_huge_canvas_is_refused_before_decoding() {
|
||||
// A 65535×65535 logical screen (16 GiB of RGBA) holding one 1×1 frame.
|
||||
@@ -239,6 +338,28 @@ mod tests {
|
||||
}
|
||||
}
|
||||
let e = super::Anim::load(&out).err().unwrap();
|
||||
assert!(e.to_string().contains("too large"), "{e}");
|
||||
assert!(e.to_string().contains("GIF canvas of 65535×65535 is too large"), "{e}");
|
||||
}
|
||||
|
||||
/// A 1×1 GIF of `n` frames.
|
||||
fn tiny_gif(n: usize) -> Vec<u8> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+125
-26
@@ -9,13 +9,11 @@
|
||||
//! every delay is a whole number of frames at (1000 / the greatest common divisor of the delays),
|
||||
//! so it is kept exactly. A still WebP (no animation flag, or a single frame) stays a still.
|
||||
|
||||
use std::io::Cursor;
|
||||
|
||||
use effectcraft_project::{AlphaMode, Footage, FootageKind};
|
||||
use effectcraft_time::{FrameRate, Tick};
|
||||
use image::{AnimationDecoder, DynamicImage, codecs::webp::WebPDecoder};
|
||||
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.
|
||||
@@ -121,29 +119,29 @@ pub(crate) fn load(bytes: &[u8]) -> Result<Anim> {
|
||||
if frame_bytes.saturating_mul(h.delays.len()) > MAX_BYTES {
|
||||
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 dec = WebPDecoder::new(Cursor::new(bytes)).map_err(|e| MediaError::Decode(format!("WebP: {e}")))?;
|
||||
// The budget above used the canvas this file's header scan found; the decoder must agree, or
|
||||
// a second VP8X header could make it allocate a far larger canvas.
|
||||
if image::ImageDecoder::dimensions(&dec) != (h.width, h.height) {
|
||||
let mut limits = craft_webp::frames::FrameLimits::default();
|
||||
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 {
|
||||
craft_webp::frames::FrameError::Malformed(m) => MediaError::Decode(format!("WebP: {m}")),
|
||||
craft_webp::frames::FrameError::LimitExceeded(m) => MediaError::Unsupported(format!("WebP: {m}")),
|
||||
})?;
|
||||
if matches!(&decoded.stop, craft_webp::frames::Stop::ByteLimit | craft_webp::frames::Stop::FrameLimit) {
|
||||
return Err(MediaError::Unsupported("animated WebP is too large to hold in memory".into()));
|
||||
}
|
||||
if let craft_webp::frames::Stop::Damaged(ref e) = decoded.stop {
|
||||
log::warn!("media: WebP stops after {} frames: {e}", decoded.frames.len());
|
||||
}
|
||||
if (decoded.width, decoded.height) != (h.width, h.height) {
|
||||
return Err(MediaError::Decode("WebP: conflicting canvas sizes".into()));
|
||||
}
|
||||
let (mut frames, mut delays, mut size) = (Vec::new(), Vec::new(), 0usize);
|
||||
for frame in dec.into_frames() {
|
||||
let frame = match frame {
|
||||
Ok(f) => f,
|
||||
Err(e) if frames.is_empty() => return Err(MediaError::Decode(format!("WebP: {e}"))),
|
||||
Err(e) => {
|
||||
log::warn!("media: WebP stops after {} frames: {e}", frames.len());
|
||||
break;
|
||||
}
|
||||
};
|
||||
let (numer, denom) = frame.delay().numer_denom_ms();
|
||||
delays.push(numer.checked_div(denom).unwrap_or(0));
|
||||
let buf = frame.into_buffer();
|
||||
size = size.saturating_add(buf.as_raw().len());
|
||||
if size > MAX_BYTES {
|
||||
return Err(MediaError::Unsupported("animated WebP is too large to hold in memory".into()));
|
||||
}
|
||||
let mut frames = Vec::with_capacity(decoded.frames.len());
|
||||
let mut delays = Vec::with_capacity(decoded.frames.len());
|
||||
for frame in decoded.frames {
|
||||
delays.push(frame.delay_ms);
|
||||
let buf = image::RgbaImage::from_raw(decoded.width, decoded.height, frame.rgba)
|
||||
.ok_or_else(|| MediaError::Decode("WebP: invalid decoded frame dimensions".into()))?;
|
||||
frames.push(DynamicImage::ImageRgba8(buf));
|
||||
}
|
||||
let starts = timing(&delays).starts;
|
||||
@@ -152,9 +150,11 @@ pub(crate) fn load(bytes: &[u8]) -> Result<Anim> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Cursor;
|
||||
|
||||
use effectcraft_project::{AlphaMode, Footage, FootageKind};
|
||||
use effectcraft_time::{FrameRate, Tick};
|
||||
use image::{ExtendedColorType, ImageEncoder, codecs::webp::WebPEncoder};
|
||||
use image::{AnimationDecoder, ExtendedColorType, ImageEncoder, codecs::webp::WebPDecoder, codecs::webp::WebPEncoder};
|
||||
|
||||
use super::timing;
|
||||
use crate::{MediaPool, probe_bytes};
|
||||
@@ -204,6 +204,43 @@ mod tests {
|
||||
chunk(b"RIFF", &body)
|
||||
}
|
||||
|
||||
/// Build an animation whose frames may cover only part of the canvas. `x` and `y` are even,
|
||||
/// as required by ANMF; bit 1 of `flags` selects no-blend and bit 0 selects disposal.
|
||||
fn webp_subframes(canvas: (u32, u32), frames: &[(u32, u32, u32, u32, [u8; 4], u32, u8)]) -> Vec<u8> {
|
||||
let (w, h) = canvas;
|
||||
let mut vp8x = vec![0x12, 0, 0, 0];
|
||||
vp8x.extend_from_slice(&u24(w - 1));
|
||||
vp8x.extend_from_slice(&u24(h - 1));
|
||||
let mut body = b"WEBP".to_vec();
|
||||
body.extend(chunk(b"VP8X", &vp8x));
|
||||
body.extend(chunk(b"ANIM", &[0; 6]));
|
||||
for &(x, y, fw, fh, rgba, delay, flags) in frames {
|
||||
let mut anmf = Vec::new();
|
||||
anmf.extend_from_slice(&u24(x / 2));
|
||||
anmf.extend_from_slice(&u24(y / 2));
|
||||
anmf.extend_from_slice(&u24(fw - 1));
|
||||
anmf.extend_from_slice(&u24(fh - 1));
|
||||
anmf.extend_from_slice(&u24(delay));
|
||||
anmf.push(flags);
|
||||
anmf.extend(vp8l(fw, fh, rgba));
|
||||
body.extend(chunk(b"ANMF", &anmf));
|
||||
}
|
||||
chunk(b"RIFF", &body)
|
||||
}
|
||||
|
||||
fn anmf_offsets(bytes: &[u8]) -> Vec<usize> {
|
||||
let mut offsets = Vec::new();
|
||||
let mut at = 12usize;
|
||||
while at + 8 <= bytes.len() {
|
||||
let size = u32::from_le_bytes(bytes[at + 4..at + 8].try_into().unwrap()) as usize;
|
||||
if &bytes[at..at + 4] == b"ANMF" {
|
||||
offsets.push(at);
|
||||
}
|
||||
at = at.saturating_add(8).saturating_add(size).saturating_add(size & 1);
|
||||
}
|
||||
offsets
|
||||
}
|
||||
|
||||
const RED: [u8; 4] = [255, 0, 0, 255];
|
||||
const GREEN: [u8; 4] = [0, 255, 0, 255];
|
||||
const BLUE: [u8; 4] = [0, 0, 255, 128];
|
||||
@@ -238,6 +275,38 @@ mod tests {
|
||||
assert_eq!(pixel_at(&pool, &f, 5.0), last);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_webp_frames_match_legacy_subrect_blending_and_disposal() {
|
||||
// Includes a blended alpha patch, then a no-blend patch disposed to the background.
|
||||
let bytes =
|
||||
webp_subframes((4, 2), &[(0, 0, 4, 2, RED, 50, 0b10), (2, 0, 2, 2, BLUE, 60, 0b00), (0, 0, 2, 2, GREEN, 70, 0b11), (2, 0, 2, 2, RED, 80, 0b10)]);
|
||||
let expected = WebPDecoder::new(Cursor::new(&bytes)).unwrap().into_frames().map(|frame| frame.unwrap().into_buffer()).collect::<Vec<_>>();
|
||||
let actual = super::load(&bytes).unwrap();
|
||||
assert_eq!(expected.len(), 4);
|
||||
for (index, (start_ms, old)) in [(0, &expected[0]), (50, &expected[1]), (110, &expected[2]), (180, &expected[3])].into_iter().enumerate() {
|
||||
let tick = Tick::from_units(start_ms, 1000);
|
||||
let frame = actual.frame_at(tick).unwrap().to_rgba8();
|
||||
assert_eq!(frame, *old, "frame {index} at {start_ms} ms");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_damaged_webp_tail_keeps_frames_already_decoded() {
|
||||
let mut bytes = webp(4, 2, &[(RED, 50), (GREEN, 60), (BLUE, 70)]);
|
||||
let offsets = anmf_offsets(&bytes);
|
||||
assert_eq!(offsets.len(), 3);
|
||||
bytes.truncate(offsets[2]);
|
||||
|
||||
let legacy =
|
||||
WebPDecoder::new(Cursor::new(&bytes)).unwrap().into_frames().map_while(|frame| frame.ok()).map(|frame| frame.into_buffer()).collect::<Vec<_>>();
|
||||
assert_eq!(legacy.len(), 2);
|
||||
let actual = super::load(&bytes).unwrap();
|
||||
for (index, old) in legacy.iter().enumerate() {
|
||||
let tick = Tick::from_units(if index == 0 { 0 } else { 50 }, 1000);
|
||||
assert_eq!(actual.frame_at(tick).unwrap().to_rgba8(), *old);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delays_rounded_to_milliseconds_read_as_the_standard_rate() {
|
||||
// 24 frames a second rounded to whole milliseconds, as ffmpeg writes them.
|
||||
@@ -312,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<u8> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user