forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

common.rs96 lines, 3.4 KiBHistoryRawDownload
1//! Minimal IVF demuxing for the ec-vp8 test fixtures (RFC's companion
2//! container; only what the witnesses need).
3
4/// One IVF frame: (pts, payload).
5pub struct IvfFrame {
6 /// Presentation timestamp from the IVF frame header.
7 #[allow(dead_code)]
8 pub pts: u64,
9 /// The compressed VP8 frame payload.
10 #[allow(dead_code)] // pts-only consumers exist in the stream witnesses
11 pub data: Vec<u8>,
12}
13
14/// The frame tag's `show_frame` bit (RFC 6386 §9.1): hidden frames
15/// (`false`) update the reference buffers but are never displayed.
16#[allow(dead_code)] // not every witness binary needs the census
17pub fn show_frame(data: &[u8]) -> bool {
18 (data[0] >> 4) & 1 == 1
19}
20
21/// Parse an IVF file into (fourcc, width, height, frames).
22pub fn parse_ivf(bytes: &[u8]) -> (&str, u16, u16, Vec<IvfFrame>) {
23 assert_eq!(&bytes[0..4], b"DKIF", "not an IVF file");
24 let hdr_len = u16::from_le_bytes([bytes[6], bytes[7]]) as usize;
25 let fourcc = std::str::from_utf8(&bytes[8..12]).unwrap();
26 let width = u16::from_le_bytes([bytes[12], bytes[13]]);
27 let height = u16::from_le_bytes([bytes[14], bytes[15]]);
28 let mut pos = hdr_len;
29 let mut frames = Vec::new();
30 while pos + 12 <= bytes.len() {
31 let sz = u32::from_le_bytes([bytes[pos], bytes[pos + 1], bytes[pos + 2], bytes[pos + 3]])
32 as usize;
33 let pts = u64::from_le_bytes([
34 bytes[pos + 4],
35 bytes[pos + 5],
36 bytes[pos + 6],
37 bytes[pos + 7],
38 bytes[pos + 8],
39 bytes[pos + 9],
40 bytes[pos + 10],
41 bytes[pos + 11],
42 ]);
43 pos += 12;
44 if pos + sz > bytes.len() {
45 break;
46 }
47 frames.push(IvfFrame {
48 pts,
49 data: bytes[pos..pos + sz].to_vec(),
50 });
51 pos += sz;
52 }
53 (fourcc, width, height, frames)
54}
55
56/// Extract the VP8 payload from a lossy WebP file (RIFF/WEBP with a VP8
57/// chunk) — the same VP8 key-frame bytes in a different container.
58#[allow(dead_code)] // only the keyframe_exact binary exercises WebP
59pub fn webp_vp8_chunk(bytes: &[u8]) -> Vec<u8> {
60 assert_eq!(&bytes[0..4], b"RIFF");
61 assert_eq!(&bytes[8..12], b"WEBP");
62 let mut pos = 12;
63 while pos + 8 <= bytes.len() {
64 let tag = &bytes[pos..pos + 4];
65 let sz = u32::from_le_bytes([
66 bytes[pos + 4],
67 bytes[pos + 5],
68 bytes[pos + 6],
69 bytes[pos + 7],
70 ]) as usize;
71 if tag == b"VP8 " {
72 return bytes[pos + 8..pos + 8 + sz].to_vec();
73 }
74 pos += 8 + sz + (sz & 1); // chunks are word-aligned
75 }
76 panic!("no VP8 chunk in webp");
77}
78
79/// Root of the workspace checkout this test runs in (crates/ec-vp8/../..).
80pub fn fixture_dir() -> Option<std::path::PathBuf> {
81 let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/vp8");
82 if dir.is_dir() { Some(dir) } else { None }
83}
84
85/// ffmpeg's decoded rawvideo byte stream for an IVF file.
86#[allow(dead_code)] // the malformed-input binary does not compare against ffmpeg
87pub fn ffmpeg_raw_yuv(path: &std::path::Path) -> Vec<u8> {
88 let out = std::process::Command::new("ffmpeg")
89 .args(["-v", "error", "-i"])
90 .arg(path)
91 .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"])
92 .output()
93 .expect("ffmpeg must be on PATH for the witnesses");
94 assert!(out.status.success(), "ffmpeg failed on {path:?}");
95 out.stdout
96}