From d297f650afd5cc30fad47d6119843d12732ffd8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 13:41:48 +0300 Subject: [PATCH] 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) --- crates/ec-h264/tests/it/common.rs | 74 ++++++++++++++++++--------- crates/ec-h264/tests/it/high10.rs | 69 ++----------------------- crates/ec-h264/tests/it/high422.rs | 37 ++------------ crates/ec-h264/tests/it/high444.rs | 16 ++---- crates/ec-h264/tests/it/mbaff_x264.rs | 60 ++-------------------- 5 files changed, 64 insertions(+), 192 deletions(-) diff --git a/crates/ec-h264/tests/it/common.rs b/crates/ec-h264/tests/it/common.rs index bc2de8ce..f2c47987 100644 --- a/crates/ec-h264/tests/it/common.rs +++ b/crates/ec-h264/tests/it/common.rs @@ -1,28 +1,67 @@ -//! Helpers shared by the merged integration-test modules: byte layouts, the -//! ffmpeg pipes and stream utilities that were identical in each file. +//! Helpers shared by the integration-test modules: byte layouts, the ffmpeg +//! pipes, the decode loop and stream utilities that were identical in each file. use std::path::{Path, PathBuf}; use ec_core::frame::{PixelFormat, VideoFrame}; +use ec_h264::{Decoder, NalOutcome}; -/// One decoded frame as planar bytes (16-bit little endian samples for High 10). -pub(super) fn frame_bytes(frame: &VideoFrame) -> Vec { - let bytes_per_sample = match frame.format { - PixelFormat::I422 => 1, - PixelFormat::I210 => 2, +/// (bytes per sample, chroma width divisor, chroma height divisor) +pub(super) fn layout(f: PixelFormat) -> (usize, usize, usize) { + match f { + PixelFormat::I444 => (1, 1, 1), + PixelFormat::I410 => (2, 1, 1), + PixelFormat::I422 => (1, 2, 1), + PixelFormat::I210 => (2, 2, 1), + PixelFormat::I420 => (1, 2, 2), + PixelFormat::I010 => (2, 2, 2), other => panic!("unexpected output format {other:?}"), - }; + } +} + +/// One decoded frame as planar bytes (16-bit little endian above 8 bits). +pub(super) fn frame_bytes(frame: &VideoFrame) -> Vec { + let (bps, dx, dy) = layout(frame.format); let (w, h) = (frame.width as usize, frame.height as usize); - let dims = [(w, h), (w.div_ceil(2), h), (w.div_ceil(2), h)]; + let (cw, ch) = (w.div_ceil(dx), h.div_ceil(dy)); let mut out = Vec::new(); - for (plane, (pw, ph)) in frame.planes.iter().zip(dims) { + for (plane, (pw, ph)) in frame.planes.iter().zip([(w, h), (cw, ch), (cw, ch)]) { for y in 0..ph { - out.extend_from_slice(plane.row(y, pw * bytes_per_sample).expect("plane row")); + out.extend_from_slice(plane.row(y, pw * bps).expect("plane row")); } } out } +pub(super) fn decode_all(bytes: &[u8]) -> Result>, ec_h264::Error> { + let mut dec = Decoder::new(); + let mut frames = Vec::new(); + for nal in ec_h264_syntax::AnnexBIter::new(bytes) { + if dec.push_nal(nal)? == NalOutcome::PictureBoundary { + dec.end_picture()?; + dec.push_nal(nal)?; + } + while let Some(f) = dec.next_frame() { + frames.push(frame_bytes(&f)); + } + } + dec.flush()?; + while let Some(f) = dec.next_frame() { + frames.push(frame_bytes(&f)); + } + Ok(frames) +} + +pub(super) fn scratch(tag: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!( + "ec-h264-{tag}-{}-{:?}", + std::process::id(), + std::thread::current().id() + )); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + pub(super) fn ffmpeg_frames(stream: &Path, pix_fmt: &str, frame_len: usize) -> Option> { let out = ec_testkit::ffmpeg() .args(["-v", "error", "-i"]) @@ -63,19 +102,6 @@ pub(super) fn find_stream(dir: &Path) -> Option { fallback } -/// (bytes per sample, chroma width divisor, chroma height divisor) -pub(super) fn layout(f: PixelFormat) -> (usize, usize, usize) { - match f { - PixelFormat::I444 => (1, 1, 1), - PixelFormat::I410 => (2, 1, 1), - PixelFormat::I422 => (1, 2, 1), - PixelFormat::I210 => (2, 2, 1), - PixelFormat::I420 => (1, 2, 2), - PixelFormat::I010 => (2, 2, 2), - other => panic!("unexpected output format {other:?}"), - } -} - pub(super) fn ffmpeg_pix_frames(stream: &Path, pix_fmt: &str) -> Option> { let out = ec_testkit::ffmpeg() .args(["-v", "error", "-f", "h264", "-i"]) diff --git a/crates/ec-h264/tests/it/high10.rs b/crates/ec-h264/tests/it/high10.rs index 57823585..1050fbdc 100644 --- a/crates/ec-h264/tests/it/high10.rs +++ b/crates/ec-h264/tests/it/high10.rs @@ -8,57 +8,7 @@ use std::path::{Path, PathBuf}; -use ec_core::frame::{PixelFormat, VideoFrame}; -use ec_h264::{Decoder, NalOutcome}; - -/// One decoded frame as I010 bytes (little-endian 16-bit samples, planes -/// back to back), checking the format and plane geometry on the way. -fn frame_bytes(frame: &VideoFrame) -> Vec { - assert_eq!(frame.format, PixelFormat::I010, "High 10 comes out as I010"); - let (w, h) = (frame.width as usize, frame.height as usize); - let dims = [ - (w, h), - (w.div_ceil(2), h.div_ceil(2)), - (w.div_ceil(2), h.div_ceil(2)), - ]; - let mut out = Vec::new(); - for (plane, (pw, ph)) in frame.planes.iter().zip(dims) { - for y in 0..ph { - out.extend_from_slice(plane.row(y, pw * 2).expect("plane row")); - } - } - out -} - -fn decode_all(bytes: &[u8]) -> Result>, ec_h264::Error> { - let mut dec = Decoder::new(); - let mut frames = Vec::new(); - for nal in ec_h264_syntax::AnnexBIter::new(bytes) { - if dec.push_nal(nal)? == NalOutcome::PictureBoundary { - dec.end_picture()?; - dec.push_nal(nal)?; - } - while let Some(f) = dec.next_frame() { - frames.push(frame_bytes(&f)); - } - } - dec.flush()?; - while let Some(f) = dec.next_frame() { - frames.push(frame_bytes(&f)); - } - Ok(frames) -} - -fn ffmpeg_frames(stream: &Path, frame_len: usize) -> Option> { - let out = ec_testkit::ffmpeg() - .args(["-v", "error", "-i"]) - .arg(stream) - .args(["-f", "rawvideo", "-pix_fmt", "yuv420p10le", "-"]) - .output() - .ok()?; - (out.status.success() && !out.stdout.is_empty() && out.stdout.len() % frame_len == 0) - .then_some(out.stdout) -} +use super::common::{decode_all, ffmpeg_frames, frame_bytes, scratch}; // ---- MD5 (RFC 1321), so the committed fixtures need no ffmpeg ---------------- @@ -142,16 +92,6 @@ fn committed_streams_match_ffmpeg_md5() { // ---- the libx264 sweep -------------------------------------------------------- -fn scratch() -> PathBuf { - let dir = std::env::temp_dir().join(format!( - "ec-h264-high10-{}-{:?}", - std::process::id(), - std::thread::current().id() - )); - std::fs::create_dir_all(&dir).unwrap(); - dir -} - struct Case { tag: &'static str, /// lavfi source and size. @@ -203,13 +143,14 @@ fn encode(dir: &Path, c: &Case) -> Option { } fn check(c: &Case) -> Result { - let dir = scratch(); + let dir = scratch("high10"); let stream = encode(&dir, c).unwrap_or_else(|| panic!("{}: ffmpeg/libx264 10-bit cannot encode", c.tag)); let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?; let ours = decode_all(&bytes).map_err(|e| format!("{}: {e}", c.tag))?; let first = ours.first().ok_or("no frames decoded")?; - let theirs = ffmpeg_frames(&stream, first.len()).ok_or("ffmpeg cannot decode")?; + let theirs = + ffmpeg_frames(&stream, "yuv420p10le", first.len()).ok_or("ffmpeg cannot decode")?; let _ = std::fs::remove_file(&stream); if theirs.len() != first.len() * ours.len() { return Err(format!( @@ -242,7 +183,7 @@ fn sweep(cases: &[Case]) { Err(e) => failed.push(e), } } - let _ = std::fs::remove_dir_all(scratch()); + let _ = std::fs::remove_dir_all(scratch("high10")); assert!( failed.is_empty(), "High 10 mismatches:\n{}", diff --git a/crates/ec-h264/tests/it/high422.rs b/crates/ec-h264/tests/it/high422.rs index 4ca3d43b..640eeef5 100644 --- a/crates/ec-h264/tests/it/high422.rs +++ b/crates/ec-h264/tests/it/high422.rs @@ -6,38 +6,7 @@ use std::path::{Path, PathBuf}; -use ec_h264::{Decoder, NalOutcome}; - -use super::common::{escape, ffmpeg_frames, frame_bytes}; - -fn decode_all(bytes: &[u8]) -> Result>, ec_h264::Error> { - let mut dec = Decoder::new(); - let mut frames = Vec::new(); - for nal in ec_h264_syntax::AnnexBIter::new(bytes) { - if dec.push_nal(nal)? == NalOutcome::PictureBoundary { - dec.end_picture()?; - dec.push_nal(nal)?; - } - while let Some(f) = dec.next_frame() { - frames.push(frame_bytes(&f)); - } - } - dec.flush()?; - while let Some(f) = dec.next_frame() { - frames.push(frame_bytes(&f)); - } - Ok(frames) -} - -fn scratch() -> PathBuf { - let dir = std::env::temp_dir().join(format!( - "ec-h264-hi422-{}-{:?}", - std::process::id(), - std::thread::current().id() - )); - std::fs::create_dir_all(&dir).unwrap(); - dir -} +use super::common::{decode_all, escape, ffmpeg_frames, scratch}; struct Case { tag: &'static str, @@ -92,7 +61,7 @@ fn encode(dir: &Path, c: &Case) -> Option { } fn check(c: &Case) -> Result { - let dir = scratch(); + let dir = scratch("hi422"); let stream = encode(&dir, c) .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag)); let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?; @@ -132,7 +101,7 @@ fn sweep(cases: &[Case]) { Err(e) => failed.push(e), } } - let _ = std::fs::remove_dir_all(scratch()); + let _ = std::fs::remove_dir_all(scratch("hi422")); assert!( failed.is_empty(), "4:2:2 mismatches:\n{}", diff --git a/crates/ec-h264/tests/it/high444.rs b/crates/ec-h264/tests/it/high444.rs index fddf2622..83342653 100644 --- a/crates/ec-h264/tests/it/high444.rs +++ b/crates/ec-h264/tests/it/high444.rs @@ -15,7 +15,7 @@ use std::path::{Path, PathBuf}; use ec_core::frame::{PixelFormat, VideoFrame}; use ec_h264::{Decoder, NalOutcome}; -use super::common::{ffmpeg_pix_frames, layout}; +use super::common::{ffmpeg_pix_frames, layout, scratch}; /// One decoded frame as planar bytes (16-bit little endian above 8 bits). fn frame_bytes(frame: &VideoFrame) -> (Vec, PixelFormat) { @@ -50,16 +50,6 @@ fn decode_all(bytes: &[u8]) -> Result, PixelFormat)>, ec_h264::Erro Ok(frames) } -fn scratch() -> PathBuf { - let dir = std::env::temp_dir().join(format!( - "ec-h264-hi444-{}-{:?}", - std::process::id(), - std::thread::current().id() - )); - std::fs::create_dir_all(&dir).unwrap(); - dir -} - #[derive(Clone, Copy)] struct Case { tag: &'static str, @@ -114,7 +104,7 @@ fn encode(dir: &Path, c: &Case) -> Option { } fn check(c: &Case) -> Result { - let dir = scratch(); + let dir = scratch("hi444"); let stream = encode(&dir, c) .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag)); let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?; @@ -186,7 +176,7 @@ fn sweep(label: &str, cases: &[Case]) { Err(e) => failed.push(e), } } - let _ = std::fs::remove_dir_all(scratch()); + let _ = std::fs::remove_dir_all(scratch("hi444")); assert!( failed.is_empty(), "{label} mismatches:\n{}", diff --git a/crates/ec-h264/tests/it/mbaff_x264.rs b/crates/ec-h264/tests/it/mbaff_x264.rs index 1a3e217f..773c0e95 100644 --- a/crates/ec-h264/tests/it/mbaff_x264.rs +++ b/crates/ec-h264/tests/it/mbaff_x264.rs @@ -6,61 +6,7 @@ use std::path::{Path, PathBuf}; -use ec_core::frame::{PixelFormat, VideoFrame}; -use ec_h264::{Decoder, NalOutcome}; - -use super::common::ffmpeg_frames; - -/// One decoded frame as planar bytes (16-bit little endian samples for High 10). -fn frame_bytes(frame: &VideoFrame) -> Vec { - let bytes_per_sample = match frame.format { - PixelFormat::I420 => 1, - PixelFormat::I010 => 2, - other => panic!("unexpected output format {other:?}"), - }; - let (w, h) = (frame.width as usize, frame.height as usize); - let dims = [ - (w, h), - (w.div_ceil(2), h.div_ceil(2)), - (w.div_ceil(2), h.div_ceil(2)), - ]; - let mut out = Vec::new(); - for (plane, (pw, ph)) in frame.planes.iter().zip(dims) { - for y in 0..ph { - out.extend_from_slice(plane.row(y, pw * bytes_per_sample).expect("plane row")); - } - } - out -} - -fn decode_all(bytes: &[u8]) -> Result>, ec_h264::Error> { - let mut dec = Decoder::new(); - let mut frames = Vec::new(); - for nal in ec_h264_syntax::AnnexBIter::new(bytes) { - if dec.push_nal(nal)? == NalOutcome::PictureBoundary { - dec.end_picture()?; - dec.push_nal(nal)?; - } - while let Some(f) = dec.next_frame() { - frames.push(frame_bytes(&f)); - } - } - dec.flush()?; - while let Some(f) = dec.next_frame() { - frames.push(frame_bytes(&f)); - } - Ok(frames) -} - -fn scratch() -> PathBuf { - let dir = std::env::temp_dir().join(format!( - "ec-h264-mbaff-{}-{:?}", - std::process::id(), - std::thread::current().id() - )); - std::fs::create_dir_all(&dir).unwrap(); - dir -} +use super::common::{decode_all, ffmpeg_frames, scratch}; struct Case { tag: &'static str, @@ -105,7 +51,7 @@ fn encode(dir: &Path, c: &Case) -> Option { } fn check(c: &Case) -> Result { - let dir = scratch(); + let dir = scratch("mbaff"); let stream = encode(&dir, c) .unwrap_or_else(|| panic!("{}: ffmpeg/libx264 cannot encode this case", c.tag)); let bytes = std::fs::read(&stream).map_err(|e| e.to_string())?; @@ -145,7 +91,7 @@ fn sweep(cases: &[Case]) { Err(e) => failed.push(e), } } - let _ = std::fs::remove_dir_all(scratch()); + let _ = std::fs::remove_dir_all(scratch("mbaff")); assert!( failed.is_empty(), "MBAFF mismatches:\n{}",