| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | #![allow(dead_code)] |
| 9 | |
| 10 | use std::path::{Path, PathBuf}; |
| 11 | use std::process::Command; |
| 12 | |
| 13 | use ec_core::frame::VideoFrame; |
| 14 | use ec_core::registry::{CodecId, CodecParameters}; |
| 15 | use ec_mpeg4::{Mpeg4Decoder, PictureInfo}; |
| 16 | |
| 17 | pub fn require() -> bool { |
| 18 | std::env::var_os("EC_REQUIRE_FIXTURES").is_some() |
| 19 | } |
| 20 | |
| 21 | pub fn have_ffmpeg() -> bool { |
| 22 | Command::new("ffmpeg") |
| 23 | .arg("-version") |
| 24 | .output() |
| 25 | .map(|o| o.status.success()) |
| 26 | .unwrap_or(false) |
| 27 | } |
| 28 | |
| 29 | |
| 30 | pub fn have_encoder(name: &str) -> bool { |
| 31 | Command::new("ffmpeg") |
| 32 | .args(["-hide_banner", "-encoders"]) |
| 33 | .output() |
| 34 | .map(|o| { |
| 35 | String::from_utf8_lossy(&o.stdout) |
| 36 | .lines() |
| 37 | .any(|l| l.split_whitespace().nth(1) == Some(name)) |
| 38 | }) |
| 39 | .unwrap_or(false) |
| 40 | } |
| 41 | |
| 42 | |
| 43 | pub fn gate() -> bool { |
| 44 | if have_ffmpeg() { |
| 45 | return true; |
| 46 | } |
| 47 | assert!( |
| 48 | !require(), |
| 49 | "ffmpeg is required (EC_REQUIRE_FIXTURES is set)" |
| 50 | ); |
| 51 | eprintln!("skipped: no ffmpeg"); |
| 52 | false |
| 53 | } |
| 54 | |
| 55 | pub fn scratch(tag: &str) -> PathBuf { |
| 56 | let base = std::env::var_os("TMPDIR") |
| 57 | .map(PathBuf::from) |
| 58 | .unwrap_or_else(std::env::temp_dir); |
| 59 | let dir = base.join(format!("ec-mpeg4-{}-{}", std::process::id(), tag)); |
| 60 | let _ = std::fs::create_dir_all(&dir); |
| 61 | dir |
| 62 | } |
| 63 | |
| 64 | pub fn run(args: &[&str]) -> bool { |
| 65 | Command::new("ffmpeg") |
| 66 | .args(["-v", "error", "-y", "-nostdin"]) |
| 67 | .args(args) |
| 68 | .status() |
| 69 | .map(|s| s.success()) |
| 70 | .unwrap_or(false) |
| 71 | } |
| 72 | |
| 73 | |
| 74 | pub fn src(size: &str, frames: u32) -> Vec<String> { |
| 75 | vec![ |
| 76 | "-f".into(), |
| 77 | "lavfi".into(), |
| 78 | "-i".into(), |
| 79 | format!("testsrc2=size={size}:rate=25"), |
| 80 | "-frames:v".into(), |
| 81 | frames.to_string(), |
| 82 | ] |
| 83 | } |
| 84 | |
| 85 | |
| 86 | pub fn encode(size: &str, frames: u32, enc: &[&str], path: &Path) -> bool { |
| 87 | let s = src(size, frames); |
| 88 | let mut args: Vec<&str> = s.iter().map(|x| x.as_str()).collect(); |
| 89 | args.extend_from_slice(enc); |
| 90 | args.push(path.to_str().unwrap()); |
| 91 | run(&args) |
| 92 | } |
| 93 | |
| 94 | |
| 95 | pub fn reference(input: &Path, in_args: &[&str], out: &Path) -> Result<Vec<u8>, String> { |
| 96 | let mut args: Vec<&str> = vec!["-threads", "1", "-cpuflags", "0"]; |
| 97 | args.extend_from_slice(in_args); |
| 98 | args.extend_from_slice(&[ |
| 99 | "-i", |
| 100 | input.to_str().unwrap(), |
| 101 | "-map", |
| 102 | "0:v:0", |
| 103 | "-f", |
| 104 | "rawvideo", |
| 105 | "-pix_fmt", |
| 106 | "yuv420p", |
| 107 | "-fps_mode", |
| 108 | "passthrough", |
| 109 | out.to_str().unwrap(), |
| 110 | ]); |
| 111 | if !run(&args) { |
| 112 | return Err("ffmpeg decode failed".into()); |
| 113 | } |
| 114 | std::fs::read(out).map_err(|e| e.to_string()) |
| 115 | } |
| 116 | |
| 117 | |
| 118 | pub fn frame_bytes(f: &VideoFrame) -> Vec<u8> { |
| 119 | let mut v = Vec::new(); |
| 120 | for p in &f.planes { |
| 121 | v.extend_from_slice(&p.data); |
| 122 | } |
| 123 | v |
| 124 | } |
| 125 | |
| 126 | |
| 127 | pub fn split_m4v(es: &[u8]) -> Vec<&[u8]> { |
| 128 | let mut cuts = vec![0usize]; |
| 129 | let mut have_vop = false; |
| 130 | let mut i = 0; |
| 131 | while i + 3 < es.len() { |
| 132 | if es[i] == 0 && es[i + 1] == 0 && es[i + 2] == 1 { |
| 133 | match es[i + 3] { |
| 134 | 0xB6 => { |
| 135 | if have_vop { |
| 136 | cuts.push(i); |
| 137 | } |
| 138 | have_vop = true; |
| 139 | } |
| 140 | 0xB0 | 0xB3 if have_vop => { |
| 141 | cuts.push(i); |
| 142 | have_vop = false; |
| 143 | } |
| 144 | _ => {} |
| 145 | } |
| 146 | i += 3; |
| 147 | } else { |
| 148 | i += 1; |
| 149 | } |
| 150 | } |
| 151 | cuts.push(es.len()); |
| 152 | cuts.windows(2).map(|w| &es[w[0]..w[1]]).collect() |
| 153 | } |
| 154 | |
| 155 | |
| 156 | pub fn split_h263(es: &[u8]) -> Vec<&[u8]> { |
| 157 | let mut cuts = vec![]; |
| 158 | let mut i = 0; |
| 159 | while i + 3 < es.len() { |
| 160 | if es[i] == 0 && es[i + 1] == 0 && es[i + 2] & 0xFC == 0x80 { |
| 161 | cuts.push(i); |
| 162 | i += 3; |
| 163 | } else { |
| 164 | i += 1; |
| 165 | } |
| 166 | } |
| 167 | cuts.push(es.len()); |
| 168 | cuts.windows(2).map(|w| &es[w[0]..w[1]]).collect() |
| 169 | } |
| 170 | |
| 171 | pub fn new_decoder() -> Mpeg4Decoder { |
| 172 | Mpeg4Decoder::new(CodecParameters::new(CodecId::Mpeg4Part2)).unwrap() |
| 173 | } |
| 174 | |
| 175 | |
| 176 | pub fn decode_packets<'a>( |
| 177 | dec: &mut Mpeg4Decoder, |
| 178 | packets: impl IntoIterator<Item = &'a [u8]>, |
| 179 | ) -> Result<Vec<(VideoFrame, PictureInfo)>, String> { |
| 180 | let mut out = Vec::new(); |
| 181 | for p in packets { |
| 182 | dec.send_bytes(p, None).map_err(|e| e.to_string())?; |
| 183 | while let Some(x) = dec.pop_picture() { |
| 184 | out.push(x); |
| 185 | } |
| 186 | } |
| 187 | dec.flush_stream().map_err(|e| e.to_string())?; |
| 188 | while let Some(x) = dec.pop_picture() { |
| 189 | out.push(x); |
| 190 | } |
| 191 | Ok(out) |
| 192 | } |
| 193 | |
| 194 | |
| 195 | pub fn compare_frames( |
| 196 | ours: &[(VideoFrame, PictureInfo)], |
| 197 | reference: &[u8], |
| 198 | ) -> Result<usize, String> { |
| 199 | if ours.is_empty() { |
| 200 | return Err("no frames".into()); |
| 201 | } |
| 202 | let fsz = frame_bytes(&ours[0].0).len(); |
| 203 | let n_ref = reference.len() / fsz; |
| 204 | if n_ref != ours.len() { |
| 205 | return Err(format!("frame count: ours {} ffmpeg {}", ours.len(), n_ref)); |
| 206 | } |
| 207 | for (i, (f, info)) in ours.iter().enumerate() { |
| 208 | let b = frame_bytes(f); |
| 209 | let r = &reference[i * fsz..(i + 1) * fsz]; |
| 210 | if b != r { |
| 211 | let pos = b.iter().zip(r).position(|(a, c)| a != c).unwrap(); |
| 212 | let w = f.width as usize; |
| 213 | let h = f.height as usize; |
| 214 | let (plane, off) = if pos < w * h { |
| 215 | ("Y", pos) |
| 216 | } else { |
| 217 | ("C", pos - w * h) |
| 218 | }; |
| 219 | let nbad = b.iter().zip(r).filter(|(a, c)| a != c).count(); |
| 220 | return Err(format!( |
| 221 | "frame {i} ({}) differs: {nbad} bytes, first at {plane} +{off} (x={} y={}) ours {} ffmpeg {}", |
| 222 | info.pict_type, |
| 223 | off % w.max(1), |
| 224 | off / w.max(1), |
| 225 | b[pos], |
| 226 | r[pos] |
| 227 | )); |
| 228 | } |
| 229 | } |
| 230 | Ok(ours.len()) |
| 231 | } |
| 232 | |
| 233 | |
| 234 | pub fn parity_es( |
| 235 | tag: &str, |
| 236 | size: &str, |
| 237 | frames: u32, |
| 238 | enc: &[&str], |
| 239 | fmt: &str, |
| 240 | ) -> Result<usize, String> { |
| 241 | let dir = scratch(tag); |
| 242 | let es_path = dir.join(format!("{tag}.es")); |
| 243 | let mut e: Vec<&str> = enc.to_vec(); |
| 244 | e.extend_from_slice(&["-f", fmt]); |
| 245 | if !encode(size, frames, &e, &es_path) { |
| 246 | return Err(format!("ffmpeg encode failed ({enc:?})")); |
| 247 | } |
| 248 | let reference = reference(&es_path, &["-f", fmt], &dir.join("ref.yuv"))?; |
| 249 | let es = std::fs::read(&es_path).map_err(|e| e.to_string())?; |
| 250 | let pkts = if fmt == "h263" { |
| 251 | split_h263(&es) |
| 252 | } else { |
| 253 | split_m4v(&es) |
| 254 | }; |
| 255 | let mut dec = new_decoder(); |
| 256 | let ours = decode_packets(&mut dec, pkts)?; |
| 257 | let n = compare_frames(&ours, &reference)?; |
| 258 | let _ = std::fs::remove_dir_all(&dir); |
| 259 | Ok(n) |
| 260 | } |