Files
filmcraft/crates/aac/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

278 lines
9.1 KiB
Rust

#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::process::Command;
use filmcraft_aac::{Decoder, Encoder, EncoderConfig, EncoderStats, split_adts};
/// ffmpeg (see `filmcraft_testkit::oracle`); `None` after printing `SKIPPED`.
pub fn ffmpeg() -> Option<PathBuf> {
filmcraft_testkit::ffmpeg_or_skip("aac oracle")
}
pub fn fixtures() -> PathBuf {
filmcraft_testkit::fixtures_dir("aac")
}
pub struct Rng(pub u64);
impl Rng {
pub fn next(&mut self) -> f32 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
((self.0 >> 40) as f32 / (1u64 << 24) as f32) * 2.0 - 1.0
}
}
use std::f32::consts::PI;
/// Logarithmic sine sweep 40 Hz → 16 kHz.
pub fn sweep(rate: u32, n: usize, amp: f32) -> Vec<f32> {
let (f0, f1) = (40f64, 16000f64.min(rate as f64 * 0.45));
let t_total = n as f64 / rate as f64;
let k = (f1 / f0).ln();
(0..n)
.map(|i| {
let t = i as f64 / rate as f64;
let phase = 2.0 * std::f64::consts::PI * f0 * t_total / k * ((t / t_total * k).exp() - 1.0);
(phase.sin() as f32) * amp
})
.collect()
}
pub fn noise(n: usize, amp: f32, seed: u64) -> Vec<f32> {
let mut r = Rng(seed);
(0..n).map(|_| r.next() * amp).collect()
}
/// Speech-like: glottal pulse train (gliding pitch) through three resonant formant filters with a
/// syllabic envelope and pauses, plus a little fricative noise.
pub fn speech(rate: u32, n: usize, seed: u64) -> Vec<f32> {
let mut r = Rng(seed);
let fs = rate as f32;
let formants = [(700.0f32, 130.0f32), (1220.0, 70.0), (2600.0, 160.0)];
let mut states = [[0f32; 2]; 3];
let mut phase = 0f32;
let mut out = Vec::with_capacity(n);
for i in 0..n {
let t = i as f32 / fs;
let f0 = 120.0 + 30.0 * (2.0 * PI * 0.7 * t).sin();
phase += f0 / fs;
let pulse = if phase >= 1.0 {
phase -= 1.0;
1.0
} else {
0.0
};
let syll = (2.0 * PI * 3.0 * t).sin().max(0.0);
let voiced = if (t * 1.3).fract() < 0.8 { syll } else { 0.0 };
let fric = if (t * 1.3).fract() > 0.85 { 0.05 * r.next() } else { 0.0 };
let mut s = 0f32;
for (j, &(f, bw)) in formants.iter().enumerate() {
let rr = (-PI * bw / fs).exp();
let a1 = 2.0 * rr * (2.0 * PI * f / fs).cos();
let a2 = -rr * rr;
let y = pulse * voiced + a1 * states[j][0] + a2 * states[j][1];
states[j][1] = states[j][0];
states[j][0] = y;
s += y * [1.0, 0.6, 0.3][j];
}
out.push(s * 0.08 + fric);
}
let peak = out.iter().fold(0f32, |m, v| m.max(v.abs()));
out.iter().map(|v| v / peak * 0.7).collect()
}
/// Music-like: a sequence of decaying harmonic chords.
pub fn chords(rate: u32, n: usize, detune: f32) -> Vec<f32> {
let fs = rate as f32;
let roots = [220.0f32, 261.63, 196.0, 246.94];
let chord_len = (fs * 0.5) as usize;
(0..n)
.map(|i| {
let c = (i / chord_len) % roots.len();
let t = (i % chord_len) as f32 / fs;
let env = (-t * 3.0).exp() * (1.0 - (-t * 200.0).exp());
let mut s = 0f32;
for (m, ratio) in [1.0f32, 1.25, 1.5, 2.0].iter().enumerate() {
let f = roots[c] * ratio * (1.0 + detune);
for h in 1..8 {
s += (2.0 * PI * f * h as f32 * t + m as f32).sin() / (h * h) as f32;
}
}
s * env * 0.18
})
.collect()
}
/// Clicks: sharp impulses every 250 ms over a quiet low tone (transient test).
pub fn clicks(rate: u32, n: usize) -> Vec<f32> {
let fs = rate as f32;
let period = (fs * 0.25) as usize;
(0..n)
.map(|i| {
let t = i as f32 / fs;
let k = i % period;
let click = if k < 256 { 0.8 * (-(k as f32) / 24.0).exp() * (2.0 * PI * 1500.0 * k as f32 / fs).cos() } else { 0.0 };
0.02 * (2.0 * PI * 330.0 * t).sin() + click
})
.collect()
}
pub struct Encoded {
pub adts: Vec<u8>,
pub raw_bytes: usize,
pub frames: usize,
pub stats: EncoderStats,
pub asc: Vec<u8>,
pub bandwidth: u32,
}
pub fn encode(cfg: EncoderConfig, planar: &[Vec<f32>]) -> Encoded {
let mut enc = Encoder::new(EncoderConfig { adts: true, ..cfg }).unwrap();
let mut adts = Vec::new();
// feed in uneven chunks
let n = planar[0].len();
let mut pos = 0;
let mut step = 700;
while pos < n {
let end = (pos + step).min(n);
let chunk: Vec<&[f32]> = planar.iter().map(|c| &c[pos..end]).collect();
for au in enc.encode(&chunk) {
adts.extend(au);
}
pos = end;
step = step * 7 % 3001 + 100;
}
for au in enc.flush() {
adts.extend(au);
}
let stats = enc.stats();
Encoded { raw_bytes: stats.bytes as usize, frames: stats.frames as usize, stats, asc: enc.audio_specific_config(), bandwidth: enc.bandwidth(), adts }
}
/// Decode with ffmpeg → planar f32 (ffmpeg's own channel order), plus stderr.
pub fn ffmpeg_decode(ff: &Path, file: &Path, channels: usize) -> (Vec<Vec<f32>>, String) {
let out = Command::new(ff).args(["-v", "error", "-i"]).arg(file).args(["-f", "f32le", "-"]).output().unwrap();
let data = out.stdout;
let n = data.len() / 4 / channels;
let mut planar = vec![Vec::with_capacity(n); channels];
for i in 0..n {
for c in 0..channels {
let o = (i * channels + c) * 4;
planar[c].push(f32::from_le_bytes([data[o], data[o + 1], data[o + 2], data[o + 3]]));
}
}
(planar, String::from_utf8_lossy(&out.stderr).into_owned())
}
/// Encode with ffmpeg's AAC encoder to ADTS.
pub fn ffmpeg_encode(ff: &Path, planar: &[Vec<f32>], rate: u32, args: &[&str], out: &Path) {
let raw = out.with_extension("f32");
let ch = planar.len();
let mut bytes = Vec::with_capacity(planar[0].len() * ch * 4);
for i in 0..planar[0].len() {
for c in planar {
bytes.extend_from_slice(&c[i].to_le_bytes());
}
}
std::fs::write(&raw, bytes).unwrap();
let layout = match ch {
1 => "mono",
2 => "stereo",
3 => "3.0",
4 => "4.0",
5 => "5.0",
6 => "5.1",
_ => "7.1(wide)",
};
let st = Command::new(ff)
.args(["-v", "error", "-y", "-f", "f32le", "-ar", &rate.to_string(), "-ch_layout", layout, "-i"])
.arg(&raw)
.args(["-c:a", "aac"])
.args(args)
.args(["-f", "adts"])
.arg(out)
.status()
.unwrap();
assert!(st.success());
}
/// Decode an ADTS file with our decoder.
pub fn our_decode(data: &[u8]) -> Vec<Vec<f32>> {
let frames = split_adts(data).unwrap();
let mut dec = Decoder::from_adts(&frames[0].0).unwrap();
let mut out: Vec<Vec<f32>> = Vec::new();
for (_, au) in frames {
let pcm = dec.decode(au).unwrap();
out.resize(pcm.len(), Vec::new());
for (o, p) in out.iter_mut().zip(pcm) {
o.extend(p);
}
}
out
}
/// SNR in dB of `dec` against `refr`.
pub fn snr(refr: &[f32], dec: &[f32]) -> f64 {
let n = refr.len().min(dec.len());
let (mut s, mut e) = (0f64, 0f64);
for i in 0..n {
s += (refr[i] as f64).powi(2);
e += (refr[i] as f64 - dec[i] as f64).powi(2);
}
10.0 * (s / e.max(1e-20)).log10()
}
/// Segmental SNR (1024-sample segments above -50 dBFS, each clamped to [-10, 60] dB).
pub fn seg_snr(refr: &[f32], dec: &[f32]) -> f64 {
let n = refr.len().min(dec.len());
let mut acc = 0f64;
let mut cnt = 0;
for s in (0..n.saturating_sub(1024)).step_by(1024) {
let (mut p, mut e) = (0f64, 0f64);
for i in s..s + 1024 {
p += (refr[i] as f64).powi(2);
e += (refr[i] as f64 - dec[i] as f64).powi(2);
}
if p / 1024.0 < 1e-5 {
continue;
}
acc += (10.0 * (p / e.max(1e-20)).log10()).clamp(-10.0, 60.0);
cnt += 1;
}
if cnt == 0 { 0.0 } else { acc / cnt as f64 }
}
pub fn max_abs_diff(a: &[f32], b: &[f32]) -> f32 {
a.iter().zip(b).fold(0f32, |m, (x, y)| m.max((x - y).abs()))
}
/// Windowed-sinc (Blackman, 401 taps) low-pass at `fc` Hz, zero-phase (delay-compensated).
pub fn lowpass(x: &[f32], fc: f32, fs: f32) -> Vec<f32> {
let taps = 401usize;
let m = (taps - 1) as f32;
let wc = fc / fs;
let h: Vec<f32> = (0..taps)
.map(|i| {
let n = i as f32 - m / 2.0;
let sinc = if n == 0.0 { 2.0 * wc } else { (2.0 * PI * wc * n).sin() / (PI * n) };
let w = 0.42 - 0.5 * (2.0 * PI * i as f32 / m).cos() + 0.08 * (4.0 * PI * i as f32 / m).cos();
sinc * w
})
.collect();
let half = taps / 2;
(0..x.len())
.map(|i| {
let mut acc = 0f32;
for (k, hk) in h.iter().enumerate() {
let j = i as isize + k as isize - half as isize;
if j >= 0 && (j as usize) < x.len() {
acc += hk * x[j as usize];
}
}
acc
})
.collect()
}