forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli4 days ago4021 commits

keyframe_exact.rs195 lines, 6.8 KiBHistoryRawDownload
1//! M1 witness: key-frame decode is byte-exact against ffmpeg's libvpx
2//! (`ffmpeg -v error -i in.ivf -f rawvideo -pix_fmt yuv420p -`), plus
3//! the lane's named refusals.
4
5use crate::ivf;
6
7use ec_vp9::decode::Decoder;
8
9/// Generate the small all-keyframe fixture with libvpx if it is absent
10/// (2 keyframes of 320x240 testsrc2, realtime cpu-used 4).
11fn ensure_fixture(path: &std::path::Path) {
12 if path.exists() {
13 return;
14 }
15 if let Some(dir) = path.parent() {
16 std::fs::create_dir_all(dir).unwrap();
17 }
18 let st = std::process::Command::new("ffmpeg")
19 .args([
20 "-y",
21 "-f",
22 "lavfi",
23 "-i",
24 "testsrc2=size=320x240:rate=30:duration=1",
25 "-pix_fmt",
26 "yuv420p",
27 "-c:v",
28 "libvpx-vp9",
29 "-g",
30 "1",
31 "-cpu-used",
32 "4",
33 "-deadline",
34 "realtime",
35 "-frames:v",
36 "2",
37 ])
38 .arg(path)
39 .output()
40 .expect("ffmpeg with libvpx-vp9 must be on PATH for the witnesses");
41 assert!(st.status.success(), "fixture generation failed: {st:?}");
42}
43
44/// ffmpeg's decoded rawvideo byte stream for the same file.
45fn ffmpeg_raw_yuv(path: &std::path::Path) -> Vec<u8> {
46 let out = std::process::Command::new("ffmpeg")
47 .args(["-v", "error", "-i"])
48 .arg(path)
49 .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"])
50 .output()
51 .expect("ffmpeg must be on PATH for the witnesses");
52 assert!(out.status.success(), "ffmpeg failed on {path:?}");
53 out.stdout
54}
55
56/// Decode every frame of an IVF with our decoder and compare each SHOWN
57/// frame's planes against ffmpeg's byte stream.
58fn compare(path: &std::path::Path) {
59 let bytes = std::fs::read(path).unwrap();
60 let (_fourcc, _w, _h, frames) = ivf::parse_ivf(&bytes);
61 let reference = ffmpeg_raw_yuv(path);
62
63 let mut decoder = Decoder::new();
64 let mut ref_pos = 0usize;
65 let mut shown = 0usize;
66 for frame in &frames {
67 let pic = decoder.decode(&frame.data).expect("decode must succeed");
68 if let Some(pic) = pic {
69 let (w, h) = (pic.width as usize, pic.height as usize);
70 assert_eq!(w, 320, "fixture width");
71 assert_eq!(h, 240, "fixture height");
72 let uv = (w / 2) * (h / 2);
73 let fr = &reference[ref_pos..ref_pos + w * h + 2 * uv];
74 ref_pos += w * h + 2 * uv;
75 let want_y = &fr[..w * h];
76 let want_u = &fr[w * h..w * h + uv];
77 let want_v = &fr[w * h + uv..];
78 assert_eq!(pic.y.len(), want_y.len());
79 assert_eq!(pic.u.len(), want_u.len());
80 assert_eq!(pic.v.len(), want_v.len());
81 for (i, (&a, &b)) in pic.y.iter().zip(want_y).enumerate() {
82 assert_eq!(
83 a as u8,
84 b,
85 "Y plane mismatch at pixel ({}, {}) frame {shown}",
86 i % w,
87 i / w
88 );
89 }
90 for (i, (&a, &b)) in pic.u.iter().zip(want_u).enumerate() {
91 assert_eq!(
92 a as u8,
93 b,
94 "U plane mismatch at ({}, {}) frame {shown}",
95 i % (w / 2),
96 i / (w / 2)
97 );
98 }
99 for (i, (&a, &b)) in pic.v.iter().zip(want_v).enumerate() {
100 assert_eq!(
101 a as u8,
102 b,
103 "V plane mismatch at ({}, {}) frame {shown}",
104 i % (w / 2),
105 i / (w / 2)
106 );
107 }
108 shown += 1;
109 }
110 }
111 assert_eq!(shown, 2, "both keyframes are shown");
112}
113
114#[test]
115fn keyframes_match_ffmpeg() {
116 let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/vp9");
117 let path = dir.join("key-320.ivf");
118 ensure_fixture(&path);
119 compare(&path);
120}
121
122#[test]
123fn lossless_64_matches_ffmpeg() {
124 let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
125 let path = dir.join("fixtures/vp9/lossless-64.ivf");
126 let bytes = std::fs::read(&path).unwrap();
127 let (_fourcc, w, h, frames) = ivf::parse_ivf(&bytes);
128 let reference = ffmpeg_raw_yuv(&path);
129 let mut decoder = Decoder::new();
130 let pic = decoder
131 .decode(&frames[0].data)
132 .expect("decode must succeed")
133 .expect("frame is shown");
134 let (w, h) = (w as usize, h as usize);
135 let uv = (w / 2) * (h / 2);
136 let want_y = &reference[..w * h];
137 let want_u = &reference[w * h..w * h + uv];
138 let want_v = &reference[w * h + uv..];
139 assert!(
140 pic.y.iter().map(|&v| v as u8).eq(want_y.iter().copied()),
141 "lossless Y must be byte-exact"
142 );
143 assert!(
144 pic.u.iter().map(|&v| v as u8).eq(want_u.iter().copied()),
145 "lossless U must be byte-exact"
146 );
147 assert!(
148 pic.v.iter().map(|&v| v as u8).eq(want_v.iter().copied()),
149 "lossless V must be byte-exact"
150 );
151 assert_eq!((pic.width, pic.height), (w as u16, h as u16));
152}
153
154#[test]
155fn inter_stream_decodes_and_matches_ffmpeg() {
156 // The altref clip is a real 60-frame inter stream with hidden frames; it
157 // used to be the "inter is refused by name" witness. Inter frames decode
158 // now, so the witness is the pixels: every shown frame must match
159 // ffmpeg's libvpx output byte for byte.
160 let Some(dir) = ivf::fixture_dir() else {
161 return;
162 };
163 let path = dir.join("vp9-superframe-altref.ivf");
164 let bytes = std::fs::read(&path).unwrap();
165 let (_, w, h, frames) = ivf::parse_ivf(&bytes);
166 let (w, h) = (w as usize, h as usize);
167 let expected = ffmpeg_raw_yuv(&path);
168 let frame_bytes = w * h + 2 * (w / 2) * (h / 2);
169 let mut decoder = Decoder::new();
170 let mut shown = 0usize;
171 let mut hidden = 0usize;
172 for (i, frame) in frames.iter().enumerate() {
173 match decoder.decode(&frame.data).expect("every frame decodes") {
174 Some(pic) => {
175 let off = shown * frame_bytes;
176 let mut got = Vec::with_capacity(frame_bytes);
177 got.extend(pic.y.iter().map(|&v| v as u8));
178 got.extend(pic.u.iter().map(|&v| v as u8));
179 got.extend(pic.v.iter().map(|&v| v as u8));
180 assert_eq!(
181 got,
182 expected[off..off + frame_bytes],
183 "frame {i}: pixel mismatch"
184 );
185 shown += 1;
186 }
187 None => hidden += 1,
188 }
189 }
190 assert!(shown >= 2, "the stream shows frames");
191 // The chunk-level hidden altref frames are consumed by the decoder's
192 // superframe split, so every IVF chunk here yields one shown frame.
193 assert_eq!(hidden, 0, "superframe chunks show exactly one frame each");
194 assert_eq!(shown * frame_bytes, expected.len());
195}