Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

Merge branch 'main' into arch/frame-cache

Jareer9d1a5b355b17
diff --cc src/crates/concat-media/src/decode.rs
index c232c85c,fc310a48..e3864c32
--- a/src/crates/concat-media/src/decode.rs
+++ b/src/crates/concat-media/src/decode.rs
@@@ -101,12 -112,13 +112,19 @@@ pub struct DecodeOptions
/// decoded frame. Never for anything a person will watch or export: the
/// frame is near the instant, not at it.
pub keyframes_only: bool,
+ /// Threads the codec may use, or zero to let it count the cores.
+ ///
+ /// Zero for anything a person is waiting on: a scrub wants its frame
+ /// on every core. A few for a proxy being written in the background,
+ /// where the whole point is that the machine stays usable meanwhile.
+ pub threads: u16,
+ /// Whether to decode on the platform's video hardware. The default
+ /// follows the process-wide preference, [`crate::set_hardware_decode`],
+ /// so the window's toggle reaches every reader without being passed
+ /// through each one; a reader can insist on software or on a device.
+ /// A device that fails is never an error: the reader carries on in
+ /// software. See [`crate::hardware`].
+ pub hardware: HwPolicy,
}
impl DecodeOptions {
@@@ -160,11 -172,18 +178,24 @@@
self
}
+ /// Limits the codec to `threads`. See [`DecodeOptions::threads`].
+ pub fn threaded(mut self, threads: u16) -> Self {
+ self.threads = threads;
+ self
+ }
++
+ /// Decodes on `device`, whatever the preference says, and in software
+ /// if the device will not. See [`DecodeOptions::hardware`].
+ pub fn accelerated(mut self, device: HwDevice) -> Self {
+ self.hardware = HwPolicy::Device(device);
+ self
+ }
+
+ /// Decodes in software, whatever the preference says.
+ pub fn in_software(mut self) -> Self {
+ self.hardware = HwPolicy::Software;
+ self
+ }
}
/// How a stream says its colour is encoded: the tags in its container,
@@@ -405,6 -429,45 +441,46 @@@ impl Decoder
Ok(this)
}
+ /// Opens the stream's codec, on `device` where the codec can decode on
+ /// it. Gives back the opened decoder and, when it is on the device, the
+ /// device and the pixel format its frames arrive in.
+ fn open_codec(
+ path: &Path,
+ stream: &ffmpeg::format::stream::Stream<'_>,
+ options: &DecodeOptions,
+ device: Option<&Device>,
+ ) -> Result<(decoder::Video, Option<(HwDevice, Pixel)>)> {
+ let mut context = ffmpeg::codec::Context::from_parameters(stream.parameters())
+ .map_err(|error| ffi::fail("codec parameters", path, error))?;
+ // Every core. libavcodec's API opens a decoder on one thread unless
+ // told otherwise (the `ffmpeg` tool turns threads on for itself), and
+ // a seek decodes every frame from the keyframe before it: on one
+ // thread a 4K H.264 file decoded at about 87 frames a second, and a
+ // scrub waited 400 ms for its frame. Frame threads for that walk,
- // slice threads for files cut into slices; zero lets the codec count.
++ // slice threads for files cut into slices; zero lets the codec
++ // count, and a caller working in the background says how many.
+ // SAFETY: the context is not opened yet, which is when threading is
+ // set, and both fields are plain integers.
+ unsafe {
+ let raw = context.as_mut_ptr();
+ (*raw).thread_type = ffmpeg::sys::FF_THREAD_FRAME | ffmpeg::sys::FF_THREAD_SLICE;
- (*raw).thread_count = 0;
++ (*raw).thread_count = i32::from(options.threads);
+ }
+ let accelerated = device.and_then(|device| {
+ device
+ .attach(&mut context)
+ .map(|format| (device.kind(), format))
+ });
+ let mut decoder = context
+ .decoder()
+ .video()
+ .map_err(|error| ffi::fail("open decoder", path, error))?;
+ if options.keyframes_only {
+ decoder.skip_frame(ffmpeg::Discard::NonKey);
+ }
+ Ok((decoder, accelerated))
+ }
+
/// How many frames have been produced so far.
pub const fn produced(&self) -> u64 {
self.produced
diff --cc src/crates/concat-media/src/lib.rs
index 45862202,c64842ce..9085d8aa
--- a/src/crates/concat-media/src/lib.rs
+++ b/src/crates/concat-media/src/lib.rs
@@@ -18,9 -18,9 +18,10 @@@ pub mod decode
pub mod encode;
pub mod error;
mod ffi;
+ pub mod hardware;
pub mod peaks;
pub mod pool;
+pub mod prefetch;
pub mod probe;
pub mod samples;
pub mod treat;
@@@ -29,9 -29,9 +30,10 @@@ pub use decode::{ColorSignal, DecodeOpt
pub use encode::{EncodeOptions, Encoder, FrameSink, VideoCodec, jpeg};
pub use error::{Error, Result};
pub use ffi::{init, linked_version};
+ pub use hardware::{HwDevice, HwPolicy, hardware_decode, set_hardware_decode};
pub use peaks::Peaks;
-pub use pool::{FrameCache, ReaderPool};
+pub use pool::{CacheStats, FrameCache, FrameRequest, ReaderPool};
+pub use prefetch::{Cursor, Direction, Moment, Prefetcher, Priority};
pub use probe::{AudioStream, MediaInfo, VideoStream, probe};
pub use samples::{AudioDecoder, AudioOptions, SampleFormat};
-pub use treat::{treat, treat_to};
+pub use treat::{fit, treat, treat_to};