forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

pgs_sup_ffmpeg.rs139 lines, 5.2 KiBHistoryRawDownload
1//! A PGS (bitmap) subtitle that goes through ffmpeg into Matroska, read back
2//! through edith's own subtitle call site ([`subtitle::of_matroska`]).
3//!
4//! The source is a `.sup` written here byte for byte, with the `PG` header a
5//! Blu-ray `.sup` carries on every segment: one display set that paints a 2x2
6//! white square on an 8x4 canvas at 500 ms, and the erase that ends it at 2 s.
7//! ffmpeg reads it with its PGS demuxer and muxes it with `-c:s copy` into an
8//! `.mkv`, which is what a rip looks like when it reaches edith.
9//!
10//! The cue the test expects is the one the display sets state: the start and
11//! end in microseconds, and the picture decoded to its pixels. Skipped loudly
12//! when ffmpeg is not on the box.
13//!
14//! ```text
15//! cargo test -p engine --release --test pgs_sup_ffmpeg -- --nocapture
16//! ```
17
18use std::path::Path;
19use std::process::Command;
20
21use engine::scratch::Scratch;
22use engine::subtitle;
23
24fn have_ffmpeg() -> bool {
25 Command::new("ffmpeg")
26 .arg("-version")
27 .output()
28 .is_ok_and(|o| o.status.success())
29}
30
31fn s(path: &Path) -> &str {
32 path.to_str().expect("a UTF-8 path")
33}
34
35fn ffmpeg(args: &[&str]) {
36 let out = Command::new("ffmpeg")
37 .args(["-v", "error", "-y"])
38 .args(args)
39 .output()
40 .expect("ffmpeg runs");
41 assert!(
42 out.status.success(),
43 "ffmpeg {args:?}: {}",
44 String::from_utf8_lossy(&out.stderr)
45 );
46}
47
48/// One `.sup` segment: `PG`, the presentation and decoding times on the 90 kHz
49/// clock, the segment type, a big-endian size, then the body.
50fn sup_segment(pts_ms: u32, kind: u8, body: &[u8]) -> Vec<u8> {
51 let pts = pts_ms * 90;
52 let mut out = b"PG".to_vec();
53 out.extend_from_slice(&pts.to_be_bytes());
54 out.extend_from_slice(&pts.to_be_bytes());
55 out.push(kind);
56 out.extend_from_slice(&(body.len() as u16).to_be_bytes());
57 out.extend_from_slice(body);
58 out
59}
60
61/// The display set that paints a 2x2 white square at (1, 1) of an 8x4 canvas,
62/// at `pts_ms`: composition, palette, the object and the end of the set.
63fn display_set(pts_ms: u32) -> Vec<u8> {
64 // Canvas 8x4, a frame-rate byte, composition 0, epoch start, no palette
65 // update, palette 0 -- and one object, id 0, at (1, 1) with no flags.
66 #[rustfmt::skip]
67 let pcs = [
68 0, 8, 0, 4, 0x10, 0, 0, 0x80, 0, 0, 1,
69 0, 0, 0, 0, 0, 1, 0, 1,
70 ];
71 // Palette 0, version 0, colour 1: white, fully opaque, limited-range YCbCr.
72 let pds = [0, 0, 1, 235, 128, 128, 255];
73 // Two rows of "two pixels of colour 1, end of line".
74 let rle = [0x00, 0x82, 0x01, 0x00, 0x00, 0x00, 0x82, 0x01, 0x00, 0x00];
75 // Object 0, version 0, first and last fragment; the length of what follows;
76 // the object's own 2x2 box; the run-length data.
77 let mut ods = vec![0, 0, 0, 0xC0];
78 ods.extend_from_slice(&[0, 0, (4 + rle.len()) as u8, 0, 2, 0, 2]);
79 ods.extend_from_slice(&rle);
80
81 let mut out = sup_segment(pts_ms, 0x16, &pcs);
82 out.extend(sup_segment(pts_ms, 0x14, &pds));
83 out.extend(sup_segment(pts_ms, 0x15, &ods));
84 out.extend(sup_segment(pts_ms, 0x80, &[]));
85 out
86}
87
88/// The display set that composes nothing: the disc clearing the screen.
89fn erase(pts_ms: u32) -> Vec<u8> {
90 let mut out = sup_segment(pts_ms, 0x16, &[0, 8, 0, 4, 0x10, 0, 1, 0, 0, 0, 0]);
91 out.extend(sup_segment(pts_ms, 0x80, &[]));
92 out
93}
94
95#[test]
96fn a_sup_muxed_into_matroska_reads_back_its_picture_and_its_timing() {
97 if !have_ffmpeg() {
98 eprintln!("skipped: no ffmpeg to mux the .sup with");
99 return;
100 }
101 let sup = Scratch::file("ve_pgs_source", "sup");
102 let mut bytes = display_set(500);
103 bytes.extend(erase(2000));
104 std::fs::write(&*sup, &bytes).expect("write the .sup");
105
106 let mkv = Scratch::file("ve_pgs_in_mkv", "mkv");
107 // `-copyts` keeps the times the .sup states; without it ffmpeg moves the
108 // whole stream to start at zero, which is a fact about ffmpeg, not the file.
109 ffmpeg(&["-copyts", "-i", s(&sup), "-c:s", "copy", s(&mkv)]);
110
111 let tracks = subtitle::of_matroska(&mkv).expect("the mkv is walked");
112 assert_eq!(tracks.len(), 1, "one PGS track: {tracks:?}");
113 let track = &tracks[0];
114 assert_eq!(track.refused, None, "a PGS track is read, not refused");
115 assert!(track.is_bitmap(), "a PGS track is pictures");
116 assert_eq!(track.cues.len(), 1, "the erase is not a cue of its own");
117
118 let cue = &track.cues[0];
119 assert_eq!(
120 (cue.start_us, cue.end_us),
121 (500_000, 2_000_000),
122 "the picture starts where its display set says and ends at the erase"
123 );
124 assert_eq!(cue.text, "", "a picture has no words");
125
126 let image = cue.image.as_ref().expect("the cue is a picture");
127 assert_eq!((image.width, image.height), (8, 4), "the disc's canvas");
128 let rgba = image.rgba().expect("the display set decodes");
129 let at = |x: usize, y: usize| rgba[(y * 8 + x) * 4..(y * 8 + x) * 4 + 4].to_vec();
130 assert_eq!(at(1, 1), vec![235, 235, 235, 255]);
131 assert_eq!(at(2, 2), vec![235, 235, 235, 255]);
132 assert_eq!(at(0, 0), vec![0, 0, 0, 0]);
133 assert_eq!(at(3, 1), vec![0, 0, 0, 0]);
134 assert_eq!(
135 rgba.as_chunks::<4>().0.iter().filter(|p| p[3] > 0).count(),
136 4,
137 "four painted pixels and no more"
138 );
139}