forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'simplify/u21' into simplify/w1b

Browse files at this commit

90b524d720f838eac80af06c9d34ff9fb6ac2953Ahmet Kaan GümüşParent: 921c9e13fe281a
Cargo.lockmodified+1 -2
@@ -707,6 +707,7 @@ name = "ec-pgs"
707707 version = "0.1.0"
708708 dependencies = [
709709 "ec-core",
710+ "ec-testkit",
710711 ]
711712
712713 [[package]]
@@ -758,9 +759,7 @@ dependencies = [
758759 name = "ec-subs"
759760 version = "0.1.0"
760761 dependencies = [
761- "ec-ass",
762762 "ec-core",
763- "ec-pgs",
764763 "ec-testkit",
765764 ]
766765
crates/ec-ass/examples/benchp1.rsdeleted+0 -44
@@ -1,44 +0,0 @@
1-//! benchp1: ASS parse throughput for lanes/bench.md ("lane benchp1").
2-//!
3-//! usage: benchp1 ass <file> [reps]
4-//!
5-//! Parses the whole file `reps` times (default 5). Each timed run repeats the parse until it
6-//! takes at least 0.2 s, so the per-parse time is not timer noise. Prints the best per-parse
7-//! time, the throughput in MB/s and the cue count of the parsed track (checked against ffprobe).
8-use std::hint::black_box;
9-use std::time::Instant;
10-
11-fn main() {
12- let args: Vec<String> = std::env::args().collect();
13- let kind = args.get(1).map(String::as_str).unwrap_or("");
14- let bytes = std::fs::read(&args[2]).expect("read subtitle file");
15- let reps: usize = args.get(3).map_or(5, |s| s.parse().expect("reps"));
16- let parse = |b: &[u8]| match kind {
17- "ass" => ec_ass::parse(b),
18-
19- _ => panic!("kind must be ass"),
20- };
21-
22- let first = parse(&bytes).expect("parse");
23- let cues = first.cues.len();
24- let skipped = first.skipped;
25- let t0 = Instant::now();
26- black_box(parse(black_box(&bytes)).expect("parse"));
27- let one = t0.elapsed().as_secs_f64().max(1e-6);
28- let inner = ((0.2 / one).ceil() as usize).max(1);
29-
30- let mut best = f64::MAX;
31- for _ in 0..reps {
32- let t = Instant::now();
33- for _ in 0..inner {
34- black_box(parse(black_box(&bytes)).expect("parse"));
35- }
36- best = best.min(t.elapsed().as_secs_f64() / inner as f64);
37- }
38- let mb_s = bytes.len() as f64 / best / 1e6;
39- println!(
40- "kind={kind} bytes={} cues={cues} skipped={skipped} best_parse_ms={:.3} mb_s={mb_s:.2} reps={reps} inner={inner}",
41- bytes.len(),
42- best * 1e3
43- );
44-}
crates/ec-ass/tests/open_media.rsadded+37 -0
@@ -0,0 +1,37 @@
1+use ec_subs::{SourceFormat, plain_text};
2+
3+fn fixture(rel: &str) -> Vec<u8> {
4+ std::fs::read(ec_testkit::fixture(rel)).unwrap()
5+}
6+
7+#[test]
8+fn a_real_ass_script_parses_whole() {
9+ let bytes = fixture("subs/tos-en.ass");
10+ let track = ec_ass::parse(&bytes).unwrap();
11+ assert_eq!(track.source, Some(SourceFormat::AssOrSsa));
12+ assert!(!track.styles.is_empty(), "a script declares styles");
13+ assert_eq!(track.cues.len(), 76, "ffmpeg reads 76 cues");
14+ assert_eq!(track.skipped, 0, "a whole script should have no torn rows");
15+ let with_text = track
16+ .cues
17+ .iter()
18+ .filter(|c| !plain_text(&c.segments).trim().is_empty())
19+ .count();
20+ assert!(
21+ with_text * 10 > track.cues.len() * 9,
22+ "{with_text} of {}",
23+ track.cues.len()
24+ );
25+ for cue in &track.cues {
26+ assert!(cue.end_us >= cue.start_us);
27+ if let Some(style) = &cue.style_ref {
28+ assert!(
29+ track.style(style).is_some(),
30+ "cue names undeclared style {style:?}"
31+ );
32+ }
33+ let text = plain_text(&cue.segments);
34+ assert!(!text.contains("{\\"), "override tag in text: {text:?}");
35+ }
36+ assert!(track.extradata.windows(8).any(|w| w == b"[Events]"));
37+}
crates/ec-dvbsub/src/lib.rsmodified+0 -4
@@ -610,10 +610,6 @@ impl Decoder for DvbSubDecoder {
610610 }
611611 }
612612
613-/// A DVB subtitle decoder as the family's registry hands one out.
614-pub fn make_decoder(_params: &CodecParameters) -> Result<Box<dyn Decoder>> {
615- Ok(Box::new(DvbSubDecoder::new()))
616-}
617613
618614 #[cfg(test)]
619615 mod tests {
crates/ec-dvdsub/src/lib.rsmodified+5 -9
@@ -29,6 +29,7 @@ use ec_core::{
2929 pub const MAX_DIMENSION: u32 = 4096;
3030 /// Ticks of the SPU delay unit: 1024 periods of the 90 kHz clock.
3131 pub const DELAY_UNIT_90K: i64 = 1024;
32+const CLOCK_90K: TimeBase = TimeBase::new(1, 90_000);
3233 /// Finished cues kept for [`DvdSubDecoder::take_cue`].
3334 pub const MAX_PENDING: usize = 64;
3435
@@ -475,7 +476,7 @@ pub struct DvdSubDecoder {
475476 params: CodecParameters,
476477 palette: Palette,
477478 canvas: (u32, u32),
478- ready: VecDeque<(Cue, Option<Timestamp>)>,
479+ ready: VecDeque<Cue>,
479480 }
480481
481482 impl DvdSubDecoder {
@@ -498,21 +499,20 @@ impl DvdSubDecoder {
498499 pub fn push_unit(&mut self, unit: &[u8], pts: Option<Timestamp>) -> Result<()> {
499500 let mut cue = decode_spu(unit, &self.palette, self.canvas)?;
500501 if let Some(p) = pts {
501- let base = TimeBase::try_new(1, 90_000).map_err(|_| Error::corrupt("dvdsub: time base"))?;
502- let delay = Timestamp::new(cue.start_90k, base)
502+ let delay = Timestamp::new(cue.start_90k, CLOCK_90K)
503503 .rescale(p.base, ec_core::Rounding::Nearest);
504504 cue.frame.pts = Some(Timestamp::new(p.ticks.saturating_add(delay.ticks), p.base));
505505 }
506506 if self.ready.len() >= MAX_PENDING {
507507 self.ready.pop_front();
508508 }
509- self.ready.push_back((cue, pts));
509+ self.ready.push_back(cue);
510510 Ok(())
511511 }
512512
513513 /// The next decoded cue with its timing, oldest first.
514514 pub fn take_cue(&mut self) -> Option<Cue> {
515- self.ready.pop_front().map(|(c, _)| c)
515+ self.ready.pop_front()
516516 }
517517 }
518518
@@ -542,10 +542,6 @@ impl Decoder for DvdSubDecoder {
542542 }
543543 }
544544
545-/// A DVD subpicture decoder as the family's registry hands one out.
546-pub fn make_decoder(params: &CodecParameters) -> Result<Box<dyn Decoder>> {
547- Ok(Box::new(DvdSubDecoder::new(params.extradata.as_deref())))
548-}
549545
550546 #[cfg(test)]
551547 mod tests {
crates/ec-pgs/Cargo.tomlmodified+3 -0
@@ -8,3 +8,6 @@ authors.workspace = true
88
99 [dependencies]
1010 ec-core.workspace = true
11+
12+[dev-dependencies]
13+ec-testkit.workspace = true
crates/ec-pgs/examples/benchp1.rsdeleted+0 -139
@@ -1,139 +0,0 @@
1-//! benchp1: PGS (.sup) decode throughput for lanes/bench.md ("lane benchp1").
2-//!
3-//! usage:
4-//! benchp1 gen <out.sup> <seconds> write a synthetic .sup: every second one display set
5-//! shows a 64x32 object, 0.8 s later a clear set follows
6-//! (2 display sets per second, 90 kHz pts).
7-//! benchp1 decode <file.sup> [reps] decode the whole file `reps` times (default 5) with a
8-//! fresh decoder each time; print the best time, MB/s, the
9-//! rendered frame count and the stream duration from the pts.
10-//!
11-//! The generator uses the same segment layout as the crate's tests (`tests/fuzz_regressions.rs`).
12-//! ffmpeg has no PGS encoder, so this is the input source for the row.
13-use std::hint::black_box;
14-use std::time::Instant;
15-
16-use ec_pgs::{PgsDecoder, SEG_END, SEG_ODS, SEG_PCS, SEG_PDS};
17-
18-fn segment(pts: u32, kind: u8, body: &[u8]) -> Vec<u8> {
19- let mut out = b"PG".to_vec();
20- out.extend_from_slice(&pts.to_be_bytes());
21- out.extend_from_slice(&pts.to_be_bytes());
22- out.push(kind);
23- out.extend_from_slice(&(body.len() as u16).to_be_bytes());
24- out.extend_from_slice(body);
25- out
26-}
27-
28-/// A 1920x1080 epoch-start composition with `objects` placed objects (0 or 1).
29-fn pcs(pts: u32, x: u16, y: u16, objects: u8) -> Vec<u8> {
30- let mut body = vec![0x07, 0x80, 0x04, 0x38, 0x10, 0, 0, 0x80, 0, 0, objects];
31- if objects == 1 {
32- body.extend_from_slice(&[0, 0, 0, 0]);
33- body.extend_from_slice(&x.to_be_bytes());
34- body.extend_from_slice(&y.to_be_bytes());
35- }
36- segment(pts, SEG_PCS, &body)
37-}
38-
39-/// Palette entry 1 opaque white.
40-fn pds(pts: u32) -> Vec<u8> {
41- segment(pts, SEG_PDS, &[0, 0, 1, 235, 128, 128, 255])
42-}
43-
44-/// One 64x32 object, RLE: each row is 64 pixels of colour 1 then an end-of-line code.
45-fn ods(pts: u32) -> Vec<u8> {
46- let (w, h) = (64u16, 32u16);
47- let mut rle = Vec::new();
48- for _ in 0..h {
49- rle.extend(std::iter::repeat_n(1u8, w as usize));
50- rle.extend_from_slice(&[0, 0]);
51- }
52- let mut body = vec![0, 0, 0, 0xC0];
53- body.extend_from_slice(&((rle.len() + 4) as u32).to_be_bytes()[1..]);
54- body.extend_from_slice(&w.to_be_bytes());
55- body.extend_from_slice(&h.to_be_bytes());
56- body.extend_from_slice(&rle);
57- segment(pts, SEG_ODS, &body)
58-}
59-
60-fn generate(path: &str, seconds: u32) {
61- let mut out = Vec::new();
62- for s in 0..seconds {
63- let t = s * 90_000;
64- let x = ((s * 37) % 1800) as u16;
65- let y = ((s * 19) % 1000) as u16;
66- out.extend(pcs(t, x, y, 1));
67- out.extend(pds(t));
68- out.extend(ods(t));
69- out.extend(segment(t, SEG_END, &[]));
70- let c = t + 72_000;
71- out.extend(pcs(c, 0, 0, 0));
72- out.extend(segment(c, SEG_END, &[]));
73- }
74- std::fs::write(path, &out).expect("write sup");
75- println!("wrote {path} bytes={} display_sets={}", out.len(), 2 * seconds);
76-}
77-
78-/// (segment count, the last segment's pts in seconds), walking the segment headers.
79-fn scan(bytes: &[u8]) -> (usize, f64) {
80- let mut pos = 0;
81- let mut last = 0u32;
82- let mut segments = 0;
83- while pos + 13 <= bytes.len() {
84- last = u32::from_be_bytes(bytes[pos + 2..pos + 6].try_into().unwrap());
85- let size = u16::from_be_bytes([bytes[pos + 11], bytes[pos + 12]]) as usize;
86- pos += 13 + size;
87- segments += 1;
88- }
89- (segments, f64::from(last) / 90_000.0)
90-}
91-
92-/// One full decode with a fresh decoder: (rendered frames, push errors). The file is fed one
93-/// segment at a time and the finished frames are dropped after each push, as a streaming reader
94-/// would. A full 1920x1080 canvas is about 8 MB, so draining per segment keeps the decoder's
95-/// output queue at most one frame deep.
96-fn decode_once(bytes: &[u8]) -> (usize, usize) {
97- let mut d = PgsDecoder::new();
98- let (mut frames, mut errors) = (0, 0);
99- let mut pos = 0;
100- while pos + 13 <= bytes.len() {
101- let size = u16::from_be_bytes([bytes[pos + 11], bytes[pos + 12]]) as usize;
102- let end = (pos + 13 + size).min(bytes.len());
103- if d.push(black_box(&bytes[pos..end])).is_err() {
104- errors += 1;
105- }
106- pos = end;
107- while let Some(f) = d.take_frame() {
108- frames += 1;
109- black_box(f);
110- }
111- }
112- (frames, errors)
113-}
114-
115-fn decode(path: &str, reps: usize) {
116- let bytes = std::fs::read(path).expect("read sup");
117- let (frames, errors) = decode_once(&bytes);
118- let mut best = f64::MAX;
119- for _ in 0..reps {
120- let t = Instant::now();
121- black_box(decode_once(black_box(&bytes)));
122- best = best.min(t.elapsed().as_secs_f64());
123- }
124- let (segments, dur) = scan(&bytes);
125- println!(
126- "bytes={} segments={segments} frames={frames} push_errors={errors} duration_s={dur:.1} best_decode_s={best:.4} mb_s={:.2} reps={reps}",
127- bytes.len(),
128- bytes.len() as f64 / best / 1e6
129- );
130-}
131-
132-fn main() {
133- let args: Vec<String> = std::env::args().collect();
134- match args.get(1).map(String::as_str) {
135- Some("gen") => generate(&args[2], args[3].parse().expect("seconds")),
136- Some("decode") => decode(&args[2], args.get(3).map_or(5, |s| s.parse().expect("reps"))),
137- _ => panic!("usage: benchp1 gen <out.sup> <seconds> | decode <file.sup> [reps]"),
138- }
139-}
crates/ec-pgs/src/lib.rsmodified+0 -4
@@ -519,10 +519,6 @@ impl Decoder for PgsDecoder {
519519 }
520520 }
521521
522-/// A PGS decoder as the family's registry hands one out.
523-pub fn make_decoder(_params: &CodecParameters) -> Result<Box<dyn Decoder>> {
524- Ok(Box::new(PgsDecoder::new()))
525-}
526522
527523 /// One palette entry: BT.601 Y'CbCr with straight alpha, the way a disc writes
528524 /// it, into RGBA.
crates/ec-pgs/tests/open_media.rsadded+54 -0
@@ -0,0 +1,54 @@
1+fn fixture(rel: &str) -> Vec<u8> {
2+ std::fs::read(ec_testkit::fixture(rel)).unwrap()
3+}
4+
5+#[test]
6+fn a_real_pgs_stream_decodes_to_plausible_bitmaps() {
7+ let bytes = fixture("open/fate/sub/pgs_sub.sup");
8+ let mut decoder = ec_pgs::PgsDecoder::new();
9+ decoder.push(&bytes).unwrap();
10+ let mut frames = 0;
11+ let mut painted = 0;
12+ let mut coverage_max = 0.0f64;
13+ while let Some(frame) = decoder.take_frame() {
14+ frames += 1;
15+ let (w, h) = (frame.width as usize, frame.height as usize);
16+ let stride = frame.planes[0].stride;
17+ assert_eq!(stride, w * 4);
18+ let opaque = frame.planes[0]
19+ .data
20+ .as_chunks::<4>()
21+ .0
22+ .iter()
23+ .filter(|p| p[3] > 0)
24+ .count();
25+ if opaque == 0 {
26+ continue;
27+ }
28+ painted += 1;
29+ let coverage = opaque as f64 / (w * h) as f64;
30+ coverage_max = coverage_max.max(coverage);
31+ assert!(
32+ coverage > 0.000_1 && coverage < 0.25,
33+ "display set {frames} covers {:.3}% of the canvas",
34+ coverage * 100.0
35+ );
36+ let rows = (0..h)
37+ .filter(|y| {
38+ frame.planes[0].data[y * stride..(y + 1) * stride]
39+ .as_chunks::<4>()
40+ .0
41+ .iter()
42+ .any(|p| p[3] > 0)
43+ })
44+ .count();
45+ assert!(rows > 4, "display set {frames} paints {rows} rows");
46+ }
47+ assert_eq!(frames, 1, "ffprobe reads one subtitle frame");
48+ assert!(
49+ painted * 3 > frames,
50+ "{painted} of {frames} display sets painted anything"
51+ );
52+ assert!(coverage_max < 0.25, "widest cue covers {coverage_max}");
53+ eprintln!("pgs: {frames} display sets, {painted} painted, widest {coverage_max:.4}");
54+}
crates/ec-subs/Cargo.tomlmodified+0 -2
@@ -11,5 +11,3 @@ ec-core.workspace = true
1111
1212 [dev-dependencies]
1313 ec-testkit.workspace = true
14-ec-ass = { path = "../ec-ass", version = "0.1.0" }
15-ec-pgs = { path = "../ec-pgs", version = "0.1.0" }
crates/ec-subs/examples/benchp1.rsdeleted+0 -44
@@ -1,44 +0,0 @@
1-//! benchp1: SubRip and WebVTT parse throughput for lanes/bench.md ("lane benchp1").
2-//!
3-//! usage: benchp1 <srt|vtt> <file> [reps]
4-//!
5-//! Parses the whole file `reps` times (default 5). Each timed run repeats the parse until it
6-//! takes at least 0.2 s, so the per-parse time is not timer noise. Prints the best per-parse
7-//! time, the throughput in MB/s and the cue count of the parsed track (checked against ffprobe).
8-use std::hint::black_box;
9-use std::time::Instant;
10-
11-fn main() {
12- let args: Vec<String> = std::env::args().collect();
13- let kind = args.get(1).map(String::as_str).unwrap_or("");
14- let bytes = std::fs::read(&args[2]).expect("read subtitle file");
15- let reps: usize = args.get(3).map_or(5, |s| s.parse().expect("reps"));
16- let parse = |b: &[u8]| match kind {
17- "srt" => ec_subs::srt::parse(b),
18- "vtt" => ec_subs::webvtt::parse(b),
19- _ => panic!("kind must be srt or vtt"),
20- };
21-
22- let first = parse(&bytes).expect("parse");
23- let cues = first.cues.len();
24- let skipped = first.skipped;
25- let t0 = Instant::now();
26- black_box(parse(black_box(&bytes)).expect("parse"));
27- let one = t0.elapsed().as_secs_f64().max(1e-6);
28- let inner = ((0.2 / one).ceil() as usize).max(1);
29-
30- let mut best = f64::MAX;
31- for _ in 0..reps {
32- let t = Instant::now();
33- for _ in 0..inner {
34- black_box(parse(black_box(&bytes)).expect("parse"));
35- }
36- best = best.min(t.elapsed().as_secs_f64() / inner as f64);
37- }
38- let mb_s = bytes.len() as f64 / best / 1e6;
39- println!(
40- "kind={kind} bytes={} cues={cues} skipped={skipped} best_parse_ms={:.3} mb_s={mb_s:.2} reps={reps} inner={inner}",
41- bytes.len(),
42- best * 1e3
43- );
44-}
crates/ec-subs/tests/open_media.rsmodified+0 -83
@@ -26,86 +26,3 @@ fn a_real_subrip_track_parses_whole() {
2626 .all(|w| w[1].start_us >= w[0].start_us)
2727 );
2828 }
29-
30-#[test]
31-fn a_real_ass_script_parses_whole() {
32- let bytes = fixture("subs/tos-en.ass");
33- let track = ec_ass::parse(&bytes).unwrap();
34- assert_eq!(track.source, Some(SourceFormat::AssOrSsa));
35- assert!(!track.styles.is_empty(), "a script declares styles");
36- assert_eq!(track.cues.len(), 76, "ffmpeg reads 76 cues");
37- assert_eq!(track.skipped, 0, "a whole script should have no torn rows");
38- let with_text = track
39- .cues
40- .iter()
41- .filter(|c| !plain_text(&c.segments).trim().is_empty())
42- .count();
43- assert!(
44- with_text * 10 > track.cues.len() * 9,
45- "{with_text} of {}",
46- track.cues.len()
47- );
48- for cue in &track.cues {
49- assert!(cue.end_us >= cue.start_us);
50- if let Some(style) = &cue.style_ref {
51- assert!(
52- track.style(style).is_some(),
53- "cue names undeclared style {style:?}"
54- );
55- }
56- let text = plain_text(&cue.segments);
57- assert!(!text.contains("{\\"), "override tag in text: {text:?}");
58- }
59- assert!(track.extradata.windows(8).any(|w| w == b"[Events]"));
60-}
61-
62-#[test]
63-fn a_real_pgs_stream_decodes_to_plausible_bitmaps() {
64- let bytes = fixture("open/fate/sub/pgs_sub.sup");
65- let mut decoder = ec_pgs::PgsDecoder::new();
66- decoder.push(&bytes).unwrap();
67- let mut frames = 0;
68- let mut painted = 0;
69- let mut coverage_max = 0.0f64;
70- while let Some(frame) = decoder.take_frame() {
71- frames += 1;
72- let (w, h) = (frame.width as usize, frame.height as usize);
73- let stride = frame.planes[0].stride;
74- assert_eq!(stride, w * 4);
75- let opaque = frame.planes[0]
76- .data
77- .as_chunks::<4>()
78- .0
79- .iter()
80- .filter(|p| p[3] > 0)
81- .count();
82- if opaque == 0 {
83- continue;
84- }
85- painted += 1;
86- let coverage = opaque as f64 / (w * h) as f64;
87- coverage_max = coverage_max.max(coverage);
88- assert!(
89- coverage > 0.000_1 && coverage < 0.25,
90- "display set {frames} covers {:.3}% of the canvas",
91- coverage * 100.0
92- );
93- let rows = (0..h)
94- .filter(|y| {
95- frame.planes[0].data[y * stride..(y + 1) * stride]
96- .as_chunks::<4>()
97- .0
98- .iter()
99- .any(|p| p[3] > 0)
100- })
101- .count();
102- assert!(rows > 4, "display set {frames} paints {rows} rows");
103- }
104- assert_eq!(frames, 1, "ffprobe reads one subtitle frame");
105- assert!(
106- painted * 3 > frames,
107- "{painted} of {frames} display sets painted anything"
108- );
109- assert!(coverage_max < 0.25, "widest cue covers {coverage_max}");
110- eprintln!("pgs: {frames} display sets, {painted} painted, widest {coverage_max:.4}");
111-}