A PGS cue is a picture the timeline carries, not a row that says no
Browse files at this commitc45b18e449c29bb503e3a4d85390bce5d98ff03eParent: 9ebff59- feat
- a PGS cue is a picture the timeline carries, not a row that says no
8 changed files +542 -62
- Cargo.lock +10 -0
- crates/app/src/main.rs +106 -11
- crates/engine/Cargo.toml +1 -0
- crates/engine/src/demux.rs +14 -7
- crates/engine/src/export.rs +23 -5
- crates/engine/src/subtitle.rs +163 -9
- crates/engine/tests/export_subs.rs +3 -0
- crates/engine/tests/subtitles.rs +222 -30
Cargo.lockmodified+10 -0
| @@ -1646,6 +1646,7 @@ dependencies = [ | ||
| 1646 | 1646 | "oxideav-core", |
| 1647 | 1647 | "oxideav-h265", |
| 1648 | 1648 | "oxideav-ogg", |
| 1649 | + "oxideav-sub-image", | |
| 1649 | 1650 | "oxideav-subtitle", |
| 1650 | 1651 | "rav1e", |
| 1651 | 1652 | "rusty_aac", |
| @@ -4037,6 +4038,15 @@ dependencies = [ | ||
| 4037 | 4038 | "oxideav-ttf", |
| 4038 | 4039 | ] |
| 4039 | 4040 | |
| 4041 | +[[package]] | |
| 4042 | +name = "oxideav-sub-image" | |
| 4043 | +version = "0.0.7" | |
| 4044 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4045 | +checksum = "cb5030e2dce8efa58c20f3e9ce4e53064b7697dc2dcc2585a678d313718582d6" | |
| 4046 | +dependencies = [ | |
| 4047 | + "oxideav-core", | |
| 4048 | +] | |
| 4049 | + | |
| 4040 | 4050 | [[package]] |
| 4041 | 4051 | name = "oxideav-subtitle" |
| 4042 | 4052 | version = "0.1.2" |
crates/app/src/main.rsmodified+106 -11
| @@ -938,6 +938,17 @@ struct Player { | ||
| 938 | 938 | fps: f64, |
| 939 | 939 | name: SharedString, |
| 940 | 940 | image: Option<Arc<RenderImage>>, |
| 941 | + /// The picture of the bitmap cue on screen, and which cue that is: the | |
| 942 | + /// track it is a row of and where it starts on the timeline. A PGS display | |
| 943 | + /// set is run-length and has to be walked into a canvas-sized buffer to be | |
| 944 | + /// drawn ([`engine::subtitle::CueImage::rgba`]), which is a thing to do | |
| 945 | + /// when the cue changes -- every few seconds -- and not at every repaint. | |
| 946 | + /// | |
| 947 | + /// The *track* is half the key because the four PGS tracks of a remux are | |
| 948 | + /// one film's subtitles in four languages: they start at the same | |
| 949 | + /// microsecond, and a picked row that only changed the language would go on | |
| 950 | + /// showing the one before it. | |
| 951 | + sub_image: Option<((usize, i64), Arc<RenderImage>)>, | |
| 941 | 952 | /// A frame that arrived before its time; shown on the tick it comes due. |
| 942 | 953 | /// The pump's buffer, not transport state -- but a frame waiting here is |
| 943 | 954 | /// what keeps a finished decoder from reading as [`Transport::Ended`] one |
| @@ -3514,6 +3525,8 @@ impl Player { | ||
| 3514 | 3525 | // emptied library, against one per displayed frame in `pump`; the |
| 3515 | 3526 | // upgrade path is threading the window through `act_on_row`. |
| 3516 | 3527 | self.image = None; |
| 3528 | + // The drawn cue with it, and its tile for the same reason as above. | |
| 3529 | + self.sub_image = None; | |
| 3517 | 3530 | self.clipboard = None; |
| 3518 | 3531 | self.selected = None; |
| 3519 | 3532 | self.selected_asset = None; |
| @@ -4912,7 +4925,7 @@ impl Render for Player { | ||
| 4912 | 4925 | ) |
| 4913 | 4926 | // After the picture, so the plate is drawn over it |
| 4914 | 4927 | // rather than under: siblings paint in order. |
| 4915 | - .children(self.subtitle_overlay(position)), | |
| 4928 | + .children(self.subtitle_overlay(position, window)), | |
| 4916 | 4929 | ), |
| 4917 | 4930 | ) |
| 4918 | 4931 | // Above the panel and only when there is one to show, so it costs |
| @@ -5333,28 +5346,63 @@ impl Player { | ||
| 5333 | 5346 | /// not the track's own: on a cut timeline an embedded track's cues ride the |
| 5334 | 5347 | /// pictures they belong to, and this is the same map the export writes the |
| 5335 | 5348 | /// file with -- so what is read here is what the file says. |
| 5336 | - fn subtitle_overlay(&self, at: f64) -> Option<impl IntoElement + use<>> { | |
| 5349 | + /// | |
| 5350 | + /// A cue off a PGS track is a *picture* and not a line | |
| 5351 | + /// ([`engine::subtitle::CueImage`]), and is drawn as one: the disc's whole | |
| 5352 | + /// canvas laid over the whole picture region, which puts every cue where | |
| 5353 | + /// the disc put it relative to its own frame, at the cost of the region and | |
| 5354 | + /// that frame not being the same shape. | |
| 5355 | + /// | |
| 5356 | + /// ponytail: that cost is a stretch -- a 2.39:1 encode in a 16:9 region | |
| 5357 | + /// leaves the cue a little wide and a little low, because this maps the | |
| 5358 | + /// canvas onto the region and not onto the letterboxed picture inside it. | |
| 5359 | + /// The upgrade path is the picture's own rect, which wants `VideoMeta`'s | |
| 5360 | + /// aspect and the measured bounds rather than a `relative()`. | |
| 5361 | + fn subtitle_overlay( | |
| 5362 | + &mut self, | |
| 5363 | + at: f64, | |
| 5364 | + window: &mut Window, | |
| 5365 | + ) -> Option<impl IntoElement + use<>> { | |
| 5337 | 5366 | if !self.subs_on { |
| 5338 | 5367 | return None; |
| 5339 | 5368 | } |
| 5340 | 5369 | let mapped = self.session.as_ref()?.timeline_cues(self.sub_track); |
| 5341 | 5370 | let cues = cues_at(&mapped, at); |
| 5342 | 5371 | if cues.is_empty() { |
| 5372 | + self.drop_sub_image(window); | |
| 5343 | 5373 | return None; |
| 5344 | 5374 | } |
| 5375 | + // The first picture cue up, decoded once and kept: two bitmap cues at | |
| 5376 | + // one moment is a thing PGS composes into one display set, so there is | |
| 5377 | + // never a second picture to stack under the first. | |
| 5378 | + let picture = cues | |
| 5379 | + .iter() | |
| 5380 | + .find_map(|cue| Some((cue.start_us, cue.image.as_ref()?))) | |
| 5381 | + .and_then(|(start_us, image)| self.sub_picture(start_us, image, window)); | |
| 5345 | 5382 | Some( |
| 5346 | 5383 | div() |
| 5347 | 5384 | .absolute() |
| 5348 | 5385 | .left_0() |
| 5349 | 5386 | .right_0() |
| 5350 | - .bottom(px(SUB_BOTTOM)) | |
| 5387 | + // A picture is placed on its own canvas and a plate is placed on | |
| 5388 | + // the region, so the picture takes the whole of it. | |
| 5389 | + .when(picture.is_some(), |d| d.top_0().bottom_0()) | |
| 5390 | + .when(picture.is_none(), |d| d.bottom(px(SUB_BOTTOM))) | |
| 5351 | 5391 | .flex() |
| 5352 | 5392 | .flex_col() |
| 5353 | 5393 | .items_center() |
| 5354 | 5394 | .gap(px(2.)) |
| 5395 | + .children(picture.map(|image| { | |
| 5396 | + img(image) | |
| 5397 | + .size_full() | |
| 5398 | + // Stretched, not fitted: the canvas *is* the frame the | |
| 5399 | + // disc composed against, so its corners are the frame's | |
| 5400 | + // corners and fitting it would move every cue. | |
| 5401 | + .object_fit(gpui::ObjectFit::Fill) | |
| 5402 | + })) | |
| 5355 | 5403 | // The plate takes no click: the picture behind it is still the |
| 5356 | 5404 | // drop target the whole window is. |
| 5357 | - .children(cues.into_iter().map(|cue| { | |
| 5405 | + .children(cues.into_iter().filter(|c| c.image.is_none()).map(|cue| { | |
| 5358 | 5406 | div() |
| 5359 | 5407 | .max_w(relative(0.9)) |
| 5360 | 5408 | .px(px(6.)) |
| @@ -5376,6 +5424,48 @@ impl Player { | ||
| 5376 | 5424 | ) |
| 5377 | 5425 | } |
| 5378 | 5426 | |
| 5427 | + /// The cue starting at `start_us` as a drawable picture, decoded on the | |
| 5428 | + /// first repaint it is up for and kept until another cue takes its place | |
| 5429 | + /// ([`Player::sub_image`]). `None` for a display set the decoder refuses, | |
| 5430 | + /// which draws nothing rather than failing the frame. | |
| 5431 | + /// | |
| 5432 | + /// Its atlas tile is released as the video's is: every [`RenderImage`] gets | |
| 5433 | + /// a fresh id and its own tile, so a film's worth of cues would grow the | |
| 5434 | + /// sprite atlas by the whole film. | |
| 5435 | + fn sub_picture( | |
| 5436 | + &mut self, | |
| 5437 | + start_us: i64, | |
| 5438 | + image: &engine::subtitle::CueImage, | |
| 5439 | + window: &mut Window, | |
| 5440 | + ) -> Option<Arc<RenderImage>> { | |
| 5441 | + let key = (self.sub_track, start_us); | |
| 5442 | + if let Some((up, ready)) = &self.sub_image | |
| 5443 | + && *up == key | |
| 5444 | + { | |
| 5445 | + return Some(ready.clone()); | |
| 5446 | + } | |
| 5447 | + let mut rgba = image.rgba()?; | |
| 5448 | + // gpui's atlas is BGRA with straight alpha; PGS decodes to RGBA with | |
| 5449 | + // straight alpha. The same swap the video frames get. | |
| 5450 | + for pixel in rgba.chunks_exact_mut(4) { | |
| 5451 | + pixel.swap(0, 2); | |
| 5452 | + } | |
| 5453 | + let buf = image::RgbaImage::from_raw(image.width, image.height, rgba)?; | |
| 5454 | + let next = Arc::new(RenderImage::new(vec![image::Frame::new(buf)])); | |
| 5455 | + self.drop_sub_image(window); | |
| 5456 | + self.sub_image = Some((key, next.clone())); | |
| 5457 | + Some(next) | |
| 5458 | + } | |
| 5459 | + | |
| 5460 | + /// Lets go of the drawn cue and its atlas tile. Called where the cue stops | |
| 5461 | + /// being on screen, which is every gap between two of them: an 8 MB tile | |
| 5462 | + /// per cue is not a thing to leave behind a film. | |
| 5463 | + fn drop_sub_image(&mut self, window: &mut Window) { | |
| 5464 | + if let Some((_, old)) = self.sub_image.take() { | |
| 5465 | + let _ = window.drop_image(old); | |
| 5466 | + } | |
| 5467 | + } | |
| 5468 | + | |
| 5379 | 5469 | /// What is decoding the picture right now, for the transport line: the |
| 5380 | 5470 | /// backend is the running worker's own (it is written where a hardware |
| 5381 | 5471 | /// session falls back to software, so this follows reality), and the codec |
| @@ -9237,14 +9327,17 @@ fn subtitle_notice(session: &mut PlaybackSession, path: &std::path::Path) -> Opt | ||
| 9237 | 9327 | } |
| 9238 | 9328 | } |
| 9239 | 9329 | |
| 9240 | -/// What a subtitle row says under its name: how many cues it holds, or -- for a | |
| 9241 | -/// track that could not be read -- the engine's own reason, verbatim. A refusal | |
| 9242 | -/// is what the row is *for*: a PGS track off a BluRay remux is pictures, and a | |
| 9243 | -/// list that dropped it would say the film has no subtitles at all. | |
| 9330 | +/// What a subtitle row says under its name: how many cues it holds and whether | |
| 9331 | +/// they are pictures ([`engine::subtitle::SubtitleTrack::is_bitmap`]) -- which | |
| 9332 | +/// is the difference between a track an export writes into the file and one it | |
| 9333 | +/// can only draw -- or, for a track that could not be read, the engine's own | |
| 9334 | +/// reason verbatim. A refusal is still what a row can be *for*: a VobSub track | |
| 9335 | +/// dropped from the list would say the film has no subtitles at all. | |
| 9244 | 9336 | fn subtitle_detail(track: &engine::subtitle::SubtitleTrack) -> String { |
| 9245 | - match &track.refused { | |
| 9246 | - Some(why) => why.clone(), | |
| 9247 | - None => format!("{} cues", track.cues.len()), | |
| 9337 | + match (&track.refused, track.is_bitmap()) { | |
| 9338 | + (Some(why), _) => why.clone(), | |
| 9339 | + (None, true) => format!("{} cues — pictures", track.cues.len()), | |
| 9340 | + (None, false) => format!("{} cues", track.cues.len()), | |
| 9248 | 9341 | } |
| 9249 | 9342 | } |
| 9250 | 9343 | |
| @@ -15210,6 +15303,7 @@ mod tests { | ||
| 15210 | 15303 | start_us, |
| 15211 | 15304 | end_us, |
| 15212 | 15305 | text: text.to_string(), |
| 15306 | + image: None, | |
| 15213 | 15307 | }; |
| 15214 | 15308 | // Two cues that hand over exactly, and one that overlaps the second -- |
| 15215 | 15309 | // a sign over a line of dialogue, which is two plates at one moment. |
| @@ -16013,6 +16107,7 @@ fn main() { | ||
| 16013 | 16107 | fps: meta.map_or(30., |meta| meta.frame_rate), |
| 16014 | 16108 | name: name.clone(), |
| 16015 | 16109 | image: None, |
| 16110 | + sub_image: None, | |
| 16016 | 16111 | held: None, |
| 16017 | 16112 | ruler: Rc::default(), |
| 16018 | 16113 | // A second is [`PPS_DEFAULT`] pixels wide until someone |
crates/engine/Cargo.tomlmodified+1 -0
| @@ -109,6 +109,7 @@ rav1e = { version = "0.8.1", default-features = false, features = ["threading"] | ||
| 109 | 109 | # project offers -- an intraframe master, ProRes-shaped, large files and all. |
| 110 | 110 | # Built from `vendor/oxideav-h265`; see the note in the workspace manifest. |
| 111 | 111 | oxideav-h265 = { path = "../../vendor/oxideav-h265" } |
| 112 | +oxideav-sub-image = "0.0.7" | |
| 112 | 113 | |
| 113 | 114 | [dev-dependencies] |
| 114 | 115 | # The still-image suite compares what the timeline shows against the file's own |
crates/engine/src/demux.rsmodified+14 -7
| @@ -1320,9 +1320,10 @@ pub struct MkvSubtitle { | ||
| 1320 | 1320 | /// `CodecPrivate`: the ASS script header (`[Script Info]` through the |
| 1321 | 1321 | /// `[Events]` `Format:` line) for `S_TEXT/ASS`, empty for `S_TEXT/UTF8`. |
| 1322 | 1322 | pub private: Vec<u8>, |
| 1323 | - /// Every cue block, in storage order -- but only for the `S_TEXT/*` codecs. | |
| 1324 | - /// A bitmap track (PGS, VobSub) comes back declared and empty: its blocks | |
| 1325 | - /// are megabytes of pictures, and nothing here can draw one. | |
| 1323 | + /// Every cue block, in storage order, for the codecs [`crate::subtitle`] | |
| 1324 | + /// reads: the `S_TEXT/*` ones and `S_HDMV/PGS`, whose blocks are display | |
| 1325 | + /// sets of run-length pictures rather than lines. A codec neither of those | |
| 1326 | + /// -- VobSub off a DVD -- comes back declared and empty. | |
| 1326 | 1327 | pub cues: Vec<MkvCue>, |
| 1327 | 1328 | } |
| 1328 | 1329 | |
| @@ -1341,6 +1342,10 @@ pub struct MkvCue { | ||
| 1341 | 1342 | pub payload: Vec<u8>, |
| 1342 | 1343 | } |
| 1343 | 1344 | |
| 1345 | +/// The Matroska codec id of Blu-ray bitmap subtitles: run-length pictures in | |
| 1346 | +/// display sets, read by [`crate::subtitle`] as [`MkvCue`]s like any other. | |
| 1347 | +pub const PGS: &str = "S_HDMV/PGS"; | |
| 1348 | + | |
| 1344 | 1349 | /// The subtitle tracks of a Matroska file, in file order. An mp4's are not read |
| 1345 | 1350 | /// (its `tx3g` is a different beast, and no file this project opens carries |
| 1346 | 1351 | /// one); anything that is not a Matroska file at all is an error, as it is for |
| @@ -1380,10 +1385,10 @@ pub fn matroska_subtitles(path: &Path) -> crate::Result<Vec<MkvSubtitle>> { | ||
| 1380 | 1385 | } |
| 1381 | 1386 | at = stop; |
| 1382 | 1387 | } |
| 1383 | - // The text tracks only: see `MkvSubtitle::cues`. | |
| 1388 | + // The tracks something reads: see `MkvSubtitle::cues`. | |
| 1384 | 1389 | let wanted: Vec<u64> = tracks |
| 1385 | 1390 | .iter() |
| 1386 | - .filter(|t| t.codec.starts_with("S_TEXT")) | |
| 1391 | + .filter(|t| t.codec.starts_with("S_TEXT") || t.codec == PGS) | |
| 1387 | 1392 | .map(|t| t.number) |
| 1388 | 1393 | .collect(); |
| 1389 | 1394 | if !wanted.is_empty() { |
| @@ -1485,8 +1490,10 @@ fn mkv_subtitle_blocks( | ||
| 1485 | 1490 | if block.flags & 0x06 != 0 { |
| 1486 | 1491 | return Err("laced subtitle blocks are not supported".into()); |
| 1487 | 1492 | } |
| 1488 | - // A megabyte of *text* in one cue is a corrupt file, not a subtitle, | |
| 1489 | - // and a crafted length may not reach an allocation through here. | |
| 1493 | + // A megabyte in one cue is a corrupt file, not a subtitle -- a line | |
| 1494 | + // of dialogue is bytes and a PGS display set of run-length pictures | |
| 1495 | + // is tens of kilobytes -- and a crafted length may not reach an | |
| 1496 | + // allocation through here. | |
| 1490 | 1497 | if block.len > 1 << 20 { |
| 1491 | 1498 | return Err("a Matroska subtitle block larger than a megabyte".into()); |
| 1492 | 1499 | } |
crates/engine/src/export.rsmodified+23 -5
| @@ -570,7 +570,10 @@ fn export_subtitles( | ||
| 570 | 570 | return None; |
| 571 | 571 | } |
| 572 | 572 | let track = project.subtitles().get(settings.subtitles?)?; |
| 573 | - if track.refused.is_some() { | |
| 573 | + // A picture is not a line: the muxer writes an `S_TEXT/UTF8` track and a | |
| 574 | + // PGS cue has no words to put in one. `planned_subtitles` says so before | |
| 575 | + // the button is pressed rather than the file coming out short. | |
| 576 | + if track.refused.is_some() || track.is_bitmap() { | |
| 574 | 577 | return None; |
| 575 | 578 | } |
| 576 | 579 | let cues = timeline_cues(project, track, meta.frame_rate); |
| @@ -633,6 +636,7 @@ pub fn timeline_cues(project: &Project, track: &SubtitleTrack, fps: f64) -> Vec< | ||
| 633 | 636 | start_us, |
| 634 | 637 | end_us, |
| 635 | 638 | text: cue.text.clone(), |
| 639 | + image: cue.image.clone(), | |
| 636 | 640 | }) |
| 637 | 641 | }) |
| 638 | 642 | .collect(); |
| @@ -660,6 +664,7 @@ pub fn timeline_cues(project: &Project, track: &SubtitleTrack, fps: f64) -> Vec< | ||
| 660 | 664 | start_us: onto(a), |
| 661 | 665 | end_us: onto(b).min(end), |
| 662 | 666 | text: cue.text.clone(), |
| 667 | + image: cue.image.clone(), | |
| 663 | 668 | }); |
| 664 | 669 | } |
| 665 | 670 | } |
| @@ -679,6 +684,12 @@ pub fn planned_subtitles(project: &Project, format: Format, pick: Option<usize>) | ||
| 679 | 684 | if let Some(why) = &track.refused { |
| 680 | 685 | return format!("{} — {why}", track.label); |
| 681 | 686 | } |
| 687 | + // Drawn over the picture, written into no file: the exported track is text | |
| 688 | + // and these cues are bitmaps. Said whatever the format, because it is the | |
| 689 | + // track and not the container that cannot be carried. | |
| 690 | + if track.is_bitmap() { | |
| 691 | + return format!("{} — pictures; drawn, not written", track.label); | |
| 692 | + } | |
| 682 | 693 | match format { |
| 683 | 694 | _ if track.cues.is_empty() => "none".into(), |
| 684 | 695 | Format::Av1 | Format::Hevc => format!("{} → embedded", track.label), |
| @@ -2321,16 +2332,19 @@ mod tests { | ||
| 2321 | 2332 | start_us: 500_000, |
| 2322 | 2333 | end_us: 1_500_000, |
| 2323 | 2334 | text: "first".into(), |
| 2335 | + image: None, | |
| 2324 | 2336 | }, |
| 2325 | 2337 | Cue { |
| 2326 | 2338 | start_us: 2_000_000, |
| 2327 | 2339 | end_us: 3_250_000, |
| 2328 | 2340 | text: "second".into(), |
| 2341 | + image: None, | |
| 2329 | 2342 | }, |
| 2330 | 2343 | Cue { |
| 2331 | 2344 | start_us: 4_000_000, |
| 2332 | 2345 | end_us: 4_750_000, |
| 2333 | 2346 | text: "third".into(), |
| 2347 | + image: None, | |
| 2334 | 2348 | }, |
| 2335 | 2349 | ], |
| 2336 | 2350 | refused: None, |
| @@ -2359,13 +2373,15 @@ mod tests { | ||
| 2359 | 2373 | Cue { |
| 2360 | 2374 | start_us: 500_000, |
| 2361 | 2375 | end_us: 1_500_000, |
| 2362 | - text: "first".into() | |
| 2376 | + text: "first".into(), | |
| 2377 | + image: None, | |
| 2363 | 2378 | }, |
| 2364 | 2379 | // Clipped to the three seconds the export writes... |
| 2365 | 2380 | Cue { |
| 2366 | 2381 | start_us: 2_000_000, |
| 2367 | 2382 | end_us: 3_000_000, |
| 2368 | - text: "second".into() | |
| 2383 | + text: "second".into(), | |
| 2384 | + image: None, | |
| 2369 | 2385 | }, |
| 2370 | 2386 | // ...and the one wholly past the end is not written at all. |
| 2371 | 2387 | ] |
| @@ -2393,13 +2409,15 @@ mod tests { | ||
| 2393 | 2409 | Cue { |
| 2394 | 2410 | start_us: 500_000, |
| 2395 | 2411 | end_us: 1_250_000, |
| 2396 | - text: "second".into() | |
| 2412 | + text: "second".into(), | |
| 2413 | + image: None, | |
| 2397 | 2414 | }, |
| 2398 | 2415 | // ...and the third simply moves back the two seconds that went. |
| 2399 | 2416 | Cue { |
| 2400 | 2417 | start_us: 2_000_000, |
| 2401 | 2418 | end_us: 2_750_000, |
| 2402 | - text: "third".into() | |
| 2419 | + text: "third".into(), | |
| 2420 | + image: None, | |
| 2403 | 2421 | }, |
| 2404 | 2422 | ] |
| 2405 | 2423 | ); |
crates/engine/src/subtitle.rsmodified+163 -9
| @@ -14,12 +14,20 @@ | ||
| 14 | 14 | //! renderer is a later thing, and a cue nobody can draw yet is still a cue that |
| 15 | 15 | //! has to be listed, timed and saved. |
| 16 | 16 | //! |
| 17 | -//! What is *refused* is refused by name rather than by omission | |
| 18 | -//! ([`SubtitleTrack::refused`]): the bitmap subtitle formats -- PGS off a | |
| 19 | -//! BluRay remux, VobSub off a DVD -- are pictures, and a film carrying four of | |
| 20 | -//! them opens with four rows that say so instead of opening with an empty list. | |
| 17 | +//! A cue is not always words. `S_HDMV/PGS` off a BluRay remux is *pictures* -- | |
| 18 | +//! run-length bitmaps the disc composed against its own frame -- and one of | |
| 19 | +//! those is a [`Cue`] with a [`CueImage`] on it and no text. The decoding is | |
| 20 | +//! `oxideav-sub-image`, for the reason above. Everything downstream carries the | |
| 21 | +//! picture the way it carries the string: [`crate::export::timeline_cues`] maps | |
| 22 | +//! it onto the timeline, a front-end draws it over the film. | |
| 23 | +//! | |
| 24 | +//! What is still *refused* is refused by name rather than by omission | |
| 25 | +//! ([`SubtitleTrack::refused`]): VobSub off a DVD is a picture format nothing | |
| 26 | +//! here reads, and a file carrying one opens with a row that says so instead of | |
| 27 | +//! opening with an empty list. | |
| 21 | 28 | |
| 22 | 29 | use std::path::{Path, PathBuf}; |
| 30 | +use std::sync::Arc; | |
| 23 | 31 | |
| 24 | 32 | use oxideav_subtitle::ir::plain_text; |
| 25 | 33 | |
| @@ -37,6 +45,84 @@ pub struct Cue { | ||
| 37 | 45 | /// is to keep `oxideav_core::Segment` beside this string -- the parsers |
| 38 | 46 | /// already hand it over -- and it belongs to whoever writes the renderer. |
| 39 | 47 | pub text: String, |
| 48 | + /// The picture this cue *is*, for a track that is pictures rather than | |
| 49 | + /// lines. `None` for every text cue -- an `.srt`, a `.vtt`, an `.ass`, an | |
| 50 | + /// `S_TEXT/*` track -- and `Some` for every cue of a PGS one, which then | |
| 51 | + /// has no [`text`](Self::text) at all. | |
| 52 | + /// | |
| 53 | + /// Shared rather than owned: [`crate::export::timeline_cues`] copies a whole | |
| 54 | + /// track's cues, per repaint, and a display set is tens of kilobytes. | |
| 55 | + pub image: Option<Arc<CueImage>>, | |
| 56 | +} | |
| 57 | + | |
| 58 | +/// A cue that is a picture: one PGS display set, kept the way the Matroska | |
| 59 | +/// block held it -- run-length and palettised, tens of kilobytes -- and turned | |
| 60 | +/// into pixels only when something is about to draw it ([`Self::rgba`]). | |
| 61 | +/// | |
| 62 | +/// Kept encoded because decoded is enormous: this film's canvas is 8 MB of | |
| 63 | +/// RGBA and its four PGS tracks carry about eleven thousand display sets | |
| 64 | +/// between them, so a track that decoded itself at import would cost gigabytes | |
| 65 | +/// where it now costs the thirty megabytes the file already spends on it. | |
| 66 | +#[derive(Clone, Debug, PartialEq, Eq)] | |
| 67 | +pub struct CueImage { | |
| 68 | + /// The canvas the disc composed against: 1920x1080 for a BluRay, whatever | |
| 69 | + /// the film's own frame was cropped to. The whole canvas is the picture -- | |
| 70 | + /// transparent everywhere the cue does not paint -- so a caller lays it | |
| 71 | + /// over the film rather than placing it. | |
| 72 | + pub width: u32, | |
| 73 | + pub height: u32, | |
| 74 | + /// The display set: `type`, big-endian size, body, segment after segment, | |
| 75 | + /// which is the shape a Matroska block holds PGS in. | |
| 76 | + set: Vec<u8>, | |
| 77 | +} | |
| 78 | + | |
| 79 | +impl CueImage { | |
| 80 | + /// The picture: straight (not premultiplied) RGBA, `width * height * 4` | |
| 81 | + /// bytes, transparent wherever the cue paints nothing. `None` for a display | |
| 82 | + /// set the decoder will not have, which is a corrupt block. | |
| 83 | + /// | |
| 84 | + /// Decoded on the spot: the run-length walk and one canvas-sized buffer, | |
| 85 | + /// which is a thing to do when the cue on screen changes and not sixty | |
| 86 | + /// times a second. A caller that draws these keeps the answer for as long | |
| 87 | + /// as the cue is up. | |
| 88 | + pub fn rgba(&self) -> Option<Vec<u8>> { | |
| 89 | + use oxideav_core::{CodecId, CodecParameters, Frame, Packet, TimeBase}; | |
| 90 | + let params = CodecParameters::subtitle(CodecId::new(oxideav_sub_image::PGS_CODEC_ID)); | |
| 91 | + let mut decoder = oxideav_sub_image::pgs::make_decoder(¶ms).ok()?; | |
| 92 | + // The decoder reads a `.sup`'s framing and a Matroska block is the same | |
| 93 | + // segments with the `PG` magic and the timestamps taken off, so they go | |
| 94 | + // back on. The times themselves are the block's and are already on the | |
| 95 | + // `Cue`, so they are written as nothing. | |
| 96 | + let mut sup = Vec::with_capacity(self.set.len() + 64); | |
| 97 | + for (kind, body) in segments(&self.set)? { | |
| 98 | + sup.extend_from_slice(b"PG"); | |
| 99 | + sup.extend_from_slice(&[0; 8]); | |
| 100 | + sup.push(kind); | |
| 101 | + sup.extend_from_slice(&(body.len() as u16).to_be_bytes()); | |
| 102 | + sup.extend_from_slice(body); | |
| 103 | + } | |
| 104 | + decoder | |
| 105 | + .send_packet(&Packet::new(0, TimeBase::new(1, 90_000), sup)) | |
| 106 | + .ok()?; | |
| 107 | + match decoder.receive_frame().ok()? { | |
| 108 | + Frame::Video(frame) => Some(frame.planes.into_iter().next()?.data), | |
| 109 | + _ => None, | |
| 110 | + } | |
| 111 | + } | |
| 112 | +} | |
| 113 | + | |
| 114 | +/// The segments of a display set as a Matroska block holds them: a type byte, a | |
| 115 | +/// big-endian size, that many bytes of body, and again until the block is out. | |
| 116 | +/// `None` for bytes that do not walk -- a truncated or mislabelled block. | |
| 117 | +fn segments(set: &[u8]) -> Option<Vec<(u8, &[u8])>> { | |
| 118 | + let mut out = Vec::new(); | |
| 119 | + let mut at = 0; | |
| 120 | + while at < set.len() { | |
| 121 | + let size = u16::from_be_bytes([*set.get(at + 1)?, *set.get(at + 2)?]) as usize; | |
| 122 | + out.push((set[at], set.get(at + 3..at + 3 + size)?)); | |
| 123 | + at += 3 + size; | |
| 124 | + } | |
| 125 | + Some(out) | |
| 40 | 126 | } |
| 41 | 127 | |
| 42 | 128 | /// A subtitle track: where it came from and what it says. |
| @@ -73,6 +159,17 @@ impl SubtitleTrack { | ||
| 73 | 159 | refused: Some(why), |
| 74 | 160 | } |
| 75 | 161 | } |
| 162 | + | |
| 163 | + /// Whether this track's cues are pictures rather than lines -- a PGS track | |
| 164 | + /// off a remux. What it decides is where words are the only answer: an | |
| 165 | + /// export writes a *text* track and cannot carry these | |
| 166 | + /// ([`crate::export::planned_subtitles`] says so out loud). | |
| 167 | + /// | |
| 168 | + /// Read off the first cue rather than all of them, because a track is one | |
| 169 | + /// codec: PGS blocks are pictures to the last one. | |
| 170 | + pub fn is_bitmap(&self) -> bool { | |
| 171 | + self.cues.first().is_some_and(|c| c.image.is_some()) | |
| 172 | + } | |
| 76 | 173 | } |
| 77 | 174 | |
| 78 | 175 | /// The one door a saved subtitle line comes back in through: a standalone file |
| @@ -126,13 +223,13 @@ fn external(path: &Path) -> SubtitleTrack { | ||
| 126 | 223 | pub fn of_matroska(path: &Path) -> crate::Result<Vec<SubtitleTrack>> { |
| 127 | 224 | Ok(crate::demux::matroska_subtitles(path)? |
| 128 | 225 | .into_iter() |
| 129 | - .map(|t| { | |
| 226 | + .map(|mut t| { | |
| 130 | 227 | let label = match (t.name.is_empty(), t.language.as_str()) { |
| 131 | 228 | (true, lang) => lang.to_owned(), |
| 132 | 229 | (false, "und") => t.name.clone(), |
| 133 | 230 | (false, lang) => format!("{lang} — {}", t.name), |
| 134 | 231 | }; |
| 135 | - match cues_of(&t) { | |
| 232 | + match cues_of(&mut t) { | |
| 136 | 233 | Ok(cues) => SubtitleTrack { |
| 137 | 234 | path: path.to_path_buf(), |
| 138 | 235 | track: Some(t.number), |
| @@ -156,7 +253,11 @@ pub fn of_matroska(path: &Path) -> crate::Result<Vec<SubtitleTrack>> { | ||
| 156 | 253 | /// reads -- rather than the markup being re-derived here -- which is also what |
| 157 | 254 | /// makes an embedded track and the standalone file it was muxed from come back |
| 158 | 255 | /// as the same cues. |
| 159 | -fn cues_of(track: &crate::demux::MkvSubtitle) -> Result<Vec<Cue>, String> { | |
| 256 | +/// | |
| 257 | +/// A PGS track is neither: its blocks are pictures, and they are moved out of | |
| 258 | +/// `track` rather than copied ([`pgs_cues`]) -- thirty megabytes a track is not | |
| 259 | +/// a thing to hold twice. | |
| 260 | +fn cues_of(track: &mut crate::demux::MkvSubtitle) -> Result<Vec<Cue>, String> { | |
| 160 | 261 | let cues = match track.codec.as_str() { |
| 161 | 262 | "S_TEXT/UTF8" | "S_TEXT/ASCII" => track |
| 162 | 263 | .cues |
| @@ -165,9 +266,11 @@ fn cues_of(track: &crate::demux::MkvSubtitle) -> Result<Vec<Cue>, String> { | ||
| 165 | 266 | start_us: c.start_us, |
| 166 | 267 | end_us: end_of(track, c), |
| 167 | 268 | text: srt_body(&String::from_utf8_lossy(&c.payload)), |
| 269 | + image: None, | |
| 168 | 270 | }) |
| 169 | 271 | .collect(), |
| 170 | 272 | "S_TEXT/ASS" | "S_TEXT/SSA" => ass_cues(track)?, |
| 273 | + crate::demux::PGS => pgs_cues(track), | |
| 171 | 274 | codec => { |
| 172 | 275 | return Err(format!( |
| 173 | 276 | "{codec} subtitles are pictures, not text — this track is listed, not read" |
| @@ -177,13 +280,62 @@ fn cues_of(track: &crate::demux::MkvSubtitle) -> Result<Vec<Cue>, String> { | ||
| 177 | 280 | Ok(cues) |
| 178 | 281 | } |
| 179 | 282 | |
| 283 | +/// The cues of an `S_HDMV/PGS` track: one display set per block, and a display | |
| 284 | +/// set is a picture the disc composes onto its own canvas. | |
| 285 | +/// | |
| 286 | +/// Only the blocks that compose *something* are cues. The other half of them | |
| 287 | +/// are the disc's "take it off again" -- a composition with no object on it -- | |
| 288 | +/// and they are what says when the picture before them goes away, which is why | |
| 289 | +/// they are walked and not skipped. A muxer writes `BlockDuration` zero for all | |
| 290 | +/// of them, so [`end_of`] is what pairs the two. | |
| 291 | +fn pgs_cues(track: &mut crate::demux::MkvSubtitle) -> Vec<Cue> { | |
| 292 | + // Before the blocks are emptied: `end_of` reads the ones after this one. | |
| 293 | + let ends: Vec<i64> = track.cues.iter().map(|c| end_of(track, c)).collect(); | |
| 294 | + track | |
| 295 | + .cues | |
| 296 | + .iter_mut() | |
| 297 | + .zip(ends) | |
| 298 | + .filter_map(|(block, end_us)| { | |
| 299 | + let (width, height) = pgs_canvas(&block.payload)?; | |
| 300 | + Some(Cue { | |
| 301 | + start_us: block.start_us, | |
| 302 | + end_us, | |
| 303 | + text: String::new(), | |
| 304 | + image: Some(Arc::new(CueImage { | |
| 305 | + width, | |
| 306 | + height, | |
| 307 | + set: std::mem::take(&mut block.payload), | |
| 308 | + })), | |
| 309 | + }) | |
| 310 | + }) | |
| 311 | + .collect() | |
| 312 | +} | |
| 313 | + | |
| 314 | +/// The canvas of a display set that paints something -- its `PCS` says how big | |
| 315 | +/// the disc's picture is, and how many objects are composed onto it. | |
| 316 | +/// | |
| 317 | +/// `None` when nothing is composed, which is the disc clearing the screen, and | |
| 318 | +/// `None` for bytes that are not a display set at all. | |
| 319 | +fn pgs_canvas(set: &[u8]) -> Option<(u32, u32)> { | |
| 320 | + /// `PresentationCompositionSegment`, which every display set opens with. | |
| 321 | + const PCS: u8 = 0x16; | |
| 322 | + let (_, pcs) = segments(set)?.into_iter().find(|&(kind, _)| kind == PCS)?; | |
| 323 | + // Width and height, then the frame rate, the composition number and state, | |
| 324 | + // the palette-update flag and the palette id -- and then how many objects | |
| 325 | + // this composition puts on the canvas. | |
| 326 | + let width = u16::from_be_bytes([*pcs.first()?, *pcs.get(1)?]); | |
| 327 | + let height = u16::from_be_bytes([*pcs.get(2)?, *pcs.get(3)?]); | |
| 328 | + (*pcs.get(10)? > 0).then_some((u32::from(width), u32::from(height))) | |
| 329 | +} | |
| 330 | + | |
| 180 | 331 | /// When a cue goes away: its `BlockDuration`, or -- for the muxer that writes |
| 181 | -/// none -- when the next one arrives, and [`NO_DURATION_US`] for the last. | |
| 332 | +/// none, and for the PGS block that writes a zero, which says the same thing -- | |
| 333 | +/// when the next one arrives, and [`NO_DURATION_US`] for the last. | |
| 182 | 334 | fn end_of(track: &crate::demux::MkvSubtitle, cue: &crate::demux::MkvCue) -> i64 { |
| 183 | 335 | /// How long a cue with nothing to say about it stays up: two seconds, a |
| 184 | 336 | /// read of one line. |
| 185 | 337 | const NO_DURATION_US: i64 = 2_000_000; |
| 186 | - match cue.duration_us { | |
| 338 | + match cue.duration_us.filter(|&d| d > 0) { | |
| 187 | 339 | Some(d) => cue.start_us + d, |
| 188 | 340 | None => track |
| 189 | 341 | .cues |
| @@ -259,6 +411,7 @@ fn ass_cues(track: &crate::demux::MkvSubtitle) -> Result<Vec<Cue>, String> { | ||
| 259 | 411 | c.end_us |
| 260 | 412 | }, |
| 261 | 413 | text: plain_text(&c.segments), |
| 414 | + image: None, | |
| 262 | 415 | }) |
| 263 | 416 | .collect()) |
| 264 | 417 | } |
| @@ -305,6 +458,7 @@ fn parse_file(path: &Path) -> crate::Result<Vec<Cue>> { | ||
| 305 | 458 | start_us: c.start_us, |
| 306 | 459 | end_us: c.end_us, |
| 307 | 460 | text: plain_text(&c.segments), |
| 461 | + image: None, | |
| 308 | 462 | }) |
| 309 | 463 | .collect()) |
| 310 | 464 | } |
crates/engine/tests/export_subs.rsmodified+3 -0
| @@ -84,16 +84,19 @@ fn track() -> SubtitleTrack { | ||
| 84 | 84 | start_us: 500_000, |
| 85 | 85 | end_us: 1_500_000, |
| 86 | 86 | text: "first line".into(), |
| 87 | + image: None, | |
| 87 | 88 | }, |
| 88 | 89 | Cue { |
| 89 | 90 | start_us: 2_000_000, |
| 90 | 91 | end_us: 3_250_000, |
| 91 | 92 | text: "second line\nwith a break".into(), |
| 93 | + image: None, | |
| 92 | 94 | }, |
| 93 | 95 | Cue { |
| 94 | 96 | start_us: 4_000_000, |
| 95 | 97 | end_us: 4_750_000, |
| 96 | 98 | text: "third line".into(), |
| 99 | + image: None, | |
| 97 | 100 | }, |
| 98 | 101 | ], |
| 99 | 102 | refused: None, |
crates/engine/tests/subtitles.rsmodified+222 -30
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | //! Subtitles: the cues of a file beside the media and of a track inside it are |
| 2 | -//! the same cues, a project keeps them across a save, and the formats that are | |
| 3 | -//! pictures rather than text are listed and skipped by name rather than | |
| 4 | -//! silently dropped. | |
| 2 | +//! the same cues, a project keeps them across a save, and a track that is | |
| 3 | +//! pictures rather than text is read as pictures rather than listed and | |
| 4 | +//! skipped. | |
| 5 | 5 | //! |
| 6 | 6 | //! ```text |
| 7 | 7 | //! cargo test -p engine --release --test subtitles -- --nocapture |
| @@ -39,16 +39,19 @@ fn expected() -> Vec<Cue> { | ||
| 39 | 39 | start_us: 500_000, |
| 40 | 40 | end_us: 1_500_000, |
| 41 | 41 | text: "first line".into(), |
| 42 | + image: None, | |
| 42 | 43 | }, |
| 43 | 44 | Cue { |
| 44 | 45 | start_us: 2_000_000, |
| 45 | 46 | end_us: 3_250_000, |
| 46 | 47 | text: "second line\nwith a break".into(), |
| 48 | + image: None, | |
| 47 | 49 | }, |
| 48 | 50 | Cue { |
| 49 | 51 | start_us: 4_000_000, |
| 50 | 52 | end_us: 4_750_000, |
| 51 | 53 | text: "third line".into(), |
| 54 | + image: None, | |
| 52 | 55 | }, |
| 53 | 56 | ] |
| 54 | 57 | } |
| @@ -100,16 +103,15 @@ fn a_file_beside_the_media_and_a_track_inside_it_read_the_same() { | ||
| 100 | 103 | ); |
| 101 | 104 | } |
| 102 | 105 | |
| 103 | -/// A subtitle track this cannot read is a row that says why -- never an error | |
| 104 | -/// that takes the whole file down with it. The repro is a BluRay remux | |
| 105 | -/// carrying four PGS tracks: opening it lists four subtitles it cannot draw, | |
| 106 | -/// and the picture and the sound come up regardless. | |
| 106 | +/// The whole of the bitmap half on a file small enough to read in one screen: | |
| 107 | +/// four PGS tracks, the first of them carrying one picture and the disc's own | |
| 108 | +/// "take it off again" after it. A row per track, cues on the one with blocks, | |
| 109 | +/// and the picture decodes to the pixels the display set describes. | |
| 107 | 110 | /// |
| 108 | 111 | /// Written here rather than muxed by the fixture script because ffmpeg cannot |
| 109 | -/// encode text subtitles into pictures; the bytes below are the `Tracks` | |
| 110 | -/// element of such a file and nothing else. | |
| 112 | +/// encode text subtitles into pictures; the bytes below are that Matroska file. | |
| 111 | 113 | #[test] |
| 112 | -fn bitmap_subtitles_are_listed_and_skipped_by_name() { | |
| 114 | +fn a_pgs_track_is_read_as_pictures_and_the_erase_after_one_ends_it() { | |
| 113 | 115 | let dir = scratch("pgs"); |
| 114 | 116 | let file = dir.join("remux.mkv"); |
| 115 | 117 | std::fs::write(&file, pgs_matroska()).expect("write the hand-made mkv"); |
| @@ -118,17 +120,96 @@ fn bitmap_subtitles_are_listed_and_skipped_by_name() { | ||
| 118 | 120 | assert_eq!(tracks.len(), 4, "every PGS track is listed"); |
| 119 | 121 | for (i, track) in tracks.iter().enumerate() { |
| 120 | 122 | assert_eq!(track.track, Some(i as u64 + 1)); |
| 121 | - assert_eq!(track.cues, Vec::new(), "no cues to have"); | |
| 122 | - let why = track.refused.clone().unwrap_or_default(); | |
| 123 | - assert!( | |
| 124 | - why.contains("S_HDMV/PGS") && why.contains("pictures"), | |
| 125 | - "the refusal names the codec: {why:?}" | |
| 126 | - ); | |
| 123 | + assert_eq!(track.refused, None, "a PGS track is read, not refused"); | |
| 127 | 124 | } |
| 128 | 125 | // The language is what a list shows, and `und` is what the spec says a |
| 129 | 126 | // track that names none is. |
| 130 | 127 | assert_eq!(tracks[0].label, "eng"); |
| 131 | 128 | assert_eq!(tracks[3].label, "und"); |
| 129 | + // The three tracks with no blocks are tracks with no cues -- and a track | |
| 130 | + // with no cues is not a bitmap track as far as anything downstream knows. | |
| 131 | + for track in &tracks[1..] { | |
| 132 | + assert_eq!(track.cues, Vec::new()); | |
| 133 | + assert!(!track.is_bitmap()); | |
| 134 | + } | |
| 135 | + | |
| 136 | + // Two blocks, one cue: the second composes no object, which is the disc | |
| 137 | + // clearing the screen, and that is where the first one ends. `BlockDuration` | |
| 138 | + // says nothing here, exactly as it says zero on a real remux. | |
| 139 | + let track = &tracks[0]; | |
| 140 | + assert!(track.is_bitmap(), "its cues are pictures"); | |
| 141 | + assert_eq!(track.cues.len(), 1, "the erase is not a cue of its own"); | |
| 142 | + let cue = &track.cues[0]; | |
| 143 | + assert_eq!((cue.start_us, cue.end_us), (500_000, 2_000_000)); | |
| 144 | + assert_eq!(cue.text, "", "a picture has no words"); | |
| 145 | + | |
| 146 | + let image = cue.image.as_ref().expect("the cue is a picture"); | |
| 147 | + assert_eq!( | |
| 148 | + (image.width, image.height), | |
| 149 | + (8, 4), | |
| 150 | + "the canvas is the disc's frame, not the object's box" | |
| 151 | + ); | |
| 152 | + let rgba = image.rgba().expect("the display set decodes"); | |
| 153 | + assert_eq!(rgba.len(), 8 * 4 * 4); | |
| 154 | + // A 2x2 white square at (1, 1) on an otherwise transparent canvas: opaque | |
| 155 | + // where the object is, and nothing anywhere else. | |
| 156 | + let at = |x: usize, y: usize| rgba[(y * 8 + x) * 4..(y * 8 + x) * 4 + 4].to_vec(); | |
| 157 | + assert_eq!(at(1, 1), vec![235, 235, 235, 255]); | |
| 158 | + assert_eq!(at(2, 2), vec![235, 235, 235, 255]); | |
| 159 | + assert_eq!(at(0, 0), vec![0, 0, 0, 0]); | |
| 160 | + assert_eq!(at(3, 1), vec![0, 0, 0, 0], "and no wider than it is"); | |
| 161 | + assert_eq!( | |
| 162 | + rgba.chunks_exact(4).filter(|p| p[3] > 0).count(), | |
| 163 | + 4, | |
| 164 | + "four painted pixels and no more" | |
| 165 | + ); | |
| 166 | +} | |
| 167 | + | |
| 168 | +/// The film that reported the bug: four PGS tracks beside one `S_TEXT/UTF8` | |
| 169 | +/// one, none of them refused any more. Skipped where the file is not -- it is | |
| 170 | +/// a 4K remux and lives in a local folder, not in this repository. | |
| 171 | +#[test] | |
| 172 | +fn the_remux_that_reported_the_bug_reads_all_five_of_its_tracks() { | |
| 173 | + let film = std::path::Path::new( | |
| 174 | + "/path/to/a-real-4k-pgs-film.mkv", | |
| 175 | + ); | |
| 176 | + if !film.exists() { | |
| 177 | + eprintln!("skipped: {} is not here", film.display()); | |
| 178 | + return; | |
| 179 | + } | |
| 180 | + let tracks = subtitle::of_matroska(film).expect("the walk"); | |
| 181 | + assert_eq!(tracks.len(), 5); | |
| 182 | + assert!( | |
| 183 | + tracks.iter().all(|t| t.refused.is_none()), | |
| 184 | + "every row is one a click can pick: {:?}", | |
| 185 | + tracks.iter().map(|t| &t.refused).collect::<Vec<_>>() | |
| 186 | + ); | |
| 187 | + // One text track and four of pictures, which is what the remux carries. | |
| 188 | + let bitmap: Vec<_> = tracks.iter().filter(|t| t.is_bitmap()).collect(); | |
| 189 | + assert_eq!(bitmap.len(), 4); | |
| 190 | + for track in &bitmap { | |
| 191 | + assert!( | |
| 192 | + track.cues.len() > 100, | |
| 193 | + "{} — {} cues", | |
| 194 | + track.label, | |
| 195 | + track.cues.len() | |
| 196 | + ); | |
| 197 | + let cue = &track.cues[0]; | |
| 198 | + let image = cue.image.as_ref().expect("a picture cue"); | |
| 199 | + assert_eq!( | |
| 200 | + (image.width, image.height), | |
| 201 | + (1920, 1080), | |
| 202 | + "the BluRay canvas" | |
| 203 | + ); | |
| 204 | + assert!(cue.end_us > cue.start_us, "a cue that is up for a while"); | |
| 205 | + let rgba = image.rgba().expect("the first display set decodes"); | |
| 206 | + assert_eq!(rgba.len(), 1920 * 1080 * 4); | |
| 207 | + assert!( | |
| 208 | + rgba.chunks_exact(4).any(|p| p[3] > 0), | |
| 209 | + "{} — the first cue paints something", | |
| 210 | + track.label | |
| 211 | + ); | |
| 212 | + } | |
| 132 | 213 | } |
| 133 | 214 | |
| 134 | 215 | /// A saved project comes back with its subtitles: the `.edith` names the file |
| @@ -187,6 +268,55 @@ fn a_project_keeps_its_subtitles_across_a_save() { | ||
| 187 | 268 | ); |
| 188 | 269 | } |
| 189 | 270 | |
| 271 | +/// A bitmap track survives a save the same way a text one does, and for the | |
| 272 | +/// same reason: a `.edith` holds the file and the track number, never a cue, so | |
| 273 | +/// what comes back is what the file still says. The pictures are read out of | |
| 274 | +/// the remux again on the way in -- thirty megabytes a track is exactly what a | |
| 275 | +/// project file is not for. | |
| 276 | +#[test] | |
| 277 | +fn a_project_keeps_a_bitmap_track_as_a_reference_and_reads_its_pictures_back() { | |
| 278 | + let dir = scratch("save_pgs"); | |
| 279 | + let media = dir.join("test_av.mp4"); | |
| 280 | + std::fs::copy(asset("test_av.mp4"), &media).expect("copy the media fixture"); | |
| 281 | + let remux = dir.join("remux.mkv"); | |
| 282 | + std::fs::write(&remux, pgs_matroska()).expect("write the hand-made mkv"); | |
| 283 | + | |
| 284 | + let mut session = engine::PlaybackSession::open(&media).expect("open the fixture"); | |
| 285 | + session.set_gain(0.0); | |
| 286 | + assert_eq!( | |
| 287 | + session.import_subtitles(&remux).expect("the remux imports"), | |
| 288 | + 4, | |
| 289 | + "one row per PGS track" | |
| 290 | + ); | |
| 291 | + assert!(session.subtitles()[0].is_bitmap()); | |
| 292 | + // An mkv export writes a *text* track and these cues are pictures, so the | |
| 293 | + // card says so before the button rather than the file coming out short. | |
| 294 | + // The same `is_bitmap` is what makes `export_subtitles` write none. | |
| 295 | + let planned = session.planned_subtitles(engine::export::Format::Hevc, Some(0)); | |
| 296 | + assert!( | |
| 297 | + planned.contains("pictures") && planned.contains("not written"), | |
| 298 | + "{planned}" | |
| 299 | + ); | |
| 300 | + | |
| 301 | + let project = dir.join("cut.edith"); | |
| 302 | + session.save_project(&project).expect("save"); | |
| 303 | + let text = std::fs::read_to_string(&project).expect("read the project back"); | |
| 304 | + assert!( | |
| 305 | + text.contains("\nsubtitle 1 remux.mkv\n"), | |
| 306 | + "the track is a reference and no picture is written: {text}" | |
| 307 | + ); | |
| 308 | + | |
| 309 | + let back = engine::PlaybackSession::open_project(&project).expect("open the project"); | |
| 310 | + assert_eq!(back.subtitles().len(), 4); | |
| 311 | + let track = &back.subtitles()[0]; | |
| 312 | + assert_eq!(track.refused, None); | |
| 313 | + assert!(track.is_bitmap(), "and it comes back as pictures"); | |
| 314 | + assert_eq!(track.cues.len(), 1); | |
| 315 | + let image = track.cues[0].image.as_ref().expect("the picture is back"); | |
| 316 | + assert_eq!((image.width, image.height), (8, 4)); | |
| 317 | + assert_eq!(image.rgba().expect("it decodes").len(), 8 * 4 * 4); | |
| 318 | +} | |
| 319 | + | |
| 190 | 320 | /// A refusal a front-end shows at the moment of the import, rather than a row |
| 191 | 321 | /// nobody asked for: a file that is not a subtitle format at all. |
| 192 | 322 | #[test] |
| @@ -207,20 +337,21 @@ fn importing_something_that_is_not_a_subtitle_says_so() { | ||
| 207 | 337 | assert_eq!(session.subtitles().len(), 0, "and nothing was added"); |
| 208 | 338 | } |
| 209 | 339 | |
| 210 | -/// The `Tracks` element of a Matroska file carrying four PGS subtitle tracks | |
| 211 | -/// and nothing else -- enough for the walk, which stops at the first `Cluster` | |
| 212 | -/// and there is none. | |
| 340 | +/// An EBML element: its id as it is written in the file, then an 8-byte | |
| 341 | +/// length, which is always a legal encoding of one. | |
| 342 | +fn element(id: &[u8], body: &[u8]) -> Vec<u8> { | |
| 343 | + let mut out = id.to_vec(); | |
| 344 | + let mut len = (body.len() as u64).to_be_bytes(); | |
| 345 | + len[0] |= 0x01; // the 8-byte length marker | |
| 346 | + out.extend_from_slice(&len); | |
| 347 | + out.extend_from_slice(body); | |
| 348 | + out | |
| 349 | +} | |
| 350 | + | |
| 351 | +/// A Matroska file carrying four PGS subtitle tracks, the first of them with | |
| 352 | +/// one display set and the erase that ends it. No `Info`, so the timestamp | |
| 353 | +/// scale is the spec's default of a millisecond a tick. | |
| 213 | 354 | fn pgs_matroska() -> Vec<u8> { |
| 214 | - /// An EBML element: its id as it is written in the file, then an 8-byte | |
| 215 | - /// length, which is always a legal encoding of one. | |
| 216 | - fn element(id: &[u8], body: &[u8]) -> Vec<u8> { | |
| 217 | - let mut out = id.to_vec(); | |
| 218 | - let mut len = (body.len() as u64).to_be_bytes(); | |
| 219 | - len[0] |= 0x01; // the 8-byte length marker | |
| 220 | - out.extend_from_slice(&len); | |
| 221 | - out.extend_from_slice(body); | |
| 222 | - out | |
| 223 | - } | |
| 224 | 355 | let mut tracks = Vec::new(); |
| 225 | 356 | for (number, language) in [(1u8, "eng"), (2, "fra"), (3, "spa"), (4, "")] { |
| 226 | 357 | let mut entry = Vec::new(); |
| @@ -232,6 +363,67 @@ fn pgs_matroska() -> Vec<u8> { | ||
| 232 | 363 | } |
| 233 | 364 | tracks.extend(element(&[0xAE], &entry)); // TrackEntry |
| 234 | 365 | } |
| 235 | - let segment = element(&[0x16, 0x54, 0xAE, 0x6B], &tracks); // Tracks | |
| 366 | + // A `SimpleBlock` on track 1: the track number as a one-byte vint, the | |
| 367 | + // timestamp relative to the cluster's, and no flags. | |
| 368 | + let block = |at: i16, data: Vec<u8>| { | |
| 369 | + let mut body = vec![0x81, (at >> 8) as u8, at as u8, 0x00]; | |
| 370 | + body.extend(data); | |
| 371 | + element(&[0xA3], &body) | |
| 372 | + }; | |
| 373 | + let mut cluster = element(&[0xE7], &[0x00]); // Timestamp: 0 | |
| 374 | + cluster.extend(block(500, pgs_display_set())); | |
| 375 | + cluster.extend(block(2000, pgs_erase())); | |
| 376 | + | |
| 377 | + let mut segment = element(&[0x16, 0x54, 0xAE, 0x6B], &tracks); // Tracks | |
| 378 | + segment.extend(element(&[0x1F, 0x43, 0xB6, 0x75], &cluster)); // Cluster | |
| 236 | 379 | element(&[0x18, 0x53, 0x80, 0x67], &segment) // Segment |
| 237 | 380 | } |
| 381 | + | |
| 382 | +/// One PGS segment as a Matroska block holds it: the type, a big-endian size, | |
| 383 | +/// the body -- and none of the `PG` magic or timestamps a `.sup` file writes, | |
| 384 | +/// which the muxer takes off. | |
| 385 | +fn pgs_segment(kind: u8, body: &[u8]) -> Vec<u8> { | |
| 386 | + let mut out = vec![kind]; | |
| 387 | + out.extend_from_slice(&(body.len() as u16).to_be_bytes()); | |
| 388 | + out.extend_from_slice(body); | |
| 389 | + out | |
| 390 | +} | |
| 391 | + | |
| 392 | +/// A display set that paints a 2x2 white square at (1, 1) of an 8x4 canvas: | |
| 393 | +/// the composition, the palette it colours by, the run-length object itself, | |
| 394 | +/// and the end of the set. | |
| 395 | +fn pgs_display_set() -> Vec<u8> { | |
| 396 | + // Canvas 8x4, a frame-rate byte, composition 0, epoch start, no palette | |
| 397 | + // update, palette 0 -- and one object, id 0, at (1, 1) with no flags. | |
| 398 | + #[rustfmt::skip] | |
| 399 | + let pcs = [ | |
| 400 | + 0, 8, 0, 4, 0x10, 0, 0, 0x80, 0, 0, 1, | |
| 401 | + 0, 0, 0, 0, 0, 1, 0, 1, | |
| 402 | + ]; | |
| 403 | + // Palette 0, version 0, then colour 1: white at full opacity, in the | |
| 404 | + // limited-range YCbCr a disc writes (index, Y, Cr, Cb, A). Colour 0 is the | |
| 405 | + // transparent one every palette starts with. | |
| 406 | + let pds = [0, 0, 1, 235, 128, 128, 255]; | |
| 407 | + // Two rows of "two pixels of colour 1, end of line", which is the shortest | |
| 408 | + // run PGS can write and still be a picture. | |
| 409 | + let rle = [0x00, 0x82, 0x01, 0x00, 0x00, 0x00, 0x82, 0x01, 0x00, 0x00]; | |
| 410 | + // Object 0, version 0, first *and* last fragment of the sequence; then the | |
| 411 | + // length of what follows, and the object's own 2x2 box. | |
| 412 | + let mut ods = vec![0, 0, 0, 0xC0]; | |
| 413 | + ods.extend_from_slice(&[0, 0, (4 + rle.len()) as u8, 0, 2, 0, 2]); | |
| 414 | + ods.extend_from_slice(&rle); | |
| 415 | + | |
| 416 | + let mut set = pgs_segment(0x16, &pcs); | |
| 417 | + set.extend(pgs_segment(0x14, &pds)); | |
| 418 | + set.extend(pgs_segment(0x15, &ods)); | |
| 419 | + set.extend(pgs_segment(0x80, &[])); | |
| 420 | + set | |
| 421 | +} | |
| 422 | + | |
| 423 | +/// The display set that composes nothing: the same canvas with no object on | |
| 424 | +/// it, which is how a disc says the picture before it is over. | |
| 425 | +fn pgs_erase() -> Vec<u8> { | |
| 426 | + let mut set = pgs_segment(0x16, &[0, 8, 0, 4, 0x10, 0, 1, 0, 0, 0, 0]); | |
| 427 | + set.extend(pgs_segment(0x80, &[])); | |
| 428 | + set | |
| 429 | +} |