Skip to content

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

MEM2: idle sources give up their decoders beyond a cap

justjohn12345517b6b041b3e
diff --git a/crates/codecs/src/gop.rs b/crates/codecs/src/gop.rs
index 06a11ac4d..603c8c8a3 100644
--- a/crates/codecs/src/gop.rs
+++ b/crates/codecs/src/gop.rs
@@ -5,8 +5,11 @@
//! for the next frames therefore hit the cache or continue the running decoder without re-seeking.
use std::collections::BTreeMap;
-use std::sync::atomic::{AtomicU64, Ordering};
-use std::sync::{Arc, Mutex};
+use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
+use std::sync::{Arc, Mutex, MutexGuard, PoisonError, TryLockError, Weak};
+use std::time::Duration;
+
+use web_time::Instant;
use filmcraft_color::ColorInfo;
use filmcraft_frame::VideoFrame;
@@ -68,7 +71,7 @@ fn count_frames<T>(out: &[T]) {
/// Run a decoder call, adding its wall time to the process-wide counter.
fn timed<R>(f: impl FnOnce() -> R) -> R {
- let t0 = web_time::Instant::now();
+ let t0 = Instant::now();
let r = f();
DECODE_NS.fetch_add(t0.elapsed().as_nanos() as u64, Ordering::Relaxed);
r
@@ -135,9 +138,23 @@ struct State {
/// The cached frames that were decoded in draft mode: served to draft requests only.
drafts: std::collections::BTreeSet<i64>,
bytes: usize,
+ /// When a frame was last asked of this cache.
+ last_used: Instant,
}
impl State {
+ /// Give up the decoder (and the spare intra decoders): the next request that has to decode
+ /// makes a new one and restarts at a sync sample. Returned so they are dropped off the lock.
+ fn release_decoder(&mut self, pool: &Pool) -> Vec<Box<dyn VideoDecoder>> {
+ let mut out = std::mem::take(&mut self.spare);
+ if let Some(d) = self.decoder.take() {
+ pool.decoders.fetch_sub(1, Ordering::Relaxed);
+ out.push(d);
+ }
+ self.next = usize::MAX;
+ out
+ }
+
/// The cached frame at `pts`, unless it is a draft frame and the request wants the exact one.
fn cached(&self, pts: i64, draft_ok: bool) -> Option<&Arc<VideoFrame>> {
self.frames.get(&pts).filter(|_| draft_ok || !self.drafts.contains(&pts))
@@ -178,9 +195,103 @@ impl Drop for DecodingGuard {
}
}
+/// A decoder keeps its reference and in-flight pictures (hundreds of MB for a frame-threaded
+/// 4K decoder), so only this many caches keep theirs once idle: a timeline's other clips make a
+/// new one when the playhead reaches them, which costs what a seek costs.
+const MAX_LIVE_DECODERS: usize = 4;
+
+/// A cache asked for a frame this recently is in use (a layer of the frame being composited,
+/// the next clip being prefetched): it keeps its decoder whatever the count.
+const IDLE: Duration = Duration::from_secs(3);
+
+/// What the caches of a process share: the cap on live decoders.
+struct Pool {
+ caches: Mutex<Vec<Weak<Shared>>>,
+ /// Caches that hold a decoder.
+ decoders: AtomicUsize,
+ max_decoders: usize,
+ idle: Duration,
+}
+
+/// The part of a [`GopCache`] its pool reaches.
+struct Shared {
+ state: Mutex<State>,
+ pool: Arc<Pool>,
+}
+
+impl Drop for Shared {
+ fn drop(&mut self) {
+ let st = self.state.get_mut().unwrap_or_else(PoisonError::into_inner);
+ drop(st.release_decoder(&self.pool));
+ }
+}
+
+/// A cache's state unless another thread is using it (it is then decoding: not idle).
+fn try_state(c: &Shared) -> Option<MutexGuard<'_, State>> {
+ match c.state.try_lock() {
+ Ok(g) => Some(g),
+ Err(TryLockError::Poisoned(e)) => Some(e.into_inner()),
+ Err(TryLockError::WouldBlock) => None,
+ }
+}
+
+impl Pool {
+ fn new(max_decoders: usize, idle: Duration) -> Self {
+ Self { caches: Mutex::new(Vec::new()), decoders: AtomicUsize::new(0), max_decoders, idle }
+ }
+
+ /// The pool of every cache in the process.
+ fn global() -> Arc<Pool> {
+ static POOL: std::sync::OnceLock<Arc<Pool>> = std::sync::OnceLock::new();
+ POOL.get_or_init(|| Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE))).clone()
+ }
+
+ fn register(&self, c: &Arc<Shared>) {
+ let mut g = self.caches.lock().unwrap_or_else(PoisonError::into_inner);
+ g.retain(|w| w.strong_count() > 0);
+ g.push(Arc::downgrade(c));
+ }
+
+ /// The idle caches other than `me` that `keep` selects, least recently used first. Called
+ /// with `me` locked: other caches are only ever try-locked, so two caches trimming each other
+ /// cannot deadlock.
+ fn idle_others(&self, me: &Shared, keep: impl Fn(&State) -> bool) -> Vec<Arc<Shared>> {
+ let all: Vec<Arc<Shared>> = self.caches.lock().unwrap_or_else(PoisonError::into_inner).iter().filter_map(Weak::upgrade).collect();
+ let mut idle: Vec<(Instant, Arc<Shared>)> = all
+ .into_iter()
+ .filter(|c| !std::ptr::eq(Arc::as_ptr(c), me))
+ .filter_map(|c| {
+ let used = try_state(&c).filter(|st| keep(st) && st.last_used.elapsed() >= self.idle).map(|st| st.last_used)?;
+ Some((used, c))
+ })
+ .collect();
+ idle.sort_by_key(|(used, _)| *used);
+ idle.into_iter().map(|(_, c)| c).collect()
+ }
+
+ /// Drop the decoders of idle caches, least recently used first, down to the cap.
+ fn trim_decoders(&self, me: &Shared) {
+ if self.decoders.load(Ordering::Relaxed) <= self.max_decoders {
+ return;
+ }
+ for c in self.idle_others(me, |st| st.decoder.is_some()) {
+ if self.decoders.load(Ordering::Relaxed) <= self.max_decoders {
+ break;
+ }
+ let released = try_state(&c).map(|mut st| st.release_decoder(self));
+ drop(released);
+ }
+ }
+}
+
+/// Caches that hold a decoder right now, process-wide (`perf.stats`).
+pub fn live_decoders() -> usize {
+ Pool::global().decoders.load(Ordering::Relaxed)
+}
+
/// Decoder + decoded-frame cache for one video track.
pub struct GopCache {
- state: Mutex<State>,
+ shared: Arc<Shared>,
/// Colour signalled by the container, which wins over the bitstream's.
explicit_color: Option<ColorInfo>,
/// Clockwise quarter turns from the container's display matrix, applied to every frame.
@@ -200,7 +311,13 @@ const CONTINUE_THROUGH: usize = 48;
impl GopCache {
pub fn new(explicit_color: Option<ColorInfo>) -> Self {
- Self {
+ // unit tests count decoder restarts: each of their caches gets a pool of its own
+ let pool = if cfg!(test) { Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE)) } else { Pool::global() };
+ Self::in_pool(explicit_color, pool)
+ }
+
+ fn in_pool(explicit_color: Option<ColorInfo>, pool: Arc<Pool>) -> Self {
+ let shared = Arc::new(Shared {
state: Mutex::new(State {
decoder: None,
intra: false,
@@ -211,11 +328,12 @@ impl GopCache {
frames: BTreeMap::new(),
drafts: Default::default(),
bytes: 0,
+ last_used: Instant::now(),
}),
- explicit_color,
- rotation: 0,
- budget: 384 << 20,
- }
+ pool: pool.clone(),
+ });
+ pool.register(&shared);
+ Self { shared, explicit_color, rotation: 0, budget: 384 << 20 }
}
/// Turn every decoded frame clockwise by `quarter_turns` × 90° (the container's display
@@ -297,8 +415,9 @@ impl GopCache {
return self.private_frame(s, i, want_pts, n);
}
let draft = filmcraft_media::cancel::draft();
- let mut st = self.state.lock().unwrap_or_else(|e| e.into_inner());
+ let mut st = self.shared.state.lock().unwrap_or_else(|e| e.into_inner());
let _decoding = DecodingGuard::enter(me);
+ st.last_used = Instant::now();
if let Some(f) = st.cached(want_pts, draft) {
HITS.fetch_add(1, Ordering::Relaxed);
return Ok(f.clone());
@@ -309,7 +428,9 @@ impl GopCache {
st.intra = d.intra_only();
st.decoder = Some(d);
st.next = usize::MAX;
+ self.shared.pool.decoders.fetch_add(1, Ordering::Relaxed);
}
+ self.shared.pool.trim_decoders(&self.shared);
// Nothing cached: the work ahead (possibly a seek and a GOP of decoding) is only worth it
// while someone still wants the frame.
if filmcraft_media::cancel::cancelled() {
@@ -462,7 +583,7 @@ impl GopCache {
DECODED.fetch_add(1, Ordering::Relaxed);
timed(|| dec.decode(&data, want_pts))
});
- let mut st = self.state.lock().unwrap_or_else(|e| e.into_inner());
+ let mut st = self.shared.state.lock().unwrap_or_else(|e| e.into_inner());
if st.spare.len() < 16 {
st.spare.push(dec);
}
@@ -480,7 +601,6 @@ mod tests {
use super::*;
use crate::video::DecodedFrame;
use std::collections::VecDeque;
- use std::sync::atomic::AtomicUsize;
/// `n` samples, a sync sample every `gop`, pts = 1000 · index (no reordering).
struct Samples {
@@ -716,4 +836,61 @@ mod tests {
assert_eq!(index_of(&c.frame(&s, 7_000).expect("frame")), 7);
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
}
+
+ #[test]
+ fn idle_caches_give_up_their_decoders_beyond_the_cap() {
+ let pool = Arc::new(Pool::new(2, Duration::ZERO));
+ let live = || pool.decoders.load(Ordering::Relaxed);
+ let s: Vec<Samples> = (0..5).map(|_| samples(300, 250, 3, false)).collect();
+ let c: Vec<GopCache> = (0..5).map(|_| GopCache::in_pool(None, pool.clone())).collect();
+ // a clip after another, as when clips are dropped on a timeline
+ for k in 0..5 {
+ assert_eq!(index_of(&c[k].frame(&s[k], 10_000).expect("frame")), 10);
+ assert!(live() <= 2, "{} live decoders after clip {k}", live());
+ }
+ assert_eq!(live(), 2);
+ // the most recently used keep theirs and carry on without a seek
+ assert_eq!(index_of(&c[4].frame(&s[4], 20_000).expect("frame")), 20);
+ assert_eq!(s[4].resets.load(Ordering::Relaxed), 1);
+ // a cache that lost its decoder still serves what it cached…
+ let decodes = s[0].decodes.load(Ordering::Relaxed);
+ assert_eq!(index_of(&c[0].frame(&s[0], 10_000).expect("cached")), 10);
+ assert_eq!(s[0].decodes.load(Ordering::Relaxed), decodes);
+ // …and decodes again with a new decoder, from the sync sample
+ assert_eq!(index_of(&c[0].frame(&s[0], 20_000).expect("frame")), 20);
+ assert_eq!(s[0].resets.load(Ordering::Relaxed), 2);
+ assert_eq!(live(), 2);
+ // a dropped cache no longer counts
+ drop(c);
+ assert_eq!(live(), 0);
+ }
+
+ #[test]
+ fn caches_in_use_keep_their_decoders_whatever_the_cap() {
+ // every layer of one composited frame was asked moments ago: none is idle
+ let pool = Arc::new(Pool::new(2, Duration::from_secs(3600)));
+ let s: Vec<Samples> = (0..5).map(|_| samples(300, 250, 3, false)).collect();
+ let c: Vec<GopCache> = (0..5).map(|_| GopCache::in_pool(None, pool.clone())).collect();
+ for t in [10_000, 11_000, 12_000] {
+ for k in 0..5 {
+ assert_eq!(index_of(&c[k].frame(&s[k], t).expect("frame")) as i64, t / 1000);
+ }
+ }
+ assert_eq!(pool.decoders.load(Ordering::Relaxed), 5);
+ assert!(s.iter().all(|s| s.resets.load(Ordering::Relaxed) == 1), "no decoder restarted");
+ }
+
+ #[test]
+ fn intra_caches_give_up_their_spare_decoders_too() {
+ let pool = Arc::new(Pool::new(1, Duration::ZERO));
+ let (a, b) = (samples(50, 1, 0, true), samples(50, 1, 0, true));
+ let (ca, cb) = (GopCache::in_pool(None, pool.clone()), GopCache::in_pool(None, pool.clone()));
+ assert_eq!(index_of(&ca.frame(&a, 3_000).expect("frame")), 3);
+ assert_eq!(index_of(&cb.frame(&b, 4_000).expect("frame")), 4);
+ let st = ca.shared.state.lock().unwrap();
+ assert!(st.decoder.is_none() && st.spare.is_empty());
+ drop(st);
+ assert_eq!(pool.decoders.load(Ordering::Relaxed), 1);
+ assert_eq!(index_of(&ca.frame(&a, 5_000).expect("frame")), 5);
+ }
}
diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs
index 13403a40e..7bcd5fc67 100644
--- a/crates/codecs/src/lib.rs
+++ b/crates/codecs/src/lib.rs
@@ -31,7 +31,7 @@ pub mod video;
use std::sync::{Arc, RwLock};
pub use audio::AudioFileSource;
-pub use gop::{GopStats, gop_stats};
+pub use gop::{GopStats, gop_stats, live_decoders};
pub use mkv::MkvSource;
pub use mp4::Mp4Source;
pub use mpeg::MpegSource;
diff --git a/crates/engine/src/perf.rs b/crates/engine/src/perf.rs
index 39695908b..fef35368f 100644
--- a/crates/engine/src/perf.rs
+++ b/crates/engine/src/perf.rs
@@ -21,6 +21,8 @@ pub fn decode_json() -> Value {
"draftFrames": g.draft,
"h264Threads": filmcraft_codecs::video::h264_threads(),
"evicted": g.evicted,
+ // sources that hold a decoder right now (idle ones give theirs up beyond a cap)
+ "liveDecoders": filmcraft_codecs::live_decoders(),
"decodeMs": g.decode_ns as f64 / 1e6,
"decodeMsPerSample": g.decode_ms_per_sample(),
"framesDecoded": g.frames,
@@ -71,6 +73,7 @@ mod tests {
"decodeMs",
"decodeMsPerSample",
"framesDecoded",
+ "liveDecoders",
] {
assert!(v["decode"][k].is_number(), "decode.{k} in {v}");
}
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index b473892b1..0f35e03df 100644
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@ -29,7 +29,7 @@ Methods (handlers in `crates/ui-egui/src/control.rs`):
| `ui.playback` | `{action: play|stop|toggle, speed?}` | |
| `ui.screenshot` | `{path?, panel?}` | PNG of the window or one panel; fails after 10 s when no frame is presented (window hidden, display asleep) |
| `ui.resize`, `ui.focus`, `app.quit` | | `ui.focus` is the only request that activates the app and takes keyboard focus |
-| `perf.stats` | – | performance counters (also the command `perf.stats`, so `engine.execute` and MCP `command_run` reach it; headless sessions return the engine part). `decode`: GOP-cache requests / hits / `cacheHitRate`, decoder seeks, samples decoded and skipped while catching up, `draftFrames` (decoded in draft mode), `h264Threads` (frame-threading workers per H.264 decoder), decoder ms (total and per sample), `framesDecoded`, `hardware` (Settings ▸ Playback ▸ Hardware decoding: `enabled`, pictures from hardware decoders `frames` vs `softwareFrames`, `sessions`, `declined` streams, mid-stream `fallbacks`); `playback`: playing, shown / dropped frames and `dropRate` of the current or last play, resolution, `draftDecode` (Settings ▸ Playback ▸ Draft decoding); `frames`: frame-worker jobs, cancelled, `requestHitRate`, `decodeMs` / `renderMs` (mean, p50, p95 of the last 256 jobs), queue, render-cost estimate, cache use; `ui`: fps and frame ms; `process`: CPU seconds; `media`, `jobs`. Counters are cumulative: diff two readings to measure an interval ([performance.md](performance.md)) |
+| `perf.stats` | – | performance counters (also the command `perf.stats`, so `engine.execute` and MCP `command_run` reach it; headless sessions return the engine part). `decode`: GOP-cache requests / hits / `cacheHitRate`, decoder seeks, samples decoded and skipped while catching up, `draftFrames` (decoded in draft mode), `h264Threads` (frame-threading workers per H.264 decoder), `liveDecoders` (sources holding a decoder now), decoder ms (total and per sample), `framesDecoded`, `hardware` (Settings ▸ Playback ▸ Hardware decoding: `enabled`, pictures from hardware decoders `frames` vs `softwareFrames`, `sessions`, `declined` streams, mid-stream `fallbacks`); `playback`: playing, shown / dropped frames and `dropRate` of the current or last play, resolution, `draftDecode` (Settings ▸ Playback ▸ Draft decoding); `frames`: frame-worker jobs, cancelled, `requestHitRate`, `decodeMs` / `renderMs` (mean, p50, p95 of the last 256 jobs), queue, render-cost estimate, cache use; `ui`: fps and frame ms; `process`: CPU seconds; `media`, `jobs`. Counters are cumulative: diff two readings to measure an interval ([performance.md](performance.md)) |
**Focus.** Driving the app never steals the user's keyboard. Started with `--control`, the app opens
without activating itself. UI requests and screenshots that need a rendered frame bring the window
diff --git a/docs/performance.md b/docs/performance.md
index 1b09b5ac2..3d34ff669 100644
--- a/docs/performance.md
+++ b/docs/performance.md
@@ -26,6 +26,14 @@ to a 12 GB footprint, 10.8 GB of it live heap in three piles.
then single samples with positional reads, as the web host already did through `BlobReader`
([web.md](web.md)). Formats decoded in one go (stills, WAV, MPEG elementary streams) are still
read whole through the reader.
+2. **A decoder per clip, for good (3.5 GB), MEM2.** Every source kept its decoder once it had
+ decoded a frame, with its reference pictures and the pictures its frame threads have in flight:
+ about 290 MB per clip here (`h264` `PicState`, `Frame::make_row`). The GOP caches now share a
+ pool (`crates/codecs/src/gop.rs`): beyond `MAX_LIVE_DECODERS` (4) the least recently used
+ caches that have been idle for 3 s drop their decoder and make a new one when asked again,
+ which costs what a seek costs. A cache in use (a layer of the frame being composited, the next
+ clip being prefetched) always keeps its decoder, so a composite of more than four clips does
+ not restart decoders on every frame. `perf.stats` reports `decode.liveDecoders`.
## Results (GPU2: standard effects on the GPU compositor, #30, before → after)