forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

pgs_bitmaps.rs135 lines, 5.9 KiBHistoryRawDownload
1//! The chain a click on a PGS row actually runs, against a real BluRay remux:
2//! the session lists the track, maps its cues onto the timeline and hands over
3//! the picture of one -- which is what the window draws over the film.
4//!
5//! Gated on the film being there: it is a 4K remux named by the local
6//! `real_library.toml` and no fixture in this repository. Without it the test
7//! prints a `SKIP` line and passes, unless `EC_REQUIRE_FIXTURES=1` is set, in
8//! which case it fails. ffmpeg has a PGS decoder but no PGS encoder, so no
9//! generated sample can stand in for the film here.
10//!
11//! What it guards is what a hand-made display set cannot -- that a real
12//! disc's blocks come whole out of the demuxer and decode, and that the map a
13//! repaint asks for is cheap enough to ask sixty times a second.
14//!
15//! ```text
16//! cargo test -p engine --release --test pgs_bitmaps -- --nocapture
17//! ```
18
19use std::time::Instant;
20
21#[test]
22fn the_session_maps_a_pgs_track_onto_the_timeline_and_draws_it() {
23 // A remux with `S_TEXT/UTF8` + 4x `S_HDMV/PGS`.
24 let Some(film) = engine::real_library::film("hevc_4k_pgs") else {
25 // Nothing to test without the film. The skip is printed as a SKIP line
26 // (visible under `--nocapture`), and `EC_REQUIRE_FIXTURES=1` turns it into
27 // a failure, for a machine that is meant to have the film.
28 if std::env::var("EC_REQUIRE_FIXTURES").is_ok_and(|v| v == "1") {
29 panic!("EC_REQUIRE_FIXTURES=1 but the PGS film `hevc_4k_pgs` is not available");
30 }
31 eprintln!("SKIP pgs_bitmaps: no `hevc_4k_pgs` in real_library.toml; this test did not run");
32 return;
33 };
34 let film = film.as_path();
35 let opened = Instant::now();
36 let mut session = engine::PlaybackSession::open(film).expect("the film opens");
37 session.set_gain(0.0);
38 let rows = session
39 .import_subtitles(film)
40 .expect("the film's own subtitle tracks");
41 eprintln!("{rows} rows in {:?}", opened.elapsed());
42 let pick = session
43 .subtitles()
44 .iter()
45 .position(|t| t.is_bitmap())
46 .expect("a bitmap row to pick");
47 assert_eq!(
48 session.subtitles()[pick].refused, None,
49 "a row a click can pick"
50 );
51
52 let mapped = Instant::now();
53 let cues = session.timeline_cues(pick);
54 eprintln!("{} cues mapped in {:?}", cues.len(), mapped.elapsed());
55 assert!(!cues.is_empty(), "no cue of this track lands on the timeline");
56 // The map is asked once per repaint, so the *second* ask is the one that has
57 // to be cheap -- the first paid for whatever the open did not.
58 let warm = Instant::now();
59 let again = session.timeline_cues(pick);
60 let warm = warm.elapsed();
61 eprintln!("mapped again in {warm:?}");
62 assert_eq!(again, cues, "the same map, twice");
63 assert!(warm.as_millis() < 16, "a repaint's worth of map: {warm:?}");
64
65 // Every cue is on screen for a while and gone again, in order: the erase
66 // blocks are what end them and are not cues themselves.
67 for pair in cues.windows(2) {
68 assert!(pair[0].end_us > pair[0].start_us, "{:?}", pair[0]);
69 assert!(pair[1].start_us >= pair[0].start_us, "{pair:?}");
70 }
71 // ...and the first one's picture is the canvas a front-end lays over the
72 // film: the disc's own frame, painted where the line is and nowhere else.
73 let drawn = Instant::now();
74 let image = cues[0].image.as_ref().expect("a picture cue");
75 let rgba = image.rgba().expect("the first display set decodes");
76 let took = drawn.elapsed();
77 eprintln!(
78 "cue 0 decoded to {}x{} in {took:?}",
79 image.width, image.height,
80 );
81 // The run-length walk is paid once per cue *change* -- the window keeps the
82 // picture while it is up ([`Player::sub_picture`]) -- so what this holds is
83 // the cost of the one frame a new line appears on: 247 µs measured on this
84 // remux, a sixty-eighth of that frame's budget. A whole repaint's worth
85 // would mean the cache above it had stopped mattering.
86 assert!(took.as_millis() < 16, "a frame's worth of decode: {took:?}");
87 assert_eq!(rgba.len(), (image.width * image.height * 4) as usize);
88 let opaque = rgba.as_chunks::<4>().0.iter().filter(|px| px[3] > 0).count();
89 assert!(opaque > 0, "the block decoded to an empty canvas");
90 assert!(
91 opaque < (image.width * image.height) as usize,
92 "a subtitle that covers the whole frame is not a subtitle"
93 );
94
95 // ...and placed on a subtitle lane, that same real row is refused by name
96 // before any file exists: both muxers write *words* (`S_TEXT/UTF8`, `tx3g`)
97 // and neither has a path that could write a run-length picture, so the lane
98 // carries no track at all rather than the file coming out short
99 // (`export::planned_lanes`; `tests/export_subs.rs` pins the empty file it
100 // leaves).
101 let lane = session.add_lane(engine::project::LaneKind::Subtitle);
102 session
103 .place_sub(
104 lane,
105 0,
106 engine::project::SubClip {
107 start: 0,
108 frames: 60,
109 track: pick,
110 in_us: cues[0].start_us,
111 out_us: cues[0].end_us,
112 link: None,
113 },
114 )
115 .expect("the lane is empty, so the placement lands");
116 assert_eq!(
117 session.planned_subtitles(engine::export::Format::Hevc, []),
118 format!(
119 "S1 {} — pictures; drawn, not written",
120 session.subtitles()[pick].label
121 ),
122 "a real PGS row on a lane says why it cannot travel"
123 );
124
125 // A text row of the same film carries no picture, which is what keeps the
126 // two kinds of row apart.
127 let text = session
128 .subtitles()
129 .iter()
130 .position(|t| !t.is_bitmap() && t.refused.is_none())
131 .expect("the film's text track");
132 let words = session.timeline_cues(text);
133 assert!(!words.is_empty());
134 assert!(words.iter().all(|c| c.image.is_none()));
135}