| 1 | |
| 2 | #![allow(dead_code)] |
| 3 | |
| 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::process::Command; |
| 6 | |
| 7 | use ec_core::Demuxer; |
| 8 | use ec_core::frame::VideoFrame; |
| 9 | |
| 10 | pub fn require() -> bool { |
| 11 | std::env::var_os("EC_REQUIRE_FIXTURES").is_some() |
| 12 | } |
| 13 | |
| 14 | pub fn have_ffmpeg() -> bool { |
| 15 | Command::new("ffmpeg") |
| 16 | .arg("-version") |
| 17 | .output() |
| 18 | .map(|o| o.status.success()) |
| 19 | .unwrap_or(false) |
| 20 | } |
| 21 | |
| 22 | |
| 23 | pub fn can_run() -> bool { |
| 24 | if have_ffmpeg() { |
| 25 | return true; |
| 26 | } |
| 27 | assert!(!require(), "ffmpeg is required (EC_REQUIRE_FIXTURES set)"); |
| 28 | eprintln!("skipping: no ffmpeg"); |
| 29 | false |
| 30 | } |
| 31 | |
| 32 | pub fn scratch(tag: &str) -> PathBuf { |
| 33 | let base = std::env::var_os("TMPDIR") |
| 34 | .map(PathBuf::from) |
| 35 | .unwrap_or_else(std::env::temp_dir); |
| 36 | let dir = base.join(format!("ec-dnxhd-{}-{}", std::process::id(), tag)); |
| 37 | let _ = std::fs::create_dir_all(&dir); |
| 38 | dir |
| 39 | } |
| 40 | |
| 41 | pub fn ffmpeg(args: &[&str]) -> bool { |
| 42 | let out = Command::new("ffmpeg") |
| 43 | .args(["-v", "error", "-y", "-nostdin"]) |
| 44 | .args(args) |
| 45 | .output(); |
| 46 | match out { |
| 47 | Ok(o) if o.status.success() => true, |
| 48 | Ok(o) => { |
| 49 | eprintln!("ffmpeg {args:?}: {}", String::from_utf8_lossy(&o.stderr)); |
| 50 | false |
| 51 | } |
| 52 | Err(_) => false, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | |
| 57 | pub fn mov_frames(path: &Path) -> Vec<Vec<u8>> { |
| 58 | let f = std::io::BufReader::new(std::fs::File::open(path).unwrap()); |
| 59 | let mut d = ec_mp4::Mp4Demuxer::new(f).unwrap(); |
| 60 | assert_eq!(d.streams()[0].params.codec, ec_core::registry::CodecId::Dnxhd); |
| 61 | let mut out = Vec::new(); |
| 62 | while let Ok(p) = d.next_packet() { |
| 63 | out.push(p.data.to_vec()); |
| 64 | } |
| 65 | out |
| 66 | } |
| 67 | |
| 68 | pub fn mkv_frames(path: &Path) -> Vec<Vec<u8>> { |
| 69 | let f = std::io::BufReader::new(std::fs::File::open(path).unwrap()); |
| 70 | let mut d = ec_matroska::MatroskaDemuxer::new(f).unwrap(); |
| 71 | assert_eq!(d.streams()[0].params.codec, ec_core::registry::CodecId::Dnxhd); |
| 72 | let mut out = Vec::new(); |
| 73 | while let Ok(p) = d.next_packet() { |
| 74 | out.push(p.data.to_vec()); |
| 75 | } |
| 76 | out |
| 77 | } |
| 78 | |
| 79 | pub fn avi_frames(path: &Path) -> Vec<Vec<u8>> { |
| 80 | let f = std::io::BufReader::new(std::fs::File::open(path).unwrap()); |
| 81 | let mut d = ec_riff::AviDemuxer::new(f).unwrap(); |
| 82 | assert_eq!(d.streams()[0].params.codec, ec_core::registry::CodecId::Dnxhd); |
| 83 | let mut out = Vec::new(); |
| 84 | while let Ok(p) = d.next_packet() { |
| 85 | out.push(p.data.to_vec()); |
| 86 | } |
| 87 | out |
| 88 | } |
| 89 | |
| 90 | |
| 91 | pub fn frame_bytes(f: &VideoFrame) -> Vec<u8> { |
| 92 | let mut v = Vec::new(); |
| 93 | for (p, (row, rows)) in f.planes.iter().zip(f.format.plane_geometry(f.width, f.height)) { |
| 94 | for y in 0..rows { |
| 95 | v.extend_from_slice(&p.data[y * p.stride..y * p.stride + row]); |
| 96 | } |
| 97 | } |
| 98 | v |
| 99 | } |