Skip to content

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

MEM3: one frame budget shared by all sources

justjohn123454486652f075f
diff --git a/crates/codecs/src/gop.rs b/crates/codecs/src/gop.rs
index 603c8c8a3..3db8d389b 100644
--- a/crates/codecs/src/gop.rs
+++ b/crates/codecs/src/gop.rs
@@ -155,6 +155,17 @@ impl State {
out
}
+ /// Evict frames, earliest first, while `more` says so.
+ fn release_frames(&mut self, pool: &Pool, more: impl Fn() -> bool) {
+ while more() {
+ let Some((pts, f)) = self.frames.pop_first() else { break };
+ self.drafts.remove(&pts);
+ self.bytes = self.bytes.saturating_sub(f.byte_size());
+ pool.bytes.fetch_sub(f.byte_size(), Ordering::Relaxed);
+ EVICTED.fetch_add(1, Ordering::Relaxed);
+ }
+ }
+
/// 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))
@@ -204,13 +215,21 @@ const MAX_LIVE_DECODERS: usize = 4;
/// 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.
+/// Decoded frames all caches may hold together before idle ones give theirs up. Each cache also
+/// has its own budget, which alone let every clip on a timeline keep hundreds of MB of frames
+/// nobody was looking at.
+pub const FRAME_BUDGET: usize = 1 << 30;
+
+/// What the caches of a process share: the cap on live decoders and the frame budget.
struct Pool {
caches: Mutex<Vec<Weak<Shared>>>,
/// Caches that hold a decoder.
decoders: AtomicUsize,
max_decoders: usize,
idle: Duration,
+ /// Bytes of the frames the caches hold.
+ bytes: AtomicUsize,
+ budget: usize,
}
/// The part of a [`GopCache`] its pool reaches.
@@ -223,6 +242,7 @@ 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));
+ self.pool.bytes.fetch_sub(st.bytes, Ordering::Relaxed);
}
}
@@ -236,14 +256,14 @@ fn try_state(c: &Shared) -> Option<MutexGuard<'_, State>> {
}
impl Pool {
- fn new(max_decoders: usize, idle: Duration) -> Self {
- Self { caches: Mutex::new(Vec::new()), decoders: AtomicUsize::new(0), max_decoders, idle }
+ fn new(max_decoders: usize, idle: Duration, budget: usize) -> Self {
+ Self { caches: Mutex::new(Vec::new()), decoders: AtomicUsize::new(0), max_decoders, idle, bytes: AtomicUsize::new(0), budget }
}
/// 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()
+ POOL.get_or_init(|| Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET))).clone()
}
fn register(&self, c: &Arc<Shared>) {
@@ -282,6 +302,24 @@ impl Pool {
drop(released);
}
}
+
+ /// Evict the frames of idle caches, least recently used first, down to the budget. Caches in
+ /// use keep theirs (each within its own budget): a frame evicted before it is shown costs a
+ /// re-decode from the keyframe.
+ fn trim_frames(&self, me: &Shared) {
+ let over = || self.bytes.load(Ordering::Relaxed) > self.budget;
+ if !over() {
+ return;
+ }
+ for c in self.idle_others(me, |st| !st.frames.is_empty()) {
+ if !over() {
+ break;
+ }
+ if let Some(mut st) = try_state(&c) {
+ st.release_frames(self, over);
+ }
+ }
+ }
}
/// Caches that hold a decoder right now, process-wide (`perf.stats`).
@@ -289,6 +327,11 @@ pub fn live_decoders() -> usize {
Pool::global().decoders.load(Ordering::Relaxed)
}
+/// Bytes of decoded frames the caches hold right now, process-wide (`perf.stats`).
+pub fn cached_bytes() -> usize {
+ Pool::global().bytes.load(Ordering::Relaxed)
+}
+
/// Decoder + decoded-frame cache for one video track.
pub struct GopCache {
shared: Arc<Shared>,
@@ -312,7 +355,7 @@ const CONTINUE_THROUGH: usize = 48;
impl GopCache {
pub fn new(explicit_color: Option<ColorInfo>) -> 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() };
+ let pool = if cfg!(test) { Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET)) } else { Pool::global() };
Self::in_pool(explicit_color, pool)
}
@@ -363,6 +406,7 @@ impl GopCache {
st.drafts.remove(&pts);
}
let budget = self.budget.max(MIN_FRAMES * f.byte_size());
+ let before = st.bytes;
st.bytes += f.byte_size();
if let Some(old) = st.frames.insert(pts, Arc::new(f)) {
st.bytes -= old.byte_size();
@@ -377,6 +421,13 @@ impl GopCache {
EVICTED.fetch_add(1, Ordering::Relaxed);
}
}
+ let pool = &self.shared.pool;
+ if st.bytes >= before {
+ pool.bytes.fetch_add(st.bytes - before, Ordering::Relaxed);
+ } else {
+ pool.bytes.fetch_sub(before - st.bytes, Ordering::Relaxed);
+ }
+ pool.trim_frames(&self.shared);
}
/// Store decoder output (in presentation order) and advance `out_max`.
@@ -839,7 +890,7 @@ mod tests {
#[test]
fn idle_caches_give_up_their_decoders_beyond_the_cap() {
- let pool = Arc::new(Pool::new(2, Duration::ZERO));
+ let pool = Arc::new(Pool::new(2, Duration::ZERO, FRAME_BUDGET));
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();
@@ -868,7 +919,7 @@ mod tests {
#[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 pool = Arc::new(Pool::new(2, Duration::from_secs(3600), FRAME_BUDGET));
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] {
@@ -882,7 +933,7 @@ mod tests {
#[test]
fn intra_caches_give_up_their_spare_decoders_too() {
- let pool = Arc::new(Pool::new(1, Duration::ZERO));
+ let pool = Arc::new(Pool::new(1, Duration::ZERO, FRAME_BUDGET));
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);
@@ -893,4 +944,44 @@ mod tests {
assert_eq!(pool.decoders.load(Ordering::Relaxed), 1);
assert_eq!(index_of(&ca.frame(&a, 5_000).expect("frame")), 5);
}
+
+ #[test]
+ fn idle_caches_give_up_their_frames_beyond_the_shared_budget() {
+ // 1×1 RGBA pictures: 4 bytes each, a shared budget of ten of them
+ let pool = Arc::new(Pool::new(usize::MAX, Duration::ZERO, 40));
+ let held = || pool.bytes.load(Ordering::Relaxed);
+ let (a, b) = (samples(300, 250, 0, false), samples(300, 250, 0, false));
+ let (ca, cb) = (GopCache::in_pool(None, pool.clone()), GopCache::in_pool(None, pool.clone()));
+ // the only cache keeps what its own budget allows
+ assert_eq!(index_of(&ca.frame(&a, 10_000).expect("frame")), 10);
+ assert_eq!(held(), 44);
+ // a second clip takes the room from the idle first one, earliest frames first
+ assert_eq!(index_of(&cb.frame(&b, 5_000).expect("frame")), 5);
+ assert_eq!(held(), 40);
+ let decodes = b.decodes.load(Ordering::Relaxed);
+ assert!((0..=5).all(|i| index_of(&cb.frame(&b, i * 1000).expect("cached")) as i64 == i));
+ assert_eq!(b.decodes.load(Ordering::Relaxed), decodes, "the clip in use lost nothing");
+ let st = ca.shared.state.lock().unwrap();
+ assert_eq!((st.frames.len(), st.bytes), (4, 16));
+ assert_eq!(st.frames.keys().next(), Some(&7_000));
+ drop(st);
+ // an evicted frame decodes again
+ assert_eq!(index_of(&ca.frame(&a, 2_000).expect("frame")), 2);
+ assert!(held() <= 40 + 4 * 3, "{} bytes held", held());
+ drop((ca, cb));
+ assert_eq!(held(), 0);
+ }
+
+ #[test]
+ fn caches_in_use_keep_their_frames_whatever_the_shared_budget() {
+ let pool = Arc::new(Pool::new(usize::MAX, Duration::from_secs(3600), 40));
+ let (a, b) = (samples(300, 250, 0, false), samples(300, 250, 0, false));
+ let (ca, cb) = (GopCache::in_pool(None, pool.clone()), GopCache::in_pool(None, pool.clone()));
+ assert_eq!(index_of(&ca.frame(&a, 10_000).expect("frame")), 10);
+ assert_eq!(index_of(&cb.frame(&b, 10_000).expect("frame")), 10);
+ assert_eq!(pool.bytes.load(Ordering::Relaxed), 88);
+ let decodes = a.decodes.load(Ordering::Relaxed);
+ assert_eq!(index_of(&ca.frame(&a, 0).expect("cached")), 0);
+ assert_eq!(a.decodes.load(Ordering::Relaxed), decodes);
+ }
}
diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs
index 7bcd5fc67..bd9710daf 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, live_decoders};
+pub use gop::{FRAME_BUDGET, GopStats, cached_bytes, 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 fef35368f..98339a92b 100644
--- a/crates/engine/src/perf.rs
+++ b/crates/engine/src/perf.rs
@@ -23,6 +23,9 @@ pub fn decode_json() -> Value {
"evicted": g.evicted,
// sources that hold a decoder right now (idle ones give theirs up beyond a cap)
"liveDecoders": filmcraft_codecs::live_decoders(),
+ // decoded frames all sources hold, and the budget idle sources are trimmed to
+ "cacheMB": filmcraft_codecs::cached_bytes() as f64 / 1e6,
+ "cacheBudgetMB": filmcraft_codecs::FRAME_BUDGET as f64 / 1e6,
"decodeMs": g.decode_ns as f64 / 1e6,
"decodeMsPerSample": g.decode_ms_per_sample(),
"framesDecoded": g.frames,
@@ -74,6 +77,8 @@ mod tests {
"decodeMsPerSample",
"framesDecoded",
"liveDecoders",
+ "cacheMB",
+ "cacheBudgetMB",
] {
assert!(v["decode"][k].is_number(), "decode.{k} in {v}");
}
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index 0f35e03df..55954846d 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), `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)) |
+| `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), `cacheMB` / `cacheBudgetMB` (decoded frames all sources hold, and the shared budget), 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 3d34ff669..b4fc202df 100644
--- a/docs/performance.md
+++ b/docs/performance.md
@@ -34,6 +34,18 @@ to a 12 GB footprint, 10.8 GB of it live heap in three piles.
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`.
+3. **A frame budget per clip, none overall (3.8 GB), MEM3.** Each GOP cache keeps up to 384 MB
+ of decoded frames (at least 64 frames), which every clip on the timeline filled and kept. The
+ pool now counts the frames of all caches against `FRAME_BUDGET` (1 GiB): over it, caches idle
+ for 3 s give up frames, least recently used cache first, earliest frames first. Caches in use
+ keep their own budget, because a frame evicted before it is shown costs a re-decode from the
+ keyframe. `perf.stats` reports `decode.cacheMB` and `decode.cacheBudgetMB`; before, this
+ memory appeared in no counter (`frames.cache` covers the frame server's rendered images and
+ GPU plans only).
+
+What remains per live decoder is unchanged: the H.264 decoder publishes each macroblock row of a
+picture as its own allocations (`Frame::make_row`, five per row), which is where the 290 MB per
+decoder comes from.
## Results (GPU2: standard effects on the GPU compositor, #30, before → after)