forge
Explore
Log in

Don't you dare go Hollow

Close subtitle lane gaps

Browse files at this commit

45b9182af3a5df169073d70edebedab7295f995dAhmet Kaan GümüşParent: e78c741
  • feat
    • close subtitle lane gaps
Show full message
feat: close subtitle lane gaps
crates/engine/src/playback.rsmodified+10 -8
@@ -2031,14 +2031,15 @@ impl PlaybackSession {
2031 }2031 }
2032 2032
2033 /// The empty stretch of `lane` covering `frame`, or `None` when `frame`2033 /// The empty stretch of `lane` covering `frame`, or `None` when `frame`
2034- /// is inside a clip or past the last one ([`Project::gap_at`]). What a2034+ /// is inside a clip/caption placement or past the last one
2035- /// right-click on empty bench space asks before offering to close it.2035+ /// ([`Project::gap_at`]). What a right-click on empty bench space asks
2036+ /// before offering to close it.
2036 pub fn gap_at(&self, lane: Lane, frame: u32) -> Option<(u32, u32)> {2037 pub fn gap_at(&self, lane: Lane, frame: u32) -> Option<(u32, u32)> {
2037 self.project.gap_at(lane, frame)2038 self.project.gap_at(lane, frame)
2038 }2039 }
2039 2040
2040 /// How many bounded gaps that same right-click lane holds. The tail after2041 /// How many bounded gaps that same right-click lane holds. The tail after
2041- /// the last clip is not counted, because nothing would ripple there.2042+ /// the last placement is not counted, because nothing would ripple there.
2042 pub fn gap_count(&self, lane: Lane) -> usize {2043 pub fn gap_count(&self, lane: Lane) -> usize {
2043 self.project.gap_count(lane)2044 self.project.gap_count(lane)
2044 }2045 }
@@ -2459,16 +2460,17 @@ impl PlaybackSession {
2459 }2460 }
2460 2461
2461 /// The lanes closing the gap `(start, frames)` on `lane` must ripple2462 /// The lanes closing the gap `(start, frames)` on `lane` must ripple
2462- /// together to keep every take crossing it in sync2463+ /// together to keep every media take crossing it in sync
2463 /// ([`Project::gap_take_scope`]) -- what a right-click on empty bench2464 /// ([`Project::gap_take_scope`]) -- what a right-click on empty bench
2464- /// space asks before offering to close it.2465+ /// space asks before offering to close it. Subtitle gaps stay on their own
2466+ /// lane because captions are not A/V take halves.
2465 pub fn gap_take_scope(&self, lane: Lane, start: u32, frames: u32) -> crate::Result<Vec<Lane>> {2467 pub fn gap_take_scope(&self, lane: Lane, start: u32, frames: u32) -> crate::Result<Vec<Lane>> {
2466 self.project.gap_take_scope(lane, start, frames)2468 self.project.gap_take_scope(lane, start, frames)
2467 }2469 }
2468 2470
2469- /// Closes every bounded gap on one lane, keeping each linked take whole when2471+ /// Closes every bounded gap on one lane. Media lanes keep linked takes
2470- /// its other half has the matching empty stretch and reporting the gaps that2472+ /// whole when the other half has the matching empty stretch and report the
2471- /// were not safe to close. One undo step for every gap that closed.2473+ /// gaps that were not safe to close; subtitle lanes ripple captions only.
2472 pub fn close_all_gaps_on_lane(&mut self, lane: Lane) -> crate::Result<GapSweep> {2474 pub fn close_all_gaps_on_lane(&mut self, lane: Lane) -> crate::Result<GapSweep> {
2473 let report = self.project.close_all_gaps_on_lane(lane)?;2475 let report = self.project.close_all_gaps_on_lane(lane)?;
2474 if report.closed > 0 {2476 if report.closed > 0 {
crates/engine/src/project.rsmodified+221 -33
@@ -4198,18 +4198,29 @@ impl Project {
4198 4198
4199 /// The empty stretch of `lane` covering `frame`, as `(start, len)` -- what4199 /// The empty stretch of `lane` covering `frame`, as `(start, len)` -- what
4200 /// a right-click on empty bench space names before offering to close it.4200 /// a right-click on empty bench space names before offering to close it.
4201- /// `None` when `frame` sits inside a clip, or past the last one: the open4201+ /// `None` when `frame` sits inside a placement, or past the last one: the
4202- /// end of a lane is not a gap, since there is nothing after it to slide4202+ /// open end of a lane is not a gap, since there is nothing after it to slide
4203- /// back ([`gap`]).4203+ /// back ([`gap`]). Subtitle lanes are asked of their [`SubClip`] list, not
4204+ /// the media [`Clip`] list they deliberately never hold.
4204 pub fn gap_at(&self, lane: Lane, frame: u32) -> Option<(u32, u32)> {4205 pub fn gap_at(&self, lane: Lane, frame: u32) -> Option<(u32, u32)> {
4205- gap(self.lane(lane), frame)4206+ let data = self.index(lane).map(|i| &self.lanes[i])?;
4207+ match data.kind {
4208+ LaneKind::Subtitle => gap(&data.subs, frame),
4209+ LaneKind::Video | LaneKind::Audio => gap(&data.clips, frame),
4210+ }
4206 }4211 }
4207 4212
4208- /// How many bounded holes `lane` holds. The open end after the last clip is4213+ /// How many bounded holes `lane` holds. The open end after the last
4209- /// not counted, like [`gap_at`](Self::gap_at), because closing it would move4214+ /// placement is not counted, like [`gap_at`](Self::gap_at), because closing
4210- /// nothing.4215+ /// it would move nothing.
4211 pub fn gap_count(&self, lane: Lane) -> usize {4216 pub fn gap_count(&self, lane: Lane) -> usize {
4212- gaps(self.lane(lane)).len()4217+ let Some(data) = self.index(lane).map(|i| &self.lanes[i]) else {
4218+ return 0;
4219+ };
4220+ match data.kind {
4221+ LaneKind::Subtitle => gaps(&data.subs).len(),
4222+ LaneKind::Video | LaneKind::Audio => gaps(&data.clips).len(),
4223+ }
4213 }4224 }
4214 4225
4215 /// The lanes closing the gap `(start, frames)` on `lane` must ripple4226 /// The lanes closing the gap `(start, frames)` on `lane` must ripple
@@ -4234,6 +4245,11 @@ impl Project {
4234 ///4245 ///
4235 /// `Ok(vec![lane])` alone for a gap that borders no take, or none whose4246 /// `Ok(vec![lane])` alone for a gap that borders no take, or none whose
4236 /// partner clip sits on another lane -- the unlinked case, untouched.4247 /// partner clip sits on another lane -- the unlinked case, untouched.
4248+ ///
4249+ /// Subtitle lanes have no media take halves to inspect. A caption can be
4250+ /// hand-grouped with clips, but it is not the imported A/V take this method
4251+ /// widens for, so a subtitle gap stays scoped to its own lane; the later
4252+ /// scoped cut still refuses an explicit group split.
4237 pub fn gap_take_scope(&self, lane: Lane, start: u32, frames: u32) -> crate::Result<Vec<Lane>> {4253 pub fn gap_take_scope(&self, lane: Lane, start: u32, frames: u32) -> crate::Result<Vec<Lane>> {
4238 let Some(li) = self.index(lane) else {4254 let Some(li) = self.index(lane) else {
4239 return Err("no track to work on".into());4255 return Err("no track to work on".into());
@@ -4281,10 +4297,13 @@ impl Project {
4281 }4297 }
4282 4298
4283 /// Close every bounded gap on `lane`, as one undo step, without making the4299 /// Close every bounded gap on `lane`, as one undo step, without making the
4284- /// lane's scope global. Each gap uses the same take-safety widening4300+ /// lane's scope global. Media lanes use the same take-safety widening
4285 /// [`gap_take_scope`](Self::gap_take_scope) gives a single close: linked4301 /// [`gap_take_scope`](Self::gap_take_scope) gives a single close: linked
4286 /// halves ride along only when their matching lane is empty over that exact4302 /// halves ride along only when their matching lane is empty over that exact
4287- /// stretch; mismatches are skipped and reported.4303+ /// stretch; mismatches are skipped and reported. Subtitle lanes close their
4304+ /// own [`SubClip`] list only: captions may be hand-grouped, but they are not
4305+ /// halves of an imported A/V take, so the media take-widening rule has
4306+ /// nothing to add there.
4288 ///4307 ///
4289 /// The walk is back-to-front. Closing a gap shifts everything after it, so4308 /// The walk is back-to-front. Closing a gap shifts everything after it, so
4290 /// later holes must be consumed while their measured starts are still true;4309 /// later holes must be consumed while their measured starts are still true;
@@ -4292,9 +4311,12 @@ impl Project {
4292 /// only before the first successful close, so an all-skipped sweep is not an4311 /// only before the first successful close, so an all-skipped sweep is not an
4293 /// undo step.4312 /// undo step.
4294 pub fn close_all_gaps_on_lane(&mut self, lane: Lane) -> crate::Result<GapSweep> {4313 pub fn close_all_gaps_on_lane(&mut self, lane: Lane) -> crate::Result<GapSweep> {
4295- let Some(_) = self.index(lane) else {4314+ let Some(i) = self.index(lane) else {
4296 return Err("no track to work on".into());4315 return Err("no track to work on".into());
4297 };4316 };
4317+ if self.lanes[i].kind == LaneKind::Subtitle {
4318+ return Ok(self.close_all_subtitle_gaps_on_lane(i));
4319+ }
4298 let mut report = GapSweep::default();4320 let mut report = GapSweep::default();
4299 let mut snapshot = false;4321 let mut snapshot = false;
4300 for (start, frames) in gaps(self.lane(lane)).into_iter().rev() {4322 for (start, frames) in gaps(self.lane(lane)).into_iter().rev() {
@@ -4324,6 +4346,35 @@ impl Project {
4324 Ok(report)4346 Ok(report)
4325 }4347 }
4326 4348
4349+ /// Subtitle lanes have the same bounded-gap sweep as media lanes, but no
4350+ /// take-scope widening: a caption is not one half of an A/V take. Explicit
4351+ /// hand groups still obey [`scope_holds_takes_whole`](Self::scope_holds_takes_whole),
4352+ /// so a grouped caption is refused rather than pulled away from its group.
4353+ fn close_all_subtitle_gaps_on_lane(&mut self, i: usize) -> GapSweep {
4354+ let mut report = GapSweep::default();
4355+ let mut snapshot = false;
4356+ let on = [i];
4357+ for (start, frames) in gaps(&self.lanes[i].subs).into_iter().rev() {
4358+ match self.scope_holds_takes_whole(&on, start) {
4359+ Ok(()) => {
4360+ if !snapshot {
4361+ self.snapshot();
4362+ snapshot = true;
4363+ }
4364+ ripple(&mut self.lanes, &on, start, frames);
4365+ report.closed += 1;
4366+ }
4367+ Err(e) => report.skipped.push(GapSkip {
4368+ start,
4369+ reason: e.to_string(),
4370+ }),
4371+ }
4372+ }
4373+ report.skipped.reverse();
4374+ debug_assert!(links_are_consistent(&self.lanes).is_ok());
4375+ report
4376+ }
4377+
4327 /// Cut **every** one of `regions` -- `(start, len)` in timeline frames --4378 /// Cut **every** one of `regions` -- `(start, len)` in timeline frames --
4328 /// out of the lanes in `scope` and close each hole, as one edit: the4379 /// out of the lanes in `scope` and close each hole, as one edit: the
4329 /// jumpcut a silence scan asks for ([`crate::silence`]).4380 /// jumpcut a silence scan asks for ([`crate::silence`]).
@@ -5036,38 +5087,67 @@ fn source_frame(c: &Clip, timeline_frame: u32) -> u32 {
5036 (c.in_frame + c.speed.source_at(timeline_frame - c.start)).min(c.out_frame - 1)5087 (c.in_frame + c.speed.source_at(timeline_frame - c.start)).min(c.out_frame - 1)
5037 }5088 }
5038 5089
5039-/// Index of the clip covering `frame`, or `None` for a gap or past the end.5090+/// The tiny interface `Clip` and `SubClip` share for gap maths. Keeping the gap
5040-/// Binary search: the sorted-disjoint invariant is what makes it legal.5091+/// finder generic makes the subtitle path use the same bounded-hole rules as
5041-fn at(clips: &[Clip], frame: u32) -> Option<usize> {5092+/// media lanes without teaching the media-specific take logic about captions.
5042- let idx = clips.partition_point(|c| c.start <= frame).checked_sub(1)?;5093+trait GapPlacement {
5043- (frame < clips[idx].end()).then_some(idx)5094+ fn start_frame(&self) -> u32;
5095+ fn end_frame(&self) -> u32;
5096+}
5097+
5098+impl GapPlacement for Clip {
5099+ fn start_frame(&self) -> u32 {
5100+ self.start
5101+ }
5102+
5103+ fn end_frame(&self) -> u32 {
5104+ self.end()
5105+ }
5106+}
5107+
5108+impl GapPlacement for SubClip {
5109+ fn start_frame(&self) -> u32 {
5110+ self.start
5111+ }
5112+
5113+ fn end_frame(&self) -> u32 {
5114+ self.end()
5115+ }
5116+}
5117+
5118+/// Index of the placement covering `frame`, or `None` for a gap or past the
5119+/// end. Binary search: the sorted-disjoint invariant is what makes it legal.
5120+fn at<T: GapPlacement>(items: &[T], frame: u32) -> Option<usize> {
5121+ let idx = items.partition_point(|item| item.start_frame() <= frame).checked_sub(1)?;
5122+ (frame < items[idx].end_frame()).then_some(idx)
5044 }5123 }
5045 5124
5046-/// The empty stretch of `clips` covering `frame`, as `(start, len)` -- `None`5125+/// The empty stretch of `items` covering `frame`, as `(start, len)` -- `None`
5047-/// when `frame` is inside a clip, or past the last one. Sorted-disjoint5126+/// when `frame` is inside a placement, or past the last one. Sorted-disjoint
5048-/// invariant like [`at`]: the first clip whose start is past `frame` is the5127+/// invariant like [`at`]: the first placement whose start is past `frame` is
5049-/// far wall, and the last one that ends at or before `frame` (or the5128+/// the far wall, and the last one that ends at or before `frame` (or the
5050 /// timeline's own head, `0`, when there is none) is the near one.5129 /// timeline's own head, `0`, when there is none) is the near one.
5051-fn gap(clips: &[Clip], frame: u32) -> Option<(u32, u32)> {5130+fn gap<T: GapPlacement>(items: &[T], frame: u32) -> Option<(u32, u32)> {
5052- if at(clips, frame).is_some() {5131+ if at(items, frame).is_some() {
5053 return None;5132 return None;
5054 }5133 }
5055- let idx = clips.partition_point(|c| c.start <= frame);5134+ let idx = items.partition_point(|item| item.start_frame() <= frame);
5056- let next = clips.get(idx)?;5135+ let next = items.get(idx)?;
5057- let start = idx.checked_sub(1).map_or(0, |i| clips[i].end());5136+ let start = idx.checked_sub(1).map_or(0, |i| items[i].end_frame());
5058- Some((start, next.start - start))5137+ Some((start, next.start_frame() - start))
5059 }5138 }
5060 5139
5061-/// Every bounded hole in `clips`, left to right. The tail after the last clip is5140+/// Every bounded hole in `items`, left to right. The tail after the last
5062-/// deliberately absent: there is no later placement for a ripple to bring back.5141+/// placement is deliberately absent: there is no later placement for a ripple
5063-fn gaps(clips: &[Clip]) -> Vec<(u32, u32)> {5142+/// to bring back.
5143+fn gaps<T: GapPlacement>(items: &[T]) -> Vec<(u32, u32)> {
5064 let mut out = Vec::new();5144 let mut out = Vec::new();
5065 let mut next = 0;5145 let mut next = 0;
5066- for c in clips {5146+ for item in items {
5067- if c.start > next {5147+ if item.start_frame() > next {
5068- out.push((next, c.start - next));5148+ out.push((next, item.start_frame() - next));
5069 }5149 }
5070- next = c.end();5150+ next = item.end_frame();
5071 }5151 }
5072 out5152 out
5073 }5153 }
@@ -5504,6 +5584,33 @@ mod tests {
5504 }5584 }
5505 }5585 }
5506 5586
5587+ /// A caption placement for pure timeline tests: the track window is the
5588+ /// same duration as the placement, so assertions can stay in frames.
5589+ fn caption(start: u32, frames: u32) -> SubClip {
5590+ SubClip {
5591+ start,
5592+ frames,
5593+ track: 0,
5594+ in_us: 0,
5595+ out_us: i64::from(frames) * 1_000_000,
5596+ link: None,
5597+ }
5598+ }
5599+
5600+ /// A subtitle palette row for tests that place captions on lanes.
5601+ fn subtitle_track() -> SubtitleTrack {
5602+ SubtitleTrack {
5603+ path: FILE.into(),
5604+ track: None,
5605+ language: "eng".into(),
5606+ name: String::new(),
5607+ label: "eng".into(),
5608+ cues: Vec::new(),
5609+ bitmap: false,
5610+ refused: None,
5611+ }
5612+ }
5613+
5507 /// A recognisable equalizer setting: one peak band, `n` telling two of them5614 /// A recognisable equalizer setting: one peak band, `n` telling two of them
5508 /// apart -- which is what makes the table's dedup and its prune visible.5615 /// apart -- which is what makes the table's dedup and its prune visible.
5509 fn band_at(n: u32) -> EqParams {5616 fn band_at(n: u32) -> EqParams {
@@ -7956,6 +8063,87 @@ mod tests {
7956 assert_eq!(shape(&p), before);8063 assert_eq!(shape(&p), before);
7957 }8064 }
7958 8065
8066+ /// Subtitle lanes use the same bounded-hole hit test as clip lanes, against
8067+ /// their own caption storage: head gaps and gaps between cues count, an
8068+ /// empty lane and the tail after the last cue do not.
8069+ #[test]
8070+ fn gap_at_finds_the_hole_a_caption_frame_sits_in() {
8071+ let mut p = Project::single(FILE, 20).with_subtitles(vec![subtitle_track()]);
8072+ let s = p.add_lane(LaneKind::Subtitle);
8073+ assert_eq!(p.gap_at(s, 0), None, "an empty subtitle lane has nothing to close");
8074+
8075+ p.place_sub(s, 5, caption(0, 3)).expect("single cue at [5,8)");
8076+ assert_eq!(p.gap_at(s, 0), Some((0, 5)), "head gap before first cue");
8077+ assert_eq!(p.gap_at(s, 4), Some((0, 5)));
8078+ assert_eq!(p.gap_at(s, 5), None, "inside the cue");
8079+ assert_eq!(p.gap_at(s, 8), None, "tail after a single cue is not a gap");
8080+
8081+ p.place_sub(s, 12, caption(0, 2)).expect("second cue at [12,14)");
8082+ assert_eq!(p.gap_at(s, 8), Some((8, 4)), "gap between cues");
8083+ assert_eq!(p.gap_at(s, 11), Some((8, 4)));
8084+ assert_eq!(p.gap_at(s, 14), None, "tail after the last cue is still open-ended");
8085+ }
8086+
8087+ /// Closing a caption gap ripples the subtitle lane only. It is not a media
8088+ /// take close, so the video and audio lanes keep their frame positions.
8089+ #[test]
8090+ fn closing_a_caption_gap_ripples_no_video_or_audio_clips() {
8091+ let mut p = Project::single(FILE, 20).with_subtitles(vec![subtitle_track()]);
8092+ let s = p.add_lane(LaneKind::Subtitle);
8093+ p.place_sub(s, 5, caption(0, 3)).expect("single cue at [5,8)");
8094+ let before_clips = shape(&p);
8095+ let before_subs = p.sub_lane(s).to_vec();
8096+
8097+ let (start, len) = p.gap_at(s, 0).expect("gap before the caption");
8098+ assert_eq!((start, len), (0, 5));
8099+ p.cut_regions(&[(start, len)], &[s]).expect("caption lane gap closes");
8100+
8101+ assert_eq!(shape(&p), before_clips, "media lanes did not move");
8102+ assert_eq!(p.sub_lane(s), &[caption(0, 3)], "caption slid left");
8103+ assert!(p.undo(), "one undo restores the caption close");
8104+ assert_eq!(shape(&p), before_clips);
8105+ assert_eq!(p.sub_lane(s), before_subs);
8106+ }
8107+
8108+ /// The subtitle sweep is the clip sweep's bounded-gap arithmetic on
8109+ /// [`SubClip`] storage: right-to-left, one undo, no tail close, and no
8110+ /// history entry for empty or already-contiguous lanes.
8111+ #[test]
8112+ fn close_all_gaps_on_subtitle_lane_closes_bounded_gaps_and_undoes_once() {
8113+ let mut p = Project::single(FILE, 30).with_subtitles(vec![subtitle_track()]);
8114+ let s = p.add_lane(LaneKind::Subtitle);
8115+ let history = p.history.len();
8116+ let report = p.close_all_gaps_on_lane(s).expect("empty subtitle lane exists");
8117+ assert_eq!(report, GapSweep::default(), "empty subtitle lane has no bounded gap");
8118+ assert_eq!(p.history.len(), history, "empty sweep is not an undo step");
8119+
8120+ p.place_sub(s, 2, caption(0, 2)).expect("head gap [0,2)");
8121+ p.place_sub(s, 7, caption(0, 2)).expect("middle gap [4,7)");
8122+ p.place_sub(s, 14, caption(0, 1)).expect("middle gap [9,14), tail after 15");
8123+ let before = p.sub_lane(s).to_vec();
8124+
8125+ assert_eq!(p.gap_count(s), 3);
8126+ let report = p.close_all_gaps_on_lane(s).expect("subtitle lane exists");
8127+ assert_eq!(report.closed, 3);
8128+ assert!(report.skipped.is_empty());
8129+ assert_eq!(
8130+ p.sub_lane(s),
8131+ &[caption(0, 2), caption(2, 2), caption(4, 1)],
8132+ "all bounded caption gaps closed without touching windows"
8133+ );
8134+ assert!(p.undo(), "one undo restores every caption gap");
8135+ assert_eq!(p.sub_lane(s), before);
8136+
8137+ let mut p = Project::single(FILE, 30).with_subtitles(vec![subtitle_track()]);
8138+ let s = p.add_lane(LaneKind::Subtitle);
8139+ p.place_sub(s, 0, caption(0, 3)).expect("contiguous at the head");
8140+ let history = p.history.len();
8141+ let report = p.close_all_gaps_on_lane(s).expect("subtitle lane exists");
8142+ assert_eq!(report, GapSweep::default(), "the open tail is not closed");
8143+ assert_eq!(p.sub_lane(s), &[caption(0, 3)]);
8144+ assert_eq!(p.history.len(), history);
8145+ }
8146+
7959 #[test]8147 #[test]
7960 fn ripple_delete_spans_partial_clips() {8148 fn ripple_delete_spans_partial_clips() {
7961 let mut p = three();8149 let mut p = three();