An untrusted pasp ratio cannot size a zero or wrapped raster
Browse files at this commit33ee2badf91aedd123dfdfa602461eda4e2e4937Parent: 0c9d76c- fix
- an untrusted pasp ratio cannot size a zero or wrapped raster
2 changed files +312 -29
- crates/engine/src/demux.rs +149 -29
- crates/engine/tests/untrusted_header.rs +163 -0
crates/engine/src/demux.rsmodified+149 -29
| @@ -372,12 +372,50 @@ impl PixelAspect { | |||
| 372 | /// The square pixels everything before anamorphic sources was. | 372 | /// The square pixels everything before anamorphic sources was. |
| 373 | pub const SQUARE: Self = Self { h: 1, v: 1 }; | 373 | pub const SQUARE: Self = Self { h: 1, v: 1 }; |
| 374 | 374 | ||
| 375 | + /// The widest display raster this engine will stretch a coded frame into. | ||
| 376 | + /// A container can claim any ratio at all, but no real one needs more | ||
| 377 | + /// than a 16k canvas's worth of columns, and the bound is what keeps a | ||
| 378 | + /// header claim from sizing an allocation no machine can satisfy. | ||
| 379 | + const MAX_DISPLAY_WIDTH: u32 = 65_535; | ||
| 380 | + | ||
| 381 | + /// The largest term a *reduced* aspect ratio may keep. A real display | ||
| 382 | + /// ratio is a small pair of integers however it was spelled -- 4:3, 16:9, | ||
| 383 | + /// 8:5, 12:11 -- and ffmpeg's unreduced DAR spellings reduce to one. | ||
| 384 | + /// A pair whose reduction still carries a term past this is not a ratio | ||
| 385 | + /// at all but four chosen bytes, and it is refused as one. | ||
| 386 | + const MAX_ASPECT_TERM: u32 = 1024; | ||
| 387 | + | ||
| 388 | + /// The ratio a container stated, or [`None`] when it stated one this | ||
| 389 | + /// engine cannot draw. Both halves arrive from header bytes -- read | ||
| 390 | + /// straight out of an mp4 `pasp` box, or computed from a Matroska | ||
| 391 | + /// `Display*` pair beside the coded size -- so the test lives here, once, | ||
| 392 | + /// rather than in each door that reads one. | ||
| 393 | + /// | ||
| 394 | + /// Unusable means: a zero half (nothing to divide by), or a *reduced* | ||
| 395 | + /// pair carrying a term past [`Self::MAX_ASPECT_TERM`] (a pair of chosen | ||
| 396 | + /// bytes rather than a ratio). Judged reduced, so an unreduced spelling of | ||
| 397 | + /// a real one -- ffmpeg writes 17280/12960 for 16:9 -- is judged by the | ||
| 398 | + /// picture it names and not by how many digits it took. | ||
| 399 | + pub fn stated(h: u32, v: u32) -> Option<Self> { | ||
| 400 | + if h == 0 || v == 0 { | ||
| 401 | + return None; | ||
| 402 | + } | ||
| 403 | + let (mut a, mut b) = (h, v); | ||
| 404 | + while b != 0 { | ||
| 405 | + (a, b) = (b, a % b); | ||
| 406 | + } | ||
| 407 | + (h / a <= Self::MAX_ASPECT_TERM && v / a <= Self::MAX_ASPECT_TERM).then_some(Self { h, v }) | ||
| 408 | + } | ||
| 409 | + | ||
| 375 | /// Whether the displayed picture is the coded one, drawn one to one. | 410 | /// Whether the displayed picture is the coded one, drawn one to one. |
| 411 | + /// | ||
| 412 | + /// Both halves of the ratio are untrusted bytes -- four of them in an mp4 | ||
| 413 | + /// `pasp` box, and a product of two header values on the Matroska door -- | ||
| 414 | + /// so a half that is zero is a claim this engine cannot divide by, and it | ||
| 415 | + /// means square rather than a division by zero: the file states nothing | ||
| 416 | + /// usable, which is what every file that states no aspect at all states. | ||
| 376 | pub fn is_square(self) -> bool { | 417 | pub fn is_square(self) -> bool { |
| 377 | - // An unstated ratio (0) and a stated 1:1 are the same pixels, and a | 418 | + self.h == 0 || self.v == 0 || self.h == self.v |
| 378 | - // degenerate one a crafted header claimed is square rather than a | ||
| 379 | - // division by zero. | ||
| 380 | - self.v == 0 || self.h == self.v | ||
| 381 | } | 419 | } |
| 382 | 420 | ||
| 383 | /// How many coded columns one displayed column is worth: the factor the | 421 | /// How many coded columns one displayed column is worth: the factor the |
| @@ -388,14 +426,33 @@ impl PixelAspect { | |||
| 388 | /// cover -- the same rounding swscale applies, and the same one that keeps | 426 | /// cover -- the same rounding swscale applies, and the same one that keeps |
| 389 | /// the stretch a whole-sample operation rather than a smear. | 427 | /// the stretch a whole-sample operation rather than a smear. |
| 390 | pub fn width_factor(self, coded_width: u32) -> u32 { | 428 | pub fn width_factor(self, coded_width: u32) -> u32 { |
| 391 | - if self.is_square() { | 429 | + // The same judgement [`Self::stated`] makes at the read, repeated here |
| 430 | + // because this is the one function every door reaches: a ratio built | ||
| 431 | + // rather than read still has to survive the multiply that follows. | ||
| 432 | + if Self::stated(self.h, self.v).is_none() { | ||
| 392 | return coded_width; | 433 | return coded_width; |
| 393 | } | 434 | } |
| 394 | // (coded * h / v), rounded up to even: 1440 at 4:3 gives 1920 exactly, | 435 | // (coded * h / v), rounded up to even: 1440 at 4:3 gives 1920 exactly, |
| 395 | // and a ratio that does not divide evenly is widened to the nearest | 436 | // and a ratio that does not divide evenly is widened to the nearest |
| 396 | // even column count rather than shown cropped or squeezed. | 437 | // even column count rather than shown cropped or squeezed. |
| 438 | + // | ||
| 439 | + // Every step is checked, because the whole point of this arithmetic is | ||
| 440 | + // that it sizes an allocation from bytes a file chose. `coded * h` is | ||
| 441 | + // at most 2^64-ish and cannot overflow a u64, but the quotient is | ||
| 442 | + // then *narrowed* to a u32 and can wrap -- 1920 * 0x00FF_FFFF narrows | ||
| 443 | + // to 2_147_481_728 -- and a narrowed zero becomes a zero-column | ||
| 444 | + // raster. A display that does not fit [`Self::MAX_DISPLAY_WIDTH`] is | ||
| 445 | + // drawn at the coded size instead: the picture is shown slightly | ||
| 446 | + // wrong, which is what every unrenderable header deserves, rather than | ||
| 447 | + // aborting the decode worker on a capacity overflow before a pixel is | ||
| 448 | + // drawn. | ||
| 397 | let wide = u64::from(coded_width) * u64::from(self.h); | 449 | let wide = u64::from(coded_width) * u64::from(self.h); |
| 398 | - (((wide + u64::from(self.v) - 1) / u64::from(self.v)) as u32 | 1) & !1 | 450 | + let scaled = (wide + u64::from(self.v) - 1) / u64::from(self.v); |
| 451 | + let rounded = scaled.next_multiple_of(2); | ||
| 452 | + if rounded == 0 || rounded > u64::from(Self::MAX_DISPLAY_WIDTH) { | ||
| 453 | + return coded_width; | ||
| 454 | + } | ||
| 455 | + rounded as u32 | ||
| 399 | } | 456 | } |
| 400 | } | 457 | } |
| 401 | 458 | ||
| @@ -2911,14 +2968,21 @@ fn rotation_of_roll(degrees: f64) -> crate::Result<Rotation> { | |||
| 2911 | /// | 2968 | /// |
| 2912 | /// The units decide what the pair *is*: 3 spells a display aspect ratio (16 and | 2969 | /// The units decide what the pair *is*: 3 spells a display aspect ratio (16 and |
| 2913 | /// 9 over 1440x1080 is what ffmpeg writes for a `setsar=4/3` source, measured), | 2970 | /// 9 over 1440x1080 is what ffmpeg writes for a `setsar=4/3` source, measured), |
| 2914 | -/// 4 spells the sample aspect ratio itself, and 0/absent spells pixels -- where | 2971 | +/// 4 spells the sample aspect ratio itself, and **0 spells pixels** -- which is |
| 2915 | -/// a pair beside the coded one at all means a crop, not an aspect. 1 and 2 | 2972 | +/// the ordinary case and the one a `Display*` pair is *for*: the spec has the |
| 2916 | -/// (centimetres, inches) are physical sizes a picture this engine shows cannot | 2973 | +/// pair in pixels, so `1920`x`1080` beside a `1440`x`1080` coded track is |
| 2917 | -/// mean anything by, and read as square rather than guessed. Every unstated or | 2974 | +/// exactly the 4/3 stretch, stated the way this door below reads it. An absent |
| 2918 | -/// unusable shape is [`PixelAspect::SQUARE`], which is also the answer the spec's | 2975 | +/// pair is the file saying nothing, which is also square; a pair that *is* |
| 2919 | -/// own default gives: DisplayWidth and DisplayHeight "SHOULD be set to the | 2976 | +/// present and equal to the coded one is square by arithmetic, not by |
| 2920 | -/// PixelWidth and PixelHeight" when they mean the same thing, so a file that | 2977 | +/// assumption. |
| 2921 | -/// means square most often says nothing at all. | 2978 | +/// |
| 2979 | +/// 1 and 2 (centimetres, inches) are physical sizes a picture this engine | ||
| 2980 | +/// shows on a screen cannot mean anything by, and read as square rather than | ||
| 2981 | +/// guessed. Every unstated or unusable shape is [`PixelAspect::SQUARE`], which | ||
| 2982 | +/// is also the answer the spec's own default gives: DisplayWidth and | ||
| 2983 | +/// DisplayHeight "SHOULD be set to the PixelWidth and PixelHeight" when they | ||
| 2984 | +/// mean the same thing, so a file that means square most often says nothing | ||
| 2985 | +/// at all. | ||
| 2922 | fn pixel_aspect_of( | 2986 | fn pixel_aspect_of( |
| 2923 | display_width: u64, | 2987 | display_width: u64, |
| 2924 | display_height: u64, | 2988 | display_height: u64, |
| @@ -2931,22 +2995,30 @@ fn pixel_aspect_of( | |||
| 2931 | if display_width == 0 || display_height == 0 || coded_width == 0 || coded_height == 0 { | 2995 | if display_width == 0 || display_height == 0 || coded_width == 0 || coded_height == 0 { |
| 2932 | return PixelAspect::SQUARE; | 2996 | return PixelAspect::SQUARE; |
| 2933 | } | 2997 | } |
| 2998 | + // A stated pair, whichever unit spells it, is a claim about a real | ||
| 2999 | + // picture and is judged by [`PixelAspect::stated`] like every other | ||
| 3000 | + // header read: a ratio that reduces to nothing usable is square, and the | ||
| 3001 | + // caller learns that by being handed it rather than by a zero-width | ||
| 3002 | + // raster later on. | ||
| 3003 | + let stated = |h: u64, v: u64| { | ||
| 3004 | + PixelAspect::stated(h.min(u64::from(u32::MAX)) as u32, v.min(u64::from(u32::MAX)) as u32) | ||
| 3005 | + .unwrap_or(PixelAspect::SQUARE) | ||
| 3006 | + }; | ||
| 2934 | match display_unit { | 3007 | match display_unit { |
| 2935 | // 4: the pair *is* the sample aspect ratio. | 3008 | // 4: the pair *is* the sample aspect ratio. |
| 2936 | - 4 => PixelAspect { | 3009 | + 4 => stated(display_width, display_height), |
| 2937 | - h: display_width.min(u32::MAX as u64) as u32, | 3010 | + // 3: the pair is a display *aspect ratio*, and 0: the pair is a |
| 2938 | - v: display_height.min(u32::MAX as u64) as u32, | 3011 | + // display *pixel size*. Both reduce to the same SAR -- the ratio |
| 2939 | - }, | 3012 | + // against the coded one -- and that they do is the point: 16/9 over |
| 2940 | - // 3: the pair is the display aspect ratio -- display_w / display_h -- | 3013 | + // 1440x1080 and 1920x1080 over 1440x1080 are the same claim about the |
| 2941 | - // which the SAR is that against the coded one: sar = dar * h / w. | 3014 | + // same picture, and both land on 4/3. `saturating_mul` because the |
| 2942 | - // 16/9 over 1440x1080 lands on 4/3. | 3015 | + // pair is untrusted and a product past a u64 is a claim with no |
| 2943 | - 3 => PixelAspect { | 3016 | + // picture behind it. |
| 2944 | - h: ((display_width * u64::from(coded_height)) | 3017 | + 0 | 3 => stated( |
| 2945 | - .min(u32::MAX as u64 * u64::from(coded_width))) as u32, | 3018 | + display_width.saturating_mul(u64::from(coded_height)), |
| 2946 | - v: ((display_height * u64::from(coded_width)) | 3019 | + display_height.saturating_mul(u64::from(coded_width)), |
| 2947 | - .min(u32::MAX as u64 * u64::from(coded_height))) as u32, | 3020 | + ), |
| 2948 | - }, | 3021 | + // A physical unit this engine cannot interpret: square. |
| 2949 | - // Pixels (0) or a physical unit this engine cannot interpret: square. | ||
| 2950 | _ => PixelAspect::SQUARE, | 3022 | _ => PixelAspect::SQUARE, |
| 2951 | } | 3023 | } |
| 2952 | } | 3024 | } |
| @@ -4173,7 +4245,11 @@ fn pasp_of(path: &Path, track_id: u32) -> PixelAspect { | |||
| 4173 | ) else { | 4245 | ) else { |
| 4174 | return PixelAspect::SQUARE; | 4246 | return PixelAspect::SQUARE; |
| 4175 | }; | 4247 | }; |
| 4176 | - PixelAspect { h, v } | 4248 | + // Eight bytes a file chose, read into a ratio that sizes every raster |
| 4249 | + // downstream, so they are judged by [`PixelAspect::stated`] here: the | ||
| 4250 | + // aspect this engine *reports* is one it can draw, not merely one it | ||
| 4251 | + // survived. | ||
| 4252 | + PixelAspect::stated(h, v).unwrap_or(PixelAspect::SQUARE) | ||
| 4177 | } | 4253 | } |
| 4178 | 4254 | ||
| 4179 | /// The `clli` and `mdcv` boxes of `track_id`'s sample entry: what an mp4 says | 4255 | /// The `clli` and `mdcv` boxes of `track_id`'s sample entry: what an mp4 says |
| @@ -5613,6 +5689,50 @@ mod tests { | |||
| 5613 | assert_eq!(first_frame_sample(entries, Some(0)), 1, "zero is no trim"); | 5689 | assert_eq!(first_frame_sample(entries, Some(0)), 1, "zero is no trim"); |
| 5614 | } | 5690 | } |
| 5615 | 5691 | ||
| 5692 | + /// Four bytes of an mp4 `pasp` box are four bytes of untrusted input, and | ||
| 5693 | + /// every one of them reaches the width an allocation is sized from. A | ||
| 5694 | + /// header that claims `h == 0`, or one whose ratio widens the coded width | ||
| 5695 | + /// past the whole `u32` range, must read as the picture it really is -- | ||
| 5696 | + /// square, drawn at the coded size -- rather than as a zero-column or a | ||
| 5697 | + /// wrapped raster the decode worker tries to allocate. | ||
| 5698 | + #[test] | ||
| 5699 | + fn an_untrusted_pixel_aspect_cannot_size_a_zero_or_wrapped_raster() { | ||
| 5700 | + // Collected, not asserted one at a time: a witness that stops at the | ||
| 5701 | + // first degenerate hides whether the other classes are caught too. | ||
| 5702 | + let wrong: Vec<String> = [ | ||
| 5703 | + PixelAspect { h: 0, v: 1 }, | ||
| 5704 | + PixelAspect { h: 1, v: 0 }, | ||
| 5705 | + PixelAspect { h: 0, v: 0 }, | ||
| 5706 | + // 1920 * 0x00FF_FFFF is 32_212_252_800: it wraps a u32 to | ||
| 5707 | + // 2_147_483_648, which is a plausible-looking "width" and an | ||
| 5708 | + // impossible one to allocate. | ||
| 5709 | + PixelAspect { h: 0x00FF_FFFF, v: 1 }, | ||
| 5710 | + PixelAspect { h: 0xFFFF_FFFF, v: 0xFFFF_FFFF - 1 }, | ||
| 5711 | + // A ratio that is not a raster anybody has: 1:0x00FF_FFFF. | ||
| 5712 | + PixelAspect { h: 1, v: 0x00FF_FFFF }, | ||
| 5713 | + ] | ||
| 5714 | + .into_iter() | ||
| 5715 | + .filter_map(|hostile| { | ||
| 5716 | + let factor = hostile.width_factor(1920); | ||
| 5717 | + (factor != 1920).then(|| { | ||
| 5718 | + format!("{hostile:?} -> {factor} (a zero column or an oversized raster)") | ||
| 5719 | + }) | ||
| 5720 | + }) | ||
| 5721 | + .collect(); | ||
| 5722 | + assert!( | ||
| 5723 | + wrong.is_empty(), | ||
| 5724 | + "a degenerate pixel aspect must read as square:\n{}", | ||
| 5725 | + wrong.join("\n") | ||
| 5726 | + ); | ||
| 5727 | + // The sane ratios still stretch, bit for bit as they always did: the | ||
| 5728 | + // hardening refuses the untrusted, it does not round the ordinary. | ||
| 5729 | + assert_eq!(PixelAspect { h: 4, v: 3 }.width_factor(1440), 1920); | ||
| 5730 | + assert_eq!(PixelAspect { h: 1, v: 1 }.width_factor(1920), 1920); | ||
| 5731 | + assert_eq!(PixelAspect::SQUARE.width_factor(1920), 1920); | ||
| 5732 | + // An unreduced spelling of the same 4:3 lands on the same picture. | ||
| 5733 | + assert_eq!(PixelAspect { h: 16, v: 12 }.width_factor(1440), 1920); | ||
| 5734 | + } | ||
| 5735 | + | ||
| 5616 | #[test] | 5736 | #[test] |
| 5617 | fn rejects_misparse() { | 5737 | fn rejects_misparse() { |
| 5618 | assert!(append_annex_b(&[0, 0, 0, 9, 0x65], 4, &mut Vec::new()).is_err()); | 5738 | assert!(append_annex_b(&[0, 0, 0, 9, 0x65], 4, &mut Vec::new()).is_err()); |
crates/engine/tests/untrusted_header.rsadded+163 -0
| @@ -0,0 +1,163 @@ | |||
| 1 | +//! What a container header claims is untrusted input, and the numbers it | ||
| 2 | +//! claims them with reach every raster this engine sizes. | ||
| 3 | +//! | ||
| 4 | +//! An mp4 `pasp` box is eight bytes: two 32-bit big-endian integers, the ratio | ||
| 5 | +//! one coded pixel is drawn at. Nothing in the file makes them sane. Four | ||
| 6 | +//! chosen bytes are enough to make the probe report a zero width, or a width | ||
| 7 | +//! that wrapped a `u32` on its way down from the multiply -- and both of those | ||
| 8 | +//! go straight into the `resize` that sizes the decode worker's stretched | ||
| 9 | +//! planes, before a single pixel is drawn. | ||
| 10 | +//! | ||
| 11 | +//! This test byte-patches a copy of the baseline fixture and opens it through | ||
| 12 | +//! the ordinary door: the patched files live in a temp directory and are made | ||
| 13 | +//! here, so no hostile bytes are committed to the repository. | ||
| 14 | +//! | ||
| 15 | +//! ```text | ||
| 16 | +//! cargo test -p engine --test untrusted_header -- --test-threads=1 | ||
| 17 | +//! ``` | ||
| 18 | + | ||
| 19 | +use std::path::{Path, PathBuf}; | ||
| 20 | + | ||
| 21 | +use engine::demux::Demuxer; | ||
| 22 | + | ||
| 23 | +fn asset(name: &str) -> PathBuf { | ||
| 24 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
| 25 | + .join("../../assets") | ||
| 26 | + .join(name) | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +/// A temp directory that cleans itself up, so a failing test does not leave a | ||
| 30 | +/// hostile mp4 lying around for the next one to trip over. | ||
| 31 | +struct Scratch(PathBuf); | ||
| 32 | + | ||
| 33 | +impl Scratch { | ||
| 34 | + fn new(tag: &str) -> Self { | ||
| 35 | + let dir = std::env::temp_dir().join(format!("edith-untrusted-header-{tag}")); | ||
| 36 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 37 | + std::fs::create_dir_all(&dir).expect("a temp directory for the patched fixture"); | ||
| 38 | + Self(dir) | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + fn path(&self, name: &str) -> PathBuf { | ||
| 42 | + self.0.join(name) | ||
| 43 | + } | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +impl Drop for Scratch { | ||
| 47 | + fn drop(&mut self) { | ||
| 48 | + let _ = std::fs::remove_dir_all(&self.0); | ||
| 49 | + } | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +/// A copy of `source` with the eight bytes of its `pasp` payload replaced. | ||
| 53 | +/// | ||
| 54 | +/// Returns the byte offset the payload was written at, so the test can name | ||
| 55 | +/// the exact bytes it changed rather than asserting about a file whose | ||
| 56 | +/// provenance is a fixture somebody might regenerate. | ||
| 57 | +fn patch_pasp(source: &Path, into: &Path, h: u32, v: u32) -> usize { | ||
| 58 | + let bytes = std::fs::read(source).expect("read the source fixture"); | ||
| 59 | + let at = bytes | ||
| 60 | + .windows(4) | ||
| 61 | + .position(|window| window == b"pasp") | ||
| 62 | + .expect("the fixture carries a pasp box") | ||
| 63 | + + 4; // the type is four bytes; the payload follows it | ||
| 64 | + let mut patched = bytes.clone(); | ||
| 65 | + patched[at..at + 4].copy_from_slice(&h.to_be_bytes()); | ||
| 66 | + patched[at + 4..at + 8].copy_from_slice(&v.to_be_bytes()); | ||
| 67 | + std::fs::write(into, patched).expect("write the patched fixture"); | ||
| 68 | + at | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +/// The claims an mp4 can make about its pixels that are not a picture: a zero | ||
| 72 | +/// half, which divides to nothing, and a huge half, whose multiply wraps a | ||
| 73 | +/// `u32` on the way down. | ||
| 74 | +#[test] | ||
| 75 | +fn a_pasp_box_cannot_declare_a_zero_or_wrapped_display_width() { | ||
| 76 | + // Read the untouched fixture first: the patches below are asserted | ||
| 77 | + // against *its* numbers, so a regenerated fixture that stops carrying a | ||
| 78 | + // `pasp` box fails here loudly instead of silently passing. | ||
| 79 | + let (baseline, _) = Demuxer::open(&asset("test_baseline.mp4")).expect("open the baseline"); | ||
| 80 | + let coded = (baseline.coded_width, baseline.coded_height); | ||
| 81 | + assert_eq!( | ||
| 82 | + (baseline.width, baseline.height), | ||
| 83 | + coded, | ||
| 84 | + "the baseline fixture is square, so its display pair is its coded pair -- which is \ | ||
| 85 | + what the patched files are asserted against" | ||
| 86 | + ); | ||
| 87 | + assert!( | ||
| 88 | + baseline.pixel_aspect.is_square(), | ||
| 89 | + "the baseline fixture states 1:1, which is square" | ||
| 90 | + ); | ||
| 91 | + | ||
| 92 | + let scratch = Scratch::new("pasp"); | ||
| 93 | + for (tag, h, v) in [ | ||
| 94 | + ("zero_h", 0u32, 1u32), | ||
| 95 | + ("zero_v", 1, 0), | ||
| 96 | + ("both_zero", 0, 0), | ||
| 97 | + // 1920 * 0x00FF_FFFF = 32_212_252_800, which narrowed to a u32 is | ||
| 98 | + // 2_147_481_728: a width that looks plausible in a log and cannot be | ||
| 99 | + // allocated on any machine this runs on. | ||
| 100 | + ("wrap_h", 0x00FF_FFFF, 1), | ||
| 101 | + // 1:0x00FF_FFFF is the other end: a two-column strip of a picture. | ||
| 102 | + ("wrap_v", 1, 0x00FF_FFFF), | ||
| 103 | + ] { | ||
| 104 | + let name = format!("pasp_{tag}.mp4"); | ||
| 105 | + let into = scratch.path(&name); | ||
| 106 | + let at = patch_pasp(&asset("test_baseline.mp4"), &into, h, v); | ||
| 107 | + assert_eq!( | ||
| 108 | + std::fs::read(&into).unwrap()[at..at + 8], | ||
| 109 | + [h.to_be_bytes(), v.to_be_bytes()].concat(), | ||
| 110 | + "{tag}: the patch landed where it was aimed" | ||
| 111 | + ); | ||
| 112 | + | ||
| 113 | + // The ordinary door: open the file the way a library open does. | ||
| 114 | + let (meta, _) = Demuxer::open(&into) | ||
| 115 | + .unwrap_or_else(|why| panic!("{tag} (pasp {h}:{v} at byte {at}): open failed: {why}")); | ||
| 116 | + | ||
| 117 | + // A header that claims an unusable ratio is read as the picture it | ||
| 118 | + // really is: the coded one, square, drawn one to one. | ||
| 119 | + assert_eq!( | ||
| 120 | + (meta.width, meta.height), | ||
| 121 | + (baseline.width, baseline.height), | ||
| 122 | + "{tag} (pasp {h}:{v}): an unusable ratio must read as square, not as a \ | ||
| 123 | + zero-column or wrapped raster" | ||
| 124 | + ); | ||
| 125 | + assert!( | ||
| 126 | + meta.pixel_aspect.is_square(), | ||
| 127 | + "{tag} (pasp {h}:{v}): the reported aspect must be one this engine can draw" | ||
| 128 | + ); | ||
| 129 | + assert!( | ||
| 130 | + meta.width > 0 && meta.height > 0, | ||
| 131 | + "{tag} (pasp {h}:{v}): a zero dimension here is what sizes an allocation to nothing" | ||
| 132 | + ); | ||
| 133 | + } | ||
| 134 | +} | ||
| 135 | + | ||
| 136 | +/// The half that matters is not the probe: it is the worker that sizes its | ||
| 137 | +/// stretched planes from the same number. A decode that used to abort on a | ||
| 138 | +/// capacity overflow here now returns the coded picture. | ||
| 139 | +#[test] | ||
| 140 | +fn a_hostile_pasp_decodes_the_coded_picture_instead_of_aborting() { | ||
| 141 | + let scratch = Scratch::new("decode"); | ||
| 142 | + let into = scratch.path("pasp_wrap.mp4"); | ||
| 143 | + patch_pasp(&asset("test_baseline.mp4"), &into, 0x00FF_FFFF, 1); | ||
| 144 | + | ||
| 145 | + let (baseline, _) = Demuxer::open(&asset("test_baseline.mp4")).expect("open the baseline"); | ||
| 146 | + let coded = (baseline.width, baseline.height); | ||
| 147 | + let (meta, _) = Demuxer::open(&into).expect("a hostile header is still an openable file"); | ||
| 148 | + let (_, frames, _) = | ||
| 149 | + engine::DecodeSession::open_at(&into, 0).expect("a hostile header does not stop the decoder"); | ||
| 150 | + let frame = frames | ||
| 151 | + .recv() | ||
| 152 | + .expect("the first frame arrives rather than the worker aborting on its allocation"); | ||
| 153 | + assert_eq!( | ||
| 154 | + frame.width, | ||
| 155 | + meta.width, | ||
| 156 | + "the decoded frame is drawn at the width the probe reported" | ||
| 157 | + ); | ||
| 158 | + assert_eq!( | ||
| 159 | + (frame.width, frame.height), | ||
| 160 | + coded, | ||
| 161 | + "the coded picture, drawn square: a wrapped claim resizes nothing" | ||
| 162 | + ); | ||
| 163 | +} | ||
| \ No newline at end of file | \ No newline at end of file | ||