From 47c08dd67ea60e0f3c529ca262714efc96908e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 19 Aug 2026 17:22:51 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20fix(engine):=20the=20inverse=20a?= =?UTF-8?q?dmits=20its=20hole,=20the=20scale=20law=20lives=20once?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit invert fabricated an answer for the frames a shift-right piece never produces -- the hole between the identity run and the piece's own image -- against its own doc. It answers None there now, and a test walks the hole asserting so. The caption scale law lived twice, one copy clamped and one not, the exact duplication that drifted a frame before: scaled_caption_frame is now the single fn both write_speed and speeded_playhead call. The successor guard saturates instead of overflowing at the clamp ceiling, and the ungrouped-layer arm of whole_take is pinned by its own test again -- a layer is lifted, never rippled. --- crates/app/src/tests/editing.rs | 25 +++++++++++++++++++++++++ crates/engine/src/map.rs | 22 +++++++++++++++++++++- crates/engine/src/project.rs | 24 ++++++++++++++---------- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/crates/app/src/tests/editing.rs b/crates/app/src/tests/editing.rs index d305fc9..49974ac 100644 --- a/crates/app/src/tests/editing.rs +++ b/crates/app/src/tests/editing.rs @@ -479,6 +479,31 @@ fn a_caption_grouped_with_a_clip_pairs_it_for_the_whole_take_question() { ); } +/// An ungrouped clip on a further lane (ord >= 1) is a layer, not a take: it +/// sits over the timeline rather than in it, so deleting it lifts a hole +/// rather than rippling one closed. Pins the `(_, 1..) => false` arm +/// `whole_take` answers before it ever reaches the video/audio arms below. +#[test] +fn an_ungrouped_clip_on_a_further_lane_is_a_layer_not_a_take() { + use engine::project::LaneKind; + + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + session.set_gain(0.0); + let v2 = session.add_lane(LaneKind::Video); + assert!(session.move_clip_to(Lane::V1, 0, v2, 0), "V1 -> V2"); + // Its old pairing with A1 came along with the move -- detach it, an + // ungrouped clip is exactly the case this arm is for. + assert!(session.ungroup(v2, 0)); + assert!( + !whole_take(&session, v2, 0), + "an ungrouped clip on ord >= 1 is a layer, not a take" + ); + // ...and its delete lifts, leaving the hole under it open: the take + // beneath (there is none here, but the lane) is untouched. + assert!(session.lift_clip(v2, 0)); + assert!(session.lane_clips(v2).is_empty()); +} + /// A caption anchor names the media half of its group: the video member for /// the cards that are about the picture, the audio member for the ones about /// the sound. A caption in no group names nothing -- which is what the cards' diff --git a/crates/engine/src/map.rs b/crates/engine/src/map.rs index 61865fb..f72ed56 100644 --- a/crates/engine/src/map.rs +++ b/crates/engine/src/map.rs @@ -119,8 +119,14 @@ impl TimelineMap { if frame < self.knots[0].0.min(self.knots[0].1) { return Some(frame); } + // Between old_lo and new_lo of an unanchored (shift-right / plain + // expansion) piece, `apply` never lands: old frames below old_lo + // stay identity (image 0..old_lo) and the piece's own image starts + // at new_lo > old_lo, leaving [old_lo, new_lo) a true hole. A + // shift-left's anchor knot ties old==new here, so this window is + // empty for it and the branch is never live. if frame < self.knots[0].1 { - return Some(lerp((0, 0), self.knots[0], frame)); + return None; } let mut prev = self.knots[0]; for &(o, n) in &self.knots { @@ -250,6 +256,20 @@ mod tests { assert_piece_laws((100, 200), (100, 100), 500); } + /// The hole a shift-right/expansion piece leaves in its own image: new + /// frames between old_lo and new_lo have no old frame that lands on + /// them, so `invert` must say so instead of fabricating one. + #[test] + fn invert_is_none_inside_a_pieces_own_hole() { + let m = TimelineMap::piece((30, 60), (45, 90)); + for f in 30..45 { + assert_eq!(m.invert(f), None, "new frame {f} has no preimage"); + } + // Just outside the hole, both ends still answer. + assert_eq!(m.invert(29), Some(29)); + assert_eq!(m.invert(45), Some(30)); + } + /// Identity is the base: everything maps to itself, in both directions, /// and the timeline's length never moved. #[test] diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 6c1381a..b11442d 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -2252,15 +2252,11 @@ impl Project { // separately, which is what let the two answers drift apart by a // frame. let (held_old, new) = (held.speed.as_f64(), speed.as_f64()); - let scale = |f: u32| { - (f64::from(held.start) + (f64::from(f) - f64::from(held.start)) * held_old / new) - .round() - .clamp(0., f64::from(u32::MAX)) as u32 - }; + let scale = |f: u32| scaled_caption_frame(held.start, held_old, new, f); let retimed = |s: SubClip| { let map = TimelineMap::piece((s.start, s.end()), (scale(s.start), scale(s.end()))); let start = map.apply(s.start); - let end = map.apply(s.end()).max(start + 1); + let end = map.apply(s.end()).max(start.saturating_add(1)); SubClip { start, frames: end - start, ..s } }; for &(l, i) in &members.clips { @@ -2363,10 +2359,7 @@ impl Project { // the held clip's proportion -- the offset the group keeps. None => { let s = self.lanes[l].subs.get(i)?; - let held_start = f64::from(held.start); - let scale = |f: u32| { - (held_start + (f64::from(f) - held_start) * held_old / new).round() as u32 - }; + let scale = |f: u32| scaled_caption_frame(held.start, held_old, new, f); ( (s.start, s.end()), TimelineMap::piece((s.start, s.end()), (scale(s.start), scale(s.end()))), @@ -4463,6 +4456,17 @@ fn handles(lanes: &[LaneData]) -> Vec { .collect() } +/// The one law a caption re-time scales by: frame `f` moves about the held +/// clip's own start by the held clip's old-to-new rate ratio. [`write_speed`] +/// commits it, [`Project::speeded_playhead`] previews it -- both build their +/// [`TimelineMap`] piece from this single answer so the two cannot drift +/// apart by a frame the way they once did. +fn scaled_caption_frame(held_start: u32, held_old: f64, new: f64, f: u32) -> u32 { + (f64::from(held_start) + (f64::from(f) - f64::from(held_start)) * held_old / new) + .round() + .clamp(0., f64::from(u32::MAX)) as u32 +} + /// Which source frame a clip plays at `timeline_frame`: its in-point plus the /// offset *at the clip's own rate*. The one place the two frame spaces meet, so /// a speed cannot be applied twice or forgotten in one of them.