diff --git a/crates/app/src/layout.rs b/crates/app/src/layout.rs index f013058..d921b0a 100644 --- a/crates/app/src/layout.rs +++ b/crates/app/src/layout.rs @@ -38,11 +38,11 @@ pub(crate) const LABEL_MIN_W: f32 = 36.; /// a shape. Ceiling: [`View::max_zoom`]'s 8 frames across the bed. The upgrade /// is a second, finer pass over the visible span only, cached like `waves` is. pub(crate) const WAVE_BPS: u32 = 40; -/// How many stand-ins ([`engine::proxy`]) may be in flight at once. Two, not -/// one per core: each is a whole film through a decoder and an encoder, and the -/// hardware seat they queue on is a single one -- more of them at once is the -/// same work done later, with the editor slower while it happens. -pub(crate) const PROXIES_AT_ONCE: usize = 2; +/// How many stand-ins may be in flight at once -- the engine's own number, not +/// a second one that happens to agree: the proxy cache sweeps with room left +/// for exactly this many encodes ([`engine::proxy::reserve`]), so a front-end +/// that started more would put the disk past the cap by whatever it added. +pub(crate) const PROXIES_AT_ONCE: usize = engine::proxy::AT_ONCE; /// Pixels per envelope column. Coarser than a pixel: the eye reads the shape, /// and a path with a point per pixel is a path per repaint. pub(crate) const WAVE_COL: f32 = 2.; diff --git a/crates/engine/src/demux.rs b/crates/engine/src/demux.rs index 7336693..47448fe 100644 --- a/crates/engine/src/demux.rs +++ b/crates/engine/src/demux.rs @@ -129,6 +129,40 @@ fn mp4_codec(fourcc: &[u8; 4]) -> Option { .map(|&(codec, ..)| codec) } +/// The one refusal that is an **answer**: this file carries no picture at all +/// that this engine reads -- a song in a video container, an audio-only mp4 or +/// Matroska, a `.mka` under any name. +/// +/// A type rather than a sentence, because two callers want two different things +/// from it. A front-end shows its `Display`, which is the words it always was, +/// and refuses the import by them. [`crate::proxy`] wants to tell it from *"this +/// file is broken"*: a song is not a film to stand in for and saying "NO PROXY" +/// about one is a refusal nobody asked for, while a film that will not open is +/// worth a line. Neither could tell them apart by matching on prose, and prose +/// is the one thing in this engine that is free to be reworded. +/// +/// A file whose picture is in a codec nothing here decodes is **not** this: it +/// has a video track, the refusal names the codec, and that stays loud. +#[derive(Debug)] +pub struct NoVideoTrack(String); + +impl std::fmt::Display for NoVideoTrack { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} + +impl std::error::Error for NoVideoTrack {} + +impl NoVideoTrack { + /// Whether `error` is one of these, however deep in a `Box` it is: the + /// question [`crate::proxy`] asks, in one place so no caller downcasts by + /// hand. + pub fn is_it(error: &crate::Error) -> bool { + error.downcast_ref::().is_some() + } +} + #[derive(Debug, Clone, Copy, PartialEq)] pub struct VideoMeta { pub width: u32, @@ -943,10 +977,14 @@ impl MkvDemuxer { None => format!( "{codec} video in a Matroska file is not supported — AV1, HEVC, H.264 and VP9 are" ), - }, - None => "this Matroska file has no video track".to_string(), - } - .into()); + } + .into(), + // No picture in it *at all*, which is a fact about the file + // and not a fault: [`NoVideoTrack`] says why that is a type. + None => crate::Error::from(NoVideoTrack( + "this Matroska file has no video track".to_string(), + )), + }); } }; // A picture nothing here can put back together is a refusal at the door, @@ -2372,10 +2410,15 @@ fn mp4_no_video(path: &Path, reader: &Mp4Reader) -> crate::Error { None => { format!("{kind} video in this mp4 is not supported — H.264, HEVC, VP9 and AV1 are") } - }, - None => "no H.264, HEVC, VP9 or AV1 video track in file".to_string(), + } + .into(), + // Nothing with a picture in it that this reads -- an audio-only mp4, + // which is a thing a person really does hand an editor. The Matroska + // door's own answer, and the same type for the same reason. + None => crate::Error::from(NoVideoTrack( + "no H.264, HEVC, VP9 or AV1 video track in file".to_string(), + )), } - .into() } /// Bits per luma sample out of a VP9 keyframe's uncompressed header (VP9 diff --git a/crates/engine/src/proxy.rs b/crates/engine/src/proxy.rs index 1cb6fc4..0dd326a 100644 --- a/crates/engine/src/proxy.rs +++ b/crates/engine/src/proxy.rs @@ -34,7 +34,7 @@ use std::path::{Path, PathBuf}; use std::time::UNIX_EPOCH; -use crate::demux::{Codec, Demuxer, VideoMeta}; +use crate::demux::{Codec, Demuxer, NoVideoTrack, VideoMeta}; use crate::export::{ExportHandle, ExportSettings, Format}; use crate::project::{Clip, LaneKind, Project, Source, Speed}; use crate::scale::FitPolicy; @@ -51,11 +51,37 @@ pub const LONG_EDGE: u32 = 1280; /// ~11.5 GB at ~1.37 MB per second of film), or a dozen of an ordinary length. /// /// A cap is not a nicety here. Proxies are written *without being asked for*, -/// on import, two at a time -- so without one the answer to "how much of the -/// disk does this take" is "all of it, eventually". Over the cap, the least -/// recently used are deleted until it fits ([`sweep`]). +/// on import, [`AT_ONCE`] at a time -- so without one the answer to "how much +/// of the disk does this take" is "all of it, eventually". Over the cap, the +/// least recently used are deleted until it fits ([`sweep`]). +/// +/// **What this really bounds.** A sweep runs before each new stand-in and +/// leaves room for the ones about to be written ([`reserve`]), so what is +/// *finished* is held to `CACHE_CAP - reserve` and the encodes in flight have +/// the reserve to grow into. The directory therefore sits at about the cap +/// rather than at the cap plus a film per encode, which is what it did when the +/// sweep aimed at the bare number. It can still pass the cap where an encode +/// outruns its own estimate -- a stand-in cannot be deleted while it is being +/// written -- and the next sweep takes that back. pub const CACHE_CAP: u64 = 20 * 1024 * 1024 * 1024; +/// How many stand-ins may be made at once. Two, not one per core: each is a +/// whole film through a decoder and an encoder, and the hardware seat they +/// queue on is a single one -- more of them at once is the same work done +/// later, with the editor slower while it happens. +/// +/// It lives here rather than in the front-end that does the fanning out because +/// the cache's own arithmetic needs it ([`reserve`]): the number of encodes in +/// flight is exactly how much room the sweep has to leave, and two places +/// holding that number separately is one place for them to disagree. +pub const AT_ONCE: usize = 2; + +/// What the hardware seat really writes against what it was asked for: 1.25, +/// measured 2026-08-13 (8.3 Mbit/s out of a 6.6 Mbit/s request on radeonsi). +/// Folded into [`reserve`] so the room left for an encode is the room it will +/// actually take, not the room it was told to. +const RATE_OVERSHOOT: f64 = 1.25; + /// Bits per pixel per second a proxy is coded at, [`crate::export`]'s own rate /// rule at three times the number: every frame being an IDR means no frame /// borrows anything from its neighbours, so the same picture at the inter rate @@ -114,6 +140,29 @@ pub fn path_for(source: &Path) -> Option { Some(dir.join(format!("{:016x}.mp4", crate::demux::fnv1a(&bytes)))) } +/// How much of [`CACHE_CAP`] the sweep leaves free for the stand-ins that are +/// about to be written into the directory: [`AT_ONCE`] of them, each about the +/// size of the one being started now. +/// +/// The estimate is the file's own arithmetic and not a guess: the picture is +/// coded at [`BITS_PER_PIXEL`] bits per pixel per second and the frame rate +/// cancels out, so it is `width * height * frames * bpp / 8` -- times +/// [`RATE_OVERSHOOT`], because the hardware seat writes over the rate it is +/// asked for. +/// +/// Held to half the cap, whatever that comes to. A single 4K feature estimates +/// at over the whole cap, and reserving that would empty the cache to make room +/// for one film -- deleting stand-ins that are in use, for a bound that cannot +/// be met anyway while two of them are being written. Half is the compromise: +/// what is *finished* stays under half the cap, so the two in flight have the +/// other half to grow into before the disk carries more than [`CACHE_CAP`]. +fn reserve(width: u32, height: u32, frames: u32) -> u64 { + let bytes = f64::from(width) * f64::from(height) * f64::from(frames) * BITS_PER_PIXEL / 8.0; + ((bytes * RATE_OVERSHOOT) as u64) + .saturating_mul(AT_ONCE as u64) + .min(CACHE_CAP / 2) +} + /// Deletes least-recently-used stand-ins until the directory holds at most /// `cap` bytes, and reports what it holds afterwards. /// @@ -254,16 +303,26 @@ pub fn generate(source: &Path) -> crate::Result { /// cluster walk, so asking `wanted` and then `generate` separately would pay /// for it twice. /// -/// A song and a still are not films and are answered before that read: neither -/// has a picture stream to stand in for, and a demuxer opened on one fails -- -/// which used to reach a front-end as "no proxy" on a `.wav`, a refusal about -/// something nobody had asked for. `Ok(None)` is the true answer, and it is the -/// same one a 1080p H.264 film gets. +/// A file with no picture in it is not a film and is answered `Ok(None)`, the +/// same answer a 1080p H.264 film gets -- it is not a failure, and reporting it +/// as one put "NO PROXY for tone.wav" in front of somebody who had imported a +/// song. +/// +/// Twice, because there are two ways to know. A name this engine already reads +/// as sound or as a still is answered before anything is opened, which is the +/// cheap half. The other half is the honest one: **an audio-only `.mp4`, `.mkv` +/// or `.mov` is a video container with no video track in it**, and only its +/// header says so -- so the demuxer's own [`NoVideoTrack`] answer is taken here +/// as the same quiet `None`. A film that *has* a picture and will not open is a +/// different thing and stays loud. pub fn generate_if_wanted(source: &Path) -> crate::Result> { if crate::is_audio(source) || crate::is_image(source) { return Ok(None); } - started(source, wanted) + match started(source, wanted) { + Err(e) if NoVideoTrack::is_it(&e) => Ok(None), + other => other, + } } /// Both doors: the cache is looked at, the header is read once, and `only_if` @@ -284,10 +343,11 @@ fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result