Merge branch 'worktree-agent-acff1c2ce8e5711d3' into worktree-agent-a6b6dbdb311480b83
Browse files at this commit7 changed files +1154 -21
- crates/app/src/player/timeline_edit.rs +3 -0
- crates/app/src/timeline_math.rs +3 -0
- crates/app/src/ui/theme.rs +6 -0
- crates/engine/src/edith.rs +9 -0
- crates/engine/src/project.rs +685 -20
- crates/engine/tests/lanes.rs +135 -1
- crates/engine/tests/subtitles.rs +313 -0
crates/app/src/player/timeline_edit.rsmodified+3 -0
| @@ -226,6 +226,9 @@ impl Player { | ||
| 226 | 226 | let (kind, lanes) = match from.kind { |
| 227 | 227 | LaneKind::Video => ("picture", "video"), |
| 228 | 228 | LaneKind::Audio => ("sound", "audio"), |
| 229 | + // Never dragged from here yet -- a subtitle lane holds no `Clip`, | |
| 230 | + // so `lane_clips` is empty and the drag above never starts on one. | |
| 231 | + LaneKind::Subtitle => ("caption", "subtitle"), | |
| 229 | 232 | }; |
| 230 | 233 | match moved { |
| 231 | 234 | true => { |
crates/app/src/timeline_math.rsmodified+3 -0
| @@ -35,6 +35,9 @@ pub(crate) fn whole_take(session: &PlaybackSession, lane: Lane, idx: usize) -> b | ||
| 35 | 35 | // The sound of a take, only while the take is still there: its group is |
| 36 | 36 | // carried by a clip on some other lane. |
| 37 | 37 | (LaneKind::Audio, _) => paired(), |
| 38 | + // A subtitle lane carries no `Clip` at all, so the lookup above already | |
| 39 | + // returned: never a take, and never rippled as one. | |
| 40 | + (LaneKind::Subtitle, _) => false, | |
| 38 | 41 | } |
| 39 | 42 | } |
| 40 | 43 |
crates/app/src/ui/theme.rsmodified+6 -0
| @@ -1095,6 +1095,12 @@ pub fn clip_kind(kind: LaneKind, image: bool) -> u32 { | ||
| 1095 | 1095 | LaneKind::Audio => CLIP_AUDIO(), |
| 1096 | 1096 | LaneKind::Video if image => CLIP_IMAGE(), |
| 1097 | 1097 | LaneKind::Video => CLIP_VIDEO(), |
| 1098 | + // A caption borrows the still's shade until the slice that draws | |
| 1099 | + // subtitle lanes gives it one of its own in every family: nothing on | |
| 1100 | + // screen paints one yet (the engine model landed first), and inventing | |
| 1101 | + // a colour here for a box nobody draws is the one thing this call says | |
| 1102 | + // not to do. | |
| 1103 | + LaneKind::Subtitle => CLIP_IMAGE(), | |
| 1098 | 1104 | } |
| 1099 | 1105 | } |
| 1100 | 1106 |
crates/engine/src/edith.rsmodified+9 -0
| @@ -432,6 +432,12 @@ fn emit( | ||
| 432 | 432 | audio += 1; |
| 433 | 433 | ("audio", audio) |
| 434 | 434 | } |
| 435 | + // Unreachable by construction: `Project::without_orphan_sources` | |
| 436 | + // leaves the subtitle lanes out of the [`Parts`] it hands a save, | |
| 437 | + // because this format has no line to write a `SubClip` on yet and a | |
| 438 | + // reader that met the keyword would refuse the file. Skipped rather | |
| 439 | + // than emitted, and the corner-cut is named at the filter. | |
| 440 | + LaneKind::Subtitle => continue, | |
| 435 | 441 | }; |
| 436 | 442 | // A lane is declared by its clips; one holding nothing has to say so on |
| 437 | 443 | // a line of its own, or the lane itself would not survive the round trip. |
| @@ -470,6 +476,9 @@ fn emit( | ||
| 470 | 476 | audio += 1; |
| 471 | 477 | ("audio", audio) |
| 472 | 478 | } |
| 479 | + // Never here, for the reason above -- and a subtitle lane has no | |
| 480 | + // volume to write anyway (`Project::set_lane_gain_db` refuses one). | |
| 481 | + LaneKind::Subtitle => continue, | |
| 473 | 482 | }; |
| 474 | 483 | if db != 0.0 { |
| 475 | 484 | out.extend_from_slice(format!("gain {keyword} {ord} {db:?}\n").as_bytes()); |
crates/engine/src/project.rsmodified+685 -20
| @@ -42,6 +42,17 @@ | ||
| 42 | 42 | //! and the caller must reseek. Every successful edit snapshots every lane, so |
| 43 | 43 | //! [`Project::undo`] is an exact restore -- of the clips *and* of the lane list. |
| 44 | 44 | //! |
| 45 | +//! Words are a third kind of lane ([`LaneKind::Subtitle`]) and not a setting on | |
| 46 | +//! the project: a caption is placed, moved, trimmed, cut and rippled by the | |
| 47 | +//! very machinery above, and it joins the undo history for free because a | |
| 48 | +//! snapshot is the whole lane list. What such a lane holds is a [`SubClip`] -- | |
| 49 | +//! a `[in_us, out_us)` window of one of the project's subtitle tracks | |
| 50 | +//! ([`Project::subtitles`], which stays the *palette* the cues are read into), | |
| 51 | +//! placed at a timeline frame like everything else. It holds no [`Clip`] at | |
| 52 | +//! all, which is what keeps every media path (the composite, the mix, an | |
| 53 | +//! equalizer, a speed) from ever meeting one: they read a lane's clips, and | |
| 54 | +//! there are none. | |
| 55 | +//! | |
| 45 | 56 | //! A clip names its file by *index* into [`Project::sources`], which is |
| 46 | 57 | //! append-only: an index handed out once stays valid forever, so a clip on the |
| 47 | 58 | //! clipboard or inside an undo snapshot can never dangle. An index -- not an |
| @@ -68,7 +79,7 @@ use crate::color::ColorParams; | ||
| 68 | 79 | use crate::eq::EqParams; |
| 69 | 80 | use crate::limiter::{Limiter, db_to_linear}; |
| 70 | 81 | use crate::scale::FitPolicy; |
| 71 | -use crate::subtitle::SubtitleTrack; | |
| 82 | +use crate::subtitle::{Cue as SubtitleCue, SubtitleTrack}; | |
| 72 | 83 | |
| 73 | 84 | /// How far down a lane's own volume goes ([`Project::set_lane_gain_db`]). |
| 74 | 85 | /// -60 dB is a thousandth of the amplitude -- a track that is out of the mix |
| @@ -455,11 +466,26 @@ impl Source { | ||
| 455 | 466 | } |
| 456 | 467 | |
| 457 | 468 | /// What a lane carries, which is what a *gap* in it means: black frames on a |
| 458 | -/// video lane, silence on an audio one. | |
| 469 | +/// video lane, silence on an audio one, nothing over the picture on a subtitle | |
| 470 | +/// one. | |
| 459 | 471 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 460 | 472 | pub enum LaneKind { |
| 461 | 473 | Video, |
| 462 | 474 | Audio, |
| 475 | + /// Words over the picture, placed and trimmed like everything else | |
| 476 | + /// ([`SubClip`]). A lane of its own rather than a setting on the project, | |
| 477 | + /// so a caption is dragged, cut, rippled and undone by the machinery every | |
| 478 | + /// other clip already goes through. | |
| 479 | + /// | |
| 480 | + /// It carries no [`Clip`] at all, and that is what keeps the media paths | |
| 481 | + /// free of it: [`Project::lane`] hands back an empty slice, so every | |
| 482 | + /// clip-indexed call ([`Project::trim`], [`Project::set_eq`], | |
| 483 | + /// [`Project::set_speed`], [`Project::lift`]) refuses it by the bounds | |
| 484 | + /// check it already had, [`Project::composite_span_at`] and | |
| 485 | + /// [`Project::audio_lanes`] never look at it, and the three doors that | |
| 486 | + /// could still put a picture on one -- [`Project::from_parts`], | |
| 487 | + /// [`Project::place`], [`Project::append_clip`] -- say so by name. | |
| 488 | + Subtitle, | |
| 463 | 489 | } |
| 464 | 490 | |
| 465 | 491 | /// Which lane an operation acts on: a kind plus a 0-based position among the |
| @@ -481,6 +507,10 @@ impl Lane { | ||
| 481 | 507 | pub const V1: Lane = Lane::new(LaneKind::Video, 0); |
| 482 | 508 | /// The first audio lane: what the audio worker plays. |
| 483 | 509 | pub const A1: Lane = Lane::new(LaneKind::Audio, 0); |
| 510 | + /// The first subtitle lane -- the one a project has only once something | |
| 511 | + /// added it ([`Project::add_lane`]); a project starts with `V1` and `A1` | |
| 512 | + /// alone. | |
| 513 | + pub const S1: Lane = Lane::new(LaneKind::Subtitle, 0); | |
| 484 | 514 | |
| 485 | 515 | pub const fn new(kind: LaneKind, ord: usize) -> Self { |
| 486 | 516 | Self { kind, ord } |
| @@ -492,19 +522,90 @@ impl Lane { | ||
| 492 | 522 | let kind = match self.kind { |
| 493 | 523 | LaneKind::Video => 'V', |
| 494 | 524 | LaneKind::Audio => 'A', |
| 525 | + LaneKind::Subtitle => 'S', | |
| 495 | 526 | }; |
| 496 | 527 | format!("{kind}{}", self.ord + 1) |
| 497 | 528 | } |
| 498 | 529 | } |
| 499 | 530 | |
| 531 | +/// A placed subtitle: a `[in_us, out_us)` window of one of the project's | |
| 532 | +/// subtitle tracks ([`Project::subtitles`], which is the *palette*), shown from | |
| 533 | +/// timeline frame [`start`](SubClip::start) for [`frames`](SubClip::frames) | |
| 534 | +/// frames. Never empty at either end. | |
| 535 | +/// | |
| 536 | +/// A [`Clip`] with the two frame spaces swapped for the two clocks a subtitle | |
| 537 | +/// actually has, and placed by the same one number: the window is in the | |
| 538 | +/// microseconds every parser here speaks ([`crate::subtitle::Cue`]), the | |
| 539 | +/// placement is in timeline frames like everything else on a lane. So the lane | |
| 540 | +/// invariant is the lane's ([`subs_sorted_disjoint`] is | |
| 541 | +/// [`sorted_disjoint`] over the same arithmetic), a move is `start` and nothing | |
| 542 | +/// else, and only the two calls that change a *duration* -- a trim, and the | |
| 543 | +/// mapping out -- ever need the timeline's rate. | |
| 544 | +/// | |
| 545 | +/// `Copy` for [`Clip`]'s reason: the palette is named by index, so a copy, a | |
| 546 | +/// paste and an undo snapshot are a plain assignment and can never dangle. | |
| 547 | +#[derive(Clone, Copy, PartialEq, Eq, Debug)] | |
| 548 | +pub struct SubClip { | |
| 549 | + /// Timeline frame its first frame is shown at. | |
| 550 | + pub start: u32, | |
| 551 | + /// How many timeline frames it covers; `>= 1`. Held rather than derived | |
| 552 | + /// from the window because a [`Project`] has no frame rate -- the rate | |
| 553 | + /// enters at the door of the two calls that need it -- and every placement | |
| 554 | + /// question (where it ends, what it overlaps) is asked in frames. | |
| 555 | + pub frames: u32, | |
| 556 | + /// Index into [`Project::subtitles`]: *which* track's words these are. | |
| 557 | + pub track: usize, | |
| 558 | + /// The half-open window of that track it shows, in microseconds from the | |
| 559 | + /// start of the track -- exactly the clock its [`crate::subtitle::Cue`]s are | |
| 560 | + /// timed in, so no conversion stands between a cue and the window that | |
| 561 | + /// keeps it. | |
| 562 | + pub in_us: i64, | |
| 563 | + pub out_us: i64, | |
| 564 | +} | |
| 565 | + | |
| 566 | +impl SubClip { | |
| 567 | + /// One past the last timeline frame it covers -- [`Clip::end`]'s twin, and | |
| 568 | + /// what every overlap question is asked of. | |
| 569 | + pub fn end(&self) -> u32 { | |
| 570 | + self.start + self.frames | |
| 571 | + } | |
| 572 | + | |
| 573 | + /// How much of its track it shows, in microseconds; `>= 1` by the | |
| 574 | + /// never-empty invariant. | |
| 575 | + pub fn window_us(&self) -> i64 { | |
| 576 | + self.out_us - self.in_us | |
| 577 | + } | |
| 578 | + | |
| 579 | + /// Where in its window the timeline frame `frame` falls, in microseconds -- | |
| 580 | + /// the cut a split, a ripple and a trim all land on. | |
| 581 | + /// | |
| 582 | + /// By proportion and not by the timeline's rate: the window and the frame | |
| 583 | + /// count measure the same stretch of time, so the fraction of the frames | |
| 584 | + /// before `frame` is the fraction of the microseconds before it. That is | |
| 585 | + /// what lets a subtitle be cut by [`clear`]'s and [`open_room`]'s twins | |
| 586 | + /// without either of them learning an fps. | |
| 587 | + fn window_at(&self, frame: u32) -> i64 { | |
| 588 | + let into = i64::from(frame.saturating_sub(self.start)); | |
| 589 | + self.in_us + self.window_us() * into / i64::from(self.frames.max(1)) | |
| 590 | + } | |
| 591 | +} | |
| 592 | + | |
| 500 | 593 | /// One lane: what it is and what it holds. A struct rather than a bare |
| 501 | 594 | /// `Vec<Clip>` because per-lane state is what the next slices add -- mute, a |
| 502 | 595 | /// compositing mode, a name -- and it belongs next to the clips it applies to. |
| 503 | 596 | #[derive(Clone, Debug)] |
| 504 | 597 | struct LaneData { |
| 505 | 598 | kind: LaneKind, |
| 506 | - /// Sorted by `start` and disjoint ([`sorted_disjoint`]). | |
| 599 | + /// Sorted by `start` and disjoint ([`sorted_disjoint`]). Always empty on a | |
| 600 | + /// [`LaneKind::Subtitle`] lane, which is what keeps every media path from | |
| 601 | + /// ever seeing a subtitle: they all read this list. | |
| 507 | 602 | clips: Vec<Clip>, |
| 603 | + /// The same, for a subtitle lane ([`subs_sorted_disjoint`]), and always | |
| 604 | + /// empty on a video or audio one. Beside the clips rather than an enum over | |
| 605 | + /// them so that everything a lane *is* -- its place in the stack, its | |
| 606 | + /// label, its undo snapshot, its removal -- stays one code path for all | |
| 607 | + /// three kinds. | |
| 608 | + subs: Vec<SubClip>, | |
| 508 | 609 | /// How loud this lane plays, in dB, `0.0` for one nobody has touched -- |
| 509 | 610 | /// [`Project::lane_gain_db`]. Whole-lane and whole-band, which is what |
| 510 | 611 | /// makes it a different thing from a clip's equalizer: it moves everything |
| @@ -518,6 +619,7 @@ impl LaneData { | ||
| 518 | 619 | Self { |
| 519 | 620 | kind, |
| 520 | 621 | clips, |
| 622 | + subs: Vec::new(), | |
| 521 | 623 | gain_db: 0.0, |
| 522 | 624 | } |
| 523 | 625 | } |
| @@ -732,6 +834,18 @@ impl Project { | ||
| 732 | 834 | .collect(); |
| 733 | 835 | for (data, lane) in lanes.iter().zip(handles(&lanes)) { |
| 734 | 836 | let name = lane.label(); |
| 837 | + // A subtitle lane carries words and never a picture | |
| 838 | + // ([`LaneKind::Subtitle`]); a file that puts one there names a | |
| 839 | + // source on a lane that has nothing to play it with, and every | |
| 840 | + // media path below would then read a clip off a lane it does not | |
| 841 | + // know about. | |
| 842 | + if data.kind == LaneKind::Subtitle && !data.clips.is_empty() { | |
| 843 | + return Err(format!( | |
| 844 | + "{name} is a subtitle track and holds {} media clip(s): a picture cannot play on one", | |
| 845 | + data.clips.len() | |
| 846 | + ) | |
| 847 | + .into()); | |
| 848 | + } | |
| 735 | 849 | for c in &data.clips { |
| 736 | 850 | if c.out_frame <= c.in_frame { |
| 737 | 851 | return Err(format!( |
| @@ -858,17 +972,29 @@ impl Project { | ||
| 858 | 972 | let Some(i) = self.index(lane) else { |
| 859 | 973 | return Err(format!("there is no {} to remove", lane.label()).into()); |
| 860 | 974 | }; |
| 861 | - if self.lane_count(lane.kind) == 1 { | |
| 975 | + // A subtitle lane is not in that rule: nothing lands on one by itself | |
| 976 | + // -- a project starts without any and one is added when a caption is -- | |
| 977 | + // so the last of them comes off like any other empty track. | |
| 978 | + if self.lane_count(lane.kind) == 1 && lane.kind != LaneKind::Subtitle { | |
| 862 | 979 | return Err(format!( |
| 863 | 980 | "{} is the only {} track: every import lands on it", |
| 864 | 981 | lane.label(), |
| 865 | 982 | match lane.kind { |
| 866 | 983 | LaneKind::Video => "video", |
| 867 | - LaneKind::Audio => "audio", | |
| 984 | + LaneKind::Audio | LaneKind::Subtitle => "audio", | |
| 868 | 985 | } |
| 869 | 986 | ) |
| 870 | 987 | .into()); |
| 871 | 988 | } |
| 989 | + if let Some(sub) = self.lanes[i].subs.first() { | |
| 990 | + return Err(format!( | |
| 991 | + "{} still holds {} subtitle(s), the first at frame {}: delete those first", | |
| 992 | + lane.label(), | |
| 993 | + self.lanes[i].subs.len(), | |
| 994 | + sub.start | |
| 995 | + ) | |
| 996 | + .into()); | |
| 997 | + } | |
| 872 | 998 | let clips = &self.lanes[i].clips; |
| 873 | 999 | if !clips.is_empty() { |
| 874 | 1000 | // Three names and a count: a lane can hold forty clips and a |
| @@ -985,10 +1111,14 @@ impl Project { | ||
| 985 | 1111 | } |
| 986 | 1112 | } |
| 987 | 1113 | |
| 988 | - /// Append a whole-file clip of `source` to the end of *every* lane, grouped | |
| 989 | - /// -- an import lands as one take. One history snapshot, so an import is one | |
| 990 | - /// undo step, and undoing it leaves the (harmless) source entry behind, | |
| 991 | - /// because indexes are forever. Refused for an unknown source index. | |
| 1114 | + /// Append a whole-file clip of `source` to the end of every *media* lane, | |
| 1115 | + /// grouped -- an import lands as one take. Subtitle lanes are skipped: they | |
| 1116 | + /// carry no [`Clip`] ([`LaneKind::Subtitle`]), and a file's own subtitles | |
| 1117 | + /// come in through [`Project::add_subtitles`], which is the palette. | |
| 1118 | + /// | |
| 1119 | + /// One history snapshot, so an import is one undo step, and undoing it | |
| 1120 | + /// leaves the (harmless) source entry behind, because indexes are forever. | |
| 1121 | + /// Refused for an unknown source index. | |
| 992 | 1122 | pub fn append_clip(&mut self, source: usize, frame_count: u32) -> bool { |
| 993 | 1123 | if source >= self.sources.len() { |
| 994 | 1124 | return false; |
| @@ -1006,7 +1136,11 @@ impl Project { | ||
| 1006 | 1136 | fit: FitPolicy::default(), |
| 1007 | 1137 | speed: Speed::NORMAL, |
| 1008 | 1138 | }; |
| 1009 | - for data in &mut self.lanes { | |
| 1139 | + for data in self | |
| 1140 | + .lanes | |
| 1141 | + .iter_mut() | |
| 1142 | + .filter(|l| l.kind != LaneKind::Subtitle) | |
| 1143 | + { | |
| 1010 | 1144 | data.clips.push(clip); |
| 1011 | 1145 | } |
| 1012 | 1146 | true |
| @@ -1145,7 +1279,20 @@ impl Project { | ||
| 1145 | 1279 | } |
| 1146 | 1280 | ( |
| 1147 | 1281 | sources, |
| 1148 | - lanes.into_iter().map(|l| (l.kind, l.clips)).collect(), | |
| 1282 | + lanes | |
| 1283 | + .into_iter() | |
| 1284 | + // corner-cut: the subtitle lanes are left out, because [`Parts`] | |
| 1285 | + // is `(LaneKind, Vec<Clip>)` and a subtitle lane's contents are | |
| 1286 | + // [`SubClip`]s -- a `.edith` has no line to write one on yet, so | |
| 1287 | + // a save that emitted the lane would write a track with nothing | |
| 1288 | + // in it and a *reader* that has never seen the keyword would | |
| 1289 | + // refuse the file. Upgrade path is the format slice that owns | |
| 1290 | + // it: a `sub` line per placement (lane ord, start, frames, | |
| 1291 | + // track, window) beside the `video`/`audio` ones, and this | |
| 1292 | + // filter goes. | |
| 1293 | + .filter(|l| l.kind != LaneKind::Subtitle) | |
| 1294 | + .map(|l| (l.kind, l.clips)) | |
| 1295 | + .collect(), | |
| 1149 | 1296 | eq, |
| 1150 | 1297 | color, |
| 1151 | 1298 | ) |
| @@ -1164,11 +1311,17 @@ impl Project { | ||
| 1164 | 1311 | /// One undo step like every other edit -- it is in the lane list, so an |
| 1165 | 1312 | /// undo restores it with the rest. `false`, and no history, for a lane that |
| 1166 | 1313 | /// is not there, for a value that is not finite, and for one already set. |
| 1314 | + /// | |
| 1315 | + /// `false` too for a subtitle lane, and that one is a *refusal* rather than | |
| 1316 | + /// a value nobody reads: words have no loudness, so a slider that appeared | |
| 1317 | + /// to move one would be a control with no effect. (A video lane's gain is | |
| 1318 | + /// meaningless in the same way but is kept settable: it is the pair `A1` | |
| 1319 | + /// carries for a take, and mixing reads it off the audio lane alone.) | |
| 1167 | 1320 | pub fn set_lane_gain_db(&mut self, lane: Lane, db: f32) -> bool { |
| 1168 | 1321 | let Some(i) = self.index(lane) else { |
| 1169 | 1322 | return false; |
| 1170 | 1323 | }; |
| 1171 | - if !db.is_finite() { | |
| 1324 | + if !db.is_finite() || lane.kind == LaneKind::Subtitle { | |
| 1172 | 1325 | return false; |
| 1173 | 1326 | } |
| 1174 | 1327 | let db = db.clamp(MIN_GAIN_DB, MAX_GAIN_DB); |
| @@ -1182,8 +1335,16 @@ impl Project { | ||
| 1182 | 1335 | |
| 1183 | 1336 | /// Every lane's volume in dB, in display order -- what a save writes, |
| 1184 | 1337 | /// beside the lanes [`Project::without_orphan_sources`] hands back. |
| 1338 | + /// | |
| 1339 | + /// The *same* lanes as that call, subtitle ones left out: a save zips the | |
| 1340 | + /// two lists, so a lane that is in one and not the other would hand every | |
| 1341 | + /// track below it the volume of the track above. | |
| 1185 | 1342 | pub fn lane_gains(&self) -> Vec<f32> { |
| 1186 | - self.lanes.iter().map(|l| l.gain_db).collect() | |
| 1343 | + self.lanes | |
| 1344 | + .iter() | |
| 1345 | + .filter(|l| l.kind != LaneKind::Subtitle) | |
| 1346 | + .map(|l| l.gain_db) | |
| 1347 | + .collect() | |
| 1187 | 1348 | } |
| 1188 | 1349 | |
| 1189 | 1350 | /// The mix settings a *load* puts back: lane volumes in display order (a |
| @@ -1287,6 +1448,341 @@ impl Project { | ||
| 1287 | 1448 | Ok(()) |
| 1288 | 1449 | } |
| 1289 | 1450 | |
| 1451 | + /// The subtitle lanes in display order -- what a front-end lays out and | |
| 1452 | + /// what [`sub_lane_cues`](Project::sub_lane_cues) is asked of, the twin of | |
| 1453 | + /// [`audio_lanes`](Project::audio_lanes). | |
| 1454 | + pub fn subtitle_lanes(&self) -> Vec<Lane> { | |
| 1455 | + self.lanes() | |
| 1456 | + .into_iter() | |
| 1457 | + .filter(|l| l.kind == LaneKind::Subtitle) | |
| 1458 | + .collect() | |
| 1459 | + } | |
| 1460 | + | |
| 1461 | + /// What is placed on `lane`, in timeline order -- [`Project::lane`]'s twin | |
| 1462 | + /// for a subtitle track, and empty for every other lane and for one that is | |
| 1463 | + /// not there. | |
| 1464 | + pub fn sub_lane(&self, lane: Lane) -> &[SubClip] { | |
| 1465 | + self.index(lane).map_or(&[][..], |i| &self.lanes[i].subs) | |
| 1466 | + } | |
| 1467 | + | |
| 1468 | + /// Put `sub` on `lane` at timeline frame `at` -- [`place`](Project::place)'s | |
| 1469 | + /// twin, with one deliberate difference: it **refuses** an overlap instead | |
| 1470 | + /// of overwriting what it lands on. | |
| 1471 | + /// | |
| 1472 | + /// A picture placed over a picture hides it and the hidden one is still | |
| 1473 | + /// there to drag back out; two captions in one frame are two lines nobody | |
| 1474 | + /// asked to stack, and the words that lost are gone from the screen with | |
| 1475 | + /// nothing to show for it. So the refusal names the placement in the way, | |
| 1476 | + /// and the caller picks another frame or another lane -- which is what a | |
| 1477 | + /// second subtitle lane is for. | |
| 1478 | + /// | |
| 1479 | + /// One undo step on success, and none on any refusal: an empty window or an | |
| 1480 | + /// empty span, a track the palette does not have | |
| 1481 | + /// ([`Project::subtitles`]), a lane that is not there, a lane that is not a | |
| 1482 | + /// subtitle lane, a span that would run past the last frame there is, and | |
| 1483 | + /// the overlap above. | |
| 1484 | + pub fn place_sub(&mut self, lane: Lane, at: u32, sub: SubClip) -> crate::Result<()> { | |
| 1485 | + if lane.kind != LaneKind::Subtitle { | |
| 1486 | + return Err(format!( | |
| 1487 | + "{} is not a subtitle track: words go on a subtitle track, pictures and sound on \ | |
| 1488 | + the others", | |
| 1489 | + lane.label() | |
| 1490 | + ) | |
| 1491 | + .into()); | |
| 1492 | + } | |
| 1493 | + let Some(i) = self.index(lane) else { | |
| 1494 | + return Err(format!("there is no {} to place on", lane.label()).into()); | |
| 1495 | + }; | |
| 1496 | + if sub.frames == 0 || sub.out_us <= sub.in_us || sub.in_us < 0 { | |
| 1497 | + return Err(format!( | |
| 1498 | + "that placement is empty: {} frames of [{}, {})", | |
| 1499 | + sub.frames, sub.in_us, sub.out_us | |
| 1500 | + ) | |
| 1501 | + .into()); | |
| 1502 | + } | |
| 1503 | + if sub.track >= self.subtitles.len() { | |
| 1504 | + return Err(format!( | |
| 1505 | + "it names subtitle track {} of {}", | |
| 1506 | + sub.track, | |
| 1507 | + self.subtitles.len() | |
| 1508 | + ) | |
| 1509 | + .into()); | |
| 1510 | + } | |
| 1511 | + let sub = SubClip { start: at, ..sub }; | |
| 1512 | + if at.checked_add(sub.frames).is_none() { | |
| 1513 | + return Err(format!("a placement at frame {at} runs past the last frame there is").into()); | |
| 1514 | + } | |
| 1515 | + if let Some(other) = self.lanes[i] | |
| 1516 | + .subs | |
| 1517 | + .iter() | |
| 1518 | + .find(|o| o.end() > sub.start && o.start < sub.end()) | |
| 1519 | + { | |
| 1520 | + return Err(format!( | |
| 1521 | + "the {} subtitle at frame {} already covers [{}, {}): move it, or place this one \ | |
| 1522 | + on another subtitle track", | |
| 1523 | + lane.label(), | |
| 1524 | + other.start, | |
| 1525 | + other.start, | |
| 1526 | + other.end() | |
| 1527 | + ) | |
| 1528 | + .into()); | |
| 1529 | + } | |
| 1530 | + self.snapshot(); | |
| 1531 | + let subs = &mut self.lanes[i].subs; | |
| 1532 | + let idx = subs.partition_point(|o| o.start < sub.start); | |
| 1533 | + subs.insert(idx, sub); | |
| 1534 | + debug_assert!(subs_sorted_disjoint(subs)); | |
| 1535 | + Ok(()) | |
| 1536 | + } | |
| 1537 | + | |
| 1538 | + /// Move the subtitle at `idx` of `from` onto `to` with its head at `start` | |
| 1539 | + /// -- [`move_clip`](Project::move_clip)'s twin, and refusing an overlap | |
| 1540 | + /// where that one clamps to it, for [`place_sub`](Project::place_sub)'s | |
| 1541 | + /// reason: there is no group to hold a subtitle still and no picture under | |
| 1542 | + /// it that a landing would hide, so "as far as it goes" would silently be a | |
| 1543 | + /// frame nobody named. | |
| 1544 | + /// | |
| 1545 | + /// The window travels untouched: a caption dragged later says the same | |
| 1546 | + /// words later, exactly as a clip dragged later plays the same pictures | |
| 1547 | + /// later. | |
| 1548 | + /// | |
| 1549 | + /// One undo step on success; nothing changed and no step on a refusal -- | |
| 1550 | + /// an index that is not there, a lane that is not a subtitle lane or is not | |
| 1551 | + /// there, an overlap, and a drop that changes neither lane nor frame. | |
| 1552 | + pub fn move_sub(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> crate::Result<()> { | |
| 1553 | + let Some(sub) = self.sub_lane(from).get(idx).copied() else { | |
| 1554 | + return Err(format!("there is no subtitle {idx} on {}", from.label()).into()); | |
| 1555 | + }; | |
| 1556 | + if to.kind != LaneKind::Subtitle { | |
| 1557 | + return Err(format!( | |
| 1558 | + "{} is not a subtitle track: a caption cannot play on it", | |
| 1559 | + to.label() | |
| 1560 | + ) | |
| 1561 | + .into()); | |
| 1562 | + } | |
| 1563 | + let (Some(here), Some(dest)) = (self.index(from), self.index(to)) else { | |
| 1564 | + return Err(format!("there is no {} to move onto", to.label()).into()); | |
| 1565 | + }; | |
| 1566 | + if (here, start) == (dest, sub.start) { | |
| 1567 | + return Err("that subtitle is already there".into()); | |
| 1568 | + } | |
| 1569 | + if start.checked_add(sub.frames).is_none() { | |
| 1570 | + return Err( | |
| 1571 | + format!("a placement at frame {start} runs past the last frame there is").into(), | |
| 1572 | + ); | |
| 1573 | + } | |
| 1574 | + let moved = SubClip { start, ..sub }; | |
| 1575 | + if let Some(other) = self.lanes[dest] | |
| 1576 | + .subs | |
| 1577 | + .iter() | |
| 1578 | + .enumerate() | |
| 1579 | + .find(|&(j, o)| { | |
| 1580 | + !(dest == here && j == idx) && o.end() > moved.start && o.start < moved.end() | |
| 1581 | + }) | |
| 1582 | + .map(|(_, o)| *o) | |
| 1583 | + { | |
| 1584 | + return Err(format!( | |
| 1585 | + "the {} subtitle at frame {} already covers [{}, {})", | |
| 1586 | + to.label(), | |
| 1587 | + other.start, | |
| 1588 | + other.start, | |
| 1589 | + other.end() | |
| 1590 | + ) | |
| 1591 | + .into()); | |
| 1592 | + } | |
| 1593 | + self.snapshot(); | |
| 1594 | + self.lanes[here].subs.remove(idx); | |
| 1595 | + let subs = &mut self.lanes[dest].subs; | |
| 1596 | + let at = subs.partition_point(|o| o.start < moved.start); | |
| 1597 | + subs.insert(at, moved); | |
| 1598 | + debug_assert!(subs_sorted_disjoint(subs)); | |
| 1599 | + debug_assert!(subs_sorted_disjoint(&self.lanes[here].subs)); | |
| 1600 | + Ok(()) | |
| 1601 | + } | |
| 1602 | + | |
| 1603 | + /// Move one `edge` of the subtitle at `idx` of `lane` to timeline frame | |
| 1604 | + /// `to` -- [`trim`](Project::trim)'s twin, and clamped like it: a hand | |
| 1605 | + /// pulling an edge past what is legal means "as far as it goes". | |
| 1606 | + /// | |
| 1607 | + /// The window follows the edge, in the seconds the frames are worth at | |
| 1608 | + /// `fps`: a head pulled in starts the caption later *in its track* (the | |
| 1609 | + /// cues that were skipped stay skipped, exactly as a trimmed clip's frames | |
| 1610 | + /// stay behind), and a tail pulled out shows more of it. `fps` is the one | |
| 1611 | + /// thing a [`Project`] does not know -- it is the caller's timeline rate, | |
| 1612 | + /// the same one [`crate::export::timeline_cues`] is asked with -- and it is | |
| 1613 | + /// needed here and nowhere else in this file, because a trim is the only | |
| 1614 | + /// edit that changes a *duration*. | |
| 1615 | + /// | |
| 1616 | + /// The walls: one frame always survives, an edge never crosses the | |
| 1617 | + /// neighbouring subtitle on its own lane, a head never walks back past the | |
| 1618 | + /// track's own start (`in_us` of 0), and a tail never runs past the last | |
| 1619 | + /// cue the track has -- which is the source length a media trim needs a | |
| 1620 | + /// caller's table for, and which this one can simply read. | |
| 1621 | + /// | |
| 1622 | + /// `Err`, with nothing changed and no undo step, for an index that is not | |
| 1623 | + /// there, an unusable `fps`, and an edge already where it was asked to go. | |
| 1624 | + pub fn trim_sub( | |
| 1625 | + &mut self, | |
| 1626 | + lane: Lane, | |
| 1627 | + idx: usize, | |
| 1628 | + edge: Edge, | |
| 1629 | + to: u32, | |
| 1630 | + fps: f64, | |
| 1631 | + ) -> crate::Result<()> { | |
| 1632 | + if !(fps.is_finite() && fps > 0.0) { | |
| 1633 | + return Err(format!("{fps} is not a frame rate to trim against").into()); | |
| 1634 | + } | |
| 1635 | + let Some((lo, hi)) = self.trim_sub_room(lane, idx, edge, fps) else { | |
| 1636 | + return Err(format!("there is no subtitle {idx} on {}", lane.label()).into()); | |
| 1637 | + }; | |
| 1638 | + let Some(sub) = self.sub_lane(lane).get(idx).copied() else { | |
| 1639 | + return Err(format!("there is no subtitle {idx} on {}", lane.label()).into()); | |
| 1640 | + }; | |
| 1641 | + let to = to.clamp(lo, hi); | |
| 1642 | + let at = match edge { | |
| 1643 | + Edge::Start => sub.start, | |
| 1644 | + Edge::End => sub.end(), | |
| 1645 | + }; | |
| 1646 | + if to == at { | |
| 1647 | + return Err(format!( | |
| 1648 | + "that edge is at frame {at} already -- it cannot go past frames {lo}..{hi}" | |
| 1649 | + ) | |
| 1650 | + .into()); | |
| 1651 | + } | |
| 1652 | + self.snapshot(); | |
| 1653 | + let i = self.index(lane).expect("the subtitle was found on it"); | |
| 1654 | + let s = &mut self.lanes[i].subs[idx]; | |
| 1655 | + match edge { | |
| 1656 | + // The words that stay are the words that were there, so the window | |
| 1657 | + // moves with the head -- a trim, not a slip. | |
| 1658 | + Edge::Start => { | |
| 1659 | + // Clamped to a window that is still a window: the walls above | |
| 1660 | + // are frames and the rounding between the two clocks is not | |
| 1661 | + // exact, and neither is a caller's hand-built placement. | |
| 1662 | + s.in_us = (s.in_us + us_of(i64::from(to) - i64::from(s.start), fps)) | |
| 1663 | + .clamp(0, s.out_us - 1); | |
| 1664 | + s.frames = s.end() - to; | |
| 1665 | + s.start = to; | |
| 1666 | + } | |
| 1667 | + Edge::End => { | |
| 1668 | + s.out_us = | |
| 1669 | + (s.out_us + us_of(i64::from(to) - i64::from(s.end()), fps)).max(s.in_us + 1); | |
| 1670 | + s.frames = to - s.start; | |
| 1671 | + } | |
| 1672 | + } | |
| 1673 | + debug_assert!(s.frames >= 1 && s.out_us > s.in_us && s.in_us >= 0); | |
| 1674 | + debug_assert!(subs_sorted_disjoint(&self.lanes[i].subs)); | |
| 1675 | + Ok(()) | |
| 1676 | + } | |
| 1677 | + | |
| 1678 | + /// How far that edge may travel, `(first, last)` timeline frame inclusive | |
| 1679 | + /// -- [`trim_room`](Project::trim_room)'s twin, and what a front-end | |
| 1680 | + /// drawing the box during a drag asks. `None` for an index that is not | |
| 1681 | + /// there. | |
| 1682 | + pub fn trim_sub_room( | |
| 1683 | + &self, | |
| 1684 | + lane: Lane, | |
| 1685 | + idx: usize, | |
| 1686 | + edge: Edge, | |
| 1687 | + fps: f64, | |
| 1688 | + ) -> Option<(u32, u32)> { | |
| 1689 | + let subs = self.sub_lane(lane); | |
| 1690 | + let s = *subs.get(idx)?; | |
| 1691 | + // How many timeline frames a stretch of the track is worth, which is | |
| 1692 | + // what turns its two walls -- its own start, and its last cue -- into | |
| 1693 | + // frames. A rate that is not a rate leaves the window where it is. | |
| 1694 | + let frames_of = |us: i64| match fps.is_finite() && fps > 0.0 { | |
| 1695 | + true => ((us as f64) * fps / 1e6).round().max(0.0).min(f64::from(u32::MAX)) as u32, | |
| 1696 | + false => 0, | |
| 1697 | + }; | |
| 1698 | + Some(match edge { | |
| 1699 | + Edge::Start => ( | |
| 1700 | + s.start | |
| 1701 | + .saturating_sub(frames_of(s.in_us)) | |
| 1702 | + .max(idx.checked_sub(1).map_or(0, |p| subs[p].end())), | |
| 1703 | + s.end() - 1, | |
| 1704 | + ), | |
| 1705 | + Edge::End => { | |
| 1706 | + // Out to the end of the track it reads, and never over the | |
| 1707 | + // subtitle behind it. A track the palette no longer has (or one | |
| 1708 | + // with no cues at all) may not grow, exactly as a source with no | |
| 1709 | + // entry in a media trim's table may not. | |
| 1710 | + let track_end = self | |
| 1711 | + .subtitles | |
| 1712 | + .get(s.track) | |
| 1713 | + .and_then(|t| t.cues.iter().map(|c| c.end_us).max()) | |
| 1714 | + .unwrap_or(s.out_us); | |
| 1715 | + ( | |
| 1716 | + s.start + 1, | |
| 1717 | + s.start | |
| 1718 | + .saturating_add(frames_of(track_end - s.in_us).max(1)) | |
| 1719 | + .min(subs.get(idx + 1).map_or(u32::MAX, |n| n.start)), | |
| 1720 | + ) | |
| 1721 | + } | |
| 1722 | + }) | |
| 1723 | + } | |
| 1724 | + | |
| 1725 | + /// Take the subtitle at `idx` off `lane`, leaving a gap and moving nothing | |
| 1726 | + /// else -- [`lift`](Project::lift)'s twin, and one undo step like it. | |
| 1727 | + /// `false` for an index that is not there (which a lane that is not a | |
| 1728 | + /// subtitle lane always is). | |
| 1729 | + pub fn lift_sub(&mut self, lane: Lane, idx: usize) -> bool { | |
| 1730 | + let Some(i) = self.index(lane).filter(|_| idx < self.sub_lane(lane).len()) else { | |
| 1731 | + return false; | |
| 1732 | + }; | |
| 1733 | + self.snapshot(); | |
| 1734 | + self.lanes[i].subs.remove(idx); | |
| 1735 | + true | |
| 1736 | + } | |
| 1737 | + | |
| 1738 | + /// What `lane` shows, as cues on the **timeline's** clock: every placement's | |
| 1739 | + /// window of its track, clipped to that window and shifted to where the | |
| 1740 | + /// placement sits. The map [`crate::export::timeline_cues`] is for a track | |
| 1741 | + /// carried through the picture's spans -- this is its twin for a track that | |
| 1742 | + /// is placed on a lane of its own, and it is what a preview draws and what | |
| 1743 | + /// an export of the lane would write. | |
| 1744 | + /// | |
| 1745 | + /// Cut, not stretched: a cue half inside the window keeps the half that is | |
| 1746 | + /// inside, and a cue wholly outside it is gone -- the same rule the media | |
| 1747 | + /// spans follow. `fps` is the timeline's rate, needed here because a | |
| 1748 | + /// placement's *position* is in frames while its words are in microseconds. | |
| 1749 | + /// | |
| 1750 | + /// Pure and cheap enough to ask per repaint (a walk of the placements and | |
| 1751 | + /// of their cues, no file opened), which is how a front-end asks it. | |
| 1752 | + pub fn sub_lane_cues(&self, lane: Lane, fps: f64) -> Vec<SubtitleCue> { | |
| 1753 | + if !(fps.is_finite() && fps > 0.0) { | |
| 1754 | + return Vec::new(); | |
| 1755 | + } | |
| 1756 | + let mut out = Vec::new(); | |
| 1757 | + for s in self.sub_lane(lane) { | |
| 1758 | + let Some(track) = self.subtitles.get(s.track) else { | |
| 1759 | + // A placement whose track was taken off the palette | |
| 1760 | + // ([`Project::remove_subtitles`]) shows nothing rather than | |
| 1761 | + // showing another track's words. | |
| 1762 | + continue; | |
| 1763 | + }; | |
| 1764 | + // Where the placement's own first microsecond lands on the | |
| 1765 | + // timeline; every cue in the window is that far along from it. | |
| 1766 | + let onto = |t: i64| (f64::from(s.start) / fps * 1e6).round() as i64 + (t - s.in_us); | |
| 1767 | + for cue in &track.cues { | |
| 1768 | + let (a, b) = (cue.start_us.max(s.in_us), cue.end_us.min(s.out_us)); | |
| 1769 | + if b <= a { | |
| 1770 | + continue; // wholly outside the window this placement keeps | |
| 1771 | + } | |
| 1772 | + out.push(SubtitleCue { | |
| 1773 | + start_us: onto(a), | |
| 1774 | + end_us: onto(b), | |
| 1775 | + text: cue.text.clone(), | |
| 1776 | + image: cue.image.clone(), | |
| 1777 | + }); | |
| 1778 | + } | |
| 1779 | + } | |
| 1780 | + // The placements come in timeline order, but two lanes' worth (or a | |
| 1781 | + // track whose cues a window reordered) do not. | |
| 1782 | + out.sort_by_key(|cue| cue.start_us); | |
| 1783 | + out | |
| 1784 | + } | |
| 1785 | + | |
| 1290 | 1786 | /// What each of [`audio_segments_from`](Project::audio_segments_from)'s |
| 1291 | 1787 | /// lanes is multiplied by on its way into the mix: the same lanes in the |
| 1292 | 1788 | /// same order, as *amplitudes*, so the mixer never sees a decibel. `1.0` |
| @@ -1587,15 +2083,34 @@ impl Project { | ||
| 1587 | 2083 | clip.out_frame = in_frame + len; |
| 1588 | 2084 | cursor = clip.end(); |
| 1589 | 2085 | } |
| 2086 | + // A subtitle's *window* is in microseconds and a rate change moves | |
| 2087 | + // no seconds, so only where it sits is remapped -- through the same | |
| 2088 | + // [`Rate::timeline_at`] every clip boundary goes through, so a | |
| 2089 | + // caption still starts on the frame its picture does. | |
| 2090 | + let mut cursor = 0; | |
| 2091 | + for sub in &mut lane.subs { | |
| 2092 | + let start = k.timeline_at(sub.start).max(cursor); | |
| 2093 | + sub.frames = k.timeline_at(sub.end()).saturating_sub(start).max(1); | |
| 2094 | + sub.start = start; | |
| 2095 | + cursor = sub.end(); | |
| 2096 | + } | |
| 1590 | 2097 | } |
| 1591 | 2098 | } |
| 1592 | 2099 | |
| 1593 | 2100 | /// Length of the timeline in frames: where the *last* lane runs out. A lane |
| 1594 | 2101 | /// that ends early is a trailing gap in that lane, not a shorter timeline. |
| 2102 | + /// | |
| 2103 | + /// A subtitle lane counts like any other: a caption placed past the last | |
| 2104 | + /// picture holds the timeline open under it, exactly as an `A2` running | |
| 2105 | + /// past `V1` does, rather than being silently clipped away by the length | |
| 2106 | + /// ([`crate::export::timeline_cues`] clips cues to this). | |
| 1595 | 2107 | pub fn timeline_frames(&self) -> u32 { |
| 1596 | 2108 | self.lanes |
| 1597 | 2109 | .iter() |
| 1598 | - .filter_map(|l| l.clips.last().map(Clip::end)) | |
| 2110 | + .filter_map(|l| match l.kind { | |
| 2111 | + LaneKind::Subtitle => l.subs.last().map(SubClip::end), | |
| 2112 | + _ => l.clips.last().map(Clip::end), | |
| 2113 | + }) | |
| 1599 | 2114 | .max() |
| 1600 | 2115 | .unwrap_or(0) |
| 1601 | 2116 | } |
| @@ -1822,12 +2337,34 @@ impl Project { | ||
| 1822 | 2337 | .flatten() |
| 1823 | 2338 | }) |
| 1824 | 2339 | .collect(); |
| 1825 | - if cut.iter().all(Option::is_none) { | |
| 2340 | + // The same question on the subtitle lanes, in their own arithmetic: a | |
| 2341 | + // razor cuts the captions with the picture, and a frame where only a | |
| 2342 | + // caption can be cut is still a cut. | |
| 2343 | + let cut_subs: Vec<bool> = self | |
| 2344 | + .lanes | |
| 2345 | + .iter() | |
| 2346 | + .enumerate() | |
| 2347 | + .map(|(i, l)| { | |
| 2348 | + on.contains(&i) && l.subs.iter().any(|s| s.start < timeline_frame && timeline_frame < s.end()) | |
| 2349 | + }) | |
| 2350 | + .collect(); | |
| 2351 | + if cut.iter().all(Option::is_none) && !cut_subs.contains(&true) { | |
| 1826 | 2352 | return false; |
| 1827 | 2353 | } |
| 1828 | 2354 | if snapshot { |
| 1829 | 2355 | self.snapshot(); |
| 1830 | 2356 | } |
| 2357 | + for (data, _) in self | |
| 2358 | + .lanes | |
| 2359 | + .iter_mut() | |
| 2360 | + .zip(&cut_subs) | |
| 2361 | + .filter(|&(_, &cut)| cut) | |
| 2362 | + { | |
| 2363 | + // A split is room of no width: the halves keep their windows by | |
| 2364 | + // proportion and nothing moves. | |
| 2365 | + sub_open_room(&mut data.subs, timeline_frame, 0); | |
| 2366 | + debug_assert!(subs_sorted_disjoint(&data.subs)); | |
| 2367 | + } | |
| 1831 | 2368 | // Each side is its own question: two clips may start together and end |
| 1832 | 2369 | // apart, and every lane whose halves do line up stays one take. |
| 1833 | 2370 | let starts: Vec<Option<u32>> = cut.iter().map(|c| c.map(|c| c.start)).collect(); |
| @@ -1879,12 +2416,24 @@ impl Project { | ||
| 1879 | 2416 | .map(|idx| (l.clips[idx].start, l.clips[idx + 1].end())) |
| 1880 | 2417 | }) |
| 1881 | 2418 | .collect(); |
| 1882 | - if joined.iter().all(Option::is_none) { | |
| 2419 | + // ...and the subtitle lanes' own halves, by [`sub_joinable`]'s rule. | |
| 2420 | + let join_subs: Vec<Option<usize>> = self | |
| 2421 | + .lanes | |
| 2422 | + .iter() | |
| 2423 | + .map(|l| sub_joinable(&l.subs, timeline_frame)) | |
| 2424 | + .collect(); | |
| 2425 | + if joined.iter().all(Option::is_none) && join_subs.iter().all(Option::is_none) { | |
| 1883 | 2426 | return false; |
| 1884 | 2427 | } |
| 1885 | 2428 | self.snapshot(); |
| 1886 | 2429 | let ids = self.group_ids(&joined); |
| 1887 | - for (data, link) in self.lanes.iter_mut().zip(ids) { | |
| 2430 | + for ((data, link), sub) in self.lanes.iter_mut().zip(ids).zip(join_subs) { | |
| 2431 | + if let Some(idx) = sub { | |
| 2432 | + data.subs[idx].frames += data.subs[idx + 1].frames; | |
| 2433 | + data.subs[idx].out_us = data.subs[idx + 1].out_us; | |
| 2434 | + data.subs.remove(idx + 1); | |
| 2435 | + debug_assert!(subs_sorted_disjoint(&data.subs)); | |
| 2436 | + } | |
| 1888 | 2437 | let Some(idx) = joinable(&data.clips, timeline_frame) else { |
| 1889 | 2438 | continue; |
| 1890 | 2439 | }; |
| @@ -2019,9 +2568,14 @@ impl Project { | ||
| 2019 | 2568 | /// another lane to be grouped *with*; [`Project::regroup`] is how clips |
| 2020 | 2569 | /// become a group again. |
| 2021 | 2570 | /// |
| 2022 | - /// Refused for an empty `clip` and for a lane that is not there. | |
| 2571 | + /// Refused for an empty `clip`, for a lane that is not there, and for a | |
| 2572 | + /// subtitle lane, which carries no picture and no sound | |
| 2573 | + /// ([`Project::place_sub`] is its door). | |
| 2023 | 2574 | pub fn place(&mut self, lane: Lane, timeline_frame: u32, clip: Clip) -> bool { |
| 2024 | - if clip.out_frame <= clip.in_frame || self.index(lane).is_none() { | |
| 2575 | + if clip.out_frame <= clip.in_frame | |
| 2576 | + || lane.kind == LaneKind::Subtitle | |
| 2577 | + || self.index(lane).is_none() | |
| 2578 | + { | |
| 2025 | 2579 | return false; |
| 2026 | 2580 | } |
| 2027 | 2581 | self.snapshot(); |
| @@ -2463,6 +3017,11 @@ impl Project { | ||
| 2463 | 3017 | .iter() |
| 2464 | 3018 | .flat_map(|l| &l.clips) |
| 2465 | 3019 | .any(|c| c.end() > at && c.end().checked_add(len).is_none()) |
| 3020 | + || self | |
| 3021 | + .lanes | |
| 3022 | + .iter() | |
| 3023 | + .flat_map(|l| &l.subs) | |
| 3024 | + .any(|s| s.end() > at && s.end().checked_add(len).is_none()) | |
| 2466 | 3025 | { |
| 2467 | 3026 | return false; |
| 2468 | 3027 | } |
| @@ -2500,6 +3059,11 @@ impl Project { | ||
| 2500 | 3059 | .collect(); |
| 2501 | 3060 | for (i, data) in self.lanes.iter_mut().enumerate() { |
| 2502 | 3061 | open_room(&mut data.clips, at, len); |
| 3062 | + // The room is opened on the subtitle lanes too, and for the reason | |
| 3063 | + // it is opened on the lanes the paste does not land on: a caption | |
| 3064 | + // that stayed put while the picture under it moved on is two things | |
| 3065 | + // that no longer say the same thing. | |
| 3066 | + sub_open_room(&mut data.subs, at, len); | |
| 2503 | 3067 | if takes.contains(&i) { |
| 2504 | 3068 | let idx = data.clips.partition_point(|c| c.start < at); |
| 2505 | 3069 | data.clips.insert(idx, clip); |
| @@ -3039,7 +3603,7 @@ impl Project { | ||
| 3039 | 3603 | /// The handle of every lane, in storage order -- the one definition of what |
| 3040 | 3604 | /// [`Lane::ord`] counts. |
| 3041 | 3605 | fn handles(lanes: &[LaneData]) -> Vec<Lane> { |
| 3042 | - let (mut video, mut audio) = (0, 0); | |
| 3606 | + let (mut video, mut audio, mut subtitle) = (0, 0, 0); | |
| 3043 | 3607 | lanes |
| 3044 | 3608 | .iter() |
| 3045 | 3609 | .map(|l| match l.kind { |
| @@ -3051,6 +3615,10 @@ fn handles(lanes: &[LaneData]) -> Vec<Lane> { | ||
| 3051 | 3615 | audio += 1; |
| 3052 | 3616 | Lane::new(LaneKind::Audio, audio - 1) |
| 3053 | 3617 | } |
| 3618 | + LaneKind::Subtitle => { | |
| 3619 | + subtitle += 1; | |
| 3620 | + Lane::new(LaneKind::Subtitle, subtitle - 1) | |
| 3621 | + } | |
| 3054 | 3622 | }) |
| 3055 | 3623 | .collect() |
| 3056 | 3624 | } |
| @@ -3137,6 +3705,94 @@ fn sorted_disjoint(clips: &[Clip]) -> bool { | ||
| 3137 | 3705 | && clips.windows(2).all(|w| w[0].end() <= w[1].start) |
| 3138 | 3706 | } |
| 3139 | 3707 | |
| 3708 | +/// The very same invariant on a subtitle lane, in that lane's own two units: | |
| 3709 | +/// sorted by `start`, no two placements overlapping, no empty placement at | |
| 3710 | +/// either end. Asserted after every mutation, exactly as its twin is. | |
| 3711 | +fn subs_sorted_disjoint(subs: &[SubClip]) -> bool { | |
| 3712 | + subs.iter() | |
| 3713 | + .all(|s| s.frames >= 1 && s.out_us > s.in_us && s.in_us >= 0) | |
| 3714 | + && subs.windows(2).all(|w| w[0].end() <= w[1].start) | |
| 3715 | +} | |
| 3716 | + | |
| 3717 | +/// What `frames` timeline frames are worth in microseconds at `fps`, signed -- | |
| 3718 | +/// the one conversion between a lane's clock and a cue's, and `0` for a rate | |
| 3719 | +/// that is not one (every caller of this has already refused such a rate). | |
| 3720 | +fn us_of(frames: i64, fps: f64) -> i64 { | |
| 3721 | + match fps.is_finite() && fps > 0.0 { | |
| 3722 | + true => ((frames as f64) / fps * 1e6).round() as i64, | |
| 3723 | + false => 0, | |
| 3724 | + } | |
| 3725 | +} | |
| 3726 | + | |
| 3727 | +/// [`clear`] for a subtitle lane: removes the timeline frames `[start, end)`, | |
| 3728 | +/// dropping what is inside the hole, splitting what straddles it and trimming | |
| 3729 | +/// what overlaps an edge. The windows follow the cut by proportion | |
| 3730 | +/// ([`SubClip::window_at`]), so what is left says exactly the words it said. | |
| 3731 | +fn sub_clear(subs: &mut Vec<SubClip>, start: u32, end: u32) { | |
| 3732 | + let mut out = Vec::with_capacity(subs.len() + 1); | |
| 3733 | + for s in subs.drain(..) { | |
| 3734 | + if s.end() <= start || s.start >= end { | |
| 3735 | + out.push(s); | |
| 3736 | + continue; | |
| 3737 | + } | |
| 3738 | + if s.start < start { | |
| 3739 | + out.push(SubClip { | |
| 3740 | + frames: start - s.start, | |
| 3741 | + out_us: s.window_at(start), | |
| 3742 | + ..s | |
| 3743 | + }); | |
| 3744 | + } | |
| 3745 | + if s.end() > end { | |
| 3746 | + out.push(SubClip { | |
| 3747 | + start: end, | |
| 3748 | + frames: s.end() - end, | |
| 3749 | + in_us: s.window_at(end), | |
| 3750 | + ..s | |
| 3751 | + }); | |
| 3752 | + } | |
| 3753 | + } | |
| 3754 | + *subs = out; | |
| 3755 | +} | |
| 3756 | + | |
| 3757 | +/// [`open_room`] for a subtitle lane: slides everything from `at` on later by | |
| 3758 | +/// `len`, splitting a placement that straddles `at` so the two halves end up on | |
| 3759 | +/// either side of the hole. A `len` of 0 is that split alone, which is what a | |
| 3760 | +/// [`Project::split`] over a subtitle lane is. | |
| 3761 | +fn sub_open_room(subs: &mut Vec<SubClip>, at: u32, len: u32) { | |
| 3762 | + if let Some(idx) = subs.iter().position(|s| s.start < at && at < s.end()) { | |
| 3763 | + let s = subs[idx]; | |
| 3764 | + let cut = s.window_at(at); | |
| 3765 | + subs[idx] = SubClip { | |
| 3766 | + frames: at - s.start, | |
| 3767 | + out_us: cut, | |
| 3768 | + ..s | |
| 3769 | + }; | |
| 3770 | + subs.insert( | |
| 3771 | + idx + 1, | |
| 3772 | + SubClip { | |
| 3773 | + start: at, | |
| 3774 | + frames: s.end() - at, | |
| 3775 | + in_us: cut, | |
| 3776 | + ..s | |
| 3777 | + }, | |
| 3778 | + ); | |
| 3779 | + } | |
| 3780 | + for s in subs.iter_mut().filter(|s| s.start >= at) { | |
| 3781 | + s.start += len; | |
| 3782 | + } | |
| 3783 | +} | |
| 3784 | + | |
| 3785 | +/// [`joinable`] for a subtitle lane: the first of the two placements a | |
| 3786 | +/// [`Project::regroup`] at `frame` would rejoin. What a split could have | |
| 3787 | +/// produced and nothing else -- they touch on the timeline, they read the same | |
| 3788 | +/// track, and the second one's window carries on where the first's stopped. | |
| 3789 | +fn sub_joinable(subs: &[SubClip], frame: u32) -> Option<usize> { | |
| 3790 | + let idx = subs.iter().position(|s| s.end() == frame)?; | |
| 3791 | + let next = subs.get(idx + 1)?; | |
| 3792 | + (next.start == frame && next.track == subs[idx].track && next.in_us == subs[idx].out_us) | |
| 3793 | + .then_some(idx) | |
| 3794 | +} | |
| 3795 | + | |
| 3140 | 3796 | /// The grouping invariant, checked in release at [`Project::from_parts`] and |
| 3141 | 3797 | /// asserted by the tests after every edit: a link id names **at most one** clip |
| 3142 | 3798 | /// per lane, and every clip carrying it covers the same timeline span -- that is |
| @@ -3251,6 +3907,15 @@ fn ripple(lanes: &mut [LaneData], on: &[usize], at: u32, len: u32) { | ||
| 3251 | 3907 | c.start -= len; |
| 3252 | 3908 | } |
| 3253 | 3909 | debug_assert!(sorted_disjoint(clips)); |
| 3910 | + // The subtitle lanes close the hole with everything else: a caption | |
| 3911 | + // left standing where the picture it belongs to was cut out is the | |
| 3912 | + // desync a lane model exists to make impossible. | |
| 3913 | + let subs = &mut lanes[i].subs; | |
| 3914 | + sub_clear(subs, at, at + len); | |
| 3915 | + for s in subs.iter_mut().filter(|s| s.start >= at) { | |
| 3916 | + s.start -= len; | |
| 3917 | + } | |
| 3918 | + debug_assert!(subs_sorted_disjoint(subs)); | |
| 3254 | 3919 | } |
| 3255 | 3920 | } |
| 3256 | 3921 |
crates/engine/tests/lanes.rsmodified+135 -1
| @@ -14,7 +14,7 @@ use std::sync::Once; | ||
| 14 | 14 | use std::time::{Duration, Instant}; |
| 15 | 15 | |
| 16 | 16 | use engine::export::{ExportSettings, Format}; |
| 17 | -use engine::project::{Clip, LaneKind, Source, Speed}; | |
| 17 | +use engine::project::{Clip, Lane, LaneKind, Source, Speed}; | |
| 18 | 18 | use engine::scale::FitPolicy; |
| 19 | 19 | use engine::scratch::Scratch; |
| 20 | 20 | use engine::{DecodeSession, ExportHandle, PlaybackSession, Project}; |
| @@ -424,3 +424,137 @@ fn one_lane_projects_are_untouched() { | ||
| 424 | 424 | assert_eq!(lists, vec![project.segments_from(0, FPS)]); |
| 425 | 425 | assert_eq!(project.sources(), [Source::new(asset("test_av.mp4"), 0)]); |
| 426 | 426 | } |
| 427 | + | |
| 428 | +/// The burden a third lane kind carries: a subtitle lane is a lane like the | |
| 429 | +/// others everywhere a *lane* is the subject -- it is added, labelled, ordered, | |
| 430 | +/// snapshotted and removed by one code path -- and every path that is about | |
| 431 | +/// media refuses it, by the bounds check it already had or by name. | |
| 432 | +/// | |
| 433 | +/// One test rather than a dozen because the claim is one claim: nothing about | |
| 434 | +/// a caption was threaded through the media machinery. | |
| 435 | +#[test] | |
| 436 | +fn a_subtitle_lane_is_a_peer_and_every_media_path_refuses_it() { | |
| 437 | + use engine::project::{Edge, SubClip}; | |
| 438 | + | |
| 439 | + let mut project = Project::single(asset("test_av.mp4"), TOTAL); | |
| 440 | + let subs = engine::subtitle::open(&asset("test_subs.srt"), None); | |
| 441 | + assert!(project.add_subtitles(&subs), "the palette takes the track"); | |
| 442 | + let s1 = project.add_lane(LaneKind::Subtitle); | |
| 443 | + assert_eq!(project.lanes(), [Lane::V1, Lane::A1, s1], "a peer in the stack"); | |
| 444 | + assert_eq!(s1.label(), "S1"); | |
| 445 | + | |
| 446 | + let caption = SubClip { | |
| 447 | + start: 0, | |
| 448 | + frames: 60, | |
| 449 | + track: 0, | |
| 450 | + in_us: 0, | |
| 451 | + out_us: 2_000_000, | |
| 452 | + }; | |
| 453 | + project.place_sub(s1, 0, caption).expect("a caption goes down"); | |
| 454 | + | |
| 455 | + // Nothing about the *picture* or the *mix* learned about it. | |
| 456 | + assert_eq!( | |
| 457 | + project.composite_spans_from(0), | |
| 458 | + project.spans_from(Lane::V1, 0), | |
| 459 | + "the composite is still V1 alone: a subtitle lane is not a video lane" | |
| 460 | + ); | |
| 461 | + assert_eq!(project.composite_clip_at(0), Some((Lane::V1, 0))); | |
| 462 | + assert_eq!(project.audio_lanes(), [Lane::A1], "and the mix is A1 alone"); | |
| 463 | + assert_eq!(project.audio_gains().len(), 1); | |
| 464 | + assert_eq!( | |
| 465 | + project.lane_gains().len(), | |
| 466 | + 2, | |
| 467 | + "a save's gain list holds the two media lanes, in step with its lane list" | |
| 468 | + ); | |
| 469 | + | |
| 470 | + // The clip-shaped calls: the lane holds no `Clip`, so each refuses by the | |
| 471 | + // check it always made, and none of them costs an undo step. | |
| 472 | + assert!(project.lane(s1).is_empty(), "there is no clip on a subtitle lane"); | |
| 473 | + assert!(!project.set_lane_gain_db(s1, -6.0), "words have no loudness"); | |
| 474 | + assert_eq!(project.lane_gain_db(s1), 0.0); | |
| 475 | + assert!(!project.set_eq(s1, 0, None), "and no equalizer"); | |
| 476 | + assert!(!project.set_color(s1, 0, None), "and no colour grade"); | |
| 477 | + assert!(!project.set_fit(s1, 0, engine::scale::FitPolicy::Fill), "and no fit"); | |
| 478 | + let err = project | |
| 479 | + .set_speed(s1, 0, Speed::from_permille(2000)) | |
| 480 | + .expect_err("and no speed"); | |
| 481 | + assert!(err.to_string().contains("there is no clip 0 on S1"), "{err}"); | |
| 482 | + assert!(!project.trim(s1, 0, Edge::End, 10, &[TOTAL]), "nothing to trim"); | |
| 483 | + assert!(!project.lift(s1, 0), "nothing to lift"); | |
| 484 | + assert!(!project.delete_in(s1, 0), "nothing to delete"); | |
| 485 | + | |
| 486 | + // ...and the three doors that could still have put a picture on one. | |
| 487 | + let clip = *project.lane(Lane::V1).first().expect("V1 holds the film"); | |
| 488 | + assert!(!project.place(s1, 0, clip), "a picture may not be placed on it"); | |
| 489 | + assert!(!project.place_take(s1, 0, clip), "nor a whole take"); | |
| 490 | + assert!(!project.move_clip(Lane::V1, 0, s1, 0), "nor dragged onto it"); | |
| 491 | + project.append_clip(0, 30); | |
| 492 | + assert!( | |
| 493 | + project.lane(s1).is_empty(), | |
| 494 | + "an import lands on the media lanes and skips this one" | |
| 495 | + ); | |
| 496 | + assert!(project.undo(), "the import is one step"); | |
| 497 | + let refused = Project::from_parts( | |
| 498 | + vec![Source::new(asset("test_av.mp4"), 0)], | |
| 499 | + vec![(LaneKind::Subtitle, vec![clip])], | |
| 500 | + Vec::new(), | |
| 501 | + Vec::new(), | |
| 502 | + ) | |
| 503 | + .expect_err("a file may not name one either"); | |
| 504 | + assert!( | |
| 505 | + refused.to_string().contains("S1 is a subtitle track"), | |
| 506 | + "the refusal says which lane and why: {refused}" | |
| 507 | + ); | |
| 508 | + | |
| 509 | + // The lane's own life cycle, which is the machinery it *does* use. | |
| 510 | + let held = project.remove_lane(s1).expect_err("it still holds a caption"); | |
| 511 | + assert!(held.to_string().contains("still holds 1 subtitle(s)"), "{held}"); | |
| 512 | + assert!(project.lift_sub(s1, 0), "the caption comes off"); | |
| 513 | + project.remove_lane(s1).expect("and then so does the lane"); | |
| 514 | + assert_eq!(project.lanes(), [Lane::V1, Lane::A1], "the last one may go"); | |
| 515 | + assert!(project.undo() && project.undo(), "both steps walk back"); | |
| 516 | + assert_eq!(project.sub_lane(s1), [caption], "caption and lane restored"); | |
| 517 | +} | |
| 518 | + | |
| 519 | +/// A rippling paste opens its room on the subtitle lanes too -- the desync a | |
| 520 | +/// lane model exists to prevent: a caption that stayed where it was while the | |
| 521 | +/// picture under it moved on says the wrong words over the wrong frames. | |
| 522 | +#[test] | |
| 523 | +fn a_paste_opens_its_room_under_the_captions_too() { | |
| 524 | + use engine::project::SubClip; | |
| 525 | + | |
| 526 | + let mut project = Project::single(asset("test_av.mp4"), TOTAL); | |
| 527 | + project.add_subtitles(&engine::subtitle::open(&asset("test_subs.srt"), None)); | |
| 528 | + let s1 = project.add_lane(LaneKind::Subtitle); | |
| 529 | + project | |
| 530 | + .place_sub( | |
| 531 | + s1, | |
| 532 | + 0, | |
| 533 | + SubClip { | |
| 534 | + start: 0, | |
| 535 | + frames: 60, | |
| 536 | + track: 0, | |
| 537 | + in_us: 0, | |
| 538 | + out_us: 2_000_000, | |
| 539 | + }, | |
| 540 | + ) | |
| 541 | + .expect("a caption over the first two seconds"); | |
| 542 | + | |
| 543 | + let clip = *project.lane(Lane::V1).first().expect("V1 holds the film"); | |
| 544 | + let pasted = Clip { | |
| 545 | + out_frame: 30, | |
| 546 | + ..clip | |
| 547 | + }; | |
| 548 | + assert!(project.paste(20, pasted), "thirty frames go in at frame 20"); | |
| 549 | + assert_eq!( | |
| 550 | + project | |
| 551 | + .sub_lane(s1) | |
| 552 | + .iter() | |
| 553 | + .map(|s| (s.start, s.frames, s.in_us, s.out_us)) | |
| 554 | + .collect::<Vec<_>>(), | |
| 555 | + [(0, 20, 0, 666_666), (50, 40, 666_666, 2_000_000)], | |
| 556 | + "the caption is split at the insert and its tail moved on with the picture" | |
| 557 | + ); | |
| 558 | + assert!(project.undo(), "and the paste is one step for the lot"); | |
| 559 | + assert_eq!(project.sub_lane(s1).len(), 1); | |
| 560 | +} |
crates/engine/tests/subtitles.rsmodified+313 -0
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | |
| 10 | 10 | use std::path::PathBuf; |
| 11 | 11 | |
| 12 | +use engine::project::{Edge, Lane, LaneKind, SubClip}; | |
| 12 | 13 | use engine::scratch::Scratch; |
| 13 | 14 | use engine::subtitle::{self, Cue}; |
| 14 | 15 | |
| @@ -691,6 +692,318 @@ fn a_track_that_states_its_language_the_modern_way_keeps_it() { | ||
| 691 | 692 | assert_eq!(got, want, "the two files list the same two tracks"); |
| 692 | 693 | } |
| 693 | 694 | |
| 695 | +/// The frame rate every placement test below counts in: at 30 fps a frame is | |
| 696 | +/// 33333 microseconds and a second is 30 frames exactly, so a window and a span | |
| 697 | +/// can be read against each other by eye. | |
| 698 | +const LANE_FPS: f64 = 30.0; | |
| 699 | +/// Never opened -- a `Project` is pure data ([`engine::Project::single`] reads | |
| 700 | +/// no file), so a path that is not there simply dedups by spelling. | |
| 701 | +const FILE: &str = "/nonexistent/subs.mp4"; | |
| 702 | + | |
| 703 | +/// A project with the three-cue `.srt` on its palette and one empty subtitle | |
| 704 | +/// lane: `V1`, `A1`, `S1`, which is the state a `+ S` press leaves. | |
| 705 | +fn with_subtitle_lane() -> (engine::Project, Lane) { | |
| 706 | + let mut project = engine::Project::single(FILE, 150); | |
| 707 | + assert!( | |
| 708 | + project.add_subtitles(&subtitle::open(&data("test_subs.srt"), None)), | |
| 709 | + "the palette takes the track" | |
| 710 | + ); | |
| 711 | + let lane = project.add_lane(LaneKind::Subtitle); | |
| 712 | + assert_eq!(lane, Lane::S1, "the first subtitle lane is S1"); | |
| 713 | + assert_eq!(lane.label(), "S1", "and is written S1"); | |
| 714 | + (project, lane) | |
| 715 | +} | |
| 716 | + | |
| 717 | +/// A placement of the whole of palette track 0, five seconds of it. | |
| 718 | +fn whole(track: usize) -> SubClip { | |
| 719 | + SubClip { | |
| 720 | + start: 0, | |
| 721 | + frames: 150, | |
| 722 | + track, | |
| 723 | + in_us: 0, | |
| 724 | + out_us: 5_000_000, | |
| 725 | + } | |
| 726 | +} | |
| 727 | + | |
| 728 | +/// The lane half of the model: a caption is placed, trimmed at either end, | |
| 729 | +/// moved and taken back -- all of it through the machinery a clip already goes | |
| 730 | +/// through, so an undo is the lane snapshot every other edit pushes. | |
| 731 | +#[test] | |
| 732 | +fn a_caption_is_placed_trimmed_moved_and_undone_on_its_lane() { | |
| 733 | + let (mut project, lane) = with_subtitle_lane(); | |
| 734 | + project.place_sub(lane, 30, whole(0)).expect("it goes down"); | |
| 735 | + assert_eq!( | |
| 736 | + project.sub_lane(lane), | |
| 737 | + [SubClip { | |
| 738 | + start: 30, | |
| 739 | + ..whole(0) | |
| 740 | + }], | |
| 741 | + "placed where it was asked for, window untouched" | |
| 742 | + ); | |
| 743 | + | |
| 744 | + // The tail in: fewer frames, and the window loses exactly the seconds the | |
| 745 | + // frames did (90 frames of 30 fps is 3s, so [0, 2s) is what is left). | |
| 746 | + project | |
| 747 | + .trim_sub(lane, 0, Edge::End, 90, LANE_FPS) | |
| 748 | + .expect("the tail comes in"); | |
| 749 | + assert_eq!( | |
| 750 | + project.sub_lane(lane)[0], | |
| 751 | + SubClip { | |
| 752 | + start: 30, | |
| 753 | + frames: 60, | |
| 754 | + track: 0, | |
| 755 | + in_us: 0, | |
| 756 | + out_us: 2_000_000, | |
| 757 | + } | |
| 758 | + ); | |
| 759 | + | |
| 760 | + // ...and the head, which moves the window's *start* with it: the words that | |
| 761 | + // stay are the words that were there. | |
| 762 | + project | |
| 763 | + .trim_sub(lane, 0, Edge::Start, 45, LANE_FPS) | |
| 764 | + .expect("the head comes in"); | |
| 765 | + assert_eq!( | |
| 766 | + project.sub_lane(lane)[0], | |
| 767 | + SubClip { | |
| 768 | + start: 45, | |
| 769 | + frames: 45, | |
| 770 | + track: 0, | |
| 771 | + in_us: 500_000, | |
| 772 | + out_us: 2_000_000, | |
| 773 | + } | |
| 774 | + ); | |
| 775 | + | |
| 776 | + // A move is `start` and nothing else -- the same words, later. | |
| 777 | + project | |
| 778 | + .move_sub(lane, 0, lane, 300) | |
| 779 | + .expect("it slides down the lane"); | |
| 780 | + assert_eq!( | |
| 781 | + project.sub_lane(lane)[0], | |
| 782 | + SubClip { | |
| 783 | + start: 300, | |
| 784 | + frames: 45, | |
| 785 | + track: 0, | |
| 786 | + in_us: 500_000, | |
| 787 | + out_us: 2_000_000, | |
| 788 | + } | |
| 789 | + ); | |
| 790 | + | |
| 791 | + // ...and onto another subtitle track, which is the same call: a drag across | |
| 792 | + // lanes is where a second caption row is *for*. | |
| 793 | + let s2 = project.add_lane(LaneKind::Subtitle); | |
| 794 | + project.move_sub(lane, 0, s2, 300).expect("it changes lane"); | |
| 795 | + assert!(project.sub_lane(lane).is_empty(), "off the first row"); | |
| 796 | + assert_eq!(project.sub_lane(s2).len(), 1, "and onto the second"); | |
| 797 | + assert!(project.undo() && project.undo(), "the move and the added lane"); | |
| 798 | + | |
| 799 | + // Every one of the four was one undo step, and the snapshots are the lane | |
| 800 | + // list's own: subtitles joined the history without a history of their own. | |
| 801 | + for want in [ | |
| 802 | + SubClip { | |
| 803 | + start: 45, | |
| 804 | + frames: 45, | |
| 805 | + track: 0, | |
| 806 | + in_us: 500_000, | |
| 807 | + out_us: 2_000_000, | |
| 808 | + }, | |
| 809 | + SubClip { | |
| 810 | + start: 30, | |
| 811 | + frames: 60, | |
| 812 | + track: 0, | |
| 813 | + in_us: 0, | |
| 814 | + out_us: 2_000_000, | |
| 815 | + }, | |
| 816 | + SubClip { | |
| 817 | + start: 30, | |
| 818 | + ..whole(0) | |
| 819 | + }, | |
| 820 | + ] { | |
| 821 | + assert!(project.undo(), "there is a step to walk back"); | |
| 822 | + assert_eq!(project.sub_lane(lane)[0], want); | |
| 823 | + } | |
| 824 | + assert!(project.undo(), "and the placement itself"); | |
| 825 | + assert!( | |
| 826 | + project.sub_lane(lane).is_empty(), | |
| 827 | + "which leaves the lane as it was added" | |
| 828 | + ); | |
| 829 | +} | |
| 830 | + | |
| 831 | +/// Two captions may not cover one frame of one lane, and every refusal says so | |
| 832 | +/// in words -- the one place this model *differs* from | |
| 833 | +/// [`engine::Project::place`], which overwrites what it lands on. A refusal | |
| 834 | +/// costs no undo step. | |
| 835 | +#[test] | |
| 836 | +fn two_captions_may_not_cover_one_frame_and_the_refusal_says_which() { | |
| 837 | + let (mut project, lane) = with_subtitle_lane(); | |
| 838 | + project | |
| 839 | + .place_sub(lane, 30, SubClip { frames: 60, ..whole(0) }) | |
| 840 | + .expect("the first goes down"); | |
| 841 | + let err = project | |
| 842 | + .place_sub(lane, 60, SubClip { frames: 60, ..whole(0) }) | |
| 843 | + .expect_err("the second lands inside it"); | |
| 844 | + assert!( | |
| 845 | + err.to_string().contains("already covers [30, 90)"), | |
| 846 | + "the refusal names the one in the way: {err}" | |
| 847 | + ); | |
| 848 | + | |
| 849 | + project | |
| 850 | + .place_sub(lane, 200, SubClip { frames: 60, ..whole(0) }) | |
| 851 | + .expect("clear of it, it goes down"); | |
| 852 | + let err = project | |
| 853 | + .move_sub(lane, 0, lane, 150) | |
| 854 | + .expect_err("and a drag onto it is refused too"); | |
| 855 | + assert!( | |
| 856 | + err.to_string().contains("already covers [200, 260)"), | |
| 857 | + "by the same words: {err}" | |
| 858 | + ); | |
| 859 | + | |
| 860 | + // Neither refusal moved anything, and neither cost a step: one undo takes | |
| 861 | + // the *second placement* back, not a refusal. | |
| 862 | + assert_eq!( | |
| 863 | + project.sub_lane(lane).iter().map(|s| s.start).collect::<Vec<_>>(), | |
| 864 | + [30, 200] | |
| 865 | + ); | |
| 866 | + assert!(project.undo()); | |
| 867 | + assert_eq!( | |
| 868 | + project.sub_lane(lane).iter().map(|s| s.start).collect::<Vec<_>>(), | |
| 869 | + [30] | |
| 870 | + ); | |
| 871 | + | |
| 872 | + // The other refusals of the door, each in its own words. | |
| 873 | + for (lane, at, sub, want) in [ | |
| 874 | + (Lane::V1, 0, whole(0), "not a subtitle track"), | |
| 875 | + (Lane::new(LaneKind::Subtitle, 4), 0, whole(0), "there is no S5"), | |
| 876 | + (Lane::S1, 0, SubClip { frames: 0, ..whole(0) }, "is empty"), | |
| 877 | + (Lane::S1, 0, SubClip { track: 7, ..whole(0) }, "subtitle track 7 of 1"), | |
| 878 | + ] { | |
| 879 | + let err = project.place_sub(lane, at, sub).expect_err("refused"); | |
| 880 | + assert!( | |
| 881 | + err.to_string().contains(want), | |
| 882 | + "{want:?} is what it says, not {err:?}" | |
| 883 | + ); | |
| 884 | + } | |
| 885 | +} | |
| 886 | + | |
| 887 | +/// The mapping: what a lane *shows* is its placements' windows of their track, | |
| 888 | +/// cut to the window and shifted to where the placement sits -- the twin of | |
| 889 | +/// `export::timeline_cues` for a track that is placed rather than carried. | |
| 890 | +#[test] | |
| 891 | +fn a_lane_maps_to_cues_clipped_to_the_window_and_shifted_onto_the_timeline() { | |
| 892 | + let (mut project, lane) = with_subtitle_lane(); | |
| 893 | + // Two seconds of the track, placed two seconds in: the first cue and no | |
| 894 | + // more -- the second starts at exactly 2s, which the half-open window ends | |
| 895 | + // at. | |
| 896 | + project | |
| 897 | + .place_sub( | |
| 898 | + lane, | |
| 899 | + 60, | |
| 900 | + SubClip { | |
| 901 | + frames: 60, | |
| 902 | + out_us: 2_000_000, | |
| 903 | + ..whole(0) | |
| 904 | + }, | |
| 905 | + ) | |
| 906 | + .expect("it goes down"); | |
| 907 | + let cues = project.sub_lane_cues(lane, LANE_FPS); | |
| 908 | + assert_eq!(cues.len(), 1, "one cue is inside that window"); | |
| 909 | + assert_eq!( | |
| 910 | + (cues[0].start_us, cues[0].end_us, &*cues[0].text), | |
| 911 | + (2_500_000, 3_500_000, "first line"), | |
| 912 | + "shifted by the two seconds the placement sits at" | |
| 913 | + ); | |
| 914 | + | |
| 915 | + // A window that starts mid-cue keeps the half that is inside it, exactly as | |
| 916 | + // a cut clip keeps the frames that are. | |
| 917 | + project.undo(); | |
| 918 | + project | |
| 919 | + .place_sub( | |
| 920 | + lane, | |
| 921 | + 0, | |
| 922 | + SubClip { | |
| 923 | + frames: 60, | |
| 924 | + in_us: 1_000_000, | |
| 925 | + out_us: 3_000_000, | |
| 926 | + ..whole(0) | |
| 927 | + }, | |
| 928 | + ) | |
| 929 | + .expect("it goes down"); | |
| 930 | + let cues = project.sub_lane_cues(lane, LANE_FPS); | |
| 931 | + assert_eq!( | |
| 932 | + cues.iter() | |
| 933 | + .map(|c| (c.start_us, c.end_us, c.text.clone())) | |
| 934 | + .collect::<Vec<_>>(), | |
| 935 | + [ | |
| 936 | + (0, 500_000, "first line".to_string()), | |
| 937 | + (1_000_000, 2_000_000, "second line\nwith a break".to_string()), | |
| 938 | + ], | |
| 939 | + "both cues clipped to the window and shifted to the placement" | |
| 940 | + ); | |
| 941 | + | |
| 942 | + // A placement whose track has been taken off the palette shows nothing -- | |
| 943 | + // never another track's words. | |
| 944 | + project.remove_subtitles(0).expect("the row comes off"); | |
| 945 | + assert!( | |
| 946 | + project.sub_lane_cues(lane, LANE_FPS).is_empty(), | |
| 947 | + "a placement with no track left shows nothing" | |
| 948 | + ); | |
| 949 | + assert!( | |
| 950 | + project.sub_lane_cues(lane, 0.0).is_empty(), | |
| 951 | + "and a rate that is not a rate maps nothing" | |
| 952 | + ); | |
| 953 | +} | |
| 954 | + | |
| 955 | +/// The razor and the ripple, which is the whole reason a caption is a lane and | |
| 956 | +/// not a setting: a cut through the picture cuts the words over it, and the | |
| 957 | +/// windows follow by proportion -- no frame rate anywhere in that arithmetic. | |
| 958 | +#[test] | |
| 959 | +fn a_caption_is_cut_and_rippled_with_the_picture_under_it() { | |
| 960 | + let (mut project, lane) = with_subtitle_lane(); | |
| 961 | + project | |
| 962 | + .place_sub( | |
| 963 | + lane, | |
| 964 | + 0, | |
| 965 | + SubClip { | |
| 966 | + frames: 60, | |
| 967 | + out_us: 2_000_000, | |
| 968 | + ..whole(0) | |
| 969 | + }, | |
| 970 | + ) | |
| 971 | + .expect("it goes down"); | |
| 972 | + | |
| 973 | + // The razor: halves that add up to the whole, in frames and in microseconds. | |
| 974 | + assert!(project.split(30), "the cut lands on the picture and the words"); | |
| 975 | + assert_eq!( | |
| 976 | + project | |
| 977 | + .sub_lane(lane) | |
| 978 | + .iter() | |
| 979 | + .map(|s| (s.start, s.frames, s.in_us, s.out_us)) | |
| 980 | + .collect::<Vec<_>>(), | |
| 981 | + [(0, 30, 0, 1_000_000), (30, 30, 1_000_000, 2_000_000)] | |
| 982 | + ); | |
| 983 | + // ...and its inverse puts the caption back exactly as it was. | |
| 984 | + assert!(project.regroup(30), "the halves rejoin"); | |
| 985 | + assert_eq!( | |
| 986 | + project.sub_lane(lane).iter().map(|s| (s.start, s.frames, s.in_us, s.out_us)).collect::<Vec<_>>(), | |
| 987 | + [(0, 60, 0, 2_000_000)] | |
| 988 | + ); | |
| 989 | + | |
| 990 | + // A rippling delete through the middle of it: the hole closes, what was | |
| 991 | + // inside it is gone from the words as well as from the picture, and the two | |
| 992 | + // halves keep exactly the seconds they kept frames for. | |
| 993 | + assert!(project.ripple_delete(20, 20), "the hole closes on every lane"); | |
| 994 | + assert_eq!( | |
| 995 | + project | |
| 996 | + .sub_lane(lane) | |
| 997 | + .iter() | |
| 998 | + .map(|s| (s.start, s.frames, s.in_us, s.out_us)) | |
| 999 | + .collect::<Vec<_>>(), | |
| 1000 | + [(0, 20, 0, 666_666), (20, 20, 1_333_333, 2_000_000)], | |
| 1001 | + "cut by proportion of the window, not by a frame rate" | |
| 1002 | + ); | |
| 1003 | + assert!(project.undo(), "and the whole ripple is one step"); | |
| 1004 | + assert_eq!(project.sub_lane(lane).len(), 1); | |
| 1005 | +} | |
| 1006 | + | |
| 694 | 1007 | /// An EBML element: its id as it is written in the file, then an 8-byte |
| 695 | 1008 | /// length, which is always a legal encoding of one. |
| 696 | 1009 | fn element(id: &[u8], body: &[u8]) -> Vec<u8> { |