diff --git a/crates/ec-dv/tests/perf.rs b/crates/ec-dv/tests/perf.rs deleted file mode 100644 index 784488b0..00000000 --- a/crates/ec-dv/tests/perf.rs +++ /dev/null @@ -1,60 +0,0 @@ -//! Throughput floor: SD >= 300 fps, HD >= 60 fps (release builds only). -mod common; - -use std::time::Instant; - -use common::*; -use ec_dv::DvVideoDecoder; - -fn fps(f: &Flavour) -> Option { - let path = make(f, "perf", "dv", true, 10)?; - let data = std::fs::read(path).unwrap(); - let fs = ec_dv::frame_profile(None, &data).unwrap().frame_size; - let frames: Vec<&[u8]> = data.chunks_exact(fs).collect(); - let mut d = DvVideoDecoder::new(); - d.decode(frames[0]).unwrap(); - let start = Instant::now(); - let mut n = 0u32; - while start.elapsed().as_secs_f64() < 1.0 || n < 20 { - for fr in &frames { - std::hint::black_box(d.decode(fr).unwrap()); - n += 1; - } - } - Some(f64::from(n) / start.elapsed().as_secs_f64()) -} - -/// A shared machine under load cannot show the floor: the check is skipped (and says so) when the -/// 1-minute load average exceeds half the cores, which is how the lane servers look while other -/// jobs run. An idle machine enforces the floor. -fn machine_is_busy() -> bool { - let load = std::fs::read_to_string("/proc/loadavg") - .ok() - .and_then(|s| s.split_whitespace().next().and_then(|v| v.parse::().ok())); - let cores = std::thread::available_parallelism().map_or(1, |n| n.get()) as f64; - load.is_some_and(|l| l > cores * 0.5) -} - -#[test] -fn decode_speed() { - let mut bad = Vec::new(); - for f in FLAVOURS { - let hd = f.size.starts_with("1440") || f.size.starts_with("1280") || f.size.starts_with("960"); - let mut r = 0f64; - for _ in 0..3 { - let Some(x) = fps(f) else { panic!("fixture {}", f.name) }; - r = r.max(x); - if r >= if hd { 60.0 } else { 300.0 } { - break; - } - } - eprintln!("{}: {r:.0} fps", f.name); - if !cfg!(debug_assertions) && !machine_is_busy() { - let floor = if hd { 60.0 } else { 300.0 }; - if r < floor { - bad.push(format!("{}: {r:.0} fps < {floor}", f.name)); - } - } - } - assert!(bad.is_empty(), "{bad:?}"); -} diff --git a/crates/ec-dv/tests/robust.rs b/crates/ec-dv/tests/robust.rs deleted file mode 100644 index 8a57ce01..00000000 --- a/crates/ec-dv/tests/robust.rs +++ /dev/null @@ -1,91 +0,0 @@ -//! Damaged and hostile input: nothing may panic, and decoding is deterministic. -use ec_dv::{DvVideoDecoder, PROFILES, extract_audio}; - -struct Rng(u64); -impl Rng { - fn next(&mut self) -> u64 { - self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); - self.0 >> 33 - } -} - -fn frame_for(p: &ec_dv::Profile, rng: &mut Rng, header_bytes: bool) -> Vec { - let mut f: Vec = (0..p.frame_size).map(|_| rng.next() as u8).collect(); - if header_bytes { - f[3] = (f[3] & 0x7f) | (p.dsf << 7); - f[80 * 5 + 48 + 3] = p.video_stype; - f[4] &= 0xf8; - for b in 0..p.frame_size / 80 { - // plausible DIF headers keep the segment status nibbles equal, as real streams do - f[b * 80 + 3] = (f[b * 80 + 3] & 0x0f) | 0xf0; - } - f[3] = (f[3] & 0x7f) | (p.dsf << 7); - f[80 * 5 + 48 + 3] = p.video_stype; - } - f -} - -#[test] -fn random_frames_of_every_profile_do_not_panic() { - let mut rng = Rng(7); - for (i, p) in PROFILES.iter().enumerate() { - if i == 2 || i == 9 { - continue; // PAL 4:1:1 / IEC 61883-5 share their header bytes with other entries - } - for round in 0..6 { - let f = frame_for(p, &mut rng, round % 2 == 0); - let mut d = DvVideoDecoder::new(); - if round % 2 == 1 { - let _ = d.decode(&f); // random header: an error is fine, a panic is not - let _ = extract_audio(&f); - continue; - } - let a = d.decode(&f).unwrap_or_else(|e| panic!("profile {i} round {round}: {e:?}")); - let b = DvVideoDecoder::new().decode(&f).unwrap(); - assert_eq!(a.planes, b.planes, "decode must be deterministic"); - assert_eq!((a.width, a.height), (p.width, p.height)); - let _ = extract_audio(&f); - } - } -} - -#[test] -fn bit_flips_and_truncations_do_not_panic() { - let mut rng = Rng(99); - let p = &PROFILES[0]; - let base = frame_for(p, &mut rng, true); - let mut d = DvVideoDecoder::new(); - for n in 0..200 { - let mut f = base.clone(); - for _ in 0..(1 + n % 20) { - let at = rng.next() as usize % f.len(); - f[at] ^= 1 << (rng.next() % 8); - } - let _ = d.decode(&f); - let cut = rng.next() as usize % f.len(); - let _ = d.decode(&f[..cut]); - let _ = extract_audio(&f[..cut]); - let _ = extract_audio(&f); - } -} - -#[test] -fn all_headers_with_hostile_audio_packs() { - let mut rng = Rng(5); - for p in PROFILES.iter() { - for _ in 0..20 { - let mut f = frame_for(p, &mut rng, true); - for c in 0..10usize { - let offs = if c & 1 == 1 { 80 * 6 + 3 + c * 12000 } else { 80 * 6 + 80 * 16 * 3 + 3 + c * 12000 }; - if offs + 5 < f.len() { - f[offs] = 0x50; - f[offs + 1] = rng.next() as u8; - f[offs + 3] = rng.next() as u8; - f[offs + 4] = rng.next() as u8; - } - } - let _ = extract_audio(&f); - let _ = DvVideoDecoder::new().decode(&f); - } - } -} diff --git a/crates/ec-prores/tests/perf.rs b/crates/ec-prores/tests/perf.rs deleted file mode 100644 index e6f6515b..00000000 --- a/crates/ec-prores/tests/perf.rs +++ /dev/null @@ -1,43 +0,0 @@ -//! Throughput of 1080p 4:2:2 HQ (ffmpeg-encoded, light noise so the entropy coder has work). -//! Prints fps for 1 and 4 threads; set `EC_PRORES_MIN_FPS` to assert the single-thread rate. - -mod common; -use common::*; - -#[test] -fn hd_hq_throughput() { - let dir = scratch("perf"); - let mov = dir.join("hd.mov"); - assert!(run(&[ - "-f", "lavfi", "-i", "testsrc2=size=1920x1080:rate=25,noise=alls=12:allf=t", - "-frames:v", "24", "-c:v", "prores_ks", "-profile", "3", "-pix_fmt", "yuv422p10le", - mov.to_str().unwrap(), - ])); - let bytes = std::fs::read(&mov).unwrap(); - let pk = packets(&bytes); - let mut single = 0.0; - for threads in [1usize, 4] { - let mut dec = decoder(threads); - // Warm up, then best of 10 passes. - for p in &pk { - dec.decode(p).unwrap(); - } - let mut best = f64::MAX; - for _ in 0..10 { - let t0 = std::time::Instant::now(); - for p in &pk { - std::hint::black_box(dec.decode(p).unwrap()); - } - best = best.min(t0.elapsed().as_secs_f64()); - } - let fps = pk.len() as f64 / best; - eprintln!("1080p 422 HQ, {} bytes/frame, {threads} thread(s): {fps:.1} fps", bytes.len() / pk.len()); - if threads == 1 { - single = fps; - } - } - if let Some(min) = std::env::var("EC_PRORES_MIN_FPS").ok().and_then(|v| v.parse::().ok()) { - assert!(single >= min, "single-thread {single:.1} fps below {min}"); - } - let _ = std::fs::remove_dir_all(dir); -} diff --git a/crates/ec-prores/tests/robust.rs b/crates/ec-prores/tests/robust.rs deleted file mode 100644 index 09c91129..00000000 --- a/crates/ec-prores/tests/robust.rs +++ /dev/null @@ -1,135 +0,0 @@ -//! Robustness: truncated, corrupted and hostile frames must never panic, and refusals must name -//! what they refused. - -mod common; -use common::*; -use ec_core::Error; - -struct Rng(u64); -impl Rng { - fn next(&mut self) -> u64 { - self.0 ^= self.0 << 13; - self.0 ^= self.0 >> 7; - self.0 ^= self.0 << 17; - self.0 - } - fn below(&mut self, n: usize) -> usize { - (self.next() % n.max(1) as u64) as usize - } -} - -/// Small frames: progressive 422, interlaced 422, 4444 with alpha. -fn frames() -> Vec> { - static CACHE: std::sync::OnceLock>> = std::sync::OnceLock::new(); - CACHE.get_or_init(make_frames).clone() -} - -fn make_frames() -> Vec> { - let dir = scratch("robust"); - let mut out = Vec::new(); - for (i, (prof, fmt, extra)) in [ - ("3", "yuv422p10le", ""), - ("1", "yuv422p10le", "+ildct"), - ("4", "yuva444p10le", ""), - ] - .into_iter() - .enumerate() - { - let mov = dir.join(format!("r{i}.mov")); - let mut a = vec![ - "-f", "lavfi", "-i", "testsrc2=size=100x70:rate=25,noise=alls=20:allf=t", "-frames:v", "1", - "-c:v", "prores_ks", "-profile", prof, "-pix_fmt", fmt, - ]; - if !extra.is_empty() { - a.extend(["-flags", extra]); - } - a.push(mov.to_str().unwrap()); - assert!(run(&a)); - out.push(packets(&std::fs::read(&mov).unwrap()).remove(0)); - } - let _ = std::fs::remove_dir_all(dir); - out -} - -#[test] -fn truncations_never_panic() { - let fs = frames(); - for f in &fs { - let mut d = decoder(2); - assert!(d.decode(f).is_ok()); - for n in (0..f.len()).step_by(7).chain(0..40) { - let _ = d.decode(&f[..n.min(f.len())]); - } - } -} - -#[test] -fn corruptions_never_panic() { - let fs = frames(); - let mut rng = Rng(0x9E3779B97F4A7C15); - for f in &fs { - let mut d = decoder(3); - for round in 0..400 { - let mut g = f.clone(); - let flips = 1 + rng.below(8); - for _ in 0..flips { - // Half the hits land in the headers, where the structure lives. - let at = if round % 2 == 0 { rng.below(g.len().min(200)) } else { rng.below(g.len()) }; - g[at] ^= 1 << rng.below(8); - } - let _ = d.decode(&g); - } - } -} - -#[test] -fn garbage_is_refused_by_name() { - let mut d = decoder(1); - let mut rng = Rng(7); - for len in [0usize, 1, 7, 8, 27, 28, 200, 5000] { - let g: Vec = (0..len).map(|_| rng.next() as u8).collect(); - assert!(d.decode(&g).is_err()); - } - // Right magic, nonsense after it. - let mut g = vec![0u8; 300]; - g[..4].copy_from_slice(&300u32.to_be_bytes()); - g[4..8].copy_from_slice(b"icpf"); - match d.decode(&g) { - Err(Error::Unsupported { .. } | Error::Corrupt { .. }) => {} - other => panic!("expected a named refusal, got {:?}", other.map(|f| f.width)), - } -} - -#[test] -fn hostile_dimensions_are_bounded() { - let fs = frames(); - let mut g = fs[0].clone(); - // width/height at frame-header bytes 8..12 (after size+icpf: offset 8+8). - g[16..20].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]); - let t = std::time::Instant::now(); - assert!(decoder(1).decode(&g).is_err()); - assert!(t.elapsed().as_secs() < 5); - g[16..20].copy_from_slice(&[0, 0, 0, 0]); - assert!(decoder(1).decode(&g).is_err()); -} - -#[test] -fn wrong_codec_is_unsupported() { - use ec_core::registry::{CodecId, CodecParameters}; - match ec_prores::ProResDecoder::new(CodecParameters::new(CodecId::H264)) { - Err(Error::Unsupported { .. }) => {} - _ => panic!("expected Unsupported"), - } -} - -/// `EC_SEED_DIR=crates/ec-prores/fuzz/corpus/decode_prores cargo test -p ec-prores --test robust -- --ignored dump_seeds` -#[test] -#[ignore] -fn dump_seeds() { - let dir = std::env::var("EC_SEED_DIR").expect("EC_SEED_DIR"); - std::fs::create_dir_all(&dir).unwrap(); - let fs = frames(); - for (i, f) in fs.iter().enumerate() { - std::fs::write(format!("{dir}/seed{i}.bin"), f).unwrap(); - } -}