| 1 | |
| 2 | #![allow(dead_code)] |
| 3 | |
| 4 | pub mod synth; |
| 5 | |
| 6 | use std::path::{Path, PathBuf}; |
| 7 | use std::process::Command; |
| 8 | |
| 9 | use ec_core::frame::VideoFrame; |
| 10 | use ec_mpeg2::Mpeg2Decoder; |
| 11 | |
| 12 | pub fn require() -> bool { |
| 13 | std::env::var_os("EC_REQUIRE_FIXTURES").is_some() |
| 14 | } |
| 15 | |
| 16 | pub fn have_ffmpeg() -> bool { |
| 17 | Command::new("ffmpeg") |
| 18 | .arg("-version") |
| 19 | .output() |
| 20 | .map(|o| o.status.success()) |
| 21 | .unwrap_or(false) |
| 22 | } |
| 23 | |
| 24 | pub fn scratch(tag: &str) -> PathBuf { |
| 25 | let base = std::env::var_os("TMPDIR") |
| 26 | .map(PathBuf::from) |
| 27 | .unwrap_or_else(std::env::temp_dir); |
| 28 | let dir = base.join(format!("ec-mpeg2-{}-{}", std::process::id(), tag)); |
| 29 | let _ = std::fs::create_dir_all(&dir); |
| 30 | dir |
| 31 | } |
| 32 | |
| 33 | pub fn run(args: &[&str]) -> bool { |
| 34 | Command::new("ffmpeg") |
| 35 | .args(["-v", "error", "-y", "-nostdin"]) |
| 36 | .args(args) |
| 37 | .status() |
| 38 | .map(|s| s.success()) |
| 39 | .unwrap_or(false) |
| 40 | } |
| 41 | |
| 42 | |
| 43 | pub fn frame_bytes(f: &VideoFrame) -> Vec<u8> { |
| 44 | let mut v = Vec::new(); |
| 45 | for p in &f.planes { |
| 46 | v.extend_from_slice(&p.data); |
| 47 | } |
| 48 | v |
| 49 | } |
| 50 | |
| 51 | pub fn decode_ours(es: &[u8], chunk: usize) -> Result<Vec<(VideoFrame, ec_mpeg2::PictureInfo)>, String> { |
| 52 | let mut dec = Mpeg2Decoder::new(ec_core::registry::CodecParameters::new(ec_core::registry::CodecId::Mpeg2Video)) |
| 53 | .map_err(|e| e.to_string())?; |
| 54 | let mut out = Vec::new(); |
| 55 | for c in es.chunks(chunk.max(1)) { |
| 56 | dec.send_bytes(c, None).map_err(|e| e.to_string())?; |
| 57 | while let Some(p) = dec.pop_picture() { |
| 58 | out.push(p); |
| 59 | } |
| 60 | } |
| 61 | dec.flush_stream().map_err(|e| e.to_string())?; |
| 62 | while let Some(p) = dec.pop_picture() { |
| 63 | out.push(p); |
| 64 | } |
| 65 | Ok(out) |
| 66 | } |
| 67 | |
| 68 | |
| 69 | pub fn compare(tag: &str, es_path: &Path, pix_fmt: &str, extra_dec: &[&str]) -> Result<usize, String> { |
| 70 | let dir = scratch(tag); |
| 71 | let refp = dir.join("ref.yuv"); |
| 72 | let mut args: Vec<&str> = vec![]; |
| 73 | args.extend_from_slice(extra_dec); |
| 74 | let es_s = es_path.to_str().unwrap(); |
| 75 | let ref_s = refp.to_str().unwrap(); |
| 76 | args.extend_from_slice(&["-i", es_s, "-f", "rawvideo", "-pix_fmt", pix_fmt, "-fps_mode", "passthrough", ref_s]); |
| 77 | if !run(&args) { |
| 78 | return Err("ffmpeg decode failed".into()); |
| 79 | } |
| 80 | let reference = std::fs::read(&refp).map_err(|e| e.to_string())?; |
| 81 | let es = std::fs::read(es_path).map_err(|e| e.to_string())?; |
| 82 | let ours = decode_ours(&es, 4093)?; |
| 83 | if ours.is_empty() { |
| 84 | return Err("no frames".into()); |
| 85 | } |
| 86 | let fsz = frame_bytes(&ours[0].0).len(); |
| 87 | let n_ref = reference.len() / fsz; |
| 88 | if n_ref != ours.len() { |
| 89 | return Err(format!("frame count: ours {} ffmpeg {}", ours.len(), n_ref)); |
| 90 | } |
| 91 | for (i, (f, info)) in ours.iter().enumerate() { |
| 92 | let b = frame_bytes(f); |
| 93 | let r = &reference[i * fsz..(i + 1) * fsz]; |
| 94 | if b != r { |
| 95 | let pos = b.iter().zip(r).position(|(a, c)| a != c).unwrap(); |
| 96 | let w = f.width as usize; |
| 97 | let h = f.height as usize; |
| 98 | let (plane, off) = if pos < w * h { ("Y", pos) } else { ("C", pos - w * h) }; |
| 99 | let nbad = b.iter().zip(r).filter(|(a, c)| a != c).count(); |
| 100 | if std::env::var_os("EC_DIFFS").is_some() { |
| 101 | let d: Vec<(usize, usize, i32)> = b.iter().zip(r).enumerate().filter(|(_, (a, c))| a != c).take(60).map(|(i, (a, c))| if i < w * h { (i % w, i / w, *a as i32 - *c as i32) } else { (i, 0, 0) }).collect(); |
| 102 | eprintln!("DIFFS {d:?}"); |
| 103 | } |
| 104 | return Err(format!( |
| 105 | "frame {i} ({:?} tr {}) differs: {} bytes, first at {plane} +{off} (x={} y={}) ours {} ffmpeg {}", |
| 106 | info.picture_type, |
| 107 | info.temporal_reference, |
| 108 | nbad, |
| 109 | off % w, |
| 110 | off / w, |
| 111 | b[pos], |
| 112 | r[pos] |
| 113 | )); |
| 114 | } |
| 115 | } |
| 116 | let _ = std::fs::remove_dir_all(&dir); |
| 117 | Ok(ours.len()) |
| 118 | } |