forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

inter_pixels_exact.rs137 lines, 5.0 KiBHistoryRawDownload
1//! Inter-frame pixel witness: a GOP (key frame + inter frames, with and
2//! without tile columns) must decode byte-identically to ffmpeg's libvpx
3//! decoder, whose rawvideo output is the oracle. Fixtures are generated
4//! deterministically with ffmpeg, so the witness is self-contained.
5//!
6//! The 1080p private streams the lane developed against are compared through
7//! `tests/scratch_pixdump.rs` (env-gated) instead; see `lanes/vp9pix.report.md`.
8
9use crate::ivf;
10use crate::common::{ffmpeg_raw_yuv, require_fixture};
11
12use ec_vp9::decode::Decoder;
13/// Generate a GOP fixture with libvpx: 6 frames of testsrc2 at 320x240,
14/// `-g 10` so frames 1..5 are inter frames.
15fn ensure_fixture(path: &std::path::Path, tile_columns: u32) {
16 if path.exists() {
17 return;
18 }
19 if let Some(dir) = path.parent() {
20 std::fs::create_dir_all(dir).unwrap();
21 }
22 let st = std::process::Command::new("ffmpeg")
23 .args([
24 "-v",
25 "error",
26 "-y",
27 "-f",
28 "lavfi",
29 "-i",
30 "testsrc2=size=320x240:rate=24:duration=0.25",
31 "-c:v",
32 "libvpx-vp9",
33 "-g",
34 "10",
35 "-crf",
36 "32",
37 "-deadline",
38 "good",
39 "-cpu-used",
40 "4",
41 "-tile-columns",
42 ])
43 .arg(tile_columns.to_string())
44 .args(["-pix_fmt", "yuv420p", "-f", "ivf"])
45 .arg(path)
46 .output()
47 .expect("ffmpeg with libvpx-vp9 must be on PATH for the witnesses");
48 assert!(st.status.success(), "fixture generation failed: {st:?}");
49}
50
51/// Decode every frame with our decoder and compare each SHOWN frame's planes
52/// against ffmpeg's stream; returns the number of shown frames compared.
53fn compare(path: &std::path::Path) -> usize {
54 let bytes = std::fs::read(path).unwrap();
55 let (_, w, h, frames) = ivf::parse_ivf(&bytes);
56 let (w, h) = (w as usize, h as usize);
57 let expected = ffmpeg_raw_yuv(path, w, h);
58 let frame_bytes = w * h + 2 * (w / 2) * (h / 2);
59
60 let mut decoder = Decoder::new();
61 let mut shown = 0usize;
62 let mut seen_inter = false;
63 for (i, f) in frames.iter().enumerate() {
64 if let Some(pic) = decoder.decode(&f.data).expect("frame decodes") {
65 let off = shown * frame_bytes;
66 assert!(
67 off + frame_bytes <= expected.len(),
68 "frame {i}: ffmpeg has fewer frames than we show"
69 );
70 let want = &expected[off..off + frame_bytes];
71 let mut got = Vec::with_capacity(frame_bytes);
72 got.extend(pic.y.iter().map(|&v| v as u8));
73 got.extend(pic.u.iter().map(|&v| v as u8));
74 got.extend(pic.v.iter().map(|&v| v as u8));
75 assert_eq!(
76 got.len(),
77 frame_bytes,
78 "frame {i}: our planes are {w}x{h} I420"
79 );
80 let diff = got.iter().zip(want).position(|(a, b)| a != b);
81 assert!(
82 diff.is_none(),
83 "frame {i}: pixel mismatch at byte {:?} (ours {} ffmpeg {})",
84 diff,
85 diff.map(|d| got[d]).unwrap_or(0),
86 diff.map(|d| want[d]).unwrap_or(0)
87 );
88 shown += 1;
89 }
90 if i > 0 {
91 seen_inter = true;
92 }
93 }
94 assert_eq!(shown * frame_bytes, expected.len(), "frame count mismatch");
95 assert!(seen_inter, "the fixture must contain inter frames");
96 shown
97}
98
99#[test]
100fn inter_gop_matches_ffmpeg() {
101 let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/vp9-witness");
102 let path = dir.join("inter-gop.ivf");
103 ensure_fixture(&path, 0);
104 assert!(compare(&path) >= 2);
105}
106
107#[test]
108fn inter_gop_with_tile_columns_matches_ffmpeg() {
109 let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/vp9-witness");
110 let path = dir.join("inter-gop-tc1.ivf");
111 ensure_fixture(&path, 1);
112 assert!(compare(&path) >= 2);
113}
114
115/// Real-content regression for the last-partial-SB-row context bug: this
116/// 120-frame 1080p stream used to abort at input frame 72 with
117/// `tile bool decoder desync`, because a chroma transform block whose
118/// write-back window runs past the frame bottom edge kept the window's
119/// TAIL slot set (`write_tx_context`), and the next row of blocks read it
120/// as an entropy context. Every shown frame must match ffmpeg.
121///
122/// Media-gated: the corpus fixture is not versioned, so the witness prints
123/// SKIP when `fixtures/bitstreams/vp9-1080p-60-8bit.ivf` is absent -- and goes
124/// RED instead under `EC_REQUIRE_FIXTURES=1`.
125#[test]
126fn corpus_1080p_60fps_matches_ffmpeg_past_frame_72() {
127 let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
128 .join("../../fixtures/bitstreams/vp9-1080p-60-8bit.ivf");
129 if !require_fixture(&path, "scripts/gen-bitstream-fixtures.sh") {
130 return;
131 }
132 let shown = compare(&path);
133 assert!(
134 shown >= 73,
135 "the witness must decode past input frame 72 (got {shown} shown frames)"
136 );
137}