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, the1+//! Helpers shared by the integration-test modules: byte layouts, the ffmpeg
2-//! ffmpeg pipes and stream utilities that were identical in each file.2+//! pipes, the decode loop and stream utilities that were identical in each file.
3 3
4 use std::path::{Path, PathBuf};4 use std::path::{Path, PathBuf};
5 5
6 use ec_core::frame::{PixelFormat, VideoFrame};6 use ec_core::frame::{PixelFormat, VideoFrame};
7+use ec_h264::{Decoder, NalOutcome};
7 8
8-/// One decoded frame as planar bytes (16-bit little endian samples for High 10).9+/// (bytes per sample, chroma width divisor, chroma height divisor)
9-pub(super) fn frame_bytes(frame: &VideoFrame) -> Vec<u8> {10+pub(super) fn layout(f: PixelFormat) -> (usize, usize, usize) {
10- let bytes_per_sample = match frame.format {11+ match f {
11- PixelFormat::I422 => 1,12+ PixelFormat::I444 => (1, 1, 1),
12- PixelFormat::I210 => 2,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),
13 other => panic!("unexpected output format {other:?}"),18 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);
15 let (w, h) = (frame.width as usize, frame.height as usize);25 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));
17 let mut out = Vec::new();27 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)]) {
19 for y in 0..ph {29 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"));
21 }31 }
22 }32 }
23 out33 out
24 }34 }
25 35
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+
26 pub(super) fn ffmpeg_frames(stream: &Path, pix_fmt: &str, frame_len: usize) -> Option<Vec<u8>> {65 pub(super) fn ffmpeg_frames(stream: &Path, pix_fmt: &str, frame_len: usize) -> Option<Vec<u8>> {
27 let out = ec_testkit::ffmpeg()66 let out = ec_testkit::ffmpeg()
28 .args(["-v", "error", "-i"])67 .args(["-v", "error", "-i"])
@@ -63,19 +102,6 @@ pub(super) fn find_stream(dir: &Path) -> Option<PathBuf> {
63 fallback102 fallback
64 }103 }
65 104
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-
79 pub(super) fn ffmpeg_pix_frames(stream: &Path, pix_fmt: &str) -> Option<Vec<u8>> {105 pub(super) fn ffmpeg_pix_frames(stream: &Path, pix_fmt: &str) -> Option<Vec<u8>> {
80 let out = ec_testkit::ffmpeg()106 let out = ec_testkit::ffmpeg()
81 .args(["-v", "error", "-f", "h264", "-i"])107 .args(["-v", "error", "-f", "h264", "-i"])
crates/ec-h264/tests/it/high10.rsmodified+5 -64
@@ -8,57 +8,7 @@
8 8
9 use std::path::{Path, PathBuf};9 use std::path::{Path, PathBuf};
10 10
11-use ec_core::frame::{PixelFormat, VideoFrame};11+use super::common::{decode_all, ffmpeg_frames, frame_bytes, scratch};
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-}
62 12
63 // ---- MD5 (RFC 1321), so the committed fixtures need no ffmpeg ----------------13 // ---- MD5 (RFC 1321), so the committed fixtures need no ffmpeg ----------------
64 14
@@ -142,16 +92,6 @@ fn committed_streams_match_ffmpeg_md5() {
142 92
143 // ---- the libx264 sweep --------------------------------------------------------93 // ---- the libx264 sweep --------------------------------------------------------
144 94
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-
155 struct Case {95 struct Case {
156 tag: &'static str,96 tag: &'static str,
157 /// lavfi source and size.97 /// lavfi source and size.
@@ -203,13 +143,14 @@ fn encode(dir: &Path, c: &Case) -> Option<PathBuf> {
203 }143 }
204 144
205 fn check(c: &Case) -> Result<usize, String> {145 fn check(c: &Case) -> Result<usize, String> {
206- let dir = scratch();146+ let dir = scratch("high10");
207 let stream =147 let stream =
208 encode(&dir, c).unwrap_or_else(|| panic!("{}: ffmpeg/libx264 10-bit cannot encode", c.tag));148 encode(&dir, c).unwrap_or_else(|| panic!("{}: ffmpeg/libx264 10-bit cannot encode", c.tag));
209 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;149 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;
210 let ours = decode_all(&bytes).map_err(|e| format!("{}: {e}", c.tag))?;150 let ours = decode_all(&bytes).map_err(|e| format!("{}: {e}", c.tag))?;
211 let first = ours.first().ok_or("no frames decoded")?;151 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")?;
213 let _ = std::fs::remove_file(&stream);154 let _ = std::fs::remove_file(&stream);
214 if theirs.len() != first.len() * ours.len() {155 if theirs.len() != first.len() * ours.len() {
215 return Err(format!(156 return Err(format!(
@@ -242,7 +183,7 @@ fn sweep(cases: &[Case]) {
242 Err(e) => failed.push(e),183 Err(e) => failed.push(e),
243 }184 }
244 }185 }
245- let _ = std::fs::remove_dir_all(scratch());186+ let _ = std::fs::remove_dir_all(scratch("high10"));
246 assert!(187 assert!(
247 failed.is_empty(),188 failed.is_empty(),
248 "High 10 mismatches:\n{}",189 "High 10 mismatches:\n{}",
crates/ec-h264/tests/it/high422.rsmodified+3 -34
@@ -6,38 +6,7 @@
6 6
7 use std::path::{Path, PathBuf};7 use std::path::{Path, PathBuf};
8 8
9-use ec_h264::{Decoder, NalOutcome};9+use super::common::{decode_all, escape, ffmpeg_frames, scratch};
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-}
41 10
42 struct Case {11 struct Case {
43 tag: &'static str,12 tag: &'static str,
@@ -92,7 +61,7 @@ fn encode(dir: &Path, c: &Case) -> Option<PathBuf> {
92 }61 }
93 62
94 fn check(c: &Case) -> Result<usize, String> {63 fn check(c: &Case) -> Result<usize, String> {
95- let dir = scratch();64+ let dir = scratch("hi422");
96 let stream = encode(&dir, c)65 let stream = encode(&dir, c)
97 .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag));66 .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag));
98 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;67 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;
@@ -132,7 +101,7 @@ fn sweep(cases: &[Case]) {
132 Err(e) => failed.push(e),101 Err(e) => failed.push(e),
133 }102 }
134 }103 }
135- let _ = std::fs::remove_dir_all(scratch());104+ let _ = std::fs::remove_dir_all(scratch("hi422"));
136 assert!(105 assert!(
137 failed.is_empty(),106 failed.is_empty(),
138 "4:2:2 mismatches:\n{}",107 "4:2:2 mismatches:\n{}",
crates/ec-h264/tests/it/high444.rsmodified+3 -13
@@ -15,7 +15,7 @@ use std::path::{Path, PathBuf};
15 use ec_core::frame::{PixelFormat, VideoFrame};15 use ec_core::frame::{PixelFormat, VideoFrame};
16 use ec_h264::{Decoder, NalOutcome};16 use ec_h264::{Decoder, NalOutcome};
17 17
18-use super::common::{ffmpeg_pix_frames, layout};18+use super::common::{ffmpeg_pix_frames, layout, scratch};
19 19
20 /// One decoded frame as planar bytes (16-bit little endian above 8 bits).20 /// One decoded frame as planar bytes (16-bit little endian above 8 bits).
21 fn frame_bytes(frame: &VideoFrame) -> (Vec<u8>, PixelFormat) {21 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
50 Ok(frames)50 Ok(frames)
51 }51 }
52 52
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-
63 #[derive(Clone, Copy)]53 #[derive(Clone, Copy)]
64 struct Case {54 struct Case {
65 tag: &'static str,55 tag: &'static str,
@@ -114,7 +104,7 @@ fn encode(dir: &Path, c: &Case) -> Option<PathBuf> {
114 }104 }
115 105
116 fn check(c: &Case) -> Result<usize, String> {106 fn check(c: &Case) -> Result<usize, String> {
117- let dir = scratch();107+ let dir = scratch("hi444");
118 let stream = encode(&dir, c)108 let stream = encode(&dir, c)
119 .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag));109 .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag));
120 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;110 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;
@@ -186,7 +176,7 @@ fn sweep(label: &str, cases: &[Case]) {
186 Err(e) => failed.push(e),176 Err(e) => failed.push(e),
187 }177 }
188 }178 }
189- let _ = std::fs::remove_dir_all(scratch());179+ let _ = std::fs::remove_dir_all(scratch("hi444"));
190 assert!(180 assert!(
191 failed.is_empty(),181 failed.is_empty(),
192 "{label} mismatches:\n{}",182 "{label} mismatches:\n{}",
crates/ec-h264/tests/it/mbaff_x264.rsmodified+3 -57
@@ -6,61 +6,7 @@
6 6
7 use std::path::{Path, PathBuf};7 use std::path::{Path, PathBuf};
8 8
9-use ec_core::frame::{PixelFormat, VideoFrame};9+use super::common::{decode_all, ffmpeg_frames, scratch};
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-}
64 10
65 struct Case {11 struct Case {
66 tag: &'static str,12 tag: &'static str,
@@ -105,7 +51,7 @@ fn encode(dir: &Path, c: &Case) -> Option<PathBuf> {
105 }51 }
106 52
107 fn check(c: &Case) -> Result<usize, String> {53 fn check(c: &Case) -> Result<usize, String> {
108- let dir = scratch();54+ let dir = scratch("mbaff");
109 let stream = encode(&dir, c)55 let stream = encode(&dir, c)
110 .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag));56 .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag));
111 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;57 let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?;
@@ -145,7 +91,7 @@ fn sweep(cases: &[Case]) {
145 Err(e) => failed.push(e),91 Err(e) => failed.push(e),
146 }92 }
147 }93 }
148- let _ = std::fs::remove_dir_all(scratch());94+ let _ = std::fs::remove_dir_all(scratch("mbaff"));
149 assert!(95 assert!(
150 failed.is_empty(),96 failed.is_empty(),
151 "MBAFF mismatches:\n{}",97 "MBAFF mismatches:\n{}",