Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Merge remote-tracking branch 'origin/fix/ripple-trim-j-cut-head' into fix/ripple-trim-l-cut-head

Peet Guerciobf00c29b142e
diff --cc crates/edit/src/lib.rs
index 03ca82a9d,2436c73bc..99df7040d
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@@ -677,19 -684,11 +686,20 @@@ pub fn ripple_trim_group(seq: &mut Sequ
for (tid, from, member) in rippling {
let Some(tr) = work.track_mut(tid) else { continue };
let follows = |i: &TrackItem| i.start < from && i.end() > from && i.link.is_some_and(|l| shifting_links.contains(&l));
+ // An L cut: the sound of a clip that ends at the cut runs on into the shortened head. That clip
+ // stays put, so its sound stays with it and the L cut still ends where it did. (Unlinked material
+ // across the cut, a music bed, still refuses, and so does a linked clip whose partner reaches past
+ // the cut.)
+ let stays = |i: &TrackItem| {
+ let cut = from - Tick(1);
+ let mut partners = linked.iter().filter(|p| Some(p.0) == i.link && p.1 != i.id).peekable();
+ edge == Edge::In && i.start < cut && i.link.is_some_and(|l| !own_links.contains(&l)) && partners.peek().is_some() && partners.all(|p| p.2 <= cut)
+ };
if !member && shift < Tick::ZERO {
- let at = if edge == Edge::In { from - Tick(1) } else { from };
- let closing = TimeRange::new(at + shift, -shift);
- if tr.items.iter().any(|i| i.range().overlaps(&closing) && !follows(i) && !stays(i)) {
+ // a shorter head closes the stretch just after the cut, a shorter tail the one before it
+ let closing = if edge == Edge::In { TimeRange::new(from - Tick(1), -shift) } else { TimeRange::new(from + shift, -shift) };
+ let own = |i: &TrackItem| i.link.is_some_and(|l| own_links.contains(&l));
- if tr.items.iter().any(|i| i.range().overlaps(&closing) && !follows(i) && !own(i)) {
++ if tr.items.iter().any(|i| i.range().overlaps(&closing) && !follows(i) && !own(i) && !stays(i)) {
return Err(EditError::SyncLockConflict);
}
}
diff --cc crates/edit/src/tests.rs
index 8ecf088c6,03f2926e9..6f89c2a0e
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@@ -572,55 -572,50 +572,103 @@@ fn hostile_speeds_are_refused_or_clampe
}
}
+#[test]
+fn shorter_head_into_an_l_cut_keeps_the_earlier_sound_where_it_is() {
+ // V1: a [0,10), b [10,20), c [20,30). a's sound on sync-locked A1 runs 3 into b: [0,13) (an L
+ // cut). b loses 3 at its head: a never moves, so its sound stays and the L cut still ends at 13.
+ let mut fx = Fx::new();
+ let (v1, a1) = (fx.v(0), fx.a(0));
+ let a = fx.put(v1, 0, 10, 0);
+ let b = fx.put(v1, 10, 10, 0);
+ fx.put(v1, 20, 10, 0);
+ let a_sound = fx.put(a1, 0, 13, 0);
+ for x in [a, a_sound] {
+ fx.seq.find_item_mut(x).unwrap().1.link = Some(7);
+ }
+ let mut n = fx.next;
+ ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 10), (10, 7), (17, 10)], vec![(0, 13)]));
+ fx.seq.check().unwrap();
+
+ // later material on A1 moves up with the pictures; landing on the L cut's sound refuses
+ let later = fx.put(a1, 15, 5, 0);
+ assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));
+ assert_eq!(fx.spans(a1), vec![(0, 13), (15, 5)], "unchanged on failure");
+ fx.seq.find_item_mut(later).unwrap().1.start = f(20);
+ ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(2), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 10), (10, 5), (15, 10)], vec![(0, 13), (18, 5)]));
+
+ // unlinked material across the cut (a music bed) still refuses
+ let mut fx = Fx::new();
+ let (v1, a1) = (fx.v(0), fx.a(0));
+ fx.put(v1, 0, 10, 0);
+ let b = fx.put(v1, 10, 10, 0);
+ fx.put(a1, 0, 13, 0);
+ let mut n = fx.next;
+ assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));
+ // so does a link with no partner left
+ fx.seq.track_mut(a1).unwrap().items[0].link = Some(9);
+ assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));
+
+ // a linked cutaway across the cut whose own partner reaches past it too is no L cut: it refuses
+ let mut fx = Fx::new();
+ let (v1, v2, a1) = (fx.v(0), fx.v(1), fx.a(0));
+ fx.put(v1, 0, 10, 0);
+ let b = fx.put(v1, 10, 10, 0);
+ fx.put(v1, 20, 10, 0);
+ let x = fx.put(v2, 5, 20, 0);
+ let xs = fx.put(a1, 5, 20, 0);
+ for c in [x, xs] {
+ fx.seq.find_item_mut(c).unwrap().1.link = Some(7);
+ }
+ let mut n = fx.next;
+ assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));
+}
++
+ #[test]
+ fn shorter_head_closes_the_stretch_after_the_cut_on_sync_locked_tracks() {
+ // V1: a [0,10), b [10,20). a's sound on sync-locked A1 runs up to the cut: [0,10). b loses 3 at
+ // its head; the stretch that closes is [10,13), where A1 is empty, so nothing refuses.
+ for group in [false, true] {
+ let mut fx = Fx::new();
+ let (v1, a1) = (fx.v(0), fx.a(0));
+ fx.put(v1, 0, 10, 0);
+ let b = fx.put(v1, 10, 10, 0);
+ fx.put(v1, 20, 10, 0);
+ fx.put(a1, 0, 10, 0);
+ let later = fx.put(a1, 20, 5, 0);
+ let mut n = fx.next;
+ if group {
+ ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)).unwrap();
+ } else {
+ trim(&mut fx.seq, b, Edge::In, TrimMode::Ripple, f(3), &mut Fx::ctx(&mut n)).unwrap();
+ }
+ assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 10), (10, 7), (17, 10)], vec![(0, 10), (17, 5)]), "group {group}");
+ // material inside the stretch that closes still refuses
+ fx.seq.find_item_mut(later).unwrap().1.start = f(11);
+ let r = if group {
+ ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(2), &mut Fx::ctx(&mut n))
+ } else {
+ trim(&mut fx.seq, b, Edge::In, TrimMode::Ripple, f(2), &mut Fx::ctx(&mut n))
+ };
+ assert_eq!(r, Err(EditError::SyncLockConflict), "group {group}");
+ }
+ // a clip on a sync-locked track that starts at the cut stays put while the pictures after it move
+ // up: only the stretch after the cut sees it (the old stretch, before the cut, let it through)
+ for group in [false, true] {
+ let mut fx = Fx::new();
+ let (v1, a1) = (fx.v(0), fx.a(0));
+ fx.put(v1, 0, 10, 0);
+ let b = fx.put(v1, 10, 10, 0);
+ fx.put(v1, 20, 10, 0);
+ fx.put(a1, 10, 5, 0);
+ let mut n = fx.next;
+ let r = if group {
+ ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n))
+ } else {
+ trim(&mut fx.seq, b, Edge::In, TrimMode::Ripple, f(3), &mut Fx::ctx(&mut n))
+ };
+ assert_eq!(r, Err(EditError::SyncLockConflict), "group {group}");
+ assert_eq!(fx.spans(v1), vec![(0, 10), (10, 10), (20, 10)], "unchanged on failure");
+ }
+ }
diff --cc crates/engine/src/split_edit_ripple_trim_tests.rs
index 249f40e9c,d3f362583..c396cbe98
--- a/crates/engine/src/split_edit_ripple_trim_tests.rs
+++ b/crates/engine/src/split_edit_ripple_trim_tests.rs
@@@ -86,30 -86,53 +86,81 @@@ fn ripple_trim_is_refused_when_the_earl
assert_eq!(start(&s, second) - start(&s, sound), rate.tick_of(12));
}
+/// The mirror, an L cut: the first clip's sound runs 6 frames on into the second picture. Shortening
+/// the second clip's head was refused as a sync-lock conflict, because the first clip's sound crossed
+/// the cut. The first clip never moves, so its sound stays and the L cut still ends where it did.
+#[test]
+fn shortening_the_head_after_an_l_cut_keeps_the_earlier_sound_in_place() {
+ let mut s = Session::default();
+ let (first, second, sound) = j_cut(&mut s);
+ let rate = s.sequence_rate();
+ // undo the J cut, then run the first clip's sound (A1) 6 frames past its picture
+ s.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ s.execute("timeline.trim", json!({"clip": sound.0, "edge": "in", "mode": "regular", "deltaFrames": 12})).unwrap();
+ let first_sound = s.active_sequence().unwrap().audio_tracks[0].items[0].id;
+ s.execute("timeline.trim", json!({"clip": first_sound.0, "edge": "out", "mode": "regular", "deltaFrames": 6})).unwrap();
+ s.execute("sequence.linkedSelection", json!({"on": true})).unwrap();
+ let end = |s: &Session, c: ClipId| s.active_sequence().unwrap().find_item(c).map(|(_, i)| i.end()).unwrap();
+ assert_eq!(end(&s, first_sound), start(&s, second) + rate.tick_of(6), "the L cut runs 6 frames into the second picture");
+ let (l_cut_ends, picture_at, picture_ends) = (end(&s, first_sound), start(&s, second), end(&s, second));
+ let before = s.project.clone();
+ s.execute("timeline.trim", json!({"clip": second.0, "edge": "in", "mode": "ripple", "deltaFrames": 3})).unwrap();
+ assert_eq!(end(&s, first_sound), l_cut_ends, "the L cut still ends where it did");
+ assert_eq!((start(&s, first), start(&s, first_sound)), (Tick::ZERO, Tick::ZERO));
+ assert_eq!((start(&s, second), end(&s, second)), (picture_at, picture_ends - rate.tick_of(3)), "the second picture is 3 frames shorter");
+ assert_eq!((start(&s, sound), end(&s, sound)), (start(&s, second), end(&s, second)), "its own sound with it");
+ s.active_sequence().unwrap().check().unwrap();
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!(*s.project, *before, "one undo step puts it back");
+}
++
+ /// A shorter head on the J-cut take itself: the picture and its early sound lose the same stretch
+ /// and the sound still leads by 12. It was refused as a sync-lock conflict because the check looked
+ /// at the stretch before the cut, where the first clip's sound sits on A1, instead of the stretch
+ /// the shorter head takes away.
+ #[test]
+ fn shorter_head_on_a_j_cut_take_shortens_picture_and_sound_together() {
+ shorter_head_on_a_j_cut_take(false);
+ }
+
+ /// The same trim grabbed by the early sound instead of the picture gives the same result.
+ #[test]
+ fn shorter_head_grabbed_by_the_early_sound_gives_the_same_result() {
+ shorter_head_on_a_j_cut_take(true);
+ }
+
+ fn shorter_head_on_a_j_cut_take(grab_sound: bool) {
+ let mut s = Session::default();
+ let (first, second, sound) = j_cut(&mut s);
+ let rate = s.sequence_rate();
+ let len = |s: &Session, c: ClipId| s.active_sequence().unwrap().find_item(c).map(|(_, i)| (i.duration, i.source_in)).unwrap();
+ let (picture_at, sound_at) = (start(&s, second), start(&s, sound));
+ let ((pic_len, pic_in), (snd_len, snd_in)) = (len(&s, second), len(&s, sound));
+ let first_before = len(&s, first);
+ let before = s.project.clone();
+ let r = s.execute("timeline.trim", json!({"clip": if grab_sound { sound.0 } else { second.0 }, "edge": "in", "mode": "ripple", "deltaFrames": 8})).unwrap();
+ let by = rate.tick_of(8);
+ assert_eq!(r["delta"], json!(by.0));
+ assert_eq!((start(&s, second), start(&s, sound)), (picture_at, sound_at), "both stay where they start");
+ assert_eq!(len(&s, second), (pic_len - by, pic_in + by), "the picture loses 8 from its head");
+ assert_eq!(len(&s, sound), (snd_len - by, snd_in + by), "its sound loses the same 8");
+ assert_eq!(start(&s, second) - start(&s, sound), rate.tick_of(12), "the sound still leads by 12");
+ assert_eq!(len(&s, first), first_before, "the clip before is untouched");
+ s.active_sequence().unwrap().check().unwrap();
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!(*s.project, *before, "one undo step puts both back");
+ }
+
+ /// Material on a sync-locked track inside the stretch a shorter head takes away still refuses it.
+ #[test]
+ fn shorter_head_is_refused_when_a_sync_locked_track_has_material_in_the_stretch() {
+ let mut s = Session::default();
+ let (_, second, _) = j_cut(&mut s);
+ let rate = s.sequence_rate();
+ let tone = s.project.items.values().find(|i| !i.has_video() && i.has_audio() && i.as_media().is_some()).map(|i| i.id).unwrap();
+ s.execute("timeline.place", json!({"item": tone.0, "audioTrack": "A1", "frame": 50, "duration": rate.tick_of(24).0})).unwrap();
+ let before = s.project.clone();
+ let e = s.execute("timeline.trim", json!({"clip": second.0, "edge": "in", "mode": "ripple", "deltaFrames": 8})).unwrap_err();
+ assert!(e.to_string().contains("sync"), "{e}");
+ assert_eq!(*s.project, *before, "a refused trim changes nothing");
+ }