Files
filmcraft/crates/h264enc/tests/it/common/mod.rs
T
aiongg 0a881266c8 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
2026-10-23 13:34:16 -04:00

166 lines
6.9 KiB
Rust

//! Shared helpers for the encoder tests: a synthetic content generator and ffmpeg/ffprobe oracle wrappers.
#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::process::Command;
pub struct Yuv {
pub w: usize,
pub h: usize,
pub y: Vec<u8>,
pub u: Vec<u8>,
pub v: Vec<u8>,
}
impl Yuv {
pub fn frame(&self) -> filmcraft_h264enc::YuvFrame<'_> {
filmcraft_h264enc::YuvFrame { y: &self.y, u: &self.u, v: &self.v, y_stride: self.w, uv_stride: self.w / 2 }
}
pub fn bytes(&self) -> Vec<u8> {
let mut v = self.y.clone();
v.extend_from_slice(&self.u);
v.extend_from_slice(&self.v);
v
}
}
fn hash(mut x: u32) -> u32 {
x ^= x >> 16;
x = x.wrapping_mul(0x7feb352d);
x ^= x >> 15;
x = x.wrapping_mul(0x846ca68b);
x ^= x >> 16;
x
}
/// Synthetic test picture `t`: four regions with a moving gradient, film-grain-like noise,
/// text-like hard edges and a sub-pixel panning texture.
pub fn synth(w: usize, h: usize, t: usize) -> Yuv {
let mut y = vec![0u8; w * h];
let mut u = vec![0u8; w * h / 4];
let mut v = vec![0u8; w * h / 4];
let tf = t as f32;
for yy in 0..h {
for xx in 0..w {
let region = (xx * 2 / w) + 2 * (yy * 2 / h);
let val: f32 = match region {
0 => {
// moving smooth gradient
let fx = xx as f32 / w as f32;
let fy = yy as f32 / h as f32;
128.0 + 90.0 * ((fx * 6.0 + tf * 0.07).sin() * (fy * 4.0 - tf * 0.05).cos())
}
1 => {
// mild noise over a slow ramp (like film grain)
let n = (hash((xx as u32).wrapping_mul(7919) ^ (yy as u32).wrapping_mul(104729) ^ (t as u32).wrapping_mul(1_000_003)) & 31) as f32;
60.0 + (xx as f32 * 0.1 + yy as f32 * 0.05) % 120.0 + n
}
2 => {
// text-like strokes: glyph grid scrolling slowly upwards
let sy = yy + t;
let gx = xx / 12;
let gy = sy / 16;
let lx = xx % 12;
let ly = sy % 16;
let bits = hash((gx as u32) * 31 + gy as u32 * 977);
let inr = |v: usize, a: usize, b: usize| (a..=b).contains(&v);
let bar = inr(lx, 2, 9);
let stroke = (inr(lx, 2, 3) && bits & 1 != 0)
|| (inr(lx, 8, 9) && bits & 2 != 0)
|| (inr(ly, 3, 4) && bar && bits & 4 != 0)
|| (inr(ly, 8, 9) && bar && bits & 8 != 0)
|| (inr(ly, 12, 13) && bar && bits & 16 != 0);
if ly < 14 && lx < 11 && stroke { 235.0 } else { 20.0 }
}
_ => {
// panning texture with sub-pixel motion
let px = xx as f32 + tf * 1.75;
let py = yy as f32 + tf * 0.5;
let c = ((px / 9.0).sin() + (py / 7.0).cos() + ((px + py) / 23.0).sin()) * 40.0;
let checker = if ((px / 32.0).floor() as i64 + (py / 32.0).floor() as i64) % 2 == 0 { 30.0 } else { -30.0 };
128.0 + c + checker
}
};
y[yy * w + xx] = val.clamp(0.0, 255.0) as u8;
}
}
for yy in 0..h / 2 {
for xx in 0..w / 2 {
let fx = xx as f32 / (w / 2) as f32;
let fy = yy as f32 / (h / 2) as f32;
u[yy * w / 2 + xx] = (128.0 + 60.0 * (fx * 3.0 + tf * 0.03).sin()) as u8;
v[yy * w / 2 + xx] = (128.0 + 50.0 * (fy * 5.0 - tf * 0.04).cos() * fx) as u8;
}
}
Yuv { w, h, y, u, v }
}
/// ffmpeg (see `filmcraft_testkit::oracle`); `None` after printing `SKIPPED`.
pub fn ffmpeg() -> Option<PathBuf> {
filmcraft_testkit::ffmpeg_or_skip("h264enc oracle")
}
/// ffprobe (see `filmcraft_testkit::oracle`); `None` after printing `SKIPPED`.
pub fn ffprobe() -> Option<PathBuf> {
filmcraft_testkit::ffprobe_or_skip("h264enc oracle")
}
/// Per-test output directory under `<workspace>/target/fixtures/h264enc/`.
pub fn out_dir(name: &str) -> PathBuf {
filmcraft_testkit::fixtures_dir(&format!("h264enc/{name}"))
}
/// Strict decode of an Annex-B file with ffmpeg; returns (diagnostics, raw yuv420p bytes). Any diagnostic
/// text is a failure for the caller.
///
/// - `-v warning`: ffmpeg reports a picture with a damaged, missing or concealed slice as
/// `corrupt decoded frame`, which is a warning. At `-v error` a stream with a whole slice missing
/// decodes silently (#74).
/// - Error concealment stays at ffmpeg's default. With `-ec 0`, ffmpeg (7.1, 8.1) flags every picture
/// that has more than one slice as corrupt, libx264's multi-slice streams included, so that mode
/// can't tell a real defect from a clean multi-slice picture. Concealment can't hide a defect here:
/// a concealed picture is still reported, and the caller compares every decoded pixel with the
/// encoder's reconstruction.
/// - `-err_detect`: also report the non-conformances ffmpeg tolerates by default.
pub fn ffmpeg_decode(file: &Path) -> (String, Vec<u8>) {
let out = Command::new(ffmpeg().unwrap())
.args(["-nostdin", "-hide_banner", "-v", "warning", "-err_detect", "+crccheck+bitstream+buffer+explode", "-i"])
.arg(file)
.args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"])
.output()
.expect("run ffmpeg");
let mut diag = String::from_utf8_lossy(&out.stderr).into_owned();
if !out.status.success() {
diag.push_str(&format!("ffmpeg exited with {}", out.status));
}
(diag, out.stdout)
}
/// Annex-B NAL units of `stream` as byte ranges (start code included).
pub fn annexb_nals(stream: &[u8]) -> Vec<std::ops::Range<usize>> {
let starts: Vec<usize> = (0..stream.len().saturating_sub(3)).filter(|&i| stream[i..i + 3] == [0, 0, 1]).collect();
starts.iter().enumerate().map(|(k, &s)| s..starts.get(k + 1).copied().unwrap_or(stream.len())).collect()
}
/// Raw frames produced by ffmpeg from a lavfi source.
pub fn ffmpeg_source(src: &str, w: usize, h: usize, frames: usize) -> Vec<Yuv> {
let out = Command::new(ffmpeg().unwrap())
.args(["-v", "error", "-f", "lavfi", "-i"])
.arg(format!("{src}=size={w}x{h}:rate=30"))
.args(["-frames:v", &frames.to_string(), "-f", "rawvideo", "-pix_fmt", "yuv420p", "-"])
.output()
.expect("run ffmpeg");
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
let fs = w * h * 3 / 2;
out.stdout.chunks_exact(fs).map(|c| Yuv { w, h, y: c[..w * h].to_vec(), u: c[w * h..w * h * 5 / 4].to_vec(), v: c[w * h * 5 / 4..].to_vec() }).collect()
}
pub fn psnr(a: &[u8], b: &[u8]) -> f64 {
assert_eq!(a.len(), b.len());
let se: f64 = a.iter().zip(b).map(|(&x, &y)| (x as f64 - y as f64).powi(2)).sum();
if se == 0.0 {
return 99.0;
}
10.0 * (255.0f64 * 255.0 * a.len() as f64 / se).log10()
}