Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(export): add turbo mp4 export with parallel chunks

Richie McIlroy771b3456b163
diff --git a/Cargo.lock b/Cargo.lock
index b25b55e39..3fdbfcef0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1741,6 +1741,7 @@ dependencies = [
name = "cap-export"
version = "0.1.0"
dependencies = [
+ "anyhow",
"base64 0.22.1",
"cap-editor",
"cap-enc-ffmpeg",
@@ -1760,6 +1761,7 @@ dependencies = [
"image 0.25.8",
"imgref",
"inquire",
+ "libc",
"mp4",
"rgb",
"serde",
diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml
index 9a214783f..d24a3fb73 100644
--- a/crates/export/Cargo.toml
+++ b/crates/export/Cargo.toml
@@ -43,6 +43,10 @@ inquire = "0.7.5"
tokio.workspace = true
serde_json.workspace = true
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
+anyhow.workspace = true
+
+[target.'cfg(unix)'.dev-dependencies]
+libc = "0.2"
[lints]
workspace = true
diff --git a/crates/export/examples/export-turbo-benchmark.rs b/crates/export/examples/export-turbo-benchmark.rs
new file mode 100644
index 000000000..9a5110b03
--- /dev/null
+++ b/crates/export/examples/export-turbo-benchmark.rs
@@ -0,0 +1,134 @@
+//! Run: cargo run --release -p cap-export --example export-turbo-benchmark -- <project.cap> <output.mp4> [WIDTH] [HEIGHT] [FPS] [standard|turbo]
+
+use std::{
+ path::PathBuf,
+ sync::{Arc, atomic::AtomicBool},
+ time::Instant,
+};
+
+use cap_export::{
+ ExporterBase,
+ mp4::{ExportCompression, Mp4ExportSettings},
+};
+use cap_project::XY;
+
+/// CPU seconds used and peak resident bytes so far.
+#[cfg(unix)]
+fn usage() -> (f64, u64) {
+ let mut usage = std::mem::MaybeUninit::<libc::rusage>::zeroed();
+ unsafe { libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) };
+ let usage = unsafe { usage.assume_init() };
+ let secs = |time: libc::timeval| time.tv_sec as f64 + time.tv_usec as f64 / 1_000_000.0;
+ #[cfg(target_os = "macos")]
+ let peak = usage.ru_maxrss as u64;
+ #[cfg(not(target_os = "macos"))]
+ let peak = usage.ru_maxrss as u64 * 1024;
+ (secs(usage.ru_utime) + secs(usage.ru_stime), peak)
+}
+
+#[cfg(not(unix))]
+fn usage() -> (f64, u64) {
+ (0.0, 0)
+}
+
+async fn run(
+ project_path: PathBuf,
+ output_path: PathBuf,
+ size: XY<u32>,
+ fps: u32,
+ turbo: bool,
+) -> anyhow::Result<()> {
+ let _ = std::fs::remove_file(&output_path);
+ let cancel = Arc::new(AtomicBool::new(false));
+ let started = Instant::now();
+ let (cpu_started, _) = usage();
+ let base = ExporterBase::builder(project_path)
+ .with_output_path(output_path.clone())
+ .build_for_mp4(cancel)
+ .await
+ .map_err(|error| anyhow::anyhow!("{error}"))?;
+ let total = base.total_frames(fps);
+ let prepared_ms = started.elapsed().as_secs_f64() * 1000.0;
+ let settings = Mp4ExportSettings {
+ fps,
+ resolution_base: size,
+ compression: match std::env::var("TURBO_BENCH_COMPRESSION").as_deref() {
+ Ok("social") => ExportCompression::Social,
+ Ok("web") => ExportCompression::Web,
+ Ok("potato") => ExportCompression::Potato,
+ _ => ExportCompression::Maximum,
+ },
+ custom_bpp: None,
+ force_ffmpeg_decoder: false,
+ optimize_filesize: std::env::var_os("TURBO_BENCH_OPTIMIZE").is_some(),
+ };
+ let path = if turbo {
+ let options = cap_export::mp4::turbo::TurboOptions {
+ chunks: std::env::var("TURBO_BENCH_CHUNKS")
+ .ok()
+ .and_then(|value| value.parse().ok()),
+ strict: true,
+ };
+ settings
+ .export_prepared_turbo(base, options, |_| true)
+ .await
+ } else {
+ settings.export_prepared(base, |_| true).await
+ }
+ .map_err(|error| anyhow::anyhow!("{error}"))?;
+ let wall = started.elapsed().as_secs_f64();
+ let (cpu, peak) = usage();
+ let bytes = std::fs::metadata(&path).map(|meta| meta.len()).unwrap_or(0);
+ println!(
+ "{}",
+ serde_json::json!({
+ "mode": if turbo { "turbo" } else { "standard" },
+ "frames": total,
+ "width": size.x,
+ "height": size.y,
+ "fps": fps,
+ "prepareMs": prepared_ms,
+ "wallSecs": wall,
+ "exportFps": f64::from(total) / wall,
+ "cpuSecs": cpu - cpu_started,
+ "peakRssMb": peak as f64 / 1_048_576.0,
+ "bytes": bytes,
+ })
+ );
+ Ok(())
+}
+
+fn main() -> anyhow::Result<()> {
+ tracing_subscriber::fmt()
+ .with_env_filter(
+ tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "warn".into()),
+ )
+ .with_writer(std::io::stderr)
+ .init();
+
+ let mut arguments = std::env::args().skip(1);
+ let project_path = arguments
+ .next()
+ .map(PathBuf::from)
+ .ok_or_else(|| anyhow::anyhow!("Pass a studio .cap project path"))?;
+ let output_path = arguments
+ .next()
+ .map(PathBuf::from)
+ .ok_or_else(|| anyhow::anyhow!("Pass an output .mp4 path"))?;
+ let mut number = |default: u32| {
+ arguments
+ .next()
+ .and_then(|value| value.parse().ok())
+ .unwrap_or(default)
+ };
+ let size = XY::new(number(1920), number(1080));
+ let fps = number(60);
+ let turbo = std::env::args().any(|argument| argument == "turbo");
+
+ tokio::runtime::Builder::new_multi_thread()
+ .worker_threads(2)
+ .thread_stack_size(16 * 1024 * 1024)
+ .enable_all()
+ .build()?
+ .block_on(run(project_path, output_path, size, fps, turbo))
+}
diff --git a/crates/export/src/estimates.rs b/crates/export/src/estimates.rs
index 3d245e931..4d009312a 100644
--- a/crates/export/src/estimates.rs
+++ b/crates/export/src/estimates.rs
@@ -125,6 +125,31 @@ pub async fn estimate_export(
project: ProjectConfiguration,
settings: ExportSettings,
cancel: Arc<AtomicBool>,
+ on_estimate: impl FnMut(ExportEstimates) + Send,
+) -> Result<ExportEstimates, String> {
+ estimate(editor, project, settings, false, cancel, on_estimate).await
+}
+
+/// [`estimate_export`] for an MP4 exported with
+/// [`crate::mp4::Mp4ExportSettings::export_prepared_turbo`]: the sample runs
+/// through the turbo encoder, so its size and pace are turbo's.
+pub async fn estimate_turbo_export(
+ editor: Arc<EditorInstance>,
+ project: ProjectConfiguration,
+ settings: ExportSettings,
+ cancel: Arc<AtomicBool>,
+ on_estimate: impl FnMut(ExportEstimates) + Send,
+) -> Result<ExportEstimates, String> {
+ let turbo = matches!(settings, ExportSettings::Mp4(_));
+ estimate(editor, project, settings, turbo, cancel, on_estimate).await
+}
+
+async fn estimate(
+ editor: Arc<EditorInstance>,
+ project: ProjectConfiguration,
+ settings: ExportSettings,
+ turbo: bool,
+ cancel: Arc<AtomicBool>,
mut on_estimate: impl FnMut(ExportEstimates) + Send,
) -> Result<ExportEstimates, String> {
if cancel.load(Ordering::Acquire) || editor.export_active.load(Ordering::Acquire) {
@@ -177,6 +202,9 @@ pub async fn estimate_export(
serde_json::to_vec(&(&project, settings))
.map_err(|error| error.to_string())?
.hash(&mut hasher);
+ if turbo {
+ turbo.hash(&mut hasher);
+ }
let key = hasher.finish();
if let Ok(cache) = CACHE.lock()
&& let Some(cached) = cache
@@ -245,7 +273,11 @@ pub async fn estimate_export(
return Err("Recording sources changed during export estimation".into());
}
let setup_seconds = setup_started.elapsed().as_secs_f64();
- let plan = sample_plan(settings);
+ let plan = if turbo {
+ turbo_sample_plan(settings)
+ } else {
+ sample_plan(settings)
+ };
let windows = sample_windows(
total_frames,
plan.window_frames,
@@ -287,10 +319,13 @@ pub async fn estimate_export(
streaming_audio: None,
streaming_output: None,
audio_cancellation: None,
+ user_cancellation: None,
sample_windows: Some(windows.clone()),
sample_timing: Some(timing.clone()),
#[cfg(feature = "test-hooks")]
video_encoder_failure_at: None,
+ #[cfg(feature = "test-hooks")]
+ turbo_chunk_failure: None,
};
let progress_editor = editor.clone();
let progress_cancel = cancel.clone();
@@ -302,6 +337,9 @@ pub async fn estimate_export(
let started = Instant::now();
let export = async {
match settings {
+ ExportSettings::Mp4(settings) if turbo => {
+ crate::mp4::turbo::export_sampled(settings, base, on_progress).await
+ }
ExportSettings::Mp4(settings) => settings.export(base, on_progress).await,
ExportSettings::Gif(settings) => settings.export(base, on_progress).await,
ExportSettings::Mov(settings) => settings.export(base, on_progress).await,
@@ -447,6 +485,19 @@ fn sample_plan(settings: ExportSettings) -> SamplePlan {
}
}
+/// The turbo encoder is libx264 whether or not the file size is optimized,
+/// so every turbo sample spans a GOP the way an optimized one does.
+fn turbo_sample_plan(settings: ExportSettings) -> SamplePlan {
+ let keyframe_interval = keyframe_interval_frames(settings);
+ SamplePlan {
+ windows: 3,
+ window_frames: keyframe_interval.unwrap_or(settings.fps() * 2) + X264_WARMUP_FRAMES as u32,
+ leading_window: false,
+ timing: FrameTiming::GopAmortized,
+ keyframe_interval,
+ }
+}
+
fn keyframe_interval_frames(settings: ExportSettings) -> Option<u32> {
match settings {
ExportSettings::Mp4(settings) => Some(
diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs
index 805ace277..6c5dc3469 100644
--- a/crates/export/src/lib.rs
+++ b/crates/export/src/lib.rs
@@ -190,6 +190,7 @@ impl ExporterBuilder {
.map_err(Error::RendererSetup)?,
);
+ let user_cancellation = cancellation.clone();
let audio_cancellation = if stream_audio {
cancellation.map(ExportAudioCancellation::new)
} else {
@@ -254,10 +255,13 @@ impl ExporterBuilder {
streaming_audio,
streaming_output,
audio_cancellation,
+ user_cancellation,
sample_windows: None,
sample_timing: None,
#[cfg(feature = "test-hooks")]
video_encoder_failure_at: None,
+ #[cfg(feature = "test-hooks")]
+ turbo_chunk_failure: None,
})
}
}
@@ -449,6 +453,12 @@ impl Mp4ExporterBase {
pub fn fail_video_encoder_at(&mut self, frames: u32) {
self.0.fail_video_encoder_at(frames);
}
+
+ /// Makes this turbo chunk's encoder fail after a few frames.
+ #[cfg(feature = "test-hooks")]
+ pub fn fail_turbo_chunk(&mut self, chunk: usize) {
+ self.0.turbo_chunk_failure = Some(chunk);
+ }
}
pub struct ExporterBase {
@@ -463,10 +473,13 @@ pub struct ExporterBase {
streaming_audio: Option<ExportAudioRenderer>,
streaming_output: Option<mp4::TemporaryExportOutput>,
audio_cancellation: Option<ExportAudioCancellation>,
+ user_cancellation: Option<Arc<AtomicBool>>,
sample_windows: Option<cap_rendering::FrameWindows>,
sample_timing: Option<Arc<estimates::SampleTiming>>,
#[cfg(feature = "test-hooks")]
video_encoder_failure_at: Option<u32>,
+ #[cfg(feature = "test-hooks")]
+ turbo_chunk_failure: Option<usize>,
}
impl ExporterBase {
diff --git a/crates/export/src/mp4.rs b/crates/export/src/mp4.rs
index be25ee2e6..90be5f8c2 100644
--- a/crates/export/src/mp4.rs
+++ b/crates/export/src/mp4.rs
@@ -24,6 +24,8 @@ use std::{
use tokio::sync::Notify;
use tracing::{info, trace, warn};
+pub mod turbo;
+
enum Mp4PipelineError {
Interrupted,
Failure(String),
@@ -209,6 +211,46 @@ impl Mp4ExportSettings {
self.export(base.0, on_progress)
}
+ /// Exports with every core: see [`turbo`]. Falls back to the standard
+ /// export when the turbo pipeline fails for any reason but cancellation.
+ pub async fn export_prepared_turbo(
+ self,
+ base: Mp4ExporterBase,
+ options: turbo::TurboOptions,
+ on_progress: impl FnMut(u32) -> bool + Send + 'static,
+ ) -> Result<PathBuf, String> {
+ let mut base = base.0;
+ let on_progress = Arc::new(Mutex::new(on_progress));
+ let user_cancellation = base
+ .audio_cancellation
+ .as_ref()
+ .map(|control| control.user.clone());
+ match turbo::export(self, &mut base, options, on_progress.clone()).await {
+ Ok(path) => Ok(path),
+ Err(turbo::TurboError::Cancelled) => Err("Export cancelled".to_string()),
+ Err(turbo::TurboError::Failed(error)) if options.strict => Err(error),
+ Err(turbo::TurboError::Failed(error)) => {
+ warn!(%error, "Turbo export failed, exporting with the standard pipeline");
+ base.reopen_streaming(user_cancellation).await?;
+ let output_size = ProjectUniforms::get_output_size(
+ &base.render_constants.options,
+ &base.project_config,
+ self.resolution_base,
+ );
+ self.export_nv12(
+ base,
+ output_size,
+ self.fps,
+ move |frame| {
+ (on_progress.lock().unwrap_or_else(PoisonError::into_inner))(frame)
+ },
+ ExportNv12Mode::default(),
+ )
+ .await
+ }
+ }
+ }
+
pub async fn export(
self,
base: ExporterBase,
@@ -755,7 +797,9 @@ impl Mp4ExportSettings {
nv12_render_startup_breakdown_ms,
audio_cancellation.is_some(),
on_progress,
- base.project_path.clone(),
+ base.sample_windows
+ .is_none()
+ .then(|| base.project_path.clone()),
)
.then(move |v| async move {
let result = v.map_err(|error| match &error {
@@ -1576,11 +1620,10 @@ async fn export_render_to_channel(
startup_breakdown_ms: Option<Arc<Mutex<Option<cap_rendering::Nv12RenderStartupBreakdownMs>>>>,
stop_on_encoder_drop: bool,
mut on_progress: impl FnMut(u32) -> bool + Send + 'static,
- project_path: PathBuf,
+ screenshot_project_path: Option<PathBuf>,
) -> Result<(), cap_rendering::RenderingError> {
let (tx_image_data, mut video_rx) = tokio::sync::mpsc::channel::<(Nv12RenderedFrame, u32)>(8);
- let screenshot_project_path = frame_windows.is_none().then_some(project_path);
let sampling = frame_windows.is_some();
let render_result = {
diff --git a/crates/export/src/mp4/turbo.rs b/crates/export/src/mp4/turbo.rs
new file mode 100644
index 000000000..48f20c908
--- /dev/null
+++ b/crates/export/src/mp4/turbo.rs
@@ -0,0 +1,1160 @@
+//! Turbo export: the timeline is split into contiguous chunks that render and
+//! encode at the same time, each with its own decoders, render loop and
+//! libx264 encoder, so decode, GPU and every CPU core stay busy. Chunk 0
+//! encodes straight into the output like a standard export; later chunks
+//! spool their packets to temporary files that are appended in order.
+//!
+//! libx264 runs in constant quality capped at the export's bitrate. On Apple
+//! silicon the single hardware encoder is the standard export's ceiling,
+//! while libx264 across all cores is 2-3x faster at matching quality.
+
+use super::*;
+use crate::estimates::SampleTiming;
+use cap_editor::SegmentMedia;
+use cap_enc_ffmpeg::{PacketSink, h264::SoftwareEncoderTuning};
+use cap_project::{ProjectConfiguration, RecordingMeta, StudioRecordingMeta};
+use cap_rendering::{ProjectRecordingsMeta, RenderVideoConstants};
+use std::{
+ io::{BufReader, BufWriter, Read, Write},
+ ops::Range,
+ sync::mpsc,
+};
+
+/// Shorter chunks spend more of their time opening decoders and layers.
+const MIN_CHUNK_SECS: u32 = 4;
+const MAX_CHUNKS: usize = 4;
+const CORES_PER_CHUNK: usize = 4;
+
+/// Frames a later chunk renders and discards before its first frame, so
+/// state carried from frame to frame (the camera blur's mask smoothing)
+/// matches a standard export's at the seam.
+fn preroll_frames(fps: u32, start: u32) -> u32 {
+ (fps / 2).max(16).min(start)
+}
+
+#[derive(Clone, Copy, Debug, Default)]
+pub struct TurboOptions {
+ /// Overrides how many chunks run at once, however short they get.
+ pub chunks: Option<usize>,
+ /// Returns the turbo pipeline's error instead of exporting again with
+ /// the standard pipeline.
+ pub strict: bool,
+}
+
+pub(crate) enum TurboError {
+ Cancelled,
+ Failed(String),
+}
+
+impl From<String> for TurboError {
+ fn from(error: String) -> Self {
+ Self::Failed(error)
+ }
+}
+
+#[derive(Debug, PartialEq, Eq)]
+struct TurboPlan {
+ chunks: Vec<Range<u32>>,
+ threads: usize,
+}
+
+fn plan(total_frames: u32, fps: u32, cores: usize, requested: Option<usize>) -> TurboPlan {
+ let count = match requested {
+ Some(count) => count.clamp(1, MAX_CHUNKS).min(total_frames.max(1) as usize),
+ None => (cores / CORES_PER_CHUNK)
+ .clamp(1, MAX_CHUNKS)
+ .min((total_frames / (fps.max(1) * MIN_CHUNK_SECS)).max(1) as usize),
+ };
+ let length = total_frames / count as u32;
+ let remainder = total_frames % count as u32;
+ let mut start = 0;
+ let chunks = (0..count as u32)
+ .map(|index| {
+ let end = start + length + u32::from(index < remainder);
+ let chunk = start..end;
+ start = end;
+ chunk
+ })
+ .collect();
+ TurboPlan {
+ chunks,
+ threads: (cores / count).max(1),
+ }
+}
+
+/// libx264 constant quality per bits per pixel and frame rate. Each preset's
+/// value matched or beat the hardware encoder's SSIM and PSNR at that preset
+/// against a lossless render of real recordings, at a smaller file, at 15, 30
+/// and 60 fps. At 60 fps the hardware encoder spends its higher bitrate on
+/// repeated source frames that libx264 makes nearly free, so matching it
+/// takes 3 steps more quality; at 15 fps 2 steps less still beats it.
+fn constant_quality(bpp: f32, fps: u32) -> u8 {
+ const POINTS: [(f32, f32); 4] = [(0.04, 22.0), (0.08, 20.0), (0.15, 18.0), (0.3, 15.0)];
+ let position = bpp.max(f32::MIN_POSITIVE).log2();
+ let crf = POINTS
+ .windows(2)
+ .find_map(|pair| {
+ let (low, high) = (pair[0], pair[1]);
+ let (from, to) = (low.0.log2(), high.0.log2());
+ (position <= to).then(|| {
+ let t = ((position - from) / (to - from)).clamp(0.0, 1.0);
+ low.1 + (high.1 - low.1) * t
+ })
+ })
+ .unwrap_or(POINTS[POINTS.len() - 1].1);
+ let fps = fps as f32;
+ let offset = if fps >= 30.0 {
+ -3.0 * ((fps - 30.0) / 30.0).min(1.0)
+ } else {
+ 2.0 * ((30.0 - fps) / 15.0).min(1.0)
+ };
+ (crf + offset).round() as u8
+}
+
+struct Progress<F> {
+ frames: Mutex<(u32, F)>,
+ stop: Arc<AtomicBool>,
+ user: Option<Arc<AtomicBool>>,
+ cancelled: AtomicBool,
+ failure: Mutex<Option<String>>,
+}
+
+impl<F: FnMut(u32) -> bool> Progress<F> {
+ fn stopped(&self) -> bool {
+ self.stop.load(Ordering::Relaxed)
+ || self
+ .user
+ .as_ref()
+ .is_some_and(|user| user.load(Ordering::Relaxed))
+ }
+
+ fn frame_rendered(&self) -> bool {
+ if self.stopped() {
+ return false;
+ }
+ let mut frames = self.frames.lock().unwrap_or_else(PoisonError::into_inner);
+ let (rendered, on_progress) = &mut *frames;
+ let keep_going = on_progress(*rendered);
+ *rendered += 1;
+ if !keep_going {
+ self.cancelled.store(true, Ordering::Relaxed);
+ self.stop.store(true, Ordering::Relaxed);
+ }
+ keep_going
+ }
+
+ /// Stops every part of the export, keeping the first failure: the parts
+ /// it stops fail afterwards only because they were stopped.
+ fn fail(&self, error: String) {
+ let mut failure = self.failure.lock().unwrap_or_else(PoisonError::into_inner);
+ if failure.is_none() && !self.stopped() {
+ *failure = Some(error);
+ }
+ self.stop.store(true, Ordering::Relaxed);
+ }
+
+ fn was_cancelled(&self) -> bool {
+ self.cancelled.load(Ordering::Relaxed)
+ || self
+ .user
+ .as_ref()
+ .is_some_and(|user| user.load(Ordering::Relaxed))
+ }
+}
+
+/// Everything a chunk's render loop reads, shared by all chunks.
+#[derive(Clone)]
+struct RenderShared {
+ constants: Arc<RenderVideoConstants>,
+ project: Arc<ProjectConfiguration>,
+ recording_meta: Arc<RecordingMeta>,
+ studio_meta: Arc<StudioRecordingMeta>,
+ recordings: Arc<ProjectRecordingsMeta>,
+ fps: u32,
+ resolution_base: XY<u32>,
+}
+
+#[derive(Clone, Copy)]
+struct EncoderSettings {
+ video_info: VideoInfo,
+ bpp: f32,
+ optimize_filesize: bool,
+ crf: u8,
+ tuning: SoftwareEncoderTuning,
+}
+
+impl EncoderSettings {
+ fn builder(&self) -> cap_enc_ffmpeg::h264::H264EncoderBuilder {
+ let builder = H264Encoder::builder(self.video_info)
+ .with_bpp(self.bpp)
+ .with_export_settings()
+ .with_external_conversion()
+ .with_encoder_priority_override(&["libx264"])
+ .with_software_tuning(self.tuning);
+ if self.optimize_filesize {
+ builder.with_crf(self.crf)
+ } else {
+ builder
+ }
+ }
+}
+
+const STOPPED: &str = "Export cancelled";
+
+fn render_chunk<F: FnMut(u32) -> bool + Send + 'static>(
+ shared: RenderShared,
+ segments: Vec<RenderSegment>,
+ frames: cap_rendering::FrameWindows,
+ preroll: u32,
+ sender: mpsc::SyncSender<ExportFrame>,
+ progress: Arc<Progress<F>>,
+ screenshot_project_path: Option<PathBuf>,
+) -> std::io::Result<std::thread::JoinHandle<Result<(), String>>> {
+ std::thread::Builder::new()
+ .name("turbo-render".to_string())
+ .stack_size(16 * 1024 * 1024)
+ .spawn(move || {
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .map_err(|error| error.to_string())?;
+ let result = runtime
+ .block_on(export_render_to_channel(
+ &shared.constants,
+ &shared.project,
+ sender,
+ &shared.recording_meta,
+ &shared.studio_meta,
+ segments,
+ shared.fps,
+ shared.resolution_base,
+ &shared.recordings,
+ Some(frames),
+ None,
+ None,
+ true,
+ {
+ let progress = progress.clone();
+ move |frame| {
+ if frame < preroll {
+ !progress.stopped()
+ } else {
+ progress.frame_rendered()
+ }
+ }
+ },
+ screenshot_project_path,
+ ))
+ .map_err(|error| match error {
+ cap_rendering::RenderingError::ImageLoadError(message)
+ if message == STOPPED =>
+ {
+ STOPPED.to_string()
+ }
+ error => error.to_string(),
+ });
+ if let Err(error) = &result
+ && error != STOPPED
+ {
+ progress.fail(error.clone());
+ }
+ result
+ })
+}
+
+/// Copies a rendered frame into the encoder's NV12 input frame.
+fn fill_input(
+ input: &ExportFrame,
+ frame: &mut ffmpeg::frame::Video,
+ pts: i64,
+) -> Result<(), String> {
+ match &input.payload {
+ ExportFramePayload::Cpu(nv12_data) => {
+ fill_nv12_frame_direct(
+ frame,
+ nv12_data,
+ input.width,
+ input.height,
+ input.y_stride,
+ pts,
+ );
+ Ok(())
+ }
+ #[cfg(target_os = "macos")]
+ ExportFramePayload::Surface(surface) => {
+ fill_nv12_frame_from_surface(frame, surface, input.width, input.height, pts)
+ .ok_or_else(|| "failed to lock NV12 surface for encoding".to_string())
+ }
+ }
+}
+
+/// Packets of one chunk, written as they are encoded and read back once the
+/// output reaches the chunk. Timestamps are frames from the chunk's start.
+struct Spool {
+ path: tempfile::TempPath,
+ writer: BufWriter<std::fs::File>,
+ time_base: ffmpeg::Rational,
+ first_packet: Option<Vec<u8>>,
+ error: Option<std::io::Error>,
+}
+
+struct SpooledChunk {
+ path: tempfile::TempPath,
+ first_packet: Option<Vec<u8>>,
+}
+
+impl Spool {
+ fn new(time_base: ffmpeg::Rational) -> std::io::Result<Self> {
+ let (file, path) = tempfile::Builder::new()
+ .prefix("cap-turbo-")
+ .suffix(".packets")
+ .tempfile()?
+ .into_parts();
+ Ok(Self {
+ path,
+ writer: BufWriter::with_capacity(1 << 20, file),
+ time_base,
+ first_packet: None,
+ error: None,
+ })
+ }
+
+ fn write(&mut self, packet: &ffmpeg::Packet) -> std::io::Result<()> {
+ let data = packet.data().unwrap_or_default();
+ if self.first_packet.is_none() {
+ self.first_packet = Some(data.to_vec());
+ }
+ let mut header = [0u8; 29];
+ header[0..8].copy_from_slice(&packet.pts().unwrap_or(0).to_le_bytes());
+ header[8..16].copy_from_slice(&packet.dts().unwrap_or(0).to_le_bytes());
+ header[16..24].copy_from_slice(&packet.duration().to_le_bytes());
+ header[24] = u8::from(packet.is_key());
+ header[25..29].copy_from_slice(&(data.len() as u32).to_le_bytes());
+ self.writer.write_all(&header)?;
+ self.writer.write_all(data)
+ }
+
+ fn finish(mut self) -> std::io::Result<SpooledChunk> {
+ if let Some(error) = self.error.take() {
+ return Err(error);
+ }
+ self.writer.flush()?;
+ Ok(SpooledChunk {
+ path: self.path,
+ first_packet: self.first_packet,
+ })
+ }
+}
+
+impl PacketSink for Spool {
+ fn stream_time_base(&self, _stream_index: usize) -> ffmpeg::Rational {
+ self.time_base
+ }
+
+ fn write_packet(&mut self, packet: ffmpeg::Packet) -> Result<(), ffmpeg::Error> {
+ if self.error.is_some() {
+ return Err(ffmpeg::Error::External);
+ }
+ self.write(&packet).map_err(|error| {
+ self.error = Some(error);
+ ffmpeg::Error::External
+ })
+ }
+}
+
+fn read_spooled_packets(
+ chunk: &SpooledChunk,
+ mut packet: impl FnMut(ffmpeg::Packet) -> Result<(), TurboError>,
+) -> Result<(), TurboError> {
+ let file = std::fs::File::open(&chunk.path).map_err(|error| error.to_string())?;
+ let mut reader = BufReader::with_capacity(1 << 20, file);
+ let mut header = [0u8; 29];
+ loop {
+ match reader.read_exact(&mut header) {
+ Ok(()) => {}
+ Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(()),
+ Err(error) => return Err(error.to_string().into()),
+ }
+ let field = |range: Range<usize>| i64::from_le_bytes(header[range].try_into().unwrap());
+ let length = u32::from_le_bytes(header[25..29].try_into().unwrap()) as usize;
+ let mut data = vec![0u8; length];
+ reader
+ .read_exact(&mut data)
+ .map_err(|error| error.to_string())?;
+ let mut spooled = ffmpeg::Packet::copy(&data);
+ spooled.set_pts(Some(field(0..8)));
+ spooled.set_dts(Some(field(8..16)));
+ spooled.set_duration(field(16..24));
+ if header[24] != 0 {
+ spooled.set_flags(ffmpeg::packet::Flags::KEY);
+ }
+ packet(spooled)?;
+ }
+}
+
+/// Encodes a chunk into its spool.
+fn encode_chunk<F: FnMut(u32) -> bool>(
+ frames: mpsc::Receiver<ExportFrame>,
+ settings: EncoderSettings,
+ progress: &Progress<F>,
+ preroll: u32,
+ fail_after: Option<u32>,
+) -> Result<SpooledChunk, String> {
+ let mut encoder = settings
+ .builder()
+ .build_software_detached()
+ .map_err(|error| error.to_string())?;
+ let mut spool = Spool::new(settings.video_info.time_base).map_err(|error| error.to_string())?;
+ let mut frame = ffmpeg::frame::Video::new(
+ ffmpeg::format::Pixel::NV12,
+ settings.video_info.width,
+ settings.video_info.height,
+ );
+ let mut converted = None;
+ for input in frames.iter() {
+ if progress.stopped() {
+ return Err(STOPPED.to_string());
+ }
+ let Some(frame_number) = input.frame_number.checked_sub(preroll) else {
+ continue;
+ };
+ if fail_after == Some(frame_number) {
+ return Err("Injected turbo chunk failure".to_string());
+ }
+ fill_input(&input, &mut frame, i64::from(frame_number))?;
+ encoder
+ .queue_frame_reusable(&mut frame, &mut converted, Duration::MAX, &mut spool)
+ .map_err(|error| error.to_string())?;
+ }
+ if progress.stopped() {
+ return Err(STOPPED.to_string());
+ }
+ encoder
+ .flush_to(&mut spool)
+ .map_err(|error| format!("Video encoding failed: {error}"))?;
+ spool.finish().map_err(|error| error.to_string())
+}
+
+/// Writes chunk 0's packets to the output and keeps the first one, whose
+/// parameter sets every later chunk has to repeat exactly.
+struct FirstPacketSink<'a> {
+ output: &'a mut ffmpeg::format::context::Output,
+ first: Option<Vec<u8>>,
+}
+
+impl PacketSink for FirstPacketSink<'_> {
+ fn stream_time_base(&self, stream_index: usize) -> ffmpeg::Rational {
+ self.output.stream_time_base(stream_index)
+ }
+
+ fn write_packet(&mut self, packet: ffmpeg::Packet) -> Result<(), ffmpeg::Error> {
+ if self.first.is_none() {
+ self.first = Some(packet.data().unwrap_or_default().to_vec());
+ }
+ packet.write_interleaved(self.output)
+ }
+}
+
+/// SPS and PPS NAL units of an Annex B access unit.
+fn parameter_sets(access_unit: &[u8]) -> Vec<&[u8]> {
+ let mut starts = Vec::new();
+ let mut index = 0;
+ while index + 3 <= access_unit.len() {
+ if access_unit[index] == 0 && access_unit[index + 1] == 0 && access_unit[index + 2] == 1 {
+ starts.push(index + 3);
+ index += 3;
+ } else {
+ index += 1;
+ }
+ }
+ starts
+ .iter()
+ .enumerate()
+ .filter_map(|(position, &start)| {
+ let mut end = starts
+ .get(position + 1)
+ .map_or(access_unit.len(), |next| next - 3);
+ while end > start && access_unit[end - 1] == 0 {
+ end -= 1;
+ }
+ let unit = &access_unit[start..end];
+ unit.first()
+ .is_some_and(|header| matches!(header & 0x1f, 7 | 8))
+ .then_some(unit)
+ })
+ .collect()
+}
+
+fn same_parameter_sets(first: Option<&[u8]>, other: Option<&[u8]>) -> bool {
+ match (first, other) {
+ (Some(first), Some(other)) => {
+ let sets = parameter_sets(first);
+ !sets.is_empty() && sets == parameter_sets(other)
+ }
+ _ => false,
+ }
+}
+
+struct AudioOutput {
+ worker: ExportAudioWorker,
+}
+
+impl AudioOutput {
+ /// Writes the audio of every frame up to `frame`, waiting for the audio
+ /// thread only when it falls more than its lag limit behind.
+ fn write_through(
+ &mut self,
+ frame: u32,
+ output: &mut ffmpeg::format::context::Output,
+ ) -> Result<(), Mp4PipelineError> {
+ let worker = &mut self.worker;
+ while worker.completed <= frame {
+ let packets = if frame - worker.completed >= worker.lag_limit {
+ worker.packets.recv().ok()
+ } else {
+ match worker.packets.try_recv() {
+ Ok(packets) => Some(packets),
+ Err(mpsc::TryRecvError::Empty) => return Ok(()),
+ Err(mpsc::TryRecvError::Disconnected) => None,
+ }
+ };
+ let Some(packets) = packets else {
+ return Err(worker
+ .thread
+ .take()
+ .and_then(|thread| join_audio_thread(thread).err())
+ .unwrap_or_else(|| {
+ Mp4PipelineError::Failure("Export audio thread stopped early".to_string())
+ }));
+ };
+ worker.completed += 1;
+ for packet in packets {
+ packet
+ .write_interleaved(output)
+ .map_err(|error| Mp4PipelineError::Failure(error.to_string()))?;
+ }
+ }
+ Ok(())
+ }
+
+ fn finish(self, output: &mut ffmpeg::format::context::Output) -> Result<(), Mp4PipelineError> {
+ let ExportAudioWorker {
+ jobs,
+ packets,
+ thread,
+ ..
+ } = self.worker;
+ drop(jobs);
+ for batch in packets.iter() {
+ for packet in batch {
+ packet
+ .write_interleaved(output)
+ .map_err(|error| Mp4PipelineError::Failure(error.to_string()))?;
+ }
+ }
+ let thread = thread.ok_or_else(|| {
+ Mp4PipelineError::Failure("Export audio thread stopped early".to_string())
+ })?;
+ join_audio_thread(thread)?
+ .flush(output)
+ .map_err(|error| Mp4PipelineError::Failure(format!("Audio encoding failed: {error}")))
+ }
+}
+
+struct StitchInput {
+ path: PathBuf,
+ settings: EncoderSettings,
+ total_frames: u32,
+ first_chunk: mpsc::Receiver<ExportFrame>,
+ later_chunks: Vec<(u32, mpsc::Receiver<Result<SpooledChunk, String>>)>,
+ audio: Option<(ExportAudioSource, ProjectConfiguration)>,
+ streaming_audio: Option<ExportAudioRenderer>,
+ sample_timing: Option<Arc<SampleTiming>>,
+}
+
+fn stitch<F: FnMut(u32) -> bool>(
+ input: StitchInput,
+ progress: &Progress<F>,
+) -> Result<(), Mp4PipelineError> {
+ let StitchInput {
+ path,
+ settings,
+ total_frames,
+ first_chunk,
+ later_chunks,
+ audio,
+ mut streaming_audio,
+ sample_timing,
+ } = input;
+ let sampled = sample_timing.is_some();
+ let stopped = || {
+ if progress.stopped() {
+ Err(Mp4PipelineError::Interrupted)
+ } else {
+ Ok(())
+ }
+ };
+ let failure = |error: String| Mp4PipelineError::Failure(error);
+
+ let first_frame = first_chunk.recv().ok();
+ stopped()?;
+ let mut path = path;
+ path.set_extension("mp4");
+ let mut output = ffmpeg::format::output(&path).map_err(|error| failure(error.to_string()))?;
+ let mut encoder = settings
+ .builder()
+ .build(&mut output)
+ .map_err(|error| failure(error.to_string()))?;
+ if let Some(timing) = &sample_timing {
+ encoder.set_packet_stats(timing.packet_stats());
+ }
+ let audio_encoder = match &audio {
+ Some(_) => Some(
+ cap_enc_ffmpeg::aac::AACEncoder::init_for_export(AudioRenderer::info(), &mut output)
+ .map_err(|error| failure(error.to_string()))?,
+ ),
+ None => None,
+ };
+ if settings.optimize_filesize {
+ let mut options = ffmpeg::Dictionary::new();
+ options.set("movflags", "+faststart");
+ output.write_header_with(options).map(|_| ())
+ } else {
+ output.write_header()
+ }
+ .map_err(|error| failure(error.to_string()))?;
+
+ let mut audio_output = match (audio, audio_encoder) {
+ (Some((source, project)), Some(audio_encoder)) => {
+ let stream_time_base = output.stream_time_base(audio_encoder.stream_index());
+ let stop = progress.stop.clone();
+ let user = progress.user.clone();
+ let mut worker = ExportAudioWorker::spawn(
+ stream_time_base,
+ audio_encoder,
+ source,
+ ExportAudioTimeline {
+ project,
+ fps: settings.video_info.frame_rate.0 as u32,
+ sampled,
+ },
+ move || {
+ stop.load(Ordering::Relaxed)
+ || user
+ .as_ref()
+ .is_some_and(|user| user.load(Ordering::Relaxed))
+ },
+ )?;
+ if !sampled {
+ for frame in 0..total_frames {
+ worker.request(frame, frame);
+ }
+ }
+ Some(AudioOutput { worker })
+ }
+ _ => None,
+ };
+
+ let mut frame = ffmpeg::frame::Video::new(
+ ffmpeg::format::Pixel::NV12,
+ settings.video_info.width,
+ settings.video_info.height,
+ );
+ let mut converted = None;
+ let mut sink = FirstPacketSink {
+ output: &mut output,
+ first: None,
+ };
+ let mut written = 0u32;
+ for input in first_frame
+ .into_iter()
+ .chain(std::iter::from_fn(|| first_chunk.recv().ok()))
+ {
+ stopped()?;
+ if sample_timing
+ .as_ref()
+ .is_some_and(|timing| timing.is_cancelled())
+ {
+ return Err(Mp4PipelineError::Interrupted);
+ }
+ if sampled && let Some(audio) = &mut audio_output {
+ audio.worker.request(written, input.timeline_frame);
+ }
+ fill_input(&input, &mut frame, input.frame_number as i64).map_err(failure)?;
+ encoder
+ .queue_frame_reusable(&mut frame, &mut converted, Duration::MAX, &mut sink)
+ .map_err(|error| failure(error.to_string()))?;
+ if let Some(audio) = &mut audio_output {
+ audio.write_through(written, &mut *sink.output)?;
+ }
+ if let Some(timing) = &sample_timing {
+ timing.record_frame(input.timeline_frame);
+ }
+ written += 1;
+ }
+ stopped()?;
+ encoder
+ .flush_to(&mut sink)
+ .map_err(|error| failure(format!("Video encoding failed: {error}")))?;
+ let first_packet = sink.first.take();
+
+ let video_time_base = settings.video_info.time_base;
+ let stream_time_base = output.stream_time_base(0);
+ for (start, chunk) in later_chunks {
+ if written != start {
+ return Err(failure(format!(
+ "Turbo chunk starting at frame {start} follows {written} written frames"
+ )));
+ }
+ let chunk = chunk
+ .recv()
+ .map_err(|_| failure("Turbo chunk ended without a result".to_string()))?
+ .map_err(failure)?;
+ stopped()?;
+ if !same_parameter_sets(first_packet.as_deref(), chunk.first_packet.as_deref()) {
+ return Err(failure(format!(
+ "Turbo chunk starting at frame {start} has different H.264 parameter sets"
+ )));
+ }
+ read_spooled_packets(&chunk, |mut packet| {
+ let offset = i64::from(start);
+ packet.set_pts(packet.pts().map(|pts| pts + offset));
+ packet.set_dts(packet.dts().map(|dts| dts + offset));
+ packet.set_stream(0);
+ packet.rescale_ts(video_time_base, stream_time_base);
+ packet
+ .write_interleaved(&mut output)
+ .map_err(|error| TurboError::Failed(error.to_string()))?;
+ if let Some(audio) = &mut audio_output {
+ audio
+ .write_through(written, &mut output)
+ .map_err(|error| TurboError::Failed(error.message()))?;
+ }
+ written += 1;
+ Ok(())
+ })
+ .map_err(|error| match error {
+ TurboError::Cancelled => Mp4PipelineError::Interrupted,
+ TurboError::Failed(error) => failure(error),
+ })?;
+ }
+ if !sampled && written != total_frames {
+ return Err(failure(format!(
+ "Turbo export wrote {written} of {total_frames} frames"
+ )));
+ }
+
+ match audio_output {
+ Some(audio) => audio.finish(&mut output)?,
+ None => {
+ if let Some(audio) = &mut streaming_audio {
+ audio.validate_to_end().map_err(Mp4PipelineError::from)?;
+ }
+ }
+ }
+ stopped()?;
+ output
+ .write_trailer()
+ .map_err(|error| failure(error.to_string()))
+}
+
+/// An export of the base's sample windows through the turbo encoder, for
+/// size and time estimates.
+pub(crate) async fn export_sampled(
+ settings: Mp4ExportSettings,
+ mut base: ExporterBase,
+ on_progress: impl FnMut(u32) -> bool + Send + 'static,
+) -> Result<PathBuf, String> {
+ match export(
+ settings,
+ &mut base,
+ TurboOptions {
+ chunks: Some(1),
+ strict: true,
+ },
+ Arc::new(Mutex::new(on_progress)),
+ )
+ .await
+ {
+ Ok(path) => Ok(path),
+ Err(TurboError::Cancelled) => Err(STOPPED.to_string()),
+ Err(TurboError::Failed(error)) => Err(error),
+ }
+}
+
+pub(crate) async fn export<F: FnMut(u32) -> bool + Send + 'static>(
+ settings: Mp4ExportSettings,
+ base: &mut ExporterBase,
+ options: TurboOptions,
+ on_progress: Arc<Mutex<F>>,
+) -> Result<PathBuf, TurboError> {
+ let fps = settings.fps;
+ let output_size = ProjectUniforms::get_output_size(
+ &base.render_constants.options,
+ &base.project_config,
+ settings.resolution_base,
+ );
+ let total_frames = base.total_frames(fps);
+ let cores = std::thread::available_parallelism()
+ .map(|cores| cores.get())
+ .unwrap_or(4);
+ let sample_windows = base.sample_windows.clone();
+ let plan = plan(
+ total_frames,
+ fps,
+ cores,
+ if sample_windows.is_some() {
+ Some(1)
+ } else {
+ options.chunks
+ },
+ );
+ info!(
+ chunks = plan.chunks.len(),
+ threads = plan.threads,
+ total_frames,
+ "Exporting mp4 with turbo"
+ );
+
+ let mut video_info =
+ VideoInfo::from_raw(RawVideoFormat::Nv12, output_size.0, output_size.1, fps);
+ video_info.time_base = ffmpeg::Rational::new(1, fps as i32);
+ let bpp = settings.effective_bpp();
+ let encoder_settings = EncoderSettings {
+ video_info,
+ bpp,
+ optimize_filesize: settings.optimize_filesize,
+ crf: settings.compression.crf_value(),
+ tuning: SoftwareEncoderTuning {
+ threads: plan.threads,
+ capped_crf: (!settings.optimize_filesize).then(|| constant_quality(bpp, fps)),
+ },
+ };
+
+ let mut extra_segments = Vec::with_capacity(plan.chunks.len().saturating_sub(1));
+ for _ in 1..plan.chunks.len() {
+ extra_segments.push(cap_editor::create_segments_without_audio(
+ &base.recording_meta,
+ &base.studio_meta,
+ settings.force_ffmpeg_decoder,
+ ));
+ }
+ let extra_segments = futures::future::try_join_all(extra_segments).await?;
+ if crate::reads_recordings_forward(&base.project_config) {
+ for segment in extra_segments.iter().flatten() {
+ segment.decoders.prefer_sequential_access();
+ }
+ }
+
+ let output_path = base.output_path.clone();
+ let mut streaming_audio = base.streaming_audio.take();
+ let audio_control = base.audio_cancellation.take();
+ let temporary_output = match base.streaming_output.take() {
+ Some(staged) => Some(staged),
+ None => crate::prepare_new_output(&base.output_path).ok().flatten(),
+ };
+ let user_cancellation = base.user_cancellation.clone();
+ let stop = audio_control
+ .as_ref()
+ .map(|control| control.stop.clone())
+ .unwrap_or_default();
+ let progress = Arc::new(Progress {
+ frames: Mutex::new((0, move |frame| {
+ (on_progress.lock().unwrap_or_else(PoisonError::into_inner))(frame)
+ })),
+ stop: stop.clone(),
+ user: user_cancellation,
+ cancelled: AtomicBool::new(false),
+ failure: Mutex::new(None),
+ });
+
+ let unused_audio = streaming_audio
+ .as_mut()
+ .and_then(|audio| audio.take_unused_sources(&base.project_config));
+ let audio_validation = unused_audio.map(|validation| {
+ let stop = stop.clone();
+ tokio::task::spawn_blocking(move || {
+ let result = validation.validate_to_end().map_err(Mp4PipelineError::from);
+ if result.is_err() {
+ stop.store(true, Ordering::Relaxed);
+ }
+ result
+ })
+ });
+
+ let audio_segments = get_audio_segments(&base.segments).await;
+ let music = load_music_tracks_uncached(&base.project_config, &base.project_path);
+ let has_recording_audio = audio_segments
+ .first()
+ .filter(|_| !base.project_config.audio.mute)
+ .is_some();
+ let audio = (has_recording_audio || !music.is_empty()).then(|| {
+ let source = match streaming_audio.take() {
+ Some(audio) => ExportAudioSource::Streaming(audio),
+ None => {
+ ExportAudioSource::Renderer(AudioRenderer::new(audio_segments).with_music(music))
+ }
+ };
+ (source, base.project_config.clone())
+ });
+
+ let shared = RenderShared {
+ constants: base.render_constants.clone(),
+ project: Arc::new(base.project_config.clone()),
+ recording_meta: Arc::new(base.recording_meta.clone()),
+ studio_meta: Arc::new(base.studio_meta.clone()),
+ recordings: base.recordings.clone(),
+ fps,
+ resolution_base: settings.resolution_base,
+ };
+ let render_segments = |segments: &[SegmentMedia]| {
+ segments
+ .iter()
+ .map(|segment| RenderSegment {
+ cursor: segment.cursor.clone(),
+ keyboard: segment.keyboard.clone(),
+ decoders: segment.decoders.clone(),
+ render_display: true,
+ })
+ .collect::<Vec<_>>()
+ };
+
+ let mut renders = Vec::with_capacity(plan.chunks.len());
+ let mut encoders = Vec::with_capacity(plan.chunks.len().saturating_sub(1));
+ let mut later_chunks = Vec::with_capacity(plan.chunks.len().saturating_sub(1));
+ let (first_tx, first_chunk) = mpsc::sync_channel::<ExportFrame>(4);
+ let spawn_failure = |error: std::io::Error| {
+ stop.store(true, Ordering::Relaxed);
+ TurboError::Failed(format!("Could not start a turbo export thread: {error}"))
+ };
+ renders.push(
+ render_chunk(
+ shared.clone(),
+ render_segments(&base.segments),
+ sample_windows
+ .clone()
+ .unwrap_or_else(|| cap_rendering::FrameWindows::new(vec![plan.chunks[0].clone()])),
+ 0,
+ first_tx,
+ progress.clone(),
+ sample_windows.is_none().then(|| base.project_path.clone()),
+ )
+ .map_err(spawn_failure)?,
+ );
+ for (index, (frames, segments)) in plan.chunks.iter().skip(1).zip(&extra_segments).enumerate() {
+ #[cfg(feature = "test-hooks")]
+ let fail_after = (base.turbo_chunk_failure == Some(index + 1)).then_some(2);
+ #[cfg(not(feature = "test-hooks"))]
+ let fail_after = {
+ let _ = index;
+ None
+ };
+ let preroll = preroll_frames(fps, frames.start);
+ let rendered = frames.start - preroll..frames.end;
+ let (frame_tx, frame_rx) = mpsc::sync_channel::<ExportFrame>(4);
+ let (result_tx, result_rx) = mpsc::sync_channel(1);
+ let worker_progress = progress.clone();
+ encoders.push(
+ std::thread::Builder::new()
+ .name("turbo-encode".to_string())
+ .spawn(move || {
+ let result = encode_chunk(
+ frame_rx,
+ encoder_settings,
+ &worker_progress,
+ preroll,
+ fail_after,
+ );
+ if let Err(error) = &result
+ && error != STOPPED
+ {
+ worker_progress.fail(error.clone());
+ }
+ let _ = result_tx.send(result);
+ })
+ .map_err(spawn_failure)?,
+ );
+ renders.push(
+ render_chunk(
+ shared.clone(),
+ render_segments(segments),
+ cap_rendering::FrameWindows::new(vec![rendered]),
+ preroll,
+ frame_tx,
+ progress.clone(),
+ None,
+ )
+ .map_err(spawn_failure)?,
+ );
+ later_chunks.push((frames.start, result_rx));
+ }
+
+ let encoder_output_path = temporary_output
+ .as_ref()
+ .map(|path| path.to_path_buf())
+ .unwrap_or_else(|| output_path.clone());
+ let stitch_input = StitchInput {
+ path: encoder_output_path,
+ settings: encoder_settings,
+ total_frames,
+ first_chunk,
+ later_chunks,
+ audio,
+ streaming_audio,
+ sample_timing: base.sample_timing.clone(),
+ };
+ let stitch_progress = progress.clone();
+ let result = tokio::task::spawn_blocking(move || {
+ let stitched = stitch(stitch_input, &stitch_progress);
+ if let Err(
+ Mp4PipelineError::Failure(error)
+ | Mp4PipelineError::AudioSourceFailure { message: error, .. },
+ ) = &stitched
+ {
+ stitch_progress.fail(error.clone());
+ }
+ let mut errors = vec![stitched.err()];
+ for render in renders {
+ errors.push(match render.join() {
+ Ok(Ok(())) => None,
+ Ok(Err(error)) if error == STOPPED => Some(Mp4PipelineError::Interrupted),
+ Ok(Err(error)) => Some(Mp4PipelineError::Failure(error)),
+ Err(_) => Some(Mp4PipelineError::Failure(
+ "Turbo render thread panicked".to_string(),
+ )),
+ });
+ }
+ for encoder in encoders {
+ if encoder.join().is_err() {
+ errors.push(Some(Mp4PipelineError::Failure(
+ "Turbo encoder thread panicked".to_string(),
+ )));
+ }
+ }
+ errors
+ })
+ .await
+ .map_err(|error| TurboError::Failed(error.to_string()))?;
+ let validation = match audio_validation {
+ Some(task) => task
+ .await
+ .map_err(|error| Mp4PipelineError::Failure(error.to_string()))
+ .and_then(|result| result)
+ .err(),
+ None => None,
+ };
+ drop(extra_segments);
+
+ let mut errors = result.into_iter().flatten().chain(validation);
+ let error = errors.find(|error| !matches!(error, Mp4PipelineError::Interrupted));
+ if progress.was_cancelled() {
+ return Err(TurboError::Cancelled);
+ }
+ let first_failure = progress
+ .failure
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .take();
+ if let Some(error) = first_failure {
+ return Err(TurboError::Failed(error));
+ }
+ if let Some(error) = error {
+ return Err(TurboError::Failed(error.message()));
+ }
+ if progress.stopped() {
+ return Err(TurboError::Failed(
+ "Turbo export stopped before it finished".to_string(),
+ ));
+ }
+ if let Some(temporary_output) = temporary_output {
+ temporary_output
+ .persist_noclobber(&output_path)
+ .map_err(|error| TurboError::Failed(error.to_string()))?;
+ }
+ drop(audio_control);
+ Ok(output_path)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn plans_cover_every_frame_in_order() {
+ for (total, fps, cores, requested) in [
+ (2312, 60, 10, None),
+ (2312, 60, 10, Some(3)),
+ (100, 30, 16, None),
+ (7, 30, 16, Some(4)),
+ (100_000, 60, 64, None),
+ ] {
+ let plan = plan(total, fps, cores, requested);
+ assert!(!plan.chunks.is_empty() && plan.chunks.len() <= MAX_CHUNKS);
+ assert_eq!(plan.chunks[0].start, 0);
+ assert_eq!(plan.chunks.last().unwrap().end, total);
+ for pair in plan.chunks.windows(2) {
+ assert_eq!(pair[0].end, pair[1].start);
+ assert!(pair[0].len().abs_diff(pair[1].len()) <= 1);
+ }
+ assert!(plan.threads >= 1);
+ }
+ }
+
+ #[test]
+ fn short_exports_stay_in_one_chunk_unless_asked() {
+ assert_eq!(plan(60 * 7, 60, 16, None).chunks.len(), 1);
+ assert_eq!(plan(60 * 8, 60, 16, None).chunks.len(), 2);
+ assert_eq!(plan(60 * 7, 60, 16, Some(3)).chunks.len(), 3);
+ assert_eq!(plan(2, 60, 16, Some(3)).chunks, vec![0..1, 1..2]);
+ assert_eq!(plan(0, 60, 16, None).chunks, vec![0..0]);
+ }
+
+ #[test]
+ fn preroll_covers_mask_smoothing_without_reaching_before_the_timeline() {
+ assert_eq!(preroll_frames(60, 1000), 30);
+ assert_eq!(preroll_frames(15, 1000), 16);
+ assert_eq!(preroll_frames(30, 10), 10);
+ assert_eq!(preroll_frames(30, 0), 0);
+ }
+
+ #[test]
+ fn constant_quality_follows_the_presets() {
+ for (compression, crf) in [
+ (ExportCompression::Maximum, 15),
+ (ExportCompression::Social, 18),
+ (ExportCompression::Web, 20),
+ (ExportCompression::Potato, 22),
+ ] {
+ assert_eq!(constant_quality(compression.bits_per_pixel(), 30), crf);
+ assert_eq!(constant_quality(compression.bits_per_pixel(), 60), crf - 3);
+ assert_eq!(constant_quality(compression.bits_per_pixel(), 15), crf + 2);
+ }
+ assert_eq!(constant_quality(1.0, 30), 15);
+ assert_eq!(constant_quality(0.01, 30), 22);
+ assert_eq!(constant_quality(0.0, 30), 22);
+ assert_eq!(constant_quality(0.3, 120), 12);
+ assert!((15..=22).contains(&constant_quality(0.2, 30)));
+ }
+
+ #[test]
+ fn parameter_sets_compare_sps_and_pps_only() {
+ let unit = |nal: &[u8]| [&[0u8, 0, 0, 1][..], nal].concat();
+ let sps = [0x67, 0x64, 0x00, 0x28];
+ let pps = [0x68, 0xee, 0x3c, 0x80];
+ let sei = [0x06, 0x05, 0x01];
+ let first = [unit(&sps), unit(&pps), unit(&sei), unit(&[0x65, 1, 2])].concat();
+ let other = [
+ unit(&sps),
+ unit(&pps),
+ unit(&[0x06, 0x09]),
+ unit(&[0x65, 3]),
+ ]
+ .concat();
+ assert_eq!(parameter_sets(&first), vec![&sps[..], &pps[..]]);
+ assert!(same_parameter_sets(Some(&first), Some(&other)));
+ let changed = [unit(&[0x67, 0x4d, 0x00, 0x28]), unit(&pps)].concat();
+ assert!(!same_parameter_sets(Some(&first), Some(&changed)));
+ assert!(!same_parameter_sets(Some(&first), None));
+ assert!(!same_parameter_sets(Some(&[0x65, 1]), Some(&[0x65, 1])));
+ }
+}