Merge branch 'worktree-agent-aabed80e6585331a7'
Browse files at this commit4 changed files +496 -10
- crates/engine/src/export.rs +172 -4
- crates/engine/src/project.rs +19 -0
- crates/engine/tests/export_subs.rs +276 -6
- crates/engine/tests/pgs_bitmaps.rs +29 -0
crates/engine/src/export.rsmodified+172 -4
| @@ -78,7 +78,7 @@ use crate::hw::{DmaFrame, DmaWant, HwEncoder, HwPicture, HwSession}; | ||
| 78 | 78 | use crate::mux::{ |
| 79 | 79 | AudioParams, Av1Params, CopyParams, HevcParams, MkvMuxer, Mp4Muxer, SubParams, VideoParams, |
| 80 | 80 | }; |
| 81 | -use crate::project::{LaneKind, Project, Rate}; | |
| 81 | +use crate::project::{Lane, LaneKind, Project, Rate}; | |
| 82 | 82 | use crate::scale::Composer; |
| 83 | 83 | use crate::subtitle::{Cue, SubtitleTrack}; |
| 84 | 84 | use crate::tonemap::{self, ToneMapper}; |
| @@ -813,6 +813,21 @@ fn export_subtitles( | ||
| 813 | 813 | if !settings.format.has_video() { |
| 814 | 814 | return Vec::new(); |
| 815 | 815 | } |
| 816 | + // A timeline with words *placed* on it writes those and nothing else: the | |
| 817 | + // lanes are the edit, and a pick list built from the palette would write the | |
| 818 | + // same track twice over. | |
| 819 | + // | |
| 820 | + // corner-cut, transitional: a project with no placement anywhere keeps the | |
| 821 | + // old behaviour, every picked track carried through the picture's spans, | |
| 822 | + // because nothing in the app can place one yet -- so every project that | |
| 823 | + // exists today takes that door and exports the file it always did. Ceiling: | |
| 824 | + // two maps for one question. Upgrade path: once the timeline places | |
| 825 | + // subtitles (T5/T6) and the format carries them, an import puts its tracks | |
| 826 | + // on lanes and this whole branch, `timeline_cues` and | |
| 827 | + // [`ExportSettings::subtitles`] go with it. | |
| 828 | + if !placed_lanes(project).is_empty() { | |
| 829 | + return lane_subtitles(project, meta.frame_rate); | |
| 830 | + } | |
| 816 | 831 | settings |
| 817 | 832 | .subtitles |
| 818 | 833 | .iter() |
| @@ -839,6 +854,76 @@ fn export_subtitles( | ||
| 839 | 854 | .collect() |
| 840 | 855 | } |
| 841 | 856 | |
| 857 | +/// The project's subtitle lanes in display order, and empty where **nothing is | |
| 858 | +/// placed on any of them** -- an empty list is what the export and the card both | |
| 859 | +/// read as "this project does not use lanes yet", so a project holding a bare | |
| 860 | +/// `S1` nobody dropped a caption on still exports by the pick list. | |
| 861 | +fn placed_lanes(project: &Project) -> Vec<Lane> { | |
| 862 | + let lanes = project.subtitle_lanes(); | |
| 863 | + match lanes.iter().any(|&l| !project.sub_lane(l).is_empty()) { | |
| 864 | + true => lanes, | |
| 865 | + false => Vec::new(), | |
| 866 | + } | |
| 867 | +} | |
| 868 | + | |
| 869 | +/// One track per subtitle *lane*, in lane order -- so the file declares them | |
| 870 | +/// top-down as the timeline shows them and a player's subtitle menu lists them | |
| 871 | +/// in that order. | |
| 872 | +/// | |
| 873 | +/// The cues are [`Project::sub_lane_cues`]: each placement's window of its | |
| 874 | +/// palette track, on the timeline's own clock. Clipped again here to what the | |
| 875 | +/// export *writes*, which ends with the last picture or sound | |
| 876 | +/// ([`Project::media_frames`]) -- a caption hanging past both holds the | |
| 877 | +/// timeline open ([`Project::timeline_frames`]) but there is no picture under | |
| 878 | +/// it to write, so a cue out there is dropped and one straddling the end keeps | |
| 879 | +/// the half that is inside. [`planned_subtitles`] says so before the button is | |
| 880 | +/// pressed. | |
| 881 | +/// | |
| 882 | +/// A lane is dropped whole -- no track at all in the file -- where any | |
| 883 | +/// placement on it is a picture ([`SubtitleTrack::is_bitmap`]), a track that | |
| 884 | +/// could not be read, or a palette row that has since gone: the muxers write | |
| 885 | +/// words, and half a lane's cues under the other half's name would be worse | |
| 886 | +/// than none. Its language and title are the lane's *first* placement's track, | |
| 887 | +/// which is the one thing the whole lane is named by. | |
| 888 | +fn lane_subtitles(project: &Project, fps: f64) -> Vec<SubParams> { | |
| 889 | + let end = (f64::from(project.media_frames()) / fps * 1e6).round() as i64; | |
| 890 | + placed_lanes(project) | |
| 891 | + .into_iter() | |
| 892 | + .filter_map(|lane| { | |
| 893 | + let placed = project.sub_lane(lane); | |
| 894 | + let first = placed.first()?; | |
| 895 | + let writable = |track: usize| { | |
| 896 | + project | |
| 897 | + .subtitles() | |
| 898 | + .get(track) | |
| 899 | + .filter(|t| t.refused.is_none() && !t.is_bitmap()) | |
| 900 | + }; | |
| 901 | + let named = writable(first.track)?; | |
| 902 | + if placed.iter().any(|s| writable(s.track).is_none()) { | |
| 903 | + return None; | |
| 904 | + } | |
| 905 | + let cues: Vec<Cue> = project | |
| 906 | + .sub_lane_cues(lane, fps) | |
| 907 | + .into_iter() | |
| 908 | + .filter_map(|cue| { | |
| 909 | + let (start_us, end_us) = (cue.start_us.max(0), cue.end_us.min(end)); | |
| 910 | + (end_us > start_us).then(|| Cue { | |
| 911 | + start_us, | |
| 912 | + end_us, | |
| 913 | + text: cue.text, | |
| 914 | + image: cue.image, | |
| 915 | + }) | |
| 916 | + }) | |
| 917 | + .collect(); | |
| 918 | + (!cues.is_empty()).then(|| SubParams { | |
| 919 | + language: named.language.clone(), | |
| 920 | + name: named.name.clone(), | |
| 921 | + cues, | |
| 922 | + }) | |
| 923 | + }) | |
| 924 | + .collect() | |
| 925 | +} | |
| 926 | + | |
| 842 | 927 | /// The track's cues where the *exported* timeline puts them, which is the only |
| 843 | 928 | /// clock the file has: the export writes timeline frame 0 onwards and nothing |
| 844 | 929 | /// else, exactly as the sound is mixed from frame 0 and resized to the |
| @@ -945,6 +1030,13 @@ pub fn planned_subtitles( | ||
| 945 | 1030 | let mut embedded: Vec<&str> = Vec::new(); |
| 946 | 1031 | let mut dropped: Vec<String> = Vec::new(); |
| 947 | 1032 | let picks: Vec<usize> = picks.into_iter().collect(); |
| 1033 | + // The lanes are the answer wherever anything is placed on one, exactly as | |
| 1034 | + // [`export_subtitles`] writes them then -- the picks a front-end still hands | |
| 1035 | + // are the palette's rows and say nothing about what the timeline holds. | |
| 1036 | + let lanes = placed_lanes(project); | |
| 1037 | + if !lanes.is_empty() { | |
| 1038 | + return planned_lanes(project, format, &lanes); | |
| 1039 | + } | |
| 948 | 1040 | // One statement about the *file*, said before any about a track: a format |
| 949 | 1041 | // that is the sound alone has nowhere to put a single one of them, and the |
| 950 | 1042 | // reason a pick would not have travelled anyway (a picture track, no cues) |
| @@ -993,6 +1085,75 @@ pub fn planned_subtitles( | ||
| 993 | 1085 | } |
| 994 | 1086 | } |
| 995 | 1087 | |
| 1088 | +/// [`planned_subtitles`] for a timeline that *places* its words: one clause per | |
| 1089 | +/// subtitle lane, named by the lane and by the palette row it shows, in the | |
| 1090 | +/// order the file will declare them ([`lane_subtitles`]). | |
| 1091 | +/// | |
| 1092 | +/// Every reason a lane does not travel is said here rather than found in the | |
| 1093 | +/// file: a picture track placed on it, a track that could not be read, a palette | |
| 1094 | +/// row that has since gone, a lane sitting entirely past the last picture, and | |
| 1095 | +/// -- before a byte is written -- more lanes than a Matroska block can number | |
| 1096 | +/// ([`crate::mux::MAX_SUB_TRACKS`], which the muxer refuses again). | |
| 1097 | +fn planned_lanes(project: &Project, format: Format, lanes: &[Lane]) -> String { | |
| 1098 | + if !format.has_video() { | |
| 1099 | + return "none — this format is the sound alone".into(); | |
| 1100 | + } | |
| 1101 | + let end = project.media_frames(); | |
| 1102 | + let (mut embedded, mut dropped, mut trailing) = (Vec::new(), Vec::new(), false); | |
| 1103 | + for &lane in lanes { | |
| 1104 | + let placed = project.sub_lane(lane); | |
| 1105 | + // An empty lane is a lane nobody has used yet, not a refusal. | |
| 1106 | + let Some(first) = placed.first() else { | |
| 1107 | + continue; | |
| 1108 | + }; | |
| 1109 | + let row = |s: &crate::project::SubClip| project.subtitles().get(s.track); | |
| 1110 | + let (label, head) = (lane.label(), row(first)); | |
| 1111 | + let name = head.map_or_else(String::new, |t| format!("{} ", t.label)); | |
| 1112 | + if let Some(gone) = placed.iter().find(|s| row(s).is_none()) { | |
| 1113 | + dropped.push(format!("{label} — #{} is no longer a track", gone.track)); | |
| 1114 | + } else if let Some((t, why)) = placed | |
| 1115 | + .iter() | |
| 1116 | + .find_map(|s| row(s).and_then(|t| t.refused.as_ref().map(|why| (t, why)))) | |
| 1117 | + { | |
| 1118 | + dropped.push(format!("{label} {} — {why}", t.label)); | |
| 1119 | + } else if let Some(t) = placed.iter().find_map(|s| row(s).filter(|t| t.is_bitmap())) { | |
| 1120 | + // Drawn over the picture, written into no file -- the same sentence | |
| 1121 | + // a picked track gets, because it is the track and not the lane that | |
| 1122 | + // cannot be carried. | |
| 1123 | + dropped.push(format!("{label} {} — pictures; drawn, not written", t.label)); | |
| 1124 | + } else if first.start >= end { | |
| 1125 | + dropped.push(format!("{label} {name}— past the last picture")); | |
| 1126 | + } else { | |
| 1127 | + embedded.push(format!("{label} {}", name.trim_end())); | |
| 1128 | + // Something on it hangs past the end of the media: the timeline is | |
| 1129 | + // held open under it ([`Project::timeline_frames`]) but the file | |
| 1130 | + // stops with the last picture, so those cues are cut off there. | |
| 1131 | + trailing |= placed.last().is_some_and(|s| s.end() > end); | |
| 1132 | + } | |
| 1133 | + } | |
| 1134 | + if format.is_mkv() && embedded.len() > crate::mux::MAX_SUB_TRACKS { | |
| 1135 | + return format!( | |
| 1136 | + "{} subtitle lanes: a Matroska block writes its track number in one byte, so one file \ | |
| 1137 | + carries at most {}", | |
| 1138 | + embedded.len(), | |
| 1139 | + crate::mux::MAX_SUB_TRACKS | |
| 1140 | + ); | |
| 1141 | + } | |
| 1142 | + let mut parts = match embedded.len() { | |
| 1143 | + 0 => Vec::new(), | |
| 1144 | + 1 => vec![format!("{} → embedded", embedded[0])], | |
| 1145 | + n => vec![format!("{n} lanes → embedded ({})", embedded.join(", "))], | |
| 1146 | + }; | |
| 1147 | + parts.extend(dropped); | |
| 1148 | + if trailing { | |
| 1149 | + parts.push("cues past the last picture are cut off there".into()); | |
| 1150 | + } | |
| 1151 | + match parts.is_empty() { | |
| 1152 | + true => "none".into(), | |
| 1153 | + false => parts.join("; "), | |
| 1154 | + } | |
| 1155 | +} | |
| 1156 | + | |
| 996 | 1157 | /// What [`start`] would encode the picture with, probed the way the export |
| 997 | 1158 | /// probes it -- the very encoder is opened and closed again -- so this is a |
| 998 | 1159 | /// measurement and not a promise. `None` for a format that carries no picture. |
| @@ -1462,7 +1623,7 @@ fn encode_audio( | ||
| 1462 | 1623 | // whole samples on its own, so the sum can miss by a sample or two and a |
| 1463 | 1624 | // source that ran out early would leave the track short under a picture |
| 1464 | 1625 | // that is not. One resize settles both, as [`run_audio`]'s does. |
| 1465 | - let total = (f64::from(project.timeline_frames()) / meta.frame_rate | |
| 1626 | + let total = (f64::from(project.media_frames()) / meta.frame_rate | |
| 1466 | 1627 | * f64::from(audio.sample_rate)) |
| 1467 | 1628 | .round() as usize |
| 1468 | 1629 | * channels; |
| @@ -1866,7 +2027,14 @@ fn run( | ||
| 1866 | 2027 | shared: &Arc<Shared>, |
| 1867 | 2028 | settings: &ExportSettings, |
| 1868 | 2029 | ) -> crate::Result<()> { |
| 1869 | - let total = project.timeline_frames(); | |
| 2030 | + // The media's length and not the timeline's: a caption placed past the last | |
| 2031 | + // picture holds the *timeline* open ([`Project::timeline_frames`]) and there | |
| 2032 | + // is nothing to write under it -- the picture loop walks the composite spans | |
| 2033 | + // and stops with them, so a file padded to the caption's end would be | |
| 2034 | + // trailing silence over a frozen last frame. The export ends with the last | |
| 2035 | + // picture or sound, the cues are cut off there ([`lane_subtitles`]), and | |
| 2036 | + // [`planned_subtitles`] says so before the button is pressed. | |
| 2037 | + let total = project.media_frames(); | |
| 1870 | 2038 | // The two preview switches are the H.264 mp4 pair's alone. Only that |
| 1871 | 2039 | // container carries a colour of its own back out ([`write_video`] tags it |
| 1872 | 2040 | // there), so asking any other for a source-space file would write pictures |
| @@ -2496,7 +2664,7 @@ fn run_audio( | ||
| 2496 | 2664 | // by the line above, so this names the encoder writing it. |
| 2497 | 2665 | *shared.encoders.lock().unwrap() = Some(audio_label(project, format, true, None).to_string()); |
| 2498 | 2666 | let channels = usize::from(audio.channels); |
| 2499 | - let frames = (f64::from(project.timeline_frames()) / meta.frame_rate | |
| 2667 | + let frames = (f64::from(project.media_frames()) / meta.frame_rate | |
| 2500 | 2668 | * f64::from(audio.sample_rate)) |
| 2501 | 2669 | .round() as u64; |
| 2502 | 2670 | let total = frames as usize * channels; |
crates/engine/src/project.rsmodified+19 -0
| @@ -2255,6 +2255,25 @@ impl Project { | ||
| 2255 | 2255 | .unwrap_or(0) |
| 2256 | 2256 | } |
| 2257 | 2257 | |
| 2258 | + /// The same, counting the *media* alone: where the last picture or sound | |
| 2259 | + /// runs out, with the captions over them ignored. | |
| 2260 | + /// | |
| 2261 | + /// What an export actually writes ends here -- the picture loop walks the | |
| 2262 | + /// composite spans and the file stops with them -- so this is the length a | |
| 2263 | + /// cue has to be inside of to be written at all (what | |
| 2264 | + /// [`crate::export::planned_subtitles`] clips a lane's cues to). Equal to | |
| 2265 | + /// [`timeline_frames`](Self::timeline_frames) on every timeline whose last | |
| 2266 | + /// thing is a picture or a sound, which is every timeline that has no | |
| 2267 | + /// caption hanging past the end of both. | |
| 2268 | + pub fn media_frames(&self) -> u32 { | |
| 2269 | + self.lanes | |
| 2270 | + .iter() | |
| 2271 | + .filter(|l| l.kind != LaneKind::Subtitle) | |
| 2272 | + .filter_map(|l| l.clips.last().map(Clip::end)) | |
| 2273 | + .max() | |
| 2274 | + .unwrap_or(0) | |
| 2275 | + } | |
| 2276 | + | |
| 2258 | 2277 | /// `(timeline_start, len)` per `V1` clip, in order -- what a UI lane needs |
| 2259 | 2278 | /// to lay clips out. Gaps show up as the holes between consecutive entries. |
| 2260 | 2279 | pub fn clip_spans(&self) -> Vec<(u32, u32)> { |
crates/engine/tests/export_subs.rsmodified+276 -6
| @@ -19,7 +19,7 @@ use std::sync::Once; | ||
| 19 | 19 | use std::time::{Duration, Instant}; |
| 20 | 20 | |
| 21 | 21 | use engine::export::{ExportSettings, Format}; |
| 22 | -use engine::project::Lane; | |
| 22 | +use engine::project::{Lane, LaneKind, SubClip}; | |
| 23 | 23 | use engine::scratch::Scratch; |
| 24 | 24 | use engine::subtitle::{self, Cue, SubtitleTrack}; |
| 25 | 25 | use engine::{ExportHandle, Project}; |
| @@ -130,6 +130,18 @@ fn both_tracks() -> Vec<SubtitleTrack> { | ||
| 130 | 130 | /// and hands back what our own reader finds in the file -- one track per pick, |
| 131 | 131 | /// in the order they were picked. |
| 132 | 132 | fn exported(name: &str, project: Project, picks: &[usize]) -> (Scratch, Vec<SubtitleTrack>) { |
| 133 | + exported_want(name, project, picks, picks.len()) | |
| 134 | +} | |
| 135 | + | |
| 136 | +/// The same, for a project whose tracks are its subtitle *lanes*: the picks say | |
| 137 | +/// nothing then ([`engine::export::planned_subtitles`] reads the lanes instead), | |
| 138 | +/// so how many tracks the file must carry is the caller's own statement. | |
| 139 | +fn exported_want( | |
| 140 | + name: &str, | |
| 141 | + project: Project, | |
| 142 | + picks: &[usize], | |
| 143 | + want: usize, | |
| 144 | +) -> (Scratch, Vec<SubtitleTrack>) { | |
| 133 | 145 | pin_software(); |
| 134 | 146 | let out = out_path(name); |
| 135 | 147 | let (meta, _) = engine::demux::Demuxer::open(&asset(MEDIA)).expect("probe the media"); |
| @@ -145,11 +157,7 @@ fn exported(name: &str, project: Project, picks: &[usize]) -> (Scratch, Vec<Subt | ||
| 145 | 157 | ); |
| 146 | 158 | wait(&handle, Duration::from_secs(300)).expect("the export finishes"); |
| 147 | 159 | let tracks = subtitle::of_matroska(&out).expect("read the export back"); |
| 148 | - assert_eq!( | |
| 149 | - tracks.len(), | |
| 150 | - picks.len(), | |
| 151 | - "exactly the picked tracks: {tracks:?}" | |
| 152 | - ); | |
| 160 | + assert_eq!(tracks.len(), want, "exactly the tracks asked for: {tracks:?}"); | |
| 153 | 161 | for track in &tracks { |
| 154 | 162 | assert_eq!(track.refused, None, "the track reads back as text"); |
| 155 | 163 | } |
| @@ -742,3 +750,265 @@ fn the_plan_says_what_each_container_does_with_them() { | ||
| 742 | 750 | "eng → embedded; fra — Signs — no cues" |
| 743 | 751 | ); |
| 744 | 752 | } |
| 753 | + | |
| 754 | +// --- The lanes ------------------------------------------------------------- | |
| 755 | +// | |
| 756 | +// A timeline that *places* its words writes one track per subtitle lane, in | |
| 757 | +// lane order, and the pick list above says nothing about it. Every project that | |
| 758 | +// exists today has no placement and takes the old door -- which is what the | |
| 759 | +// tests above are, unchanged. | |
| 760 | + | |
| 761 | +/// A project of the fixture media with one subtitle lane per `(track, at, | |
| 762 | +/// frames, in_us, out_us)`, in the order given: `S1` first. | |
| 763 | +fn on_lanes(tracks: Vec<SubtitleTrack>, placed: &[(usize, u32, u32, i64, i64)]) -> Project { | |
| 764 | + let mut project = Project::single(asset(MEDIA), FRAMES).with_subtitles(tracks); | |
| 765 | + for &(track, at, frames, in_us, out_us) in placed { | |
| 766 | + let lane = project.add_lane(LaneKind::Subtitle); | |
| 767 | + project | |
| 768 | + .place_sub( | |
| 769 | + lane, | |
| 770 | + at, | |
| 771 | + SubClip { | |
| 772 | + start: at, | |
| 773 | + frames, | |
| 774 | + track, | |
| 775 | + in_us, | |
| 776 | + out_us, | |
| 777 | + }, | |
| 778 | + ) | |
| 779 | + .expect("the lane is empty, so the placement lands"); | |
| 780 | + } | |
| 781 | + project | |
| 782 | +} | |
| 783 | + | |
| 784 | +/// The whole of a palette row, placed at frame 0: the window every "this lane | |
| 785 | +/// carries that track" test wants. | |
| 786 | +const WHOLE: (u32, u32, i64, i64) = (0, FRAMES, 0, 5_000_000); | |
| 787 | + | |
| 788 | +/// Two lanes are two tracks, **in lane order** -- not in the palette's. `S1` | |
| 789 | +/// carries the French row and `S2` the English one, and the file declares them | |
| 790 | +/// that way round, which is what a subtitle menu will list. | |
| 791 | +#[test] | |
| 792 | +fn a_lane_is_a_track_and_the_lanes_order_the_file() { | |
| 793 | + let project = on_lanes( | |
| 794 | + both_tracks(), | |
| 795 | + &[ | |
| 796 | + (1, WHOLE.0, WHOLE.1, WHOLE.2, WHOLE.3), | |
| 797 | + (0, WHOLE.0, WHOLE.1, WHOLE.2, WHOLE.3), | |
| 798 | + ], | |
| 799 | + ); | |
| 800 | + // The card says the same before the button is pressed, lane by lane. | |
| 801 | + assert_eq!( | |
| 802 | + engine::export::planned_subtitles(&project, Format::Hevc, [0, 1]), | |
| 803 | + "2 lanes → embedded (S1 fra — Signs, S2 eng)" | |
| 804 | + ); | |
| 805 | + | |
| 806 | + // ...and the picks are *not* what travels: this asks for the palette's order | |
| 807 | + // and gets the lanes' one. | |
| 808 | + let (out, tracks) = exported_want("lanes_two", project, &[0, 1], 2); | |
| 809 | + assert_eq!(tracks[0].label, "fra — Signs", "S1 is the first track"); | |
| 810 | + assert_eq!(tracks[1].label, "eng", "S2 is the second"); | |
| 811 | + for track in &tracks { | |
| 812 | + assert_eq!(track.cues.len(), 3, "the lane's whole window: {track:?}"); | |
| 813 | + near(track.cues[0].start_us, 500_000, "cue start"); | |
| 814 | + near(track.cues[2].end_us, 4_750_000, "cue end"); | |
| 815 | + } | |
| 816 | + if let Some(listing) = ffmpeg_streams(&out) { | |
| 817 | + assert!( | |
| 818 | + listing.contains("(fra): Subtitle") && listing.contains("(eng): Subtitle"), | |
| 819 | + "a second implementation reads both lanes: {listing}" | |
| 820 | + ); | |
| 821 | + } | |
| 822 | + std::fs::remove_file(&out).unwrap(); | |
| 823 | +} | |
| 824 | + | |
| 825 | +/// The same two lanes into an **mp4**, where a track is `tx3g` timed text: | |
| 826 | +/// ffmpeg reads two `mov_text` streams in the lanes' order. | |
| 827 | +#[test] | |
| 828 | +fn two_lanes_leave_an_mp4_as_two_timed_text_tracks() { | |
| 829 | + let project = on_lanes( | |
| 830 | + both_tracks(), | |
| 831 | + &[ | |
| 832 | + (1, WHOLE.0, WHOLE.1, WHOLE.2, WHOLE.3), | |
| 833 | + (0, WHOLE.0, WHOLE.1, WHOLE.2, WHOLE.3), | |
| 834 | + ], | |
| 835 | + ); | |
| 836 | + let out = exported_mp4("lanes_mp4", project, &[]); | |
| 837 | + let Some(listing) = ffmpeg_streams(&out) else { | |
| 838 | + println!("no ffmpeg on this box: an mp4's tx3g has no other reader here"); | |
| 839 | + std::fs::remove_file(&out).unwrap(); | |
| 840 | + return; | |
| 841 | + }; | |
| 842 | + assert_eq!( | |
| 843 | + listing.matches("Subtitle: mov_text").count(), | |
| 844 | + 2, | |
| 845 | + "one stream per lane, though nothing was picked: {listing}" | |
| 846 | + ); | |
| 847 | + assert!( | |
| 848 | + listing.contains("(fra): Subtitle") && listing.contains("(eng): Subtitle"), | |
| 849 | + "both lanes' languages: {listing}" | |
| 850 | + ); | |
| 851 | + let srt = ffmpeg_srt(&out, 0).expect("ffmpeg is here"); | |
| 852 | + println!("ffmpeg reads mp4 lane S1 back as:\n{srt}"); | |
| 853 | + assert!( | |
| 854 | + srt.contains("first line") && srt.contains("third line"), | |
| 855 | + "{srt}" | |
| 856 | + ); | |
| 857 | + std::fs::remove_file(&out).unwrap(); | |
| 858 | +} | |
| 859 | + | |
| 860 | +/// A *trimmed* placement carries the cues its window keeps and no others, at the | |
| 861 | +/// times the placement puts them: 2.5s..5.0s of the track, dropped at timeline | |
| 862 | +/// frame 60, is the last two cues two and a half seconds earlier than the track | |
| 863 | +/// states them -- and the first one, outside the window, is not in the file. | |
| 864 | +#[test] | |
| 865 | +fn a_trimmed_placement_writes_the_cues_it_keeps_where_it_puts_them() { | |
| 866 | + let project = on_lanes(vec![track()], &[(0, 60, 75, 2_500_000, 5_000_000)]); | |
| 867 | + let (out, tracks) = exported_want("lanes_trim", project, &[], 1); | |
| 868 | + let cues = &tracks[0].cues; | |
| 869 | + assert_eq!(cues.len(), 2, "the first cue is outside the window: {cues:?}"); | |
| 870 | + // 2.0..3.25 of the track, of which 2.5s on is inside the window, landing at | |
| 871 | + // the placement's frame 60 (2.0s). | |
| 872 | + near( | |
| 873 | + cues[0].start_us, | |
| 874 | + 2_000_000, | |
| 875 | + "the straddling cue starts at the drop", | |
| 876 | + ); | |
| 877 | + near(cues[0].end_us, 2_750_000, "...and keeps the rest of its length"); | |
| 878 | + assert_eq!(cues[0].text, "second line\nwith a break"); | |
| 879 | + // 4.0..4.75, a second and a half into the window. | |
| 880 | + near( | |
| 881 | + cues[1].start_us, | |
| 882 | + 3_500_000, | |
| 883 | + "the last cue moves with the placement", | |
| 884 | + ); | |
| 885 | + near(cues[1].end_us, 4_250_000, "...end with it"); | |
| 886 | + assert_eq!(cues[1].text, "third line"); | |
| 887 | + if let Some(srt) = ffmpeg_srt(&out, 0) { | |
| 888 | + println!("ffmpeg reads the trimmed lane back as:\n{srt}"); | |
| 889 | + assert!(!srt.contains("first line"), "{srt}"); | |
| 890 | + assert!(srt.contains("00:00:02,000 --> 00:00:02,750"), "{srt}"); | |
| 891 | + assert!(srt.contains("00:00:03,500 --> 00:00:04,250"), "{srt}"); | |
| 892 | + } | |
| 893 | + std::fs::remove_file(&out).unwrap(); | |
| 894 | +} | |
| 895 | + | |
| 896 | +/// A lane nobody has placed anything on is not a project that uses lanes: the | |
| 897 | +/// picks still say what travels, byte for byte the file today's app exports. | |
| 898 | +#[test] | |
| 899 | +fn an_empty_lane_leaves_the_pick_list_in_charge() { | |
| 900 | + let mut project = Project::single(asset(MEDIA), FRAMES).with_subtitles(both_tracks()); | |
| 901 | + project.add_lane(LaneKind::Subtitle); | |
| 902 | + assert_eq!( | |
| 903 | + engine::export::planned_subtitles(&project, Format::Hevc, [0]), | |
| 904 | + "eng → embedded", | |
| 905 | + "the pick list's words, not a lane's" | |
| 906 | + ); | |
| 907 | + let (out, tracks) = exported("lanes_none", project, &[0]); | |
| 908 | + assert_eq!(tracks.len(), 1, "the pick, and only the pick"); | |
| 909 | + assert_eq!(tracks[0].label, "eng"); | |
| 910 | + assert_eq!(tracks[0].cues.len(), 3); | |
| 911 | + std::fs::remove_file(&out).unwrap(); | |
| 912 | +} | |
| 913 | + | |
| 914 | +/// A lane of **pictures** writes nothing at all, and says why before the button | |
| 915 | +/// is pressed: both muxers write words (`S_TEXT/UTF8`, `tx3g`) and neither has a | |
| 916 | +/// path that could write a bitmap -- the refusal is the capability's absence and | |
| 917 | +/// not a policy. | |
| 918 | +#[test] | |
| 919 | +fn a_lane_of_pictures_is_refused_in_words_and_written_nowhere() { | |
| 920 | + let mut pictures = track(); | |
| 921 | + pictures.bitmap = true; | |
| 922 | + pictures.label = "PGS".into(); | |
| 923 | + let project = on_lanes( | |
| 924 | + vec![track(), pictures], | |
| 925 | + &[ | |
| 926 | + (0, WHOLE.0, WHOLE.1, WHOLE.2, WHOLE.3), | |
| 927 | + (1, WHOLE.0, WHOLE.1, WHOLE.2, WHOLE.3), | |
| 928 | + ], | |
| 929 | + ); | |
| 930 | + assert_eq!( | |
| 931 | + engine::export::planned_subtitles(&project, Format::Hevc, []), | |
| 932 | + "S1 eng → embedded; S2 PGS — pictures; drawn, not written", | |
| 933 | + "the picture lane costs its own row and not the other's" | |
| 934 | + ); | |
| 935 | + // ...and the file has the one lane's track and nothing for the other: zero | |
| 936 | + // streams for a lane of pictures, not an empty one. | |
| 937 | + let (out, tracks) = exported_want("lanes_pgs", project, &[], 1); | |
| 938 | + assert_eq!( | |
| 939 | + tracks[0].label, "eng", | |
| 940 | + "the words travelled; the pictures did not" | |
| 941 | + ); | |
| 942 | + std::fs::remove_file(&out).unwrap(); | |
| 943 | +} | |
| 944 | + | |
| 945 | +/// A caption hanging past the last picture: the timeline is held open under it | |
| 946 | +/// ([`Project::timeline_frames`]) but the export ends with the picture, so what | |
| 947 | +/// is out there is cut off -- a cue straddling the end keeps the half inside, a | |
| 948 | +/// lane wholly past it writes no track, and the card says so. | |
| 949 | +#[test] | |
| 950 | +fn a_caption_past_the_last_picture_is_cut_off_with_the_file() { | |
| 951 | + // S1 straddles the end: dropped at frame 120 (4.0s) with a 60-frame window | |
| 952 | + // of the track's first two seconds, so its one cue runs 4.5s..5.5s and the | |
| 953 | + // media stops at 5.0s. S2 sits entirely past the end. | |
| 954 | + let project = on_lanes( | |
| 955 | + vec![track(), track()], | |
| 956 | + &[(0, 120, 60, 0, 2_000_000), (1, FRAMES, 60, 0, 2_000_000)], | |
| 957 | + ); | |
| 958 | + assert_eq!(project.timeline_frames(), 210, "the captions hold it open"); | |
| 959 | + assert_eq!(project.media_frames(), FRAMES, "the pictures do not"); | |
| 960 | + assert_eq!( | |
| 961 | + engine::export::planned_subtitles(&project, Format::Hevc, []), | |
| 962 | + "S1 eng → embedded; S2 eng — past the last picture; cues past the last picture are cut \ | |
| 963 | + off there" | |
| 964 | + ); | |
| 965 | + | |
| 966 | + let (out, tracks) = exported_want("lanes_tail", project, &[], 1); | |
| 967 | + let cues = &tracks[0].cues; | |
| 968 | + assert_eq!(cues.len(), 1, "the one cue the window holds: {cues:?}"); | |
| 969 | + near(cues[0].start_us, 4_500_000, "where the placement puts it"); | |
| 970 | + near(cues[0].end_us, 5_000_000, "cut off with the last picture"); | |
| 971 | + std::fs::remove_file(&out).unwrap(); | |
| 972 | +} | |
| 973 | + | |
| 974 | +/// The Matroska ceiling counts lanes as it counts picks: past `MAX_SUB_TRACKS` a | |
| 975 | +/// block could not number the track, so the card says so and the export refuses | |
| 976 | +/// by name and leaves no file. | |
| 977 | +#[test] | |
| 978 | +fn more_lanes_than_a_block_can_number_are_refused_by_name() { | |
| 979 | + pin_software(); | |
| 980 | + let over = engine::mux::MAX_SUB_TRACKS + 1; | |
| 981 | + let project = on_lanes( | |
| 982 | + (0..over).map(|_| track()).collect(), | |
| 983 | + &(0..over) | |
| 984 | + .map(|i| (i, WHOLE.0, WHOLE.1, WHOLE.2, WHOLE.3)) | |
| 985 | + .collect::<Vec<_>>(), | |
| 986 | + ); | |
| 987 | + let said = engine::export::planned_subtitles(&project, Format::Hevc, []); | |
| 988 | + assert!( | |
| 989 | + said.contains(&format!("{over} subtitle lanes")) | |
| 990 | + && said.contains(&format!("at most {}", engine::mux::MAX_SUB_TRACKS)), | |
| 991 | + "the card says how many were placed and how many fit: {said}" | |
| 992 | + ); | |
| 993 | + | |
| 994 | + let out = out_path("lanes_ceiling"); | |
| 995 | + let (meta, _) = engine::demux::Demuxer::open(&asset(MEDIA)).expect("probe the media"); | |
| 996 | + let handle = engine::export::start( | |
| 997 | + project, | |
| 998 | + meta, | |
| 999 | + &out, | |
| 1000 | + &ExportSettings { | |
| 1001 | + format: Format::Hevc, | |
| 1002 | + ..Default::default() | |
| 1003 | + }, | |
| 1004 | + ); | |
| 1005 | + let said = wait(&handle, Duration::from_secs(300)) | |
| 1006 | + .expect_err("a file that cannot be numbered is not written") | |
| 1007 | + .to_string(); | |
| 1008 | + assert!( | |
| 1009 | + said.contains(&format!("{over} subtitle tracks")) | |
| 1010 | + && said.contains(&format!("at most {}", engine::mux::MAX_SUB_TRACKS)), | |
| 1011 | + "the muxer refuses it again, in its own words: {said}" | |
| 1012 | + ); | |
| 1013 | + assert!(!out.exists(), "and no half-written file is left: {said}"); | |
| 1014 | +} |
crates/engine/tests/pgs_bitmaps.rsmodified+29 -0
| @@ -81,6 +81,35 @@ fn the_session_maps_a_pgs_track_onto_the_timeline_and_draws_it() { | ||
| 81 | 81 | "a subtitle that covers the whole frame is not a subtitle" |
| 82 | 82 | ); |
| 83 | 83 | |
| 84 | + // ...and placed on a subtitle lane, that same real row is refused by name | |
| 85 | + // before any file exists: both muxers write *words* (`S_TEXT/UTF8`, `tx3g`) | |
| 86 | + // and neither has a path that could write a run-length picture, so the lane | |
| 87 | + // carries no track at all rather than the file coming out short | |
| 88 | + // (`export::planned_lanes`; `tests/export_subs.rs` pins the empty file it | |
| 89 | + // leaves). | |
| 90 | + let lane = session.add_lane(engine::project::LaneKind::Subtitle); | |
| 91 | + session | |
| 92 | + .place_sub( | |
| 93 | + lane, | |
| 94 | + 0, | |
| 95 | + engine::project::SubClip { | |
| 96 | + start: 0, | |
| 97 | + frames: 60, | |
| 98 | + track: pick, | |
| 99 | + in_us: cues[0].start_us, | |
| 100 | + out_us: cues[0].end_us, | |
| 101 | + }, | |
| 102 | + ) | |
| 103 | + .expect("the lane is empty, so the placement lands"); | |
| 104 | + assert_eq!( | |
| 105 | + session.planned_subtitles(engine::export::Format::Hevc, []), | |
| 106 | + format!( | |
| 107 | + "S1 {} — pictures; drawn, not written", | |
| 108 | + session.subtitles()[pick].label | |
| 109 | + ), | |
| 110 | + "a real PGS row on a lane says why it cannot travel" | |
| 111 | + ); | |
| 112 | + | |
| 84 | 113 | // A text row of the same film carries no picture, which is what keeps the |
| 85 | 114 | // two kinds of row apart. |
| 86 | 115 | let text = session |