| 1 | |
| 2 | |
| 3 | |
| 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::process::Command; |
| 6 | |
| 7 | use ec_core::frame::{PixelFormat, VideoFrame}; |
| 8 | |
| 9 | |
| 10 | pub(super) fn frame_bytes(frame: &VideoFrame) -> Vec<u8> { |
| 11 | let bytes_per_sample = match frame.format { |
| 12 | PixelFormat::I422 => 1, |
| 13 | PixelFormat::I210 => 2, |
| 14 | other => panic!("unexpected output format {other:?}"), |
| 15 | }; |
| 16 | let (w, h) = (frame.width as usize, frame.height as usize); |
| 17 | let dims = [(w, h), (w.div_ceil(2), h), (w.div_ceil(2), h)]; |
| 18 | let mut out = Vec::new(); |
| 19 | for (plane, (pw, ph)) in frame.planes.iter().zip(dims) { |
| 20 | for y in 0..ph { |
| 21 | out.extend_from_slice(plane.row(y, pw * bytes_per_sample).expect("plane row")); |
| 22 | } |
| 23 | } |
| 24 | out |
| 25 | } |
| 26 | |
| 27 | pub(super) fn ffmpeg_frames(stream: &Path, pix_fmt: &str, frame_len: usize) -> Option<Vec<u8>> { |
| 28 | let out = Command::new("ffmpeg") |
| 29 | .args(["-v", "error", "-i"]) |
| 30 | .arg(stream) |
| 31 | .args(["-f", "rawvideo", "-pix_fmt", pix_fmt, "-"]) |
| 32 | .output() |
| 33 | .ok()?; |
| 34 | (out.status.success() && !out.stdout.is_empty() && out.stdout.len() % frame_len == 0) |
| 35 | .then_some(out.stdout) |
| 36 | } |
| 37 | |
| 38 | pub(super) fn escape(rbsp: &[u8]) -> Vec<u8> { |
| 39 | let mut out = Vec::with_capacity(rbsp.len() + 8); |
| 40 | let mut zeros = 0; |
| 41 | for &b in rbsp { |
| 42 | if zeros >= 2 && b <= 3 { |
| 43 | out.push(3); |
| 44 | zeros = 0; |
| 45 | } |
| 46 | out.push(b); |
| 47 | zeros = if b == 0 { zeros + 1 } else { 0 }; |
| 48 | } |
| 49 | out |
| 50 | } |
| 51 | |
| 52 | pub(super) fn find_stream(dir: &Path) -> Option<PathBuf> { |
| 53 | let mut fallback = None; |
| 54 | for entry in std::fs::read_dir(dir).ok()? { |
| 55 | let p = entry.ok()?.path(); |
| 56 | let ext = p.extension()?.to_str()?.to_ascii_lowercase(); |
| 57 | if matches!(ext.as_str(), "jsv" | "264" | "avc" | "26l" | "h264" | "jvt") { |
| 58 | return Some(p); |
| 59 | } |
| 60 | if ext == "bit" { |
| 61 | fallback = Some(p); |
| 62 | } |
| 63 | } |
| 64 | fallback |
| 65 | } |
| 66 | |
| 67 | |
| 68 | pub(super) fn layout(f: PixelFormat) -> (usize, usize, usize) { |
| 69 | match f { |
| 70 | PixelFormat::I444 => (1, 1, 1), |
| 71 | PixelFormat::I410 => (2, 1, 1), |
| 72 | PixelFormat::I422 => (1, 2, 1), |
| 73 | PixelFormat::I210 => (2, 2, 1), |
| 74 | PixelFormat::I420 => (1, 2, 2), |
| 75 | PixelFormat::I010 => (2, 2, 2), |
| 76 | other => panic!("unexpected output format {other:?}"), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | pub(super) fn ffmpeg_pix_frames(stream: &Path, pix_fmt: &str) -> Option<Vec<u8>> { |
| 81 | let out = Command::new("ffmpeg") |
| 82 | .args(["-v", "error", "-f", "h264", "-i"]) |
| 83 | .arg(stream) |
| 84 | .args(["-f", "rawvideo", "-pix_fmt", pix_fmt, "-"]) |
| 85 | .output() |
| 86 | .ok()?; |
| 87 | (out.status.success() && !out.stdout.is_empty()).then_some(out.stdout) |
| 88 | } |