🔀 merge(copycanvas): the copy door reads the project canvas
Browse files at this commit2 changed files +180 -1
- crates/engine/src/export.rs +113 -1
- crates/engine/tests/video_copy.rs +67 -0
crates/engine/src/export.rsmodified+113 -1
| @@ -1837,7 +1837,8 @@ pub fn exact_refusal(project: &Project, meta: &VideoMeta, settings: &ExportSetti | ||
| 1837 | 1837 | settings.format.name() |
| 1838 | 1838 | ), |
| 1839 | 1839 | true => "the edit does not let this file copy the source exactly — a cut off a sync \ |
| 1840 | - point, a grade, a speed or a transform forces a re-encode" | |
| 1840 | + point, a grade, a speed, a transform or a canvas the source is not forces a \ | |
| 1841 | + re-encode" | |
| 1841 | 1842 | .to_string(), |
| 1842 | 1843 | }) |
| 1843 | 1844 | } |
| @@ -1854,6 +1855,16 @@ impl CopyPlan { | ||
| 1854 | 1855 | }; |
| 1855 | 1856 | let entries = project.sources(); |
| 1856 | 1857 | let mut sources: Vec<Option<MkvDemuxer>> = (0..entries.len()).map(|_| None).collect(); |
| 1858 | + // The DRAWN pair each source is displayed at, kept beside its demuxer: | |
| 1859 | + // the fit gate below asks what a clip's policy does with this source's | |
| 1860 | + // picture on this canvas, and the picture the encoder places is the | |
| 1861 | + // drawn one ([`VideoMeta::width`]/`height`] -- the coded one with the | |
| 1862 | + // file's turn and its pixel aspect already applied), not the pair the | |
| 1863 | + // samples are stored at. A non-square source at its own displayed size | |
| 1864 | + // is therefore the identity again, which is what a copy of one is: the | |
| 1865 | + // aspect travels beside the track in the `Display*` elements rather | |
| 1866 | + // than being resampled into it. | |
| 1867 | + let mut drawn: Vec<Option<(u32, u32)>> = (0..entries.len()).map(|_| None).collect(); | |
| 1857 | 1868 | let mut declared: Option<(Vec<u8>, ColorDescription, crate::demux::PixelAspect)> = None; |
| 1858 | 1869 | let mut regions: Vec<CopyRegion> = Vec::new(); |
| 1859 | 1870 | let r_start = settings.range.map_or(0, |(s, _)| s); |
| @@ -1952,8 +1963,56 @@ impl CopyPlan { | ||
| 1952 | 1963 | return None; |
| 1953 | 1964 | } |
| 1954 | 1965 | sources[source] = Some(demuxer); |
| 1966 | + drawn[source] = Some((source_meta.width, source_meta.height)); | |
| 1955 | 1967 | } |
| 1956 | 1968 | let demuxer = sources.get_mut(source)?.as_mut()?; |
| 1969 | + // **The canvas gate** -- the condition this path was missing, and | |
| 1970 | + // the one that decides whether a copy can state the project's size. | |
| 1971 | + // | |
| 1972 | + // A copied packet is the source's own coded picture; the file this | |
| 1973 | + // export writes has to be the size the *project* is, and those are | |
| 1974 | + // two different pairs held in two different fields: | |
| 1975 | + // | |
| 1976 | + // * `meta.coded_width`/`coded_height` is the pair a *copied* track | |
| 1977 | + // declares -- what the samples are stored at, and what the check | |
| 1978 | + // above compares every source against. Only a *source* ever moves | |
| 1979 | + // it. | |
| 1980 | + // * `meta.width`/`meta.height` is the canvas -- what a person chose | |
| 1981 | + // with [`crate::playback::PlaybackSession::set_resolution`], | |
| 1982 | + // which writes those two and leaves the coded pair exactly where | |
| 1983 | + // it was. The encode door sizes everything from it | |
| 1984 | + // ([`Composer`]), so this is the size the file must have. | |
| 1985 | + // | |
| 1986 | + // ...and the two meet in the *drawn* pair: what this span is | |
| 1987 | + // composed from is the source's picture as a decoder delivers it, | |
| 1988 | + // turn and pixel aspect already applied | |
| 1989 | + // ([`VideoMeta::width`]/`height`]), which is what the check below is | |
| 1990 | + // asked about. | |
| 1991 | + // | |
| 1992 | + // A canvas that differs from the source's drawn pair means every | |
| 1993 | + // span is a picture the export has to *place onto* that canvas, and | |
| 1994 | + // there is no resampler on this path at all -- so a copy would ship | |
| 1995 | + // the source's frame size under the project's name, with the seat | |
| 1996 | + // line reading "copy · …" all the way out. That is the defect: not | |
| 1997 | + // a wrong rate or a wrong codec but the wrong geometry, silently, | |
| 1998 | + // on the one door that is supposed to be exact. | |
| 1999 | + // | |
| 2000 | + // Asked as a *placement* rather than as two separate conditions -- | |
| 2001 | + // is this clip's policy the identity for this source on this canvas | |
| 2002 | + // -- because that is the one thing a condition has to mean here: | |
| 2003 | + // [`fit_is_identity`] computes it from [`scale::fit_rect`] itself, | |
| 2004 | + // so it holds for `Fill` on a canvas the size of the picture the | |
| 2005 | + // source draws at (there is nothing to crop there) and refuses a | |
| 2006 | + // canvas of another size under *every* policy, and a | |
| 2007 | + // [`crate::scale::FitPolicy`] added later is judged by what it | |
| 2008 | + // places rather than by being absent from a list here. | |
| 2009 | + if !fit_is_identity( | |
| 2010 | + drawn.get(source).copied().flatten()?, | |
| 2011 | + (meta.width, meta.height), | |
| 2012 | + project.composite_fit_at(span.start), | |
| 2013 | + ) { | |
| 2014 | + return None; | |
| 2015 | + } | |
| 1957 | 2016 | // The window this span reads, in the blocks the whole engine counts |
| 1958 | 2017 | // a source's frames in. It has to *be* whole groups of pictures: a |
| 1959 | 2018 | // copy that began between two sync points would hand a decoder |
| @@ -2067,6 +2126,59 @@ impl CopyPlan { | ||
| 2067 | 2126 | } |
| 2068 | 2127 | } |
| 2069 | 2128 | |
| 2129 | +/// Whether `policy` places the picture `source` is DRAWN at onto this `canvas` | |
| 2130 | +/// without touching a sample: the whole picture, at the whole canvas, 1:1. | |
| 2131 | +/// | |
| 2132 | +/// The 1:1 is the half the rects cannot state: a source scaled onto a canvas | |
| 2133 | +/// of another aspect is placed whole and cropped nowhere, which is a resize | |
| 2134 | +/// and not an identity -- so the pairs themselves are compared first. | |
| 2135 | +/// | |
| 2136 | +/// The pair asked about is the **drawn** one ([`VideoMeta::width`]/`height`]), | |
| 2137 | +/// never the coded one: what the fit is composed against is what a decoder | |
| 2138 | +/// hands the canvas after the file's turn and its pixel aspect have been | |
| 2139 | +/// applied, so a 1440x1080 file with a 4:3 sample aspect is a 1920x1080 | |
| 2140 | +/// picture, and a project at 1920x1080 is copying it exactly. The coded pair | |
| 2141 | +/// has its own question -- what a copied track *declares* -- and the coded-to- | |
| 2142 | +/// coded check in [`CopyPlan::of`] already asks that one; asking it twice, in | |
| 2143 | +/// the geometry's stead, refuses a copy main performs byte for byte. | |
| 2144 | +/// | |
| 2145 | +/// Asked from [`scale::fit_rect`] -- the one function that *is* a fit policy's | |
| 2146 | +/// meaning -- rather than from the policy itself, which is what makes this | |
| 2147 | +/// canvas-aware twice over: `Fill` on a canvas the size of the source's own | |
| 2148 | +/// samples is the identity (nothing is cropped, because there is nothing to | |
| 2149 | +/// crop), while `Fit` on a canvas of another aspect is not (it bars or crops). | |
| 2150 | +/// A policy added to [`FitPolicy`] later is judged by what it places, not by | |
| 2151 | +/// being absent from a list here. | |
| 2152 | +fn fit_is_identity( | |
| 2153 | + source: (u32, u32), | |
| 2154 | + canvas: (u32, u32), | |
| 2155 | + policy: crate::scale::FitPolicy, | |
| 2156 | +) -> bool { | |
| 2157 | + // The pairs themselves: a picture is the canvas only when it *is* the | |
| 2158 | + // canvas, and the rects below cannot say so -- a source scaled onto a | |
| 2159 | + // canvas of another aspect is still placed whole and still cropped | |
| 2160 | + // nowhere, which is a resize, and a resize is exactly what a copied packet | |
| 2161 | + // cannot be. | |
| 2162 | + if source != canvas { | |
| 2163 | + return false; | |
| 2164 | + } | |
| 2165 | + let (placed, cropped) = crate::scale::fit_rect(source.0, source.1, canvas.0, canvas.1, policy); | |
| 2166 | + placed | |
| 2167 | + == crate::scale::Rect { | |
| 2168 | + x: 0, | |
| 2169 | + y: 0, | |
| 2170 | + w: canvas.0, | |
| 2171 | + h: canvas.1, | |
| 2172 | + } | |
| 2173 | + && cropped | |
| 2174 | + == crate::scale::Rect { | |
| 2175 | + x: 0, | |
| 2176 | + y: 0, | |
| 2177 | + w: source.0, | |
| 2178 | + h: source.1, | |
| 2179 | + } | |
| 2180 | +} | |
| 2181 | + | |
| 2070 | 2182 | /// The same lane, *decoded*: what a packet copy cannot carry comes out here as |
| 2071 | 2183 | /// AAC written by this project's own encoder. |
| 2072 | 2184 | /// |
crates/engine/tests/video_copy.rsmodified+67 -0
| @@ -404,6 +404,73 @@ fn a_touched_timeline_is_still_encoded() { | ||
| 404 | 404 | assert!(!line.starts_with("copy"), "an mp4 was copied: {line}"); |
| 405 | 405 | } |
| 406 | 406 | |
| 407 | +/// The canvas gate, which is the gate that is missing: a project resized to a | |
| 408 | +/// size its source is not comes out at the **project's** size, not at the | |
| 409 | +/// source's frame size wearing a "copy" label. | |
| 410 | +/// | |
| 411 | +/// The seat line is the symptom -- a user reads "copy · …" and hands over a | |
| 412 | +/// file at the wrong resolution -- so this checks both halves: the file was | |
| 413 | +/// encoded rather than copied, and it is the canvas that is in it. | |
| 414 | +#[test] | |
| 415 | +fn a_canvas_the_source_is_not_is_encoded_at_the_canvas() { | |
| 416 | + let source = asset("test_hevc.mkv"); | |
| 417 | + let (source_meta, _) = Demuxer::open(&source).expect("open the fixture"); | |
| 418 | + let (canvas, portrait) = ((960, 540), (720, 1280)); | |
| 419 | + assert_ne!( | |
| 420 | + (canvas.0, canvas.1), | |
| 421 | + (source_meta.coded_width, source_meta.coded_height), | |
| 422 | + "the fixture is already this size: the gate is not exercised" | |
| 423 | + ); | |
| 424 | + | |
| 425 | + for (label, (w, h)) in [("a smaller canvas", canvas), ("a taller canvas", portrait)] { | |
| 426 | + let mut session = PlaybackSession::open(&source).expect("open"); | |
| 427 | + assert!(session.set_resolution(w, h), "resize the project"); | |
| 428 | + let out = Scratch::file("ve_copy_canvas", "mkv"); | |
| 429 | + let line = export(&session, &out, Format::Hevc); | |
| 430 | + assert!( | |
| 431 | + !line.starts_with("copy"), | |
| 432 | + "{label} ({w}x{h}) was copied: {line}" | |
| 433 | + ); | |
| 434 | + let (written, _) = Demuxer::open(&out).expect("reopen the export"); | |
| 435 | + assert_eq!( | |
| 436 | + (written.coded_width, written.coded_height), | |
| 437 | + (w, h), | |
| 438 | + "{label} ({w}x{h}) came out at the source's size" | |
| 439 | + ); | |
| 440 | + } | |
| 441 | +} | |
| 442 | + | |
| 443 | +/// ...and the fit policy is read against the *canvas*, not on its own: a clip | |
| 444 | +/// on a canvas the size of its own coded picture is the identity under every | |
| 445 | +/// policy, `Fill` included, so `Fill` there is still the source's own blocks. | |
| 446 | +/// A gate that refused every non-default policy would pass the test above and | |
| 447 | +/// break this one. | |
| 448 | +#[test] | |
| 449 | +fn a_fit_policy_on_a_matching_canvas_is_still_the_identity() { | |
| 450 | + let source = asset("test_hevc.mkv"); | |
| 451 | + for (label, fit) in [ | |
| 452 | + ("fill", engine::scale::FitPolicy::Fill), | |
| 453 | + ("stretch", engine::scale::FitPolicy::Stretch), | |
| 454 | + ("centre", engine::scale::FitPolicy::Center), | |
| 455 | + ] { | |
| 456 | + let mut session = PlaybackSession::open(&source).expect("open"); | |
| 457 | + assert!(session.set_fit(Lane::V1, 0, fit), "{label}: set the fit"); | |
| 458 | + let out = Scratch::file("ve_copy_fit", "mkv"); | |
| 459 | + let line = export(&session, &out, Format::Hevc); | |
| 460 | + assert!( | |
| 461 | + line.starts_with("copy · "), | |
| 462 | + "{label} on the source's own canvas was not copied: {line}" | |
| 463 | + ); | |
| 464 | + let before = blocks(&source); | |
| 465 | + let after = blocks(&out); | |
| 466 | + assert_eq!(before.len(), after.len(), "{label}: block count"); | |
| 467 | + assert!( | |
| 468 | + before.iter().zip(&after).all(|(a, b)| a.bytes == b.bytes), | |
| 469 | + "{label}: the blocks are not the source's own" | |
| 470 | + ); | |
| 471 | + } | |
| 472 | +} | |
| 473 | + | |
| 407 | 474 | /// A cut that lands between two sync points is the case this path refuses: the |
| 408 | 475 | /// head of the second region would need pictures that are not in the file, so |
| 409 | 476 | /// the whole export goes back through the encoder rather than writing a file |