| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::fs::File; |
| 6 | use std::io::BufReader; |
| 7 | use std::path::PathBuf; |
| 8 | use std::process::Command; |
| 9 | |
| 10 | use ec_core::{CodecId, Demuxer}; |
| 11 | use ec_matroska::MatroskaDemuxer; |
| 12 | use ec_truehd::sync::AccessUnitHeader; |
| 13 | |
| 14 | |
| 15 | |
| 16 | fn find_file() -> Option<PathBuf> { |
| 17 | let home = std::env::var("HOME").ok()?; |
| 18 | let downloads = PathBuf::from(home).join("Downloads"); |
| 19 | for entry in glob_dir(&downloads) { |
| 20 | let name = entry.file_name()?.to_str()?; |
| 21 | if name.contains("ReelR") && name.ends_with(".mkv") { |
| 22 | return Some(entry); |
| 23 | } |
| 24 | } |
| 25 | None |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | fn glob_dir(dir: &std::path::Path) -> Vec<PathBuf> { |
| 32 | let mut out = Vec::new(); |
| 33 | let Ok(entries) = std::fs::read_dir(dir) else { |
| 34 | return out; |
| 35 | }; |
| 36 | for entry in entries.flatten() { |
| 37 | let path = entry.path(); |
| 38 | if path.is_dir() { |
| 39 | out.extend(glob_dir(&path)); |
| 40 | } else { |
| 41 | out.push(path); |
| 42 | } |
| 43 | } |
| 44 | out |
| 45 | } |
| 46 | |
| 47 | fn have_ffprobe() -> bool { |
| 48 | Command::new("ffprobe") |
| 49 | .arg("-version") |
| 50 | .output() |
| 51 | .map(|o| o.status.success()) |
| 52 | .unwrap_or(false) |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | fn ffprobe_packet_count(path: &std::path::Path, stream: usize) -> Option<u64> { |
| 58 | let out = Command::new("ffprobe") |
| 59 | .args(["-v", "error", "-select_streams"]) |
| 60 | .arg(stream.to_string()) |
| 61 | .args([ |
| 62 | "-count_packets", |
| 63 | "-show_entries", |
| 64 | "stream=nb_read_packets", |
| 65 | "-of", |
| 66 | "csv=p=0", |
| 67 | ]) |
| 68 | .arg(path) |
| 69 | .output() |
| 70 | .ok()?; |
| 71 | if !out.status.success() { |
| 72 | return None; |
| 73 | } |
| 74 | String::from_utf8_lossy(&out.stdout).trim().parse().ok() |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn walks_every_truehd_access_unit_of_a_real_7_1_remux() { |
| 79 | let Some(path) = find_file() else { |
| 80 | eprintln!("skip: no 'ReelR ... TrueHD ...mkv' under ~/Downloads"); |
| 81 | return; |
| 82 | }; |
| 83 | |
| 84 | let file = File::open(&path).expect("open the file"); |
| 85 | let mut demux = MatroskaDemuxer::new(BufReader::new(file)).expect("open the container"); |
| 86 | |
| 87 | let track = demux |
| 88 | .streams() |
| 89 | .iter() |
| 90 | .find(|s| s.params.codec == CodecId::TrueHd) |
| 91 | .expect("a TrueHD track"); |
| 92 | let stream_index = track.index; |
| 93 | let audio = track.params.audio().expect("audio parameters"); |
| 94 | assert_eq!(audio.sample_rate, 48_000, "rate"); |
| 95 | assert_eq!(audio.layout.channel_count(), 8, "channels"); |
| 96 | |
| 97 | |
| 98 | |
| 99 | eprintln!("container-reported bit depth: {:?}", audio.bits_per_sample); |
| 100 | |
| 101 | let mut au_count = 0u64; |
| 102 | let mut major_syncs = 0u64; |
| 103 | loop { |
| 104 | let packet = match demux.next_packet() { |
| 105 | Ok(p) => p, |
| 106 | Err(e) if e.is_eof() => break, |
| 107 | Err(e) => panic!("demux error: {e}"), |
| 108 | }; |
| 109 | if packet.stream != stream_index { |
| 110 | continue; |
| 111 | } |
| 112 | let header = AccessUnitHeader::parse(&packet.data) |
| 113 | .unwrap_or_else(|e| panic!("access unit {au_count}: {e}")); |
| 114 | assert_eq!( |
| 115 | header.length, |
| 116 | packet.data.len(), |
| 117 | "access unit {au_count} claims a length that does not match its packet" |
| 118 | ); |
| 119 | if header.major_sync.is_some() { |
| 120 | major_syncs += 1; |
| 121 | } |
| 122 | au_count += 1; |
| 123 | } |
| 124 | |
| 125 | eprintln!("{au_count} access units, {major_syncs} carrying a major sync"); |
| 126 | assert!(au_count > 0, "found no TrueHD access units at all"); |
| 127 | |
| 128 | if have_ffprobe() { |
| 129 | |
| 130 | |
| 131 | let absolute = stream_index as usize; |
| 132 | if let Some(expected) = ffprobe_packet_count(&path, absolute) { |
| 133 | assert_eq!(au_count, expected, "AU count vs ffprobe's packet count"); |
| 134 | } else { |
| 135 | eprintln!("skip: ffprobe packet count unavailable for stream {absolute}"); |
| 136 | } |
| 137 | } else { |
| 138 | eprintln!("skip: ffprobe not on PATH, no oracle comparison"); |
| 139 | } |
| 140 | } |