forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

real_library.rs146 lines, 5.1 KiBHistoryRawDownload
1//! The three parsers against streams taken out of the user's own library.
2//!
3//! Hand-written cases live in the crates' unit tests; what cannot be written by
4//! hand is a disc's PGS display set or a fansub's thousand-line ASS script.
5//! `scripts/gen-subtitle-fixtures.sh` extracts them (fixtures are gitignored,
6//! the script is not), and every test here *skips* when its fixture is absent
7//! so a fresh clone still runs green.
8
9use std::path::PathBuf;
10
11use ec_subs::{SourceFormat, plain_text};
12
13fn fixture(name: &str) -> Option<Vec<u8>> {
14 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
15 .join("../../fixtures/subs")
16 .join(name);
17 match std::fs::read(&path) {
18 Ok(bytes) => Some(bytes),
19 Err(_) => {
20 eprintln!("skipping: no {name} — run scripts/gen-subtitle-fixtures.sh");
21 None
22 }
23 }
24}
25
26#[test]
27fn a_real_subrip_track_parses_whole() {
28 let Some(bytes) = fixture("real.srt") else {
29 return;
30 };
31 let track = ec_subs::srt::parse(&bytes).unwrap();
32 assert_eq!(track.source, Some(SourceFormat::Srt));
33 assert!(track.cues.len() > 100, "{} cues", track.cues.len());
34 assert_eq!(track.skipped, 0, "a whole file should have no torn blocks");
35 // Every cue is up for a positive time and says something.
36 for cue in &track.cues {
37 assert!(cue.end_us >= cue.start_us);
38 assert!(
39 !plain_text(&cue.segments).trim().is_empty(),
40 "empty cue at {}us",
41 cue.start_us
42 );
43 }
44 // A subtitle track runs forwards.
45 assert!(
46 track
47 .cues
48 .windows(2)
49 .all(|w| w[1].start_us >= w[0].start_us)
50 );
51}
52
53#[test]
54fn a_real_ass_script_parses_whole() {
55 let Some(bytes) = fixture("real.ass") else {
56 return;
57 };
58 let track = ec_ass::parse(&bytes).unwrap();
59 assert_eq!(track.source, Some(SourceFormat::AssOrSsa));
60 assert!(!track.styles.is_empty(), "a script declares styles");
61 assert!(track.cues.len() > 100, "{} cues", track.cues.len());
62 assert_eq!(track.skipped, 0, "a whole script should have no torn rows");
63 // Every cue names a style the script declared, and most say words: a
64 // signs-and-songs script has drawing-only lines, which are not words.
65 let with_text = track
66 .cues
67 .iter()
68 .filter(|c| !plain_text(&c.segments).trim().is_empty())
69 .count();
70 assert!(
71 with_text * 10 > track.cues.len() * 9,
72 "{with_text} of {}",
73 track.cues.len()
74 );
75 for cue in &track.cues {
76 assert!(cue.end_us >= cue.start_us);
77 if let Some(style) = &cue.style_ref {
78 assert!(
79 track.style(style).is_some(),
80 "cue names undeclared style {style:?}"
81 );
82 }
83 // Override tags never leak into the words.
84 let text = plain_text(&cue.segments);
85 assert!(!text.contains("{\\"), "override tag in text: {text:?}");
86 }
87 // The header is replayable as a Matroska `CodecPrivate`.
88 assert!(track.extradata.windows(8).any(|w| w == b"[Events]"));
89}
90
91#[test]
92fn a_real_pgs_stream_decodes_to_plausible_bitmaps() {
93 let Some(bytes) = fixture("pgs-1080p.sup") else {
94 return;
95 };
96 let mut decoder = ec_pgs::PgsDecoder::new();
97 decoder.push(&bytes).unwrap();
98 let mut frames = 0;
99 let mut painted = 0;
100 let mut coverage_max = 0.0f64;
101 while let Some(frame) = decoder.take_frame() {
102 frames += 1;
103 // A disc composes against its own video frame, and this one is HD.
104 assert_eq!((frame.width, frame.height), (1920, 1080));
105 let stride = frame.planes[0].stride;
106 assert_eq!(stride, 1920 * 4);
107 let opaque = frame.planes[0]
108 .data
109 .as_chunks::<4>()
110 .0
111 .iter()
112 .filter(|p| p[3] > 0)
113 .count();
114 if opaque == 0 {
115 // An erase display set: a set that composes nothing at all.
116 continue;
117 }
118 painted += 1;
119 let coverage = opaque as f64 / (1920.0 * 1080.0);
120 coverage_max = coverage_max.max(coverage);
121 // Subtitles are lines of text over a film: ink, not a filled canvas.
122 assert!(
123 coverage > 0.000_1 && coverage < 0.25,
124 "display set {frames} covers {:.3}% of the canvas",
125 coverage * 100.0
126 );
127 // The ink sits in the lower or upper band, never as a single stray row.
128 let rows = (0..1080)
129 .filter(|y| {
130 frame.planes[0].data[y * stride..(y + 1) * stride]
131 .as_chunks::<4>()
132 .0
133 .iter()
134 .any(|p| p[3] > 0)
135 })
136 .count();
137 assert!(rows > 4, "display set {frames} paints {rows} rows");
138 }
139 assert!(frames > 100, "{frames} display sets");
140 assert!(
141 painted * 3 > frames,
142 "{painted} of {frames} display sets painted anything"
143 );
144 assert!(coverage_max < 0.25, "widest cue covers {coverage_max}");
145 eprintln!("pgs: {frames} display sets, {painted} painted, widest {coverage_max:.4}");
146}