forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

common.rs88 lines, 2.9 KiBHistoryRawDownload
1//! Helpers shared by the merged integration-test modules: byte layouts, the
2//! ffmpeg pipes and stream utilities that were identical in each file.
3
4use std::path::{Path, PathBuf};
5use std::process::Command;
6
7use ec_core::frame::{PixelFormat, VideoFrame};
8
9/// One decoded frame as planar bytes (16-bit little endian samples for High 10).
10pub(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
27pub(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
38pub(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
52pub(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/// (bytes per sample, chroma width divisor, chroma height divisor)
68pub(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
80pub(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}