FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#318: a reversed clip shows exactly its source range
txiki739dfd67b27742c
diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rsindex bcf4a0dda..780042189 100644--- a/crates/render/src/lib.rs+++ b/crates/render/src/lib.rs@@ -489,11 +489,24 @@ fn plain_layer(frame: &VideoFrame, n: usize, opacity: f32, blend: Blend) -> Laye/// convert it with.fn media_frame(item: &TrackItem, t: Tick, sources: &dyn SourceProvider, want: f32, src_size: (u32, u32)) -> Option<(SharedSource, Arc<VideoFrame>, usize)> {let src = sources.source(item.item)?;- let frame = src.video_frame(FrameRequest { time: item.source_time_at(t), scale: want }).ok()?;+ let frame = src.video_frame(FrameRequest { time: video_source_time(item, t, src.info().frame_rate()), scale: want }).ok()?;let n = decimation(frame.width as f32, src_size.0 as f32 * want);Some((src, frame, n))}+/// The media time to ask a source for the video frame a clip shows at timeline `t`. A reversed+/// clip's source time is the instant just before its mirrored position; readers that round the+/// request to the nearest unit of a coarse timescale (MP4) land on the next frame from there,+/// showing one frame past the clip's source range and never its In (#318). Snapped down to the+/// start of the `media_rate` frame that contains it, every reader shows that frame.+pub(crate) fn video_source_time(item: &TrackItem, t: Tick, media_rate: filmcraft_time::FrameRate) -> Tick {+ let ft = item.source_time_at(t);+ if !item.reverse || item.frame_hold.is_some() || media_rate.frame_duration().0 <= 0 {+ return ft;+ }+ media_rate.tick_of(media_rate.frame_at(ft)).clamp(item.source_in.min(ft), ft)+}+/// The linear image of a media clip's `frame`, colour managed, and blended with the next frame when/// the clip's speed asks for it.#[allow(clippy::too_many_arguments)]diff --git a/crates/render/src/plan.rs b/crates/render/src/plan.rsindex bf4875e0f..7d27fdcc5 100644--- a/crates/render/src/plan.rs+++ b/crates/render/src/plan.rs@@ -293,7 +293,8 @@ fn push_item(let motion = motion_matrix(seq, item, size, mt);let lin = ((motion.a * motion.a + motion.b * motion.b).sqrt()).max((motion.c * motion.c + motion.d * motion.d).sqrt());let want = (lin * opts.scale as f64).clamp(1.0 / 64.0, 1.0) as f32;- let Ok(frame) = src.video_frame(FrameRequest { time: ft, scale: want }) else { return };+ let time = crate::video_source_time(item, t, src.info().frame_rate());+ let Ok(frame) = src.video_frame(FrameRequest { time, scale: want }) else { return };let cs = crate::colorman::source_space(project, item.item, &frame);// log / HDR / wide-gamut media is converted on the CPU (below), and so are blended// in-between frames (Frame Blending / Optical Flow on speed-changed clips)diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rsindex 20ec4fefb..4707051cf 100644--- a/crates/render/src/tests.rs+++ b/crates/render/src/tests.rs@@ -369,3 +369,87 @@ fn a_multicam_clip_showing_itself_plans_and_renders_to_an_end() {let img = render_sequence(&p, a, Tick::ZERO, opts, &sources);assert!(img.w > 0 && img.h > 0);}++/// A 24 fps source that picks frames like the MP4 reader: the requested time is converted to the+/// track timescale rounding to the nearest unit, then the frame at or before it is shown. It+/// records the frames it was asked for.+struct NearestUnitSource {+ inner: GeneratorSource,+ asked: std::sync::Mutex<Vec<i64>>,+}++impl MediaSource for NearestUnitSource {+ fn info(&self) -> &filmcraft_media::MediaInfo {+ self.inner.info()+ }+ fn video_frame(&self, req: FrameRequest) -> filmcraft_media::Result<Arc<filmcraft_frame::VideoFrame>> {+ // 12288 units a second: 512 a frame at 24 fps+ let units = req.time.to_rational_round(1, 12_288);+ self.asked.lock().unwrap().push(units.div_euclid(512));+ self.inner.video_frame(req)+ }+ fn audio(&self, start: i64, frames: usize, sample_rate: u32) -> filmcraft_media::Result<filmcraft_frame::AudioBuffer> {+ self.inner.audio(start, frames, sample_rate)+ }+}++/// #318: a trimmed clip (source frames 10–29) played in reverse shows 29 down to 10. Its source+/// time is the instant just before the mirrored position; a reader that rounds to the nearest+/// timescale unit used to land on the next frame, showing frame 30 (outside the clip) and never 10.+#[test]+fn a_reversed_clip_shows_exactly_its_source_range() {+ let r = FrameRate::FPS_24;+ let mut p = Project::new("r");+ let g = GeneratorSource::new(Generator::ColorMatte { color: [0.2, 0.4, 0.6, 1.0] }, 64, 64, r, Tick(4 * TICKS_PER_SECOND));+ let info = g.info().clone();+ let item = p.add_item(+ "ids",+ Label::Iris,+ ItemKind::Media(MediaClip {+ media: MediaRef::Generator(g.generator.clone()),+ info,+ interpret: Default::default(),+ mark_in: None,+ mark_out: None,+ markers: vec![],+ offline: false,+ proxy: None,+ identity: None,+ }),+ None,+ );+ let src = Arc::new(NearestUnitSource { inner: g, asked: Default::default() });+ let mut map = SourceMap::default();+ map.0.insert(item, src.clone() as SharedSource);+ let seq = p.new_sequence("s", SequenceSettings { width: 64, height: 64, frame_rate: r, ..Default::default() }, 1, 0, None);+ let ti = p.make_track_item(item, TrackKind::Video, Tick::ZERO, TimeRange::new(r.tick_of(10), r.tick_of(20)), r).unwrap();+ let id = ti.id;+ p.sequence_mut(seq).unwrap().video_tracks[0].items.push(ti);+ let opts = RenderOptions { scale: 1.0, ..Default::default() };+ let shown = |p: &Project, f: i64, planned: bool| -> Vec<i64> {+ src.asked.lock().unwrap().clear();+ if planned {+ plan::execute_cpu(&plan::plan_frame(p, seq, r.tick_of(f), opts, &map));+ } else {+ render_sequence(p, seq, r.tick_of(f), opts, &map);+ }+ let mut asked = src.asked.lock().unwrap().clone();+ asked.dedup();+ asked+ };+ // forward control: 10, 17, 29+ for (f, want) in [(0, 10), (7, 17), (19, 29)] {+ for planned in [false, true] {+ assert_eq!(shown(&p, f, planned), [want], "forward, timeline frame {f}, planned {planned}");+ }+ }+ p.sequence_mut(seq).unwrap().find_item_mut(id).unwrap().1.reverse = true;+ for (f, want) in [(0, 29), (7, 22), (19, 10)] {+ for planned in [false, true] {+ assert_eq!(shown(&p, f, planned), [want], "reversed, timeline frame {f}, planned {planned}");+ }+ }+ // every timeline frame stays inside the clip's source range, each source frame shown once+ let all: Vec<i64> = (0..20).flat_map(|f| shown(&p, f, false)).collect();+ assert_eq!(all, (10..=29).rev().collect::<Vec<_>>());+}