forge
Explore
Log in

Don't you dare go Hollow

The captions of a re-rate go through the map itself

Browse files at this commit

675eb6fa08b81896ee8b301e2267641276419ea5Ahmet Kaan GümüşParent: 84be3e4
  • fix
    • the captions of a re-rate go through the map itself
Show full message
🐛 fix(engine): the captions of a re-rate go through the map itself

write_speed re-timed a group's captions with its own ratio formula,
round(frames * new/old) -- one frame adrift from the TimelineMap that
speeded_playhead answers with (92 divergent cells over a length x rate
grid; a 90f clip at 2x landed its caption's last frame on 48 while the
playhead was sent to 47). The write now builds the same per-caption map
and takes start and end from apply(), one source of truth.

Two coverage holes closed: the caption arm of speeded_playhead (a
playhead in a caption's overhang past its clip) had never been
exercised; the write-vs-playhead agreement is now pinned over the grid,
exact for compression, containment for expansion where apply() answers
the start of a frame's new block by construction.
crates/engine/src/project.rsmodified+134 -7
@@ -2246,14 +2246,22 @@ impl Project {
2246 let Some(held) = self.lane(lane).get(idx).copied() else {2246 let Some(held) = self.lane(lane).get(idx).copied() else {
2247 return Err(format!("there is no clip {idx} on {}", lane.label()).into());2247 return Err(format!("there is no clip {idx} on {}", lane.label()).into());
2248 };2248 };
2249- let (old, new) = (f64::from(held.frames()), f64::from(speed.frames(held.len())));2249+ // The same per-member piece `speeded_playhead` builds for this op,
2250- let ratio = new / old.max(1.);2250+ // asked here instead of read there -- a caption's length comes from
2251- let retimed = |s: SubClip| SubClip {2251+ // the map's own ends (`end - start`), never `round(len * ratio)`
2252- start: ((f64::from(s.start) - f64::from(held.start)) * ratio + f64::from(held.start))2252+ // separately, which is what let the two answers drift apart by a
2253+ // frame.
2254+ let (held_old, new) = (held.speed.as_f64(), speed.as_f64());
2255+ let scale = |f: u32| {
2256+ (f64::from(held.start) + (f64::from(f) - f64::from(held.start)) * held_old / new)
2253 .round()2257 .round()
2254- .clamp(0., f64::from(u32::MAX)) as u32,2258+ .clamp(0., f64::from(u32::MAX)) as u32
2255- frames: (f64::from(s.frames) * ratio).round().clamp(1., f64::from(u32::MAX)) as u32,2259+ };
2256- ..s2260+ let retimed = |s: SubClip| {
2261+ let map = TimelineMap::piece((s.start, s.end()), (scale(s.start), scale(s.end())));
2262+ let start = map.apply(s.start);
2263+ let end = map.apply(s.end()).max(start + 1);
2264+ SubClip { start, frames: end - start, ..s }
2257 };2265 };
2258 for &(l, i) in &members.clips {2266 for &(l, i) in &members.clips {
2259 let clips = &self.lanes[l].clips;2267 let clips = &self.lanes[l].clips;
@@ -5337,6 +5345,125 @@ mod tests {
5337 assert!(subs_sorted_disjoint(&p.lanes[p.index(s1).unwrap()].subs));5345 assert!(subs_sorted_disjoint(&p.lanes[p.index(s1).unwrap()].subs));
5338 }5346 }
5339 5347
5348+ /// [`Project::speeded_playhead`]'s caption arm fires only when the
5349+ /// playhead is inside a caption member and outside every clip member --
5350+ /// a shape every other fixture avoids by keeping its caption inside the
5351+ /// held clip. Here the caption overhangs the clip's tail, and the
5352+ /// playhead standing in that overhang is answered by the caption's own
5353+ /// piece: it scales about the held clip's head by the held clip's own
5354+ /// proportion, same as [`write_speed`](Project::write_speed) re-times it.
5355+ #[test]
5356+ fn the_caption_arm_answers_a_playhead_in_the_overhang_past_its_clip() {
5357+ let caption = |start: u32, frames: u32| SubClip {
5358+ start,
5359+ frames,
5360+ track: 0,
5361+ in_us: 0,
5362+ out_us: i64::from(frames) * 1_000_000,
5363+ link: None,
5364+ };
5365+ let track = || SubtitleTrack {
5366+ path: FILE.into(),
5367+ track: None,
5368+ language: "eng".into(),
5369+ name: String::new(),
5370+ label: "eng".into(),
5371+ cues: Vec::new(),
5372+ bitmap: false,
5373+ refused: None,
5374+ };
5375+ let mut p = Project::single(FILE, 90).with_subtitles(vec![track()]);
5376+ let s1 = p.add_lane(LaneKind::Subtitle);
5377+ // The caption spans [80, 100) -- ten frames past the clip's own end
5378+ // at 90 -- grouped with the clip so it re-times by the clip's ratio.
5379+ p.place_sub(s1, 80, caption(80, 20)).expect("placed");
5380+ p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped");
5381+ // At 2x the clip halves to 45 frames; the overhang frame 95, which
5382+ // no clip member's old span reaches, is the caption's own question:
5383+ // half of 95 is 47.5, rounded to 48.
5384+ assert_eq!(
5385+ p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), 95),
5386+ Some(48),
5387+ "the overhang frame answers through the caption's own piece"
5388+ );
5389+ }
5390+
5391+ /// `write_speed` and `speeded_playhead` are two questions of the SAME
5392+ /// map ([`map`]'s architecture law): a caption's landed span comes from
5393+ /// the map's own ends, and the playhead standing at the caption's own
5394+ /// last frame before the write has to land exactly where the write put
5395+ /// it -- across clip lengths and rates that round differently, and an
5396+ /// odd-length caption straddling the clip's own tail edge, the corner
5397+ /// where `write_speed` used to answer a frame short.
5398+ #[test]
5399+ fn write_speed_and_speeded_playhead_agree_at_a_captions_own_last_frame() {
5400+ let caption = |start: u32, frames: u32| SubClip {
5401+ start,
5402+ frames,
5403+ track: 0,
5404+ in_us: 0,
5405+ out_us: i64::from(frames) * 1_000_000,
5406+ link: None,
5407+ };
5408+ let track = || SubtitleTrack {
5409+ path: FILE.into(),
5410+ track: None,
5411+ language: "eng".into(),
5412+ name: String::new(),
5413+ label: "eng".into(),
5414+ cues: Vec::new(),
5415+ bitmap: false,
5416+ refused: None,
5417+ };
5418+ for &len in &[90u32, 91, 177, 300, 301] {
5419+ for &permille in &[500u16, 2000, 2500, 3000] {
5420+ let mut p = Project::single(FILE, len).with_subtitles(vec![track()]);
5421+ let s1 = p.add_lane(LaneKind::Subtitle);
5422+ // An odd-length caption straddling the clip's tail edge: its
5423+ // own last frame lands past the held clip.
5424+ let c_start = len.saturating_sub(3);
5425+ p.place_sub(s1, c_start, caption(c_start, 7)).expect("placed");
5426+ p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped");
5427+ let speed = Speed::from_permille(permille);
5428+ let last = c_start + 7 - 1;
5429+ let mapped = p
5430+ .speeded_playhead(Lane::V1, 0, speed, last)
5431+ .expect("the caption is there");
5432+ if p.set_speed(Lane::V1, 0, speed).is_err() {
5433+ continue; // a refusal changes nothing: not this law's cell
5434+ }
5435+ let landed = p.sub_lane(s1)[0];
5436+ if permille >= 1000 {
5437+ // A speed-up compresses the timeline: many old frames
5438+ // share one new frame, so the caption's own last old
5439+ // frame lands flush with the write's own boundary --
5440+ // the exact law [`write_speed`]'s worked drift-fix case
5441+ // is stated against.
5442+ assert_eq!(
5443+ mapped,
5444+ landed.end() - 1,
5445+ "len {len} permille {permille}: the playhead at the caption's own \
5446+ last frame lands where the write actually put it"
5447+ );
5448+ } else {
5449+ // A slow-down expands it: one old frame becomes several
5450+ // new ones, and `apply` answers where that frame's own
5451+ // block *starts*, not where the caption's last new
5452+ // frame (the end of that same block) sits -- a
5453+ // one-frame gap inherent to the piece's own semantics,
5454+ // not a drift between the two callers. Both still have
5455+ // to agree the answer sits inside the caption the write
5456+ // actually landed.
5457+ assert!(
5458+ (landed.start..landed.end()).contains(&mapped),
5459+ "len {len} permille {permille}: the playhead lands inside the \
5460+ caption the write actually put ({landed:?}), got {mapped}"
5461+ );
5462+ }
5463+ }
5464+ }
5465+ }
5466+
5340 /// A grouped caption's cues re-time with its clip: the placement's window5467 /// A grouped caption's cues re-time with its clip: the placement's window
5341 /// crossed the timeline at its own proportion before any rate, and a5468 /// crossed the timeline at its own proportion before any rate, and a
5342 /// re-rate changes that proportion (the frames compress, the window does5469 /// re-rate changes that proportion (the frames compress, the window does