forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

ffmpeg_parity.rs200 lines, 6.7 KiBHistoryRawDownload
1//! Byte-exactness against ffmpeg's `dvvideo` decoder (native pix_fmt) and its DV demuxer audio.
2mod common;
3
4use std::io::Cursor;
5
6use common::*;
7use ec_core::registry::{CodecId, Demuxer};
8use ec_dv::{Chroma, DvDemuxer, DvVideoDecoder};
9
10fn frames_of(path: &std::path::Path) -> (Vec<Vec<u8>>, Vec<Vec<u8>>) {
11 let data = std::fs::read(path).unwrap();
12 let mut dm = DvDemuxer::new(Cursor::new(data)).unwrap();
13 let n_streams = dm.streams().len();
14 let mut video = Vec::new();
15 let mut audio = vec![Vec::new(); n_streams - 1];
16 while let Ok(p) = dm.next_packet() {
17 if p.stream == 0 {
18 video.push(p.data.to_vec());
19 } else {
20 audio[p.stream as usize - 1].extend_from_slice(&p.data);
21 }
22 }
23 (video, audio)
24}
25
26fn native_bytes(frames: &[Vec<u8>]) -> Vec<u8> {
27 let mut dec = DvVideoDecoder::new();
28 let mut out = Vec::new();
29 for f in frames {
30 let pic = dec.decode(f).expect("decode");
31 for p in &pic.planes {
32 out.extend_from_slice(p);
33 }
34 }
35 out
36}
37
38fn first_diff(a: &[u8], b: &[u8]) -> Option<usize> {
39 if a.len() != b.len() {
40 return Some(a.len().min(b.len()));
41 }
42 a.iter().zip(b).position(|(x, y)| x != y)
43}
44
45fn check_flavour(f: &Flavour, ilme: bool, noise: u32) {
46 let tag = format!("p{}{}", u8::from(ilme), noise);
47 let path = make(f, &tag, "dv", ilme, noise).unwrap_or_else(|| panic!("could not make {}", f.name));
48 let (video, audio) = frames_of(&path);
49 assert!(video.len() >= 2, "{}: frame count {}", f.name, video.len());
50 let want = ffmpeg_raw_video(&path, &[]);
51 let got = native_bytes(&video);
52 if let Some(i) = first_diff(&got, &want) {
53 panic!("{} ilme={ilme} noise={noise}: video differs at byte {i} (got {} want {} bytes)", f.name, got.len(), want.len());
54 }
55 if f.audio {
56 assert!(!audio.is_empty(), "{}: no audio streams", f.name);
57 for (n, a) in audio.iter().enumerate() {
58 let w = ffmpeg_pcm(&path, n).expect("ffmpeg audio");
59 let len = a.len().min(w.len());
60 assert_eq!(&a[..len], &w[..len], "{}: audio stream {n} differs", f.name);
61 assert!(len >= w.len().saturating_sub(8000), "{}: audio too short ({} vs {})", f.name, a.len(), w.len());
62 }
63 }
64}
65
66macro_rules! flavour_test {
67 ($name:ident, $idx:expr) => {
68 #[test]
69 fn $name() {
70 if !ffmpeg_or_skip() {
71 return;
72 }
73 let f = &FLAVOURS[$idx];
74 for (ilme, noise) in [(false, 0), (true, 6), (true, 25), (false, 40)] {
75 check_flavour(f, ilme, noise);
76 }
77 }
78 };
79}
80
81flavour_test!(ntsc_411, 0);
82flavour_test!(pal_420, 1);
83flavour_test!(pal_411, 2);
84flavour_test!(ntsc_422_dvcpro50, 3);
85flavour_test!(pal_422_dvcpro50, 4);
86flavour_test!(hd_1080i50, 5);
87flavour_test!(hd_1080i60, 6);
88flavour_test!(hd_720p50, 7);
89flavour_test!(hd_720p60, 8);
90
91#[test]
92fn native_formats() {
93 if !ffmpeg_or_skip() {
94 return;
95 }
96 for (i, want) in [Chroma::Yuv411, Chroma::Yuv420, Chroma::Yuv411, Chroma::Yuv422, Chroma::Yuv422].into_iter().enumerate() {
97 let f = &FLAVOURS[i];
98 let path = make(f, "fmt", "dv", false, 5).unwrap();
99 let (video, _) = frames_of(&path);
100 let pic = DvVideoDecoder::new().decode(&video[0]).unwrap();
101 assert_eq!(pic.chroma, want, "{}", f.name);
102 }
103 let _ = CodecId::DvVideo;
104}
105
106/// Offsets (within a frame) of the 80-byte video DIF blocks.
107fn video_blocks(frame_size: usize, difseg: usize) -> Vec<usize> {
108 let seq_bytes = 150 * 80;
109 let mut v = Vec::new();
110 for s in 0..frame_size / seq_bytes {
111 let _ = difseg;
112 for b in 6..150 {
113 if (b - 6) % 16 != 0 {
114 v.push(s * seq_bytes + b * 80);
115 }
116 }
117 }
118 v
119}
120
121struct Rng(u64);
122impl Rng {
123 fn next(&mut self) -> u64 {
124 self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
125 self.0 >> 33
126 }
127}
128
129/// Damaged and field-flagged streams: bit recovery across blocks, macroblocks and segments, the
130/// retry/concealment path and the HD last-row field-mode placement all have to match ffmpeg.
131fn check_mutated(f: &Flavour, mode: u32, seed: u64) {
132 let path = make(f, &format!("m{mode}_{seed}"), "dv", true, 8).unwrap();
133 let mut data = std::fs::read(&path).unwrap();
134 let fs = ec_dv::frame_profile(None, &data).unwrap().frame_size;
135 let blocks = video_blocks(fs, 0);
136 let mut rng = Rng(seed);
137 for frame in data.chunks_exact_mut(fs) {
138 match mode {
139 0 => {
140 // set the DCT mode bit of every block (HD: block 0 selects field mode)
141 for &b in &blocks {
142 frame[b + 5] |= 0x40;
143 }
144 }
145 1 => {
146 // sprinkle random bytes
147 for _ in 0..40 {
148 let b = blocks[rng.next() as usize % blocks.len()];
149 frame[b + 4 + rng.next() as usize % 76] = rng.next() as u8;
150 }
151 }
152 2 => {
153 // flip single bits (usually just a changed coefficient, sometimes a lost EOB)
154 for _ in 0..200 {
155 let b = blocks[rng.next() as usize % blocks.len()];
156 frame[b + 4 + rng.next() as usize % 76] ^= 1 << (rng.next() % 8);
157 }
158 }
159 4 => {
160 // random block status nibbles (error concealment path)
161 for &b in &blocks {
162 if rng.next().is_multiple_of(11) {
163 frame[b + 3] = (frame[b + 3] & 0x0f) | ((rng.next() as u8 & 0x0f) << 4);
164 }
165 }
166 }
167 _ => {
168 // random QNO and class bits
169 for &b in &blocks {
170 if rng.next().is_multiple_of(3) {
171 frame[b + 3] = (frame[b + 3] & 0xf0) | (rng.next() as u8 & 0x0f);
172 }
173 }
174 }
175 }
176 }
177 std::fs::write(&path, &data).unwrap();
178 let (video, _) = frames_of(&path);
179 // `-cpuflags 0`: ffmpeg's C simple IDCT. Its SIMD versions agree on every sane coefficient
180 // set but wrap differently on the absurd ones damaged streams produce.
181 let want = ffmpeg_raw_video(&path, &["-cpuflags", "0"]);
182 let got = native_bytes(&video);
183 if let Some(i) = first_diff(&got, &want) {
184 panic!("{} mode={mode} seed={seed}: differs at byte {i} (got {} want {})", f.name, got.len(), want.len());
185 }
186}
187
188#[test]
189fn mutated_streams_match_ffmpeg() {
190 if !ffmpeg_or_skip() {
191 return;
192 }
193 for f in FLAVOURS {
194 for mode in 0..5 {
195 for seed in [1u64, 2, 3] {
196 check_mutated(f, mode, seed);
197 }
198 }
199 }
200}