tools/golden subs: ec-subs srt/vtt/ass cues, pgs, dvd (mkv/vob/mp4/idx+sub), dvb (mkv/ts) bitmaps, cc608/708 from mpeg2
Browse files at this commitb226b6719706eec61c2bba3c0e4a30d74a979af9Parent: 5b64ff02 changed files +342 -1
- tools/golden/Cargo.toml +4 -0
- tools/golden/src/subs.rs +338 -1
tools/golden/Cargo.tomlmodified+4 -0
| @@ -45,3 +45,7 @@ ec-vp8.workspace = true | |||
| 45 | ec-vp9.workspace = true | 45 | ec-vp9.workspace = true |
| 46 | ec-vp9-syntax.workspace = true | 46 | ec-vp9-syntax.workspace = true |
| 47 | sha2 = "0.11.3" | 47 | sha2 = "0.11.3" |
| 48 | +ec-subs = { path = "../../crates/ec-subs", version = "0.1.0" } | ||
| 49 | +ec-ass = { path = "../../crates/ec-ass", version = "0.1.0" } | ||
| 50 | +ec-pgs = { path = "../../crates/ec-pgs", version = "0.1.0" } | ||
| 51 | +ec-cc = { path = "../../crates/ec-cc", version = "0.1.0" } | ||
tools/golden/src/subs.rsmodified+338 -1
| @@ -1 +1,338 @@ | |||
| 1 | -pub fn run() {} | 1 | +use crate::{error, guard, hash, name, text}; |
| 2 | +use ec_core::frame::VideoFrame; | ||
| 3 | +use ec_core::{CodecId, Decoder, Demuxer, Error, TimeBase, Timestamp}; | ||
| 4 | +use std::fs::File; | ||
| 5 | +use std::io::BufReader; | ||
| 6 | +use std::path::{Path, PathBuf}; | ||
| 7 | + | ||
| 8 | +const TEXT_DIR: &str = "subs"; | ||
| 9 | +const PGS_DIR: &str = "open/fate/sub"; | ||
| 10 | +const MPEG2_DIR: &str = "codecs"; | ||
| 11 | +const DVD_DIR: &str = concat!( | ||
| 12 | + env!("CARGO_MANIFEST_DIR"), | ||
| 13 | + "/../../crates/ec-dvdsub/tests/data" | ||
| 14 | +); | ||
| 15 | +const DVB_DIR: &str = concat!( | ||
| 16 | + env!("CARGO_MANIFEST_DIR"), | ||
| 17 | + "/../../crates/ec-dvbsub/tests/data" | ||
| 18 | +); | ||
| 19 | +const CANVAS: (u32, u32) = (720, 576); | ||
| 20 | + | ||
| 21 | +pub fn run() { | ||
| 22 | + text_tracks(); | ||
| 23 | + pgs(); | ||
| 24 | + dvd(); | ||
| 25 | + dvb(); | ||
| 26 | + captions(); | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +fn files(dir: &str, exts: &[&str]) -> Vec<PathBuf> { | ||
| 30 | + let Ok(entries) = std::fs::read_dir(ec_testkit::root().join(dir)) else { | ||
| 31 | + return Vec::new(); | ||
| 32 | + }; | ||
| 33 | + let mut found: Vec<PathBuf> = entries | ||
| 34 | + .map(|entry| entry.expect("fixture entry").path()) | ||
| 35 | + .filter(|p| { | ||
| 36 | + p.extension() | ||
| 37 | + .and_then(|e| e.to_str()) | ||
| 38 | + .is_some_and(|e| exts.contains(&e)) | ||
| 39 | + }) | ||
| 40 | + .collect(); | ||
| 41 | + found.sort(); | ||
| 42 | + found | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +fn bytes_of(path: &Path) -> Vec<u8> { | ||
| 46 | + std::fs::read(path).expect("read fixture") | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +fn reader(path: &Path) -> BufReader<File> { | ||
| 50 | + BufReader::new(File::open(path).expect("open fixture")) | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +fn canvas(case: &str, meta: &str, frame: &VideoFrame) { | ||
| 54 | + let mut bytes = format!( | ||
| 55 | + "{}x{} pts={:?} {meta}\n", | ||
| 56 | + frame.width, frame.height, frame.pts | ||
| 57 | + ) | ||
| 58 | + .into_bytes(); | ||
| 59 | + bytes.extend_from_slice(&frame.planes[0].data); | ||
| 60 | + hash(case, &bytes); | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +fn text_tracks() { | ||
| 64 | + for path in files(TEXT_DIR, &["srt", "vtt", "ass", "ssa"]) { | ||
| 65 | + let codec = path | ||
| 66 | + .extension() | ||
| 67 | + .and_then(|e| e.to_str()) | ||
| 68 | + .unwrap_or_default() | ||
| 69 | + .to_string(); | ||
| 70 | + let base = format!("subs/{codec}/{}", name(&path)); | ||
| 71 | + guard(&base, || text_track(&base, &codec, &path)); | ||
| 72 | + } | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +fn text_track(base: &str, codec: &str, path: &Path) { | ||
| 76 | + let bytes = bytes_of(path); | ||
| 77 | + let parsed = match codec { | ||
| 78 | + "srt" => ec_subs::srt::parse(&bytes), | ||
| 79 | + "vtt" => ec_subs::webvtt::parse(&bytes), | ||
| 80 | + _ => ec_ass::parse(&bytes), | ||
| 81 | + }; | ||
| 82 | + let track = match parsed { | ||
| 83 | + Ok(track) => track, | ||
| 84 | + Err(e) => { | ||
| 85 | + error(base, e); | ||
| 86 | + return; | ||
| 87 | + } | ||
| 88 | + }; | ||
| 89 | + text( | ||
| 90 | + &format!("{base}/track"), | ||
| 91 | + format!( | ||
| 92 | + "cues={} skipped={} source={:?}", | ||
| 93 | + track.cues.len(), | ||
| 94 | + track.skipped, | ||
| 95 | + track.source | ||
| 96 | + ), | ||
| 97 | + ); | ||
| 98 | + hash( | ||
| 99 | + &format!("{base}/styles"), | ||
| 100 | + format!("{:?}{:?}", track.styles, track.metadata).as_bytes(), | ||
| 101 | + ); | ||
| 102 | + hash(&format!("{base}/extradata"), &track.extradata); | ||
| 103 | + for (i, cue) in track.cues.iter().enumerate() { | ||
| 104 | + hash(&format!("{base}/{i}"), format!("{cue:?}").as_bytes()); | ||
| 105 | + } | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +fn pgs() { | ||
| 109 | + for path in files(PGS_DIR, &["sup"]) { | ||
| 110 | + let base = format!("subs/pgs/{}", name(&path)); | ||
| 111 | + guard(&base, || pgs_file(&base, &path)); | ||
| 112 | + } | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +fn pgs_file(base: &str, path: &Path) { | ||
| 116 | + let mut dec = ec_pgs::PgsDecoder::new(); | ||
| 117 | + if let Err(e) = dec.push(&bytes_of(path)) { | ||
| 118 | + error(base, e); | ||
| 119 | + return; | ||
| 120 | + } | ||
| 121 | + let mut i = 0; | ||
| 122 | + while let Some(frame) = dec.take_frame() { | ||
| 123 | + canvas(&format!("{base}/{i}"), "", &frame); | ||
| 124 | + i += 1; | ||
| 125 | + } | ||
| 126 | +} | ||
| 127 | + | ||
| 128 | +fn sub_stream(d: &dyn Demuxer, codec: CodecId) -> Option<u32> { | ||
| 129 | + d.streams() | ||
| 130 | + .iter() | ||
| 131 | + .position(|s| s.params.codec == codec) | ||
| 132 | + .map(|i| i as u32) | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +fn dvd() { | ||
| 136 | + let dir = Path::new(DVD_DIR); | ||
| 137 | + let mkv = format!("subs/dvd/{}", "dvd.mkv"); | ||
| 138 | + guard(&mkv, || dvd_mkv(&mkv, &dir.join("dvd.mkv"))); | ||
| 139 | + let vob = format!("subs/dvd/{}", "dvd.vob"); | ||
| 140 | + guard(&vob, || dvd_vob(&vob, &dir.join("dvd.vob"))); | ||
| 141 | + let mp4 = format!("subs/dvd/{}", "dvd.mp4"); | ||
| 142 | + guard(&mp4, || dvd_mp4(&mp4, &dir.join("dvd.mp4"))); | ||
| 143 | + let idx = format!("subs/dvd/{}", "dvd.idx+sub"); | ||
| 144 | + guard(&idx, || { | ||
| 145 | + dvd_idx(&idx, &dir.join("dvd.idx"), &dir.join("dvd.sub")) | ||
| 146 | + }); | ||
| 147 | +} | ||
| 148 | + | ||
| 149 | +fn dvd_mkv(base: &str, path: &Path) { | ||
| 150 | + match ec_matroska::MatroskaDemuxer::new(reader(path)) { | ||
| 151 | + Ok(mut d) => dvd_run(base, &mut d), | ||
| 152 | + Err(e) => error(base, e), | ||
| 153 | + } | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +fn dvd_vob(base: &str, path: &Path) { | ||
| 157 | + match ec_mpegts::PsDemuxer::new(reader(path)) { | ||
| 158 | + Ok(mut d) => dvd_run(base, &mut d), | ||
| 159 | + Err(e) => error(base, e), | ||
| 160 | + } | ||
| 161 | +} | ||
| 162 | + | ||
| 163 | +fn dvd_mp4(base: &str, path: &Path) { | ||
| 164 | + match ec_mp4::Mp4Demuxer::new(reader(path)) { | ||
| 165 | + Ok(mut d) => dvd_run(base, &mut d), | ||
| 166 | + Err(e) => error(base, e), | ||
| 167 | + } | ||
| 168 | +} | ||
| 169 | + | ||
| 170 | +fn dvd_run(base: &str, d: &mut dyn Demuxer) { | ||
| 171 | + let Some(stream) = sub_stream(d, CodecId::DvdSub) else { | ||
| 172 | + error(base, "no DvdSub stream"); | ||
| 173 | + return; | ||
| 174 | + }; | ||
| 175 | + let extra = d.streams()[stream as usize] | ||
| 176 | + .params | ||
| 177 | + .extradata | ||
| 178 | + .as_deref() | ||
| 179 | + .map(<[u8]>::to_vec); | ||
| 180 | + let mut dec = ec_dvdsub::DvdSubDecoder::new(extra.as_deref()); | ||
| 181 | + let mut i = 0; | ||
| 182 | + loop { | ||
| 183 | + let packet = match d.next_packet() { | ||
| 184 | + Ok(p) => p, | ||
| 185 | + Err(Error::Eof) => break, | ||
| 186 | + Err(e) => { | ||
| 187 | + error(base, e); | ||
| 188 | + break; | ||
| 189 | + } | ||
| 190 | + }; | ||
| 191 | + if packet.stream != stream { | ||
| 192 | + continue; | ||
| 193 | + } | ||
| 194 | + if let Err(e) = dec.send_packet(&packet) { | ||
| 195 | + error(&format!("{base}/{i}"), e); | ||
| 196 | + i += 1; | ||
| 197 | + continue; | ||
| 198 | + } | ||
| 199 | + while let Some(cue) = dec.take_cue() { | ||
| 200 | + let meta = format!( | ||
| 201 | + "packet_pts={:?} start={} end={:?} area={:?} forced={}", | ||
| 202 | + packet.pts, cue.start_90k, cue.end_90k, cue.area, cue.forced | ||
| 203 | + ); | ||
| 204 | + canvas(&format!("{base}/{i}"), &meta, &cue.frame); | ||
| 205 | + i += 1; | ||
| 206 | + } | ||
| 207 | + } | ||
| 208 | +} | ||
| 209 | + | ||
| 210 | +fn dvd_idx(base: &str, idx_path: &Path, sub_path: &Path) { | ||
| 211 | + let idx_bytes = bytes_of(idx_path); | ||
| 212 | + let idx = ec_dvdsub::parse_idx(&idx_bytes); | ||
| 213 | + text( | ||
| 214 | + &format!("{base}/index"), | ||
| 215 | + format!( | ||
| 216 | + "size={:?} languages={:?} entries={:?} palette={:?}", | ||
| 217 | + idx.size, idx.languages, idx.entries, idx.palette | ||
| 218 | + ), | ||
| 219 | + ); | ||
| 220 | + let sub = bytes_of(sub_path); | ||
| 221 | + let mut dec = ec_dvdsub::DvdSubDecoder::new(Some(idx_bytes.as_slice())); | ||
| 222 | + let ticks = TimeBase::try_new(1, 90_000).expect("90 kHz base"); | ||
| 223 | + let mut i = 0; | ||
| 224 | + for (unit, pts) in ec_dvdsub::sub_file_units(&sub, 0) { | ||
| 225 | + let case = format!("{base}/spu/{i}"); | ||
| 226 | + match ec_dvdsub::decode_spu(&unit, &ec_dvdsub::DEFAULT_PALETTE, CANVAS) { | ||
| 227 | + Ok(cue) => canvas( | ||
| 228 | + &case, | ||
| 229 | + &format!( | ||
| 230 | + "area={:?} start={} end={:?}", | ||
| 231 | + cue.area, cue.start_90k, cue.end_90k | ||
| 232 | + ), | ||
| 233 | + &cue.frame, | ||
| 234 | + ), | ||
| 235 | + Err(e) => error(&case, e), | ||
| 236 | + } | ||
| 237 | + if let Err(e) = dec.push_unit(&unit, Some(Timestamp::new(pts, ticks))) { | ||
| 238 | + error(&format!("{base}/{i}"), e); | ||
| 239 | + i += 1; | ||
| 240 | + continue; | ||
| 241 | + } | ||
| 242 | + while let Some(cue) = dec.take_cue() { | ||
| 243 | + let meta = format!( | ||
| 244 | + "unit_pts={pts} start={} end={:?} area={:?}", | ||
| 245 | + cue.start_90k, cue.end_90k, cue.area | ||
| 246 | + ); | ||
| 247 | + canvas(&format!("{base}/{i}"), &meta, &cue.frame); | ||
| 248 | + i += 1; | ||
| 249 | + } | ||
| 250 | + } | ||
| 251 | +} | ||
| 252 | + | ||
| 253 | +fn dvb() { | ||
| 254 | + let dir = Path::new(DVB_DIR); | ||
| 255 | + let mkv = format!("subs/dvb/{}", "dvb.mkv"); | ||
| 256 | + guard(&mkv, || dvb_mkv(&mkv, &dir.join("dvb.mkv"))); | ||
| 257 | + let ts = format!("subs/dvb/{}", "dvb.ts"); | ||
| 258 | + guard(&ts, || dvb_ts(&ts, &dir.join("dvb.ts"))); | ||
| 259 | +} | ||
| 260 | + | ||
| 261 | +fn dvb_mkv(base: &str, path: &Path) { | ||
| 262 | + match ec_matroska::MatroskaDemuxer::new(reader(path)) { | ||
| 263 | + Ok(mut d) => dvb_run(base, &mut d), | ||
| 264 | + Err(e) => error(base, e), | ||
| 265 | + } | ||
| 266 | +} | ||
| 267 | + | ||
| 268 | +fn dvb_ts(base: &str, path: &Path) { | ||
| 269 | + match ec_mpegts::TsDemuxer::new(reader(path)) { | ||
| 270 | + Ok(mut d) => dvb_run(base, &mut d), | ||
| 271 | + Err(e) => error(base, e), | ||
| 272 | + } | ||
| 273 | +} | ||
| 274 | + | ||
| 275 | +fn dvb_run(base: &str, d: &mut dyn Demuxer) { | ||
| 276 | + let Some(stream) = sub_stream(d, CodecId::DvbSub) else { | ||
| 277 | + error(base, "no DvbSub stream"); | ||
| 278 | + return; | ||
| 279 | + }; | ||
| 280 | + let mut dec = ec_dvbsub::DvbSubDecoder::new(); | ||
| 281 | + let mut i = 0; | ||
| 282 | + loop { | ||
| 283 | + let packet = match d.next_packet() { | ||
| 284 | + Ok(p) => p, | ||
| 285 | + Err(Error::Eof) => break, | ||
| 286 | + Err(e) => { | ||
| 287 | + error(base, e); | ||
| 288 | + break; | ||
| 289 | + } | ||
| 290 | + }; | ||
| 291 | + if packet.stream != stream { | ||
| 292 | + continue; | ||
| 293 | + } | ||
| 294 | + if let Err(e) = dec.send_packet(&packet) { | ||
| 295 | + error(&format!("{base}/{i}"), e); | ||
| 296 | + i += 1; | ||
| 297 | + continue; | ||
| 298 | + } | ||
| 299 | + while let Some(cue) = dec.take_cue() { | ||
| 300 | + let meta = format!( | ||
| 301 | + "packet_pts={:?} timeout={} empty={} state={} regions={:?}", | ||
| 302 | + packet.pts, cue.timeout_ms, cue.empty, cue.page_state, cue.regions | ||
| 303 | + ); | ||
| 304 | + canvas(&format!("{base}/{i}"), &meta, &cue.frame); | ||
| 305 | + i += 1; | ||
| 306 | + } | ||
| 307 | + } | ||
| 308 | +} | ||
| 309 | + | ||
| 310 | +fn captions() { | ||
| 311 | + for path in files(MPEG2_DIR, &["m2v"]) { | ||
| 312 | + let base = format!("subs/cc/{}", name(&path)); | ||
| 313 | + guard(&base, || caption_file(&base, &path)); | ||
| 314 | + } | ||
| 315 | +} | ||
| 316 | + | ||
| 317 | +fn caption_file(base: &str, path: &Path) { | ||
| 318 | + let mut x = ec_cc::extract::Mpeg2Extractor::new(); | ||
| 319 | + x.push(&bytes_of(path), None); | ||
| 320 | + let timed = x.finish((30, 1)); | ||
| 321 | + let decoded = ec_cc::decode_all(&timed); | ||
| 322 | + text( | ||
| 323 | + &format!("{base}/counts"), | ||
| 324 | + format!( | ||
| 325 | + "pictures={} cea608={} cea708={} unsupported={:?}", | ||
| 326 | + timed.len(), | ||
| 327 | + decoded.cea608.len(), | ||
| 328 | + decoded.cea708.len(), | ||
| 329 | + decoded.unsupported_708_services | ||
| 330 | + ), | ||
| 331 | + ); | ||
| 332 | + for (i, cue) in decoded.cea608.iter().enumerate() { | ||
| 333 | + hash(&format!("{base}/608/{i}"), format!("{cue:?}").as_bytes()); | ||
| 334 | + } | ||
| 335 | + for (i, cue) in decoded.cea708.iter().enumerate() { | ||
| 336 | + hash(&format!("{base}/708/{i}"), format!("{cue:?}").as_bytes()); | ||
| 337 | + } | ||
| 338 | +} | ||