diff --git a/crates/engine/src/av_decode.m b/crates/engine/src/av_decode.m index aaf61a0f130d6ce00311904add6e0618a57bb322..4021863e72c5ffd1d633ca596f88b6e84d4caf64 100644 --- a/crates/engine/src/av_decode.m +++ b/crates/engine/src/av_decode.m @@ -412,7 +412,7 @@ AvVideoDecoder *av_video_open( obj.trackStartSec = trackStartSec; obj.durSec = durSec; obj.lastFrameIdx = -1; - obj.lastFrameRgba = [NSMutableData dataWithLength:(NSUInteger)outW * (NSUInteger)outH * 4]; + obj.lastFrameRgba = nil; obj.hasLastFrame = NO; // Verify AVAssetReader can actually open and decode this asset/codec. @@ -530,9 +530,14 @@ int av_video_decode_frame( CFRelease(chosen); if (ok) { - if (obj.lastFrameRgba.length >= required) { - memcpy(obj.lastFrameRgba.mutableBytes, out_rgba, required); - obj.hasLastFrame = YES; + if (obj.nbFrames <= 0 || frame_idx >= obj.nbFrames - 4) { + if (!obj.lastFrameRgba || obj.lastFrameRgba.length < required) { + obj.lastFrameRgba = [NSMutableData dataWithLength:required]; + } + if (obj.lastFrameRgba.length >= required) { + memcpy(obj.lastFrameRgba.mutableBytes, out_rgba, required); + obj.hasLastFrame = YES; + } } obj.lastFrameIdx = frame_idx; } else { diff --git a/crates/engine/src/cache.rs b/crates/engine/src/cache.rs index 7256736bdd7920afcda38f80475256a573967e38..96352dec8114b927dec61372e99443bbba506d05 100644 --- a/crates/engine/src/cache.rs +++ b/crates/engine/src/cache.rs @@ -669,6 +669,11 @@ impl NodeCache { } } + #[cfg(test)] + pub(crate) fn is_entry_sticky(&self, key: u64) -> bool { + self.entries.get(&key).is_some_and(|e| e.sticky) + } + /// Starts per-node profiling for the upcoming frame evaluation. pub fn begin_frame_profile(&mut self) { let stats = self.pool_stats(); diff --git a/crates/engine/src/eval/mod.rs b/crates/engine/src/eval/mod.rs index 6261b0221d2ce4e85e47600ec3cf36f4a2feb04c..d2276adeb4c1af397be6c1cab21c648f1f8db9f8 100644 --- a/crates/engine/src/eval/mod.rs +++ b/crates/engine/src/eval/mod.rs @@ -105,6 +105,45 @@ pub struct Engine { pub hiq: bool, } +fn is_video_filename(filename: &str) -> bool { + let Some(ext) = std::path::Path::new(filename) + .extension() + .and_then(|e| e.to_str()) + else { + return false; + }; + matches!( + ext.to_ascii_lowercase().as_str(), + "mov" | "mp4" | "m4v" | "mkv" | "avi" | "webm" | "mxf" + ) +} + +fn is_static_loader_node(node: &Node) -> bool { + let Some(clip) = node.clips.first() else { + return false; + }; + if clip.length.unwrap_or(1) > 1 { + return false; + } + if is_video_filename(&clip.filename) { + return false; + } + if clip + .alternate_filename + .as_deref() + .is_some_and(is_video_filename) + { + return false; + } + if node + .static_str("ProxyFilename") + .is_some_and(is_video_filename) + { + return false; + } + true +} + impl Engine { pub fn new(device: wgpu::Device, queue: wgpu::Queue) -> Self { let supports_f32_filter = device.features().contains(wgpu::Features::FLOAT32_FILTERABLE); @@ -508,13 +547,16 @@ impl Engine { } let res = self.eval_node_inner(comp, id, time, frame_i, enc, visiting, stats); visiting.remove(&(id, time_key)); - if res.content_hash != 0 - && matches!( - comp.node(id).kind.as_str(), - "Loader" | "TextPlus" | "Renderer3D" | "pRender" | "PolylineMask" | "WandMask" - ) - { - self.cache.mark_entry_sticky(res.content_hash); + if res.content_hash != 0 { + let node = comp.node(id); + let sticky = match node.kind.as_str() { + "Loader" => is_static_loader_node(node), + "TextPlus" | "Renderer3D" | "pRender" | "PolylineMask" | "WandMask" => true, + _ => false, + }; + if sticky { + self.cache.mark_entry_sticky(res.content_hash); + } } self.frame_memo.insert((id, time_key, memo_sz), res.clone()); res @@ -1590,4 +1632,156 @@ Composition { assert!(eng.cache.vram_drops() > 0); assert_eq!(eng.last_bg_vram_drops.get(), eng.cache.vram_drops()); } + + #[test] + fn static_still_loader_is_sticky_while_video_and_sequence_loaders_are_not() { + let dir = std::env::temp_dir().join("fission_sticky_loader_tiering_test"); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + + let still_path = dir.join("still.png"); + image::RgbaImage::from_pixel(32, 32, image::Rgba([200, 100, 50, 255])) + .save(&still_path) + .unwrap(); + + for i in 0..3 { + let seq_path = dir.join(format!("seq_{i:04}.png")); + image::RgbaImage::from_pixel(32, 32, image::Rgba([40 * i as u8, 120, 200, 255])) + .save(&seq_path) + .unwrap(); + } + let seq_first = dir.join("seq_0000.png"); + + let video_path = dir.join("anim.mp4"); + let has_video = std::process::Command::new("ffmpeg") + .args([ + "-y", + "-v", + "error", + "-f", + "lavfi", + "-i", + "testsrc2=size=64x64:rate=24:duration=0.5", + "-c:v", + "libx264", + "-pix_fmt", + "yuv420p", + ]) + .arg(&video_path) + .status() + .is_ok_and(|s| s.success()); + + let video_tool = if has_video { + format!( + r#" + VidLoader = Loader {{ + Clips = {{ + Clip {{ + ID = "Clip1", + Filename = "{}", + FormatID = "QuickTimeMovies", + Length = 12, + TrimIn = 0, + TrimOut = 11, + GlobalStart = 0, + GlobalEnd = 11, + }}, + }}, + }},"#, + video_path.to_string_lossy().replace('\\', "/") + ) + } else { + String::new() + }; + + let src = format!( + r#" +Composition {{ + CurrentTime = 0, + RenderRange = {{ 0, 5 }}, + GlobalRange = {{ 0, 5 }}, + Tools = ordered() {{ + StillLoader = Loader {{ + Clips = {{ + Clip {{ + ID = "Clip1", + Filename = "{}", + FormatID = "PNGFormat", + StartFrame = -1, + GlobalStart = 0, + GlobalEnd = 10, + }}, + }}, + }}, + SeqLoader = Loader {{ + Clips = {{ + Clip {{ + ID = "Clip1", + Filename = "{}", + FormatID = "PNGFormat", + StartFrame = 0, + Length = 3, + TrimIn = 0, + TrimOut = 2, + GlobalStart = 0, + GlobalEnd = 2, + }}, + }}, + }},{video_tool} + }}, +}} +"#, + still_path.to_string_lossy().replace('\\', "/"), + seq_first.to_string_lossy().replace('\\', "/"), + ); + + let comp = Comp::from_fusion_str(&src).expect("parse loader tiering comp"); + let still_id = comp.find_node("StillLoader").unwrap(); + let seq_id = comp.find_node("SeqLoader").unwrap(); + assert!(is_static_loader_node(comp.node(still_id))); + assert!(!is_static_loader_node(comp.node(seq_id))); + if has_video { + let vid_id = comp.find_node("VidLoader").unwrap(); + assert!(!is_static_loader_node(comp.node(vid_id))); + } + + let Ok(mut eng) = Engine::new_headless() else { + let _ = std::fs::remove_dir_all(&dir); + return; + }; + + let (still_tex, _) = eng.evaluate(&comp, still_id, 0.0); + assert_ne!(still_tex.content_hash, 0); + assert!( + eng.cache.is_entry_sticky(still_tex.content_hash), + "static still Loader must be marked sticky" + ); + + let (seq_tex, _) = eng.evaluate(&comp, seq_id, 0.0); + assert_ne!(seq_tex.content_hash, 0); + assert!( + !eng.cache.is_entry_sticky(seq_tex.content_hash), + "multi-frame image-sequence Loader must NOT be sticky" + ); + + if has_video { + let vid_id = comp.find_node("VidLoader").unwrap(); + let (vid_tex, _) = eng.evaluate(&comp, vid_id, 0.0); + assert_ne!(vid_tex.content_hash, 0); + assert!( + !eng.cache.is_entry_sticky(vid_tex.content_hash), + "video Loader must NOT be sticky" + ); + + // Also verify lazy EOF frame-hold in video decoder works cleanly past EOF. + let mut dec = crate::media::open_video_decoder(&video_path).expect("open video decoder"); + let nb = dec.info().nb_frames; + assert!(nb >= 6); + let near_eof = dec.decode_frame(nb - 2).expect("decode near EOF"); + let past_eof = dec.decode_frame(nb + 1).expect("hold frame past EOF"); + assert_eq!(near_eof.len(), past_eof.len()); + } + + let _ = std::fs::remove_dir_all(&dir); + } }