diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rs index 562dbdf..6b20c8d 100644 --- a/crates/engine/src/export.rs +++ b/crates/engine/src/export.rs @@ -1837,7 +1837,8 @@ pub fn exact_refusal(project: &Project, meta: &VideoMeta, settings: &ExportSetti 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" + point, a grade, a speed, a transform or a canvas the source is not forces a \ + re-encode" .to_string(), }) } @@ -1854,6 +1855,16 @@ impl CopyPlan { }; let entries = project.sources(); let mut sources: Vec> = (0..entries.len()).map(|_| None).collect(); + // The DRAWN pair each source is displayed at, kept beside its demuxer: + // the fit gate below asks what a clip's policy does with this source's + // picture on this canvas, and the picture the encoder places is the + // drawn one ([`VideoMeta::width`]/`height`] -- the coded one with the + // file's turn and its pixel aspect already applied), not the pair the + // samples are stored at. A non-square source at its own displayed size + // is therefore the identity again, which is what a copy of one is: the + // aspect travels beside the track in the `Display*` elements rather + // than being resampled into it. + let mut drawn: Vec> = (0..entries.len()).map(|_| None).collect(); let mut declared: Option<(Vec, ColorDescription, crate::demux::PixelAspect)> = None; let mut regions: Vec = Vec::new(); let r_start = settings.range.map_or(0, |(s, _)| s); @@ -1952,8 +1963,56 @@ impl CopyPlan { return None; } sources[source] = Some(demuxer); + drawn[source] = Some((source_meta.width, source_meta.height)); } let demuxer = sources.get_mut(source)?.as_mut()?; + // **The canvas gate** -- the condition this path was missing, and + // the one that decides whether a copy can state the project's size. + // + // A copied packet is the source's own coded picture; the file this + // export writes has to be the size the *project* is, and those are + // two different pairs held in two different fields: + // + // * `meta.coded_width`/`coded_height` is the pair a *copied* track + // declares -- what the samples are stored at, and what the check + // above compares every source against. Only a *source* ever moves + // it. + // * `meta.width`/`meta.height` is the canvas -- what a person chose + // with [`crate::playback::PlaybackSession::set_resolution`], + // which writes those two and leaves the coded pair exactly where + // it was. The encode door sizes everything from it + // ([`Composer`]), so this is the size the file must have. + // + // ...and the two meet in the *drawn* pair: what this span is + // composed from is the source's picture as a decoder delivers it, + // turn and pixel aspect already applied + // ([`VideoMeta::width`]/`height`]), which is what the check below is + // asked about. + // + // A canvas that differs from the source's drawn pair means every + // span is a picture the export has to *place onto* that canvas, and + // there is no resampler on this path at all -- so a copy would ship + // the source's frame size under the project's name, with the seat + // line reading "copy · …" all the way out. That is the defect: not + // a wrong rate or a wrong codec but the wrong geometry, silently, + // on the one door that is supposed to be exact. + // + // Asked as a *placement* rather than as two separate conditions -- + // is this clip's policy the identity for this source on this canvas + // -- because that is the one thing a condition has to mean here: + // [`fit_is_identity`] computes it from [`scale::fit_rect`] itself, + // so it holds for `Fill` on a canvas the size of the picture the + // source draws at (there is nothing to crop there) and refuses a + // canvas of another size under *every* policy, and a + // [`crate::scale::FitPolicy`] added later is judged by what it + // places rather than by being absent from a list here. + if !fit_is_identity( + drawn.get(source).copied().flatten()?, + (meta.width, meta.height), + project.composite_fit_at(span.start), + ) { + return None; + } // The window this span reads, in the blocks the whole engine counts // a source's frames in. It has to *be* whole groups of pictures: a // copy that began between two sync points would hand a decoder @@ -2067,6 +2126,59 @@ impl CopyPlan { } } +/// Whether `policy` places the picture `source` is DRAWN at onto this `canvas` +/// without touching a sample: the whole picture, at the whole canvas, 1:1. +/// +/// The 1:1 is the half the rects cannot state: a source scaled onto a canvas +/// of another aspect is placed whole and cropped nowhere, which is a resize +/// and not an identity -- so the pairs themselves are compared first. +/// +/// The pair asked about is the **drawn** one ([`VideoMeta::width`]/`height`]), +/// never the coded one: what the fit is composed against is what a decoder +/// hands the canvas after the file's turn and its pixel aspect have been +/// applied, so a 1440x1080 file with a 4:3 sample aspect is a 1920x1080 +/// picture, and a project at 1920x1080 is copying it exactly. The coded pair +/// has its own question -- what a copied track *declares* -- and the coded-to- +/// coded check in [`CopyPlan::of`] already asks that one; asking it twice, in +/// the geometry's stead, refuses a copy main performs byte for byte. +/// +/// Asked from [`scale::fit_rect`] -- the one function that *is* a fit policy's +/// meaning -- rather than from the policy itself, which is what makes this +/// canvas-aware twice over: `Fill` on a canvas the size of the source's own +/// samples is the identity (nothing is cropped, because there is nothing to +/// crop), while `Fit` on a canvas of another aspect is not (it bars or crops). +/// A policy added to [`FitPolicy`] later is judged by what it places, not by +/// being absent from a list here. +fn fit_is_identity( + source: (u32, u32), + canvas: (u32, u32), + policy: crate::scale::FitPolicy, +) -> bool { + // The pairs themselves: a picture is the canvas only when it *is* the + // canvas, and the rects below cannot say so -- a source scaled onto a + // canvas of another aspect is still placed whole and still cropped + // nowhere, which is a resize, and a resize is exactly what a copied packet + // cannot be. + if source != canvas { + return false; + } + let (placed, cropped) = crate::scale::fit_rect(source.0, source.1, canvas.0, canvas.1, policy); + placed + == crate::scale::Rect { + x: 0, + y: 0, + w: canvas.0, + h: canvas.1, + } + && cropped + == crate::scale::Rect { + x: 0, + y: 0, + w: source.0, + h: source.1, + } +} + /// The same lane, *decoded*: what a packet copy cannot carry comes out here as /// AAC written by this project's own encoder. /// diff --git a/crates/engine/tests/video_copy.rs b/crates/engine/tests/video_copy.rs index 8b42a85..9aa3e8b 100644 --- a/crates/engine/tests/video_copy.rs +++ b/crates/engine/tests/video_copy.rs @@ -404,6 +404,73 @@ fn a_touched_timeline_is_still_encoded() { assert!(!line.starts_with("copy"), "an mp4 was copied: {line}"); } +/// The canvas gate, which is the gate that is missing: a project resized to a +/// size its source is not comes out at the **project's** size, not at the +/// source's frame size wearing a "copy" label. +/// +/// The seat line is the symptom -- a user reads "copy · …" and hands over a +/// file at the wrong resolution -- so this checks both halves: the file was +/// encoded rather than copied, and it is the canvas that is in it. +#[test] +fn a_canvas_the_source_is_not_is_encoded_at_the_canvas() { + let source = asset("test_hevc.mkv"); + let (source_meta, _) = Demuxer::open(&source).expect("open the fixture"); + let (canvas, portrait) = ((960, 540), (720, 1280)); + assert_ne!( + (canvas.0, canvas.1), + (source_meta.coded_width, source_meta.coded_height), + "the fixture is already this size: the gate is not exercised" + ); + + for (label, (w, h)) in [("a smaller canvas", canvas), ("a taller canvas", portrait)] { + let mut session = PlaybackSession::open(&source).expect("open"); + assert!(session.set_resolution(w, h), "resize the project"); + let out = Scratch::file("ve_copy_canvas", "mkv"); + let line = export(&session, &out, Format::Hevc); + assert!( + !line.starts_with("copy"), + "{label} ({w}x{h}) was copied: {line}" + ); + let (written, _) = Demuxer::open(&out).expect("reopen the export"); + assert_eq!( + (written.coded_width, written.coded_height), + (w, h), + "{label} ({w}x{h}) came out at the source's size" + ); + } +} + +/// ...and the fit policy is read against the *canvas*, not on its own: a clip +/// on a canvas the size of its own coded picture is the identity under every +/// policy, `Fill` included, so `Fill` there is still the source's own blocks. +/// A gate that refused every non-default policy would pass the test above and +/// break this one. +#[test] +fn a_fit_policy_on_a_matching_canvas_is_still_the_identity() { + let source = asset("test_hevc.mkv"); + for (label, fit) in [ + ("fill", engine::scale::FitPolicy::Fill), + ("stretch", engine::scale::FitPolicy::Stretch), + ("centre", engine::scale::FitPolicy::Center), + ] { + let mut session = PlaybackSession::open(&source).expect("open"); + assert!(session.set_fit(Lane::V1, 0, fit), "{label}: set the fit"); + let out = Scratch::file("ve_copy_fit", "mkv"); + let line = export(&session, &out, Format::Hevc); + assert!( + line.starts_with("copy · "), + "{label} on the source's own canvas was not copied: {line}" + ); + let before = blocks(&source); + let after = blocks(&out); + assert_eq!(before.len(), after.len(), "{label}: block count"); + assert!( + before.iter().zip(&after).all(|(a, b)| a.bytes == b.bytes), + "{label}: the blocks are not the source's own" + ); + } +} + /// A cut that lands between two sync points is the case this path refuses: the /// head of the second region would need pictures that are not in the file, so /// the whole export goes back through the encoder rather than writing a file