forge
Explore
Log in

Don't you dare go Hollow

U6: ec-h264 tests/it: frame_bytes, decode_all, scratch and ffmpeg_frames live once in common.rs (layout-driven, one copy per helper; same bytes, same temp dir prefixes per module)

Browse files at this commit

d297f650afd5cc30fad47d6119843d12732ffd8eAhmet Kaan GümüşParent: a38a1c4
crates/ec-h264/tests/it/common.rsmodified+50 -24
@@ -1,28 +1,67 @@
1-//! Helpers shared by the merged integration-test modules: byte layouts, the
2-//! ffmpeg pipes and stream utilities that were identical in each file.
1+//! Helpers shared by the integration-test modules: byte layouts, the ffmpeg
2+//! pipes, the decode loop and stream utilities that were identical in each file.
33
44 use std::path::{Path, PathBuf};
55
66 use ec_core::frame::{PixelFormat, VideoFrame};
7+use ec_h264::{Decoder, NalOutcome};
78
8-/// One decoded frame as planar bytes (16-bit little endian samples for High 10).
9-pub(super) fn frame_bytes(frame: &VideoFrame) -> Vec<u8> {
10- let bytes_per_sample = match frame.format {
11- PixelFormat::I422 => 1,
12- PixelFormat::I210 => 2,
9+/// (bytes per sample, chroma width divisor, chroma height divisor)
10+pub(super) fn layout(f: PixelFormat) -> (usize, usize, usize) {
11+ match f {
12+ PixelFormat::I444 => (1, 1, 1),
13+ PixelFormat::I410 => (2, 1, 1),
14+ PixelFormat::I422 => (1, 2, 1),
15+ PixelFormat::I210 => (2, 2, 1),
16+ PixelFormat::I420 => (1, 2, 2),
17+ PixelFormat::I010 => (2, 2, 2),
1318 other => panic!("unexpected output format {other:?}"),
14- };
19+ }
20+}
21+
22+/// One decoded frame as planar bytes (16-bit little endian above 8 bits).
23+pub(super) fn frame_bytes(frame: &VideoFrame) -> Vec<u8> {
24+ let (bps, dx, dy) = layout(frame.format);
1525 let (w, h) = (frame.width as usize, frame.height as usize);
16- let dims = [(w, h), (w.div_ceil(2), h), (w.div_ceil(2), h)];
26+ let (cw, ch) = (w.div_ceil(dx), h.div_ceil(dy));
1727 let mut out = Vec::new();
18- for (plane, (pw, ph)) in frame.planes.iter().zip(dims) {
28+ for (plane, (pw, ph)) in frame.planes.iter().zip([(w, h), (cw, ch), (cw, ch)]) {
1929 for y in 0..ph {
20- out.extend_from_slice(plane.row(y, pw * bytes_per_sample).expect("plane row"));
30+ out.extend_from_slice(plane.row(y, pw * bps).expect("plane row"));
2131 }
2232 }
2333 out
2434 }
2535
36+pub(super) fn decode_all(bytes: &[u8]) -> Result<Vec<Vec<u8>>, ec_h264::Error> {
37+ let mut dec = Decoder::new();
38+ let mut frames = Vec::new();
39+ for nal in ec_h264_syntax::AnnexBIter::new(bytes) {
40+ if dec.push_nal(nal)? == NalOutcome::PictureBoundary {
41+ dec.end_picture()?;
42+ dec.push_nal(nal)?;
43+ }
44+ while let Some(f) = dec.next_frame() {
45+ frames.push(frame_bytes(&f));
46+ }
47+ }
48+ dec.flush()?;
49+ while let Some(f) = dec.next_frame() {
50+ frames.push(frame_bytes(&f));
51+ }
52+ Ok(frames)
53+}
54+
55+pub(super) fn scratch(tag: &str) -> PathBuf {
56+ let dir = std::env::temp_dir().join(format!(
57+ "ec-h264-{tag}-{}-{:?}",
58+ std::process::id(),
59+ std::thread::current().id()
60+ ));
61+ std::fs::create_dir_all(&dir).unwrap();
62+ dir
63+}
64+
2665 pub(super) fn ffmpeg_frames(stream: &Path, pix_fmt: &str, frame_len: usize) -> Option<Vec<u8>> {
2766 let out = ec_testkit::ffmpeg()
2867 .args(["-v", "error", "-i"])
@@ -63,19 +102,6 @@ pub(super) fn find_stream(dir: &Path) -> Option<PathBuf> {
63102 fallback
64103 }
65104
66-/// (bytes per sample, chroma width divisor, chroma height divisor)
67-pub(super) fn layout(f: PixelFormat) -> (usize, usize, usize) {
68- match f {
69- PixelFormat::I444 => (1, 1, 1),
70- PixelFormat::I410 => (2, 1, 1),
71- PixelFormat::I422 => (1, 2, 1),
72- PixelFormat::I210 => (2, 2, 1),
73- PixelFormat::I420 => (1, 2, 2),
74- PixelFormat::I010 => (2, 2, 2),
75- other => panic!("unexpected output format {other:?}"),
76- }
77-}
78-
79105 pub(super) fn ffmpeg_pix_frames(stream: &Path, pix_fmt: &str) -> Option<Vec<u8>> {
80106 let out = ec_testkit::ffmpeg()
81107 .args(["-v", "error", "-f", "h264", "-i"])
crates/ec-h264/tests/it/high10.rsmodified+5 -64
@@ -8,57 +8,7 @@
88
99 use std::path::{Path, PathBuf};
1010
11-use ec_core::frame::{PixelFormat, VideoFrame};
12-use ec_h264::{Decoder, NalOutcome};
13-
14-/// One decoded frame as I010 bytes (little-endian 16-bit samples, planes
15-/// back to back), checking the format and plane geometry on the way.
16-fn frame_bytes(frame: &VideoFrame) -> Vec<u8> {
17- assert_eq!(frame.format, PixelFormat::I010, "High 10 comes out as I010");
18- let (w, h) = (frame.width as usize, frame.height as usize);
19- let dims = [
20- (w, h),
21- (w.div_ceil(2), h.div_ceil(2)),
22- (w.div_ceil(2), h.div_ceil(2)),
23- ];
24- let mut out = Vec::new();
25- for (plane, (pw, ph)) in frame.planes.iter().zip(dims) {
26- for y in 0..ph {
27- out.extend_from_slice(plane.row(y, pw * 2).expect("plane row"));
28- }
29- }
30- out
31-}
32-
33-fn decode_all(bytes: &[u8]) -> Result<Vec<Vec<u8>>, ec_h264::Error> {
34- let mut dec = Decoder::new();
35- let mut frames = Vec::new();
36- for nal in ec_h264_syntax::AnnexBIter::new(bytes) {
37- if dec.push_nal(nal)? == NalOutcome::PictureBoundary {
38- dec.end_picture()?;
39- dec.push_nal(nal)?;
40- }
41- while let Some(f) = dec.next_frame() {
42- frames.push(frame_bytes(&f));
43- }
44- }
45- dec.flush()?;
46- while let Some(f) = dec.next_frame() {
47- frames.push(frame_bytes(&f));
48- }
49- Ok(frames)
50-}
51-
52-fn ffmpeg_frames(stream: &Path, frame_len: usize) -> Option<Vec<u8>> {
53- let out = ec_testkit::ffmpeg()
54- .args(["-v", "error", "-i"])
55- .arg(stream)
56- .args(["-f", "rawvideo", "-pix_fmt", "yuv420p10le", "-"])
57- .output()
58- .ok()?;
59- (out.status.success() && !out.stdout.is_empty() && out.stdout.len() % frame_len == 0)
60- .then_some(out.stdout)
61-}
11+use super::common::{decode_all, ffmpeg_frames, frame_bytes, scratch};
6212
6313 // ---- MD5 (RFC 1321), so the committed fixtures need no ffmpeg ----------------
6414
@@ -142,16 +92,6 @@ fn committed_streams_match_ffmpeg_md5() {
14292
14393 // ---- the libx264 sweep --------------------------------------------------------
14494
145-fn scratch() -> PathBuf {
146- let dir = std::env::temp_dir().join(format!(
147- "ec-h264-high10-{}-{:?}",
148- std::process::id(),
149- std::thread::current().id()
150- ));
151- std::fs::create_dir_all(&dir).unwrap();
152- dir
153-}
154-
15595 struct Case {
15696 tag: &'static str,
15797 /// lavfi source and size.
@@ -203,13 +143,14 @@ fn encode(dir: &Path, c: &Case) -> Option<PathBuf> {
203143 }
204144
205145 fn check(c: &Case) -> Result<usize, String> {
206- let dir = scratch();
146+ let dir = scratch("high10");
207147 let stream =
208148 encode(&dir, c).unwrap_or_else(|| panic!("{}: ffmpeg/libx264 10-bit cannot encode", c.tag));
209149 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;
210150 let ours = decode_all(&bytes).map_err(|e| format!("{}: {e}", c.tag))?;
211151 let first = ours.first().ok_or("no frames decoded")?;
212- let theirs = ffmpeg_frames(&stream, first.len()).ok_or("ffmpeg cannot decode")?;
152+ let theirs =
153+ ffmpeg_frames(&stream, "yuv420p10le", first.len()).ok_or("ffmpeg cannot decode")?;
213154 let _ = std::fs::remove_file(&stream);
214155 if theirs.len() != first.len() * ours.len() {
215156 return Err(format!(
@@ -242,7 +183,7 @@ fn sweep(cases: &[Case]) {
242183 Err(e) => failed.push(e),
243184 }
244185 }
245- let _ = std::fs::remove_dir_all(scratch());
186+ let _ = std::fs::remove_dir_all(scratch("high10"));
246187 assert!(
247188 failed.is_empty(),
248189 "High 10 mismatches:\n{}",
crates/ec-h264/tests/it/high422.rsmodified+3 -34
@@ -6,38 +6,7 @@
66
77 use std::path::{Path, PathBuf};
88
9-use ec_h264::{Decoder, NalOutcome};
10-
11-use super::common::{escape, ffmpeg_frames, frame_bytes};
12-
13-fn decode_all(bytes: &[u8]) -> Result<Vec<Vec<u8>>, ec_h264::Error> {
14- let mut dec = Decoder::new();
15- let mut frames = Vec::new();
16- for nal in ec_h264_syntax::AnnexBIter::new(bytes) {
17- if dec.push_nal(nal)? == NalOutcome::PictureBoundary {
18- dec.end_picture()?;
19- dec.push_nal(nal)?;
20- }
21- while let Some(f) = dec.next_frame() {
22- frames.push(frame_bytes(&f));
23- }
24- }
25- dec.flush()?;
26- while let Some(f) = dec.next_frame() {
27- frames.push(frame_bytes(&f));
28- }
29- Ok(frames)
30-}
31-
32-fn scratch() -> PathBuf {
33- let dir = std::env::temp_dir().join(format!(
34- "ec-h264-hi422-{}-{:?}",
35- std::process::id(),
36- std::thread::current().id()
37- ));
38- std::fs::create_dir_all(&dir).unwrap();
39- dir
40-}
9+use super::common::{decode_all, escape, ffmpeg_frames, scratch};
4110
4211 struct Case {
4312 tag: &'static str,
@@ -92,7 +61,7 @@ fn encode(dir: &Path, c: &Case) -> Option<PathBuf> {
9261 }
9362
9463 fn check(c: &Case) -> Result<usize, String> {
95- let dir = scratch();
64+ let dir = scratch("hi422");
9665 let stream = encode(&dir, c)
9766 .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag));
9867 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;
@@ -132,7 +101,7 @@ fn sweep(cases: &[Case]) {
132101 Err(e) => failed.push(e),
133102 }
134103 }
135- let _ = std::fs::remove_dir_all(scratch());
104+ let _ = std::fs::remove_dir_all(scratch("hi422"));
136105 assert!(
137106 failed.is_empty(),
138107 "4:2:2 mismatches:\n{}",
crates/ec-h264/tests/it/high444.rsmodified+3 -13
@@ -15,7 +15,7 @@ use std::path::{Path, PathBuf};
1515 use ec_core::frame::{PixelFormat, VideoFrame};
1616 use ec_h264::{Decoder, NalOutcome};
1717
18-use super::common::{ffmpeg_pix_frames, layout};
18+use super::common::{ffmpeg_pix_frames, layout, scratch};
1919
2020 /// One decoded frame as planar bytes (16-bit little endian above 8 bits).
2121 fn frame_bytes(frame: &VideoFrame) -> (Vec<u8>, PixelFormat) {
@@ -50,16 +50,6 @@ fn decode_all(bytes: &[u8]) -> Result<Vec<(Vec<u8>, PixelFormat)>, ec_h264::Erro
5050 Ok(frames)
5151 }
5252
53-fn scratch() -> PathBuf {
54- let dir = std::env::temp_dir().join(format!(
55- "ec-h264-hi444-{}-{:?}",
56- std::process::id(),
57- std::thread::current().id()
58- ));
59- std::fs::create_dir_all(&dir).unwrap();
60- dir
61-}
62-
6353 #[derive(Clone, Copy)]
6454 struct Case {
6555 tag: &'static str,
@@ -114,7 +104,7 @@ fn encode(dir: &Path, c: &Case) -> Option<PathBuf> {
114104 }
115105
116106 fn check(c: &Case) -> Result<usize, String> {
117- let dir = scratch();
107+ let dir = scratch("hi444");
118108 let stream = encode(&dir, c)
119109 .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag));
120110 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;
@@ -186,7 +176,7 @@ fn sweep(label: &str, cases: &[Case]) {
186176 Err(e) => failed.push(e),
187177 }
188178 }
189- let _ = std::fs::remove_dir_all(scratch());
179+ let _ = std::fs::remove_dir_all(scratch("hi444"));
190180 assert!(
191181 failed.is_empty(),
192182 "{label} mismatches:\n{}",
crates/ec-h264/tests/it/mbaff_x264.rsmodified+3 -57
@@ -6,61 +6,7 @@
66
77 use std::path::{Path, PathBuf};
88
9-use ec_core::frame::{PixelFormat, VideoFrame};
10-use ec_h264::{Decoder, NalOutcome};
11-
12-use super::common::ffmpeg_frames;
13-
14-/// One decoded frame as planar bytes (16-bit little endian samples for High 10).
15-fn frame_bytes(frame: &VideoFrame) -> Vec<u8> {
16- let bytes_per_sample = match frame.format {
17- PixelFormat::I420 => 1,
18- PixelFormat::I010 => 2,
19- other => panic!("unexpected output format {other:?}"),
20- };
21- let (w, h) = (frame.width as usize, frame.height as usize);
22- let dims = [
23- (w, h),
24- (w.div_ceil(2), h.div_ceil(2)),
25- (w.div_ceil(2), h.div_ceil(2)),
26- ];
27- let mut out = Vec::new();
28- for (plane, (pw, ph)) in frame.planes.iter().zip(dims) {
29- for y in 0..ph {
30- out.extend_from_slice(plane.row(y, pw * bytes_per_sample).expect("plane row"));
31- }
32- }
33- out
34-}
35-
36-fn decode_all(bytes: &[u8]) -> Result<Vec<Vec<u8>>, ec_h264::Error> {
37- let mut dec = Decoder::new();
38- let mut frames = Vec::new();
39- for nal in ec_h264_syntax::AnnexBIter::new(bytes) {
40- if dec.push_nal(nal)? == NalOutcome::PictureBoundary {
41- dec.end_picture()?;
42- dec.push_nal(nal)?;
43- }
44- while let Some(f) = dec.next_frame() {
45- frames.push(frame_bytes(&f));
46- }
47- }
48- dec.flush()?;
49- while let Some(f) = dec.next_frame() {
50- frames.push(frame_bytes(&f));
51- }
52- Ok(frames)
53-}
54-
55-fn scratch() -> PathBuf {
56- let dir = std::env::temp_dir().join(format!(
57- "ec-h264-mbaff-{}-{:?}",
58- std::process::id(),
59- std::thread::current().id()
60- ));
61- std::fs::create_dir_all(&dir).unwrap();
62- dir
63-}
9+use super::common::{decode_all, ffmpeg_frames, scratch};
6410
6511 struct Case {
6612 tag: &'static str,
@@ -105,7 +51,7 @@ fn encode(dir: &Path, c: &Case) -> Option<PathBuf> {
10551 }
10652
10753 fn check(c: &Case) -> Result<usize, String> {
108- let dir = scratch();
54+ let dir = scratch("mbaff");
10955 let stream = encode(&dir, c)
11056 .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag));
11157 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;
@@ -145,7 +91,7 @@ fn sweep(cases: &[Case]) {
14591 Err(e) => failed.push(e),
14692 }
14793 }
148- let _ = std::fs::remove_dir_all(scratch());
94+ let _ = std::fs::remove_dir_all(scratch("mbaff"));
14995 assert!(
15096 failed.is_empty(),
15197 "MBAFF mismatches:\n{}",