| 1 | |
| 2 | #![allow(dead_code)] |
| 3 | |
| 4 | use std::io::Cursor; |
| 5 | use std::path::PathBuf; |
| 6 | use std::process::Command; |
| 7 | |
| 8 | use ec_core::registry::{CodecId, CodecParameters, Demuxer}; |
| 9 | use ec_mp4::Mp4Demuxer; |
| 10 | use ec_prores::{DecodedFrame, ProResDecoder}; |
| 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 | |
| 25 | pub fn ffmpeg_or_skip() -> bool { |
| 26 | if have_ffmpeg() { |
| 27 | return true; |
| 28 | } |
| 29 | assert!(!require(), "EC_REQUIRE_FIXTURES set but ffmpeg is not available"); |
| 30 | eprintln!("skipping: no ffmpeg"); |
| 31 | false |
| 32 | } |
| 33 | |
| 34 | pub fn scratch(tag: &str) -> PathBuf { |
| 35 | let base = std::env::var_os("TMPDIR").map(PathBuf::from).unwrap_or_else(std::env::temp_dir); |
| 36 | let dir = base.join(format!("ec-prores-{}-{}", std::process::id(), tag)); |
| 37 | let _ = std::fs::create_dir_all(&dir); |
| 38 | dir |
| 39 | } |
| 40 | |
| 41 | pub fn run(args: &[&str]) -> bool { |
| 42 | Command::new("ffmpeg") |
| 43 | .args(["-v", "error", "-y", "-nostdin"]) |
| 44 | .args(args) |
| 45 | .status() |
| 46 | .map(|s| s.success()) |
| 47 | .unwrap_or(false) |
| 48 | } |
| 49 | |
| 50 | |
| 51 | pub fn packets(mov: &[u8]) -> Vec<Vec<u8>> { |
| 52 | let mut dm = Mp4Demuxer::new(Cursor::new(mov.to_vec())).expect("demux"); |
| 53 | let vs = dm.streams().iter().position(|s| s.params.codec == CodecId::ProRes).expect("no ProRes stream") as u32; |
| 54 | let mut out = Vec::new(); |
| 55 | while let Ok(p) = dm.next_packet() { |
| 56 | if p.stream == vs { |
| 57 | out.push(p.data.to_vec()); |
| 58 | } |
| 59 | } |
| 60 | out |
| 61 | } |
| 62 | |
| 63 | pub fn decoder(threads: usize) -> ProResDecoder { |
| 64 | let mut d = ProResDecoder::new(CodecParameters::new(CodecId::ProRes)).unwrap(); |
| 65 | d.set_threads(threads); |
| 66 | d |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | pub fn raw_ours(frames: &[DecodedFrame]) -> Vec<u8> { |
| 72 | let mut v = Vec::new(); |
| 73 | for f in frames { |
| 74 | let (w, h) = (f.video.width as usize, f.video.height as usize); |
| 75 | let (sx, sy) = f.video.format.chroma_shift(); |
| 76 | for (i, pl) in f.video.planes.iter().enumerate() { |
| 77 | let (pw, ph) = if i == 0 { (w, h) } else { (w.div_ceil(1 << sx), h.div_ceil(1 << sy)) }; |
| 78 | for y in 0..ph { |
| 79 | v.extend_from_slice(&pl.data[y * pl.stride..y * pl.stride + pw * 2]); |
| 80 | } |
| 81 | } |
| 82 | if let Some(a) = &f.alpha { |
| 83 | for y in 0..h { |
| 84 | for x in 0..w { |
| 85 | v.extend_from_slice(&(a.data[y * a.stride + x] >> 4).to_le_bytes()); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | v |
| 91 | } |
| 92 | |
| 93 | pub fn decode_ours(mov: &[u8], threads: usize) -> Result<Vec<DecodedFrame>, String> { |
| 94 | let mut dec = decoder(threads); |
| 95 | packets(mov).iter().map(|p| dec.decode_full(p).map_err(|e| e.to_string())).collect() |
| 96 | } |
| 97 | |
| 98 | |
| 99 | pub fn raw_ffmpeg(tag: &str, path: &std::path::Path, pix_fmt: &str) -> Result<Vec<u8>, String> { |
| 100 | let out = scratch(tag).join("ref.raw"); |
| 101 | let ok = run(&[ |
| 102 | "-i", |
| 103 | path.to_str().unwrap(), |
| 104 | "-f", |
| 105 | "rawvideo", |
| 106 | "-pix_fmt", |
| 107 | pix_fmt, |
| 108 | "-fps_mode", |
| 109 | "passthrough", |
| 110 | out.to_str().unwrap(), |
| 111 | ]); |
| 112 | if !ok { |
| 113 | return Err("ffmpeg decode failed".into()); |
| 114 | } |
| 115 | std::fs::read(&out).map_err(|e| e.to_string()) |
| 116 | } |
| 117 | |
| 118 | |
| 119 | pub fn first_diff(a: &[u8], b: &[u8]) -> Option<usize> { |
| 120 | if a.len() != b.len() { |
| 121 | return Some(a.len().min(b.len())); |
| 122 | } |
| 123 | a.iter().zip(b).position(|(x, y)| x != y) |
| 124 | } |