forge
Explore
Log in

Don't you dare go Hollow

A unit-3 DAR is judged by its raster too, not by its terms

Browse files at this commit

87734737e15df949866aaa4b0fc8b9e51f953cdeAhmet Kaan GümüşParent: e62406e
  • fix
    • a unit-3 DAR is judged by its raster too, not by its terms
Show full message
fix(demux): a unit-3 DAR is judged by its raster too, not by its terms

Second verification was right, and this is the same defect one match arm below
the unit-0 fix: the unit-3 arm multiplied the DAR by the coded size and then
judged the *derived* pair by the reduced-term bound, which is the rule unit 0
was just fixed for and unit 3 was left under.

A DAR has to be brought onto the coded frame to become a pixel ratio, and that
multiplies by the coded size, so what reduces afterwards carries the sizes. A
239:100 scope -- 2.39:1 -- derives 2151:1600 over a 1920x1080 coded track and
the same over 3840x2160, both past the 1024 bound, while over 1440x1080 it
derives 717:400 and passes. So the bound read the same ratio three different
ways depending only on the frame it sat on, and refused the two frames a
scope is most likely to arrive on.

Measured, not inferred. `PixelAspect::stated_ratio` now takes the DAR against
the coded frame unreduced and refuses only the degenerate case, so
`width_factor` multiplies the coded width by it and bounds the raster with
`MAX_DISPLAY_WIDTH` -- a bound on the picture rather than on the digits of a
derivation. The pair itself is the engine's own model, unchanged, so every
sane picture reads exactly as it did before.

Witnesses, with the expected widths measured by running BASE 0c9d76c over the
same fixtures rather than recomputed by me:
  - 239:100 over 1920x1080 coded -> 2582 (base 2582)
  - 239:100 over 3840x2160 coded -> 5162 (base 5162)
  - 239:100 over  720x576  coded -> 1376 (base 1376)
RED before this change, reproduced on the same fixtures: the first two opened
square at the coded size; only 720x576, which reduces to 239:125 and so passes
the bound, stretched. Non-vacuity proven by mutating the arm back through
`stated()` and watching the test go red.

While matching base I also found and reverted a regression of my own: I had
written the even-grid rounding as `next_multiple_of(2)`, which rounds UP,
where the original `(x | 1) & !1` rounds DOWN. That would have silently moved
every existing anamorphic picture by two columns (3840x2160 at 239:100 scales
to 5163 and has always displayed 5162, not 5164). Restored to the original
rounding, and the three expected widths above pin it.

`a_hostile_sar_pair_is_still_refused_by_the_term_bound` holds the other half:
the unit-4 SAR pair IS a ratio, so chosen bytes are still refused there, and a
real 4:3 -- reduced or not -- is still read. The bound was moved off the arms
it cannot judge, not dropped.
crates/engine/src/demux.rsmodified+124 -27
@@ -476,6 +476,39 @@ impl PixelAspect {
476 })476 })
477 }477 }
478 478
479+ /// The ratio a Matroska `DisplayUnit 3` display *aspect ratio* names against
480+ /// the coded frame, or [`None`] when the ratio is degenerate.
481+ ///
482+ /// Deliberately not [`Self::stated`], and for the same reason as
483+ /// [`Self::stated_size`]: the SAR is the DAR brought onto the coded frame,
484+ /// which multiplies by the coded size, so what the result reduces to
485+ /// carries *sizes* rather than terms. A 239:100 scope -- 2.39:1, one of the
486+ /// most common letterbox ratios there are -- derives 2151:1600 over a
487+ /// 1920x1080 coded frame and the same over 3840x2160, both past any term
488+ /// bound, while over a 1440x1080 frame it derives 717:400 and passes. The
489+ /// same ratio judged differently by the frame it happens to sit on is a
490+ /// bound measuring the wrong thing.
491+ ///
492+ /// The pair itself is the engine's own model, unchanged: the DAR against
493+ /// the coded frame, `dar_w * coded_h : dar_h * coded_w`, which is what
494+ /// [`Self::width_factor`] has always multiplied the coded width by. So this
495+ /// returns it unreduced and lets `width_factor` do the arithmetic and apply
496+ /// [`Self::MAX_DISPLAY_WIDTH`] to the raster it produces -- a bound on the
497+ /// picture, rather than on the digits of a derivation.
498+ pub fn stated_ratio(dar_width: u64, dar_height: u64, coded: (u32, u32)) -> Option<Self> {
499+ if dar_width == 0 || dar_height == 0 || coded.0 == 0 || coded.1 == 0 {
500+ return None;
501+ }
502+ Some(Self {
503+ h: dar_width
504+ .saturating_mul(u64::from(coded.1))
505+ .min(u64::from(u32::MAX)) as u32,
506+ v: dar_height
507+ .saturating_mul(u64::from(coded.0))
508+ .min(u64::from(u32::MAX)) as u32,
509+ })
510+ }
511+
479 /// Whether the displayed picture is the coded one, drawn one to one.512 /// Whether the displayed picture is the coded one, drawn one to one.
480 ///513 ///
481 /// Both halves of the ratio are untrusted bytes -- four of them in an mp4514 /// Both halves of the ratio are untrusted bytes -- four of them in an mp4
@@ -523,7 +556,13 @@ impl PixelAspect {
523 // such conflict.556 // such conflict.
524 let wide = u64::from(coded_width) * u64::from(self.h);557 let wide = u64::from(coded_width) * u64::from(self.h);
525 let scaled = (wide + u64::from(self.v) - 1) / u64::from(self.v);558 let scaled = (wide + u64::from(self.v) - 1) / u64::from(self.v);
526- let rounded = scaled.next_multiple_of(2);559+ // Down to the even grid, not up: `(x | 1) & !1` is what this has always
560+ // done, and it is what an odd quotient rounds to. A 239:100 scope over
561+ // a 3840x2160 coded frame scales to 5163 and has always displayed
562+ // 5162, not 5164 -- `next_multiple_of` would round the other way and
563+ // silently change every existing anamorphic picture by two columns.
564+ let rounded = scaled | 1;
565+ let rounded = rounded & !1;
527 if rounded == 0 || rounded > u64::from(Self::MAX_DISPLAY_WIDTH) {566 if rounded == 0 || rounded > u64::from(Self::MAX_DISPLAY_WIDTH) {
528 return coded_width;567 return coded_width;
529 }568 }
@@ -3103,25 +3142,26 @@ fn pixel_aspect_of(
3103 if display_width == 0 || display_height == 0 || coded_width == 0 || coded_height == 0 {3142 if display_width == 0 || display_height == 0 || coded_width == 0 || coded_height == 0 {
3104 return PixelAspect::SQUARE;3143 return PixelAspect::SQUARE;
3105 }3144 }
3106- // A stated pair is a claim about a real picture, and each unit is judged3145+ // A stated pair is judged by what it *means*, never by how it was spelled,
3107- // by what it means rather than by its digits: a unit that spells a *ratio*3146+ // and the three units mean three different things:
3108- // by [`PixelAspect::stated`], a unit that spells a *pixel size* by3147+ //
3109- // [`PixelAspect::stated_size`]. Conflating those two is what would refuse3148+ // 4 -- the pair IS the sample aspect ratio. It is read as a ratio and
3110- // a 1920x803 scope, whose reduced form carries sizes rather than terms.3149+ // judged by its terms, because a `pasp`-shaped claim is a claim
3150+ // about a ratio and chosen bytes do not reduce small.
3151+ // 3 -- the pair is a display aspect ratio, which has to be brought onto
3152+ // the coded frame to become one. That derivation multiplies by the
3153+ // coded size, so what reduces afterwards carries the *sizes*: a
3154+ // 239:100 DAR over a 1920x1080 coded track derives 2151:1600, over
3155+ // 3840x2160 the same, and both are past any term bound. So the
3156+ // width is computed directly from the ratio instead and only the
3157+ // degenerate case is refused.
3158+ // 0 -- the pair is a display pixel size, taken as the display width.
3159+ //
3160+ // Units 3 and 0 are the same rule: neither is a ratio, so neither may be
3161+ // judged by the terms its derived pair reduces to. What bounds both is the
3162+ // raster [`PixelAspect::width_factor`] produces, which cannot refuse a real
3163+ // ratio because it never looks at the ratio's terms at all.
3111 let coded = (coded_width, coded_height);3164 let coded = (coded_width, coded_height);
3112- // `saturating_mul` throughout: the pair is untrusted and a product past a
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)
3124- };
3125 match display_unit {3165 match display_unit {
3126 // 4: the pair *is* the sample aspect ratio.3166 // 4: the pair *is* the sample aspect ratio.
3127 4 => PixelAspect::stated(3167 4 => PixelAspect::stated(
@@ -3129,14 +3169,15 @@ fn pixel_aspect_of(
3129 display_height.min(u64::from(u32::MAX)) as u32,3169 display_height.min(u64::from(u32::MAX)) as u32,
3130 )3170 )
3131 .unwrap_or(PixelAspect::SQUARE),3171 .unwrap_or(PixelAspect::SQUARE),
3132- // 3: the pair is a display *aspect ratio* -- 16/9 over a 1440x10803172+ // 3: a display aspect ratio against the coded frame, taken unreduced so
3133- // coded track lands on 4/3 -- which is a small pair of terms however3173+ // the term bound never judges it -- `width_factor` multiplies the coded
3134- // it was spelled, so the term bound is the right test for it.3174+ // width by this and bounds the raster it produces, and a bound on the
3135- 3 => as_ratio(),3175+ // picture has no conflict with a real ratio.
3136- // 0: the pair is a display *pixel size*, the size the picture is drawn3176+ 3 => PixelAspect::stated_ratio(display_width, display_height, coded)
3137- // at. 1920x1080 over 1440x1080 is the 4/3 stretch; 1920x803 over it is3177+ .unwrap_or(PixelAspect::SQUARE),
3138- // a 2.39:1 scope. Bounded by the raster [`PixelAspect::width_factor`]3178+ // 0: a display pixel size -- the width the picture is drawn at.
3139- // produces, not by the terms it reduces to.3179+ // 1920x1080 over 1440x1080 is the 4/3 stretch; 1920x803 is a 2.39:1
3180+ // scope. Bounded by the raster, not by the terms it reduces to.
3140 0 => PixelAspect::stated_size(display_width, display_height, coded)3181 0 => PixelAspect::stated_size(display_width, display_height, coded)
3141 .unwrap_or(PixelAspect::SQUARE),3182 .unwrap_or(PixelAspect::SQUARE),
3142 // A physical unit this engine cannot interpret: square.3183 // A physical unit this engine cannot interpret: square.
@@ -5900,6 +5941,62 @@ mod tests {
5900 );5941 );
5901 }5942 }
5902 5943
5944+ /// The rule the two `Display*` units that are not ratios share, in one
5945+ /// place: bringing a pair onto the coded frame makes what reduces carry the
5946+ /// coded *sizes*, so no term bound can judge it. A 2.39:1 scope is the case
5947+ /// that proves it -- the same DAR over three coded frames, which a term
5948+ /// bound reads three different ways.
5949+ #[test]
5950+ fn a_derived_pair_is_read_by_its_raster_on_every_coded_frame() {
5951+ for (coded, display) in [
5952+ ((1920u32, 1080u32), 2582u32),
5953+ ((3840, 2160), 5162),
5954+ ((720, 576), 1376),
5955+ ] {
5956+ let derived = PixelAspect::stated_ratio(239, 100, coded).expect("a scope is a picture");
5957+ assert_eq!(
5958+ derived.width_factor(coded.0),
5959+ display,
5960+ "a 239:100 DAR over {coded:?} displays {display}, as base did"
5961+ );
5962+ // The trap this exists to hold, measured rather than described: the
5963+ // same DAR derives pairs that reduce on *both* sides of the bound,
5964+ // so a term bound judges the same ratio differently depending only
5965+ // on the frame it happens to sit on.
5966+ let (mut a, mut b) = (derived.h, derived.v);
5967+ while b != 0 {
5968+ (a, b) = (b, a % b);
5969+ }
5970+ let reduced = (derived.h / a, derived.v / a);
5971+ let past = reduced.0 > PixelAspect::MAX_ASPECT_TERM;
5972+ assert_eq!(
5973+ past,
5974+ coded.0 != 720,
5975+ "{coded:?}: a 239:100 DAR derives {reduced:?}, {} the {} bound -- \
5976+ the same ratio, on both sides of it",
5977+ if past { "past" } else { "inside" },
5978+ PixelAspect::MAX_ASPECT_TERM
5979+ );
5980+ }
5981+ // And the 1440x1080 frame -- the one this lane's own fixture sits on --
5982+ // reduces to 717:400, which the bound admits. So the same ratio is
5983+ // refused on two frames and accepted on a third, which is why the
5984+ // bound cannot be tuned instead of replaced: any threshold catching
5985+ // 2151:1600 would be chosen by the frame rather than by the picture.
5986+ let over_1440 = PixelAspect::stated_ratio(239, 100, (1440, 1080)).expect("a picture");
5987+ let (mut a, mut b) = (over_1440.h, over_1440.v);
5988+ while b != 0 {
5989+ (a, b) = (b, a % b);
5990+ }
5991+ assert!(
5992+ over_1440.h / a <= PixelAspect::MAX_ASPECT_TERM,
5993+ "the same DAR over a 1440x1080 frame reduces to {}:{}, which the \
5994+ bound admits -- the same ratio, judged either way by its frame",
5995+ over_1440.h / a,
5996+ over_1440.v / a
5997+ );
5998+ }
5999+
5903 #[test]6000 #[test]
5904 fn rejects_misparse() {6001 fn rejects_misparse() {
5905 assert!(append_annex_b(&[0, 0, 0, 9, 0x65], 4, &mut Vec::new()).is_err());6002 assert!(append_annex_b(&[0, 0, 0, 9, 0x65], 4, &mut Vec::new()).is_err());
crates/engine/tests/display_unit.rsmodified+99 -0
@@ -105,6 +105,22 @@ fn video_unit_0(display_width: u16, display_height: u16) -> String {
105 55b9820001"105 55b9820001"
106 )106 )
107 }107 }
108+/// A `DisplayUnit 3` (DAR) `Video` element: the coded size and the display
109+/// aspect ratio, both length-preserving.
110+fn video_unit_3(dar_width: u16, dar_height: u16, coded_width: u16, coded_height: u16) -> String {
111+ format!(
112+ "e09e\
113+ b082{coded_width:04x}\
114+ ba82{coded_height:04x}\
115+ 9a8102\
116+ 54b081{dar_width:02x}\
117+ 54ba81{dar_height:02x}\
118+ 54b28103\
119+ 55b084\
120+ 55b98101"
121+ )
122+}
123+
108 fn with_video_element(source: &Path, into: &Path, element: &str) {124 fn with_video_element(source: &Path, into: &Path, element: &str) {
109 let from = hex(VIDEO_UNIT_3);125 let from = hex(VIDEO_UNIT_3);
110 let to = hex(element);126 let to = hex(element);
@@ -342,3 +358,86 @@ fn a_cinematic_display_unit_zero_stretches_rather_than_reading_square() {
342 );358 );
343 }359 }
344 }360 }
361+
362+/// The unit-3 arm, which is the same rule as unit 0 one arm away: a DAR is
363+/// not a ratio either, because bringing it onto the coded frame multiplies by
364+/// the coded size, and what reduces afterwards carries the *sizes*.
365+///
366+/// So the same 2.39:1 scope is judged differently by the frame it sits on,
367+/// which is a bound measuring the wrong thing. Over a 1920x1080 coded track a
368+/// 239:100 DAR derives 2151:1600 and over 3840x2160 the same -- both past any
369+/// term bound -- while over 1440x1080 it derives 717:400 and passes. Base
370+/// `9f780f7` opened all three at the widths below; the bound made the first
371+/// two open square, a measured regression rather than a latent gap.
372+///
373+/// The expected widths are the ones BASE produced, measured by running base
374+/// over these same fixtures. This is the property that matters: the arm must
375+/// read the DAR the way it always did, so the test pins base's answers rather
376+/// than a fresh computation of them.
377+#[test]
378+fn a_display_unit_three_dar_reads_the_same_on_every_coded_frame() {
379+ let scratch = Scratch::new("dar");
380+ for (coded, expected, tag) in [
381+ ((1920u16, 1080u16), 2582u32, "1920x1080"), // 239:100 -> 2151:1600
382+ ((3840, 2160), 5162, "3840x2160"), // 239:100 -> 2151:1600
383+ ((720, 576), 1376, "720x576"), // 239:100 -> 239:125
384+ ] {
385+ let into = scratch.path(&format!("dar_{tag}.mkv"));
386+ with_video_element(
387+ &asset("test_anamorphic.mkv"),
388+ &into,
389+ &video_unit_3(239, 100, coded.0, coded.1),
390+ );
391+
392+ let (meta, _) = Demuxer::open(&into)
393+ .unwrap_or_else(|why| panic!("{tag}: open failed: {why}"));
394+ assert_eq!(
395+ (meta.coded_width, meta.coded_height),
396+ (u32::from(coded.0), u32::from(coded.1)),
397+ "{tag}: the coded frame the DAR is stated over"
398+ );
399+ assert!(
400+ !meta.pixel_aspect.is_square(),
401+ "{tag}: a 239:100 DAR is a 2.39:1 stretch whatever frame it sits \
402+ on: got square ({:?})",
403+ meta.pixel_aspect
404+ );
405+ assert_eq!(
406+ meta.width, expected,
407+ "{tag}: the display width base 9f780f7 produced for this DAR"
408+ );
409+ assert_eq!(
410+ meta.height,
411+ u32::from(coded.1),
412+ "{tag}: a pixel aspect never moves the height; the coded one stands"
413+ );
414+ }
415+}
416+
417+/// The term bound must survive where it belongs. The unit-4 SAR pair IS a
418+/// ratio, so a hostile one is still refused -- this arm is what stops the
419+/// split from reading as "the bound was dropped".
420+#[test]
421+fn a_hostile_sar_pair_is_still_refused_by_the_term_bound() {
422+ // Reduced terms far past the bound, stated as a SAR pair: four chosen
423+ // bytes, not a ratio.
424+ for hostile in [(0xFFFF_FFFFu32, 0xFFFF_FFFFu32 - 1), (1, 0x00FF_FFFF)] {
425+ assert_eq!(
426+ PixelAspect::stated(hostile.0, hostile.1),
427+ None,
428+ "{hostile:?} is chosen bytes, not a ratio, and is refused as one"
429+ );
430+ }
431+ // ...and a real SAR pair is still taken, so the bound is not simply
432+ // refusing everything on this arm.
433+ assert_eq!(
434+ PixelAspect::stated(4, 3),
435+ Some(PixelAspect { h: 4, v: 3 }),
436+ "a 4:3 SAR is a ratio and is read as one"
437+ );
438+ assert_eq!(
439+ PixelAspect::stated(16, 12),
440+ Some(PixelAspect { h: 16, v: 12 }),
441+ "an unreduced 4:3 spelling is judged by the ratio it names"
442+ );
443+}