forge
Explore
Log in

Don't you dare go Hollow

Exact quality row copies the source's own coded picture

Browse files at this commit

49dbed8187d98431b4017dbf604aa3fbe8ab95f2Ahmet Kaan GümüşParent: cafb588
  • feat
    • Exact quality row copies the source's own coded picture
Show full message
✨ feat(export): Exact quality row copies the source's own coded picture

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.rsmodified+31 -2
@@ -72,6 +72,7 @@ pub(crate) fn sample_rate_choices(current: Option<u32>) -> Vec<ChoiceRow> {
7272 /// them.
7373 #[derive(Clone, Copy, PartialEq, Debug)]
7474 pub(crate) enum Quality {
75+ Exact,
7576 Auto,
7677 Low,
7778 Medium,
@@ -80,7 +81,8 @@ pub(crate) enum Quality {
8081 }
8182
8283 impl Quality {
83- pub(crate) const ALL: [Quality; 5] = [
84+ pub(crate) const ALL: [Quality; 6] = [
85+ Quality::Exact,
8486 Quality::Auto,
8587 Quality::Low,
8688 Quality::Medium,
@@ -90,6 +92,7 @@ impl Quality {
9092
9193 pub(crate) fn label(self) -> &'static str {
9294 match self {
95+ Quality::Exact => "Exact",
9396 Quality::Auto => "Auto",
9497 Quality::Low => "Low",
9598 Quality::Medium => "Medium",
@@ -101,6 +104,12 @@ impl Quality {
101104 /// The figure the row stands for, said in the units the row is chosen by.
102105 pub(crate) fn detail(self, custom_mbps: u32) -> String {
103106 match self {
107+ // corner-cut: worded without naming the mechanism (which codec,
108+ // which container) -- DESIGN.md §8 keeps a card's detail line a
109+ // fact about the *output*, not an instruction about how to get
110+ // there; [`exact_refusal`] is where the mechanism's own limits
111+ // are said, once the row has actually been picked.
112+ Quality::Exact => "input quality — copy where cuts allow".to_string(),
104113 Quality::Auto => "from the picture size and frame rate".to_string(),
105114 Quality::Custom => {
106115 format!("{custom_mbps} Mbps — wheel or ± steps, n types one, {MBPS_MIN}–{MBPS_MAX}")
@@ -858,12 +867,17 @@ pub(crate) fn export_settings(
858867 // has dimmed cannot have been changed.
859868 audio_kbps: Some(audio_kbps),
860869 bitrate: match quality {
861- Quality::Auto => None,
870+ // A copied span carries its source's own bits, and a fallback
871+ // re-encode -- the only case `bitrate` reaches an encoder under
872+ // Exact -- is the automatic figure ([`engine::export::run`]),
873+ // not a preset's, so this asks for neither.
874+ Quality::Exact | Quality::Auto => None,
862875 Quality::Low => Some(2_000_000),
863876 Quality::Medium => Some(6_000_000),
864877 Quality::High => Some(12_000_000),
865878 Quality::Custom => Some(u64::from(custom_mbps) * 1_000_000),
866879 },
880+ exact: quality == Quality::Exact,
867881 // The card's own row now ([`encoder_choices`]), kept with the project:
868882 // it was a `VE_SW_ENC` env pin and nothing else, which is a switch
869883 // nobody exporting a film would ever find.
@@ -918,6 +932,21 @@ pub(crate) const MBPS_DIGITS: usize = 3;
918932 /// cannot say what the timeline says (`export::copy_audio`), so none of them is
919933 /// a refusal any more -- and every video format carries the sound, so there is
920934 /// nothing here that is one format's alone.
935+/// Why the Exact row would refuse this project's own picked format right
936+/// now, asked before the button is pressed rather than minutes into a
937+/// worker -- [`engine::export::exact_refusal`] is the same gate `start`
938+/// checks again, so a caller here and the worker can never disagree about
939+/// whether Exact is honoured. `None` for every quality but `Exact` itself,
940+/// since nothing else has anything to refuse.
941+pub(crate) fn exact_refusal(session: &PlaybackSession, format: Format, quality: Quality) -> Option<String> {
942+ if quality != Quality::Exact {
943+ return None;
944+ }
945+ let (project, meta) = session.export_snapshot();
946+ let settings = export_settings(quality, 0, format, DEFAULT_AUDIO_KBPS, EncoderSeat::Auto);
947+ engine::export::exact_refusal(&project, &meta, &settings)
948+}
949+
921950 pub(crate) fn format_refusal(session: &PlaybackSession, format: Format) -> Option<String> {
922951 if !format.has_video() {
923952 return None;
crates/app/src/player/export.rsmodified+8 -0
@@ -726,6 +726,14 @@ impl Player {
726726 cx.notify();
727727 return;
728728 }
729+ // Exact asks the same question format_refusal just did, for the same
730+ // reason: the row can be picked, then the cut or the format changed
731+ // under it, and this is the fence with a keystroke to blame.
732+ if let Some(why) = exact_refusal(session, self.format, self.quality) {
733+ self.notify_user(format!("NOT EXPORTED — {why}").into());
734+ cx.notify();
735+ return;
736+ }
729737 session.pause();
730738 self.export = Some(session.export_to_with(&self.export_path, &settings));
731739 // The clock starts with the worker, not with the first repaint that
crates/engine/src/export.rsmodified+61 -0
@@ -373,6 +373,18 @@ pub struct ExportSettings {
373373 /// export nobody has marked in or out on writes the same bytes it always
374374 /// did. Session state, like the mark itself: never saved to a `.edith`.
375375 pub range: Option<(u32, u32)>,
376+ /// The Exact quality row: this file must be the source's own coded
377+ /// picture, copied wherever [`CopyPlan`] already says a copy is exactly
378+ /// what the timeline says. `bitrate` and `seat` mean nothing to a copied
379+ /// track, but a fallback re-encode (a cut off a sync point, a grade, a
380+ /// speed, a transform) still honours them, so a copy-minded caller sends
381+ /// `bitrate: None` beside this and gets the automatic figure rather than
382+ /// a preset's.
383+ ///
384+ /// [`exact_refusal`] is the gate a front-end asks before the button is
385+ /// pressed; [`run`] asks it again, which is what makes this a setting a
386+ /// worker can trust rather than one only the card checked.
387+ pub exact: bool,
376388 }
377389
378390 /// The lowest H.264 level (Table A-1) whose frame size and macroblock rate
@@ -1673,6 +1685,52 @@ struct CopyRegion {
16731685 start: u32,
16741686 }
16751687
1688+/// Why [`ExportSettings::exact`] cannot be honoured for this project right
1689+/// now, or `None` where [`CopyPlan`] already carries the whole file. A state
1690+/// report, not an instruction: it says what the file *is* rather than what to
1691+/// pick instead.
1692+///
1693+/// Two shapes of reason, told apart because only one of them is permanent.
1694+/// **The container cannot carry the source's own codec at all** -- Exact was
1695+/// asked of an mp4, or of a Matroska file whose codec is not the source's own
1696+/// -- which is [`CopyPlan`]'s own corner-cut (mp4 copies nothing yet) and
1697+/// [`CopyPlan::of`]'s codec check, so it is checked here the same way rather
1698+/// than duplicated with different words. **The edit is what stops it** -- a
1699+/// cut off a sync point, a grade, a speed, a dissolve, a moved or scaled
1700+/// clip -- which the same format would carry exactly if the edit let it, so
1701+/// those six [`CopyPlan::of`] gates are named together rather than picked
1702+/// apart into six different sentences.
1703+pub fn exact_refusal(project: &Project, meta: &VideoMeta, settings: &ExportSettings) -> Option<String> {
1704+ if !settings.exact || CopyPlan::of(project, meta, settings).is_some() {
1705+ return None;
1706+ }
1707+ let codec = match settings.format {
1708+ Format::Hevc => Codec::Hevc,
1709+ Format::Av1 => Codec::Av1,
1710+ _ => {
1711+ return Some(format!(
1712+ "{} cannot carry a source's own coded picture byte for byte — pick an HEVC or AV1 Matroska to copy it exactly",
1713+ settings.format.name()
1714+ ));
1715+ }
1716+ };
1717+ let carries = project.sources().iter().all(|source| {
1718+ crate::demux::is_matroska(&source.path)
1719+ && Demuxer::open(&source.path)
1720+ .ok()
1721+ .is_some_and(|(source_meta, _)| source_meta.codec == codec)
1722+ });
1723+ Some(match carries {
1724+ false => format!(
1725+ "{} cannot carry the source's own codec byte for byte",
1726+ settings.format.name()
1727+ ),
1728+ true => "the edit does not let this file copy the source exactly — a cut off a sync \
1729+ point, a grade, a speed or a transform forces a re-encode"
1730+ .to_string(),
1731+ })
1732+}
1733+
16761734 impl CopyPlan {
16771735 /// The plan for this timeline, or `None` where anything at all makes a copy
16781736 /// say something other than what the timeline says -- in which case the
@@ -2429,6 +2487,9 @@ fn run(
24292487 if settings.intra_only && settings.format != Format::Mp4 {
24302488 return Err(format!("{} cannot be written intra-only", settings.format.name()).into());
24312489 }
2490+ if let Some(why) = exact_refusal(project, meta, settings) {
2491+ return Err(why.into());
2492+ }
24322493 let sources = project.sources();
24332494 // The picture's decision first, before a sample of sound is touched: it is
24342495 // the answer that decides what the whole export *is*, it costs one header
crates/engine/tests/video_copy.rsmodified+70 -0
@@ -169,6 +169,76 @@ fn an_untouched_av1_timeline_leaves_as_its_source_packets() {
169169 }
170170 }
171171
172+/// The Exact quality row asked of a timeline nobody has touched: it must copy
173+/// exactly as the plain [`Format::Hevc`] row above already does, since the
174+/// only thing `exact` adds over an untouched timeline is the flag itself --
175+/// what it changes is the *fallback*, and there is none to reach here.
176+#[test]
177+fn exact_on_an_untouched_matroska_timeline_is_stream_identical() {
178+ let source = asset("test_hevc.mkv");
179+ let session = PlaybackSession::open(&source).expect("open the fixture");
180+ let out = Scratch::file("ve_copy_exact", "mkv");
181+ let settings = ExportSettings {
182+ format: Format::Hevc,
183+ exact: true,
184+ ..Default::default()
185+ };
186+ let handle = session.export_to_with(&out, &settings);
187+ let started = Instant::now();
188+ while !handle.is_finished() {
189+ assert!(
190+ started.elapsed() < Duration::from_secs(600),
191+ "the export did not finish in 600 s"
192+ );
193+ std::thread::sleep(Duration::from_millis(20));
194+ }
195+ let line = handle.encoders().unwrap_or_default();
196+ handle
197+ .result()
198+ .expect("an outcome")
199+ .unwrap_or_else(|e| panic!("export failed: {e}"));
200+ assert!(line.starts_with("copy · "), "exact did not copy: {line}");
201+ let (before, after) = (blocks(&source), blocks(&out));
202+ assert_eq!(before.len(), after.len(), "block count");
203+ for (i, (a, b)) in before.iter().zip(&after).enumerate() {
204+ assert_eq!(a.bytes, b.bytes, "block {i} is not the source's bytes");
205+ assert_eq!(a.key, b.key, "block {i} sync flag");
206+ }
207+}
208+
209+/// Exact asked of an mp4: [`CopyPlan`]'s own corner-cut is that mp4 copies
210+/// nothing yet, so the container cannot carry the source's codec byte for
211+/// byte -- a state report, named before a frame is touched, and not a silent
212+/// re-encode wearing the Exact label.
213+#[test]
214+fn exact_refuses_a_container_that_cannot_carry_the_codec() {
215+ let source = asset("test_hevc.mkv");
216+ let session = PlaybackSession::open(&source).expect("open the fixture");
217+ let out = Scratch::file("ve_copy_exact_refused", "mp4");
218+ let settings = ExportSettings {
219+ format: Format::Mp4,
220+ exact: true,
221+ ..Default::default()
222+ };
223+ let handle = session.export_to_with(&out, &settings);
224+ let started = Instant::now();
225+ while !handle.is_finished() {
226+ assert!(
227+ started.elapsed() < Duration::from_secs(600),
228+ "the export did not finish in 600 s"
229+ );
230+ std::thread::sleep(Duration::from_millis(20));
231+ }
232+ let err = handle
233+ .result()
234+ .expect("an outcome")
235+ .expect_err("an mp4 cannot honour exact");
236+ assert!(
237+ err.to_string().contains("cannot carry"),
238+ "unexpected refusal: {err}"
239+ );
240+}
241+
172242 /// A cut placed on a sync point: the middle of the film is deleted and both
173243 /// sides leave as copies. What is checked is the join -- the blocks either side
174244 /// of it are the source's own, the timestamps rise a frame at a time across it