The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
A frame the device already holds is not uploaded again, and jobs run on kept workers
Jareer92b70c7c51c2
diff --git a/engine/crates/concat-core/src/frame.rs b/engine/crates/concat-core/src/frame.rsindex 0324149e..77083f3a 100644--- a/engine/crates/concat-core/src/frame.rs+++ b/engine/crates/concat-core/src/frame.rs@@ -13,23 +13,62 @@//! that every call site has to branch on.use std::fmt;+use std::sync::atomic::{AtomicU64, Ordering};/// Bytes per pixel in the one format Concat uses.pub const BYTES_PER_PIXEL: usize = 4;+/// The next frame identity; see [`Frame::id`].+static NEXT_ID: AtomicU64 = AtomicU64::new(1);++fn mint() -> u64 {+ NEXT_ID.fetch_add(1, Ordering::Relaxed)+}+/// An RGBA8 image.////// Width and height are fixed at construction, and the buffer is always/// exactly `width * height * 4` bytes, so indexing arithmetic cannot go wrong/// after the fact.-#[derive(Clone, PartialEq, Eq)]pub struct Frame {width: u32,height: u32,pixels: Vec<u8>,+ /// Which picture this is; see [`Frame::id`].+ id: u64,+}++impl Clone for Frame {+ /// A copy is a picture of its own: it may be changed next, and a+ /// renderer holding the original must not take it for the copy.+ fn clone(&self) -> Self {+ Self {+ width: self.width,+ height: self.height,+ pixels: self.pixels.clone(),+ id: mint(),+ }+ }+}++impl PartialEq for Frame {+ /// The same picture, whatever its identity.+ fn eq(&self, other: &Self) -> bool {+ self.width == other.width && self.height == other.height && self.pixels == other.pixels+ }}+impl Eq for Frame {}+impl Frame {+ /// A number no other frame has had, and this one loses the moment its+ /// pixels are touched: a renderer keys what it has uploaded by it, so a+ /// picture it already holds - a still, a title, a paused clip's frame+ /// - is not uploaded again. Never zero.+ pub fn id(&self) -> u64 {+ self.id+ }+/// How many bytes a frame of this size occupies.pub const fn byte_len(width: u32, height: u32) -> usize {width as usize * height as usize * BYTES_PER_PIXEL@@ -45,6 +84,7 @@ impl Frame {width,height,pixels,+ id: mint(),}}@@ -54,6 +94,7 @@ impl Frame {width,height,pixels: vec![0u8; Self::byte_len(width, height)],+ id: mint(),}}@@ -66,6 +107,7 @@ impl Frame {width,height,pixels,+ id: mint(),})}@@ -89,8 +131,10 @@ impl Frame {&self.pixels}- /// The raw RGBA bytes, mutably.+ /// The raw RGBA bytes, mutably. A new identity with them: whatever is+ /// written makes this a different picture from the one uploaded.pub fn pixels_mut(&mut self) -> &mut [u8] {+ self.id = mint();&mut self.pixels}@@ -121,6 +165,7 @@ impl Frame {/// Writes one pixel. Out-of-bounds writes are ignored.pub fn set_pixel(&mut self, x: u32, y: u32, rgba: [u8; 4]) {+ self.id = mint();if let Some(offset) = self.offset_of(x, y) {self.pixels[offset..offset + BYTES_PER_PIXEL].copy_from_slice(&rgba);}@@ -130,6 +175,7 @@ impl Frame {/// clipping whatever falls outside. No blending: the source's pixels/// replace what was there.pub fn blit(&mut self, source: &Frame, x: u32, y: u32) {+ self.id = mint();let width = self.width;let height = self.height;let columns = source.width.min(width.saturating_sub(x)) as usize;@@ -149,6 +195,7 @@ impl Frame {/// Fills the whole frame with one colour.pub fn fill(&mut self, rgba: [u8; 4]) {+ self.id = mint();for pixel in self.pixels.chunks_exact_mut(BYTES_PER_PIXEL) {pixel.copy_from_slice(&rgba);}diff --git a/engine/crates/concat-render/src/gpu.rs b/engine/crates/concat-render/src/gpu.rsindex 62be0009..ce439e4a 100644--- a/engine/crates/concat-render/src/gpu.rs+++ b/engine/crates/concat-render/src/gpu.rs@@ -109,6 +109,10 @@ struct CompiledShader {struct PooledTexture {texture: wgpu::Texture,bind_group: wgpu::BindGroup,+ /// The identity of the frame uploaded into it, or zero for a texture+ /// a pass drew: what lets a frame the device already holds - a still,+ /// a title, a paused clip - skip its upload.+ holds: u64,}/// The reusable output target and its readback buffer, for one output size.@@ -745,10 +749,24 @@ impl WgpuCompositor {self.target.as_ref().expect("just ensured")}- /// Claims a pooled texture of the layer's size, uploading its pixels.+ /// Claims a pooled texture of the layer's size holding the frame's+ /// pixels: the one that already does, moved into this frame's claimed+ /// run, or a fresh claim with the pixels uploaded into it.fn upload(&mut self, frame: &Frame) -> usize {+ let key = (frame.width(), frame.height());+ let identity = frame.id();+ let used = self.used.get(&key).copied().unwrap_or(0);+ if let Some(pool) = self.pool.get_mut(&key)+ && let Some(found) = (used..pool.len()).find(|&slot| pool[slot].holds == identity)+ {+ pool.swap(used, found);+ *self.used.entry(key).or_insert(0) = used + 1;+ return used;+ }let index = self.claim(frame.width(), frame.height());- let texture = &self.pool[&(frame.width(), frame.height())][index].texture;+ let pooled = &mut self.pool.get_mut(&key).expect("just claimed")[index];+ pooled.holds = identity;+ let texture = &pooled.texture;self.queue.write_texture(wgpu::TexelCopyTextureInfo {texture,@@ -814,11 +832,14 @@ impl WgpuCompositor {pool.push(PooledTexture {texture,bind_group,+ holds: 0,});}let index = *used;*used += 1;+ // Whatever is drawn into it next is not the frame it held.+ pool[index].holds = 0;index}diff --git a/engine/crates/concat/src/host.rs b/engine/crates/concat/src/host.rsindex 67cc9378..81d1c183 100644--- a/engine/crates/concat/src/host.rs+++ b/engine/crates/concat/src/host.rs@@ -124,14 +124,53 @@ impl Shell {}}-/// Runs `work` on its own thread, then `then` on the event-loop thread with-/// the result, the state and the window - followed by a full publish, so a+/// The workers every background job runs on: a few threads kept for the+/// life of the process, fed from one queue. A thread per job was a thread+/// per monitor frame, thirty a second during playback.+fn workers() -> &'static std::sync::mpsc::Sender<Box<dyn FnOnce() + Send>> {+ static WORKERS: std::sync::OnceLock<std::sync::mpsc::Sender<Box<dyn FnOnce() + Send>>> =+ std::sync::OnceLock::new();+ WORKERS.get_or_init(|| {+ let (sender, receiver) = std::sync::mpsc::channel::<Box<dyn FnOnce() + Send>>();+ let receiver = std::sync::Arc::new(std::sync::Mutex::new(receiver));+ // Three: the monitor's frame, its prefetch, and whatever thumbnail+ // or analysis is running, without any of them queuing behind the+ // others.+ for index in 0..3 {+ let receiver = std::sync::Arc::clone(&receiver);+ std::thread::Builder::new()+ .name(format!("concat-worker-{index}"))+ .spawn(move || {+ loop {+ let job = receiver+ .lock()+ .unwrap_or_else(|poisoned| poisoned.into_inner())+ .recv();+ match job {+ Ok(job) => job(),+ Err(_) => return,+ }+ }+ })+ .expect("a worker thread");+ }+ sender+ })+}++/// Runs `work` on a worker with nothing to report: a prefetch, a warm-up.+pub fn spawn_detached(work: impl FnOnce() + Send + 'static) {+ let _ = workers().send(Box::new(work));+}++/// Runs `work` on a worker, then `then` on the event-loop thread with the+/// result, the state and the window - followed by a full publish, so a/// completion never has to remember to redraw.pub fn spawn<T: Send + 'static>(work: impl FnOnce() -> T + Send + 'static,then: impl FnOnce(&mut Studio, &App, &Models, T) + Send + 'static,) {- std::thread::spawn(move || {+ spawn_detached(move || {let result = work();let _ = slint::invoke_from_event_loop(move || {Shell::with(|shell, app| {diff --git a/engine/crates/concat/src/studio.rs b/engine/crates/concat/src/studio.rsindex a4db7a45..5d1b4e14 100644--- a/engine/crates/concat/src/studio.rs+++ b/engine/crates/concat/src/studio.rs@@ -1831,8 +1831,14 @@ impl Studio {.frame(std::sync::Arc::clone(&clips), &settings, spec).map(|bytes| Picture::Pixels(bytes, width, height))};- // Decode-ahead for whatever comes next, while the pool is warm.- monitor.prefetch(clips, &settings, spec, 2);+ // Decode-ahead for whatever comes next, on a worker of its+ // own, so the frame goes to the window without waiting for+ // it and the next frame can start meanwhile.+ {+ let monitor = monitor.clone();+ let settings = settings.clone();+ crate::host::spawn_detached(move || monitor.prefetch(clips, &settings, spec, 2));+ }frame},|studio, _, _, result| {