forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBcc667e6Tahinli3 months ago285 commits

pgs_bitmaps.rs93 lines, 3.7 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 in a local folder and no
6//! fixture in this repository, so on a machine 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::path::Path;
16use std::time::Instant;
17
18/// A remux with `S_TEXT/UTF8` + 4x `S_HDMV/PGS`.
19const FILM: &str = "/path/to/a-real-4k-pgs-film.mkv";
20
21#[test]
22fn the_session_maps_a_pgs_track_onto_the_timeline_and_draws_it() {
23 let film = Path::new(FILM);
24 if !film.exists() {
25 eprintln!("skipped: {FILM} is not on this machine");
26 return;
27 }
28 let opened = Instant::now();
29 let mut session = engine::PlaybackSession::open(film).expect("the film opens");
30 session.set_gain(0.0);
31 let rows = session
32 .import_subtitles(film)
33 .expect("the film's own subtitle tracks");
34 eprintln!("{rows} rows in {:?}", opened.elapsed());
35 let pick = session
36 .subtitles()
37 .iter()
38 .position(|t| t.is_bitmap())
39 .expect("a bitmap row to pick");
40 assert_eq!(
41 session.subtitles()[pick].refused, None,
42 "a row a click can pick"
43 );
44
45 let mapped = Instant::now();
46 let cues = session.timeline_cues(pick);
47 eprintln!("{} cues mapped in {:?}", cues.len(), mapped.elapsed());
48 assert!(!cues.is_empty(), "no cue of this track lands on the timeline");
49 // The map is asked once per repaint, so the *second* ask is the one that has
50 // to be cheap -- the first paid for whatever the open did not.
51 let warm = Instant::now();
52 let again = session.timeline_cues(pick);
53 let warm = warm.elapsed();
54 eprintln!("mapped again in {warm:?}");
55 assert_eq!(again, cues, "the same map, twice");
56 assert!(warm.as_millis() < 16, "a repaint's worth of map: {warm:?}");
57
58 // Every cue is on screen for a while and gone again, in order: the erase
59 // blocks are what end them and are not cues themselves.
60 for pair in cues.windows(2) {
61 assert!(pair[0].end_us > pair[0].start_us, "{:?}", pair[0]);
62 assert!(pair[1].start_us >= pair[0].start_us, "{pair:?}");
63 }
64 // ...and the first one's picture is the canvas a front-end lays over the
65 // film: the disc's own frame, painted where the line is and nowhere else.
66 let drawn = Instant::now();
67 let image = cues[0].image.as_ref().expect("a picture cue");
68 let rgba = image.rgba().expect("the first display set decodes");
69 eprintln!(
70 "cue 0 decoded to {}x{} in {:?}",
71 image.width,
72 image.height,
73 drawn.elapsed()
74 );
75 assert_eq!(rgba.len(), (image.width * image.height * 4) as usize);
76 let opaque = rgba.chunks_exact(4).filter(|px| px[3] > 0).count();
77 assert!(opaque > 0, "the block decoded to an empty canvas");
78 assert!(
79 opaque < (image.width * image.height) as usize,
80 "a subtitle that covers the whole frame is not a subtitle"
81 );
82
83 // A text row of the same film carries no picture, which is what keeps the
84 // two kinds of row apart.
85 let text = session
86 .subtitles()
87 .iter()
88 .position(|t| !t.is_bitmap() && t.refused.is_none())
89 .expect("the film's text track");
90 let words = session.timeline_cues(text);
91 assert!(!words.is_empty());
92 assert!(words.iter().all(|c| c.image.is_none()));
93}