forge
Explore
Log in

Don't you dare go Hollow

A region's caption pieces re-time by boundaries, forced apart

Browse files at this commit

3a0448f7a2018c441504e0be13a5571faa3f3113Ahmet Kaan GümüşParent: b801cf5
  • fix
    • a region's caption pieces re-time by boundaries, forced apart
Show full message
🐛 fix(timeline): a region's caption pieces re-time by boundaries, forced apart

Rounding each in-region caption piece's start and length independently
could land two pieces on the same frame (two one-frame cues of a
thirty-frame region at 2x both want frame 15) and let the region's last
cue run past where its first follower lands -- a debug-assert panic,
and in release a silent overlap the lane invariant forbids.

The pieces now re-time by their mapped boundaries walked in order: each
piece at least one frame, none past the region's own new end, and the
first caption behind the region lands exactly on it. A rounding
artifact is clamped rather than refused -- nothing about the words
asked for an overlap.
crates/engine/src/project.rsmodified+79 -4
@@ -3956,13 +3956,31 @@ impl Project {
3956 // -- same words on less timeline, its window untouched, for3956 // -- same words on less timeline, its window untouched, for
3957 // [`write_speed`]'s reason -- and everything behind the region3957 // [`write_speed`]'s reason -- and everything behind the region
3958 // slides up by what the region gave back.3958 // slides up by what the region gave back.
3959+ //
3960+ // The pieces inside re-time by their *boundaries*, not each
3961+ // on its own: rounding one piece's start and its neighbour's
3962+ // start apart can land both on the same frame (two one-frame
3963+ // cues of a thirty-frame region at 2x both want frame 15), so
3964+ // the mapped boundaries are walked in order and forced apart
3965+ // -- each piece at least one frame, none past the region's own
3966+ // new end, where the first follower lands exactly. A rounding
3967+ // artifact is clamped, not refused: nothing about the words
3968+ // asked for an overlap.
3959 let share = f64::from(after) / f64::from(len).max(1.);3969 let share = f64::from(after) / f64::from(len).max(1.);
3960- for s in &mut self.lanes[l].subs {3970+ let map = |bound: u32| at + (f64::from(bound - at) * share).round() as u32;
3971+ let mut prev = at;
3972+ let subs = &mut self.lanes[l].subs;
3973+ for i in 0..subs.len() {
3974+ let s = subs[i];
3961 if s.start >= at + len {3975 if s.start >= at + len {
3962- s.start -= delta;3976+ subs[i].start = s.start - delta;
3963 } else if s.start >= at && s.end() <= at + len {3977 } else if s.start >= at && s.end() <= at + len {
3964- s.start = at + (f64::from(s.start - at) * share).round() as u32;3978+ let mut start = map(s.start).max(prev);
3965- s.frames = (f64::from(s.frames) * share).round().max(1.) as u32;3979+ let end = map(s.end()).max(start + 1).min(at + after);
3980+ start = start.min(end - 1);
3981+ subs[i].start = start;
3982+ subs[i].frames = end - start;
3983+ prev = end;
3966 }3984 }
3967 }3985 }
3968 debug_assert!(subs_sorted_disjoint(&self.lanes[l].subs));3986 debug_assert!(subs_sorted_disjoint(&self.lanes[l].subs));
@@ -5086,6 +5104,63 @@ mod tests {
5086 assert!(subs_sorted_disjoint(&p.lanes[p.index(s1).unwrap()].subs));5104 assert!(subs_sorted_disjoint(&p.lanes[p.index(s1).unwrap()].subs));
5087 }5105 }
5088 5106
5107+ /// The region's caption pieces re-time by mapped boundaries forced apart:
5108+ /// rounding start and length independently can land two cues on one frame,
5109+ /// and the artifact is clamped -- one frame each, in order, none past the
5110+ /// region's new end -- never an overlap on the lane.
5111+ #[test]
5112+ fn a_regions_caption_pieces_never_round_onto_one_another() {
5113+ let caption = |start: u32, frames: u32| SubClip {
5114+ start,
5115+ frames,
5116+ track: 0,
5117+ in_us: 0,
5118+ out_us: i64::from(frames) * 1_000_000,
5119+ link: None,
5120+ };
5121+ let track = |n| SubtitleTrack {
5122+ path: FILE.into(),
5123+ track: None,
5124+ language: "eng".into(),
5125+ name: String::new(),
5126+ label: format!("eng{n}"),
5127+ cues: Vec::new(),
5128+ bitmap: false,
5129+ refused: None,
5130+ };
5131+ // Three lanes, one repro shape each: adjacent one-frame cues, a
5132+ // three-frame pair, and the region's last cue against the first
5133+ // caption behind it. The region [0, 30) plays at 2x: 15 frames.
5134+ let mut p = Project::single(FILE, 60).with_subtitles(vec![track(0), track(1), track(2)]);
5135+ let lanes: Vec<Lane> = (0..3).map(|_| p.add_lane(LaneKind::Subtitle)).collect();
5136+ p.place_sub(lanes[0], 9, caption(9, 1)).expect("placed");
5137+ p.place_sub(lanes[0], 10, caption(10, 1)).expect("placed");
5138+ p.place_sub(lanes[1], 5, caption(5, 3)).expect("placed");
5139+ p.place_sub(lanes[1], 8, caption(8, 3)).expect("placed");
5140+ p.place_sub(lanes[2], 29, caption(29, 1)).expect("placed");
5141+ p.place_sub(lanes[2], 30, caption(30, 15)).expect("placed");
5142+
5143+ let mut scope = vec![Lane::V1, Lane::A1];
5144+ scope.extend(lanes.iter().copied());
5145+ p.speed_regions(&[(0, 30)], Speed::from_permille(2000), &scope)
5146+ .expect("the region speeds up");
5147+ // Adjacent one-frame cues: both map to frame 5, and are forced apart.
5148+ let spans = |p: &Project, lane: Lane| -> Vec<(u32, u32)> {
5149+ p.sub_lane(lane).iter().map(|s| (s.start, s.frames)).collect()
5150+ };
5151+ assert_eq!(spans(&p, lanes[0]), [(5, 1), (6, 1)], "one frame each, in order");
5152+ // The three-frame pair: [5, 8) and [8, 11) map to [3, 5) and [4, 6),
5153+ // and the walk pushes the second to where the first ends.
5154+ assert_eq!(spans(&p, lanes[1]), [(3, 1), (4, 2)], "forced apart, kept in order");
5155+ // The last cue of the region ends exactly where the region now does
5156+ // (frame 15), and the caption behind it lands on that same frame:
5157+ // touching, never overlapping.
5158+ assert_eq!(spans(&p, lanes[2]), [(14, 1), (15, 15)], "the cue ends with the region");
5159+ for &lane in &lanes {
5160+ assert!(subs_sorted_disjoint(&p.lanes[p.index(lane).unwrap()].subs));
5161+ }
5162+ }
5163+
5089 /// The scope law names a caption half too: a scoped cut that would move one5164 /// The scope law names a caption half too: a scoped cut that would move one
5090 // track of a group whose caption sits outside the scope is refused.5165 // track of a group whose caption sits outside the scope is refused.
5091 #[test]5166 #[test]