Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge pull request #784 from vangel76/fix-329-seek-latency

Brandon Thomas565fd4118df1
diff --cc crates/media/src/pool.rs
index 266d4e558,a8dae92a4..9933098d2
--- a/crates/media/src/pool.rs
+++ b/crates/media/src/pool.rs
@@@ -355,8 -353,10 +355,10 @@@ impl MediaPool
if footage.missing {
return None;
}
- match Inner::frame_at(&self.inner, footage, t, true, size) {
+ match Inner::frame_at(&self.inner, footage, t, true, size, float) {
Ok(img) => Some(img),
+ // nobody wants the frame any more (`cancellable`): not an error
+ Err(MediaError::Cancelled) => None,
Err(e) => {
log::warn!("media: frame of {} at {:?}: {e}", footage.path, t);
None
@@@ -806,14 -793,17 +808,20 @@@ impl FootageSource for MediaPool
fn cache_budget(&self) -> Option<usize> {
Some(self.budget())
}
+ /// A movie's decoder gives up on a seek (before it, and between samples while it decodes
+ /// forward from the keyframe) once `cancel` is set: a scrub's superseded frames no longer
+ /// hold the decoder the frame under the pointer waits for (#329).
+ fn cancellable(&self, cancel: &Arc<AtomicBool>, f: &mut dyn FnMut()) {
+ filmcraft_media::cancel::with_cancel(cancel, f)
+ }
fn frame(&self, item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<Image>> {
- self.frame_sized(item, footage, t, None)
+ self.frame_sized(item, footage, t, None, FloatCurve::Srgb)
}
fn frame_at_size(&self, item: ItemId, footage: &Footage, t: Tick, width: u32, height: u32) -> Option<Arc<Image>> {
- self.frame_sized(item, footage, t, Some((width, height)))
+ self.frame_sized(item, footage, t, Some((width, height)), FloatCurve::Srgb)
+ }
+ fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option<Arc<Image>> {
+ self.frame_sized(item, footage, t, size, float)
}
fn vector_frame(&self, _item: ItemId, footage: &Footage, scale: f64) -> Option<Arc<Image>> {
diff --cc crates/media/tests/footage.rs
index 58a6ecf2a,36e3657ae..1c0a97dd8
--- a/crates/media/tests/footage.rs
+++ b/crates/media/tests/footage.rs
@@@ -838,132 -675,24 +838,154 @@@ fn conformed_flac_audio_reads_back_like
let _ = std::fs::remove_dir_all(&folder);
}
+/// File ▸ Import of an STL part: importable by extension and probed as a 3D model item.
+#[test]
+fn stl_imports_as_a_model() {
+ assert!(effectcraft_media::is_importable(Path::new("part.stl")));
+ let ascii = "solid part\nfacet normal 0 0 1\nouter loop\nvertex 0 0 0\nvertex 1 0 0\nvertex 0 1 0\nendloop\nendfacet\nendsolid part\n";
+ let f = effectcraft_media::probe_bytes("part.stl", Arc::from(ascii.as_bytes())).expect("probe");
+ assert_eq!(f.kind, FootageKind::Model);
+ assert_eq!(f.codec, "STL (1 meshes, 1 triangles)");
+}
+
+/// `src` decoded by ffmpeg to 8-bit (`deep`: 16-bit) RGBA, premultiplied like the pool's frames.
+fn ffmpeg_rgba(src: &Path, deep: bool) -> Vec<[f32; 4]> {
+ let out = Command::new("ffmpeg")
+ .args(["-hide_banner", "-loglevel", "error", "-i"])
+ .arg(src)
+ .args(["-sws_flags", "accurate_rnd+full_chroma_int+bitexact", "-f", "rawvideo", "-pix_fmt", if deep { "rgba64le" } else { "rgba" }, "-"])
+ .output()
+ .expect("run ffmpeg");
+ assert!(out.status.success(), "ffmpeg decode of {}", src.display());
+ let px = |v: [f32; 4]| [v[0] * v[3], v[1] * v[3], v[2] * v[3], v[3]];
+ if !deep {
+ return out.stdout.as_chunks::<4>().0.iter().map(|p| px(p.map(|v| v as f32 / 255.0))).collect();
+ }
+ out.stdout.as_chunks::<8>().0.iter().map(|p| px(std::array::from_fn(|i| u16::from_le_bytes([p[2 * i], p[2 * i + 1]]) as f32 / 65535.0))).collect()
+}
+
+/// #756: DPX stills in every layout and depth ffmpeg's encoder writes (8-bit luma / RGB / RGBA /
+/// ABGR, 10- and 12-bit filled RGB, 16-bit luma / RGB / RGBA, big- and little-endian, lines
+/// padded to 32-bit words) import and decode as ffmpeg decodes them.
+#[test]
+fn dpx_stills_match_ffmpeg() {
+ let Some(png) = alpha_png() else { return };
+ let src = png.to_str().expect("utf8");
+ let formats = [
+ ("rgb24", "DPX 8-bit RGB", false),
+ ("rgba", "DPX 8-bit RGBA", true),
+ ("abgr", "DPX 8-bit ABGR", true),
+ ("gray", "DPX 8-bit luma", false),
+ ("gray16be", "DPX 16-bit luma", false),
+ ("gbrp10be", "DPX 10-bit RGB", false),
+ ("gbrp10le", "DPX 10-bit RGB", false),
+ ("gbrp12be", "DPX 12-bit RGB", false),
+ ("gbrp12le", "DPX 12-bit RGB", false),
+ ("rgb48be", "DPX 16-bit RGB", false),
+ ("rgba64le", "DPX 16-bit RGBA", true),
+ ];
+ for (pf, codec, alpha) in formats {
+ // 250 px wide: 8-bit and 10-bit lines need padding to a 32-bit word.
+ let Some(p) = ffmpeg_fixture(&format!("dpx-{pf}.dpx"), &["-i", src, "-vf", "scale=250:100", "-pix_fmt", pf]) else { return };
+ assert!(effectcraft_media::is_importable(&p));
+ let f = probe(&p).expect(pf);
+ assert_eq!(f.kind, FootageKind::Still);
+ assert_eq!((f.width, f.height, f.codec.as_str()), (250, 100, codec), "{pf}");
+ assert_eq!(f.alpha, if alpha { AlphaMode::Straight } else { AlphaMode::Ignore }, "{pf}");
+ let img = MediaPool::new().frame_at(&f, Tick::ZERO).expect(pf);
+ let want = ffmpeg_rgba(&p, !codec.contains("8-bit"));
+ assert_eq!(want.len(), img.data.len(), "{pf}");
+ let worst = img.data.iter().zip(&want).flat_map(|(a, b)| (0..4).map(move |k| (a[k] - b[k]).abs())).fold(0.0f32, f32::max);
+ // (within one 16-bit step: ffmpeg widens 10/12-bit samples by bit replication)
+ assert!(worst < 2.0 / 65535.0, "{pf}: worst difference {worst}");
+ }
+}
+
+/// #756: a numbered run of DPX files plays as an image sequence.
+#[test]
+fn dpx_sequence_plays() {
+ let Some(first) = ffmpeg_fixture("dpxseq/plate.0001.dpx", &["-f", "lavfi", "-i", "color=c=0x202020:size=32x16", "-frames:v", "1", "-pix_fmt", "gbrp10le"])
+ else {
+ return;
+ };
+ let Some(second) = ffmpeg_fixture("dpxseq/plate.0002.dpx", &["-f", "lavfi", "-i", "color=c=0xe0e0e0:size=32x16", "-frames:v", "1", "-pix_fmt", "gbrp10le"])
+ else {
+ return;
+ };
+ let mut f = probe(&first).expect("probe");
+ f.kind = FootageKind::Sequence;
+ f.sequence = [&first, &second].iter().map(|p| p.to_string_lossy().to_string()).collect();
+ f.sync_sequence_duration();
+ let pool = MediaPool::new();
+ let level = |n| pool.frame_at(&f, t_of(f.frame_rate, n)).expect("frame").data[0][0];
+ assert!((level(0) - 32.0 / 255.0).abs() < 0.01 && (level(1) - 224.0 / 255.0).abs() < 0.01, "{} {}", level(0), level(1));
+}
+
+/// #756: a Kodak Cineon file (no ffmpeg encoder: written here from the spec, 10-bit RGB
+/// left-justified in big-endian 32-bit words; checked against ImageMagick's reader when written)
+/// shows its log code values as stored; a truncated one is refused, never a panic.
+#[test]
+fn cineon_still_shows_code_values() {
+ let (w, h) = (5u32, 3u32);
+ let code = |x: u32, y: u32| [(x * 37 + y * 101) % 1024, (x * 11 + y * 7 + 300) % 1024, (x * 3 + y * 251 + 700) % 1024];
+ let mut b = vec![0u8; 1024];
+ b[..4].copy_from_slice(&[0x80, 0x2a, 0x5f, 0xd7]);
+ b[4..8].copy_from_slice(&1024u32.to_be_bytes());
+ b[8..12].copy_from_slice(&1024u32.to_be_bytes());
+ b[24..28].copy_from_slice(b"V4.5");
+ b[193] = 3;
+ for c in 0..3 {
+ let o = 196 + 28 * c;
+ b[o + 1] = c as u8 + 1;
+ b[o + 2] = 10;
+ b[o + 4..o + 8].copy_from_slice(&w.to_be_bytes());
+ b[o + 8..o + 12].copy_from_slice(&h.to_be_bytes());
+ }
+ b[681] = 5;
+ for y in 0..h {
+ for x in 0..w {
+ let [r, g, bl] = code(x, y);
+ b.extend_from_slice(&((r << 22) | (g << 12) | (bl << 2)).to_be_bytes());
+ }
+ }
+ let bytes: Arc<[u8]> = b.clone().into();
+ let f = effectcraft_media::probe_bytes("scan.cin", bytes.clone()).expect("probe");
+ assert_eq!((f.kind, f.width, f.height, f.codec.as_str(), f.alpha), (FootageKind::Still, 5, 3, "Cineon 10-bit RGB, log", AlphaMode::Ignore));
+ assert!(effectcraft_media::is_importable(Path::new("scan.cin")));
+ let pool = MediaPool::new();
+ pool.add_bytes("scan.cin", bytes);
+ let img = pool.frame_at(&f, Tick::ZERO).expect("frame");
+ for y in 0..h {
+ for x in 0..w {
+ let want = code(x, y).map(|v| v as f32 / 1023.0);
+ let got = img.get(i64::from(x), i64::from(y));
+ assert!((0..3).all(|k| (got[k] - want[k]).abs() < 1e-4) && got[3] == 1.0, "{x},{y}: {got:?} vs {want:?}");
+ }
+ }
+ for n in [3, 100, 700, 1024, b.len() - 1] {
+ let cut: Arc<[u8]> = b.get(..n).expect("cut").into();
+ assert!(effectcraft_media::probe_bytes("cut.cin", cut).is_err(), "{n}");
+ }
+}
++
+ /// #329: a frame read that is called off (a scrub has moved on) gives up before the decoder
+ /// seeks and decodes forward from the keyframe, so it no longer holds the decoder the newest
+ /// frame waits for; the frame decodes as usual when it is wanted again.
+ #[test]
+ fn a_called_off_read_does_not_decode() {
+ use std::sync::atomic::AtomicBool;
+ let Some(p) = h264_mp4() else { return };
+ let f = probe(&p).expect("probe");
+ let pool = MediaPool::new();
+ let t = t_of(f.frame_rate, 45);
+ let off = Arc::new(AtomicBool::new(true));
+ let mut got = None;
+ pool.cancellable(&off, &mut || got = Some(pool.frame(ItemId(1), &f, t)));
+ assert!(got.expect("ran").is_none(), "a called-off read decoded its frame");
+ assert_eq!(pool.stats().frames, 0, "nothing cached for it");
+ let on = Arc::new(AtomicBool::new(false));
+ let mut got = None;
+ pool.cancellable(&on, &mut || got = pool.frame(ItemId(1), &f, t));
+ let img = got.expect("decoded once wanted again");
+ assert_eq!((img.width, img.height), (1920, 1080));
+ }
diff --cc crates/render/src/lib.rs
index a77b7c04c,4f7258acc..6490d7f5b
--- a/crates/render/src/lib.rs
+++ b/crates/render/src/lib.rs
@@@ -1275,11 -1330,11 +1356,11 @@@ impl<'a> Renderer<'a>
{
return Some(Arc::new(b.img.clone()));
}
- let a = self.footage.frame(item, f, f.frame_rate.tick_of(i));
- let b = self.footage.frame(item, f, f.frame_rate.tick_of(i + 1));
+ let a = self.footage.frame_with(item, f, f.frame_rate.tick_of(i), None, float);
+ let b = self.footage.frame_with(item, f, f.frame_rate.tick_of(i + 1), None, float);
if let (Some(a), Some(b)) = (a, b) {
let img = blend_frames(mode, &a, &b, w as f32);
- if let (Some(c), Some(k)) = (self.cache, key) {
+ if let (Some(c), Some(k)) = (self.cache_to_keep(), key) {
c.insert(k, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 }));
}
return Some(Arc::new(img));