U18: delete edith_parity (hardcoded foreign path) and ogg real_library (private library, EC_REAL_LIBRARY)
Browse files at this commit3bb98ab9672ee1ff2de6e4fdcb0b005e5977f635Parent: ca690273 changed files +0 -718
- crates/ec-mp4/tests/it/edith_parity.rs +0 -402
- crates/ec-mp4/tests/it/main.rs +0 -1
- crates/ec-ogg/tests/real_library.rs +0 -315
crates/ec-mp4/tests/it/edith_parity.rsdeleted+0 -402
| @@ -1,402 +0,0 @@ | ||
| 1 | -//! Parity with ffprobe, file by file: what ec-mp4 reads out of an mp4 or mov has | |
| 2 | -//! to be what ffprobe reads out of the same bytes, sample for sample and field | |
| 3 | -//! for field. This is the swap criterion for edith's `mp4::` demuxer: identical | |
| 4 | -//! packet bytes, identical timestamps, identical stream metadata. | |
| 5 | -//! | |
| 6 | -//! | |
| 7 | -//! Per track, the comparison is: codec, coded size, sample rate, channels, | |
| 8 | -//! timebase, rotation, and per sample the byte offset, size, presentation time | |
| 9 | -//! (`cts` against ffprobe `pts`), the sync flag, and the decode time up to one | |
| 10 | -//! constant. ffmpeg's `dts` carries a constant shift of its own (the largest | |
| 11 | -//! composition delay), so only its differences are the same claim. Edit lists | |
| 12 | -//! are applied on both sides, the same way ffmpeg applies them. | |
| 13 | -//! | |
| 14 | -//! Where edith's engine reads the file differently on purpose (it trims the | |
| 15 | -//! picture by the edit list where ffprobe shifts it, and ignores empty edits), | |
| 16 | -//! the edith-only values are printed in the table, not compared. See | |
| 17 | -//! `lanes/mp4demux.report.md`. | |
| 18 | - | |
| 19 | -use std::collections::HashMap; | |
| 20 | -use std::fs::File; | |
| 21 | -use std::io::BufReader; | |
| 22 | -use std::path::{Path, PathBuf}; | |
| 23 | - | |
| 24 | -use ec_core::{CodecId, Demuxer, MediaParameters}; | |
| 25 | -use ec_mp4::{Mp4Demuxer, Sample}; | |
| 26 | - | |
| 27 | -use crate::common::ffmpeg_name; | |
| 28 | - | |
| 29 | -/// One ffprobe `key=value` record block, keyed by field name. | |
| 30 | -type Record = HashMap<String, String>; | |
| 31 | - | |
| 32 | -/// Every `stream=`/`packet=` block of `default=nw=1` output, split where the | |
| 33 | -/// `first` key starts a new block. | |
| 34 | -fn records(text: &str, first: &str) -> Vec<Record> { | |
| 35 | - let mut out: Vec<Record> = Vec::new(); | |
| 36 | - for line in text.lines() { | |
| 37 | - let Some((key, value)) = line.split_once('=') else { | |
| 38 | - continue; | |
| 39 | - }; | |
| 40 | - if key == first { | |
| 41 | - out.push(Record::new()); | |
| 42 | - } | |
| 43 | - if let Some(record) = out.last_mut() { | |
| 44 | - record.insert(key.to_string(), value.to_string()); | |
| 45 | - } | |
| 46 | - } | |
| 47 | - out | |
| 48 | -} | |
| 49 | - | |
| 50 | -fn ffprobe(path: &Path, entries: &str, first: &str) -> Vec<Record> { | |
| 51 | - let out = ec_testkit::ffprobe() | |
| 52 | - .args(["-v", "error", "-show_entries", entries, "-of", "default=nw=1"]) | |
| 53 | - .arg(path) | |
| 54 | - .output() | |
| 55 | - .expect("ffprobe runs"); | |
| 56 | - assert!( | |
| 57 | - out.status.success(), | |
| 58 | - "ffprobe refused {}: {}", | |
| 59 | - path.display(), | |
| 60 | - String::from_utf8_lossy(&out.stderr) | |
| 61 | - ); | |
| 62 | - records(&String::from_utf8_lossy(&out.stdout), first) | |
| 63 | -} | |
| 64 | - | |
| 65 | -/// What ffprobe says about one stream, `None` where a field is `N/A` or absent. | |
| 66 | -#[derive(Debug, Default)] | |
| 67 | -struct ProbeStream { | |
| 68 | - codec: String, | |
| 69 | - width: Option<u32>, | |
| 70 | - height: Option<u32>, | |
| 71 | - sample_rate: Option<u32>, | |
| 72 | - channels: Option<u32>, | |
| 73 | - time_base: String, | |
| 74 | - duration_ts: Option<i64>, | |
| 75 | - rotation: Option<i32>, | |
| 76 | - attached_pic: bool, | |
| 77 | -} | |
| 78 | - | |
| 79 | -#[derive(Debug, Clone, Copy)] | |
| 80 | -struct ProbePacket { | |
| 81 | - pts: Option<i64>, | |
| 82 | - dts: Option<i64>, | |
| 83 | - size: u64, | |
| 84 | - pos: Option<u64>, | |
| 85 | - key: bool, | |
| 86 | -} | |
| 87 | - | |
| 88 | -fn number<T: std::str::FromStr>(value: Option<&String>) -> Option<T> { | |
| 89 | - value.and_then(|v| v.parse().ok()) | |
| 90 | -} | |
| 91 | - | |
| 92 | -fn probe_streams(path: &Path, rotations: &HashMap<u32, i32>) -> Vec<ProbeStream> { | |
| 93 | - ffprobe( | |
| 94 | - path, | |
| 95 | - "stream=index,codec_name,codec_type,width,height,sample_rate,channels,time_base,duration_ts:stream_disposition=attached_pic", | |
| 96 | - "index", | |
| 97 | - ) | |
| 98 | - .into_iter() | |
| 99 | - .map(|r| { | |
| 100 | - let index = number::<u32>(r.get("index")).unwrap_or(u32::MAX); | |
| 101 | - ProbeStream { | |
| 102 | - codec: r.get("codec_name").cloned().unwrap_or_default(), | |
| 103 | - width: number(r.get("width")), | |
| 104 | - height: number(r.get("height")), | |
| 105 | - sample_rate: number(r.get("sample_rate")), | |
| 106 | - channels: number(r.get("channels")), | |
| 107 | - time_base: r.get("time_base").cloned().unwrap_or_default(), | |
| 108 | - duration_ts: number(r.get("duration_ts")), | |
| 109 | - rotation: rotations.get(&index).copied(), | |
| 110 | - attached_pic: r | |
| 111 | - .get("DISPOSITION:attached_pic") | |
| 112 | - .is_some_and(|v| v == "1"), | |
| 113 | - } | |
| 114 | - }) | |
| 115 | - .collect() | |
| 116 | -} | |
| 117 | - | |
| 118 | -/// Display rotation per stream index, from the `side_data_list` of the JSON | |
| 119 | -/// stream dump. The `side_data=rotation` entry query is not used: on a 2 GB film | |
| 120 | -/// ffprobe spends minutes decoding for it, while the JSON dump is immediate. | |
| 121 | -fn rotations(path: &Path) -> HashMap<u32, i32> { | |
| 122 | - let out = ec_testkit::ffprobe() | |
| 123 | - .args(["-v", "error", "-show_streams", "-of", "json"]) | |
| 124 | - .arg(path) | |
| 125 | - .output() | |
| 126 | - .expect("ffprobe runs"); | |
| 127 | - let mut found = HashMap::new(); | |
| 128 | - let mut current = None; | |
| 129 | - for line in String::from_utf8_lossy(&out.stdout).lines() { | |
| 130 | - let line = line.trim(); | |
| 131 | - // Stream objects open with `"index": n,` at the stream's own indent. | |
| 132 | - if let Some(rest) = line.strip_prefix("\"index\": ") { | |
| 133 | - current = rest.trim_end_matches(',').parse::<u32>().ok(); | |
| 134 | - } else if let Some(rest) = line.strip_prefix("\"rotation\": ") | |
| 135 | - && let (Some(index), Ok(r)) = (current, rest.trim_end_matches(',').parse::<i32>()) | |
| 136 | - { | |
| 137 | - found.insert(index, r); | |
| 138 | - } | |
| 139 | - } | |
| 140 | - found | |
| 141 | -} | |
| 142 | - | |
| 143 | -/// Packets per ffprobe stream index, each list in the order ffprobe demuxes | |
| 144 | -/// them, which for one stream is decode order. | |
| 145 | -fn probe_packets(path: &Path) -> HashMap<u32, Vec<ProbePacket>> { | |
| 146 | - let mut by_stream: HashMap<u32, Vec<ProbePacket>> = HashMap::new(); | |
| 147 | - for r in ffprobe( | |
| 148 | - path, | |
| 149 | - "packet=stream_index,pts,dts,size,pos,flags", | |
| 150 | - "stream_index", | |
| 151 | - ) { | |
| 152 | - let Some(index) = number::<u32>(r.get("stream_index")) else { | |
| 153 | - continue; | |
| 154 | - }; | |
| 155 | - by_stream.entry(index).or_default().push(ProbePacket { | |
| 156 | - pts: number(r.get("pts")), | |
| 157 | - dts: number(r.get("dts")), | |
| 158 | - size: number(r.get("size")).unwrap_or(0), | |
| 159 | - pos: number(r.get("pos")), | |
| 160 | - key: r.get("flags").is_some_and(|f| f.starts_with('K')), | |
| 161 | - }); | |
| 162 | - } | |
| 163 | - by_stream | |
| 164 | -} | |
| 165 | - | |
| 166 | -/// ffprobe's rotation (counter-clockwise degrees) as this crate's clockwise | |
| 167 | -/// quarter turn: the same matrix, the other direction. | |
| 168 | -fn expected_rotation(rotation: Option<i32>) -> Option<u8> { | |
| 169 | - match rotation { | |
| 170 | - None => Some(0), | |
| 171 | - Some(r) if r % 90 == 0 => Some((-r / 90).rem_euclid(4) as u8), | |
| 172 | - Some(_) => None, | |
| 173 | - } | |
| 174 | -} | |
| 175 | - | |
| 176 | -/// One file, compared. Returns the table row and every mismatch it found. | |
| 177 | -fn compare(path: &Path) -> (String, Vec<String>) { | |
| 178 | - let mut problems = Vec::new(); | |
| 179 | - let file = File::open(path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); | |
| 180 | - let demuxer = Mp4Demuxer::new(BufReader::new(file)) | |
| 181 | - .unwrap_or_else(|e| panic!("{}: {e}", path.display())); | |
| 182 | - let name = path.file_name().unwrap_or_default().to_string_lossy().into_owned(); | |
| 183 | - // Cover art is not a track: ffprobe lists an `covr` picture as a video stream | |
| 184 | - // with `attached_pic` set, and this crate does not list it (a player that | |
| 185 | - // shows an album cover as a video stream is the bug). Dropped on both sides. | |
| 186 | - let rotations = rotations(path); | |
| 187 | - let all_probed = probe_streams(path, &rotations); | |
| 188 | - let probed: Vec<ProbeStream> = all_probed.into_iter().filter(|p| !p.attached_pic).collect(); | |
| 189 | - let packets = probe_packets(path); | |
| 190 | - let tracks = demuxer.tracks().to_vec(); | |
| 191 | - if probed.len() != tracks.len() { | |
| 192 | - // The track position is the only index the two share, so the counts agree | |
| 193 | - // or the rest of the comparison is meaningless. | |
| 194 | - problems.push(format!("track count: ec {} ffprobe {}", tracks.len(), probed.len())); | |
| 195 | - } | |
| 196 | - let mut rows = Vec::new(); | |
| 197 | - for (position, track) in tracks.iter().enumerate() { | |
| 198 | - let Some(stream) = track.stream else { | |
| 199 | - continue; | |
| 200 | - }; | |
| 201 | - let Some(p) = probed.get(position) else { | |
| 202 | - problems.push(format!("track {position}: no ffprobe stream")); | |
| 203 | - continue; | |
| 204 | - }; | |
| 205 | - let info = demuxer.streams()[stream as usize].clone(); | |
| 206 | - let samples: Vec<Sample> = demuxer.samples(stream).to_vec(); | |
| 207 | - let tag = format!("track {position} ({})", ffmpeg_name(info.params.codec)); | |
| 208 | - if ffmpeg_name(info.params.codec) != p.codec { | |
| 209 | - problems.push(format!("{tag}: codec, ffprobe says {}", p.codec)); | |
| 210 | - } | |
| 211 | - let tb = format!("{}/{}", info.time_base.num(), info.time_base.den()); | |
| 212 | - if tb != p.time_base { | |
| 213 | - problems.push(format!("{tag}: timebase {tb} vs {}", p.time_base)); | |
| 214 | - } | |
| 215 | - match &info.params.media { | |
| 216 | - MediaParameters::Video(v) => { | |
| 217 | - if Some((v.width, v.height)) != p.width.zip(p.height) { | |
| 218 | - problems.push(format!( | |
| 219 | - "{tag}: coded size {}x{} vs ffprobe {:?}x{:?}", | |
| 220 | - v.width, v.height, p.width, p.height | |
| 221 | - )); | |
| 222 | - } | |
| 223 | - // Both sides name the same matrix; ffprobe counts counter-clockwise. | |
| 224 | - if info.rotation.steps() != expected_rotation(p.rotation) { | |
| 225 | - problems.push(format!( | |
| 226 | - "{tag}: rotation {:?} vs ffprobe {:?}", | |
| 227 | - info.rotation, p.rotation | |
| 228 | - )); | |
| 229 | - } | |
| 230 | - } | |
| 231 | - MediaParameters::Audio(a) => { | |
| 232 | - // Implicit HE-AAC (SBR with no extension frequency in the config): | |
| 233 | - // ffprobe decodes the first frames for the doubled output rate, | |
| 234 | - // and so does edith (`mp4_aac_prime`); this reader reports the core's. | |
| 235 | - let implicit_sbr = info.params.codec == CodecId::Aac | |
| 236 | - && p.sample_rate == Some(a.sample_rate * 2); | |
| 237 | - if Some(a.sample_rate) != p.sample_rate && !implicit_sbr { | |
| 238 | - problems.push(format!("{tag}: rate {} vs {:?}", a.sample_rate, p.sample_rate)); | |
| 239 | - } | |
| 240 | - if Some(a.layout.channel_count() as u32) != p.channels { | |
| 241 | - problems.push(format!( | |
| 242 | - "{tag}: channels {} vs {:?}", | |
| 243 | - a.layout.channel_count(), | |
| 244 | - p.channels | |
| 245 | - )); | |
| 246 | - } | |
| 247 | - } | |
| 248 | - MediaParameters::Subtitle => {} | |
| 249 | - } | |
| 250 | - // Per sample, against ffprobe's packets of the same stream. | |
| 251 | - let theirs = packets.get(&(position as u32)).cloned().unwrap_or_default(); | |
| 252 | - // A subtitle track ends in a zero-duration sample at the movie's end, an | |
| 253 | - // end marker ffprobe does not hand out as a packet. Dropped on this side | |
| 254 | - // for the comparison only; the sample is still in the table. | |
| 255 | - let mut samples = samples; | |
| 256 | - if matches!(info.params.media, MediaParameters::Subtitle) | |
| 257 | - && samples.len() == theirs.len() + 1 | |
| 258 | - && samples.last().is_some_and(|s| s.duration == 0) | |
| 259 | - { | |
| 260 | - samples.pop(); | |
| 261 | - } | |
| 262 | - if theirs.len() != samples.len() { | |
| 263 | - problems.push(format!( | |
| 264 | - "{tag}: {} samples vs {} packets", | |
| 265 | - samples.len(), | |
| 266 | - theirs.len() | |
| 267 | - )); | |
| 268 | - } | |
| 269 | - // VP8 and VP9 carry no `stss` in these files, so the box makes every | |
| 270 | - // sample a sync sample, while ffprobe reads the key flag out of the | |
| 271 | - // bitstream. The box is what edith follows; the flag difference is named. | |
| 272 | - let box_says_all_sync = samples.iter().all(|s| s.sync); | |
| 273 | - let bitstream_keys = theirs.iter().filter(|p| p.key).count(); | |
| 274 | - let vp_box_only = matches!(info.params.codec, CodecId::Vp8 | CodecId::Vp9) | |
| 275 | - && box_says_all_sync | |
| 276 | - && bitstream_keys < samples.len(); | |
| 277 | - let mut dts_shift: Option<i64> = None; | |
| 278 | - let (mut bad_pos, mut bad_size, mut bad_pts, mut bad_sync, mut bad_dts) = (0, 0, 0, 0, 0); | |
| 279 | - for (ours, theirs) in samples.iter().zip(&theirs) { | |
| 280 | - if theirs.pos != Some(ours.offset) { | |
| 281 | - bad_pos += 1; | |
| 282 | - } | |
| 283 | - if u64::from(ours.size) != theirs.size { | |
| 284 | - bad_size += 1; | |
| 285 | - } | |
| 286 | - if theirs.pts != Some(ours.cts) { | |
| 287 | - bad_pts += 1; | |
| 288 | - } | |
| 289 | - if theirs.key != ours.sync && !vp_box_only { | |
| 290 | - bad_sync += 1; | |
| 291 | - } | |
| 292 | - match (theirs.dts, dts_shift) { | |
| 293 | - (Some(d), None) => dts_shift = Some(ours.dts - d), | |
| 294 | - (Some(d), Some(k)) if ours.dts - d != k => bad_dts += 1, | |
| 295 | - (None, _) => bad_dts += 1, | |
| 296 | - _ => {} | |
| 297 | - } | |
| 298 | - } | |
| 299 | - for (what, n) in [ | |
| 300 | - ("offset", bad_pos), | |
| 301 | - ("size", bad_size), | |
| 302 | - ("pts", bad_pts), | |
| 303 | - ("sync", bad_sync), | |
| 304 | - ("dts", bad_dts), | |
| 305 | - ] { | |
| 306 | - if n > 0 { | |
| 307 | - problems.push(format!("{tag}: {n} samples differ in {what}")); | |
| 308 | - } | |
| 309 | - } | |
| 310 | - let syncs = demuxer.sync_in_display_order(stream).len(); | |
| 311 | - rows.push(format!( | |
| 312 | - " t{position} {:<7} n={:<6} trim={:<8} first={:<6} dts-shift={:<6} edits={:?} display-syncs={syncs} duration_ts={:?}/{:?}", | |
| 313 | - ffmpeg_name(info.params.codec), | |
| 314 | - samples.len(), | |
| 315 | - format!("{:?}", demuxer.presentation_trim(stream)), | |
| 316 | - demuxer.first_frame_sample(stream), | |
| 317 | - dts_shift.map_or("-".into(), |k| k.to_string()), | |
| 318 | - track.edits.iter().map(|e| (e.segment_duration, e.media_time)).collect::<Vec<_>>(), | |
| 319 | - p.duration_ts, | |
| 320 | - info.duration, | |
| 321 | - )); | |
| 322 | - } | |
| 323 | - let row = format!("{name}\n{}", rows.join("\n")); | |
| 324 | - (row, problems) | |
| 325 | -} | |
| 326 | - | |
| 327 | -/// Every mp4-family file under `dir`, recursively, into `files`. | |
| 328 | -fn collect(dir: &Path, files: &mut Vec<PathBuf>) { | |
| 329 | - let Ok(entries) = std::fs::read_dir(dir) else { | |
| 330 | - return; | |
| 331 | - }; | |
| 332 | - for entry in entries.flatten() { | |
| 333 | - let path = entry.path(); | |
| 334 | - if path.is_dir() { | |
| 335 | - collect(&path, files); | |
| 336 | - continue; | |
| 337 | - } | |
| 338 | - let name = path.to_string_lossy().to_ascii_lowercase(); | |
| 339 | - if [".mp4", ".m4a", ".m4v", ".mov"].iter().any(|ext| name.ends_with(ext)) { | |
| 340 | - files.push(path); | |
| 341 | - } | |
| 342 | - } | |
| 343 | -} | |
| 344 | - | |
| 345 | -fn corpus() -> Vec<PathBuf> { | |
| 346 | - let mut files = Vec::new(); | |
| 347 | - collect(&ec_testkit::root().join("mp4corpus"), &mut files); | |
| 348 | - files.sort(); | |
| 349 | - files.dedup(); | |
| 350 | - files | |
| 351 | -} | |
| 352 | - | |
| 353 | -#[test] | |
| 354 | -fn every_corpus_file_matches_ffprobe_sample_for_sample() { | |
| 355 | - let files = corpus(); | |
| 356 | - assert!(!files.is_empty(), "no mp4/mov corpus; run scripts/fixtures.sh"); | |
| 357 | - let mut report = Vec::new(); | |
| 358 | - let mut failures = Vec::new(); | |
| 359 | - for path in &files { | |
| 360 | - let (row, problems) = compare(path); | |
| 361 | - report.push(row); | |
| 362 | - if !problems.is_empty() { | |
| 363 | - failures.push(format!("{}:\n {}", path.display(), problems.join("\n "))); | |
| 364 | - } | |
| 365 | - } | |
| 366 | - println!("{}", report.join("\n")); | |
| 367 | - assert!( | |
| 368 | - failures.is_empty(), | |
| 369 | - "{} of {} files differ from ffprobe:\n{}", | |
| 370 | - failures.len(), | |
| 371 | - files.len(), | |
| 372 | - failures.join("\n") | |
| 373 | - ); | |
| 374 | -} | |
| 375 | - | |
| 376 | -/// The edith-side reading of the edit list and the sample table, on fixtures | |
| 377 | -/// whose answers are known: an AAC track whose edit list trims one 1024-sample | |
| 378 | -/// priming packet, and an open-GOP stream whose keyframes sit out of display | |
| 379 | -/// order. | |
| 380 | -#[test] | |
| 381 | -fn edith_trim_and_display_order_read_the_known_fixtures() { | |
| 382 | - let aac = ec_testkit::fixture("audio/aac-mp4-stereo-48000.mp4"); | |
| 383 | - let demuxer = Mp4Demuxer::new(BufReader::new(File::open(&aac).expect("fixture"))) | |
| 384 | - .expect("the fixture demuxes"); | |
| 385 | - assert_eq!(demuxer.presentation_trim(0), Some(1024)); | |
| 386 | - assert_eq!(demuxer.first_frame_sample(0), 2, "the trim lands on packet 2"); | |
| 387 | - assert!(demuxer.sync_in_display_order(0).is_empty(), "no ctts, no reorder"); | |
| 388 | - | |
| 389 | - let gop = ec_testkit::fixture("video/h264-open-gop.mp4"); | |
| 390 | - let demuxer = Mp4Demuxer::new(BufReader::new(File::open(&gop).expect("fixture"))) | |
| 391 | - .expect("the fixture demuxes"); | |
| 392 | - let syncs = demuxer.sync_in_display_order(0); | |
| 393 | - assert!(!syncs.is_empty(), "B-frames: the display table answers"); | |
| 394 | - assert!( | |
| 395 | - syncs.windows(2).all(|w| w[0].0 < w[1].0 && w[0].1 != w[1].1), | |
| 396 | - "display indices ascend: {syncs:?}" | |
| 397 | - ); | |
| 398 | - let samples = demuxer.samples(0); | |
| 399 | - for &(_, decode) in &syncs { | |
| 400 | - assert!(samples[decode as usize].sync, "decode {decode} is a sync sample"); | |
| 401 | - } | |
| 402 | -} |
crates/ec-mp4/tests/it/main.rsmodified+0 -1
| @@ -7,7 +7,6 @@ mod chapters; | ||
| 7 | 7 | mod common; |
| 8 | 8 | mod dts; |
| 9 | 9 | mod edit_trim; |
| 10 | -mod edith_parity; | |
| 11 | 10 | mod fuzz_regressions; |
| 12 | 11 | mod mp4; |
| 13 | 12 | mod mux_entries; |
crates/ec-ogg/tests/real_library.rsdeleted+0 -315
| @@ -1,315 +0,0 @@ | ||
| 1 | -//! | |
| 2 | -//! Per file the oracle checks: | |
| 3 | -//! - packet count and every packet's size (ffprobe, audio stream 0); | |
| 4 | -//! - the concatenated packet payloads (ffmpeg `-c copy -f data`), byte for byte; | |
| 5 | -//! - the header block: Opus's `OpusHead` against ffprobe's `extradata_size` | |
| 6 | -//! exactly; Vorbis's three Xiph-laced packets by their lacing and magic (see | |
| 7 | -//! below for why the size is not compared); | |
| 8 | -//! - timing: where a packet carries a pts, it equals ffprobe's pts after the | |
| 9 | -//! Opus pre-skip shift; where a page ends on a packet, the granule position | |
| 10 | -//! equals that packet's ffprobe end (pts + duration) after the same shift. | |
| 11 | -//! | |
| 12 | -//! Timing rule, as ffmpeg's Ogg demuxer applies it (measured on these files): | |
| 13 | -//! - Vorbis: granule = the sample position at the packet's end, at the stream | |
| 14 | -//! rate; ffmpeg's pts = granule - duration. No shift. The first packet is the | |
| 15 | -//! one exception: ffmpeg starts it at minus its own length, while ec-ogg, by | |
| 16 | -//! its documented convention, starts the stream at zero and states no | |
| 17 | -//! duration for a Vorbis packet. Its pts is therefore not compared; its | |
| 18 | -//! granule still is. | |
| 19 | -//! - Opus: granule counts 48 kHz samples *including* the pre-skip; ffmpeg's pts | |
| 20 | -//! = granule - pre_skip - duration, so the end is `granule - pre_skip`. Our | |
| 21 | -//! packet pts start at zero, so ours = ffmpeg's pts + pre_skip. | |
| 22 | -//! | |
| 23 | -//! Header block: ffmpeg's stated Vorbis extradata size differs from ours on | |
| 24 | -//! some files (ours keeps the comment header verbatim, which the remux path | |
| 25 | -//! relies on for bit-identical output). ffprobe does not expose the extradata | |
| 26 | -//! bytes, so the Vorbis block is checked structurally and the size difference | |
| 27 | -//! is reported, not failed. | |
| 28 | -//! | |
| 29 | -//! Scope: demux only. The decoded PCM is not compared here. | |
| 30 | -//! | |
| 31 | - | |
| 32 | -use std::path::Path; | |
| 33 | - | |
| 34 | -use ec_core::Demuxer; | |
| 35 | -use ec_ogg::{Mapping, OggDemuxer, granule_of}; | |
| 36 | - | |
| 37 | -/// One packet as ffprobe reports it. | |
| 38 | -struct Probed { | |
| 39 | - pts: Option<i64>, | |
| 40 | - duration: Option<i64>, | |
| 41 | - size: usize, | |
| 42 | -} | |
| 43 | - | |
| 44 | -/// The verdict for one file, with the text the table prints. | |
| 45 | -enum Verdict { | |
| 46 | - Match(String), | |
| 47 | - Refused(String), | |
| 48 | - Failed(String), | |
| 49 | -} | |
| 50 | - | |
| 51 | -/// Parses an ffprobe csv cell: `N/A` and blanks are absent. | |
| 52 | -fn cell(s: &str) -> Option<i64> { | |
| 53 | - let s = s.trim(); | |
| 54 | - if s.is_empty() || s == "N/A" { | |
| 55 | - None | |
| 56 | - } else { | |
| 57 | - s.parse().ok() | |
| 58 | - } | |
| 59 | -} | |
| 60 | - | |
| 61 | -/// Audio stream 0's packets: header packets are not among them, as with ours. | |
| 62 | -fn ffprobe_packets(path: &Path) -> Result<Vec<Probed>, String> { | |
| 63 | - let out = ec_testkit::ffprobe() | |
| 64 | - .args([ | |
| 65 | - "-v", | |
| 66 | - "error", | |
| 67 | - "-select_streams", | |
| 68 | - "a:0", | |
| 69 | - "-show_entries", | |
| 70 | - "packet=pts,duration,size", | |
| 71 | - "-of", | |
| 72 | - "csv=p=0", | |
| 73 | - ]) | |
| 74 | - .arg(path) | |
| 75 | - .output() | |
| 76 | - .map_err(|e| format!("ffprobe: {e}"))?; | |
| 77 | - if !out.status.success() { | |
| 78 | - return Err("ffprobe failed on the packets".into()); | |
| 79 | - } | |
| 80 | - let mut packets = Vec::new(); | |
| 81 | - for line in String::from_utf8_lossy(&out.stdout).lines() { | |
| 82 | - let f: Vec<&str> = line.split(',').collect(); | |
| 83 | - if f.len() < 3 { | |
| 84 | - return Err(format!("unexpected ffprobe packet row: {line}")); | |
| 85 | - } | |
| 86 | - let size = cell(f[2]).ok_or_else(|| format!("no packet size in: {line}"))?; | |
| 87 | - packets.push(Probed { | |
| 88 | - pts: cell(f[0]), | |
| 89 | - duration: cell(f[1]), | |
| 90 | - size: size as usize, | |
| 91 | - }); | |
| 92 | - } | |
| 93 | - Ok(packets) | |
| 94 | -} | |
| 95 | - | |
| 96 | -/// ffprobe's stated extradata size for audio stream 0. | |
| 97 | -fn ffprobe_extradata_size(path: &Path) -> Result<usize, String> { | |
| 98 | - let out = ec_testkit::ffprobe() | |
| 99 | - .args([ | |
| 100 | - "-v", | |
| 101 | - "error", | |
| 102 | - "-select_streams", | |
| 103 | - "a:0", | |
| 104 | - "-show_entries", | |
| 105 | - "stream=extradata_size", | |
| 106 | - "-of", | |
| 107 | - "csv=p=0", | |
| 108 | - ]) | |
| 109 | - .arg(path) | |
| 110 | - .output() | |
| 111 | - .map_err(|e| format!("ffprobe: {e}"))?; | |
| 112 | - let text = String::from_utf8_lossy(&out.stdout); | |
| 113 | - let first = text.lines().next().unwrap_or_default().trim_matches(','); | |
| 114 | - cell(first) | |
| 115 | - .map(|n| n as usize) | |
| 116 | - .ok_or_else(|| format!("no extradata_size: {text:?}")) | |
| 117 | -} | |
| 118 | - | |
| 119 | -/// ffmpeg's concatenated audio packet payloads, copied and not decoded. | |
| 120 | -fn ffmpeg_payload(path: &Path) -> Result<Vec<u8>, String> { | |
| 121 | - let out = ec_testkit::ffmpeg() | |
| 122 | - .args(["-v", "error", "-i"]) | |
| 123 | - .arg(path) | |
| 124 | - .args(["-map", "0:a:0", "-c", "copy", "-f", "data", "-"]) | |
| 125 | - .output() | |
| 126 | - .map_err(|e| format!("ffmpeg: {e}"))?; | |
| 127 | - if !out.status.success() { | |
| 128 | - return Err(format!( | |
| 129 | - "ffmpeg failed to copy the packets: {}", | |
| 130 | - String::from_utf8_lossy(&out.stderr).trim() | |
| 131 | - )); | |
| 132 | - } | |
| 133 | - Ok(out.stdout) | |
| 134 | -} | |
| 135 | - | |
| 136 | -/// Reads one file with ec-ogg and compares it with the oracle. | |
| 137 | -fn check_file(path: &Path) -> Verdict { | |
| 138 | - let file = match std::fs::File::open(path) { | |
| 139 | - Ok(f) => f, | |
| 140 | - Err(e) => return Verdict::Failed(format!("open: {e}")), | |
| 141 | - }; | |
| 142 | - let mut demuxer = match OggDemuxer::open(file) { | |
| 143 | - Ok(d) => d, | |
| 144 | - Err(e) => { | |
| 145 | - let text = e.to_string(); | |
| 146 | - if text.contains("no logical stream with a known mapping") { | |
| 147 | - return Verdict::Refused(text); | |
| 148 | - } | |
| 149 | - return Verdict::Failed(format!("open: {text}")); | |
| 150 | - } | |
| 151 | - }; | |
| 152 | - let Some(mapping) = demuxer.mapping(0) else { | |
| 153 | - return Verdict::Refused("no audio stream with a known mapping".into()); | |
| 154 | - }; | |
| 155 | - let pre_skip = match mapping { | |
| 156 | - Mapping::Opus { pre_skip, .. } => i64::from(pre_skip), | |
| 157 | - _ => 0, | |
| 158 | - }; | |
| 159 | - let streams = demuxer.streams().to_vec(); | |
| 160 | - let block: &[u8] = streams[0].params.extradata.as_ref().map_or(&[], |b| &b[..]); | |
| 161 | - let vorbis = matches!(mapping, Mapping::Vorbis { .. }); | |
| 162 | - | |
| 163 | - let mut ours = Vec::new(); | |
| 164 | - loop { | |
| 165 | - match demuxer.next_packet() { | |
| 166 | - Ok(p) if p.stream == 0 => ours.push(p), | |
| 167 | - Ok(_) => {} | |
| 168 | - Err(e) if e.is_eof() => break, | |
| 169 | - Err(e) => return Verdict::Failed(format!("demux: {e}")), | |
| 170 | - } | |
| 171 | - } | |
| 172 | - if demuxer.damaged_pages() != 0 { | |
| 173 | - return Verdict::Failed(format!("{} damaged pages", demuxer.damaged_pages())); | |
| 174 | - } | |
| 175 | - | |
| 176 | - let probed = match ffprobe_packets(path) { | |
| 177 | - Ok(p) => p, | |
| 178 | - Err(e) => return Verdict::Failed(e), | |
| 179 | - }; | |
| 180 | - let payload = match ffmpeg_payload(path) { | |
| 181 | - Ok(p) => p, | |
| 182 | - Err(e) => return Verdict::Failed(e), | |
| 183 | - }; | |
| 184 | - let header_len = match ffprobe_extradata_size(path) { | |
| 185 | - Ok(n) => n, | |
| 186 | - Err(e) => return Verdict::Failed(e), | |
| 187 | - }; | |
| 188 | - | |
| 189 | - let mut problems: Vec<String> = Vec::new(); | |
| 190 | - if ours.len() != probed.len() { | |
| 191 | - problems.push(format!( | |
| 192 | - "packet count {} (ffprobe {})", | |
| 193 | - ours.len(), | |
| 194 | - probed.len() | |
| 195 | - )); | |
| 196 | - } | |
| 197 | - let size_bad = ours | |
| 198 | - .iter() | |
| 199 | - .zip(&probed) | |
| 200 | - .filter(|(p, q)| p.data.len() != q.size) | |
| 201 | - .count(); | |
| 202 | - if size_bad > 0 { | |
| 203 | - problems.push(format!("{size_bad} packet sizes differ")); | |
| 204 | - } | |
| 205 | - let ours_payload: Vec<u8> = ours.iter().flat_map(|p| p.data.iter().copied()).collect(); | |
| 206 | - if ours_payload != payload { | |
| 207 | - let at = ours_payload | |
| 208 | - .iter() | |
| 209 | - .zip(&payload) | |
| 210 | - .position(|(a, b)| a != b) | |
| 211 | - .unwrap_or(ours_payload.len().min(payload.len())); | |
| 212 | - problems.push(format!( | |
| 213 | - "payload differs: {} bytes vs ffmpeg {}, first difference at byte {at}", | |
| 214 | - ours_payload.len(), | |
| 215 | - payload.len() | |
| 216 | - )); | |
| 217 | - } | |
| 218 | - if vorbis { | |
| 219 | - if let Some(p) = vorbis_header_problem(block) { | |
| 220 | - problems.push(p); | |
| 221 | - } | |
| 222 | - } else if block.len() != header_len { | |
| 223 | - problems.push(format!( | |
| 224 | - "header block {} bytes (ffprobe extradata {header_len})", | |
| 225 | - block.len() | |
| 226 | - )); | |
| 227 | - } | |
| 228 | - | |
| 229 | - // Timing, checked on the pairs both sides state. | |
| 230 | - let mut pts_checked = 0usize; | |
| 231 | - let mut pts_bad = 0usize; | |
| 232 | - let mut granule_checked = 0usize; | |
| 233 | - let mut granule_bad = 0usize; | |
| 234 | - let mut first_timing: Option<String> = None; | |
| 235 | - for (i, (p, q)) in ours.iter().zip(&probed).enumerate() { | |
| 236 | - // The Vorbis first packet's start is the convention exception (see the | |
| 237 | - // module doc): not compared. | |
| 238 | - if let Some(pts) = p.pts.filter(|_| i > 0 || !vorbis) { | |
| 239 | - pts_checked += 1; | |
| 240 | - if q.pts.map(|f| f + pre_skip) != Some(pts) { | |
| 241 | - pts_bad += 1; | |
| 242 | - first_timing.get_or_insert(format!( | |
| 243 | - "packet {i}: pts {pts}, ffmpeg pts {:?} (+{pre_skip})", | |
| 244 | - q.pts | |
| 245 | - )); | |
| 246 | - } | |
| 247 | - } | |
| 248 | - if let Some(g) = granule_of(p) { | |
| 249 | - granule_checked += 1; | |
| 250 | - let end = q.pts.zip(q.duration).map(|(a, b)| a + b); | |
| 251 | - if end != Some(g - pre_skip) { | |
| 252 | - granule_bad += 1; | |
| 253 | - first_timing.get_or_insert(format!( | |
| 254 | - "packet {i}: granule {g}, ffmpeg end {end:?} (-{pre_skip})" | |
| 255 | - )); | |
| 256 | - } | |
| 257 | - } | |
| 258 | - } | |
| 259 | - if pts_bad > 0 { | |
| 260 | - problems.push(format!("{pts_bad} of {pts_checked} pts differ")); | |
| 261 | - } | |
| 262 | - if granule_bad > 0 { | |
| 263 | - problems.push(format!( | |
| 264 | - "{granule_bad} of {granule_checked} granule ends differ" | |
| 265 | - )); | |
| 266 | - } | |
| 267 | - if let Some(t) = first_timing { | |
| 268 | - problems.push(format!("first timing difference: {t}")); | |
| 269 | - } | |
| 270 | - | |
| 271 | - let summary = format!( | |
| 272 | - "{} packets, {} bytes, {} pts and {} granules checked", | |
| 273 | - ours.len(), | |
| 274 | - payload.len(), | |
| 275 | - pts_checked, | |
| 276 | - granule_checked | |
| 277 | - ); | |
| 278 | - if problems.is_empty() { | |
| 279 | - Verdict::Match(summary) | |
| 280 | - } else { | |
| 281 | - Verdict::Failed(format!("{summary}; {}", problems.join("; "))) | |
| 282 | - } | |
| 283 | -} | |
| 284 | - | |
| 285 | -/// Our Vorbis header block: three Xiph-laced packets, identification, | |
| 286 | -/// comment and setup, each carrying its own magic. `None` when it is sound. | |
| 287 | -fn vorbis_header_problem(block: &[u8]) -> Option<String> { | |
| 288 | - let Ok(parts) = ec_ogg::xiph_unlace(block) else { | |
| 289 | - return Some("header block is not Xiph-laced".into()); | |
| 290 | - }; | |
| 291 | - if parts.len() != 3 { | |
| 292 | - return Some(format!("{} header packets, want 3", parts.len())); | |
| 293 | - } | |
| 294 | - for (part, kind) in parts.iter().zip([1u8, 3, 5]) { | |
| 295 | - if part.len() < 7 || part[0] != kind || &part[1..7] != b"vorbis" { | |
| 296 | - return Some(format!("header packet of kind {kind} lacks its magic")); | |
| 297 | - } | |
| 298 | - } | |
| 299 | - if parts[0].len() != 30 { | |
| 300 | - return Some(format!( | |
| 301 | - "identification header is {} bytes, want 30", | |
| 302 | - parts[0].len() | |
| 303 | - )); | |
| 304 | - } | |
| 305 | - None | |
| 306 | -} | |
| 307 | - | |
| 308 | -#[test] | |
| 309 | -fn real_library_ogg_streams_match_ffmpeg() { | |
| 310 | - let path = ec_testkit::fixture("open/sintel_trailer-480p.ogv"); | |
| 311 | - match check_file(&path) { | |
| 312 | - Verdict::Match(s) => eprintln!("{}: match: {s}", path.display()), | |
| 313 | - Verdict::Refused(e) | Verdict::Failed(e) => panic!("{}: {e}", path.display()), | |
| 314 | - } | |
| 315 | -} |