forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

real_library_video.rs154 lines, 5.2 KiBHistoryRawDownload
1//! The software VP8 and VP9 seats on the owner's own files, frame for frame
2//! against ffmpeg's `rawvideo` output of the same file. Every shown picture is
3//! cropped to the display size, serialised as 8-bit I420 (Y, then U, then V,
4//! chroma at `div_ceil(2)`), and compared byte for byte with the next frame ffmpeg
5//! writes -- a byte-identical frame has an identical MD5. Frame count must agree
6//! too, and ffmpeg must have nothing left over.
7//!
8//! Named by `real_library.toml` (gitignored): `vp8_webm` and `vp9_opus_mp4`.
9//! Skips where the key is absent or ffmpeg is not installed.
10
11use crate::reallib;
12
13use std::io::Read;
14use std::path::Path;
15
16use ec_vp8::decode::Decoder as Vp8Decoder;
17use ec_vp9::decode::Decoder as Vp9Decoder;
18use engine::demux::Demuxer;
19use reallib::{ffmpeg_pipe, fill, have};
20
21/// The display-size I420 bytes of one picture, given its plane rows.
22fn i420<T: Copy>(
23 y: &[T],
24 u: &[T],
25 v: &[T],
26 (w, h): (usize, usize),
27 (stride, uv_stride): (usize, usize),
28) -> Vec<u8>
29where
30 u16: From<T>,
31{
32 let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
33 let mut out = Vec::with_capacity(w * h + 2 * cw * ch);
34 let mut plane = |src: &[T], pitch: usize, pw: usize, ph: usize| {
35 for row in 0..ph {
36 out.extend(
37 src[row * pitch..row * pitch + pw]
38 .iter()
39 .map(|s| u16::from(*s) as u8),
40 );
41 }
42 };
43 plane(y, stride, w, h);
44 plane(u, uv_stride, cw, ch);
45 plane(v, uv_stride, cw, ch);
46 out
47}
48
49/// Decodes every shown frame of the file with `next`, comparing each against
50/// ffmpeg's `rawvideo` output as it arrives. Returns the frame count.
51fn compare_with_ffmpeg(
52 key: &str,
53 path: &Path,
54 mut next: impl FnMut() -> Option<(Vec<u8>, (usize, usize))>,
55) -> usize {
56 let (mut child, mut ff) = ffmpeg_pipe(
57 &[
58 "-map",
59 "0:v:0",
60 "-f",
61 "rawvideo",
62 "-pix_fmt",
63 "yuv420p",
64 "-fps_mode",
65 "passthrough",
66 "-",
67 ],
68 path,
69 );
70 let mut frames = 0usize;
71 let mut want = Vec::new();
72 while let Some((got, (w, h))) = next() {
73 let size = w * h + 2 * w.div_ceil(2) * h.div_ceil(2);
74 want.resize(size, 0);
75 let n = fill(&mut ff, &mut want);
76 assert_eq!(
77 n, size,
78 "{key}: ffmpeg ended before frame {frames} ({n} of {size} bytes)"
79 );
80 assert!(
81 got == want,
82 "{key}: frame {frames} ({w}x{h}) differs from ffmpeg's rawvideo"
83 );
84 frames += 1;
85 }
86 let mut rest = Vec::new();
87 ff.read_to_end(&mut rest).expect("drain ffmpeg");
88 assert!(
89 rest.is_empty(),
90 "{key}: ffmpeg has {} bytes of frames ec did not decode",
91 rest.len()
92 );
93 let status = child.wait().expect("wait for ffmpeg");
94 assert!(status.success(), "{key}: ffmpeg failed");
95 frames
96}
97
98fn vp8_frames(path: &Path) -> impl FnMut() -> Option<(Vec<u8>, (usize, usize))> {
99 let (_, mut demuxer) = Demuxer::open(path).expect("open the VP8 file");
100 let mut decoder = Vp8Decoder::new();
101 move || loop {
102 let au = demuxer.next_access_unit().expect("demux")?;
103 if let Some(pic) = decoder.decode(&au).expect("ec-vp8 decode") {
104 let dims = (usize::from(pic.width), usize::from(pic.height));
105 let bytes = i420(&pic.y, &pic.u, &pic.v, dims, (pic.stride, pic.uv_stride));
106 return Some((bytes, dims));
107 }
108 }
109}
110
111fn vp9_frames(path: &Path) -> impl FnMut() -> Option<(Vec<u8>, (usize, usize))> {
112 let (_, mut demuxer) = Demuxer::open(path).expect("open the VP9 file");
113 let mut decoder = Vp9Decoder::new();
114 move || loop {
115 let au = demuxer.next_access_unit().expect("demux")?;
116 if let Some(pic) = decoder.decode(&au).expect("ec-vp9 decode") {
117 let dims = (usize::from(pic.width), usize::from(pic.height));
118 let bytes = i420(&pic.y, &pic.u, &pic.v, dims, (pic.stride, pic.uv_stride));
119 return Some((bytes, dims));
120 }
121 }
122}
123
124/// The VP8 WebM: a 2560x1321 VP8 stream in Matroska, odd height so the chroma
125/// rounding is exercised.
126#[test]
127fn real_vp8_webm_frames_match_ffmpeg() {
128 let Some(path) = engine::real_library::film("vp8_webm") else {
129 return;
130 };
131 if !have("ffmpeg") {
132 eprintln!("skipped: no ffmpeg to decode the reference frames with");
133 return;
134 }
135 let frames = compare_with_ffmpeg("vp8_webm", &path, vp8_frames(&path));
136 eprintln!("vp8_webm: {frames} frames byte-identical to ffmpeg's rawvideo");
137 assert!(frames > 0, "no frames decoded");
138}
139
140/// The VP9 Profile 0 stream in mp4, with Opus beside it (the audio is not this
141/// test's concern).
142#[test]
143fn real_vp9_opus_mp4_frames_match_ffmpeg() {
144 let Some(path) = engine::real_library::film("vp9_opus_mp4") else {
145 return;
146 };
147 if !have("ffmpeg") {
148 eprintln!("skipped: no ffmpeg to decode the reference frames with");
149 return;
150 }
151 let frames = compare_with_ffmpeg("vp9_opus_mp4", &path, vp9_frames(&path));
152 eprintln!("vp9_opus_mp4: {frames} frames byte-identical to ffmpeg's rawvideo");
153 assert!(frames > 0, "no frames decoded");
154}