Merge branch 'simplify/g1-demux' into simplify/g1
Browse files at this commit1 changed file +236 -1
- tools/golden/src/demux.rs +236 -1
tools/golden/src/demux.rsmodified+236 -1
| @@ -1 +1,236 @@ | |||
| 1 | -pub fn run() {} | 1 | +use crate::{error, guard, hash, name, text}; |
| 2 | +use ec_core::error::Error as CoreError; | ||
| 3 | +use ec_core::packet::{Packet, SideData}; | ||
| 4 | +use ec_core::registry::{Demuxer, StreamInfo}; | ||
| 5 | +use ec_probe::Reader; | ||
| 6 | +use std::fs::File; | ||
| 7 | +use std::io::BufReader; | ||
| 8 | +use std::path::{Path, PathBuf}; | ||
| 9 | + | ||
| 10 | +const PACKETS: usize = 300; | ||
| 11 | +const FILES: usize = 30; | ||
| 12 | + | ||
| 13 | +const ROOTS: [&str; 11] = [ | ||
| 14 | + "audio", | ||
| 15 | + "bitstreams", | ||
| 16 | + "codecs", | ||
| 17 | + "derived", | ||
| 18 | + "mp4corpus", | ||
| 19 | + "open", | ||
| 20 | + "subs", | ||
| 21 | + "video", | ||
| 22 | + "vectors", | ||
| 23 | + "vp8", | ||
| 24 | + "vp9", | ||
| 25 | +]; | ||
| 26 | + | ||
| 27 | +const CONTAINERS: [(&str, &[&str]); 7] = [ | ||
| 28 | + ("mp4", &["mp4", "mov", "m4a"]), | ||
| 29 | + ("mkv", &["mkv", "webm", "mka"]), | ||
| 30 | + ("ts", &["ts", "m2ts"]), | ||
| 31 | + ("ps", &["ps", "vob", "mpg"]), | ||
| 32 | + ("ogg", &["ogg", "opus"]), | ||
| 33 | + ("avi", &["avi"]), | ||
| 34 | + ("wav", &["wav"]), | ||
| 35 | +]; | ||
| 36 | + | ||
| 37 | +pub fn run() { | ||
| 38 | + let files = fixture_files(); | ||
| 39 | + for (container, exts) in CONTAINERS { | ||
| 40 | + let picked = files | ||
| 41 | + .iter() | ||
| 42 | + .filter(|path| has_ext(path, exts)) | ||
| 43 | + .take(FILES); | ||
| 44 | + for path in picked { | ||
| 45 | + let case = format!("demux/{container}/{}", name(path)); | ||
| 46 | + guard(&case, || run_file(container, path)); | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +fn fixture_files() -> Vec<PathBuf> { | ||
| 52 | + let root = ec_testkit::root(); | ||
| 53 | + let mut files = Vec::new(); | ||
| 54 | + for dir in ROOTS { | ||
| 55 | + let path = root.join(dir); | ||
| 56 | + if path.is_dir() { | ||
| 57 | + walk(&path, &mut files); | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + files | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +fn walk(dir: &Path, out: &mut Vec<PathBuf>) { | ||
| 64 | + let mut entries: Vec<PathBuf> = std::fs::read_dir(dir) | ||
| 65 | + .expect("fixture directory") | ||
| 66 | + .map(|entry| entry.expect("fixture entry").path()) | ||
| 67 | + .collect(); | ||
| 68 | + entries.sort(); | ||
| 69 | + for path in entries { | ||
| 70 | + if path.is_dir() { | ||
| 71 | + walk(&path, out); | ||
| 72 | + } else { | ||
| 73 | + out.push(path); | ||
| 74 | + } | ||
| 75 | + } | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +fn has_ext(path: &Path, exts: &[&str]) -> bool { | ||
| 79 | + path.extension() | ||
| 80 | + .and_then(|e| e.to_str()) | ||
| 81 | + .is_some_and(|e| exts.contains(&e)) | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +fn run_file(container: &str, path: &Path) { | ||
| 85 | + let rel = name(path); | ||
| 86 | + let case = format!("demux/{container}/{rel}"); | ||
| 87 | + match open_demuxer(container, path) { | ||
| 88 | + Ok(Some(mut demuxer)) => through_demuxer(&case, demuxer.as_mut()), | ||
| 89 | + Ok(None) => {} | ||
| 90 | + Err(e) => error(&case, e), | ||
| 91 | + } | ||
| 92 | + through_reader(&format!("demux/probe/{rel}"), path); | ||
| 93 | +} | ||
| 94 | + | ||
| 95 | +fn open_demuxer(container: &str, path: &Path) -> ec_core::error::Result<Option<Box<dyn Demuxer>>> { | ||
| 96 | + let file = BufReader::new(File::open(path)?); | ||
| 97 | + let demuxer: Box<dyn Demuxer> = match container { | ||
| 98 | + "mp4" => Box::new(ec_mp4::Mp4Demuxer::new(file)?), | ||
| 99 | + "mkv" => Box::new(ec_matroska::MatroskaDemuxer::new(file)?), | ||
| 100 | + "ts" => Box::new(ec_mpegts::TsDemuxer::new(file)?), | ||
| 101 | + "ps" => Box::new(ec_mpegts::PsDemuxer::new(file)?), | ||
| 102 | + "ogg" => Box::new(ec_ogg::OggDemuxer::open(file)?), | ||
| 103 | + "avi" => Box::new(ec_riff::AviDemuxer::new(file)?), | ||
| 104 | + _ => return Ok(None), | ||
| 105 | + }; | ||
| 106 | + Ok(Some(demuxer)) | ||
| 107 | +} | ||
| 108 | + | ||
| 109 | +fn through_demuxer(case: &str, demuxer: &mut dyn Demuxer) { | ||
| 110 | + let streams: Vec<String> = demuxer.streams().iter().map(stream_line).collect(); | ||
| 111 | + text(case, streams.join(" ")); | ||
| 112 | + for index in 0..PACKETS { | ||
| 113 | + match demuxer.next_packet() { | ||
| 114 | + Ok(packet) => packet_line(case, index, &packet), | ||
| 115 | + Err(CoreError::Eof) => break, | ||
| 116 | + Err(e) => { | ||
| 117 | + error(&format!("{case}/{index}"), e); | ||
| 118 | + break; | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +fn through_reader(case: &str, path: &Path) { | ||
| 125 | + let mut reader = match Reader::open(path) { | ||
| 126 | + Ok(reader) => reader, | ||
| 127 | + Err(e) => { | ||
| 128 | + error(case, e); | ||
| 129 | + return; | ||
| 130 | + } | ||
| 131 | + }; | ||
| 132 | + text( | ||
| 133 | + case, | ||
| 134 | + format!( | ||
| 135 | + "format={} duration={:?}", | ||
| 136 | + reader.format().name(), | ||
| 137 | + reader.duration().map(|t| (t.ticks, t.base)) | ||
| 138 | + ), | ||
| 139 | + ); | ||
| 140 | + text(case, format!("tags={:?}", reader.tags())); | ||
| 141 | + for unsupported in reader.unsupported() { | ||
| 142 | + text( | ||
| 143 | + case, | ||
| 144 | + format!( | ||
| 145 | + "unsupported {} {:?} {}", | ||
| 146 | + unsupported.stream, unsupported.codec, unsupported.reason | ||
| 147 | + ), | ||
| 148 | + ); | ||
| 149 | + } | ||
| 150 | + for index in 0..PACKETS { | ||
| 151 | + match reader.next_packet() { | ||
| 152 | + Ok(packet) => packet_line(case, index, &packet), | ||
| 153 | + Err(CoreError::Eof) => break, | ||
| 154 | + Err(e) => { | ||
| 155 | + error(&format!("{case}/{index}"), e); | ||
| 156 | + break; | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + } | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +fn stream_line(s: &StreamInfo) -> String { | ||
| 163 | + format!( | ||
| 164 | + "[{} {:?} {:?} start={} dur={} lang={} pad={} default={}]", | ||
| 165 | + s.index, | ||
| 166 | + s.params.codec, | ||
| 167 | + s.time_base, | ||
| 168 | + opt(s.start_time), | ||
| 169 | + opt(s.duration), | ||
| 170 | + s.language.as_deref().unwrap_or("-"), | ||
| 171 | + s.initial_padding, | ||
| 172 | + s.default | ||
| 173 | + ) | ||
| 174 | +} | ||
| 175 | + | ||
| 176 | +fn packet_line(case: &str, index: usize, p: &Packet) { | ||
| 177 | + let case = format!("{case}/{index}"); | ||
| 178 | + text( | ||
| 179 | + &case, | ||
| 180 | + format!( | ||
| 181 | + "s{} tb={:?} pts={} dts={} dur={} flags={} side={} len={}", | ||
| 182 | + p.stream, | ||
| 183 | + p.time_base, | ||
| 184 | + opt(p.pts), | ||
| 185 | + opt(p.dts), | ||
| 186 | + opt(p.duration), | ||
| 187 | + flags(p), | ||
| 188 | + side(p), | ||
| 189 | + p.data.len() | ||
| 190 | + ), | ||
| 191 | + ); | ||
| 192 | + hash(&format!("{case}/payload"), &p.data); | ||
| 193 | +} | ||
| 194 | + | ||
| 195 | +fn opt(value: Option<i64>) -> String { | ||
| 196 | + value.map_or_else(|| "-".to_owned(), |v| v.to_string()) | ||
| 197 | +} | ||
| 198 | + | ||
| 199 | +fn flags(p: &Packet) -> String { | ||
| 200 | + let f = p.flags; | ||
| 201 | + let mut out = String::new(); | ||
| 202 | + for (on, letter) in [ | ||
| 203 | + (f.keyframe, 'K'), | ||
| 204 | + (f.header, 'H'), | ||
| 205 | + (f.corrupt, 'C'), | ||
| 206 | + (f.discard, 'D'), | ||
| 207 | + ] { | ||
| 208 | + if on { | ||
| 209 | + out.push(letter); | ||
| 210 | + } | ||
| 211 | + } | ||
| 212 | + if out.is_empty() { | ||
| 213 | + out.push('-'); | ||
| 214 | + } | ||
| 215 | + out | ||
| 216 | +} | ||
| 217 | + | ||
| 218 | +fn side(p: &Packet) -> String { | ||
| 219 | + if p.side_data.is_empty() { | ||
| 220 | + return "-".to_owned(); | ||
| 221 | + } | ||
| 222 | + p.side_data | ||
| 223 | + .iter() | ||
| 224 | + .map(side_tag) | ||
| 225 | + .collect::<Vec<String>>() | ||
| 226 | + .join(",") | ||
| 227 | +} | ||
| 228 | + | ||
| 229 | +fn side_tag(s: &SideData) -> String { | ||
| 230 | + match s { | ||
| 231 | + SideData::CodecConfig(b) => format!("cfg:{}", b.len()), | ||
| 232 | + SideData::Palette(b) => format!("pal:{}", b.len()), | ||
| 233 | + SideData::DisplayMatrix(b) => format!("dm:{}", b.len()), | ||
| 234 | + SideData::Custom { kind, data } => format!("c{kind}:{}", data.len()), | ||
| 235 | + } | ||
| 236 | +} | ||