Skip to content

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

Merge pull request #887 from vangel76/fix-886-unsupported-video-notice

Brandon Thomasab12890a9e54
diff --cc crates/media/src/pool.rs
index b58c9f078,b08c83d07..56ecac8ac
--- a/crates/media/src/pool.rs
+++ b/crates/media/src/pool.rs
@@@ -357,12 -359,13 +363,15 @@@ 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,
+ // (a movie's failure is logged once per file, when it is recorded)
Err(e) => {
- log::warn!("media: frame of {} at {:?}: {e}", footage.path, t);
+ if lock(&self.inner.problems).get(footage.path.as_str()).is_none() {
+ log::warn!("media: frame of {} at {:?}: {e}", footage.path, t);
+ }
None
}
}
@@@ -613,7 -599,20 +622,20 @@@ impl Inner
.clone()
}
+ /// Remember why the movie at `path` cannot be shown (`codec` is the footage's, when known)
+ /// and log it the first time. I/O errors (missing or unreadable file) are not decoder
+ /// problems and are left to the missing-footage handling.
+ fn note_problem(&self, path: &Arc<str>, codec: &str, e: &MediaError) {
+ if matches!(e, MediaError::Io(_)) {
+ return;
+ }
+ let reason = describe_problem(codec, e);
+ if lock(&self.problems).insert(path.clone(), reason.clone()).is_none() {
+ log::warn!("media: {path}: {reason}");
+ }
+ }
+
- /// The decoded frames of the animated GIF at `path`. The first caller decodes it, outside the
+ /// The decoded frames of the animated GIF or WebP at `path`. The first caller decodes it, outside the
/// map's lock; callers asking for the same file meanwhile wait for that.
fn gif(&self, path: &Arc<str>) -> Result<Arc<Anim>> {
let slot = lock(&self.gifs).entry(path.clone()).or_default().clone();
@@@ -784,22 -773,22 +806,25 @@@
Ok(dynamic_to_image(frame, op))
}
Some(mt) => {
- let src = self.source(path).ok_or_else(|| MediaError::Io(format!("{path}: cannot open")))?;
+ let (src, turn) = self.opened(path).ok_or_else(|| MediaError::Io(format!("{path}: cannot open")))?;
- let vf = src.video_frame(FrameRequest::full(filmcraft_time::Tick(mt.0))).map_err(MediaError::from)?;
+ let vf = src
+ .video_frame(FrameRequest::full(filmcraft_time::Tick(mt.0)))
+ .map_err(MediaError::from)
+ .inspect_err(|e| self.note_problem(path, &footage.codec, e))?;
+ // A movie shown turned a quarter (a phone held upright): the footage's size and
+ // the render's are the turned picture's, the decoded frame's are the file's.
+ let side = |(w, h): (u32, u32)| if crate::probe::swaps_sides(turn) { (h, w) } else { (w, h) };
// A reduced-resolution render's frame, made at its size (a frame of the footage's
// own size only, as the renderer resamples only those).
- let (w, h) = loc.key.size;
+ let (w, h) = side(loc.key.size);
if w > 0
- && (vf.width, vf.height) == (footage.width, footage.height)
+ && (vf.width, vf.height) == side((footage.width, footage.height))
&& let Some(img) = frame_to_image_scaled(&vf, w, h)
{
- return Ok(img);
+ return Ok(turned(img, turn));
}
let buf = lock(&self.cache).lru.take_spare(vf.width as usize * vf.height as usize);
- Ok(frame_to_image_in(&vf, op, buf))
+ Ok(turned(frame_to_image_in(&vf, op, buf), turn))
}
}
}
@@@ -827,22 -816,20 +852,29 @@@ 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 problem(&self, footage: &Footage) -> Option<String> {
+ if footage.missing || footage.kind != FootageKind::Video {
+ return None;
+ }
+ lock(&self.inner.problems).get(footage.path.as_str()).cloned()
+ }
+
fn vector_frame(&self, _item: ItemId, footage: &Footage, scale: f64) -> Option<Arc<Image>> {
if footage.missing || !effectcraft_render::is_vector_footage(footage) {
return None;
diff --cc crates/media/tests/footage.rs
index cc2025031,8afc5bc75..3d4bcfbc8
--- a/crates/media/tests/footage.rs
+++ b/crates/media/tests/footage.rs
@@@ -891,156 -675,31 +891,185 @@@ 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), (250, 100), "{pf}");
+ // Some ffmpeg builds state the transfer (", linear"), which the codec name then carries.
+ assert!(f.codec == codec || f.codec.starts_with(&format!("{codec}, ")), "{pf}: {}", f.codec);
+ 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));
+}
++
+ /// H.264 High 4:2:2 10-bit: the software decoder lacks it; the pool says why, once, and the
+ /// footage reports it (not a black frame without a word).
+ #[test]
+ fn an_unsupported_stream_reports_why_it_shows_no_picture() {
+ let Some(p) = ffmpeg_fixture(
+ "h264_high422_10bit.mp4",
+ &["-f", "lavfi", "-i", "testsrc=size=320x180:rate=24:duration=1", "-c:v", "libx264", "-profile:v", "high422", "-pix_fmt", "yuv422p10le"],
+ ) else {
+ return;
+ };
+ let f = probe(&p).expect("probe");
+ let pool = MediaPool::new();
+ assert_eq!(pool.problem(&f), None, "nothing is wrong before a frame was asked for");
+ if pool.frame(ItemId(1), &f, Tick::ZERO).is_some() {
+ // a decoder that handles 4:2:2 (FilmCraft's, or a hardware one): no notice
+ assert_eq!(pool.problem(&f), None);
+ return;
+ }
+ let why = pool.problem(&f).expect("a failed frame leaves a reason");
+ assert!(why.contains("4:2:2"), "names the stream: {why}");
+ assert!(why.contains("isn't supported"), "says it is unsupported: {why}");
+ assert!(!why.contains("decode error"), "no error wrappers: {why}");
+ // it stays for the other frames, and is dropped when the file is forgotten
+ assert!(pool.frame(ItemId(1), &f, Tick::from_seconds_f64(0.5)).is_none());
+ assert_eq!(pool.problem(&f).as_deref(), Some(why.as_str()));
+ pool.forget(&f.path);
+ assert_eq!(pool.problem(&f), None);
+ }
diff --cc crates/render/src/lib.rs
index 6490d7f5b,8084a2f68..0c1a844cc
--- a/crates/render/src/lib.rs
+++ b/crates/render/src/lib.rs
@@@ -111,80 -85,13 +111,87 @@@ pub trait FootageSource: Send + Sync
fn vector_frame(&self, _item: ItemId, _footage: &Footage, _scale: f64) -> Option<Arc<Image>> {
None
}
+ /// Run `f`, which reads frames from this source, as work that can be called off: once
+ /// `cancel` is set, long decodes it starts (a movie seeking back to a keyframe and decoding
+ /// forward) stop early and their frames come back `None`. Sources that can't stop a decode
+ /// just run `f`. See [`CancellableFootage`].
+ fn cancellable(&self, _cancel: &Arc<AtomicBool>, f: &mut dyn FnMut()) {
+ f()
+ }
+ /// Reads through this source have been called off: frames may have come back `None` that
+ /// exist, so what was made from them must not be kept.
+ fn called_off(&self) -> bool {
+ false
+ }
+}
+
+/// A render's view of a [`FootageSource`] whose frame reads can be called off with `cancel`
+/// (a viewer frame nobody waits for any more, #329). Every read goes through
+/// [`FootageSource::cancellable`] on its own, so reads made for this render on another thread
+/// (a parallel loop) answer to this render's flag, and reads for other renders never do.
+pub struct CancellableFootage<'a> {
+ pub inner: &'a dyn FootageSource,
+ pub cancel: Arc<AtomicBool>,
+}
+
+impl CancellableFootage<'_> {
+ fn read<R>(&self, f: impl FnOnce() -> Option<R>) -> Option<R> {
+ if self.cancel.load(Ordering::Relaxed) {
+ return None;
+ }
+ let (mut f, mut out) = (Some(f), None);
+ self.inner.cancellable(&self.cancel, &mut || out = f.take().and_then(|f| f()));
+ out
+ }
+}
+
+impl FootageSource for CancellableFootage<'_> {
+ fn frame(&self, item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<Image>> {
+ self.read(|| self.inner.frame(item, footage, t))
+ }
+ fn frame_at_size(&self, item: ItemId, footage: &Footage, t: Tick, width: u32, height: u32) -> Option<Arc<Image>> {
+ self.read(|| self.inner.frame_at_size(item, footage, t, width, height))
+ }
+ fn audio(&self, item: ItemId, footage: &Footage, t: Tick, frames: usize, rate: u32) -> Option<Vec<f32>> {
+ self.inner.audio(item, footage, t, frames, rate)
+ }
+ fn aux(&self, item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<effectcraft_raster::AuxChannels>> {
+ self.inner.aux(item, footage, t)
+ }
+ fn model(&self, item: ItemId, footage: &Footage) -> Option<Arc<effectcraft_model::Model>> {
+ self.inner.model(item, footage)
+ }
+ fn set_cache_budget(&self, bytes: usize) {
+ self.inner.set_cache_budget(bytes)
+ }
+ fn purge(&self) {
+ self.inner.purge()
+ }
+ fn forget(&self, path: &str) {
+ self.inner.forget(path)
+ }
+ fn set_conform_folder(&self, folder: Option<std::path::PathBuf>) {
+ self.inner.set_conform_folder(folder)
+ }
+ fn cache_budget(&self) -> Option<usize> {
+ self.inner.cache_budget()
+ }
+ fn vector_frame(&self, item: ItemId, footage: &Footage, scale: f64) -> Option<Arc<Image>> {
+ self.inner.vector_frame(item, footage, scale)
+ }
+ fn cancellable(&self, cancel: &Arc<AtomicBool>, f: &mut dyn FnMut()) {
+ self.inner.cancellable(cancel, f)
+ }
+ fn called_off(&self) -> bool {
+ self.cancel.load(Ordering::Relaxed) || self.inner.called_off()
+ }
+ /// Why `footage` shows no picture although its file is there: a read of it failed because
+ /// no decoder handles its codec, profile, chroma format or bit depth, or the stream is damaged
+ /// (e.g. "H.264 (High 4:2:2) isn't supported by the built-in decoder: ..."). `None` while it
+ /// decodes, and for sources that don't track failures. Stays set until the file is forgotten.
+ fn problem(&self, _footage: &Footage) -> Option<String> {
+ None
+ }
}
/// Footage that can be rasterised at any scale (SVG).