🔀 merge(subtitles): a PGS cue is a picture the timeline carries
Browse files at this commit8 changed files +545 -64
- Cargo.lock +10 -0
- crates/app/src/main.rs +106 -11
- crates/engine/Cargo.toml +1 -0
- crates/engine/src/demux.rs +16 -8
- crates/engine/src/export.rs +23 -5
- crates/engine/src/subtitle.rs +164 -10
- crates/engine/tests/export_subs.rs +3 -0
- crates/engine/tests/subtitles.rs +222 -30
Cargo.lockmodified+10 -0
| @@ -1647,6 +1647,7 @@ dependencies = [ | |||
| 1647 | "oxideav-core", | 1647 | "oxideav-core", |
| 1648 | "oxideav-h265", | 1648 | "oxideav-h265", |
| 1649 | "oxideav-ogg", | 1649 | "oxideav-ogg", |
| 1650 | + "oxideav-sub-image", | ||
| 1650 | "oxideav-subtitle", | 1651 | "oxideav-subtitle", |
| 1651 | "rav1e", | 1652 | "rav1e", |
| 1652 | "rusty_aac", | 1653 | "rusty_aac", |
| @@ -4038,6 +4039,15 @@ dependencies = [ | |||
| 4038 | "oxideav-ttf", | 4039 | "oxideav-ttf", |
| 4039 | ] | 4040 | ] |
| 4040 | 4041 | ||
| 4042 | +[[package]] | ||
| 4043 | +name = "oxideav-sub-image" | ||
| 4044 | +version = "0.0.7" | ||
| 4045 | +source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 4046 | +checksum = "cb5030e2dce8efa58c20f3e9ce4e53064b7697dc2dcc2585a678d313718582d6" | ||
| 4047 | +dependencies = [ | ||
| 4048 | + "oxideav-core", | ||
| 4049 | +] | ||
| 4050 | + | ||
| 4041 | [[package]] | 4051 | [[package]] |
| 4042 | name = "oxideav-subtitle" | 4052 | name = "oxideav-subtitle" |
| 4043 | version = "0.1.2" | 4053 | version = "0.1.2" |
crates/app/src/main.rsmodified+106 -11
| @@ -938,6 +938,17 @@ struct Player { | |||
| 938 | fps: f64, | 938 | fps: f64, |
| 939 | name: SharedString, | 939 | name: SharedString, |
| 940 | image: Option<Arc<RenderImage>>, | 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 | /// A frame that arrived before its time; shown on the tick it comes due. | 952 | /// A frame that arrived before its time; shown on the tick it comes due. |
| 942 | /// The pump's buffer, not transport state -- but a frame waiting here is | 953 | /// The pump's buffer, not transport state -- but a frame waiting here is |
| 943 | /// what keeps a finished decoder from reading as [`Transport::Ended`] one | 954 | /// what keeps a finished decoder from reading as [`Transport::Ended`] one |
| @@ -3514,6 +3525,8 @@ impl Player { | |||
| 3514 | // emptied library, against one per displayed frame in `pump`; the | 3525 | // emptied library, against one per displayed frame in `pump`; the |
| 3515 | // upgrade path is threading the window through `act_on_row`. | 3526 | // upgrade path is threading the window through `act_on_row`. |
| 3516 | self.image = None; | 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 | self.clipboard = None; | 3530 | self.clipboard = None; |
| 3518 | self.selected = None; | 3531 | self.selected = None; |
| 3519 | self.selected_asset = None; | 3532 | self.selected_asset = None; |
| @@ -4912,7 +4925,7 @@ impl Render for Player { | |||
| 4912 | ) | 4925 | ) |
| 4913 | // After the picture, so the plate is drawn over it | 4926 | // After the picture, so the plate is drawn over it |
| 4914 | // rather than under: siblings paint in order. | 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 | // Above the panel and only when there is one to show, so it costs | 4931 | // Above the panel and only when there is one to show, so it costs |
| @@ -5333,28 +5346,63 @@ impl Player { | |||
| 5333 | /// not the track's own: on a cut timeline an embedded track's cues ride the | 5346 | /// not the track's own: on a cut timeline an embedded track's cues ride the |
| 5334 | /// pictures they belong to, and this is the same map the export writes the | 5347 | /// pictures they belong to, and this is the same map the export writes the |
| 5335 | /// file with -- so what is read here is what the file says. | 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 | if !self.subs_on { | 5366 | if !self.subs_on { |
| 5338 | return None; | 5367 | return None; |
| 5339 | } | 5368 | } |
| 5340 | let mapped = self.session.as_ref()?.timeline_cues(self.sub_track); | 5369 | let mapped = self.session.as_ref()?.timeline_cues(self.sub_track); |
| 5341 | let cues = cues_at(&mapped, at); | 5370 | let cues = cues_at(&mapped, at); |
| 5342 | if cues.is_empty() { | 5371 | if cues.is_empty() { |
| 5372 | + self.drop_sub_image(window); | ||
| 5343 | return None; | 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 | Some( | 5382 | Some( |
| 5346 | div() | 5383 | div() |
| 5347 | .absolute() | 5384 | .absolute() |
| 5348 | .left_0() | 5385 | .left_0() |
| 5349 | .right_0() | 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 | .flex() | 5391 | .flex() |
| 5352 | .flex_col() | 5392 | .flex_col() |
| 5353 | .items_center() | 5393 | .items_center() |
| 5354 | .gap(px(2.)) | 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 | // The plate takes no click: the picture behind it is still the | 5403 | // The plate takes no click: the picture behind it is still the |
| 5356 | // drop target the whole window is. | 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 | div() | 5406 | div() |
| 5359 | .max_w(relative(0.9)) | 5407 | .max_w(relative(0.9)) |
| 5360 | .px(px(6.)) | 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 | /// What is decoding the picture right now, for the transport line: the | 5469 | /// What is decoding the picture right now, for the transport line: the |
| 5380 | /// backend is the running worker's own (it is written where a hardware | 5470 | /// backend is the running worker's own (it is written where a hardware |
| 5381 | /// session falls back to software, so this follows reality), and the codec | 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 | 9330 | +/// What a subtitle row says under its name: how many cues it holds and whether |
| 9241 | -/// track that could not be read -- the engine's own reason, verbatim. A refusal | 9331 | +/// they are pictures ([`engine::subtitle::SubtitleTrack::is_bitmap`]) -- which |
| 9242 | -/// is what the row is *for*: a PGS track off a BluRay remux is pictures, and a | 9332 | +/// is the difference between a track an export writes into the file and one it |
| 9243 | -/// list that dropped it would say the film has no subtitles at all. | 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 | fn subtitle_detail(track: &engine::subtitle::SubtitleTrack) -> String { | 9336 | fn subtitle_detail(track: &engine::subtitle::SubtitleTrack) -> String { |
| 9245 | - match &track.refused { | 9337 | + match (&track.refused, track.is_bitmap()) { |
| 9246 | - Some(why) => why.clone(), | 9338 | + (Some(why), _) => why.clone(), |
| 9247 | - None => format!("{} cues", track.cues.len()), | 9339 | + (None, true) => format!("{} cues — pictures", track.cues.len()), |
| 9340 | + (None, false) => format!("{} cues", track.cues.len()), | ||
| 9248 | } | 9341 | } |
| 9249 | } | 9342 | } |
| 9250 | 9343 | ||
| @@ -15274,6 +15367,7 @@ mod tests { | |||
| 15274 | start_us, | 15367 | start_us, |
| 15275 | end_us, | 15368 | end_us, |
| 15276 | text: text.to_string(), | 15369 | text: text.to_string(), |
| 15370 | + image: None, | ||
| 15277 | }; | 15371 | }; |
| 15278 | // Two cues that hand over exactly, and one that overlaps the second -- | 15372 | // Two cues that hand over exactly, and one that overlaps the second -- |
| 15279 | // a sign over a line of dialogue, which is two plates at one moment. | 15373 | // a sign over a line of dialogue, which is two plates at one moment. |
| @@ -16077,6 +16171,7 @@ fn main() { | |||
| 16077 | fps: meta.map_or(30., |meta| meta.frame_rate), | 16171 | fps: meta.map_or(30., |meta| meta.frame_rate), |
| 16078 | name: name.clone(), | 16172 | name: name.clone(), |
| 16079 | image: None, | 16173 | image: None, |
| 16174 | + sub_image: None, | ||
| 16080 | held: None, | 16175 | held: None, |
| 16081 | ruler: Rc::default(), | 16176 | ruler: Rc::default(), |
| 16082 | // A second is [`PPS_DEFAULT`] pixels wide until someone | 16177 | // A second is [`PPS_DEFAULT`] pixels wide until someone |
crates/engine/Cargo.tomlmodified+1 -0
| @@ -115,6 +115,7 @@ rav1e = { version = "0.8.1", default-features = false, features = ["threading"] | |||
| 115 | # project offers -- an intraframe master, ProRes-shaped, large files and all. | 115 | # project offers -- an intraframe master, ProRes-shaped, large files and all. |
| 116 | # Built from `vendor/oxideav-h265`; see the note in the workspace manifest. | 116 | # Built from `vendor/oxideav-h265`; see the note in the workspace manifest. |
| 117 | oxideav-h265 = { path = "../../vendor/oxideav-h265" } | 117 | oxideav-h265 = { path = "../../vendor/oxideav-h265" } |
| 118 | +oxideav-sub-image = "0.0.7" | ||
| 118 | 119 | ||
| 119 | [dev-dependencies] | 120 | [dev-dependencies] |
| 120 | # The still-image suite compares what the timeline shows against the file's own | 121 | # The still-image suite compares what the timeline shows against the file's own |
crates/engine/src/demux.rsmodified+16 -8
| @@ -1580,9 +1580,10 @@ pub struct MkvSubtitle { | |||
| 1580 | /// `CodecPrivate`: the ASS script header (`[Script Info]` through the | 1580 | /// `CodecPrivate`: the ASS script header (`[Script Info]` through the |
| 1581 | /// `[Events]` `Format:` line) for `S_TEXT/ASS`, empty for `S_TEXT/UTF8`. | 1581 | /// `[Events]` `Format:` line) for `S_TEXT/ASS`, empty for `S_TEXT/UTF8`. |
| 1582 | pub private: Vec<u8>, | 1582 | pub private: Vec<u8>, |
| 1583 | - /// Every cue block, in storage order -- but only for the `S_TEXT/*` codecs. | 1583 | + /// Every cue block, in storage order, for the codecs [`crate::subtitle`] |
| 1584 | - /// A bitmap track (PGS, VobSub) comes back declared and empty: its blocks | 1584 | + /// reads: the `S_TEXT/*` ones and `S_HDMV/PGS`, whose blocks are display |
| 1585 | - /// are megabytes of pictures, and nothing here can draw one. | 1585 | + /// sets of run-length pictures rather than lines. A codec neither of those |
| 1586 | + /// -- VobSub off a DVD -- comes back declared and empty. | ||
| 1586 | pub cues: Vec<MkvCue>, | 1587 | pub cues: Vec<MkvCue>, |
| 1587 | /// Why this track's blocks were not read at all: a `ContentEncodings` this | 1588 | /// Why this track's blocks were not read at all: a `ContentEncodings` this |
| 1588 | /// cannot undo. Listed like a bitmap track rather than raised, so a film | 1589 | /// cannot undo. Listed like a bitmap track rather than raised, so a film |
| @@ -1607,6 +1608,10 @@ pub struct MkvCue { | |||
| 1607 | pub payload: Vec<u8>, | 1608 | pub payload: Vec<u8>, |
| 1608 | } | 1609 | } |
| 1609 | 1610 | ||
| 1611 | +/// The Matroska codec id of Blu-ray bitmap subtitles: run-length pictures in | ||
| 1612 | +/// display sets, read by [`crate::subtitle`] as [`MkvCue`]s like any other. | ||
| 1613 | +pub const PGS: &str = "S_HDMV/PGS"; | ||
| 1614 | + | ||
| 1610 | /// The subtitle tracks of a Matroska file, in file order. An mp4's are not read | 1615 | /// The subtitle tracks of a Matroska file, in file order. An mp4's are not read |
| 1611 | /// (its `tx3g` is a different beast, and no file this project opens carries | 1616 | /// (its `tx3g` is a different beast, and no file this project opens carries |
| 1612 | /// one); anything that is not a Matroska file at all is an error, as it is for | 1617 | /// one); anything that is not a Matroska file at all is an error, as it is for |
| @@ -1646,10 +1651,12 @@ pub fn matroska_subtitles(path: &Path) -> crate::Result<Vec<MkvSubtitle>> { | |||
| 1646 | } | 1651 | } |
| 1647 | at = stop; | 1652 | at = stop; |
| 1648 | } | 1653 | } |
| 1649 | - // The text tracks only: see `MkvSubtitle::cues`. | 1654 | + // The tracks something reads: see `MkvSubtitle::cues`. |
| 1650 | let wanted: Vec<u64> = tracks | 1655 | let wanted: Vec<u64> = tracks |
| 1651 | .iter() | 1656 | .iter() |
| 1652 | - .filter(|t| t.codec.starts_with("S_TEXT") && t.unsupported.is_none()) | 1657 | + .filter(|t| { |
| 1658 | + (t.codec.starts_with("S_TEXT") || t.codec == PGS) && t.unsupported.is_none() | ||
| 1659 | + }) | ||
| 1653 | .map(|t| t.number) | 1660 | .map(|t| t.number) |
| 1654 | .collect(); | 1661 | .collect(); |
| 1655 | if !wanted.is_empty() { | 1662 | if !wanted.is_empty() { |
| @@ -1764,9 +1771,10 @@ fn mkv_subtitle_blocks( | |||
| 1764 | _ => mkv_lace(file, &block)?, | 1771 | _ => mkv_lace(file, &block)?, |
| 1765 | }; | 1772 | }; |
| 1766 | for (at, len) in frames { | 1773 | for (at, len) in frames { |
| 1767 | - // A megabyte of *text* in one cue is a corrupt file, not a | 1774 | + // A megabyte in one cue is a corrupt file, not a subtitle -- a |
| 1768 | - // subtitle, and a crafted length may not reach an allocation | 1775 | + // line of dialogue is bytes and a PGS display set of run-length |
| 1769 | - // through here. | 1776 | + // pictures is tens of kilobytes -- and a crafted length may not |
| 1777 | + // reach an allocation through here. | ||
| 1770 | if len > 1 << 20 { | 1778 | if len > 1 << 20 { |
| 1771 | return Err("a Matroska subtitle block larger than a megabyte".into()); | 1779 | return Err("a Matroska subtitle block larger than a megabyte".into()); |
| 1772 | } | 1780 | } |
crates/engine/src/export.rsmodified+23 -5
| @@ -570,7 +570,10 @@ fn export_subtitles( | |||
| 570 | return None; | 570 | return None; |
| 571 | } | 571 | } |
| 572 | let track = project.subtitles().get(settings.subtitles?)?; | 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 | return None; | 577 | return None; |
| 575 | } | 578 | } |
| 576 | let cues = timeline_cues(project, track, meta.frame_rate); | 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 | start_us, | 636 | start_us, |
| 634 | end_us, | 637 | end_us, |
| 635 | text: cue.text.clone(), | 638 | text: cue.text.clone(), |
| 639 | + image: cue.image.clone(), | ||
| 636 | }) | 640 | }) |
| 637 | }) | 641 | }) |
| 638 | .collect(); | 642 | .collect(); |
| @@ -660,6 +664,7 @@ pub fn timeline_cues(project: &Project, track: &SubtitleTrack, fps: f64) -> Vec< | |||
| 660 | start_us: onto(a), | 664 | start_us: onto(a), |
| 661 | end_us: onto(b).min(end), | 665 | end_us: onto(b).min(end), |
| 662 | text: cue.text.clone(), | 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 | if let Some(why) = &track.refused { | 684 | if let Some(why) = &track.refused { |
| 680 | return format!("{} — {why}", track.label); | 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 | match format { | 693 | match format { |
| 683 | _ if track.cues.is_empty() => "none".into(), | 694 | _ if track.cues.is_empty() => "none".into(), |
| 684 | Format::Av1 | Format::Hevc => format!("{} → embedded", track.label), | 695 | Format::Av1 | Format::Hevc => format!("{} → embedded", track.label), |
| @@ -2321,16 +2332,19 @@ mod tests { | |||
| 2321 | start_us: 500_000, | 2332 | start_us: 500_000, |
| 2322 | end_us: 1_500_000, | 2333 | end_us: 1_500_000, |
| 2323 | text: "first".into(), | 2334 | text: "first".into(), |
| 2335 | + image: None, | ||
| 2324 | }, | 2336 | }, |
| 2325 | Cue { | 2337 | Cue { |
| 2326 | start_us: 2_000_000, | 2338 | start_us: 2_000_000, |
| 2327 | end_us: 3_250_000, | 2339 | end_us: 3_250_000, |
| 2328 | text: "second".into(), | 2340 | text: "second".into(), |
| 2341 | + image: None, | ||
| 2329 | }, | 2342 | }, |
| 2330 | Cue { | 2343 | Cue { |
| 2331 | start_us: 4_000_000, | 2344 | start_us: 4_000_000, |
| 2332 | end_us: 4_750_000, | 2345 | end_us: 4_750_000, |
| 2333 | text: "third".into(), | 2346 | text: "third".into(), |
| 2347 | + image: None, | ||
| 2334 | }, | 2348 | }, |
| 2335 | ], | 2349 | ], |
| 2336 | refused: None, | 2350 | refused: None, |
| @@ -2359,13 +2373,15 @@ mod tests { | |||
| 2359 | Cue { | 2373 | Cue { |
| 2360 | start_us: 500_000, | 2374 | start_us: 500_000, |
| 2361 | end_us: 1_500_000, | 2375 | end_us: 1_500_000, |
| 2362 | - text: "first".into() | 2376 | + text: "first".into(), |
| 2377 | + image: None, | ||
| 2363 | }, | 2378 | }, |
| 2364 | // Clipped to the three seconds the export writes... | 2379 | // Clipped to the three seconds the export writes... |
| 2365 | Cue { | 2380 | Cue { |
| 2366 | start_us: 2_000_000, | 2381 | start_us: 2_000_000, |
| 2367 | end_us: 3_000_000, | 2382 | end_us: 3_000_000, |
| 2368 | - text: "second".into() | 2383 | + text: "second".into(), |
| 2384 | + image: None, | ||
| 2369 | }, | 2385 | }, |
| 2370 | // ...and the one wholly past the end is not written at all. | 2386 | // ...and the one wholly past the end is not written at all. |
| 2371 | ] | 2387 | ] |
| @@ -2393,13 +2409,15 @@ mod tests { | |||
| 2393 | Cue { | 2409 | Cue { |
| 2394 | start_us: 500_000, | 2410 | start_us: 500_000, |
| 2395 | end_us: 1_250_000, | 2411 | end_us: 1_250_000, |
| 2396 | - text: "second".into() | 2412 | + text: "second".into(), |
| 2413 | + image: None, | ||
| 2397 | }, | 2414 | }, |
| 2398 | // ...and the third simply moves back the two seconds that went. | 2415 | // ...and the third simply moves back the two seconds that went. |
| 2399 | Cue { | 2416 | Cue { |
| 2400 | start_us: 2_000_000, | 2417 | start_us: 2_000_000, |
| 2401 | end_us: 2_750_000, | 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+164 -10
| @@ -14,12 +14,20 @@ | |||
| 14 | //! renderer is a later thing, and a cue nobody can draw yet is still a cue that | 14 | //! renderer is a later thing, and a cue nobody can draw yet is still a cue that |
| 15 | //! has to be listed, timed and saved. | 15 | //! has to be listed, timed and saved. |
| 16 | //! | 16 | //! |
| 17 | -//! What is *refused* is refused by name rather than by omission | 17 | +//! A cue is not always words. `S_HDMV/PGS` off a BluRay remux is *pictures* -- |
| 18 | -//! ([`SubtitleTrack::refused`]): the bitmap subtitle formats -- PGS off a | 18 | +//! run-length bitmaps the disc composed against its own frame -- and one of |
| 19 | -//! BluRay remux, VobSub off a DVD -- are pictures, and a film carrying four of | 19 | +//! those is a [`Cue`] with a [`CueImage`] on it and no text. The decoding is |
| 20 | -//! them opens with four rows that say so instead of opening with an empty list. | 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 | use std::path::{Path, PathBuf}; | 29 | use std::path::{Path, PathBuf}; |
| 30 | +use std::sync::Arc; | ||
| 23 | 31 | ||
| 24 | use oxideav_subtitle::ir::plain_text; | 32 | use oxideav_subtitle::ir::plain_text; |
| 25 | 33 | ||
| @@ -37,6 +45,84 @@ pub struct Cue { | |||
| 37 | /// is to keep `oxideav_core::Segment` beside this string -- the parsers | 45 | /// is to keep `oxideav_core::Segment` beside this string -- the parsers |
| 38 | /// already hand it over -- and it belongs to whoever writes the renderer. | 46 | /// already hand it over -- and it belongs to whoever writes the renderer. |
| 39 | pub text: String, | 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 | /// A subtitle track: where it came from and what it says. | 128 | /// A subtitle track: where it came from and what it says. |
| @@ -73,6 +159,17 @@ impl SubtitleTrack { | |||
| 73 | refused: Some(why), | 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 | /// The one door a saved subtitle line comes back in through: a standalone file | 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 | pub fn of_matroska(path: &Path) -> crate::Result<Vec<SubtitleTrack>> { | 223 | pub fn of_matroska(path: &Path) -> crate::Result<Vec<SubtitleTrack>> { |
| 127 | Ok(crate::demux::matroska_subtitles(path)? | 224 | Ok(crate::demux::matroska_subtitles(path)? |
| 128 | .into_iter() | 225 | .into_iter() |
| 129 | - .map(|t| { | 226 | + .map(|mut t| { |
| 130 | let label = match (t.name.is_empty(), t.language.as_str()) { | 227 | let label = match (t.name.is_empty(), t.language.as_str()) { |
| 131 | (true, lang) => lang.to_owned(), | 228 | (true, lang) => lang.to_owned(), |
| 132 | (false, "und") => t.name.clone(), | 229 | (false, "und") => t.name.clone(), |
| 133 | (false, lang) => format!("{lang} — {}", t.name), | 230 | (false, lang) => format!("{lang} — {}", t.name), |
| 134 | }; | 231 | }; |
| 135 | - match cues_of(&t) { | 232 | + match cues_of(&mut t) { |
| 136 | Ok(cues) => SubtitleTrack { | 233 | Ok(cues) => SubtitleTrack { |
| 137 | path: path.to_path_buf(), | 234 | path: path.to_path_buf(), |
| 138 | track: Some(t.number), | 235 | track: Some(t.number), |
| @@ -156,9 +253,13 @@ pub fn of_matroska(path: &Path) -> crate::Result<Vec<SubtitleTrack>> { | |||
| 156 | /// reads -- rather than the markup being re-derived here -- which is also what | 253 | /// reads -- rather than the markup being re-derived here -- which is also what |
| 157 | /// makes an embedded track and the standalone file it was muxed from come back | 254 | /// makes an embedded track and the standalone file it was muxed from come back |
| 158 | /// as the same cues. | 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 | // A track compressed or encrypted with something the demuxer cannot undo is | 261 | // A track compressed or encrypted with something the demuxer cannot undo is |
| 161 | - // refused in those words, like a bitmap one: the row stays, and it says why. | 262 | + // refused in those words: the row stays, and it says why. |
| 162 | if let Some(why) = &track.unsupported { | 263 | if let Some(why) = &track.unsupported { |
| 163 | return Err(why.clone()); | 264 | return Err(why.clone()); |
| 164 | } | 265 | } |
| @@ -170,9 +271,11 @@ fn cues_of(track: &crate::demux::MkvSubtitle) -> Result<Vec<Cue>, String> { | |||
| 170 | start_us: c.start_us, | 271 | start_us: c.start_us, |
| 171 | end_us: end_of(track, c), | 272 | end_us: end_of(track, c), |
| 172 | text: srt_body(&String::from_utf8_lossy(&c.payload)), | 273 | text: srt_body(&String::from_utf8_lossy(&c.payload)), |
| 274 | + image: None, | ||
| 173 | }) | 275 | }) |
| 174 | .collect(), | 276 | .collect(), |
| 175 | "S_TEXT/ASS" | "S_TEXT/SSA" => ass_cues(track)?, | 277 | "S_TEXT/ASS" | "S_TEXT/SSA" => ass_cues(track)?, |
| 278 | + crate::demux::PGS => pgs_cues(track), | ||
| 176 | codec => { | 279 | codec => { |
| 177 | return Err(format!( | 280 | return Err(format!( |
| 178 | "{codec} subtitles are pictures, not text — this track is listed, not read" | 281 | "{codec} subtitles are pictures, not text — this track is listed, not read" |
| @@ -182,13 +285,62 @@ fn cues_of(track: &crate::demux::MkvSubtitle) -> Result<Vec<Cue>, String> { | |||
| 182 | Ok(cues) | 285 | Ok(cues) |
| 183 | } | 286 | } |
| 184 | 287 | ||
| 288 | +/// The cues of an `S_HDMV/PGS` track: one display set per block, and a display | ||
| 289 | +/// set is a picture the disc composes onto its own canvas. | ||
| 290 | +/// | ||
| 291 | +/// Only the blocks that compose *something* are cues. The other half of them | ||
| 292 | +/// are the disc's "take it off again" -- a composition with no object on it -- | ||
| 293 | +/// and they are what says when the picture before them goes away, which is why | ||
| 294 | +/// they are walked and not skipped. A muxer writes `BlockDuration` zero for all | ||
| 295 | +/// of them, so [`end_of`] is what pairs the two. | ||
| 296 | +fn pgs_cues(track: &mut crate::demux::MkvSubtitle) -> Vec<Cue> { | ||
| 297 | + // Before the blocks are emptied: `end_of` reads the ones after this one. | ||
| 298 | + let ends: Vec<i64> = track.cues.iter().map(|c| end_of(track, c)).collect(); | ||
| 299 | + track | ||
| 300 | + .cues | ||
| 301 | + .iter_mut() | ||
| 302 | + .zip(ends) | ||
| 303 | + .filter_map(|(block, end_us)| { | ||
| 304 | + let (width, height) = pgs_canvas(&block.payload)?; | ||
| 305 | + Some(Cue { | ||
| 306 | + start_us: block.start_us, | ||
| 307 | + end_us, | ||
| 308 | + text: String::new(), | ||
| 309 | + image: Some(Arc::new(CueImage { | ||
| 310 | + width, | ||
| 311 | + height, | ||
| 312 | + set: std::mem::take(&mut block.payload), | ||
| 313 | + })), | ||
| 314 | + }) | ||
| 315 | + }) | ||
| 316 | + .collect() | ||
| 317 | +} | ||
| 318 | + | ||
| 319 | +/// The canvas of a display set that paints something -- its `PCS` says how big | ||
| 320 | +/// the disc's picture is, and how many objects are composed onto it. | ||
| 321 | +/// | ||
| 322 | +/// `None` when nothing is composed, which is the disc clearing the screen, and | ||
| 323 | +/// `None` for bytes that are not a display set at all. | ||
| 324 | +fn pgs_canvas(set: &[u8]) -> Option<(u32, u32)> { | ||
| 325 | + /// `PresentationCompositionSegment`, which every display set opens with. | ||
| 326 | + const PCS: u8 = 0x16; | ||
| 327 | + let (_, pcs) = segments(set)?.into_iter().find(|&(kind, _)| kind == PCS)?; | ||
| 328 | + // Width and height, then the frame rate, the composition number and state, | ||
| 329 | + // the palette-update flag and the palette id -- and then how many objects | ||
| 330 | + // this composition puts on the canvas. | ||
| 331 | + let width = u16::from_be_bytes([*pcs.first()?, *pcs.get(1)?]); | ||
| 332 | + let height = u16::from_be_bytes([*pcs.get(2)?, *pcs.get(3)?]); | ||
| 333 | + (*pcs.get(10)? > 0).then_some((u32::from(width), u32::from(height))) | ||
| 334 | +} | ||
| 335 | + | ||
| 185 | /// When a cue goes away: its `BlockDuration`, or -- for the muxer that writes | 336 | /// When a cue goes away: its `BlockDuration`, or -- for the muxer that writes |
| 186 | -/// none -- when the next one arrives, and [`NO_DURATION_US`] for the last. | 337 | +/// none, and for the PGS block that writes a zero, which says the same thing -- |
| 338 | +/// when the next one arrives, and [`NO_DURATION_US`] for the last. | ||
| 187 | fn end_of(track: &crate::demux::MkvSubtitle, cue: &crate::demux::MkvCue) -> i64 { | 339 | fn end_of(track: &crate::demux::MkvSubtitle, cue: &crate::demux::MkvCue) -> i64 { |
| 188 | /// How long a cue with nothing to say about it stays up: two seconds, a | 340 | /// How long a cue with nothing to say about it stays up: two seconds, a |
| 189 | /// read of one line. | 341 | /// read of one line. |
| 190 | const NO_DURATION_US: i64 = 2_000_000; | 342 | const NO_DURATION_US: i64 = 2_000_000; |
| 191 | - match cue.duration_us { | 343 | + match cue.duration_us.filter(|&d| d > 0) { |
| 192 | Some(d) => cue.start_us + d, | 344 | Some(d) => cue.start_us + d, |
| 193 | None => track | 345 | None => track |
| 194 | .cues | 346 | .cues |
| @@ -264,6 +416,7 @@ fn ass_cues(track: &crate::demux::MkvSubtitle) -> Result<Vec<Cue>, String> { | |||
| 264 | c.end_us | 416 | c.end_us |
| 265 | }, | 417 | }, |
| 266 | text: plain_text(&c.segments), | 418 | text: plain_text(&c.segments), |
| 419 | + image: None, | ||
| 267 | }) | 420 | }) |
| 268 | .collect()) | 421 | .collect()) |
| 269 | } | 422 | } |
| @@ -310,6 +463,7 @@ fn parse_file(path: &Path) -> crate::Result<Vec<Cue>> { | |||
| 310 | start_us: c.start_us, | 463 | start_us: c.start_us, |
| 311 | end_us: c.end_us, | 464 | end_us: c.end_us, |
| 312 | text: plain_text(&c.segments), | 465 | text: plain_text(&c.segments), |
| 466 | + image: None, | ||
| 313 | }) | 467 | }) |
| 314 | .collect()) | 468 | .collect()) |
| 315 | } | 469 | } |
crates/engine/tests/export_subs.rsmodified+3 -0
| @@ -84,16 +84,19 @@ fn track() -> SubtitleTrack { | |||
| 84 | start_us: 500_000, | 84 | start_us: 500_000, |
| 85 | end_us: 1_500_000, | 85 | end_us: 1_500_000, |
| 86 | text: "first line".into(), | 86 | text: "first line".into(), |
| 87 | + image: None, | ||
| 87 | }, | 88 | }, |
| 88 | Cue { | 89 | Cue { |
| 89 | start_us: 2_000_000, | 90 | start_us: 2_000_000, |
| 90 | end_us: 3_250_000, | 91 | end_us: 3_250_000, |
| 91 | text: "second line\nwith a break".into(), | 92 | text: "second line\nwith a break".into(), |
| 93 | + image: None, | ||
| 92 | }, | 94 | }, |
| 93 | Cue { | 95 | Cue { |
| 94 | start_us: 4_000_000, | 96 | start_us: 4_000_000, |
| 95 | end_us: 4_750_000, | 97 | end_us: 4_750_000, |
| 96 | text: "third line".into(), | 98 | text: "third line".into(), |
| 99 | + image: None, | ||
| 97 | }, | 100 | }, |
| 98 | ], | 101 | ], |
| 99 | refused: None, | 102 | refused: None, |
crates/engine/tests/subtitles.rsmodified+222 -30
| @@ -1,7 +1,7 @@ | |||
| 1 | //! Subtitles: the cues of a file beside the media and of a track inside it are | 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 | 2 | +//! the same cues, a project keeps them across a save, and a track that is |
| 3 | -//! pictures rather than text are listed and skipped by name rather than | 3 | +//! pictures rather than text is read as pictures rather than listed and |
| 4 | -//! silently dropped. | 4 | +//! skipped. |
| 5 | //! | 5 | //! |
| 6 | //! ```text | 6 | //! ```text |
| 7 | //! cargo test -p engine --release --test subtitles -- --nocapture | 7 | //! cargo test -p engine --release --test subtitles -- --nocapture |
| @@ -39,16 +39,19 @@ fn expected() -> Vec<Cue> { | |||
| 39 | start_us: 500_000, | 39 | start_us: 500_000, |
| 40 | end_us: 1_500_000, | 40 | end_us: 1_500_000, |
| 41 | text: "first line".into(), | 41 | text: "first line".into(), |
| 42 | + image: None, | ||
| 42 | }, | 43 | }, |
| 43 | Cue { | 44 | Cue { |
| 44 | start_us: 2_000_000, | 45 | start_us: 2_000_000, |
| 45 | end_us: 3_250_000, | 46 | end_us: 3_250_000, |
| 46 | text: "second line\nwith a break".into(), | 47 | text: "second line\nwith a break".into(), |
| 48 | + image: None, | ||
| 47 | }, | 49 | }, |
| 48 | Cue { | 50 | Cue { |
| 49 | start_us: 4_000_000, | 51 | start_us: 4_000_000, |
| 50 | end_us: 4_750_000, | 52 | end_us: 4_750_000, |
| 51 | text: "third line".into(), | 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 | 106 | +/// The whole of the bitmap half on a file small enough to read in one screen: |
| 104 | -/// that takes the whole file down with it. The repro is a BluRay remux | 107 | +/// four PGS tracks, the first of them carrying one picture and the disc's own |
| 105 | -/// carrying four PGS tracks: opening it lists four subtitles it cannot draw, | 108 | +/// "take it off again" after it. A row per track, cues on the one with blocks, |
| 106 | -/// and the picture and the sound come up regardless. | 109 | +/// and the picture decodes to the pixels the display set describes. |
| 107 | /// | 110 | /// |
| 108 | /// Written here rather than muxed by the fixture script because ffmpeg cannot | 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` | 112 | +/// encode text subtitles into pictures; the bytes below are that Matroska file. |
| 110 | -/// element of such a file and nothing else. | ||
| 111 | #[test] | 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 | let dir = scratch("pgs"); | 115 | let dir = scratch("pgs"); |
| 114 | let file = dir.join("remux.mkv"); | 116 | let file = dir.join("remux.mkv"); |
| 115 | std::fs::write(&file, pgs_matroska()).expect("write the hand-made mkv"); | 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 | assert_eq!(tracks.len(), 4, "every PGS track is listed"); | 120 | assert_eq!(tracks.len(), 4, "every PGS track is listed"); |
| 119 | for (i, track) in tracks.iter().enumerate() { | 121 | for (i, track) in tracks.iter().enumerate() { |
| 120 | assert_eq!(track.track, Some(i as u64 + 1)); | 122 | assert_eq!(track.track, Some(i as u64 + 1)); |
| 121 | - assert_eq!(track.cues, Vec::new(), "no cues to have"); | 123 | + assert_eq!(track.refused, None, "a PGS track is read, not refused"); |
| 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 | - ); | ||
| 127 | } | 124 | } |
| 128 | // The language is what a list shows, and `und` is what the spec says a | 125 | // The language is what a list shows, and `und` is what the spec says a |
| 129 | // track that names none is. | 126 | // track that names none is. |
| 130 | assert_eq!(tracks[0].label, "eng"); | 127 | assert_eq!(tracks[0].label, "eng"); |
| 131 | assert_eq!(tracks[3].label, "und"); | 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 | /// A saved project comes back with its subtitles: the `.edith` names the file | 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 | /// A refusal a front-end shows at the moment of the import, rather than a row | 320 | /// A refusal a front-end shows at the moment of the import, rather than a row |
| 191 | /// nobody asked for: a file that is not a subtitle format at all. | 321 | /// nobody asked for: a file that is not a subtitle format at all. |
| 192 | #[test] | 322 | #[test] |
| @@ -207,20 +337,21 @@ fn importing_something_that_is_not_a_subtitle_says_so() { | |||
| 207 | assert_eq!(session.subtitles().len(), 0, "and nothing was added"); | 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 | 340 | +/// An EBML element: its id as it is written in the file, then an 8-byte |
| 211 | -/// and nothing else -- enough for the walk, which stops at the first `Cluster` | 341 | +/// length, which is always a legal encoding of one. |
| 212 | -/// and there is none. | 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 | fn pgs_matroska() -> Vec<u8> { | 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 | let mut tracks = Vec::new(); | 355 | let mut tracks = Vec::new(); |
| 225 | for (number, language) in [(1u8, "eng"), (2, "fra"), (3, "spa"), (4, "")] { | 356 | for (number, language) in [(1u8, "eng"), (2, "fra"), (3, "spa"), (4, "")] { |
| 226 | let mut entry = Vec::new(); | 357 | let mut entry = Vec::new(); |
| @@ -232,6 +363,67 @@ fn pgs_matroska() -> Vec<u8> { | |||
| 232 | } | 363 | } |
| 233 | tracks.extend(element(&[0xAE], &entry)); // TrackEntry | 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 | element(&[0x18, 0x53, 0x80, 0x67], &segment) // Segment | 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 | +} | ||