Matroska DisplayUnit 0 states the stretch, not a crop
Browse files at this commitd4dc736e15595bc70a1bc7dce54bfee3740ad15eParent: 33ee2ba- fix
- Matroska DisplayUnit 0 states the stretch, not a crop
1 changed file +218 -0
crates/engine/tests/display_unit.rsadded+218 -0
| @@ -0,0 +1,218 @@ | ||
| 1 | +//! Matroska `DisplayUnit` 0: the `Display*` pair is in *pixels*, so a pair | |
| 2 | +//! beside the coded size is the stretch, not a crop. | |
| 3 | +//! | |
| 4 | +//! The Matroska spec is explicit that unit 0 means the `DisplayWidth` and | |
| 5 | +//! `DisplayHeight` pair *is* a pixel size. So `DisplayWidth=1920`, | |
| 6 | +//! `DisplayHeight=1080` beside a `PixelWidth=1440`, `PixelHeight=1080` track | |
| 7 | +//! is exactly the 4/3 anamorphic stretch -- the same picture | |
| 8 | +//! `test_anamorphic.mkv` states the other two ways round, in SAR (4/3) and in | |
| 9 | +//! DAR (16/9). | |
| 10 | +//! | |
| 11 | +//! No fixture stated it that way until this one: `scripts/gen_fixtures.sh` | |
| 12 | +//! produces unit 3 (ffmpeg spells the aspect as a DAR), so the unit-0 arm of | |
| 13 | +//! the reader had never been seen by a test, and it read as square -- the | |
| 14 | +//! same answer as a file that says nothing at all. The picture was shown at | |
| 15 | +//! 1440x1080 instead of 1920x1080, and nothing said so. | |
| 16 | +//! | |
| 17 | +//! The fixture is patched here from `test_anamorphic.mkv` rather than | |
| 18 | +//! committed as a binary: the `Video` element keeps its exact byte length, so | |
| 19 | +//! every absolute offset in the file -- the SeekHead and every CueCluster- | |
| 20 | +//! Position -- stays correct and the copy seeks exactly as the original does. | |
| 21 | +//! | |
| 22 | +//! ```text | |
| 23 | +//! cargo test -p engine --test display_unit -- --test-threads=1 | |
| 24 | +//! ``` | |
| 25 | + | |
| 26 | +use std::path::{Path, PathBuf}; | |
| 27 | + | |
| 28 | +use engine::demux::{Demuxer, PixelAspect}; | |
| 29 | + | |
| 30 | +fn asset(name: &str) -> PathBuf { | |
| 31 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 32 | + .join("../../assets") | |
| 33 | + .join(name) | |
| 34 | +} | |
| 35 | + | |
| 36 | +struct Scratch(PathBuf); | |
| 37 | + | |
| 38 | +impl Scratch { | |
| 39 | + fn new(tag: &str) -> Self { | |
| 40 | + let dir = std::env::temp_dir().join(format!("edith-display-unit-{tag}")); | |
| 41 | + let _ = std::fs::remove_dir_all(&dir); | |
| 42 | + std::fs::create_dir_all(&dir).expect("a temp directory for the patched fixture"); | |
| 43 | + Self(dir) | |
| 44 | + } | |
| 45 | + | |
| 46 | + fn path(&self, name: &str) -> PathBuf { | |
| 47 | + self.0.join(name) | |
| 48 | + } | |
| 49 | +} | |
| 50 | + | |
| 51 | +impl Drop for Scratch { | |
| 52 | + fn drop(&mut self) { | |
| 53 | + let _ = std::fs::remove_dir_all(&self.0); | |
| 54 | + } | |
| 55 | +} | |
| 56 | + | |
| 57 | +/// The bytes of the `Video` element of `test_anamorphic.mkv`, as ffmpeg's | |
| 58 | +/// Matroska muxer wrote them: a 1440x1080 track with its aspect stated as a | |
| 59 | +/// 16/9 display aspect ratio. | |
| 60 | +const VIDEO_UNIT_3: &str = concat!( | |
| 61 | + "e09e", // Video, 30 bytes | |
| 62 | + "b08205a0", // PixelWidth 1440 | |
| 63 | + "ba820438", // PixelHeight 1080 | |
| 64 | + "9a8102", // FlagInterlaced | |
| 65 | + "54b08110", // DisplayWidth 16 | |
| 66 | + "54ba8109", // DisplayHeight 9 | |
| 67 | + "54b28103", // DisplayUnit 3 -- a display aspect ratio | |
| 68 | + "55b084", "55b98101" // Colour { MatrixCoefficients 1 } | |
| 69 | +); | |
| 70 | + | |
| 71 | +/// The same 30 bytes with the aspect stated the third way Matroska allows: | |
| 72 | +/// unit 0, where the `Display*` pair is in pixels and so *is* the drawn | |
| 73 | +/// size. 1920x1080 over a 1440x1080 coded track is the same 4/3 stretch. | |
| 74 | +/// | |
| 75 | +/// Byte-for-byte the same length as [`VIDEO_UNIT_3`], which is what keeps | |
| 76 | +/// the rest of the file's offsets honest: the `Display*` values grow from | |
| 77 | +/// one-byte to two-byte payloads (+3 bytes with the wider unit encoding), | |
| 78 | +/// `FlagInterlaced` is dropped (-3), and `MatrixCoefficients` is restated | |
| 79 | +/// over two bytes instead of one (+1) inside the `Colour` element. | |
| 80 | +const VIDEO_UNIT_0: &str = concat!( | |
| 81 | + "e09e", // Video, 30 bytes -- unchanged | |
| 82 | + "b08205a0", // PixelWidth 1440, unchanged: the samples are still these | |
| 83 | + "ba820438", // PixelHeight 1080, unchanged | |
| 84 | + "54b0820780", // DisplayWidth 1920 -- in *pixels* | |
| 85 | + "54ba820438", // DisplayHeight 1080 -- in *pixels* | |
| 86 | + "54b28100", // DisplayUnit 0 -- pixels | |
| 87 | + "55b085", "55b9820001" // Colour { MatrixCoefficients 1 } | |
| 88 | +); | |
| 89 | +fn with_video_element(source: &Path, into: &Path, element: &str) { | |
| 90 | + let from = hex(VIDEO_UNIT_3); | |
| 91 | + let to = hex(element); | |
| 92 | + assert_eq!( | |
| 93 | + from.len(), | |
| 94 | + to.len(), | |
| 95 | + "a length-preserving patch: {element} is {} bytes, {} are needed", | |
| 96 | + to.len(), | |
| 97 | + from.len() | |
| 98 | + ); | |
| 99 | + let bytes = std::fs::read(source).expect("read the source fixture"); | |
| 100 | + let at = bytes | |
| 101 | + .windows(from.len()) | |
| 102 | + .position(|window| window == from) | |
| 103 | + .unwrap_or_else(|| panic!("the fixture carries the expected {VIDEO_UNIT_3} element")); | |
| 104 | + let mut patched = bytes.clone(); | |
| 105 | + patched[at..at + from.len()].copy_from_slice(&to); | |
| 106 | + std::fs::write(into, patched).expect("write the patched fixture"); | |
| 107 | +} | |
| 108 | + | |
| 109 | +fn hex(text: &str) -> Vec<u8> { | |
| 110 | + (0..text.len() / 2) | |
| 111 | + .map(|i| u8::from_str_radix(&text[i * 2..i * 2 + 2], 16).expect("hex in the fixture")) | |
| 112 | + .collect() | |
| 113 | +} | |
| 114 | + | |
| 115 | +/// The reduced form of a ratio, so a ratio stated unreduced (the DAR arm | |
| 116 | +/// multiplies through by the coded size) compares equal to its simplest | |
| 117 | +/// spelling -- the same comparison the anamorphic suite makes. | |
| 118 | +fn reduced(aspect: PixelAspect) -> (u32, u32) { | |
| 119 | + let gcd = |mut a: u32, mut b: u32| { | |
| 120 | + while b != 0 { | |
| 121 | + (a, b) = (b, a % b); | |
| 122 | + } | |
| 123 | + a.max(1) | |
| 124 | + }; | |
| 125 | + let g = gcd(aspect.h, aspect.v); | |
| 126 | + (aspect.h / g, aspect.v / g) | |
| 127 | +} | |
| 128 | + | |
| 129 | +/// The three spellings of one anamorphic picture must name one picture. | |
| 130 | +#[test] | |
| 131 | +fn a_display_unit_of_zero_states_the_stretch_in_pixels() { | |
| 132 | + let scratch = Scratch::new("pixels"); | |
| 133 | + let unit0 = scratch.path("display_unit0.mkv"); | |
| 134 | + with_video_element(&asset("test_anamorphic.mkv"), &unit0, VIDEO_UNIT_0); | |
| 135 | + | |
| 136 | + // The patch landed, and it landed on a file the demuxer opens. | |
| 137 | + let patched = std::fs::read(&unit0).expect("read back the patched fixture"); | |
| 138 | + let original = std::fs::read(asset("test_anamorphic.mkv")).expect("read the original"); | |
| 139 | + assert_eq!( | |
| 140 | + patched.len(), | |
| 141 | + original.len(), | |
| 142 | + "the patch preserves the file length, so no offset in it moved" | |
| 143 | + ); | |
| 144 | + let wanted = hex(VIDEO_UNIT_0); | |
| 145 | + assert_eq!( | |
| 146 | + patched | |
| 147 | + .windows(wanted.len()) | |
| 148 | + .position(|window| window == wanted), | |
| 149 | + Some(369), | |
| 150 | + "the DisplayUnit-0 element is where it was aimed" | |
| 151 | + ); | |
| 152 | + | |
| 153 | + let (meta, _) = Demuxer::open(&unit0).expect("open the DisplayUnit-0 fixture"); | |
| 154 | + | |
| 155 | + // The samples are stored at the coded size, exactly as before the patch. | |
| 156 | + assert_eq!( | |
| 157 | + (meta.coded_width, meta.coded_height), | |
| 158 | + (1440, 1080), | |
| 159 | + "unit 0 changes what the file *says* about the picture, not how it is stored" | |
| 160 | + ); | |
| 161 | + | |
| 162 | + // Unit 0 means the pair IS a pixel size, so 1920x1080 beside a 1440x1080 | |
| 163 | + // coded track is the 4/3 stretch -- and this is the whole defect: read as | |
| 164 | + // square, the picture was drawn at 1440x1080, a quarter-width picture of | |
| 165 | + // the same frame, with nothing to say so. | |
| 166 | + assert!( | |
| 167 | + !meta.pixel_aspect.is_square(), | |
| 168 | + "DisplayUnit 0 with a Display pair beside the coded one is a stretch, \ | |
| 169 | + not a crop: got square ({:?})", | |
| 170 | + meta.pixel_aspect | |
| 171 | + ); | |
| 172 | + assert_eq!( | |
| 173 | + (meta.width, meta.height), | |
| 174 | + (1920, 1080), | |
| 175 | + "the Display pair is the drawn size: 1920x1080 over a 1440x1080 coded track" | |
| 176 | + ); | |
| 177 | + | |
| 178 | + // ...and it is the *same* ratio the other two spellings name, so all three | |
| 179 | + // fixtures of one source agree on one drawn picture. Compared reduced, | |
| 180 | + // because a ratio is named by the picture and not by how it was spelled: | |
| 181 | + // the DAR arm multiplies through by the coded size and says 2073600 / | |
| 182 | + // 1555200 where the SAR arm says 4 / 3. | |
| 183 | + assert_eq!( | |
| 184 | + reduced(meta.pixel_aspect), | |
| 185 | + (4, 3), | |
| 186 | + "1920x1080 over a 1440x1080 coded track is the same 4/3 the SAR and \ | |
| 187 | + DAR spellings name" | |
| 188 | + ); | |
| 189 | + for sibling in ["test_anamorphic.mp4", "test_anamorphic.mkv"] { | |
| 190 | + let (other, _) = Demuxer::open(&asset(sibling)).expect("open the sibling fixture"); | |
| 191 | + assert_eq!( | |
| 192 | + (other.width, other.height), | |
| 193 | + (meta.width, meta.height), | |
| 194 | + "{sibling} states the same picture the DisplayUnit spelling states" | |
| 195 | + ); | |
| 196 | + } | |
| 197 | +} | |
| 198 | + | |
| 199 | +/// Units 1 and 2 are physical sizes -- centimetres, inches -- which a picture | |
| 200 | +/// this engine shows on a screen cannot mean anything by. They stay square, | |
| 201 | +/// and saying so is the deliberate answer rather than an accident of the | |
| 202 | +/// match falling through. | |
| 203 | +#[test] | |
| 204 | +fn a_physical_display_unit_is_read_as_square() { | |
| 205 | + let scratch = Scratch::new("physical"); | |
| 206 | + for (unit, tag) in [(1u8, "cm"), (2u8, "in")] { | |
| 207 | + let element = VIDEO_UNIT_0.replacen("54b28100", &format!("54b2810{unit}"), 1); | |
| 208 | + let into = scratch.path(&format!("display_unit{unit}.mkv")); | |
| 209 | + with_video_element(&asset("test_anamorphic.mkv"), &into, &element); | |
| 210 | + let (meta, _) = Demuxer::open(&into).expect("open the physical-unit fixture"); | |
| 211 | + assert!( | |
| 212 | + meta.pixel_aspect.is_square(), | |
| 213 | + "DisplayUnit {unit} ({tag}) is a physical size this engine cannot interpret, \ | |
| 214 | + so the pixels stay square: got {:?}", | |
| 215 | + meta.pixel_aspect | |
| 216 | + ); | |
| 217 | + } | |
| 218 | +} | |
| \ No newline at end of file |