diff --git a/crates/app/src/export_ui.rs b/crates/app/src/export_ui.rs index 6100e97..3bc487a 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,37 @@ 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) +} + +/// Which format picking Exact should set, from the source's own codec -- +/// "the input's own quality" means the input's own codec, so this is what +/// `Player::pick_quality` derives the format from rather than leaving +/// whatever format a previous pick left behind. `None` for a codec Exact has +/// no copy path for at all (h264); the row itself still refuses through +/// [`exact_refusal`] in that case, since a mixed-source project can fail the +/// same way after the format is set. +pub(crate) fn exact_format(session: &PlaybackSession) -> Option { + let (_, meta) = session.export_snapshot(); + match meta.codec { + engine::demux::Codec::Hevc => Some(Format::Hevc), + engine::demux::Codec::Av1 => Some(Format::Av1), + _ => None, + } +} + 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..ae1ed39 100644 --- a/crates/app/src/player/export.rs +++ b/crates/app/src/player/export.rs @@ -502,6 +502,25 @@ impl Player { } } + /// A quality row, by click or by its own key (`Custom` goes through + /// [`Self::edit_mbps`] instead). Exact is the one row that is also a + /// format decision -- "the input's own quality" means the input's own + /// codec, so picking it derives the container from the source rather + /// than leaving whatever format a previous pick left behind for + /// [`exact_refusal`] to refuse minutes later. A source this cannot copy + /// (h264, mixed sources) leaves the format alone; the row itself + /// ([`crate::ui::cards`]'s quality list) already says why. + pub(crate) fn pick_quality(&mut self, quality: Quality) { + self.quality = quality; + if quality != Quality::Exact { + return; + } + let Some(format) = self.session.as_ref().and_then(exact_format) else { + return; + }; + self.set_format(format); + } + /// The container row: the same codec in the other box, which retargets the /// destination exactly as picking a codec does -- and does nothing at all /// for a codec with only one box, so the stroke cannot invent a choice the @@ -726,6 +745,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/app/src/tests/cards.rs b/crates/app/src/tests/cards.rs index 2b7fef2..ae26023 100644 --- a/crates/app/src/tests/cards.rs +++ b/crates/app/src/tests/cards.rs @@ -1727,6 +1727,30 @@ fn a_preset_over_a_picture_this_timeline_has_none_of_carries_the_codec_refusal() assert_eq!(format_refusal(&session, audio_format), None); } +/// Picking Exact must derive the container from the source's own codec +/// rather than leave whatever format was already picked -- an HEVC or AV1 +/// source lands on its own copy-capable Matroska format, and a source Exact +/// has no copy path for at all (h264) leaves `exact_format` with nothing to +/// set, so the row's own [`exact_refusal`] is what a click on it would see. +#[test] +fn exact_derives_its_format_from_the_source_codec() { + let hevc = PlaybackSession::open(asset("test_hevc.mkv")).expect("an hevc fixture"); + assert_eq!(crate::exact_format(&hevc), Some(Format::Hevc)); + + let av1 = PlaybackSession::open(asset("test_av1.mkv")).expect("an av1 fixture"); + assert_eq!(crate::exact_format(&av1), Some(Format::Av1)); + + let h264 = PlaybackSession::open(asset("test_h264.mkv")).expect("an h264 fixture"); + assert_eq!(h264.export_snapshot().1.codec, engine::demux::Codec::H264); + assert_eq!(crate::exact_format(&h264), None); + // No copy path for h264 at any format -- the row's own refusal is what a + // click on Exact would still show, since `exact_format` sets nothing and + // the format is left at whatever it already was (Mp4's own refusal, or + // Hevc's if the pane was left there from an earlier pick). + assert!(crate::exact_refusal(&h264, Format::Mp4, Quality::Exact).is_some()); + assert!(crate::exact_refusal(&h264, Format::Hevc, Quality::Exact).is_some()); +} + /// The stacking-scrims bug: opening the speed card while the colour card was /// up left both up at once, because each `open_*` cleared a different /// hand-picked subset of the other flags rather than all of them. The fix is diff --git a/crates/app/src/ui/cards.rs b/crates/app/src/ui/cards.rs index 7e7873b..e91a18f 100644 --- a/crates/app/src/ui/cards.rs +++ b/crates/app/src/ui/cards.rs @@ -604,6 +604,14 @@ impl Player { .mbps_edit .as_ref() .filter(|_| quality == Quality::Custom); + // Exact's own row carries whatever [`exact_refusal`] says of + // the format currently picked, whether or not Exact is the + // row in force -- the same reason `start_export` would give, + // said here instead of only after the button is pressed. + let exact_why = (quality == Quality::Exact) + .then(|| self.session.as_ref()) + .flatten() + .and_then(|s| exact_refusal(s, self.format, quality)); let mut r = entry( ("quality", i), match quality { @@ -611,9 +619,10 @@ impl Player { _ => "q", }, quality.label().into(), - match field { - Some(edit) => edit.detail().into(), - None => quality.detail(self.custom_mbps).into(), + match (&exact_why, field) { + (Some(why), _) => why.clone().into(), + (None, Some(edit)) => edit.detail().into(), + (None, None) => quality.detail(self.custom_mbps).into(), }, self.quality == quality, true, @@ -622,7 +631,7 @@ impl Player { move |this, _: &ClickEvent, _, cx| { match quality { Quality::Custom => this.edit_mbps(), - _ => this.quality = quality, + _ => this.pick_quality(quality), } cx.notify(); }, 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