The copy door reads the project canvas, not just the coded size
Browse files at this commit2d54df8883eed5666a34cb7d0ecbb489cff903b8Parent: 0c9d76c- fix
- the copy door reads the project canvas, not just the coded size
2 changed files +159 -1
crates/engine/src/export.rsmodified+92 -1
| @@ -1837,7 +1837,8 @@ pub fn exact_refusal(project: &Project, meta: &VideoMeta, settings: &ExportSetti | |||
| 1837 | settings.format.name() | 1837 | settings.format.name() |
| 1838 | ), | 1838 | ), |
| 1839 | true => "the edit does not let this file copy the source exactly — a cut off a sync \ | 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 | .to_string(), | 1842 | .to_string(), |
| 1842 | }) | 1843 | }) |
| 1843 | } | 1844 | } |
| @@ -1854,6 +1855,11 @@ impl CopyPlan { | |||
| 1854 | }; | 1855 | }; |
| 1855 | let entries = project.sources(); | 1856 | let entries = project.sources(); |
| 1856 | let mut sources: Vec<Option<MkvDemuxer>> = (0..entries.len()).map(|_| None).collect(); | 1857 | let mut sources: Vec<Option<MkvDemuxer>> = (0..entries.len()).map(|_| None).collect(); |
| 1858 | + // The coded pair each source is stored at, kept beside its demuxer: | ||
| 1859 | + // the fit gate below asks what a clip's own policy does with *this* | ||
| 1860 | + // source's picture on this canvas, and the picture it has is the coded | ||
| 1861 | + // one, not the displayed one. | ||
| 1862 | + let mut coded: Vec<Option<(u32, u32)>> = (0..entries.len()).map(|_| None).collect(); | ||
| 1857 | let mut declared: Option<(Vec<u8>, ColorDescription, crate::demux::PixelAspect)> = None; | 1863 | let mut declared: Option<(Vec<u8>, ColorDescription, crate::demux::PixelAspect)> = None; |
| 1858 | let mut regions: Vec<CopyRegion> = Vec::new(); | 1864 | let mut regions: Vec<CopyRegion> = Vec::new(); |
| 1859 | let r_start = settings.range.map_or(0, |(s, _)| s); | 1865 | let r_start = settings.range.map_or(0, |(s, _)| s); |
| @@ -1952,8 +1958,49 @@ impl CopyPlan { | |||
| 1952 | return None; | 1958 | return None; |
| 1953 | } | 1959 | } |
| 1954 | sources[source] = Some(demuxer); | 1960 | sources[source] = Some(demuxer); |
| 1961 | + coded[source] = Some((source_meta.coded_width, source_meta.coded_height)); | ||
| 1955 | } | 1962 | } |
| 1956 | let demuxer = sources.get_mut(source)?.as_mut()?; | 1963 | let demuxer = sources.get_mut(source)?.as_mut()?; |
| 1964 | + // **The canvas gate** -- the condition this path was missing, and | ||
| 1965 | + // the one that decides whether a copy can state the project's size. | ||
| 1966 | + // | ||
| 1967 | + // A copied packet is the source's own coded picture; the file this | ||
| 1968 | + // export writes has to be the size the *project* is, and those are | ||
| 1969 | + // two different pairs held in two different fields: | ||
| 1970 | + // | ||
| 1971 | + // * `meta.coded_width`/`coded_height` is what the demuxer wrote | ||
| 1972 | + // and the check above compares against it. Only a *source* ever | ||
| 1973 | + // moves it. | ||
| 1974 | + // * `meta.width`/`meta.height` is the canvas -- what a person chose | ||
| 1975 | + // with [`crate::playback::PlaybackSession::set_resolution`], | ||
| 1976 | + // which writes those two and leaves the coded pair exactly | ||
| 1977 | + // where it was. The encode door sizes everything from it | ||
| 1978 | + // ([`Composer`]), so this is the size the file must have. | ||
| 1979 | + // | ||
| 1980 | + // A canvas that differs from the source's coded pair means every | ||
| 1981 | + // span is a picture the export has to *place onto* that canvas, and | ||
| 1982 | + // there is no resampler on this path at all -- so a copy would ship | ||
| 1983 | + // the source's frame size under the project's name, with the seat | ||
| 1984 | + // line reading "copy · …" all the way out. That is the defect: not | ||
| 1985 | + // a wrong rate or a wrong codec but the wrong geometry, silently, | ||
| 1986 | + // on the one door that is supposed to be exact. | ||
| 1987 | + // | ||
| 1988 | + // Asked as a *placement* rather than as two separate conditions -- | ||
| 1989 | + // is this clip's policy the identity for this source on this canvas | ||
| 1990 | + // -- because that is the one thing a condition has to mean here: | ||
| 1991 | + // [`fit_is_identity`] computes it from [`scale::fit_rect`] itself, | ||
| 1992 | + // so it holds for `Fill` on a canvas the size of the source's own | ||
| 1993 | + // samples (there is nothing to crop there) and refuses a canvas of | ||
| 1994 | + // another size under *every* policy, and a | ||
| 1995 | + // [`crate::scale::FitPolicy`] added later is judged by what it | ||
| 1996 | + // places rather than by being absent from a list here. | ||
| 1997 | + if !fit_is_identity( | ||
| 1998 | + coded.get(source).copied().flatten()?, | ||
| 1999 | + (meta.width, meta.height), | ||
| 2000 | + project.composite_fit_at(span.start), | ||
| 2001 | + ) { | ||
| 2002 | + return None; | ||
| 2003 | + } | ||
| 1957 | // The window this span reads, in the blocks the whole engine counts | 2004 | // The window this span reads, in the blocks the whole engine counts |
| 1958 | // a source's frames in. It has to *be* whole groups of pictures: a | 2005 | // a source's frames in. It has to *be* whole groups of pictures: a |
| 1959 | // copy that began between two sync points would hand a decoder | 2006 | // copy that began between two sync points would hand a decoder |
| @@ -2067,6 +2114,50 @@ impl CopyPlan { | |||
| 2067 | } | 2114 | } |
| 2068 | } | 2115 | } |
| 2069 | 2116 | ||
| 2117 | +/// Whether `policy` places a source stored at `source` onto this `canvas` | ||
| 2118 | +/// without touching a sample: the whole picture, at the whole canvas, 1:1. | ||
| 2119 | +/// | ||
| 2120 | +/// The 1:1 is the half the rects cannot state: a source scaled onto a canvas | ||
| 2121 | +/// of another aspect is placed whole and cropped nowhere, which is a resize | ||
| 2122 | +/// and not an identity -- so the pairs themselves are compared first. | ||
| 2123 | +/// | ||
| 2124 | +/// Asked from [`scale::fit_rect`] -- the one function that *is* a fit policy's | ||
| 2125 | +/// meaning -- rather than from the policy itself, which is what makes this | ||
| 2126 | +/// canvas-aware twice over: `Fill` on a canvas the size of the source's own | ||
| 2127 | +/// samples is the identity (nothing is cropped, because there is nothing to | ||
| 2128 | +/// crop), while `Fit` on a canvas of another aspect is not (it bars or crops). | ||
| 2129 | +/// A policy added to [`FitPolicy`] later is judged by what it places, not by | ||
| 2130 | +/// being absent from a list here. | ||
| 2131 | +fn fit_is_identity( | ||
| 2132 | + source: (u32, u32), | ||
| 2133 | + canvas: (u32, u32), | ||
| 2134 | + policy: crate::scale::FitPolicy, | ||
| 2135 | +) -> bool { | ||
| 2136 | + // The pairs themselves: a picture is the canvas only when it *is* the | ||
| 2137 | + // canvas, and the rects below cannot say so -- a source scaled onto a | ||
| 2138 | + // canvas of another aspect is still placed whole and still cropped | ||
| 2139 | + // nowhere, which is a resize, and a resize is exactly what a copied packet | ||
| 2140 | + // cannot be. | ||
| 2141 | + if source != canvas { | ||
| 2142 | + return false; | ||
| 2143 | + } | ||
| 2144 | + let (placed, cropped) = crate::scale::fit_rect(source.0, source.1, canvas.0, canvas.1, policy); | ||
| 2145 | + placed | ||
| 2146 | + == crate::scale::Rect { | ||
| 2147 | + x: 0, | ||
| 2148 | + y: 0, | ||
| 2149 | + w: canvas.0, | ||
| 2150 | + h: canvas.1, | ||
| 2151 | + } | ||
| 2152 | + && cropped | ||
| 2153 | + == crate::scale::Rect { | ||
| 2154 | + x: 0, | ||
| 2155 | + y: 0, | ||
| 2156 | + w: source.0, | ||
| 2157 | + h: source.1, | ||
| 2158 | + } | ||
| 2159 | +} | ||
| 2160 | + | ||
| 2070 | /// The same lane, *decoded*: what a packet copy cannot carry comes out here as | 2161 | /// The same lane, *decoded*: what a packet copy cannot carry comes out here as |
| 2071 | /// AAC written by this project's own encoder. | 2162 | /// AAC written by this project's own encoder. |
| 2072 | /// | 2163 | /// |
crates/engine/tests/video_copy.rsmodified+67 -0
| @@ -404,6 +404,73 @@ fn a_touched_timeline_is_still_encoded() { | |||
| 404 | assert!(!line.starts_with("copy"), "an mp4 was copied: {line}"); | 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 | /// A cut that lands between two sync points is the case this path refuses: the | 474 | /// A cut that lands between two sync points is the case this path refuses: the |
| 408 | /// head of the second region would need pictures that are not in the file, so | 475 | /// head of the second region would need pictures that are not in the file, so |
| 409 | /// the whole export goes back through the encoder rather than writing a file | 476 | /// the whole export goes back through the encoder rather than writing a file |