mirror of
https://github.com/storytold/filmcraft.git
synced 2026-10-24 01:58:19 -06:00
* 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
155 lines
6.2 KiB
Rust
155 lines
6.2 KiB
Rust
//! Development tool: encode synthetic (or lavfi) content and verify against ffmpeg.
|
|
//!
|
|
//! `cargo run --release -p filmcraft-h264enc --example h264enc_synth -- W H FRAMES [profile] [preset] [bframes] [rate] [slices] [src]`
|
|
//! profile: baseline|main|high, preset: speed|balanced|quality, rate: qpN | crfN | cbrN | vbrN
|
|
#[path = "../tests/it/common/mod.rs"]
|
|
mod common;
|
|
|
|
use filmcraft_h264enc::*;
|
|
use std::time::Instant;
|
|
|
|
fn main() {
|
|
let a: Vec<String> = std::env::args().collect();
|
|
let arg = |i: usize, d: &str| a.get(i).cloned().unwrap_or_else(|| d.to_string());
|
|
let w: usize = arg(1, "320").parse().unwrap();
|
|
let h: usize = arg(2, "240").parse().unwrap();
|
|
let n: usize = arg(3, "10").parse().unwrap();
|
|
let mut cfg = EncoderConfig::new(w as u32, h as u32, 30, 1);
|
|
cfg.profile = match arg(4, "high").as_str() {
|
|
"baseline" => Profile::Baseline,
|
|
"main" => Profile::Main,
|
|
_ => Profile::High,
|
|
};
|
|
cfg.preset = match arg(5, "balanced").as_str() {
|
|
"speed" => Preset::Speed,
|
|
"quality" => Preset::Quality,
|
|
_ => Preset::Balanced,
|
|
};
|
|
cfg.bframes = arg(6, "0").parse().unwrap();
|
|
let rate = arg(7, "qp26");
|
|
cfg.rate = if let Some(q) = rate.strip_prefix("qp") {
|
|
RateControl::Qp(q.parse().unwrap())
|
|
} else if let Some(q) = rate.strip_prefix("crf") {
|
|
RateControl::Crf(q.parse().unwrap())
|
|
} else if let Some(q) = rate.strip_prefix("cbr") {
|
|
RateControl::Cbr { kbps: q.parse().unwrap() }
|
|
} else {
|
|
let k: u32 = rate.strip_prefix("vbr").unwrap().parse().unwrap();
|
|
RateControl::Vbr { target_kbps: k, max_kbps: k * 2 }
|
|
};
|
|
cfg.slices = arg(8, "0").parse().unwrap();
|
|
let src_kind = arg(9, "synth");
|
|
if let Ok(t) = std::env::var("THREADS") {
|
|
cfg.threads = t.parse().unwrap();
|
|
}
|
|
if let Ok(k) = std::env::var("KEYINT") {
|
|
cfg.keyint = k.parse().unwrap();
|
|
}
|
|
if let Ok(k) = std::env::var("AQ") {
|
|
cfg.aq_strength = k.parse().unwrap();
|
|
}
|
|
// PERF=k: generate only k distinct pictures and cycle them (for long speed runs without verification).
|
|
let distinct = std::env::var("PERF").ok().map_or(n, |k| k.parse::<usize>().unwrap().min(n));
|
|
let base: Vec<common::Yuv> =
|
|
if src_kind == "synth" { (0..distinct).map(|t| common::synth(w, h, t)).collect() } else { common::ffmpeg_source(&src_kind, w, h, distinct) };
|
|
let perf = distinct < n;
|
|
let frames: Vec<&common::Yuv> = (0..n).map(|i| &base[i % distinct]).collect();
|
|
let dir = common::out_dir("dev");
|
|
let mut enc = Encoder::new(cfg).unwrap();
|
|
enc.set_recon_capture(true);
|
|
let mut out = Vec::new();
|
|
let mut pk = Vec::new();
|
|
let t0 = Instant::now();
|
|
if perf {
|
|
enc.set_recon_capture(false);
|
|
}
|
|
for (i, f) in frames.iter().enumerate() {
|
|
pk.extend(enc.encode(&f.frame(), i as i64).unwrap());
|
|
}
|
|
pk.extend(enc.flush());
|
|
let el = t0.elapsed().as_secs_f64();
|
|
for p in &pk {
|
|
out.extend_from_slice(&p.data);
|
|
}
|
|
let file = dir.join("out.h264");
|
|
std::fs::write(&file, &out).unwrap();
|
|
let mut recon = enc.take_recon();
|
|
recon.sort_by_key(|r| r.pts);
|
|
let kbps = out.len() as f64 * 8.0 / (n as f64 / 30.0) / 1000.0;
|
|
println!("encoded {n} frames in {el:.3}s = {:.1} fps, {} bytes, {kbps:.1} kbps", n as f64 / el, out.len());
|
|
for p in pk.iter().take(12) {
|
|
print!("{:?}(pts{} dts{} qp{} {}B) ", p.frame_type, p.pts, p.dts, p.qp, p.data.len());
|
|
}
|
|
println!();
|
|
if common::ffmpeg().is_none() || perf {
|
|
return;
|
|
}
|
|
let (err, dec) = common::ffmpeg_decode(&file);
|
|
if !err.is_empty() {
|
|
println!("ffmpeg stderr: {err}");
|
|
}
|
|
let fs = w * h * 3 / 2;
|
|
println!("ffmpeg decoded {} frames (expected {n})", dec.len() / fs);
|
|
let mut src_all = Vec::new();
|
|
let mut psnr_sum = 0.0;
|
|
for (i, r) in recon.iter().enumerate() {
|
|
let mut rb = r.y.clone();
|
|
rb.extend_from_slice(&r.u);
|
|
rb.extend_from_slice(&r.v);
|
|
let s = frames[i].bytes();
|
|
src_all.extend_from_slice(&s);
|
|
let p = common::psnr(&frames[i].y, &r.y);
|
|
psnr_sum += p;
|
|
if dec.len() >= (i + 1) * fs {
|
|
let d = &dec[i * fs..(i + 1) * fs];
|
|
if d != rb.as_slice() {
|
|
let pos = d.iter().zip(&rb).position(|(a, b)| a != b).unwrap();
|
|
let (plane, off) = if pos < w * h {
|
|
("Y", pos)
|
|
} else if pos < w * h * 5 / 4 {
|
|
("U", pos - w * h)
|
|
} else {
|
|
("V", pos - w * h * 5 / 4)
|
|
};
|
|
let pw = if plane == "Y" { w } else { w / 2 };
|
|
let (x, y) = (off % pw, off / pw);
|
|
let mbsz = if plane == "Y" { 16 } else { 8 };
|
|
let nd = d.iter().zip(&rb).filter(|(a, b)| a != b).count();
|
|
println!(
|
|
"frame {i} (pts {}): MISMATCH first at {plane} ({x},{y}) mb ({},{}) dec={} rec={} ({nd} samples differ)",
|
|
r.pts,
|
|
x / mbsz,
|
|
y / mbsz,
|
|
d[pos],
|
|
rb[pos]
|
|
);
|
|
if std::env::var("MAP").is_ok() {
|
|
let (mbw, mbh) = (w.div_ceil(16), h.div_ceil(16));
|
|
for my in 0..mbh {
|
|
let mut line = String::new();
|
|
for mx in 0..mbw {
|
|
let mut n = 0;
|
|
for yy in my * 16..(my * 16 + 13).min(h) {
|
|
for xx in mx * 16..(mx * 16 + 13).min(w) {
|
|
n += (d[yy * w + xx] != rb[yy * w + xx]) as usize;
|
|
}
|
|
}
|
|
line.push(if n == 0 {
|
|
'.'
|
|
} else if n < 16 {
|
|
'e'
|
|
} else {
|
|
'X'
|
|
});
|
|
}
|
|
println!(" {line}");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
println!("mean luma PSNR vs source: {:.2} dB", psnr_sum / recon.len() as f64);
|
|
std::fs::write(dir.join("src.yuv"), &src_all).unwrap();
|
|
}
|