From 49dbed8187d98431b4017dbf604aa3fbe8ab95f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 06:59:31 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(export):=20Exact=20quality=20r?= =?UTF-8?q?ow=20copies=20the=20source's=20own=20coded=20picture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds Quality::Exact to the export card: wherever CopyPlan already says a copy is exactly what the timeline says, the file leaves as the source's own bytes. exact_refusal names why not (wrong container, edit forces a re-encode) before the button is pressed and again inside run(), so the card and the worker can never disagree. --- crates/app/src/export_ui.rs | 33 ++++++++++++++- crates/app/src/player/export.rs | 8 ++++ crates/engine/src/export.rs | 61 +++++++++++++++++++++++++++ crates/engine/tests/video_copy.rs | 70 +++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 2 deletions(-) diff --git a/crates/app/src/export_ui.rs b/crates/app/src/export_ui.rs index 27d4f3a..46a045b 100644 --- a/crates/app/src/export_ui.rs +++ b/crates/app/src/export_ui.rs @@ -72,6 +72,7 @@ pub(crate) fn sample_rate_choices(current: Option) -> Vec { /// them. #[derive(Clone, Copy, PartialEq, Debug)] pub(crate) enum Quality { + Exact, Auto, Low, Medium, @@ -80,7 +81,8 @@ pub(crate) enum Quality { } impl Quality { - pub(crate) const ALL: [Quality; 5] = [ + pub(crate) const ALL: [Quality; 6] = [ + Quality::Exact, Quality::Auto, Quality::Low, Quality::Medium, @@ -90,6 +92,7 @@ impl Quality { pub(crate) fn label(self) -> &'static str { match self { + Quality::Exact => "Exact", Quality::Auto => "Auto", Quality::Low => "Low", Quality::Medium => "Medium", @@ -101,6 +104,12 @@ impl Quality { /// The figure the row stands for, said in the units the row is chosen by. pub(crate) fn detail(self, custom_mbps: u32) -> String { match self { + // corner-cut: worded without naming the mechanism (which codec, + // which container) -- DESIGN.md §8 keeps a card's detail line a + // fact about the *output*, not an instruction about how to get + // there; [`exact_refusal`] is where the mechanism's own limits + // are said, once the row has actually been picked. + Quality::Exact => "input quality — copy where cuts allow".to_string(), Quality::Auto => "from the picture size and frame rate".to_string(), Quality::Custom => { format!("{custom_mbps} Mbps — wheel or ± steps, n types one, {MBPS_MIN}–{MBPS_MAX}") @@ -858,12 +867,17 @@ pub(crate) fn export_settings( // has dimmed cannot have been changed. audio_kbps: Some(audio_kbps), bitrate: match quality { - Quality::Auto => None, + // A copied span carries its source's own bits, and a fallback + // re-encode -- the only case `bitrate` reaches an encoder under + // Exact -- is the automatic figure ([`engine::export::run`]), + // not a preset's, so this asks for neither. + Quality::Exact | Quality::Auto => None, Quality::Low => Some(2_000_000), Quality::Medium => Some(6_000_000), Quality::High => Some(12_000_000), Quality::Custom => Some(u64::from(custom_mbps) * 1_000_000), }, + exact: quality == Quality::Exact, // The card's own row now ([`encoder_choices`]), kept with the project: // it was a `VE_SW_ENC` env pin and nothing else, which is a switch // nobody exporting a film would ever find. @@ -918,6 +932,21 @@ pub(crate) const MBPS_DIGITS: usize = 3; /// cannot say what the timeline says (`export::copy_audio`), so none of them is /// a refusal any more -- and every video format carries the sound, so there is /// nothing here that is one format's alone. +/// Why the Exact row would refuse this project's own picked format right +/// now, asked before the button is pressed rather than minutes into a +/// worker -- [`engine::export::exact_refusal`] is the same gate `start` +/// checks again, so a caller here and the worker can never disagree about +/// whether Exact is honoured. `None` for every quality but `Exact` itself, +/// since nothing else has anything to refuse. +pub(crate) fn exact_refusal(session: &PlaybackSession, format: Format, quality: Quality) -> Option { + if quality != Quality::Exact { + return None; + } + let (project, meta) = session.export_snapshot(); + let settings = export_settings(quality, 0, format, DEFAULT_AUDIO_KBPS, EncoderSeat::Auto); + engine::export::exact_refusal(&project, &meta, &settings) +} + pub(crate) fn format_refusal(session: &PlaybackSession, format: Format) -> Option { if !format.has_video() { return None; diff --git a/crates/app/src/player/export.rs b/crates/app/src/player/export.rs index 37f56fa..8a73598 100644 --- a/crates/app/src/player/export.rs +++ b/crates/app/src/player/export.rs @@ -726,6 +726,14 @@ impl Player { cx.notify(); return; } + // Exact asks the same question format_refusal just did, for the same + // reason: the row can be picked, then the cut or the format changed + // under it, and this is the fence with a keystroke to blame. + if let Some(why) = exact_refusal(session, self.format, self.quality) { + self.notify_user(format!("NOT EXPORTED — {why}").into()); + cx.notify(); + return; + } session.pause(); self.export = Some(session.export_to_with(&self.export_path, &settings)); // The clock starts with the worker, not with the first repaint that diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rs index d99290c..f0816ee 100644 --- a/crates/engine/src/export.rs +++ b/crates/engine/src/export.rs @@ -373,6 +373,18 @@ pub struct ExportSettings { /// export nobody has marked in or out on writes the same bytes it always /// did. Session state, like the mark itself: never saved to a `.edith`. pub range: Option<(u32, u32)>, + /// The Exact quality row: this file must be the source's own coded + /// picture, copied wherever [`CopyPlan`] already says a copy is exactly + /// what the timeline says. `bitrate` and `seat` mean nothing to a copied + /// track, but a fallback re-encode (a cut off a sync point, a grade, a + /// speed, a transform) still honours them, so a copy-minded caller sends + /// `bitrate: None` beside this and gets the automatic figure rather than + /// a preset's. + /// + /// [`exact_refusal`] is the gate a front-end asks before the button is + /// pressed; [`run`] asks it again, which is what makes this a setting a + /// worker can trust rather than one only the card checked. + pub exact: bool, } /// The lowest H.264 level (Table A-1) whose frame size and macroblock rate @@ -1673,6 +1685,52 @@ struct CopyRegion { start: u32, } +/// Why [`ExportSettings::exact`] cannot be honoured for this project right +/// now, or `None` where [`CopyPlan`] already carries the whole file. A state +/// report, not an instruction: it says what the file *is* rather than what to +/// pick instead. +/// +/// Two shapes of reason, told apart because only one of them is permanent. +/// **The container cannot carry the source's own codec at all** -- Exact was +/// asked of an mp4, or of a Matroska file whose codec is not the source's own +/// -- which is [`CopyPlan`]'s own corner-cut (mp4 copies nothing yet) and +/// [`CopyPlan::of`]'s codec check, so it is checked here the same way rather +/// than duplicated with different words. **The edit is what stops it** -- a +/// cut off a sync point, a grade, a speed, a dissolve, a moved or scaled +/// clip -- which the same format would carry exactly if the edit let it, so +/// those six [`CopyPlan::of`] gates are named together rather than picked +/// apart into six different sentences. +pub fn exact_refusal(project: &Project, meta: &VideoMeta, settings: &ExportSettings) -> Option { + if !settings.exact || CopyPlan::of(project, meta, settings).is_some() { + return None; + } + let codec = match settings.format { + Format::Hevc => Codec::Hevc, + Format::Av1 => Codec::Av1, + _ => { + return Some(format!( + "{} cannot carry a source's own coded picture byte for byte — pick an HEVC or AV1 Matroska to copy it exactly", + settings.format.name() + )); + } + }; + let carries = project.sources().iter().all(|source| { + crate::demux::is_matroska(&source.path) + && Demuxer::open(&source.path) + .ok() + .is_some_and(|(source_meta, _)| source_meta.codec == codec) + }); + Some(match carries { + false => format!( + "{} cannot carry the source's own codec byte for byte", + settings.format.name() + ), + true => "the edit does not let this file copy the source exactly — a cut off a sync \ + point, a grade, a speed or a transform forces a re-encode" + .to_string(), + }) +} + impl CopyPlan { /// The plan for this timeline, or `None` where anything at all makes a copy /// say something other than what the timeline says -- in which case the @@ -2429,6 +2487,9 @@ fn run( if settings.intra_only && settings.format != Format::Mp4 { return Err(format!("{} cannot be written intra-only", settings.format.name()).into()); } + if let Some(why) = exact_refusal(project, meta, settings) { + return Err(why.into()); + } let sources = project.sources(); // The picture's decision first, before a sample of sound is touched: it is // the answer that decides what the whole export *is*, it costs one header diff --git a/crates/engine/tests/video_copy.rs b/crates/engine/tests/video_copy.rs index d6b4a0e..8b42a85 100644 --- a/crates/engine/tests/video_copy.rs +++ b/crates/engine/tests/video_copy.rs @@ -169,6 +169,76 @@ fn an_untouched_av1_timeline_leaves_as_its_source_packets() { } } +/// The Exact quality row asked of a timeline nobody has touched: it must copy +/// exactly as the plain [`Format::Hevc`] row above already does, since the +/// only thing `exact` adds over an untouched timeline is the flag itself -- +/// what it changes is the *fallback*, and there is none to reach here. +#[test] +fn exact_on_an_untouched_matroska_timeline_is_stream_identical() { + let source = asset("test_hevc.mkv"); + let session = PlaybackSession::open(&source).expect("open the fixture"); + let out = Scratch::file("ve_copy_exact", "mkv"); + let settings = ExportSettings { + format: Format::Hevc, + exact: true, + ..Default::default() + }; + let handle = session.export_to_with(&out, &settings); + let started = Instant::now(); + while !handle.is_finished() { + assert!( + started.elapsed() < Duration::from_secs(600), + "the export did not finish in 600 s" + ); + std::thread::sleep(Duration::from_millis(20)); + } + let line = handle.encoders().unwrap_or_default(); + handle + .result() + .expect("an outcome") + .unwrap_or_else(|e| panic!("export failed: {e}")); + assert!(line.starts_with("copy · "), "exact did not copy: {line}"); + let (before, after) = (blocks(&source), blocks(&out)); + assert_eq!(before.len(), after.len(), "block count"); + for (i, (a, b)) in before.iter().zip(&after).enumerate() { + assert_eq!(a.bytes, b.bytes, "block {i} is not the source's bytes"); + assert_eq!(a.key, b.key, "block {i} sync flag"); + } +} + +/// Exact asked of an mp4: [`CopyPlan`]'s own corner-cut is that mp4 copies +/// nothing yet, so the container cannot carry the source's codec byte for +/// byte -- a state report, named before a frame is touched, and not a silent +/// re-encode wearing the Exact label. +#[test] +fn exact_refuses_a_container_that_cannot_carry_the_codec() { + let source = asset("test_hevc.mkv"); + let session = PlaybackSession::open(&source).expect("open the fixture"); + let out = Scratch::file("ve_copy_exact_refused", "mp4"); + let settings = ExportSettings { + format: Format::Mp4, + exact: true, + ..Default::default() + }; + let handle = session.export_to_with(&out, &settings); + let started = Instant::now(); + while !handle.is_finished() { + assert!( + started.elapsed() < Duration::from_secs(600), + "the export did not finish in 600 s" + ); + std::thread::sleep(Duration::from_millis(20)); + } + let err = handle + .result() + .expect("an outcome") + .expect_err("an mp4 cannot honour exact"); + assert!( + err.to_string().contains("cannot carry"), + "unexpected refusal: {err}" + ); +} + /// A cut placed on a sync point: the middle of the film is deleted and both /// sides leave as copies. What is checked is the join -- the blocks either side /// of it are the source's own, the timestamps rise a frame at a time across it