| 1 | |
| 2 | #![allow(dead_code)] |
| 3 | |
| 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::process::Command; |
| 6 | |
| 7 | pub fn require() -> bool { |
| 8 | std::env::var_os("EC_REQUIRE_FIXTURES").is_some() |
| 9 | } |
| 10 | |
| 11 | pub fn have_ffmpeg() -> bool { |
| 12 | Command::new("ffmpeg").arg("-version").output().map(|o| o.status.success()).unwrap_or(false) |
| 13 | } |
| 14 | |
| 15 | |
| 16 | pub fn ffmpeg_or_skip() -> bool { |
| 17 | if have_ffmpeg() { |
| 18 | return true; |
| 19 | } |
| 20 | assert!(!require(), "EC_REQUIRE_FIXTURES set but ffmpeg is not available"); |
| 21 | eprintln!("skipping: no ffmpeg"); |
| 22 | false |
| 23 | } |
| 24 | |
| 25 | pub fn scratch(tag: &str) -> PathBuf { |
| 26 | let base = std::env::var_os("TMPDIR").map(PathBuf::from).unwrap_or_else(std::env::temp_dir); |
| 27 | let dir = base.join(format!("ec-dv-{}-{}", std::process::id(), tag)); |
| 28 | let _ = std::fs::create_dir_all(&dir); |
| 29 | dir |
| 30 | } |
| 31 | |
| 32 | pub fn run(args: &[&str]) -> bool { |
| 33 | let out = Command::new("ffmpeg").args(["-v", "error", "-y", "-nostdin"]).args(args).output(); |
| 34 | match out { |
| 35 | Ok(o) => { |
| 36 | if !o.status.success() { |
| 37 | eprintln!("ffmpeg {:?} failed: {}", args, String::from_utf8_lossy(&o.stderr)); |
| 38 | } |
| 39 | o.status.success() |
| 40 | } |
| 41 | Err(_) => false, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
| 46 | #[derive(Clone, Copy, Debug)] |
| 47 | pub struct Flavour { |
| 48 | pub name: &'static str, |
| 49 | pub size: &'static str, |
| 50 | pub rate: &'static str, |
| 51 | pub pix_fmt: &'static str, |
| 52 | |
| 53 | pub audio: bool, |
| 54 | pub frames: u32, |
| 55 | } |
| 56 | |
| 57 | pub const FLAVOURS: &[Flavour] = &[ |
| 58 | Flavour { name: "ntsc411", size: "720x480", rate: "30000/1001", pix_fmt: "yuv411p", audio: true, frames: 8 }, |
| 59 | Flavour { name: "pal420", size: "720x576", rate: "25", pix_fmt: "yuv420p", audio: true, frames: 8 }, |
| 60 | Flavour { name: "pal411", size: "720x576", rate: "25", pix_fmt: "yuv411p", audio: true, frames: 6 }, |
| 61 | Flavour { name: "ntsc422", size: "720x480", rate: "30000/1001", pix_fmt: "yuv422p", audio: true, frames: 6 }, |
| 62 | Flavour { name: "pal422", size: "720x576", rate: "25", pix_fmt: "yuv422p", audio: true, frames: 6 }, |
| 63 | Flavour { name: "hd1080i50", size: "1440x1080", rate: "25", pix_fmt: "yuv422p", audio: true, frames: 4 }, |
| 64 | Flavour { name: "hd1080i60", size: "1280x1080", rate: "30000/1001", pix_fmt: "yuv422p", audio: true, frames: 4 }, |
| 65 | Flavour { name: "hd720p50", size: "960x720", rate: "50", pix_fmt: "yuv422p", audio: false, frames: 6 }, |
| 66 | Flavour { name: "hd720p60", size: "960x720", rate: "60000/1001", pix_fmt: "yuv422p", audio: false, frames: 6 }, |
| 67 | ]; |
| 68 | |
| 69 | |
| 70 | |
| 71 | pub fn make(f: &Flavour, tag: &str, format: &str, ilme: bool, noise: u32) -> Option<PathBuf> { |
| 72 | let dir = scratch(f.name); |
| 73 | let path = dir.join(format!("{tag}.{format}")); |
| 74 | let vsrc = format!("testsrc2=size={}:rate={}:duration=3,noise=alls={noise}:allf=t", f.size, f.rate); |
| 75 | let frames = f.frames.to_string(); |
| 76 | let mut args: Vec<&str> = vec!["-f", "lavfi", "-i", &vsrc]; |
| 77 | let asrc = "sine=frequency=440:sample_rate=48000:duration=3"; |
| 78 | if f.audio { |
| 79 | args.extend(["-f", "lavfi", "-i", asrc, "-ac", "2"]); |
| 80 | } else { |
| 81 | args.push("-an"); |
| 82 | } |
| 83 | args.extend(["-frames:v", &frames, "-c:v", "dvvideo", "-pix_fmt", f.pix_fmt]); |
| 84 | if ilme { |
| 85 | args.extend(["-flags", "+ildct+ilme"]); |
| 86 | } |
| 87 | if f.audio { |
| 88 | args.extend(["-c:a", "pcm_s16le", "-ar", "48000", "-shortest"]); |
| 89 | } |
| 90 | args.extend(["-f", format]); |
| 91 | let p = path.to_str().unwrap().to_string(); |
| 92 | args.push(&p); |
| 93 | if run(&args) { Some(path) } else { None } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | pub fn ffmpeg_raw_video(path: &Path, extra: &[&str]) -> Vec<u8> { |
| 98 | let out = path.with_extension("ref.yuv"); |
| 99 | let p = path.to_str().unwrap(); |
| 100 | let o = out.to_str().unwrap(); |
| 101 | let mut args: Vec<&str> = extra.to_vec(); |
| 102 | args.extend(["-i", p, "-map", "0:v:0", "-f", "rawvideo", o]); |
| 103 | assert!(run(&args), "ffmpeg decode failed"); |
| 104 | std::fs::read(&out).unwrap() |
| 105 | } |
| 106 | |
| 107 | |
| 108 | pub fn ffmpeg_pcm(path: &Path, n: usize) -> Option<Vec<u8>> { |
| 109 | let out = path.with_extension(format!("ref{n}.pcm")); |
| 110 | let m = format!("0:a:{n}"); |
| 111 | if !run(&["-i", path.to_str().unwrap(), "-map", &m, "-f", "s16le", out.to_str().unwrap()]) { |
| 112 | return None; |
| 113 | } |
| 114 | std::fs::read(&out).ok() |
| 115 | } |