Judge a DisplayUnit 0 pixel pair by its raster, not by its terms
Browse files at this commit598e6c8b2a92b6eaafc1636e7e02d637732800fcParent: fcb7844- fix
- judge a DisplayUnit 0 pixel pair by its raster, not by its terms
4 changed files +272 -45
- crates/engine/src/demux.rs +143 -43
- crates/engine/src/lib.rs +12 -2
- crates/engine/tests/display_unit.rs +77 -0
- crates/engine/tests/untrusted_header.rs +40 -0
crates/engine/src/demux.rsmodified+143 -43
| @@ -441,6 +441,41 @@ impl PixelAspect { | |||
| 441 | (h / a <= Self::MAX_ASPECT_TERM && v / a <= Self::MAX_ASPECT_TERM).then_some(Self { h, v }) | 441 | (h / a <= Self::MAX_ASPECT_TERM && v / a <= Self::MAX_ASPECT_TERM).then_some(Self { h, v }) |
| 442 | } | 442 | } |
| 443 | 443 | ||
| 444 | + /// The ratio a Matroska `DisplayUnit 0` `Display*` pair names, or [`None`] | ||
| 445 | + /// when the pair is not a picture. | ||
| 446 | + /// | ||
| 447 | + /// This is deliberately *not* [`Self::stated`], and the reason is what a | ||
| 448 | + /// unit-0 pair reduces to. The pair is a pixel SIZE, so reducing it | ||
| 449 | + /// against the coded size carries the sizes, not terms: 1920x1080 over | ||
| 450 | + /// 1440x1080 reduces to 4:3, but a 1920x803 scope -- one of the two most | ||
| 451 | + /// common letterbox ratios there are -- reduces to 1440:803, and a | ||
| 452 | + /// 1920x1037 flat to 1440:1037. A term bound tight enough to catch chosen | ||
| 453 | + /// bytes refuses exactly the pictures this arm exists to show. | ||
| 454 | + /// | ||
| 455 | + /// Only the *width* half is taken, because width is the only axis an | ||
| 456 | + /// aspect moves in this engine: `VideoMeta::height` is the coded height | ||
| 457 | + /// for every file, by design and by its own documentation. So the pair | ||
| 458 | + /// states the display width and the height is left where it is -- the same | ||
| 459 | + /// answer a 4:3 SAR over 1440x1080 gives, 1920x1080. | ||
| 460 | + /// | ||
| 461 | + /// What needs bounding is the raster, and [`Self::width_factor`] bounds | ||
| 462 | + /// that with [`Self::MAX_DISPLAY_WIDTH`] on every path. So this has only | ||
| 463 | + /// to refuse the degenerate claims: a zero half. | ||
| 464 | + pub fn stated_size(display_width: u64, display_height: u64, coded: (u32, u32)) -> Option<Self> { | ||
| 465 | + // The height is checked for the degenerate claim and then goes unused: | ||
| 466 | + // a zero-height pair is a header that lies, and one this engine reads | ||
| 467 | + // as stating nothing. A real height cannot reach an allocation on this | ||
| 468 | + // path, because `VideoMeta::height` is the *coded* height, which both | ||
| 469 | + // doors already bound against [`crate::MAX_RESOLUTION`]. | ||
| 470 | + if display_width == 0 || display_height == 0 || coded.0 == 0 { | ||
| 471 | + return None; | ||
| 472 | + } | ||
| 473 | + Some(Self { | ||
| 474 | + h: display_width.min(u64::from(u32::MAX)) as u32, | ||
| 475 | + v: coded.0, | ||
| 476 | + }) | ||
| 477 | + } | ||
| 478 | + | ||
| 444 | /// Whether the displayed picture is the coded one, drawn one to one. | 479 | /// Whether the displayed picture is the coded one, drawn one to one. |
| 445 | /// | 480 | /// |
| 446 | /// Both halves of the ratio are untrusted bytes -- four of them in an mp4 | 481 | /// Both halves of the ratio are untrusted bytes -- four of them in an mp4 |
| @@ -460,26 +495,32 @@ impl PixelAspect { | |||
| 460 | /// cover -- the same rounding swscale applies, and the same one that keeps | 495 | /// cover -- the same rounding swscale applies, and the same one that keeps |
| 461 | /// the stretch a whole-sample operation rather than a smear. | 496 | /// the stretch a whole-sample operation rather than a smear. |
| 462 | pub fn width_factor(self, coded_width: u32) -> u32 { | 497 | pub fn width_factor(self, coded_width: u32) -> u32 { |
| 463 | - // The same judgement [`Self::stated`] makes at the read, repeated here | 498 | + if self.is_square() { |
| 464 | - // because this is the one function every door reaches: a ratio built | ||
| 465 | - // rather than read still has to survive the multiply that follows. | ||
| 466 | - if Self::stated(self.h, self.v).is_none() { | ||
| 467 | return coded_width; | 499 | return coded_width; |
| 468 | } | 500 | } |
| 469 | // (coded * h / v), rounded up to even: 1440 at 4:3 gives 1920 exactly, | 501 | // (coded * h / v), rounded up to even: 1440 at 4:3 gives 1920 exactly, |
| 470 | // and a ratio that does not divide evenly is widened to the nearest | 502 | // and a ratio that does not divide evenly is widened to the nearest |
| 471 | // even column count rather than shown cropped or squeezed. | 503 | // even column count rather than shown cropped or squeezed. |
| 472 | // | 504 | // |
| 473 | - // Every step is checked, because the whole point of this arithmetic is | 505 | + // This is the one function every door reaches, so it is also where the |
| 474 | - // that it sizes an allocation from bytes a file chose. `coded * h` is | 506 | + // arithmetic is made total -- a ratio may be *read* here ([`Self::stated`]), |
| 475 | - // at most 2^64-ish and cannot overflow a u64, but the quotient is | 507 | + // *built* here from a `Display*` pixel pair ([`Self::stated_size`]), or |
| 476 | - // then *narrowed* to a u32 and can wrap -- 1920 * 0x00FF_FFFF narrows | 508 | + // both, and this cannot tell which. So it bounds the one thing all |
| 477 | - // to 2_147_481_728 -- and a narrowed zero becomes a zero-column | 509 | + // three agree on: the raster. `coded * h` cannot overflow a u64, but |
| 478 | - // raster. A display that does not fit [`Self::MAX_DISPLAY_WIDTH`] is | 510 | + // the quotient is then *narrowed* to a u32 and can wrap -- 1920 * |
| 479 | - // drawn at the coded size instead: the picture is shown slightly | 511 | + // 0x00FF_FFFF narrows to 2_147_481_728 -- and a narrowed zero becomes a |
| 480 | - // wrong, which is what every unrenderable header deserves, rather than | 512 | + // zero-column raster. Anything outside [`Self::MAX_DISPLAY_WIDTH`], in |
| 481 | - // aborting the decode worker on a capacity overflow before a pixel is | 513 | + // either direction, is drawn at the coded size instead: the picture is |
| 482 | - // drawn. | 514 | + // shown slightly wrong, which is what every unrenderable header |
| 515 | + // deserves, rather than aborting the decode worker on a capacity | ||
| 516 | + // overflow before a pixel is drawn. | ||
| 517 | + // | ||
| 518 | + // Judging the *ratio* instead would be the wrong test here: a | ||
| 519 | + // `DisplayUnit 0` pair is a pixel size, so its reduced form carries | ||
| 520 | + // the sizes (a 1920x803 scope reduces to 1440:803) and no term bound | ||
| 521 | + // can be both tight enough to catch chosen bytes and loose enough to | ||
| 522 | + // admit the two most common letterbox ratios. The raster bound has no | ||
| 523 | + // such conflict. | ||
| 483 | let wide = u64::from(coded_width) * u64::from(self.h); | 524 | let wide = u64::from(coded_width) * u64::from(self.h); |
| 484 | let scaled = (wide + u64::from(self.v) - 1) / u64::from(self.v); | 525 | let scaled = (wide + u64::from(self.v) - 1) / u64::from(self.v); |
| 485 | let rounded = scaled.next_multiple_of(2); | 526 | let rounded = scaled.next_multiple_of(2); |
| @@ -3062,29 +3103,42 @@ fn pixel_aspect_of( | |||
| 3062 | if display_width == 0 || display_height == 0 || coded_width == 0 || coded_height == 0 { | 3103 | if display_width == 0 || display_height == 0 || coded_width == 0 || coded_height == 0 { |
| 3063 | return PixelAspect::SQUARE; | 3104 | return PixelAspect::SQUARE; |
| 3064 | } | 3105 | } |
| 3065 | - // A stated pair, whichever unit spells it, is a claim about a real | 3106 | + // A stated pair is a claim about a real picture, and each unit is judged |
| 3066 | - // picture and is judged by [`PixelAspect::stated`] like every other | 3107 | + // by what it means rather than by its digits: a unit that spells a *ratio* |
| 3067 | - // header read: a ratio that reduces to nothing usable is square, and the | 3108 | + // by [`PixelAspect::stated`], a unit that spells a *pixel size* by |
| 3068 | - // caller learns that by being handed it rather than by a zero-width | 3109 | + // [`PixelAspect::stated_size`]. Conflating those two is what would refuse |
| 3069 | - // raster later on. | 3110 | + // a 1920x803 scope, whose reduced form carries sizes rather than terms. |
| 3070 | - let stated = |h: u64, v: u64| { | 3111 | + let coded = (coded_width, coded_height); |
| 3071 | - PixelAspect::stated(h.min(u64::from(u32::MAX)) as u32, v.min(u64::from(u32::MAX)) as u32) | 3112 | + // `saturating_mul` throughout: the pair is untrusted and a product past a |
| 3072 | - .unwrap_or(PixelAspect::SQUARE) | 3113 | + // u64 is a claim with no picture behind it. |
| 3114 | + let as_ratio = || { | ||
| 3115 | + PixelAspect::stated( | ||
| 3116 | + display_width | ||
| 3117 | + .saturating_mul(u64::from(coded_height)) | ||
| 3118 | + .min(u64::from(u32::MAX)) as u32, | ||
| 3119 | + display_height | ||
| 3120 | + .saturating_mul(u64::from(coded_width)) | ||
| 3121 | + .min(u64::from(u32::MAX)) as u32, | ||
| 3122 | + ) | ||
| 3123 | + .unwrap_or(PixelAspect::SQUARE) | ||
| 3073 | }; | 3124 | }; |
| 3074 | match display_unit { | 3125 | match display_unit { |
| 3075 | // 4: the pair *is* the sample aspect ratio. | 3126 | // 4: the pair *is* the sample aspect ratio. |
| 3076 | - 4 => stated(display_width, display_height), | 3127 | + 4 => PixelAspect::stated( |
| 3077 | - // 3: the pair is a display *aspect ratio*, and 0: the pair is a | 3128 | + display_width.min(u64::from(u32::MAX)) as u32, |
| 3078 | - // display *pixel size*. Both reduce to the same SAR -- the ratio | 3129 | + display_height.min(u64::from(u32::MAX)) as u32, |
| 3079 | - // against the coded one -- and that they do is the point: 16/9 over | 3130 | + ) |
| 3080 | - // 1440x1080 and 1920x1080 over 1440x1080 are the same claim about the | 3131 | + .unwrap_or(PixelAspect::SQUARE), |
| 3081 | - // same picture, and both land on 4/3. `saturating_mul` because the | 3132 | + // 3: the pair is a display *aspect ratio* -- 16/9 over a 1440x1080 |
| 3082 | - // pair is untrusted and a product past a u64 is a claim with no | 3133 | + // coded track lands on 4/3 -- which is a small pair of terms however |
| 3083 | - // picture behind it. | 3134 | + // it was spelled, so the term bound is the right test for it. |
| 3084 | - 0 | 3 => stated( | 3135 | + 3 => as_ratio(), |
| 3085 | - display_width.saturating_mul(u64::from(coded_height)), | 3136 | + // 0: the pair is a display *pixel size*, the size the picture is drawn |
| 3086 | - display_height.saturating_mul(u64::from(coded_width)), | 3137 | + // at. 1920x1080 over 1440x1080 is the 4/3 stretch; 1920x803 over it is |
| 3087 | - ), | 3138 | + // a 2.39:1 scope. Bounded by the raster [`PixelAspect::width_factor`] |
| 3139 | + // produces, not by the terms it reduces to. | ||
| 3140 | + 0 => PixelAspect::stated_size(display_width, display_height, coded) | ||
| 3141 | + .unwrap_or(PixelAspect::SQUARE), | ||
| 3088 | // A physical unit this engine cannot interpret: square. | 3142 | // A physical unit this engine cannot interpret: square. |
| 3089 | _ => PixelAspect::SQUARE, | 3143 | _ => PixelAspect::SQUARE, |
| 3090 | } | 3144 | } |
| @@ -5764,8 +5818,41 @@ mod tests { | |||
| 5764 | /// wrapped raster the decode worker tries to allocate. | 5818 | /// wrapped raster the decode worker tries to allocate. |
| 5765 | #[test] | 5819 | #[test] |
| 5766 | fn an_untrusted_pixel_aspect_cannot_size_a_zero_or_wrapped_raster() { | 5820 | fn an_untrusted_pixel_aspect_cannot_size_a_zero_or_wrapped_raster() { |
| 5767 | - // Collected, not asserted one at a time: a witness that stops at the | 5821 | + // Two contracts, held separately, because they are enforced in two |
| 5768 | - // first degenerate hides whether the other classes are caught too. | 5822 | + // places and conflating them is what would break a real ratio. |
| 5823 | + // | ||
| 5824 | + // 1. At the READ, a ratio is refused: a zero half, or a *reduced* | ||
| 5825 | + // pair carrying a term past the bound. This is where an mp4 `pasp` | ||
| 5826 | + // is judged, and it is right there because a `pasp` pair IS the | ||
| 5827 | + // ratio. | ||
| 5828 | + for hostile in [ | ||
| 5829 | + PixelAspect { h: 0, v: 1 }, | ||
| 5830 | + PixelAspect { h: 1, v: 0 }, | ||
| 5831 | + PixelAspect { h: 0, v: 0 }, | ||
| 5832 | + // Four chosen bytes, not a ratio: reduced terms stay enormous. | ||
| 5833 | + PixelAspect { h: 0xFFFF_FFFF, v: 0xFFFF_FFFF - 1 }, | ||
| 5834 | + PixelAspect { h: 1, v: 0x00FF_FFFF }, | ||
| 5835 | + ] { | ||
| 5836 | + assert_eq!( | ||
| 5837 | + PixelAspect::stated(hostile.h, hostile.v), | ||
| 5838 | + None, | ||
| 5839 | + "{hostile:?} is not a ratio this engine can draw" | ||
| 5840 | + ); | ||
| 5841 | + } | ||
| 5842 | + // A `DisplayUnit 0` pixel pair is NOT a ratio, so it is judged by | ||
| 5843 | + // `stated_size` instead -- and the two most common letterbox ratios | ||
| 5844 | + // must survive that arm, because its reduced form carries sizes: | ||
| 5845 | + for (dw, dh) in [(1920u64, 1080u64), (1920, 803), (1920, 1037)] { | ||
| 5846 | + assert!( | ||
| 5847 | + PixelAspect::stated_size(dw, dh, (1440, 1080)).is_some(), | ||
| 5848 | + "a {dw}x{dh} Display pair over a 1440x1080 coded track is a real picture" | ||
| 5849 | + ); | ||
| 5850 | + } | ||
| 5851 | + | ||
| 5852 | + // 2. In the ARITHMETIC, whatever ratio arrived -- read or built -- | ||
| 5853 | + // cannot produce a zero column or a raster past the bound. This is | ||
| 5854 | + // the contract that holds for every door, including the one that | ||
| 5855 | + // builds its ratio from a pixel size rather than reading one. | ||
| 5769 | let wrong: Vec<String> = [ | 5856 | let wrong: Vec<String> = [ |
| 5770 | PixelAspect { h: 0, v: 1 }, | 5857 | PixelAspect { h: 0, v: 1 }, |
| 5771 | PixelAspect { h: 1, v: 0 }, | 5858 | PixelAspect { h: 1, v: 0 }, |
| @@ -5774,23 +5861,27 @@ mod tests { | |||
| 5774 | // 2_147_483_648, which is a plausible-looking "width" and an | 5861 | // 2_147_483_648, which is a plausible-looking "width" and an |
| 5775 | // impossible one to allocate. | 5862 | // impossible one to allocate. |
| 5776 | PixelAspect { h: 0x00FF_FFFF, v: 1 }, | 5863 | PixelAspect { h: 0x00FF_FFFF, v: 1 }, |
| 5777 | - PixelAspect { h: 0xFFFF_FFFF, v: 0xFFFF_FFFF - 1 }, | ||
| 5778 | - // A ratio that is not a raster anybody has: 1:0x00FF_FFFF. | ||
| 5779 | - PixelAspect { h: 1, v: 0x00FF_FFFF }, | ||
| 5780 | ] | 5864 | ] |
| 5781 | .into_iter() | 5865 | .into_iter() |
| 5782 | .filter_map(|hostile| { | 5866 | .filter_map(|hostile| { |
| 5783 | let factor = hostile.width_factor(1920); | 5867 | let factor = hostile.width_factor(1920); |
| 5784 | - (factor != 1920).then(|| { | 5868 | + (factor == 0 || factor > PixelAspect::MAX_DISPLAY_WIDTH) |
| 5785 | - format!("{hostile:?} -> {factor} (a zero column or an oversized raster)") | 5869 | + .then(|| format!("{hostile:?} -> {factor} (a zero column or an oversized raster)")) |
| 5786 | - }) | ||
| 5787 | }) | 5870 | }) |
| 5788 | .collect(); | 5871 | .collect(); |
| 5789 | assert!( | 5872 | assert!( |
| 5790 | wrong.is_empty(), | 5873 | wrong.is_empty(), |
| 5791 | - "a degenerate pixel aspect must read as square:\n{}", | 5874 | + "an unusable ratio must not size a raster:\n{}", |
| 5792 | wrong.join("\n") | 5875 | wrong.join("\n") |
| 5793 | ); | 5876 | ); |
| 5877 | + // The wrap case lands on the coded width rather than on the wrapped | ||
| 5878 | + // number, which is the specific refusal the defect turned on. | ||
| 5879 | + assert_eq!( | ||
| 5880 | + PixelAspect { h: 0x00FF_FFFF, v: 1 }.width_factor(1920), | ||
| 5881 | + 1920, | ||
| 5882 | + "a wrapped multiply is drawn at the coded width, not at 2_147_481_728" | ||
| 5883 | + ); | ||
| 5884 | + | ||
| 5794 | // The sane ratios still stretch, bit for bit as they always did: the | 5885 | // The sane ratios still stretch, bit for bit as they always did: the |
| 5795 | // hardening refuses the untrusted, it does not round the ordinary. | 5886 | // hardening refuses the untrusted, it does not round the ordinary. |
| 5796 | assert_eq!(PixelAspect { h: 4, v: 3 }.width_factor(1440), 1920); | 5887 | assert_eq!(PixelAspect { h: 4, v: 3 }.width_factor(1440), 1920); |
| @@ -5798,6 +5889,15 @@ mod tests { | |||
| 5798 | assert_eq!(PixelAspect::SQUARE.width_factor(1920), 1920); | 5889 | assert_eq!(PixelAspect::SQUARE.width_factor(1920), 1920); |
| 5799 | // An unreduced spelling of the same 4:3 lands on the same picture. | 5890 | // An unreduced spelling of the same 4:3 lands on the same picture. |
| 5800 | assert_eq!(PixelAspect { h: 16, v: 12 }.width_factor(1440), 1920); | 5891 | assert_eq!(PixelAspect { h: 16, v: 12 }.width_factor(1440), 1920); |
| 5892 | + // And a 2.39:1 scope built from a pixel size widens the coded frame | ||
| 5893 | + // by its own ratio rather than being refused for carrying sizes. | ||
| 5894 | + assert_eq!( | ||
| 5895 | + PixelAspect::stated_size(1920, 803, (1440, 1080)) | ||
| 5896 | + .expect("a scope is a picture") | ||
| 5897 | + .width_factor(1440), | ||
| 5898 | + 1920, | ||
| 5899 | + "a 1920-wide scope over 1440 coded columns widens to 1920" | ||
| 5900 | + ); | ||
| 5801 | } | 5901 | } |
| 5802 | 5902 | ||
| 5803 | #[test] | 5903 | #[test] |
crates/engine/src/lib.rsmodified+12 -2
| @@ -94,8 +94,18 @@ pub fn is_image(path: &std::path::Path) -> bool { | |||
| 94 | }) | 94 | }) |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | -/// The largest picture this engine composes onto, either way: 8K, which is where | 97 | +/// The largest picture this engine composes onto, either way: 7680, which is |
| 98 | -/// a per-frame buffer stops being a sane thing to allocate. | 98 | +/// where a per-frame buffer stops being a sane thing to allocate. |
| 99 | +/// | ||
| 100 | +/// That is 8K **UHD** (7680x4320), not 8K DCI (8192x4320). A DCI-width master | ||
| 101 | +/// -- a ProRes 4444 XQ feature, for instance -- is 8192 wide and is refused | ||
| 102 | +/// here by name, which it was not before the container doors asked this bound | ||
| 103 | +/// of a file's own header rather than only of a keystroke and a `.edith` line. | ||
| 104 | +/// The bound is deliberately not raised to 8192 to admit it: `is_resolution` | ||
| 105 | +/// gates those two doors at 7680 too, and widening the ceiling on every path | ||
| 106 | +/// to accommodate a container is a wider decision than a header fix should | ||
| 107 | +/// make. A caller that needs DCI width needs the ceiling raised deliberately, | ||
| 108 | +/// on all four doors at once. | ||
| 99 | pub const MAX_RESOLUTION: u32 = 7680; | 109 | pub const MAX_RESOLUTION: u32 = 7680; |
| 100 | 110 | ||
| 101 | /// Whether a project resolution is a picture at all: not zero either way, and | 111 | /// Whether a project resolution is a picture at all: not zero either way, and |
crates/engine/tests/display_unit.rsmodified+77 -0
| @@ -86,6 +86,25 @@ const VIDEO_UNIT_0: &str = concat!( | |||
| 86 | "54b28100", // DisplayUnit 0 -- pixels | 86 | "54b28100", // DisplayUnit 0 -- pixels |
| 87 | "55b085", "55b9820001" // Colour { MatrixCoefficients 1 } | 87 | "55b085", "55b9820001" // Colour { MatrixCoefficients 1 } |
| 88 | ); | 88 | ); |
| 89 | + | ||
| 90 | +/// The same `DisplayUnit 0` element with a different `Display*` pixel pair, | ||
| 91 | +/// for the ratios whose reduced form is not a small pair of integers. | ||
| 92 | +fn video_unit_0(display_width: u16, display_height: u16) -> String { | ||
| 93 | + // Both `Display*` values are two-byte payloads, so a width past 0x7FFF | ||
| 94 | + // would need three and change the element's length. The lengths here are | ||
| 95 | + // all inside that range, which is what keeps every offset in the file | ||
| 96 | + // honest. | ||
| 97 | + format!( | ||
| 98 | + "e09e\ | ||
| 99 | + b08205a0\ | ||
| 100 | + ba820438\ | ||
| 101 | + 54b082{display_width:04x}\ | ||
| 102 | + 54ba82{display_height:04x}\ | ||
| 103 | + 54b28100\ | ||
| 104 | + 55b085\ | ||
| 105 | + 55b9820001" | ||
| 106 | + ) | ||
| 107 | +} | ||
| 89 | fn with_video_element(source: &Path, into: &Path, element: &str) { | 108 | fn with_video_element(source: &Path, into: &Path, element: &str) { |
| 90 | let from = hex(VIDEO_UNIT_3); | 109 | let from = hex(VIDEO_UNIT_3); |
| 91 | let to = hex(element); | 110 | let to = hex(element); |
| @@ -265,3 +284,61 @@ fn a_real_size_up_to_the_bound_still_opens() { | |||
| 265 | bound from refusing the library" | 284 | bound from refusing the library" |
| 266 | ); | 285 | ); |
| 267 | } | 286 | } |
| 287 | + | ||
| 288 | +/// The regression this arm must not carry: a `DisplayUnit 0` pair is a pixel | ||
| 289 | +/// SIZE, so its *reduced* form is the size against the coded size, not a small | ||
| 290 | +/// pair of integers -- and the common cinematic display ratios do not reduce | ||
| 291 | +/// small. A 1920x803 scope over a 1440x1080 coded track reduces to 1440:803, | ||
| 292 | +/// and 1920x1037 (1.85:1 flat) likewise. | ||
| 293 | +/// | ||
| 294 | +/// The reduced-term bound is the right test for an mp4 `pasp`, where the pair | ||
| 295 | +/// *is* the ratio, and the wrong test here: applying it made the two most | ||
| 296 | +/// common letterbox ratios open square, on exactly the arm this fix newly | ||
| 297 | +/// enabled. The quantity that needs bounding on this door is the resulting | ||
| 298 | +/// display WIDTH, which `width_factor` already caps. | ||
| 299 | +#[test] | ||
| 300 | +fn a_cinematic_display_unit_zero_stretches_rather_than_reading_square() { | ||
| 301 | + let scratch = Scratch::new("cinema"); | ||
| 302 | + for (dw, dh, ratio, tag) in [ | ||
| 303 | + (1920u16, 803u16, "2.39:1 scope", "scope"), | ||
| 304 | + (1920, 1037, "1.85:1 flat", "flat"), | ||
| 305 | + ] { | ||
| 306 | + let into = scratch.path(&format!("{tag}.mkv")); | ||
| 307 | + with_video_element( | ||
| 308 | + &asset("test_anamorphic.mkv"), | ||
| 309 | + &into, | ||
| 310 | + &video_unit_0(dw, dh), | ||
| 311 | + ); | ||
| 312 | + | ||
| 313 | + let (meta, _) = Demuxer::open(&into) | ||
| 314 | + .unwrap_or_else(|why| panic!("{tag} ({ratio}): open failed: {why}")); | ||
| 315 | + assert!( | ||
| 316 | + !meta.pixel_aspect.is_square(), | ||
| 317 | + "{tag} ({ratio}): a DisplayUnit 0 pixel pair of {dw}x{dh} over a \ | ||
| 318 | + 1440x1080 coded track is a stretch, not square: got {:?}", | ||
| 319 | + meta.pixel_aspect | ||
| 320 | + ); | ||
| 321 | + // The stated DISPLAY WIDTH is the drawn width: unit 0 states it in | ||
| 322 | + // pixels, so it is taken as stated, not re-derived. The height stays | ||
| 323 | + // the coded one because a pixel aspect moves the width only in this | ||
| 324 | + // engine -- `VideoMeta::height` is the coded height for every file, by | ||
| 325 | + // design. A 1920x803 scope is therefore drawn 1920x1080 here: the | ||
| 326 | + // width is the file's own claim and the height is this engine's | ||
| 327 | + // documented model, and the two are not the same thing. | ||
| 328 | + assert_eq!( | ||
| 329 | + meta.width, | ||
| 330 | + u32::from(dw), | ||
| 331 | + "{tag} ({ratio}): the stated Display width is the drawn width" | ||
| 332 | + ); | ||
| 333 | + assert_eq!( | ||
| 334 | + meta.height, | ||
| 335 | + 1080, | ||
| 336 | + "{tag} ({ratio}): a pixel aspect never moves the height; the coded one stands" | ||
| 337 | + ); | ||
| 338 | + assert_eq!( | ||
| 339 | + (meta.coded_width, meta.coded_height), | ||
| 340 | + (1440, 1080), | ||
| 341 | + "{tag} ({ratio}): the samples are still stored at the coded size" | ||
| 342 | + ); | ||
| 343 | + } | ||
| 344 | +} | ||
crates/engine/tests/untrusted_header.rsmodified+40 -0
| @@ -205,3 +205,43 @@ fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> { | |||
| 205 | .windows(needle.len()) | 205 | .windows(needle.len()) |
| 206 | .position(|window| window == needle) | 206 | .position(|window| window == needle) |
| 207 | } | 207 | } |
| 208 | + | ||
| 209 | +/// The boundary of the bound, stated as a test rather than left as a surprise: | ||
| 210 | +/// 7680 -- 8K UHD -- opens, and 8192 -- 8K DCI, a real ProRes 4444 XQ master | ||
| 211 | +/// width -- is refused by name. | ||
| 212 | +/// | ||
| 213 | +/// The refusal of a DCI-width master is a behaviour change this lane | ||
| 214 | +/// introduced: before it, the bound was asked only of a keystroke and a | ||
| 215 | +/// `.edith` line, never of a file's own header, so a container could state any | ||
| 216 | +/// size at all. `MAX_RESOLUTION` stays 7680 rather than being raised to 8192, | ||
| 217 | +/// because it gates those two doors at 7680 too and widening the ceiling on | ||
| 218 | +/// every path to accommodate a container is a wider decision than a header fix | ||
| 219 | +/// should make. | ||
| 220 | +#[test] | ||
| 221 | +fn the_resolution_bound_admits_uhd_and_refuses_dci_width_by_name() { | ||
| 222 | + let scratch = Scratch::new("dci"); | ||
| 223 | + let coded_height = 720u16; | ||
| 224 | + | ||
| 225 | + let uhd = scratch.path("uhd_7680.mp4"); | ||
| 226 | + patch_sample_entry_size(&asset("test_baseline.mp4"), &uhd, 7680, coded_height); | ||
| 227 | + let (meta, _) = Demuxer::open(&uhd).expect("8K UHD is inside the bound and opens"); | ||
| 228 | + assert_eq!( | ||
| 229 | + (meta.coded_width, meta.coded_height), | ||
| 230 | + (7680, u32::from(coded_height)), | ||
| 231 | + "an 8K UHD header is read as stated" | ||
| 232 | + ); | ||
| 233 | + | ||
| 234 | + let dci = scratch.path("dci_8192.mp4"); | ||
| 235 | + patch_sample_entry_size(&asset("test_baseline.mp4"), &dci, 8192, coded_height); | ||
| 236 | + let Err(why) = Demuxer::open(&dci) else { | ||
| 237 | + panic!("an 8192-wide DCI master is refused by name, never opened"); | ||
| 238 | + }; | ||
| 239 | + assert!( | ||
| 240 | + engine::demux::UnsupportedDisplaySize::is_it(&why), | ||
| 241 | + "the refusal is the named one a caller can match: {why}" | ||
| 242 | + ); | ||
| 243 | + assert!( | ||
| 244 | + why.to_string().contains("8192"), | ||
| 245 | + "and it names the width claimed: {why}" | ||
| 246 | + ); | ||
| 247 | +} | ||