Skip to content

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

edit: a ripple trim takes a later split edit along

punkhop4a0ab32480ff
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs
index 3a84f6d33..d369fb0fd 100644
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@ -559,8 +559,9 @@ pub fn trim(seq: &mut Sequence, clip: ClipId, edge: Edge, mode: TrimMode, delta:
/// Ripple-trim one edge of a group of linked items (e.g. a clip and its audio partners) as a single
/// edit: every member's edge moves by the same delta, later material on the members' tracks and on
-/// sync-locked tracks shifts by the change. Only tracks without a group member can block the edit
-/// (a sync-locked track whose material would be overwritten).
+/// sync-locked tracks shifts by the change, and so does a clip that starts before the cut, reaches
+/// past it and is linked to a clip that shifts (a split edit). The edit is refused when it would
+/// overwrite material on a sync-locked track, or when such a linked clip has no room to follow.
pub fn ripple_trim_group(seq: &mut Sequence, clips: &[ClipId], edge: Edge, delta: Tick, ctx: &mut EditCtx) -> Result<Tick> {
let Some(&first) = clips.first() else { return Ok(Tick::ZERO) };
let mut d = delta;
@@ -606,27 +607,54 @@ pub fn ripple_trim_group(seq: &mut Sequence, clips: &[ClipId], edge: Edge, delta
origins.iter().find(|(t, _)| *t == tid).map(|o| o.1).unwrap_or_default()
};
let shift = if edge == Edge::In { -d } else { d };
- for tr in work.all_tracks_mut() {
- if tr.locked {
- continue;
- }
- let member = origins.iter().find(|(t, _)| *t == tr.id).map(|o| o.1);
- if member.is_none() && !tr.sync_lock {
- continue;
- }
- let from = member.unwrap_or(main_from);
- if member.is_none() && shift < Tick::ZERO {
+ // where the shift starts on each track that ripples: the members' tracks and the sync-locked ones
+ let rippling: Vec<(TrackId, Tick, bool)> = work
+ .all_tracks()
+ .filter(|tr| !tr.locked)
+ .filter_map(|tr| match origins.iter().find(|(t, _)| *t == tr.id) {
+ Some(o) => Some((tr.id, o.1, true)),
+ None => tr.sync_lock.then_some((tr.id, main_from, false)),
+ })
+ .collect();
+ // A split edit: the linked partner of a later clip can start before the shift does and reach
+ // past it (its sound leads the cut). It follows the clip it is linked to, or the two would
+ // drift apart.
+ // (the trimmed clips' own partners are trimmed, or left alone, by the caller: they never follow)
+ let own_links: Vec<u64> = clips.iter().filter_map(|c| work.find_item(*c).and_then(|(_, i)| i.link)).collect();
+ let mut shifting_links: Vec<u64> = Vec::new();
+ for (tid, from, _) in &rippling {
+ let Some(tr) = work.track(*tid) else { continue };
+ let later = tr.items.iter().filter(|i| !clips.contains(&i.id) && i.start >= *from);
+ shifting_links.extend(later.filter_map(|i| i.link).filter(|l| !own_links.contains(l)));
+ }
+ 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));
+ if !member && shift < Tick::ZERO {
let at = if edge == Edge::In { from - Tick(1) } else { from };
- if !track_range_empty(tr, TimeRange::new(at + shift, -shift)) {
+ let closing = TimeRange::new(at + shift, -shift);
+ if tr.items.iter().any(|i| i.range().overlaps(&closing) && !follows(i)) {
return Err(EditError::SyncLockConflict);
}
}
+ let followers: Vec<ClipId> = tr.items.iter().filter(|i| !clips.contains(&i.id) && follows(i)).map(|i| i.id).collect();
for i in &mut tr.items {
- if !clips.contains(&i.id) && i.start >= from {
+ if !clips.contains(&i.id) && (i.start >= from || followers.contains(&i.id)) {
i.start += shift;
}
}
tr.sort();
+ // a transition into a follower from a clip that stayed behind no longer sits on a cut
+ let ends: Vec<(ClipId, Tick)> = tr.items.iter().map(|i| (i.id, i.end())).collect();
+ let starts: Vec<(ClipId, Tick)> = tr.items.iter().map(|i| (i.id, i.start)).collect();
+ tr.transitions.retain(|t| match (t.from, t.to) {
+ (Some(a), Some(b)) if followers.contains(&b) => ends.iter().find(|e| e.0 == a).map(|e| e.1) == starts.iter().find(|x| x.0 == b).map(|x| x.1),
+ _ => true,
+ });
+ // a follower with no room (a clip in its way, or the sequence start) blocks the edit
+ if tr.items.first().is_some_and(|i| i.start < Tick::ZERO) || tr.items.windows(2).any(|w| w[0].end() > w[1].start) {
+ return Err(EditError::SyncLockConflict);
+ }
}
transitions_follow_cuts(seq, &mut work);
work.check().map_err(EditError::Other)?;
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index fac06e430..33a14cf9f 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -192,6 +192,79 @@ fn ripple_trim_shifts_following() {
assert_eq!(fx.spans(v1), vec![(0, 12), (12, 10)]);
}
+#[test]
+fn ripple_trim_group_takes_a_split_edit_along() {
+ // V1: a [0,10) then b [10,20). b's linked sound leads it by 4 on sync-locked A1: [6,20).
+ 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);
+ let sound = fx.put(a1, 6, 14, 0);
+ for c in [b, sound] {
+ fx.seq.find_item_mut(c).unwrap().1.link = Some(7);
+ }
+ let mut n = fx.next;
+ ripple_trim_group(&mut fx.seq, &[a], Edge::Out, f(3), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 13), (13, 10)], vec![(9, 14)]), "longer: the early sound follows its picture");
+ ripple_trim_group(&mut fx.seq, &[a], Edge::Out, -f(5), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 8), (8, 10)], vec![(4, 14)]), "shorter: it follows too");
+ // the sound would start before the sequence does: refused, nothing changes
+ assert_eq!(ripple_trim_group(&mut fx.seq, &[a], Edge::Out, -f(5), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));
+ // another clip right before the sound leaves it no room either
+ fx.put(a1, 0, 4, 0);
+ assert_eq!(ripple_trim_group(&mut fx.seq, &[a], Edge::Out, -f(2), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));
+ assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 8), (8, 10)], vec![(0, 4), (4, 14)]), "unchanged on failure");
+ // a linked clip that ends before the cut is not a split edit and stays where it is
+ let early = fx.put(fx.a(1), 0, 3, 0);
+ fx.seq.find_item_mut(early).unwrap().1.link = Some(7);
+ ripple_trim_group(&mut fx.seq, &[a], Edge::Out, f(2), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!(fx.spans(fx.a(1)), vec![(0, 3)]);
+ fx.seq.check().unwrap();
+
+ // In edge: V1 b [10,20), c [20,30); c's sound starts 12 early on A1: [8,30). b loses 3 at its head.
+ let mut fx = Fx::new();
+ let (v1, v2, a1, a2) = (fx.v(0), fx.v(1), fx.a(0), fx.a(1));
+ fx.put(v1, 0, 10, 0);
+ let b = fx.put(v1, 10, 10, 0);
+ let c = fx.put(v1, 20, 10, 0);
+ let c_sound = fx.put(a1, 8, 22, 0);
+ // two more clips linked to b that the caller did not put in the group
+ let b_sound = fx.put(a2, 10, 10, 0);
+ let b_top = fx.put(v2, 12, 8, 0);
+ for (x, link) in [(c, 7), (c_sound, 7), (b, 8), (b_sound, 8), (b_top, 8)] {
+ fx.seq.find_item_mut(x).unwrap().1.link = Some(link);
+ }
+ 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![(5, 22)]), "c and its early sound move up together");
+ assert_eq!((fx.spans(v2), fx.spans(a2)), (vec![(9, 8)], vec![(10, 10)]), "the trimmed clip's own partners never follow");
+
+ // a crossfade into the early sound from a clip that stays behind no longer sits on a cut: it goes
+ 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);
+ let stays = fx.put(a1, 0, 6, 0);
+ let sound = fx.put(a1, 6, 14, 0);
+ for x in [b, sound] {
+ fx.seq.find_item_mut(x).unwrap().1.link = Some(7);
+ }
+ fx.seq.track_mut(a1).unwrap().transitions = vec![Transition {
+ id: TransitionId(1),
+ effect: filmcraft_project::find_effect("cross_dissolve").unwrap().instance(),
+ start: f(4),
+ duration: f(4),
+ from: Some(stays),
+ to: Some(sound),
+ align: Default::default(),
+ reverse: false,
+ }];
+ let mut n = fx.next;
+ ripple_trim_group(&mut fx.seq, &[a], Edge::Out, f(3), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!(fx.spans(a1), vec![(0, 6), (9, 14)]);
+ assert!(fx.seq.track(a1).unwrap().transitions.is_empty());
+}
+
#[test]
fn roll_slip_slide() {
let mut fx = Fx::new();
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index a5f34e61d..fe245c876 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -922,6 +922,8 @@ mod settings_tests;
#[cfg(test)]
mod shortcuts_tests;
#[cfg(test)]
+mod split_edit_ripple_trim_tests;
+#[cfg(test)]
mod tests;
#[cfg(test)]
mod transcript_tests;
diff --git a/crates/engine/src/split_edit_ripple_trim_tests.rs b/crates/engine/src/split_edit_ripple_trim_tests.rs
new file mode 100644
index 000000000..0b824e158
--- /dev/null
+++ b/crates/engine/src/split_edit_ripple_trim_tests.rs
@@ -0,0 +1,87 @@
+//! A ripple trim ahead of a split edit: the later clip's sound starts before its picture (a J cut)
+//! and has to move with it.
+
+use filmcraft_project::ClipId;
+use filmcraft_time::Tick;
+use serde_json::json;
+
+use crate::Session;
+
+fn start(s: &Session, c: ClipId) -> Tick {
+ s.active_sequence().unwrap().find_item(c).map(|(_, i)| i.start).unwrap()
+}
+
+/// Two clips in a row on V1. The first has its sound on A1, the second on A2, and the second's
+/// sound starts 12 frames before its picture. Returns (first picture, second picture, second sound).
+fn j_cut(s: &mut Session) -> (ClipId, ClipId, ClipId) {
+ s.execute("file.openDemoProject", json!({})).unwrap();
+ s.execute("file.newSequence", json!({"name": "J cut", "video": 1, "audio": 2})).unwrap();
+ let rate = s.sequence_rate();
+ let item = s.project.items.values().find(|i| i.has_video() && i.has_audio() && i.as_media().is_some()).map(|i| i.id).unwrap();
+ let (source_in, len) = (rate.tick_of(48).0, rate.tick_of(48).0);
+ s.execute("timeline.place", json!({"item": item.0, "track": "V1", "audioTrack": "A1", "frame": 0, "sourceIn": source_in, "duration": len})).unwrap();
+ s.execute("timeline.place", json!({"item": item.0, "track": "V1", "audioTrack": "A2", "frame": 48, "sourceIn": source_in, "duration": len})).unwrap();
+ let q = s.active_sequence().unwrap();
+ let (first, second, sound) = (q.video_tracks[0].items[0].id, q.video_tracks[0].items[1].id, q.audio_tracks[1].items[0].id);
+ // the split: only the sound's In edge moves, so Linked Selection is off for this one trim
+ s.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ s.execute("timeline.trim", json!({"clip": sound.0, "edge": "in", "mode": "regular", "deltaFrames": -12})).unwrap();
+ s.execute("sequence.linkedSelection", json!({"on": true})).unwrap();
+ assert_eq!(start(s, second) - start(s, sound), rate.tick_of(12), "the sound leads its picture by 12 frames");
+ (first, second, sound)
+}
+
+/// Lengthening the clip before a J cut moved the later picture and left its sound behind: a lead
+/// of 12 frames became 20, with no error.
+#[test]
+fn lengthening_before_a_j_cut_keeps_the_sound_with_its_picture() {
+ ripple_and_check(8);
+}
+
+/// Shortening it was refused as a sync-lock conflict, because the early sound sat in the stretch
+/// that closes.
+#[test]
+fn shortening_before_a_j_cut_keeps_the_sound_with_its_picture() {
+ ripple_and_check(-8);
+}
+
+fn ripple_and_check(frames: i64) {
+ let mut s = Session::default();
+ let (first, second, sound) = j_cut(&mut s);
+ let rate = s.sequence_rate();
+ let (picture_at, sound_at) = (start(&s, second), start(&s, sound));
+ let before = s.project.clone();
+ let r = s.execute("timeline.trim", json!({"clip": first.0, "edge": "out", "mode": "ripple", "deltaFrames": frames})).unwrap();
+ let by = rate.tick_of(frames);
+ assert_eq!(r["delta"], json!(by.0));
+ assert_eq!(start(&s, second), picture_at + by, "the later picture ripples");
+ assert_eq!(start(&s, sound), sound_at + by, "its sound still leads by 12 frames");
+ s.active_sequence().unwrap().check().unwrap();
+ let after = s.project.clone();
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!(*s.project, *before, "one undo step puts both back");
+ s.execute("edit.redo", json!({})).unwrap();
+ assert_eq!(*s.project, *after);
+}
+
+/// The early sound has no room to follow: the sequence start, or another clip right before it on
+/// its track. The trim is refused and nothing moves.
+#[test]
+fn ripple_trim_is_refused_when_the_early_sound_has_no_room() {
+ let mut s = Session::default();
+ let (first, second, sound) = j_cut(&mut s);
+ let rate = s.sequence_rate();
+ let before = s.project.clone();
+ // the sound starts at frame 36: 40 frames earlier is before the sequence starts
+ let e = s.execute("timeline.trim", json!({"clip": first.0, "edge": "out", "mode": "ripple", "deltaFrames": -40})).unwrap_err();
+ assert!(e.to_string().contains("sync"), "{e}");
+ assert_eq!(*s.project, *before, "a refused trim changes nothing");
+ 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": "A2", "frame": 12, "duration": rate.tick_of(24).0})).unwrap();
+ let before = s.project.clone();
+ assert_eq!(s.active_sequence().unwrap().audio_tracks[1].items[0].end(), start(&s, sound));
+ let e = s.execute("timeline.trim", json!({"clip": first.0, "edge": "out", "mode": "ripple", "deltaFrames": -8})).unwrap_err();
+ assert!(e.to_string().contains("sync"), "{e}");
+ assert_eq!(*s.project, *before, "a refused trim changes nothing");
+ assert_eq!(start(&s, second) - start(&s, sound), rate.tick_of(12));
+}