ec-hw: hw_oracle example, per-frame framecrc vs ffmpeg software decode, reports Vk fallback
Browse files at this commita63faef499401e738d4636f400f81ebc38b65d52Parent: dada6231 changed file +139 -0
crates/ec-hw/examples/hw_oracle.rsadded+139 -0
| @@ -0,0 +1,139 @@ | |||
| 1 | +//! Decodes each stream through HwDecoder and compares every frame with ffmpeg's software decode (framecrc, all planes). | ||
| 2 | +//! | ||
| 3 | +//! ```text | ||
| 4 | +//! cargo run --release -p ec-hw --example hw_oracle -- <stream>... | ||
| 5 | +//! ``` | ||
| 6 | + | ||
| 7 | +use std::fs; | ||
| 8 | +use std::path::Path; | ||
| 9 | +use std::process::Command; | ||
| 10 | + | ||
| 11 | +use ec_hw::{Backend, Codec, HwDecoder}; | ||
| 12 | + | ||
| 13 | +fn main() { | ||
| 14 | + let mut failed = 0; | ||
| 15 | + for path in std::env::args().skip(1) { | ||
| 16 | + let line = match check(Path::new(&path)) { | ||
| 17 | + Ok((backend, frames)) if backend == Backend::Vk => format!("PASS {path} Vk {frames} frames"), | ||
| 18 | + Ok((backend, frames)) => { | ||
| 19 | + failed += 1; | ||
| 20 | + format!("FALLBACK {path} {backend:?} {frames} frames") | ||
| 21 | + } | ||
| 22 | + Err(e) => { | ||
| 23 | + failed += 1; | ||
| 24 | + format!("FAIL {path}: {e}") | ||
| 25 | + } | ||
| 26 | + }; | ||
| 27 | + println!("{line}"); | ||
| 28 | + } | ||
| 29 | + std::process::exit(i32::from(failed > 0)); | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +fn check(path: &Path) -> Result<(Backend, usize), String> { | ||
| 33 | + let data = fs::read(path).map_err(|e| e.to_string())?; | ||
| 34 | + let (codec, units) = split(path, &data)?; | ||
| 35 | + let (wide, expected) = reference(path)?; | ||
| 36 | + let mut decoder = HwDecoder::open(codec).map_err(|e| format!("open: {e}"))?; | ||
| 37 | + let mut ours = Vec::new(); | ||
| 38 | + for (pts, unit) in units.iter().enumerate() { | ||
| 39 | + decoder.decode(unit, pts as i64).map_err(|e| format!("decode unit {pts}: {e}"))?; | ||
| 40 | + drain(&mut decoder, wide, &mut ours)?; | ||
| 41 | + } | ||
| 42 | + decoder.flush().map_err(|e| format!("flush: {e}"))?; | ||
| 43 | + drain(&mut decoder, wide, &mut ours)?; | ||
| 44 | + let backend = decoder.backend(); | ||
| 45 | + for (index, (got, want)) in ours.iter().zip(&expected).enumerate() { | ||
| 46 | + if got != want { | ||
| 47 | + return Err(format!("{backend:?} frame {index}: ours {got:?}, ffmpeg {want:?}")); | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + if ours.len() != expected.len() { | ||
| 51 | + return Err(format!("{backend:?} frame count: ours {}, ffmpeg {}", ours.len(), expected.len())); | ||
| 52 | + } | ||
| 53 | + Ok((backend, ours.len())) | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +fn drain(decoder: &mut HwDecoder, wide: bool, out: &mut Vec<(usize, u32)>) -> Result<(), String> { | ||
| 57 | + while let Some(picture) = decoder.next_picture().map_err(|e| format!("next picture: {e}"))? { | ||
| 58 | + let bytes = if wide { | ||
| 59 | + let frame = picture.to_i420_16().map_err(|e| format!("to_i420_16: {e}"))?; | ||
| 60 | + [frame.y, frame.u, frame.v] | ||
| 61 | + .concat() | ||
| 62 | + .iter() | ||
| 63 | + .flat_map(|s| s.to_le_bytes()) | ||
| 64 | + .collect() | ||
| 65 | + } else { | ||
| 66 | + let frame = picture.to_i420().map_err(|e| format!("to_i420: {e}"))?; | ||
| 67 | + [frame.y, frame.u, frame.v].concat() | ||
| 68 | + }; | ||
| 69 | + out.push((bytes.len(), adler32(&bytes))); | ||
| 70 | + } | ||
| 71 | + Ok(()) | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +fn split<'a>(path: &Path, data: &'a [u8]) -> Result<(Codec, Vec<&'a [u8]>), String> { | ||
| 75 | + match path.extension().and_then(|e| e.to_str()) { | ||
| 76 | + Some("h264" | "264") => Ok((Codec::H264, vec![data])), | ||
| 77 | + Some("hevc" | "h265" | "265") => Ok((Codec::H265, vec![data])), | ||
| 78 | + Some("ivf") => { | ||
| 79 | + let codec = match &data[8..12] { | ||
| 80 | + b"AV01" => Codec::Av1, | ||
| 81 | + b"VP90" => Codec::Vp9, | ||
| 82 | + other => return Err(format!("ivf fourcc {other:?}")), | ||
| 83 | + }; | ||
| 84 | + Ok((codec, ivf_units(data))) | ||
| 85 | + } | ||
| 86 | + other => Err(format!("unknown extension {other:?}")), | ||
| 87 | + } | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +fn ivf_units(ivf: &[u8]) -> Vec<&[u8]> { | ||
| 91 | + let mut pos = usize::from(u16::from_le_bytes([ivf[6], ivf[7]])); | ||
| 92 | + let mut units = Vec::new(); | ||
| 93 | + while pos + 12 <= ivf.len() { | ||
| 94 | + let len = u32::from_le_bytes(ivf[pos..pos + 4].try_into().expect("frame size")) as usize; | ||
| 95 | + units.push(&ivf[pos + 12..(pos + 12 + len).min(ivf.len())]); | ||
| 96 | + pos += 12 + len; | ||
| 97 | + } | ||
| 98 | + units | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +fn reference(path: &Path) -> Result<(bool, Vec<(usize, u32)>), String> { | ||
| 102 | + let probe = Command::new("ffprobe") | ||
| 103 | + .args(["-v", "error", "-select_streams", "v:0", "-show_entries", "stream=pix_fmt", "-of", "csv=p=0"]) | ||
| 104 | + .arg(path) | ||
| 105 | + .output() | ||
| 106 | + .map_err(|e| e.to_string())?; | ||
| 107 | + let wide = String::from_utf8_lossy(&probe.stdout).contains("10"); | ||
| 108 | + let out = Command::new("ffmpeg") | ||
| 109 | + .args(["-v", "error", "-i"]) | ||
| 110 | + .arg(path) | ||
| 111 | + .args(["-noautoscale", "-pix_fmt", if wide { "yuv420p10le" } else { "yuv420p" }, "-f", "framecrc", "-"]) | ||
| 112 | + .output() | ||
| 113 | + .map_err(|e| e.to_string())?; | ||
| 114 | + if !out.status.success() { | ||
| 115 | + return Err(format!("ffmpeg: {}", String::from_utf8_lossy(&out.stderr).lines().next().unwrap_or(""))); | ||
| 116 | + } | ||
| 117 | + let frames = String::from_utf8_lossy(&out.stdout) | ||
| 118 | + .lines() | ||
| 119 | + .filter(|l| !l.starts_with('#')) | ||
| 120 | + .filter_map(|l| { | ||
| 121 | + let f: Vec<&str> = l.split(',').map(str::trim).collect(); | ||
| 122 | + Some((f.get(4)?.parse().ok()?, u32::from_str_radix(f.get(5)?.trim_start_matches("0x"), 16).ok()?)) | ||
| 123 | + }) | ||
| 124 | + .collect(); | ||
| 125 | + Ok((wide, frames)) | ||
| 126 | +} | ||
| 127 | + | ||
| 128 | +fn adler32(data: &[u8]) -> u32 { | ||
| 129 | + let (mut a, mut b) = (0u32, 0u32); | ||
| 130 | + for chunk in data.chunks(5552) { | ||
| 131 | + for &x in chunk { | ||
| 132 | + a += u32::from(x); | ||
| 133 | + b += a; | ||
| 134 | + } | ||
| 135 | + a %= 65521; | ||
| 136 | + b %= 65521; | ||
| 137 | + } | ||
| 138 | + b << 16 | a | ||
| 139 | +} | ||