The anamorphic fixtures and the suite that pins them
Browse files at this commitdb45d3c154c52d75cb0ed79dfa8d51078936ba16Parent: 312b13e- test
- the anamorphic fixtures and the suite that pins them
2 changed files +389 -0
- crates/engine/tests/anamorphic.rs +358 -0
- scripts/gen_fixtures.sh +31 -0
crates/engine/tests/anamorphic.rsadded+358 -0
| @@ -0,0 +1,358 @@ | |||
| 1 | +//! The sample aspect ratio: a file whose pixels are not square is drawn | ||
| 2 | +//! wider than they are stored, and every rendition this engine makes -- | ||
| 3 | +//! the probe, the preview and the export -- is the drawn one. | ||
| 4 | +//! | ||
| 5 | +//! `test_anamorphic.mp4` and `test_anamorphic.mkv` (`scripts/gen_fixtures.sh`) | ||
| 6 | +//! are one 1440x1080 source with `setsar=4/3` asked of it: the classic | ||
| 7 | +//! anamorphic 16:9 frame on a 4:3 raster, the way a DVD rip says "widen me". | ||
| 8 | +//! The engine's answer is ffmpeg's own: the decode keeps the coded pixels, | ||
| 9 | +//! the container's ratio is read as metadata, and the stretch is applied | ||
| 10 | +//! exactly once, at the conversion to the square-pixel raster -- so the | ||
| 11 | +//! preview shows 1920x1080 and the corners of it are picture, not bars. | ||
| 12 | +//! | ||
| 13 | +//! The copy door is the other half of ffmpeg's behaviour: a copied file | ||
| 14 | +//! keeps the coded pixels and carries the ratio beside them (Matroska's | ||
| 15 | +//! `Display*` elements), and where the container cannot state it -- an mp4 | ||
| 16 | +//! has no `pasp` in this writer -- a copy refuses by name rather than | ||
| 17 | +//! writing a header that lies about its own shape. | ||
| 18 | +//! | ||
| 19 | +//! ```text | ||
| 20 | +//! cargo test -p engine --release --test anamorphic -- --test-threads=1 | ||
| 21 | +//! ``` | ||
| 22 | + | ||
| 23 | +use std::path::PathBuf; | ||
| 24 | +use std::sync::Once; | ||
| 25 | +use std::time::{Duration, Instant}; | ||
| 26 | + | ||
| 27 | +use engine::demux::Demuxer; | ||
| 28 | +use engine::export::{ExportSettings, Format}; | ||
| 29 | +use engine::scratch::Scratch; | ||
| 30 | +use engine::PlaybackSession; | ||
| 31 | + | ||
| 32 | +fn asset(name: &str) -> PathBuf { | ||
| 33 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
| 34 | + .join("../../assets") | ||
| 35 | + .join(name) | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +/// Which seat an export takes is decided by *process-wide* environment | ||
| 39 | +/// variables, so a test binary that shares the machine has to borrow them | ||
| 40 | +/// rather than assume: this pins both decode and encode to software, which is | ||
| 41 | +/// the seat every machine has, and holds the borrow for the whole test binary. | ||
| 42 | +static SEAT: std::sync::RwLock<()> = std::sync::RwLock::new(()); | ||
| 43 | + | ||
| 44 | +type Shared = std::sync::RwLockReadGuard<'static, ()>; | ||
| 45 | + | ||
| 46 | +#[must_use = "the seat pin holds only while its guard is alive"] | ||
| 47 | +fn pin_software() -> Shared { | ||
| 48 | + let borrowed = SEAT.read().unwrap_or_else(|poisoned| poisoned.into_inner()); | ||
| 49 | + static PIN: Once = Once::new(); | ||
| 50 | + PIN.call_once(|| unsafe { | ||
| 51 | + std::env::set_var("VE_SW", "1"); | ||
| 52 | + std::env::set_var("VE_SW_ENC", "1"); | ||
| 53 | + }); | ||
| 54 | + borrowed | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +fn wait(handle: &engine::ExportHandle, limit: Duration) -> engine::Result<()> { | ||
| 58 | + let started = Instant::now(); | ||
| 59 | + while !handle.is_finished() { | ||
| 60 | + assert!( | ||
| 61 | + started.elapsed() < limit, | ||
| 62 | + "export did not finish in {limit:?}" | ||
| 63 | + ); | ||
| 64 | + std::thread::sleep(Duration::from_millis(20)); | ||
| 65 | + } | ||
| 66 | + handle.result().expect("a finished export has an outcome") | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +/// The coded and displayed shapes every anamorphic test here agrees on: | ||
| 70 | +/// 1440x1080 stored, 4/3 drawn, 1920x1080 shown. | ||
| 71 | +const CODED: (u32, u32) = (1440, 1080); | ||
| 72 | +const DISPLAY: (u32, u32) = (1920, 1080); | ||
| 73 | + | ||
| 74 | +/// The reduced form of `h/v`, so a ratio stated unreduced (a DAR pair like | ||
| 75 | +/// ffmpeg's 17280/12960) compares equal to its simplest spelling. | ||
| 76 | +fn gcd(mut a: u32, mut b: u32) -> u32 { | ||
| 77 | + while b != 0 { | ||
| 78 | + (a, b) = (b, a % b); | ||
| 79 | + } | ||
| 80 | + a.max(1) | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +/// The header says what the samples are and what they are drawn at, and both | ||
| 84 | +/// doors say the same thing about the same picture. | ||
| 85 | +#[test] | ||
| 86 | +fn the_container_aspect_is_probed_off_both_doors() { | ||
| 87 | + for file in ["test_anamorphic.mp4", "test_anamorphic.mkv"] { | ||
| 88 | + let (meta, _) = Demuxer::open(&asset(file)).expect("open a fixture"); | ||
| 89 | + assert_eq!( | ||
| 90 | + (meta.coded_width, meta.coded_height), | ||
| 91 | + CODED, | ||
| 92 | + "{file}: the samples are stored at the coded size" | ||
| 93 | + ); | ||
| 94 | + assert_eq!( | ||
| 95 | + (meta.width, meta.height), | ||
| 96 | + DISPLAY, | ||
| 97 | + "{file}: 4/3 over 1440x1080 is drawn 1920x1080" | ||
| 98 | + ); | ||
| 99 | + // The mkv door reads ffmpeg's DAR spelling (16/9) and the mp4 door the | ||
| 100 | + // SAR itself (4/3); both name the same drawn picture, so the test | ||
| 101 | + // compares the reduced ratio, not the spelling. | ||
| 102 | + let (h, v) = (meta.pixel_aspect.h, meta.pixel_aspect.v); | ||
| 103 | + let g = gcd(h, v); | ||
| 104 | + assert_eq!( | ||
| 105 | + (h / g, v / g), | ||
| 106 | + (4, 3), | ||
| 107 | + "{file}: the ratio one coded pixel is drawn at" | ||
| 108 | + ); | ||
| 109 | + assert!(!meta.pixel_aspect.is_square(), "{file}: it is not square"); | ||
| 110 | + assert_eq!( | ||
| 111 | + meta.rotation, | ||
| 112 | + engine::Rotation::None, | ||
| 113 | + "{file}: no turn was asked of it" | ||
| 114 | + ); | ||
| 115 | + } | ||
| 116 | + // A square-pixel file states none of it, and both doors answer square: | ||
| 117 | + // the default that makes the whole feature invisible to the ordinary | ||
| 118 | + // library. | ||
| 119 | + for file in ["test_baseline.mp4", "test_rotation90.mp4"] { | ||
| 120 | + let (meta, _) = Demuxer::open(&asset(file)).expect("open a fixture"); | ||
| 121 | + assert!( | ||
| 122 | + meta.pixel_aspect.is_square(), | ||
| 123 | + "{file}: no container said an aspect, so the pixels are square" | ||
| 124 | + ); | ||
| 125 | + // Drawn one to one: the displayed pair is the coded pair, swapped | ||
| 126 | + // where the file's matrix turns it and never widened. | ||
| 127 | + let (dw, dh) = match meta.rotation.swaps_axes() { | ||
| 128 | + true => (meta.coded_height, meta.coded_width), | ||
| 129 | + false => (meta.coded_width, meta.coded_height), | ||
| 130 | + }; | ||
| 131 | + assert_eq!( | ||
| 132 | + (meta.width, meta.height), | ||
| 133 | + (dw, dh), | ||
| 134 | + "{file}: drawn one to one, swapped only by the turn" | ||
| 135 | + ); | ||
| 136 | + } | ||
| 137 | + // The rotation still swaps on an anamorphic file whose *matrix* asks for | ||
| 138 | + // a turn: `turn_and_stretch` is the composed order -- the aspect widens | ||
| 139 | + // the coded frame, then the quarter turn swaps the stretched pair -- the | ||
| 140 | + // same composition ffmpeg's own probe reports for such a file. | ||
| 141 | + let turned = asset(".anamorphic_turned.mp4"); | ||
| 142 | + if std::process::Command::new("ffmpeg") | ||
| 143 | + .args(["-y", "-v", "error", "-f", "lavfi", "-i"]) | ||
| 144 | + .arg("testsrc2=size=180x320:rate=30:duration=1") | ||
| 145 | + .args([ | ||
| 146 | + "-vf", | ||
| 147 | + "setsar=4/3", | ||
| 148 | + "-c:v", | ||
| 149 | + "libx264", | ||
| 150 | + "-profile:v", | ||
| 151 | + "baseline", | ||
| 152 | + "-pix_fmt", | ||
| 153 | + "yuv420p", | ||
| 154 | + ]) | ||
| 155 | + .arg(&turned) | ||
| 156 | + .status() | ||
| 157 | + .is_ok_and(|s| s.success()) | ||
| 158 | + { | ||
| 159 | + let (meta, _) = Demuxer::open(&turned).expect("open the turned fixture"); | ||
| 160 | + // 180x320 at SAR 4/3 draws 240x320; a 270-degree matrix turns it. | ||
| 161 | + assert_eq!( | ||
| 162 | + (meta.coded_width, meta.coded_height), | ||
| 163 | + (180, 320), | ||
| 164 | + "the turned fixture's samples" | ||
| 165 | + ); | ||
| 166 | + assert_eq!( | ||
| 167 | + (meta.width, meta.height), | ||
| 168 | + (240, 320), | ||
| 169 | + "the aspect widens the coded frame before the turn swaps it" | ||
| 170 | + ); | ||
| 171 | + let _ = std::fs::remove_file(&turned); | ||
| 172 | + } | ||
| 173 | +} | ||
| 174 | + | ||
| 175 | +/// The preview: the composed picture is the displayed shape, filled to the | ||
| 176 | +/// corners -- no letterbox, no squeeze, no coded-shaped picture drawn into a | ||
| 177 | +/// wider window. The corner gate is numeric: a picture that left bars would | ||
| 178 | +/// sample black. | ||
| 179 | +#[test] | ||
| 180 | +fn the_preview_shows_the_drawn_picture() { | ||
| 181 | + let _seat = pin_software(); | ||
| 182 | + for file in ["test_anamorphic.mp4", "test_anamorphic.mkv"] { | ||
| 183 | + let (_, frames, _) = | ||
| 184 | + engine::DecodeSession::open_at(&asset(file), 0).expect("open for a frame"); | ||
| 185 | + let frame = frames.recv().expect("a first frame"); | ||
| 186 | + assert_eq!( | ||
| 187 | + (frame.width, frame.height), | ||
| 188 | + DISPLAY, | ||
| 189 | + "{file}: the composed picture is the drawn shape" | ||
| 190 | + ); | ||
| 191 | + let (w, h) = (frame.width as usize, frame.height as usize); | ||
| 192 | + let at = |x: usize, y: usize| { | ||
| 193 | + let i = (y * w + x) * 4; | ||
| 194 | + let (b, g, r) = (frame.bgra[i], frame.bgra[i + 1], frame.bgra[i + 2]); | ||
| 195 | + (u32::from(r), u32::from(g), u32::from(b)) | ||
| 196 | + }; | ||
| 197 | + let m = w / 10; | ||
| 198 | + let corners = [ | ||
| 199 | + at(m, m), | ||
| 200 | + at(w - 1 - m, m), | ||
| 201 | + at(w - 1 - m, h - 1 - m), | ||
| 202 | + at(m, h - 1 - m), | ||
| 203 | + ]; | ||
| 204 | + assert!( | ||
| 205 | + corners.iter().all(|(r, g, b)| *r + *g + *b > 48), | ||
| 206 | + "{file}: the corners are picture, not black: {corners:?}" | ||
| 207 | + ); | ||
| 208 | + let dar = w as f64 / h as f64; | ||
| 209 | + assert!( | ||
| 210 | + (dar - 16.0 / 9.0).abs() < 0.01, | ||
| 211 | + "{file}: the measured display aspect is 16:9, measured {dar:.4}" | ||
| 212 | + ); | ||
| 213 | + } | ||
| 214 | +} | ||
| 215 | + | ||
| 216 | +/// The export: the drawn shape is what the file says, and the pixels are the | ||
| 217 | +/// ones the preview showed -- re-encoded, so the new file is square-pixel at | ||
| 218 | +/// the displayed size and states no aspect of its own. | ||
| 219 | +#[test] | ||
| 220 | +fn an_export_of_an_anamorphic_file_is_drawn_and_square() { | ||
| 221 | + let _seat = pin_software(); | ||
| 222 | + let source = asset("test_anamorphic.mkv"); | ||
| 223 | + let mut session = PlaybackSession::open(&source).expect("open the fixture"); | ||
| 224 | + session.pause(); | ||
| 225 | + let preview = loop { | ||
| 226 | + if let Some(frame) = session.try_frame() { | ||
| 227 | + break frame; | ||
| 228 | + } | ||
| 229 | + std::thread::sleep(Duration::from_millis(10)); | ||
| 230 | + }; | ||
| 231 | + let out = Scratch::file("ve_anamorphic_export", "mp4"); | ||
| 232 | + let handle = session.export_to_with(&out, &ExportSettings::default()); | ||
| 233 | + wait(&handle, Duration::from_secs(120)).expect("export"); | ||
| 234 | + | ||
| 235 | + let (meta, _) = Demuxer::open(&out).expect("reopen the export"); | ||
| 236 | + assert_eq!( | ||
| 237 | + (meta.coded_width, meta.coded_height), | ||
| 238 | + DISPLAY, | ||
| 239 | + "the re-encoded file is coded at the displayed shape" | ||
| 240 | + ); | ||
| 241 | + assert!( | ||
| 242 | + meta.pixel_aspect.is_square(), | ||
| 243 | + "the aspect was applied, not carried: the export states none" | ||
| 244 | + ); | ||
| 245 | + // Preview vs export, same clip: the drawn picture to the byte. | ||
| 246 | + let mut reopened = PlaybackSession::open(&out).expect("reopen the export"); | ||
| 247 | + let exported = loop { | ||
| 248 | + if let Some(frame) = reopened.try_frame() { | ||
| 249 | + break frame; | ||
| 250 | + } | ||
| 251 | + std::thread::sleep(Duration::from_millis(10)); | ||
| 252 | + }; | ||
| 253 | + assert_eq!( | ||
| 254 | + (exported.width, exported.height), | ||
| 255 | + DISPLAY, | ||
| 256 | + "the export plays at the drawn shape" | ||
| 257 | + ); | ||
| 258 | + // Encode is lossy, so the picture is measured, not diffed: a wrong shape | ||
| 259 | + // or a squeezed picture would move far more than a rounding code. | ||
| 260 | + let moved = preview | ||
| 261 | + .bgra | ||
| 262 | + .iter() | ||
| 263 | + .zip(&exported.bgra) | ||
| 264 | + .map(|(a, b)| u32::from(a.abs_diff(*b))) | ||
| 265 | + .sum::<u32>(); | ||
| 266 | + let mean = moved as f64 / preview.bgra.len() as f64; | ||
| 267 | + assert!( | ||
| 268 | + mean < 2.0, | ||
| 269 | + "the export is {mean:.2} mean codes from the preview -- not the same picture" | ||
| 270 | + ); | ||
| 271 | +} | ||
| 272 | + | ||
| 273 | +/// The copy door, Matroska side: an anamorphic source's Exact row copies the | ||
| 274 | +/// coded blocks and carries the aspect beside them -- read back as the same | ||
| 275 | +/// 4/3 off the same elements the probe reads. | ||
| 276 | +#[test] | ||
| 277 | +fn a_matroska_copy_keeps_the_pixel_aspect() { | ||
| 278 | + // The copy itself decodes nothing, but opening the HEVC source and | ||
| 279 | + // reopening the copy do -- there is no software HEVC decoder, so both | ||
| 280 | + // need the plugin. A machine without it skips, exactly as | ||
| 281 | + // `video_copy`'s decode-back test does. | ||
| 282 | + let Ok(session) = PlaybackSession::open(asset("test_anamorphic_hevc.mkv")) else { | ||
| 283 | + println!("no VA-API plugin: skipping the copy-keeps-aspect test"); | ||
| 284 | + return; | ||
| 285 | + }; | ||
| 286 | + let out = Scratch::file("ve_anamorphic_copy", "mkv"); | ||
| 287 | + let handle = session.export_to_with( | ||
| 288 | + &out, | ||
| 289 | + &ExportSettings { | ||
| 290 | + format: Format::Hevc, | ||
| 291 | + ..Default::default() | ||
| 292 | + }, | ||
| 293 | + ); | ||
| 294 | + while !handle.is_finished() { | ||
| 295 | + std::thread::sleep(Duration::from_millis(20)); | ||
| 296 | + } | ||
| 297 | + let encoders = handle.encoders().unwrap_or_default(); | ||
| 298 | + handle.result().expect("an outcome").expect("copy export"); | ||
| 299 | + assert!( | ||
| 300 | + encoders.contains("copy"), | ||
| 301 | + "an untouched anamorphic timeline copies: {encoders}" | ||
| 302 | + ); | ||
| 303 | + let (meta, _) = Demuxer::open(&out).expect("reopen the copy"); | ||
| 304 | + assert_eq!( | ||
| 305 | + (meta.coded_width, meta.coded_height), | ||
| 306 | + CODED, | ||
| 307 | + "the copied track declares the coded shape" | ||
| 308 | + ); | ||
| 309 | + let (h, v) = (meta.pixel_aspect.h, meta.pixel_aspect.v); | ||
| 310 | + let g = gcd(h, v); | ||
| 311 | + assert_eq!( | ||
| 312 | + (h / g, v / g), | ||
| 313 | + (4, 3), | ||
| 314 | + "the copied track states the aspect it was copied with" | ||
| 315 | + ); | ||
| 316 | + assert_eq!( | ||
| 317 | + (meta.width, meta.height), | ||
| 318 | + DISPLAY, | ||
| 319 | + "and so reads back as the drawn shape" | ||
| 320 | + ); | ||
| 321 | +} | ||
| 322 | + | ||
| 323 | +/// The copy door, mp4 side: the sample entry this writer produces has no | ||
| 324 | +/// `pasp` box, so an anamorphic source's Exact row refuses by name rather | ||
| 325 | +/// than writing a header that lies about its own shape -- and the export | ||
| 326 | +/// still re-encodes to the right shape when asked without Exact. | ||
| 327 | +#[test] | ||
| 328 | +fn an_mp4_copy_of_an_anamorphic_source_refuses_in_words() { | ||
| 329 | + let _seat = pin_software(); | ||
| 330 | + // The refusal is decided at the header read, but the session must open | ||
| 331 | + // the HEVC source first -- plugin on this machine or skip, as above. | ||
| 332 | + let Ok(session) = PlaybackSession::open(asset("test_anamorphic_hevc.mkv")) else { | ||
| 333 | + println!("no VA-API plugin: skipping the mp4 exact refusal test"); | ||
| 334 | + return; | ||
| 335 | + }; | ||
| 336 | + let out = Scratch::file("ve_anamorphic_exact_refused", "mp4"); | ||
| 337 | + let settings = ExportSettings { | ||
| 338 | + format: Format::Mp4, | ||
| 339 | + exact: true, | ||
| 340 | + ..Default::default() | ||
| 341 | + }; | ||
| 342 | + let handle = session.export_to_with(&out, &settings); | ||
| 343 | + wait(&handle, Duration::from_secs(300)) | ||
| 344 | + .expect_err("an mp4 cannot honour exact for an anamorphic source"); | ||
| 345 | + let error = handle | ||
| 346 | + .result() | ||
| 347 | + .expect("a finished export has an outcome") | ||
| 348 | + .expect_err("the export refused") | ||
| 349 | + .to_string(); | ||
| 350 | + assert!( | ||
| 351 | + error.contains("pixel aspect"), | ||
| 352 | + "the refusal names the aspect it would lose: {error}" | ||
| 353 | + ); | ||
| 354 | + assert!( | ||
| 355 | + !out.exists() || std::fs::read(&out).is_err(), | ||
| 356 | + "no lying header was written" | ||
| 357 | + ); | ||
| 358 | +} | ||
scripts/gen_fixtures.shmodified+31 -0
| @@ -769,5 +769,36 @@ for ext in mp4 mkv; do | |||
| 769 | done | 769 | done |
| 770 | rm -f assets/.rotation_tall_src.mp4 | 770 | rm -f assets/.rotation_tall_src.mp4 |
| 771 | 771 | ||
| 772 | +# The sample aspect ratio: `test_anamorphic.*` are one 1440x1080 source whose | ||
| 773 | +# pixels are drawn 4/3 wide -- the classic anamorphic 16:9 frame stored on a | ||
| 774 | +# 4:3 raster (`-vf setsar=4/3`), so the displayed picture is 1920x1080 and the | ||
| 775 | +# stored one never is. This is how a DVD rip and a Blu-ray remux say "widen | ||
| 776 | +# me": the pixels stay where the encoder put them and the container carries | ||
| 777 | +# the ratio. | ||
| 778 | +# | ||
| 779 | +# What each muxer actually writes, byte-verified by walking the output rather | ||
| 780 | +# than taken off a table: the Matroska muxer writes `DisplayWidth` 16, | ||
| 781 | +# `DisplayHeight` 9 and `DisplayUnit` 3 -- a *display aspect ratio* pair, not | ||
| 782 | +# a sample aspect ratio, and 4:3 SAR over 1440x1080 is exactly 16:9 DAR -- and | ||
| 783 | +# a square-pixel file gets no `Display*` element at all. The mp4 muxer writes | ||
| 784 | +# a `pasp` box (hSpacing 4, vSpacing 3) inside the `avc1` sample entry, and | ||
| 785 | +# 1/1 for a square one. `tests/anamorphic.rs` reads both spellings through the | ||
| 786 | +# engine and asserts the same displayed shape off either. | ||
| 787 | +ffmpeg -y -f lavfi -i "testsrc2=size=1440x1080:rate=30:duration=1" \ | ||
| 788 | + -vf "setsar=4/3" -c:v libx264 -profile:v baseline -pix_fmt yuv420p \ | ||
| 789 | + assets/test_anamorphic.mp4 | ||
| 790 | +ffmpeg -y -f lavfi -i "testsrc2=size=1440x1080:rate=30:duration=1" \ | ||
| 791 | + -vf "setsar=4/3" -c:v libx264 -profile:v baseline -pix_fmt yuv420p \ | ||
| 792 | + assets/test_anamorphic.mkv | ||
| 793 | + | ||
| 794 | +# ...and the anamorphic HEVC Matroska twin, for the copy path: `tests/`'s | ||
| 795 | +# copy door only copies HEVC or AV1 Matroska (`tests/video_copy.rs`), so an | ||
| 796 | +# anamorphic *copy* needs an anamorphic source in a codec that door takes. | ||
| 797 | +# Same source as above through libx265; the SAR rides the stream. | ||
| 798 | +ffmpeg -y -f lavfi -i "testsrc2=size=1440x1080:rate=30:duration=2" \ | ||
| 799 | + -vf "setsar=4/3" -c:v libx265 -pix_fmt yuv420p \ | ||
| 800 | + -x265-params "log-level=error:keyint=30:min-keyint=30" \ | ||
| 801 | + assets/test_anamorphic_hevc.mkv | ||
| 802 | + | ||
| 772 | 803 | ||
| 773 | echo "fixtures written to assets/" | 804 | echo "fixtures written to assets/" |