GitCorner

Code

ossartcrafteffectcraft๐ŸŒ
665bfac0b339
//! Encode a short comp to every output format and decode it back: through EffectCraft's own
//! media layer (FilmCraft decoders) for MP4/MOV, the `image`/`gif` crates for stills and GIF,
//! and โ€” when installed โ€” ffprobe as an external oracle (skipped otherwise).
//!
//! The comp: 64ร—48 @ 10 fps, 1.2 s (12 frames), dark-blue background, a red solid of 32ร—48
//! centred, starting at frame 5. So frame < 5 is background everywhere; from frame 5 the centre
//! is red and the corners stay background.

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};
use effectcraft_media::MediaPool;
use effectcraft_project::{Comp, ItemId, ItemKind, LayerSource, Project, Solid, build};
use effectcraft_raster::Image;
use effectcraft_render::{FootageSource, NoFootage};
use effectcraft_time::{FrameRate, Tick};

const W: u32 = 64;
const H: u32 = 48;
const BG: [f32; 3] = [0.1, 0.1, 0.4];
const FRAMES: u64 = 12;

/// An H.264 encoder this ffmpeg build actually ships, or `None` when it has none. Distro "free"
/// builds often omit `libx264` but ship an alternative (e.g. libopenh264), so pick the first
/// present by name rather than hard-coding one that may be absent. The oracle only needs *an*
/// H.264 encoder; the encoder identity is irrelevant to the assertion.
fn h264_encoder() -> Option<&'static str> {
    static ENC: std::sync::OnceLock<Option<&'static str>> = std::sync::OnceLock::new();
    *ENC.get_or_init(|| {
        let out = Command::new("ffmpeg").args(["-hide_banner", "-encoders"]).output().ok()?;
        if !out.status.success() {
            return None;
        }
        let text = String::from_utf8_lossy(&out.stdout);
        ["libx264", "libopenh264", "h264_nvenc", "h264_vaapi", "h264_qsv", "h264_v4l2m2m", "h264_amf"]
            .into_iter()
            .find(|name| text.lines().any(|l| l.split_whitespace().any(|tok| tok == *name)))
    })
}

fn out_dir(name: &str) -> PathBuf {
    let d = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/test-out/export").join(name);
    let _ = std::fs::remove_dir_all(&d);
    std::fs::create_dir_all(&d).expect("mkdir");
    d
}

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(1.2));
    comp.background = BG;
    let sid = p.add_item("Red", Label::Red, None, ItemKind::Solid(Solid { color: [1.0, 0.0, 0.0], width: 32, height: H, pixel_aspect: 1.0 }));
    let mut l = build::layer(&mut p, &comp, "Red", LayerSource::Solid { item: sid }, (32, H), None);
    l.in_point = comp.frame_rate.tick_of(5);
    comp.layers.push(l);
    if let Some(path) = audio {
        let f = effectcraft_media::probe(path).expect("probe wav");
        let fid = p.add_item("tone.wav", Label::SeaFoam, None, ItemKind::Footage(f));
        let l = build::layer(&mut p, &comp, "tone", LayerSource::Footage { item: fid }, (0, 0), None);
        comp.layers.push(l);
    }
    let cid = p.add_item("Comp 1", Label::Sandstone, None, ItemKind::Comp(comp.into()));
    (p, cid)
}

fn settings() -> RenderSettings {
    RenderSettings { time_span: TimeSpan::LengthOfComp, ..Default::default() }
}

/// Exports the standard test comp and checks progress for all [`FRAMES`] frames.
fn run(p: &Project, cid: ItemId, footage: &dyn FootageSource, om: &OutputModule, path: &Path) -> effectcraft_export::Report {
    run_frames(p, cid, footage, om, path, FRAMES)
}

/// [`run`] for a comp of `frames` frames.
fn run_frames(p: &Project, cid: ItemId, footage: &dyn FootageSource, om: &OutputModule, path: &Path, frames: u64) -> effectcraft_export::Report {
    let s = settings();
    let path = path.to_string_lossy().to_string();
    let job = Job {
        project: p,
        footage,
        expr: None,
        accel: None,
        comp: cid,
        settings: &s,
        output: om,
        path: &path,
        sink: None,
        options: Default::default(),
        nested_switches: true,
    };
    let mut last = Progress::default();
    let mut calls = 0;
    let r = export(&job, &mut |pr| {
        assert!(pr.done >= last.done, "progress is monotonic");
        last = *pr;
        calls += 1;
        true
    })
    .expect("export");
    assert_eq!(last.done, frames);
    assert_eq!(last.total, frames);
    assert!(calls >= 2);
    assert_eq!(r.frames, frames);
    r
}

fn px(img: &Image, x: u32, y: u32) -> [f32; 4] {
    img.data[img.idx(x, y)]
}

fn near(a: [f32; 3], b: [f32; 3], tol: f32) -> bool {
    a.iter().zip(b).all(|(x, y)| (x - y).abs() <= tol)
}

/// Check a decoded (premultiplied, opaque) frame `k`.
fn check_frame(img: &Image, k: u64, tol: f32) {
    let corner = px(img, 2, 2);
    assert!(near([corner[0], corner[1], corner[2]], BG, tol), "frame {k} corner {corner:?}");
    let c = px(img, img.width / 2, img.height / 2);
    let want = if k >= 5 { [1.0, 0.0, 0.0] } else { BG };
    assert!(near([c[0], c[1], c[2]], want, tol), "frame {k} centre {c:?} want {want:?}");
}

fn decode_movie(path: &Path, frames: u64, tol: f32) -> effectcraft_project::Footage {
    let f = effectcraft_media::probe(path).expect("probe our output");
    assert_eq!((f.width, f.height), (W, H));
    assert!(f.has_video);
    let n = f.frame_rate.frame_at(f.duration - Tick(1)) + 1;
    assert_eq!(n as u64, frames, "frame count from duration {:?} @ {:?}", f.duration, f.frame_rate);
    let pool = MediaPool::new();
    for k in 0..frames {
        let img = pool.frame_at(&f, f.frame_rate.tick_of(k as i64)).expect("decode");
        check_frame(&img, k, tol);
    }
    f
}

fn ffprobe(path: &Path) -> Option<String> {
    let out = Command::new("ffprobe")
        .args(["-v", "error", "-count_frames", "-show_entries", "stream=codec_name,width,height,nb_read_frames,pix_fmt", "-of", "compact=p=0:nk=1"])
        .arg(path)
        .output()
        .ok()?;
    if !out.status.success() {
        panic!("ffprobe rejected {}: {}", path.display(), String::from_utf8_lossy(&out.stderr));
    }
    Some(String::from_utf8_lossy(&out.stdout).to_string())
}

/// Frame `k` decoded by ffmpeg (external oracle) as RGBA, or `None` without ffmpeg.
fn ffmpeg_frame(path: &Path, k: u64) -> Option<Image> {
    let png = path.with_extension(format!("ffmpeg-{k}.png"));
    let st = Command::new("ffmpeg")
        .args(["-hide_banner", "-loglevel", "error", "-y", "-i"])
        .arg(path)
        .args(["-vf", &format!("select=eq(n\\,{k})"), "-frames:v", "1", "-pix_fmt", "rgba"])
        .arg(&png)
        .status()
        .ok()?;
    assert!(st.success(), "ffmpeg failed to decode {}", path.display());
    let img = image::open(&png).expect("ffmpeg png").to_rgba8();
    Some(Image::from_rgba8(img.width(), img.height(), img.as_raw()))
}

fn ffmpeg_check(path: &Path, tol: f32) {
    for k in [0, 4, 5, FRAMES - 1] {
        match ffmpeg_frame(path, k) {
            Some(img) => check_frame(&img, k, tol),
            None => {
                eprintln!("ffmpeg not found: skipping oracle decode");
                return;
            }
        }
    }
}

#[test]
fn h264_mp4_roundtrip() {
    let d = out_dir("h264");
    let (p, cid) = project(None);
    let om = OutputModule { bitrate_kbps: 4000, ..OutputModule::for_format(OutputFormat::H264) };
    let path = d.join("comp.mp4");
    let r = run(&p, cid, &NoFootage, &om, &path);
    assert!(r.bytes > 100 && !r.audio);
    decode_movie(&path, FRAMES, 0.08);
    ffmpeg_check(&path, 0.08);
    if let Some(s) = ffprobe(&path) {
        assert!(s.contains("h264|64|48|yuv420p"), "{s}");
        assert!(s.lines().next().unwrap_or("").ends_with("|12"), "{s}");
    }
}

#[test]
fn h264_odd_size_is_cropped_even() {
    let d = out_dir("h264-odd");
    let (p, cid) = project(None);
    let om = OutputModule::for_format(OutputFormat::H264);
    let s = RenderSettings { resolution: 31.0 / 64.0, ..settings() };
    let comp = p.comp(cid).expect("comp");
    assert_eq!(s.output_size(comp).0 % 2, 1);
    let path = d.join("odd.mp4").to_string_lossy().to_string();
    let job = Job {
        project: &p,
        footage: &NoFootage,
        expr: None,
        accel: None,
        comp: cid,
        settings: &s,
        output: &om,
        path: &path,
        sink: None,
        options: Default::default(),
        nested_switches: true,
    };
    let r = export(&job, &mut |_| true).expect("export");
    assert_eq!((r.width % 2, r.height % 2), (0, 0));
}

#[test]
fn prores_mov_roundtrip() {
    let d = out_dir("prores");
    let (p, cid) = project(None);
    let om = OutputModule::for_format(OutputFormat::ProRes);
    let path = d.join("comp.mov");
    run(&p, cid, &NoFootage, &om, &path);
    decode_movie(&path, FRAMES, 0.03);
    ffmpeg_check(&path, 0.03);
    if let Some(s) = ffprobe(&path) {
        assert!(s.starts_with("prores|64|48"), "{s}");
        assert!(s.lines().next().unwrap_or("").ends_with("|12"), "{s}");
    }
}

#[test]
fn prores_4444_with_alpha() {
    let d = out_dir("prores4444");
    let (p, cid) = project(None);
    let om = OutputModule { channels: Channels::Rgba, prores_profile: ProResProfile::Hq, ..OutputModule::for_format(OutputFormat::ProRes) };
    let path = d.join("alpha.mov");
    run(&p, cid, &NoFootage, &om, &path);
    let f = effectcraft_media::probe(&path).expect("probe");
    let img = MediaPool::new().frame_at(&f, f.frame_rate.tick_of(7)).expect("decode");
    assert!(px(&img, 2, 2)[3] < 0.02, "corner transparent: {:?}", px(&img, 2, 2));
    let c = px(&img, W / 2, H / 2);
    assert!(c[3] > 0.98 && c[0] > 0.95 && c[1] < 0.05, "centre opaque red: {c:?}");
    if let Some(s) = ffprobe(&path) {
        assert!(s.starts_with("prores|64|48|yuva444"), "{s}");
    }
}

/// Checks stored alpha run codewords in exported MOVs across both 4444 profiles,
/// alpha modes and motion-blur settings, without relying on permissive decoders.
#[test]
fn prores_alpha_final_run_is_present_in_exported_movies() {
    let dir = out_dir("prores-alpha-final-run");
    let png = dir.join("alpha.png");
    // One macroblock with 255 transparent pixels followed by one opaque pixel.
    // The last alpha difference is -1 (modulo 65536), requiring a singleton run.
    let mut pixels = image::RgbaImage::new(16, 16);
    pixels.put_pixel(15, 15, image::Rgba([255, 80, 20, 255]));
    pixels.save(&png).expect("synthetic alpha PNG");

    for profile in [ProResProfile::P4444, ProResProfile::P4444Xq] {
        for alpha_mode in [AlphaMode::Straight, AlphaMode::Premultiplied] {
            for motion_blur in [false, true] {
                let mut project = Project::default();
                let rate = FrameRate::FPS_30;
                let mut comp = Comp::new(16, 16, rate, rate.tick_of(1));
                comp.enable_motion_blur = motion_blur;
                let footage = effectcraft_media::probe(&png).expect("probe synthetic PNG");
                let item = project.add_item("Alpha", Label::SeaFoam, None, ItemKind::Footage(footage));
                let mut layer = build::layer(&mut project, &comp, "Alpha", LayerSource::Footage { item }, (16, 16), None);
                layer.switches.motion_blur = motion_blur;
                comp.layers.push(layer);
                let comp = project.add_item("Alpha", Label::Sandstone, None, ItemKind::Comp(comp.into()));
                let output = OutputModule {
                    channels: Channels::Rgba,
                    prores_profile: profile,
                    alpha_mode,
                    audio: AudioOutput::Off,
                    ..OutputModule::for_format(OutputFormat::ProRes)
                };
                let path = dir.join(format!("{profile:?}-{alpha_mode:?}-blur-{motion_blur}.mov"));
                run_frames(&project, comp, &MediaPool::new(), &output, &path, 1);

                let bytes = std::fs::read(&path).expect("exported MOV");
                let movie = craft_media::isobmff::open(bytes.as_slice()).expect("demux exported MOV");
                let video = movie.tracks.iter().find(|track| track.kind == craft_media::isobmff::TrackKind::Video).expect("video track");
                assert_eq!(video.samples.len(), 1);
                let sample = &video.samples[0];
                let frame = &bytes[sample.offset as usize..][..sample.size as usize];
                let (header, start, size) = craft_video::prores::FrameHeader::parse(frame).expect("ProRes header");
                assert_eq!((header.width, header.height), (16, 16));
                let picture = &frame[start..size];
                let header = craft_video::prores::PictureHeader::parse(picture).expect("picture header");
                assert_eq!(header.num_slices, 1);
                let slice = &picture[header.header_size + 2..];
                let color_bytes: usize = slice[2..8].as_chunks::<2>().0.iter().map(|b| u16::from_be_bytes(*b) as usize).sum();
                let alpha = &slice[(slice[0] >> 3) as usize + color_bytes..];
                // RDD 36:2022 Tables 12โ€“14: +1, run 255, -1, run 1, alignment.
                // The old encoder stopped after -1, giving [00 00 fe 01]. Its
                // own decoder and FFmpeg accept that truncated payload, so a
                // round-trip alone cannot enforce the native decoder's syntax.
                assert_eq!(alpha, [0x00, 0x00, 0xfe, 0x01, 0x80], "{profile:?}, {alpha_mode:?}, blur={motion_blur}");
            }
        }
    }
}

fn check_still_dir(dir: &Path, ext: &str, tol: f32, alpha: bool) {
    for k in 0..FRAMES {
        let path = sequence_path(&dir.join(format!("f_[#####].{ext}")).to_string_lossy(), k as i64);
        let img = image::open(&path).unwrap_or_else(|e| panic!("{path}: {e}")).to_rgba8();
        assert_eq!(img.dimensions(), (W, H));
        let img = Image::from_rgba8(W, H, img.as_raw());
        if alpha {
            let corner = px(&img, 2, 2);
            assert!(corner[3] < 0.01, "{path}: corner {corner:?}");
            let c = px(&img, W / 2, H / 2);
            assert_eq!(c[3] > 0.99, k >= 5, "{path}: centre {c:?}");
        } else {
            check_frame(&img, k, tol);
        }
    }
    assert!(!dir.join(format!("f_{:05}.{ext}", FRAMES)).exists());
}

#[test]
fn image_sequences_roundtrip() {
    let (p, cid) = project(None);
    for (fmt, ext, tol, ch) in [
        (OutputFormat::PngSequence, "png", 0.01, Channels::Rgb),
        (OutputFormat::PngSequence, "png", 0.01, Channels::Rgba),
        (OutputFormat::JpegSequence, "jpg", 0.06, Channels::Rgb),
        (OutputFormat::TiffSequence, "tif", 0.01, Channels::Rgb),
        (OutputFormat::TiffSequence, "tif", 0.01, Channels::Rgba),
        (OutputFormat::TargaSequence, "tga", 0.01, Channels::Rgb),
        (OutputFormat::TargaSequence, "tga", 0.01, Channels::Rgba),
    ] {
        let d = out_dir(&format!("seq-{ext}-{ch:?}"));
        let om = OutputModule { channels: ch, ..OutputModule::for_format(fmt) };
        let r = run(&p, cid, &NoFootage, &om, &d.join(format!("f_[#####].{ext}")));
        assert!(r.path.ends_with(&format!("f_00000.{ext}")), "{}", r.path);
        check_still_dir(&d, ext, tol, ch == Channels::Rgba);
    }
}

#[test]
fn exr_sequence_is_linear() {
    let (p, cid) = project(None);
    let d = out_dir("seq-exr");
    let om = OutputModule::for_format(OutputFormat::ExrSequence);
    run(&p, cid, &NoFootage, &om, &d.join("f_[#####].exr"));
    let img = image::open(d.join("f_00000.exr")).expect("exr").to_rgba32f();
    assert_eq!(img.dimensions(), (W, H));
    let c = img.get_pixel(2, 2).0;
    // sRGB 0.4 โ†’ linear โ‰ˆ 0.133
    assert!((c[2] - 0.1329).abs() < 0.01 && (c[3] - 1.0).abs() < 1e-6, "{c:?}");
}

/// Sequence frames are scan-line EXR, not tiled, so scan-line readers such as FilmCraft open
/// them (#2805).
#[test]
fn exr_sequence_writes_scan_lines() {
    let (p, cid) = project(None);
    let d = out_dir("seq-exr-scanline");
    let om = OutputModule::for_format(OutputFormat::ExrSequence);
    run(&p, cid, &NoFootage, &om, &d.join("f_[#####].exr"));
    let bytes = std::fs::read(d.join("f_00000.exr")).expect("exr");
    // Version field (little-endian u32 at bytes 4..8): version 2, then the flags byte. Its 0x02
    // bit is the tiled bit, 0x200 of the field, so a scan-line file has 0 there.
    assert_eq!(&bytes[4..6], &[2, 0], "{:02x?}", &bytes[..8]);
    let meta = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(&bytes), false).expect("exr header");
    assert_eq!(meta.headers[0].blocks, exr::meta::BlockDescription::ScanLines);
    assert_eq!(meta.headers[0].compression, exr::compression::Compression::ZIP16);
}

/// The RGBA samples of an EXR file, as stored (f16 widened to f32), and their sample types.
fn exr_samples(path: &Path) -> (Vec<f32>, Vec<exr::prelude::SampleType>) {
    use exr::prelude::{ReadChannels, ReadLayers};
    let image = exr::prelude::read()
        .no_deep_data()
        .largest_resolution_level()
        .rgba_channels(
            |res, _| (res.width(), vec![0f32; res.width() * res.height() * 4]),
            |(w, v): &mut (usize, Vec<f32>), p: exr::prelude::Vec2<usize>, (r, g, b, a): (f32, f32, f32, f32)| {
                let o = (p.y() * *w + p.x()) * 4;
                v[o..o + 4].copy_from_slice(&[r, g, b, a]);
            },
        )
        .first_valid_layer()
        .all_attributes()
        .from_file(path)
        .expect("read exr");
    let c = &image.layer_data.channel_data.channels;
    (image.layer_data.channel_data.pixels.1, vec![c.0.sample_type, c.1.sample_type, c.2.sample_type, c.3.as_ref().expect("alpha").sample_type])
}

/// Every OpenEXR compression at both bit depths renders through the Render Queue as scan lines
/// with that compression and sample type, and reads back through the media pool (#2911):
/// lossless 32-bit float matches the uncompressed render exactly, lossless half float stores
/// exactly the half rounding of it, and lossy methods stay within the tolerance the docs give.
#[test]
fn exr_compressions_and_bit_depths_round_trip() {
    use effectcraft_export::render_queue::{ExrBitDepth, ExrCompression};
    use exr::prelude::{SampleType, f16};
    let (p, cid) = project(None);
    let render = |c: ExrCompression, d: ExrBitDepth| {
        let dir = out_dir(&format!("seq-exr-{}-{}", c.name(), d.name()));
        let om = OutputModule { channels: Channels::Rgba, exr_compression: c, exr_bit_depth: d, ..OutputModule::for_format(OutputFormat::ExrSequence) };
        run(&p, cid, &NoFootage, &om, &dir.join("f_[#####].exr"));
        dir.join("f_00007.exr")
    };
    let pooled = |path: &Path| {
        let f = effectcraft_media::probe(path).expect("probe");
        MediaPool::new().frame_at(&f, Tick(0)).expect("decode")
    };
    let reference = render(ExrCompression::None, ExrBitDepth::Float);
    let (want, _) = exr_samples(&reference);
    let want_pooled = pooled(&reference);
    assert!(want.iter().any(|v| *v > 0.5) && want.contains(&0.0), "the frame has red, background and transparent corners");
    for d in ExrBitDepth::ALL {
        let ty = if d == ExrBitDepth::Half { SampleType::F16 } else { SampleType::F32 };
        for c in ExrCompression::ALL {
            let path = render(c, d);
            let bytes = std::fs::read(&path).expect("exr");
            assert_eq!(&bytes[4..6], &[2, 0], "{c:?} {d:?}: single-part scan lines");
            let meta = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(&bytes), false).expect("exr header");
            assert_eq!(meta.headers[0].blocks, exr::meta::BlockDescription::ScanLines, "{c:?} {d:?}");
            assert_eq!(meta.headers[0].compression.to_string(), exr_compression_name(c), "{c:?} {d:?}");
            let (got, types) = exr_samples(&path);
            assert_eq!(types, [ty; 4], "{c:?} {d:?}");
            let lossy = c.is_lossy(d);
            for (i, (g, w)) in got.iter().zip(&want).enumerate() {
                let w = if d == ExrBitDepth::Half { f16::from_f32(*w).to_f32() } else { *w };
                if lossy {
                    assert!((g - w).abs() <= 0.03 + 0.06 * w.abs(), "{c:?} {d:?} sample {i}: {g} vs {w}");
                } else {
                    assert_eq!(g.to_bits(), w.to_bits(), "{c:?} {d:?} sample {i}: {g} vs {w}");
                }
            }
            let img = pooled(&path);
            assert_eq!((img.width, img.height), (W, H));
            let tol = if lossy {
                0.06
            } else if d == ExrBitDepth::Half {
                2e-3
            } else {
                0.0
            };
            for (k, (a, b)) in img.data.iter().zip(&want_pooled.data).enumerate() {
                assert!(a.iter().zip(b).all(|(x, y)| (x - y).abs() <= tol), "{c:?} {d:?} pixel {k}: {a:?} vs {b:?}");
            }
        }
    }
}

/// The `exr` crate's name for the compression an Output Module setting writes.
fn exr_compression_name(c: effectcraft_export::render_queue::ExrCompression) -> String {
    use effectcraft_export::render_queue::ExrCompression::*;
    use exr::compression::Compression as X;
    match c {
        None => X::Uncompressed,
        Rle => X::RLE,
        Zips => X::ZIP1,
        Zip => X::ZIP16,
        Piz => X::PIZ,
        Pxr24 => X::PXR24,
        B44 => X::B44,
        B44a => X::B44A,
        Dwaa => X::DWAA(Option::None),
        Dwab => X::DWAB(Option::None),
    }
    .to_string()
}

/// A 32 bpc project whose only layer is a full-frame opaque solid of the given colour.
fn bright_project(color: [f32; 3]) -> (Project, ItemId) {
    let mut p = Project::default();
    p.settings.bit_depth = effectcraft_project::BitDepth::Bpc32;
    let comp = Comp::new(W, H, FrameRate::new(10, 1), Tick::from_seconds_f64(0.1));
    let sid = p.add_item("Bright", Label::Red, None, ItemKind::Solid(Solid { color, width: W, height: H, pixel_aspect: 1.0 }));
    let mut comp = comp;
    let l = build::layer(&mut p, &comp, "Bright", LayerSource::Solid { item: sid }, (W, H), None);
    comp.layers.push(l);
    let cid = p.add_item("Comp 1", Label::Sandstone, None, ItemKind::Comp(comp.into()));
    (p, cid)
}

/// Export one frame of a 32 bpc solid to an EXR sequence and read its centre pixel.
fn exr_centre(color: [f32; 3], channels: Channels, name: &str) -> [f32; 4] {
    let (p, cid) = bright_project(color);
    let d = out_dir(name);
    let mut om = OutputModule::for_format(OutputFormat::ExrSequence);
    om.channels = channels;
    let s = RenderSettings { time_span: TimeSpan::LengthOfComp, ..Default::default() };
    let path = d.join("f_[#####].exr").to_string_lossy().to_string();
    let job = Job {
        project: &p,
        footage: &NoFootage,
        expr: None,
        accel: None,
        comp: cid,
        settings: &s,
        output: &om,
        path: &path,
        sink: None,
        options: Default::default(),
        nested_switches: true,
    };
    export(&job, &mut |_| true).expect("export");
    let img = image::open(d.join("f_00000.exr")).expect("exr").to_rgba32f();
    img.get_pixel(W / 2, H / 2).0
}

/// The sRGB decode of the frame the writer is handed (the solid is sRGB-encoded in the frame).
fn lin(v: f32) -> f32 {
    effectcraft_color::srgb_to_linear(v)
}

#[test]
fn exr_sequence_keeps_values_above_one() {
    // Issue #339: a 32 bpc project's over-range colour must reach the EXR, in scene-linear light.
    let src = [1.5, 1.25, 0.75];
    for channels in [Channels::Rgba, Channels::Rgb] {
        let c = exr_centre(src, channels, "seq-exr-over");
        for i in 0..3 {
            assert!((c[i] - lin(src[i])).abs() < 1e-3 * lin(src[i]).max(1.0), "{channels:?} channel {i}: {c:?}");
        }
        assert!(c[0] > 1.5 && c[1] > 1.25, "{channels:?} not clamped: {c:?}");
        assert!((c[3] - 1.0).abs() < 1e-6, "{c:?}");
    }
}

#[test]
fn exr_sequence_values_at_or_below_one_are_unchanged() {
    for channels in [Channels::Rgba, Channels::Rgb] {
        let c = exr_centre([1.0, 0.4, 0.0], channels, "seq-exr-in-range");
        assert!((c[0] - 1.0).abs() < 1e-4 && (c[1] - lin(0.4)).abs() < 1e-4 && c[2].abs() < 1e-6, "{channels:?}: {c:?}");
    }
}
/// 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 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 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);
        k += 1;
    }
    assert_eq!(k, FRAMES);
    assert_eq!(total_delay, 120, "1.2 s in centiseconds");
    if let Some(s) = ffprobe(&path) {
        assert!(s.starts_with("gif|64|48"), "{s}");
    }
}

#[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);
    let d = out_dir("cancel");
    let om = OutputModule::for_format(OutputFormat::PngSequence);
    let s = settings();
    let path = d.join("c_[#####].png").to_string_lossy().to_string();
    let job = Job {
        project: &p,
        footage: &NoFootage,
        expr: None,
        accel: None,
        comp: cid,
        settings: &s,
        output: &om,
        path: &path,
        sink: None,
        options: Default::default(),
        nested_switches: true,
    };
    let r = export(&job, &mut |_| false);
    assert!(matches!(r, Err(effectcraft_export::ExportError::Cancelled)));
    let written = std::fs::read_dir(&d).map(|r| r.count()).unwrap_or(0);
    assert_eq!(written, 0, "cancelled before the first batch");
}

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
}

#[test]
fn movies_carry_audio() {
    let d = out_dir("audio");
    let wav = d.join("tone.wav");
    write_tone(&wav, 2.0);
    let (p, cid) = project(Some(&wav));
    let pool = MediaPool::new();
    for (fmt, name) in [(OutputFormat::H264, "a.mp4"), (OutputFormat::ProRes, "a.mov")] {
        let om = OutputModule { audio: AudioOutput::Auto, ..OutputModule::for_format(fmt) };
        let path = d.join(name);
        let r = run(&p, cid, &pool, &om, &path);
        assert!(r.audio, "{name}: auto audio is on for a comp with a sound layer");
        let f = effectcraft_media::probe(&path).expect("probe");
        assert!(f.has_audio, "{name}");
        let dec = MediaPool::new();
        let s = dec.audio_samples(&f, Tick::from_seconds_f64(0.3), 24_000, 48_000);
        let (l, r) = (rms(s.iter().step_by(2).copied()), rms(s.iter().skip(1).step_by(2).copied()));
        // sine of amplitude 0.5 โ†’ RMS โ‰ˆ 0.354
        assert!((l - 0.3536).abs() < 0.03, "{name}: left RMS {l}");
        assert!(r < 0.01, "{name}: right RMS {r}");
        if Command::new("ffprobe").arg("-version").output().is_ok() {
            let out = Command::new("ffprobe")
                .args(["-v", "error", "-select_streams", "a:0", "-show_entries", "stream=codec_name,channels,sample_rate", "-of", "compact=p=0:nk=1"])
                .arg(&path)
                .output()
                .expect("ffprobe");
            let s = String::from_utf8_lossy(&out.stdout);
            let want = if fmt == OutputFormat::H264 { "aac|48000|2" } else { "pcm_s16le|48000|2" };
            assert!(s.trim() == want, "{name}: {s}");
        }
        // Audio off.
        let om = OutputModule { audio: AudioOutput::Off, ..om };
        let path = d.join(format!("silent-{name}"));
        assert!(!run(&p, cid, &pool, &om, &path).audio);
        assert!(!effectcraft_media::probe(&path).expect("probe").has_audio);
    }
}

/// `src`'s audio decoded by ffmpeg as interleaved stereo `f32` at 48 kHz (external oracle).
fn ffmpeg_audio(src: &Path) -> Vec<f32> {
    let out = Command::new("ffmpeg")
        .args(["-hide_banner", "-loglevel", "error", "-i"])
        .arg(src)
        .args(["-f", "f32le", "-ac", "2", "-ar", "48000", "-"])
        .output()
        .expect("ffmpeg");
    assert!(out.status.success(), "ffmpeg failed to decode {}", src.display());
    out.stdout.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes(*b)).collect()
}

/// Movies of a 29.97 fps comp whose footage has AAC audio at 44.1 kHz sound like the footage
/// (both decoded by ffmpeg): reading footage audio at another rate ramped from a wrong position
/// at the start of every batch of frames, so exports stuttered (#274).
#[test]
fn movies_carry_resampled_footage_audio() {
    let d = out_dir("audio-44k");
    let clip = d.join("clip.mp4");
    let Some(enc) = h264_encoder() else {
        eprintln!("ffmpeg has no H.264 encoder: skipping");
        return;
    };
    let made = Command::new("ffmpeg")
        .args(["-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x48:rate=30000/1001:duration=3", "-f", "lavfi", "-i"])
        .args(["aevalsrc=0.5*sin(2*PI*440*t)|0.5*sin(2*PI*(200+400*t/3)*t):s=44100:d=3", "-c:v", enc, "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest"])
        .arg(&clip)
        .status()
        .is_ok_and(|s| s.success());
    if !made {
        eprintln!("ffmpeg could not make the H.264 fixture: skipping");
        return;
    }
    let fps = FrameRate::new(30000, 1001);
    let mut p = Project::default();
    let mut comp = Comp::new(W, H, fps, fps.tick_of(60));
    let fid = p.add_item("clip.mp4", Label::SeaFoam, None, ItemKind::Footage(effectcraft_media::probe(&clip).expect("probe clip")));
    let l = build::layer(&mut p, &comp, "clip", LayerSource::Footage { item: fid }, (64, 48), None);
    comp.layers.push(l);
    let cid = p.add_item("Comp 1", Label::Sandstone, None, ItemKind::Comp(comp.into()));
    let want = ffmpeg_audio(&clip);
    let pool = MediaPool::new();
    // AAC is lossy; PCM differs from ffmpeg's resampler by little more than 16-bit rounding.
    for (fmt, name, tol) in [(OutputFormat::H264, "a.mp4", 0.06), (OutputFormat::ProRes, "a.mov", 0.01)] {
        let path = d.join(name);
        assert!(run_frames(&p, cid, &pool, &OutputModule::for_format(fmt), &path, 60).audio);
        let got = ffmpeg_audio(&path);
        // 60 frames at 29.97 fps: 96 096 samples at 48 kHz.
        assert!((got.len() as i64 / 2 - 96_096).abs() < 1024, "{name}: {} samples", got.len() / 2);
        // (Up to the last AAC frames, where the cut to silence smears back into the sound.)
        let worst = got.iter().zip(&want).take(94_000 * 2).map(|(g, w)| (g - w).abs()).fold(0.0f32, f32::max);
        eprintln!("{name}: max error {worst:.4}");
        assert!(worst < tol, "{name}: max error {worst} against the footage");
    }
}

#[test]
fn webm_vp9_opus_roundtrip() {
    let d = out_dir("webm");
    let wav = d.join("tone.wav");
    write_tone(&wav, 2.0);
    let (p, cid) = project(Some(&wav));
    let pool = MediaPool::new();
    let om = OutputModule { quality: 90, audio: AudioOutput::Auto, ..OutputModule::for_format(OutputFormat::WebM) };
    let path = d.join("comp.webm");
    let r = run(&p, cid, &pool, &om, &path);
    assert!(r.audio && r.bytes > 200, "{r:?}");
    // Decoded back through FilmCraft (Matroska demuxer, VP9 and Opus decoders).
    let f = decode_movie(&path, FRAMES, 0.08);
    assert!(f.has_audio);
    let s = MediaPool::new().audio_samples(&f, Tick::from_seconds_f64(0.3), 24_000, 48_000);
    let l = rms(s.iter().step_by(2).copied());
    assert!((l - 0.3536).abs() < 0.06, "left RMS {l}");
    ffmpeg_check(&path, 0.08);
    if let Some(s) = ffprobe(&path) {
        assert!(s.contains("vp9|64|48|yuv420p"), "{s}");
        assert!(s.contains("opus"), "{s}");
    }
}

#[test]
fn webm_with_alpha() {
    let d = out_dir("webm-alpha");
    let (p, cid) = project(None);
    // Transparent outside the red solid.
    let om = OutputModule { channels: Channels::Rgba, ..OutputModule::for_format(OutputFormat::WebM) };
    let path = d.join("alpha.webm");
    run(&p, cid, &NoFootage, &om, &path);
    let f = effectcraft_media::probe(&path).expect("probe our output");
    assert_eq!((f.width, f.height), (W, H));
    let bytes = std::fs::read(&path).unwrap();
    // AlphaMode element (0x53C0) and BlockAdditions (0x75A1).
    assert!(bytes.windows(3).any(|w| w == [0x53, 0xC0, 0x81]), "AlphaMode 1");
    assert!(bytes.windows(2).any(|w| w == [0x75, 0xA1]), "BlockAdditions");
    // ffmpeg's libvpx decoder reads the alpha channel.
    let png = path.with_extension("alpha.png");
    let ok = Command::new("ffmpeg")
        .args(["-hide_banner", "-loglevel", "error", "-y", "-c:v", "libvpx-vp9", "-i"])
        .arg(&path)
        .args(["-vf", "select=eq(n\\,8)", "-frames:v", "1", "-pix_fmt", "rgba"])
        .arg(&png)
        .status()
        .map(|s| s.success())
        .unwrap_or(false);
    if ok {
        let img = image::open(&png).unwrap().to_rgba8();
        assert!(img.get_pixel(2, 2)[3] < 20, "corner transparent");
        assert!(img.get_pixel(W / 2, H / 2)[3] > 235, "centre opaque");
    } else {
        eprintln!("ffmpeg with libvpx not available: skipping alpha oracle");
    }
}

#[test]
fn wav_and_aiff_audio_only() {
    let d = out_dir("audio-only");
    let wav = d.join("tone.wav");
    write_tone(&wav, 2.0);
    let (p, cid) = project(Some(&wav));
    let pool = MediaPool::new();
    for (fmt, name, magic) in [(OutputFormat::Wav, "out.wav", &b"RIFF"[..]), (OutputFormat::Aiff, "out.aif", &b"FORM"[..])] {
        let om = OutputModule::for_format(fmt);
        let path = d.join(name);
        let r = run(&p, cid, &pool, &om, &path);
        assert!(r.audio);
        let bytes = std::fs::read(&path).unwrap();
        assert_eq!(&bytes[..4], magic);
        // 1.2 s of 48 kHz 16-bit stereo.
        assert!((bytes.len() as i64 - 57_600 * 4).abs() < 200, "{name}: {}", bytes.len());
        let l = if fmt == OutputFormat::Wav {
            // Decoded by FilmCraft.
            let f = effectcraft_media::probe(&path).expect("probe audio");
            assert!(f.has_audio && !f.has_video, "{name}");
            let s = MediaPool::new().audio_samples(&f, Tick::from_seconds_f64(0.3), 24_000, 48_000);
            rms(s.iter().step_by(2).copied())
        } else {
            // AIFF: big-endian samples after the SSND chunk's offset/block size.
            let at = bytes.windows(4).position(|w| w == b"SSND").expect("SSND") + 16;
            let left = bytes[at..].as_chunks::<4>().0.iter().skip(14_400).take(24_000).map(|c| i16::from_be_bytes([c[0], c[1]]) as f32 / 32767.0);
            rms(left)
        };
        assert!((l - 0.3536).abs() < 0.01, "{name}: left RMS {l}");
        if let Some(s) = ffprobe(&path) {
            assert!(s.contains("pcm_s16"), "{s}");
        }
    }
}

/// An AIFF EffectCraft renders imports back (#1428): plain 16-bit and AIFF-C `fl32`.
#[test]
fn rendered_aiff_imports_back() {
    use effectcraft_export::render_queue::AudioFormat;
    let d = out_dir("aiff-import");
    let wav = d.join("tone.wav");
    write_tone(&wav, 2.0);
    let (p, cid) = project(Some(&wav));
    let pool = MediaPool::new();
    for (format, name) in [(AudioFormat::S16, "s16.aif"), (AudioFormat::F32, "f32.aif")] {
        // 44.1 kHz, not the 48 kHz the samples are read back at: a wrong rate shows in the duration.
        let om = OutputModule { audio_sample_rate: 44_100, audio_channels: 2, audio_format: format, ..OutputModule::for_format(OutputFormat::Aiff) };
        let path = d.join(name);
        assert!(run(&p, cid, &pool, &om, &path).audio);
        let f = effectcraft_media::probe(&path).unwrap_or_else(|e| panic!("{name}: {e}"));
        assert!(f.has_audio && !f.has_video, "{name}");
        assert_eq!(f.duration, Tick::from_seconds_f64(1.2), "{name}: 52 920 frames at 44.1 kHz");
        // Tone on the left, silence on the right: the two channels are read as two.
        let s = MediaPool::new().audio_samples(&f, Tick::from_seconds_f64(0.3), 24_000, 48_000);
        let (l, r) = (rms(s.iter().step_by(2).copied()), rms(s.iter().skip(1).step_by(2).copied()));
        assert!((l - 0.3536).abs() < 0.01, "{name}: left RMS {l}");
        assert!(r < 0.01, "{name}: right RMS {r}");
    }
}