Exact quality row copies the source's own coded picture
Browse files at this commit49dbed8187d98431b4017dbf604aa3fbe8ab95f2Parent: cafb588- feat
- Exact quality row copies the source's own coded picture
4 changed files +170 -2
- crates/app/src/export_ui.rs +31 -2
- crates/app/src/player/export.rs +8 -0
- crates/engine/src/export.rs +61 -0
- crates/engine/tests/video_copy.rs +70 -0
crates/app/src/export_ui.rsmodified+31 -2
| @@ -72,6 +72,7 @@ pub(crate) fn sample_rate_choices(current: Option<u32>) -> Vec<ChoiceRow> { | ||
| 72 | 72 | /// them. |
| 73 | 73 | #[derive(Clone, Copy, PartialEq, Debug)] |
| 74 | 74 | pub(crate) enum Quality { |
| 75 | + Exact, | |
| 75 | 76 | Auto, |
| 76 | 77 | Low, |
| 77 | 78 | Medium, |
| @@ -80,7 +81,8 @@ pub(crate) enum Quality { | ||
| 80 | 81 | } |
| 81 | 82 | |
| 82 | 83 | impl Quality { |
| 83 | - pub(crate) const ALL: [Quality; 5] = [ | |
| 84 | + pub(crate) const ALL: [Quality; 6] = [ | |
| 85 | + Quality::Exact, | |
| 84 | 86 | Quality::Auto, |
| 85 | 87 | Quality::Low, |
| 86 | 88 | Quality::Medium, |
| @@ -90,6 +92,7 @@ impl Quality { | ||
| 90 | 92 | |
| 91 | 93 | pub(crate) fn label(self) -> &'static str { |
| 92 | 94 | match self { |
| 95 | + Quality::Exact => "Exact", | |
| 93 | 96 | Quality::Auto => "Auto", |
| 94 | 97 | Quality::Low => "Low", |
| 95 | 98 | Quality::Medium => "Medium", |
| @@ -101,6 +104,12 @@ impl Quality { | ||
| 101 | 104 | /// The figure the row stands for, said in the units the row is chosen by. |
| 102 | 105 | pub(crate) fn detail(self, custom_mbps: u32) -> String { |
| 103 | 106 | 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(), | |
| 104 | 113 | Quality::Auto => "from the picture size and frame rate".to_string(), |
| 105 | 114 | Quality::Custom => { |
| 106 | 115 | format!("{custom_mbps} Mbps — wheel or ± steps, n types one, {MBPS_MIN}–{MBPS_MAX}") |
| @@ -858,12 +867,17 @@ pub(crate) fn export_settings( | ||
| 858 | 867 | // has dimmed cannot have been changed. |
| 859 | 868 | audio_kbps: Some(audio_kbps), |
| 860 | 869 | 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, | |
| 862 | 875 | Quality::Low => Some(2_000_000), |
| 863 | 876 | Quality::Medium => Some(6_000_000), |
| 864 | 877 | Quality::High => Some(12_000_000), |
| 865 | 878 | Quality::Custom => Some(u64::from(custom_mbps) * 1_000_000), |
| 866 | 879 | }, |
| 880 | + exact: quality == Quality::Exact, | |
| 867 | 881 | // The card's own row now ([`encoder_choices`]), kept with the project: |
| 868 | 882 | // it was a `VE_SW_ENC` env pin and nothing else, which is a switch |
| 869 | 883 | // nobody exporting a film would ever find. |
| @@ -918,6 +932,21 @@ pub(crate) const MBPS_DIGITS: usize = 3; | ||
| 918 | 932 | /// cannot say what the timeline says (`export::copy_audio`), so none of them is |
| 919 | 933 | /// a refusal any more -- and every video format carries the sound, so there is |
| 920 | 934 | /// 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 | + | |
| 921 | 950 | pub(crate) fn format_refusal(session: &PlaybackSession, format: Format) -> Option<String> { |
| 922 | 951 | if !format.has_video() { |
| 923 | 952 | return None; |
crates/app/src/player/export.rsmodified+8 -0
| @@ -726,6 +726,14 @@ impl Player { | ||
| 726 | 726 | cx.notify(); |
| 727 | 727 | return; |
| 728 | 728 | } |
| 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 | + } | |
| 729 | 737 | session.pause(); |
| 730 | 738 | self.export = Some(session.export_to_with(&self.export_path, &settings)); |
| 731 | 739 | // 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 { | ||
| 373 | 373 | /// export nobody has marked in or out on writes the same bytes it always |
| 374 | 374 | /// did. Session state, like the mark itself: never saved to a `.edith`. |
| 375 | 375 | 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, | |
| 376 | 388 | } |
| 377 | 389 | |
| 378 | 390 | /// The lowest H.264 level (Table A-1) whose frame size and macroblock rate |
| @@ -1673,6 +1685,52 @@ struct CopyRegion { | ||
| 1673 | 1685 | start: u32, |
| 1674 | 1686 | } |
| 1675 | 1687 | |
| 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 | + | |
| 1676 | 1734 | impl CopyPlan { |
| 1677 | 1735 | /// The plan for this timeline, or `None` where anything at all makes a copy |
| 1678 | 1736 | /// say something other than what the timeline says -- in which case the |
| @@ -2429,6 +2487,9 @@ fn run( | ||
| 2429 | 2487 | if settings.intra_only && settings.format != Format::Mp4 { |
| 2430 | 2488 | return Err(format!("{} cannot be written intra-only", settings.format.name()).into()); |
| 2431 | 2489 | } |
| 2490 | + if let Some(why) = exact_refusal(project, meta, settings) { | |
| 2491 | + return Err(why.into()); | |
| 2492 | + } | |
| 2432 | 2493 | let sources = project.sources(); |
| 2433 | 2494 | // The picture's decision first, before a sample of sound is touched: it is |
| 2434 | 2495 | // 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() { | ||
| 169 | 169 | } |
| 170 | 170 | } |
| 171 | 171 | |
| 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 | + | |
| 172 | 242 | /// A cut placed on a sync point: the middle of the film is deleted and both |
| 173 | 243 | /// sides leave as copies. What is checked is the join -- the blocks either side |
| 174 | 244 | /// of it are the source's own, the timestamps rise a frame at a time across it |