Merge pull request #3671 from storytold/codex/gif-writer-consumer

export: write animated GIFs with the shared craft-gif writer (#778)
This commit is contained in:
FredApps
2026-11-16 09:36:06 +01:00
committed by GitHub
9 changed files with 135 additions and 108 deletions
Generated
+5 -1
View File
@@ -1179,7 +1179,10 @@ source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b4255
[[package]]
name = "craft-gif"
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 = [
"gif",
]
[[package]]
name = "craft-i18n"
@@ -1760,6 +1763,7 @@ name = "effectcraft-export"
version = "0.24.1"
dependencies = [
"craft-audio",
"craft-gif",
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9)",
"craft-video",
"effectcraft-color",
+1 -1
View File
@@ -19,7 +19,7 @@ craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/c
craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-eps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "bc3e521bd37e53f2b5b4c55278111b55fd9a40f9" }
craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
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" }
+2 -1
View File
@@ -17,16 +17,17 @@ effectcraft-render = { workspace = true }
craft-video = { workspace = true }
craft-audio = { workspace = true }
craft-media = { workspace = true }
craft-gif = { workspace = true }
effectcraft-opusenc = { workspace = true }
image = { workspace = true, features = ["png", "jpeg", "tiff", "exr", "tga"] }
exr = { workspace = true }
gif = { workspace = true }
rayon = { workspace = true }
web-time = { workspace = true }
thiserror = { workspace = true }
log = { workspace = true }
[dev-dependencies]
gif = { workspace = true }
tempfile = { workspace = true }
effectcraft-media = { workspace = true }
effectcraft-effects = { workspace = true }
+36 -17
View File
@@ -421,13 +421,22 @@ fn write_still<W: std::io::Write + std::io::Seek>(cx: &Cx, file: &mut W, img: &I
}
async fn gif_export(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut State<'_>) -> Result<Report> {
if w > u16::MAX as u32 || h > u16::MAX as u32 {
return Err(ExportError::Unsupported("GIF frames are limited to 65535 px".into()));
}
use craft_gif::write::{AnimationWriter, Disposal, Options, PreparedFrame};
validate_gif_dimensions(w, h)?;
let path = job.place(job.path, 1);
let mut file = job.create(&path)?;
let mut enc = gif::Encoder::new(&mut file, w as u16, h as u16, &[]).map_err(|e| ExportError::Encode(e.to_string()))?;
enc.set_repeat(if job.output.gif_loop { gif::Repeat::Infinite } else { gif::Repeat::Finite(0) }).map_err(|e| ExportError::Encode(e.to_string()))?;
let mut enc = AnimationWriter::new(
&mut file,
w,
h,
Options {
loop_forever: job.output.gif_loop,
disposal: if job.output.channels == Channels::Rgba { Disposal::Background } else { Disposal::Keep },
speed: 10,
},
)
.map_err(|e| ExportError::Encode(e.to_string()))?;
let rate = job.settings.rate(comp);
// GIF delays are in 1/100 s: carry the rounding error so long animations keep their timing.
let cs_per_frame = 100.0 * rate.den as f64 / rate.num as f64;
@@ -437,30 +446,31 @@ async fn gif_export(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut State<'_
let mut i = 0;
while i < st.total {
let end = (i + batch).min(st.total);
let frames: Vec<gif::Frame<'static>> = job
.frames(comp, (i..end).collect(), |_, img| {
let mut px = job.pixels_with(&img, comp, job.output.channels, w, h, false);
gif::Frame::from_rgba_speed(w as u16, h as u16, &mut px, 10)
})
let frames: Vec<std::result::Result<PreparedFrame, craft_gif::write::Error>> = job
.frames(comp, (i..end).collect(), |_, img| craft_gif::write::prepare_frame(w, h, job.pixels_with(&img, comp, job.output.channels, w, h, false), 10))
.await;
for mut f in frames {
for frame in frames {
clock += cs_per_frame;
let d = (clock.round() as u64).saturating_sub(emitted).max(1);
emitted += d;
f.delay = d.min(u16::MAX as u64) as u16;
if job.output.channels == Channels::Rgba {
f.dispose = gif::DisposalMethod::Background;
}
enc.write_frame(&f).map_err(|e| ExportError::Encode(e.to_string()))?;
let frame = frame.map_err(|e| ExportError::Encode(e.to_string()))?;
enc.write_prepared_frame(frame, d.min(u16::MAX as u64) as u16).map_err(|e| ExportError::Encode(e.to_string()))?;
}
st.advance(end - i)?;
i = end;
}
drop(enc);
let _ = enc.finish().map_err(|e| ExportError::Encode(e.to_string()))?;
let bytes = file.finish()?;
Ok(Report { path, frames: 0, width: w, height: h, seconds: 0.0, bytes, audio: false, log: None, overflow: vec![] })
}
fn validate_gif_dimensions(w: u32, h: u32) -> Result<()> {
if w > u16::MAX as u32 || h > u16::MAX as u32 {
return Err(ExportError::Unsupported("GIF frames are limited to 65535 px".into()));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
@@ -478,4 +488,13 @@ mod tests {
assert_eq!(OutputFormat::H264.coded_size(1, 101), (2, 100));
assert_eq!(OutputFormat::Gif.coded_size(1, 101), (1, 101));
}
#[test]
fn gif_dimension_limit_keeps_the_existing_unsupported_error() {
assert!(validate_gif_dimensions(u16::MAX as u32, u16::MAX as u32).is_ok());
assert!(matches!(
validate_gif_dimensions(u16::MAX as u32 + 1, 1),
Err(ExportError::Unsupported(message)) if message == "GIF frames are limited to 65535 px"
));
}
}
+1 -27
View File
@@ -8,6 +8,7 @@
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::support::write_tone;
use effectcraft_color::Label;
use effectcraft_export::render_queue::{
AudioOutput, CodecProfile, OpusApplication, OutputFormat, OutputModule, RateControlMode, RenderSettings, TimeSpan, WebmVideoCodec,
@@ -299,30 +300,3 @@ fn rms(v: impl Iterator<Item = f32>) -> f32 {
let (s, n) = v.fold((0.0f64, 0usize), |(s, n), x| (s + (x as f64) * (x as f64), n + 1));
(s / n.max(1) as f64).sqrt() as f32
}
/// 16-bit stereo WAV with a 440 Hz sine at amplitude 0.5 (left) and silence (right).
fn write_tone(path: &Path, secs: f64) {
let sr = 48_000u32;
let n = (secs * sr as f64) as usize;
let mut data = Vec::with_capacity(n * 4);
for i in 0..n {
let s = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * i as f64 / sr as f64).sin() * 32767.0).round() as i16;
data.extend_from_slice(&s.to_le_bytes());
data.extend_from_slice(&0i16.to_le_bytes());
}
let mut w = Vec::new();
w.extend_from_slice(b"RIFF");
w.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
w.extend_from_slice(b"WAVEfmt ");
w.extend_from_slice(&16u32.to_le_bytes());
w.extend_from_slice(&1u16.to_le_bytes());
w.extend_from_slice(&2u16.to_le_bytes());
w.extend_from_slice(&sr.to_le_bytes());
w.extend_from_slice(&(sr * 4).to_le_bytes());
w.extend_from_slice(&4u16.to_le_bytes());
w.extend_from_slice(&16u16.to_le_bytes());
w.extend_from_slice(b"data");
w.extend_from_slice(&(data.len() as u32).to_le_bytes());
w.extend_from_slice(&data);
std::fs::write(path, w).expect("write wav");
}
+1
View File
@@ -18,6 +18,7 @@ mod opus_roundtrip;
mod prores_color;
mod render_settings;
mod roundtrip;
mod support;
mod targa_sequence;
mod uncompressed_mov;
mod vp9_roundtrip;
+58 -34
View File
@@ -9,6 +9,7 @@
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::support::write_tone;
use effectcraft_color::Label;
use effectcraft_export::render_queue::{AlphaMode, AudioOutput, Channels, OutputFormat, OutputModule, ProResProfile, RenderSettings, TimeSpan, sequence_path};
use effectcraft_export::{Job, Progress, export};
@@ -543,21 +544,47 @@ fn exr_sequence_values_at_or_below_one_are_unchanged() {
assert!((c[0] - 1.0).abs() < 1e-4 && (c[1] - lin(0.4)).abs() < 1e-4 && c[2].abs() < 1e-6, "{channels:?}: {c:?}");
}
}
#[test]
fn gif_roundtrip() {
/// Render the test project as a GIF with `om` into `out_dir(name)`, and open it for RGBA decoding.
fn render_gif(name: &str, om: &OutputModule) -> (PathBuf, gif::Decoder<std::fs::File>) {
let (p, cid) = project(None);
let d = out_dir("gif");
let om = OutputModule::for_format(OutputFormat::Gif);
let path = d.join("anim.gif");
run(&p, cid, &NoFootage, &om, &path);
let path = out_dir(name).join("anim.gif");
run(&p, cid, &NoFootage, om, &path);
let mut opts = gif::DecodeOptions::new();
opts.set_color_output(gif::ColorOutput::RGBA);
let mut dec = opts.read_info(std::fs::File::open(&path).expect("open")).expect("gif header");
let dec = opts.read_info(std::fs::File::open(&path).expect("open")).expect("gif header");
(path, dec)
}
/// The test project's frames, rendered as PNGs, re-encoded through the former app-local gif 0.13
/// path (`Frame::from_rgba_speed` at speed 10, 1/10 s per frame, looping).
fn reference_gif() -> Vec<u8> {
let (p, cid) = project(None);
let png_pattern = out_dir("gif-byte-parity").join("frame_[#####].png");
run(&p, cid, &NoFootage, &OutputModule::for_format(OutputFormat::PngSequence), &png_pattern);
let png_pattern = png_pattern.to_string_lossy();
let mut expected = Vec::new();
let mut encoder = gif::Encoder::new(&mut expected, W as u16, H as u16, &[]).expect("reference GIF encoder");
encoder.set_repeat(gif::Repeat::Infinite).expect("reference GIF repeat");
for frame_number in 0..FRAMES {
let mut rgba = image::open(sequence_path(&png_pattern, frame_number as i64)).expect("reference PNG").to_rgba8().into_raw();
let mut frame = gif::Frame::from_rgba_speed(W as u16, H as u16, &mut rgba, 10);
frame.delay = 10;
encoder.write_frame(&frame).expect("reference GIF frame");
}
drop(encoder);
expected
}
#[test]
fn gif_roundtrip() {
let (path, mut dec) = render_gif("gif", &OutputModule::for_format(OutputFormat::Gif));
assert_eq!((dec.width() as u32, dec.height() as u32), (W, H));
assert_eq!(dec.repeat(), gif::Repeat::Infinite);
let mut k = 0;
let mut total_delay = 0u32;
while let Some(f) = dec.read_next_frame().expect("frame") {
assert_eq!((f.width as u32, f.height as u32), (W, H));
assert_eq!(f.dispose, gif::DisposalMethod::Keep);
total_delay += f.delay as u32;
let img = Image::from_rgba8(W, H, &f.buffer);
check_frame(&img, k, 0.06);
@@ -570,6 +597,30 @@ fn gif_roundtrip() {
}
}
#[test]
fn gif_matches_the_former_encoder_byte_for_byte() {
// The shared writer must keep the complete output, quantization and LZW data included.
let (path, _) = render_gif("gif-parity", &OutputModule::for_format(OutputFormat::Gif));
let actual = std::fs::read(&path).expect("GIF bytes");
let expected = reference_gif();
let difference = actual.iter().zip(&expected).position(|(a, b)| a != b);
assert!(
actual.len() == expected.len() && difference.is_none(),
"GIF bytes differ: {} vs {} bytes, first difference at {difference:?}",
actual.len(),
expected.len()
);
}
#[test]
fn gif_rgba_keeps_background_disposal_and_non_looping() {
let om = OutputModule { channels: Channels::Rgba, gif_loop: false, ..OutputModule::for_format(OutputFormat::Gif) };
let (_, mut dec) = render_gif("gif-rgba", &om);
assert_eq!(dec.repeat(), gif::Repeat::Finite(0));
let first = dec.read_next_frame().expect("frame").expect("first frame");
assert_eq!(first.dispose, gif::DisposalMethod::Background);
}
#[test]
fn cancel_stops_early() {
let (p, cid) = project(None);
@@ -596,33 +647,6 @@ fn cancel_stops_early() {
assert_eq!(written, 0, "cancelled before the first batch");
}
/// 16-bit stereo WAV with a 440 Hz sine at amplitude 0.5 (left) and silence (right).
fn write_tone(path: &Path, secs: f64) {
let sr = 48_000u32;
let n = (secs * sr as f64) as usize;
let mut data = Vec::with_capacity(n * 4);
for i in 0..n {
let s = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * i as f64 / sr as f64).sin() * 32767.0).round() as i16;
data.extend_from_slice(&s.to_le_bytes());
data.extend_from_slice(&0i16.to_le_bytes());
}
let mut w = Vec::new();
w.extend_from_slice(b"RIFF");
w.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
w.extend_from_slice(b"WAVEfmt ");
w.extend_from_slice(&16u32.to_le_bytes());
w.extend_from_slice(&1u16.to_le_bytes());
w.extend_from_slice(&2u16.to_le_bytes());
w.extend_from_slice(&sr.to_le_bytes());
w.extend_from_slice(&(sr * 4).to_le_bytes());
w.extend_from_slice(&4u16.to_le_bytes());
w.extend_from_slice(&16u16.to_le_bytes());
w.extend_from_slice(b"data");
w.extend_from_slice(&(data.len() as u32).to_le_bytes());
w.extend_from_slice(&data);
std::fs::write(path, w).expect("write wav");
}
fn rms(v: impl Iterator<Item = f32>) -> f32 {
let (s, n) = v.fold((0.0f64, 0usize), |(s, n), x| (s + (x as f64) * (x as f64), n + 1));
(s / n.max(1) as f64).sqrt() as f32
+30
View File
@@ -0,0 +1,30 @@
//! Helpers shared by the integration test modules.
use std::path::Path;
/// 16-bit stereo 48 kHz WAV: a 440 Hz sine at 0.5 on the left, silence on the right.
pub(crate) fn write_tone(path: &Path, secs: f64) {
let sr = 48_000u32;
let n = (secs * sr as f64) as usize;
let mut data = Vec::with_capacity(n * 4);
for i in 0..n {
let s = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * i as f64 / sr as f64).sin() * 32767.0).round() as i16;
data.extend_from_slice(&s.to_le_bytes());
data.extend_from_slice(&0i16.to_le_bytes());
}
let mut w = Vec::new();
w.extend_from_slice(b"RIFF");
w.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
w.extend_from_slice(b"WAVEfmt ");
w.extend_from_slice(&16u32.to_le_bytes());
w.extend_from_slice(&1u16.to_le_bytes());
w.extend_from_slice(&2u16.to_le_bytes());
w.extend_from_slice(&sr.to_le_bytes());
w.extend_from_slice(&(sr * 4).to_le_bytes());
w.extend_from_slice(&4u16.to_le_bytes());
w.extend_from_slice(&16u16.to_le_bytes());
w.extend_from_slice(b"data");
w.extend_from_slice(&(data.len() as u32).to_le_bytes());
w.extend_from_slice(&data);
std::fs::write(path, w).expect("write wav");
}
+1 -27
View File
@@ -8,6 +8,7 @@
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::support::write_tone;
use craft_media::isobmff::TrackKind;
use effectcraft_color::Label;
use effectcraft_export::render_queue::{AudioFormat, AudioOutput, Channels, OutputFormat, OutputModule, RenderSettings, TimeSpan, sequence_path};
@@ -28,33 +29,6 @@ fn out_dir(name: &str) -> PathBuf {
d
}
/// 16-bit stereo 48 kHz WAV: a 440 Hz sine at 0.5 on the left, silence on the right.
fn write_tone(path: &Path, secs: f64) {
let sr = 48_000u32;
let n = (secs * sr as f64) as usize;
let mut data = Vec::with_capacity(n * 4);
for i in 0..n {
let s = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * i as f64 / sr as f64).sin() * 32767.0).round() as i16;
data.extend_from_slice(&s.to_le_bytes());
data.extend_from_slice(&0i16.to_le_bytes());
}
let mut w = Vec::new();
w.extend_from_slice(b"RIFF");
w.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
w.extend_from_slice(b"WAVEfmt ");
w.extend_from_slice(&16u32.to_le_bytes());
w.extend_from_slice(&1u16.to_le_bytes());
w.extend_from_slice(&2u16.to_le_bytes());
w.extend_from_slice(&sr.to_le_bytes());
w.extend_from_slice(&(sr * 4).to_le_bytes());
w.extend_from_slice(&4u16.to_le_bytes());
w.extend_from_slice(&16u16.to_le_bytes());
w.extend_from_slice(b"data");
w.extend_from_slice(&(data.len() as u32).to_le_bytes());
w.extend_from_slice(&data);
std::fs::write(path, w).expect("write wav");
}
fn project(audio: Option<&Path>) -> (Project, ItemId) {
let mut p = Project::default();
let mut comp = Comp::new(W, H, FrameRate::new(10, 1), Tick::from_seconds_f64(FRAMES as f64 / 10.0));