mirror of
https://github.com/storytold/filmcraft.git
synced 2026-10-24 10:10:05 -06:00
CI: xtask ci in the dev profile, one test binary per crate, nextest (#2860)
* Tests: one integration-test binary per crate Each crate with several tests/*.rs files keeps them as modules of tests/it/, so cargo links one test executable per crate instead of one per file. Files that change process-wide state or time a workload stay binaries of their own. * Profiles: line tables for workspace crates, no debug info for dependencies * Windows ARM64: nightly and on release pushes instead of every push to main * xtask ci: clippy and tests in the dev profile The release profile's thin LTO and single codegen unit made linking the test binaries take most of the gate. The codec and audio crates stay at opt-level 3 in the dev profile, so their tests run at release speed. * xtask ci: nextest, doctests, no examples, one wasm check Tests run with cargo nextest when it is installed (cargo test otherwise) and build with --lib --bins --tests, so examples aren't linked; doctests run separately. The wasm check keeps going past the first failing crate. VP8 fixture generation locks across processes, since nextest runs each test in its own. * VP9 synthetic fixtures: write aside and rename A test in another process could read a fixture ffmpeg was still writing. * Fuzz tests: a third of each sweep by default, all of it with FILMCRAFT_FUZZ_ITERS Every seed, mutation kind and thread count still runs. Header bytes each get one overwrite value and two bit flips in rotation, so every value and bit index still occurs across the region. FILMCRAFT_FUZZ_ITERS=N runs the whole sweep plus N random rounds per seed, as before. * Platform fallback tests: one failure point per hybrid path The failure-anywhere test injected a failure at four points around every restart point; the hybrid's paths differ only in the first GOP, around the second and last restart points, at the end and never. The replay test needs the first two GOPs. * Export tests: shorter ranges with the same checks The bitrate test measures 0.5–1.5 s, where each preset reaches its own target (on 0–3 s the simple demo left two presets at the same rate). The every-preset test exports three frames per preset. * Audio tests: effects in parallel, a table-driven FFT for the Opus quality metric The extreme-settings test keeps both settings and the 40 s tail for every effect, one thread each. The Opus quality metric's FFT builds its twiddles once and allocates nothing per transform (same algorithm and summation order). * CI: dev-profile gate with nextest xtask ci no longer builds with --release, so the release-profile overrides go; nextest is installed for the test step. * CI: apt packages through the cached install-apt-packages action * Review fixes: fuzz tests with a panic hook in their own binaries, loud fixture lock The fuzz tests of av1, h264, hevc, mpeg2v, vp8 and vp9 count panics through a process-wide hook, so a panic in another test of the same process would fail them; they are binaries of their own again. The VP8 fixture lock fails the test instead of skipping it silently, and the full-sweep doc comments say how many truncation points the full run samples. * Fuzz tests: rotate the default subset per run (FILMCRAFT_FUZZ_SEED replays one) * platform tests: decode-switch tests in their own binaries, one NVENC lock
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
//! Decoder accuracy against ffmpeg's decoder on streams produced by ffmpeg's AAC encoder (oracle
|
||||
//! only; skipped when ffmpeg is absent).
|
||||
|
||||
use crate::common;
|
||||
|
||||
use common::*;
|
||||
|
||||
struct Case {
|
||||
name: &'static str,
|
||||
rate: u32,
|
||||
channels: usize,
|
||||
args: &'static [&'static str],
|
||||
}
|
||||
|
||||
fn input(rate: u32, channels: usize) -> Vec<Vec<f32>> {
|
||||
let n = rate as usize * 3 / 2;
|
||||
(0..channels)
|
||||
.map(|c| {
|
||||
let a = chords(rate, n, 0.003 * c as f32);
|
||||
let b = clicks(rate, n);
|
||||
let s = speech(rate, n, 5 + c as u64);
|
||||
a.iter().zip(&b).zip(&s).map(|((x, y), z)| 0.6 * x + 0.3 * y + 0.3 * z * if c % 2 == 0 { 1.0 } else { -0.5 }).collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Returns (max abs error, samples compared).
|
||||
fn compare(case: &Case) -> Option<(f32, usize)> {
|
||||
let ff = ffmpeg()?;
|
||||
let path = fixtures().join(format!("ffenc_{}.aac", case.name));
|
||||
ffmpeg_encode(&ff, &input(case.rate, case.channels), case.rate, case.args, &path);
|
||||
let data = std::fs::read(&path).unwrap();
|
||||
let (ref_pcm, err) = ffmpeg_decode(&ff, &path, case.channels);
|
||||
assert!(err.trim().is_empty(), "{err}");
|
||||
let ours = our_decode(&data);
|
||||
assert_eq!(ours.len(), case.channels);
|
||||
// ffmpeg reorders channels to its native layouts; match each of our (AAC element order)
|
||||
// channels to the closest ffmpeg channel and require a permutation.
|
||||
let mut worst = 0f32;
|
||||
let mut taken = vec![false; case.channels];
|
||||
for c in 0..case.channels {
|
||||
let (best, err) =
|
||||
(0..case.channels).filter(|&j| !taken[j]).map(|j| (j, max_abs_diff(&ours[c], &ref_pcm[j]))).min_by(|a, b| a.1.total_cmp(&b.1)).unwrap();
|
||||
taken[best] = true;
|
||||
assert_eq!(ours[c].len(), ref_pcm[best].len(), "{}: length", case.name);
|
||||
worst = worst.max(err);
|
||||
}
|
||||
Some((worst, ours[0].len()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matches_ffmpeg_decoder() {
|
||||
if ffmpeg().is_none() {
|
||||
eprintln!("ffmpeg not found; skipping");
|
||||
return;
|
||||
}
|
||||
let cases = [
|
||||
Case { name: "stereo_44k_128k", rate: 44100, channels: 2, args: &["-b:a", "128k", "-aac_pns", "0"] },
|
||||
Case { name: "stereo_48k_256k", rate: 48000, channels: 2, args: &["-b:a", "256k", "-aac_pns", "0"] },
|
||||
Case { name: "mono_48k_64k", rate: 48000, channels: 1, args: &["-b:a", "64k", "-aac_pns", "0"] },
|
||||
Case {
|
||||
name: "stereo_44k_96k_is_ms_tns",
|
||||
rate: 44100,
|
||||
channels: 2,
|
||||
args: &["-b:a", "96k", "-aac_pns", "0", "-aac_is", "1", "-aac_ms", "1", "-aac_tns", "1"],
|
||||
},
|
||||
Case { name: "stereo_32k_48k_is", rate: 32000, channels: 2, args: &["-b:a", "48k", "-aac_pns", "0", "-aac_is", "1"] },
|
||||
Case { name: "mono_22k_32k", rate: 22050, channels: 1, args: &["-b:a", "32k", "-aac_pns", "0"] },
|
||||
Case { name: "mono_16k_24k", rate: 16000, channels: 1, args: &["-b:a", "24k", "-aac_pns", "0"] },
|
||||
Case { name: "mono_8k_16k", rate: 8000, channels: 1, args: &["-b:a", "16k", "-aac_pns", "0"] },
|
||||
Case { name: "stereo_96k_256k", rate: 96000, channels: 2, args: &["-b:a", "256k", "-aac_pns", "0"] },
|
||||
Case { name: "c3_48k", rate: 48000, channels: 3, args: &["-b:a", "192k", "-aac_pns", "0"] },
|
||||
Case { name: "c51_48k_320k", rate: 48000, channels: 6, args: &["-b:a", "320k", "-aac_pns", "0"] },
|
||||
Case { name: "c71_48k_448k", rate: 48000, channels: 8, args: &["-b:a", "448k", "-aac_pns", "0"] },
|
||||
Case { name: "stereo_44k_fast_coder", rate: 44100, channels: 2, args: &["-b:a", "160k", "-aac_coder", "fast", "-aac_pns", "0"] },
|
||||
];
|
||||
println!("{:<28} {:>10} {:>12}", "stream", "samples/ch", "max |err|");
|
||||
let mut fails = Vec::new();
|
||||
for c in &cases {
|
||||
let (err, n) = compare(c).unwrap();
|
||||
println!("{:<28} {:>10} {:>12.3e}", c.name, n, err);
|
||||
if err >= 1e-3 {
|
||||
fails.push((c.name, err));
|
||||
}
|
||||
}
|
||||
assert!(fails.is_empty(), "decoder mismatches: {fails:?}");
|
||||
}
|
||||
|
||||
/// PNS output is random by design, and ffmpeg's encoder does not emit it: build noise-substituted
|
||||
/// access units by hand (mono, and a common-window pair with M/S = correlated noise) and compare
|
||||
/// per-frame energies and the L/R correlation with ffmpeg's decode.
|
||||
#[test]
|
||||
fn pns_matches_ffmpeg() {
|
||||
use filmcraft_aac::AdtsHeader;
|
||||
use filmcraft_bitstream::BitWriter;
|
||||
let Some(ff) = ffmpeg() else { return };
|
||||
fn ics(bw: &mut BitWriter, with_info: bool, nrg: u32) {
|
||||
bw.write_bits(100, 8); // global_gain
|
||||
if with_info {
|
||||
bw.write_bits(0, 4); // reserved, ONLY_LONG, sine
|
||||
bw.write_bits(40, 6);
|
||||
bw.write_bits(0, 1);
|
||||
}
|
||||
bw.write_bits(13, 4); // NOISE_HCB for all 40 bands
|
||||
bw.write_bits(31, 5);
|
||||
bw.write_bits(9, 5);
|
||||
bw.write_bits(nrg, 9); // first noise energy (PCM)
|
||||
for _ in 1..40 {
|
||||
bw.write_bits(0, 1); // difference 0
|
||||
}
|
||||
bw.write_bits(0, 3); // pulse, tns, gain control
|
||||
}
|
||||
for channels in [1usize, 2] {
|
||||
let mut stream = Vec::new();
|
||||
for f in 0..12u32 {
|
||||
let mut bw = BitWriter::new();
|
||||
let nrg = 256 + 20 + (f % 6) * 7;
|
||||
if channels == 1 {
|
||||
bw.write_bits(0, 3);
|
||||
bw.write_bits(0, 4);
|
||||
ics(&mut bw, true, nrg);
|
||||
} else {
|
||||
bw.write_bits(1, 3);
|
||||
bw.write_bits(0, 4);
|
||||
bw.write_bits(1, 1); // common_window
|
||||
bw.write_bits(0, 4);
|
||||
bw.write_bits(40, 6);
|
||||
bw.write_bits(0, 1);
|
||||
bw.write_bits(2, 2); // ms_mask_present = all
|
||||
ics(&mut bw, false, nrg);
|
||||
ics(&mut bw, false, nrg - 8);
|
||||
}
|
||||
bw.write_bits(7, 3);
|
||||
let au = bw.finish();
|
||||
stream.extend_from_slice(&AdtsHeader::write(2, 4, channels as u8, au.len(), 0x7FF));
|
||||
stream.extend_from_slice(&au);
|
||||
}
|
||||
let path = fixtures().join(format!("handmade_pns_{channels}.aac"));
|
||||
std::fs::write(&path, &stream).unwrap();
|
||||
let (ref_pcm, err) = ffmpeg_decode(&ff, &path, channels);
|
||||
assert!(err.trim().is_empty(), "{err}");
|
||||
let ours = our_decode(&stream);
|
||||
let mut worst_db = 0f64;
|
||||
for c in 0..channels {
|
||||
let e = |v: &[f32]| 10.0 * v.iter().map(|x| (*x as f64).powi(2)).sum::<f64>().log10();
|
||||
let (eo, ef) = (e(&ours[c]), e(&ref_pcm[c]));
|
||||
println!("PNS {channels}ch ch{c}: total energy ours {eo:.2} dB, ffmpeg {ef:.2} dB");
|
||||
assert!((eo - ef).abs() < 0.5);
|
||||
assert_eq!(ours[c].len(), ref_pcm[c].len());
|
||||
for s in (1024..ours[c].len() - 1024).step_by(1024) {
|
||||
let ea: f64 = ours[c][s..s + 1024].iter().map(|v| (*v as f64).powi(2)).sum();
|
||||
let eb: f64 = ref_pcm[c][s..s + 1024].iter().map(|v| (*v as f64).powi(2)).sum();
|
||||
worst_db = worst_db.max((10.0 * (ea / eb).log10()).abs());
|
||||
}
|
||||
}
|
||||
let corr = |a: &[f32], b: &[f32]| {
|
||||
let (mut ab, mut aa, mut bb) = (0f64, 0f64, 0f64);
|
||||
for (x, y) in a.iter().zip(b) {
|
||||
ab += *x as f64 * *y as f64;
|
||||
aa += (*x as f64).powi(2);
|
||||
bb += (*y as f64).powi(2);
|
||||
}
|
||||
ab / (aa * bb).sqrt()
|
||||
};
|
||||
let (c_ours, c_ff) = if channels == 2 { (corr(&ours[0], &ours[1]), corr(&ref_pcm[0], &ref_pcm[1])) } else { (0.0, 0.0) };
|
||||
println!("PNS {channels}ch: per-frame energy max deviation {worst_db:.2} dB; L/R correlation ours {c_ours:.3} ffmpeg {c_ff:.3}");
|
||||
if channels == 1 {
|
||||
assert!(worst_db < 1.0);
|
||||
} else {
|
||||
// ISO/IEC 14496-3 §4.6.13: noise bands with ms_used share one random vector (correlated
|
||||
// noise). ffmpeg does not implement this and generates independent noise.
|
||||
assert!(c_ours > 0.99);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user