forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB978e685Tahinli3 months ago627 commits

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