Stretch before the turn, so a rotated anamorphic file fills its canvas
Browse files at this commitf4bc699de9442218dd018b79ba8db59b948866cdParent: db45d3c- fix
- stretch before the turn, so a rotated anamorphic file fills its canvas
4 changed files +220 -83
- crates/engine/src/decode.rs +39 -32
- crates/engine/src/demux.rs +13 -10
- crates/engine/src/export.rs +23 -22
- crates/engine/tests/anamorphic.rs +145 -19
crates/engine/src/decode.rsmodified+39 -32
| @@ -1293,40 +1293,19 @@ impl Render { | ||
| 1293 | 1293 | width: u32, |
| 1294 | 1294 | height: u32, |
| 1295 | 1295 | ) -> Frame { |
| 1296 | - // The file's own turn, before anything else: a phone's portrait video | |
| 1297 | - // arrives as landscape planes, and the grade, the canvas and the | |
| 1298 | - // conversion below all have to see the picture the way it is meant to | |
| 1299 | - // be seen. Nothing here at all for the ordinary file that states no | |
| 1300 | - // turn -- `Rotation::None` takes the planes by reference and not one | |
| 1301 | - // byte is copied. | |
| 1302 | - let (y, u, v, width, height) = match self.rotation.is_none() { | |
| 1303 | - true => (y, u, v, width, height), | |
| 1304 | - false => { | |
| 1305 | - let steps = self.rotation.steps(); | |
| 1306 | - let (rw, rh) = crate::scale::rotate_i420_90s_into( | |
| 1307 | - y, | |
| 1308 | - u, | |
| 1309 | - v, | |
| 1310 | - width, | |
| 1311 | - height, | |
| 1312 | - steps, | |
| 1313 | - &mut self.turned, | |
| 1314 | - ); | |
| 1315 | - ( | |
| 1316 | - &self.turned.0[..], | |
| 1317 | - &self.turned.1[..], | |
| 1318 | - &self.turned.2[..], | |
| 1319 | - rw, | |
| 1320 | - rh, | |
| 1321 | - ) | |
| 1322 | - } | |
| 1323 | - }; | |
| 1324 | 1296 | // The pixel aspect, applied exactly once -- the single conversion to |
| 1325 | 1297 | // the square-pixel raster this engine makes, where ffmpeg's swscale |
| 1326 | - // makes its own. Only a file whose samples are not square reaches the | |
| 1327 | - // resample: a square one takes the branch by a bool and not one byte | |
| 1328 | - // is touched, which is what keeps every existing picture identical to | |
| 1329 | - // the day before anamorphic sources existed. | |
| 1298 | + // makes its own -- *before* the display-matrix turn. The ratio belongs | |
| 1299 | + // to the axis that was horizontal before the turn: the coded width is | |
| 1300 | + // stretched by h/v and the turn then swaps the stretched pair, which | |
| 1301 | + // is the composition the probe declares (a 1440x1080 SAR-4/3 file | |
| 1302 | + // turned 90° displays 1080x1920, DAR 9:16). Stretching after the turn | |
| 1303 | + // instead lands the ratio on the coded height and renders a | |
| 1304 | + // 1440x1440 square into a 9:16 canvas -- letterboxed, and the bars | |
| 1305 | + // baked into any export. Only a file whose samples are not square | |
| 1306 | + // reaches the resample: a square one takes the branch by a bool and | |
| 1307 | + // not one byte is touched, which is what keeps every existing picture | |
| 1308 | + // identical to the day before anamorphic sources existed. | |
| 1330 | 1309 | let (y, u, v, width, height) = if self.pixel_aspect.is_square() { |
| 1331 | 1310 | (y, u, v, width, height) |
| 1332 | 1311 | } else { |
| @@ -1365,6 +1344,34 @@ impl Render { | ||
| 1365 | 1344 | height, |
| 1366 | 1345 | ) |
| 1367 | 1346 | }; |
| 1347 | + // The file's own turn, after the stretch: a phone's portrait video | |
| 1348 | + // arrives as landscape planes, and the grade, the canvas and the | |
| 1349 | + // conversion below all have to see the picture the way it is meant to | |
| 1350 | + // be seen. Nothing here at all for the ordinary file that states no | |
| 1351 | + // turn -- `Rotation::None` takes the planes by reference and not one | |
| 1352 | + // byte is copied. | |
| 1353 | + let (y, u, v, width, height) = match self.rotation.is_none() { | |
| 1354 | + true => (y, u, v, width, height), | |
| 1355 | + false => { | |
| 1356 | + let steps = self.rotation.steps(); | |
| 1357 | + let (rw, rh) = crate::scale::rotate_i420_90s_into( | |
| 1358 | + y, | |
| 1359 | + u, | |
| 1360 | + v, | |
| 1361 | + width, | |
| 1362 | + height, | |
| 1363 | + steps, | |
| 1364 | + &mut self.turned, | |
| 1365 | + ); | |
| 1366 | + ( | |
| 1367 | + &self.turned.0[..], | |
| 1368 | + &self.turned.1[..], | |
| 1369 | + &self.turned.2[..], | |
| 1370 | + rw, | |
| 1371 | + rh, | |
| 1372 | + ) | |
| 1373 | + } | |
| 1374 | + }; | |
| 1368 | 1375 | let passthrough = self.canvas.is_passthrough(width, height); |
| 1369 | 1376 | // A transform still has a picture to place even where the canvas |
| 1370 | 1377 | // itself would hand this size back untouched -- moving, scaling, |
crates/engine/src/demux.rsmodified+13 -10
| @@ -1384,16 +1384,19 @@ impl MkvDemuxer { | ||
| 1384 | 1384 | } |
| 1385 | 1385 | } |
| 1386 | 1386 | }; |
| 1387 | - // The displayed shape, in the order the two facts compose: the aspect | |
| 1388 | - // widens the *coded* frame (180x320 at SAR 4/3 is drawn 240x320 -- | |
| 1389 | - // ffprobe's own `display_aspect_ratio` of such a file, measured), then | |
| 1390 | - // the quarter turn swaps that stretched pair the way it swaps the | |
| 1391 | - // coded one. The other order is a different picture: 320x180 then | |
| 1392 | - // widened by 4/3 lands at 426x180, and a pixel's drawn shape turns | |
| 1393 | - // with the picture it belongs to. The SAR only ever widens a width, | |
| 1394 | - // never a height; the displayed height is the coded one without a | |
| 1395 | - // turn and the stretched coded width with one. The coded pair stays | |
| 1396 | - // what the decoder hands back, and carries on to | |
| 1387 | + // The displayed shape, in the order the two facts compose -- the same | |
| 1388 | + // order the render applies them in | |
| 1389 | + // ([`crate::decode::Render::frame`]): the aspect widens the *coded* | |
| 1390 | + // frame (180x320 at SAR 4/3 is drawn 240x320 -- ffprobe's own | |
| 1391 | + // `display_aspect_ratio` of such a file, measured), then the quarter | |
| 1392 | + // turn swaps that stretched pair the way it swaps the coded one. The | |
| 1393 | + // other order is a different picture: 320x180 then widened by 4/3 | |
| 1394 | + // lands at 426x180, and a rotated anamorphic file rendered that way | |
| 1395 | + // lands a square raster in the 9:16 canvas its probe declares -- | |
| 1396 | + // letterboxed in the preview and in the export both. The SAR only | |
| 1397 | + // ever widens a width, never a height; the displayed height is the | |
| 1398 | + // coded one without a turn and the stretched coded width with one. | |
| 1399 | + // The coded pair stays what the decoder hands back, and carries on to | |
| 1397 | 1400 | // [`VideoMeta::coded_width`]/[`VideoMeta::coded_height`]. |
| 1398 | 1401 | let (width, height) = match video.rotation.swaps_axes() { |
| 1399 | 1402 | true => (video.height, video.pixel_aspect.width_factor(video.width)), |
crates/engine/src/export.rsmodified+23 -22
| @@ -3344,30 +3344,12 @@ fn place_picture<'a>( | ||
| 3344 | 3344 | transformed: &'a mut (Vec<u8>, Vec<u8>, Vec<u8>), |
| 3345 | 3345 | canvas: &'a mut Composer, |
| 3346 | 3346 | ) -> (&'a [u8], &'a [u8], &'a [u8], u32, u32) { |
| 3347 | - // The file's own turn, taken exactly as playback's decode funnel takes it | |
| 3348 | - // ([`crate::decode`]): a phone's portrait video is landscape in the file, | |
| 3349 | - // and the encoder must be fed what the preview showed. A file that states | |
| 3350 | - // no turn skips this entirely -- the planes go on borrowed. | |
| 3351 | - let (y, u, v, width, height) = match rotation.is_none() { | |
| 3352 | - true => (y, u, v, width, height), | |
| 3353 | - false => { | |
| 3354 | - let (w, h) = crate::scale::rotate_i420_90s_into( | |
| 3355 | - y, | |
| 3356 | - u, | |
| 3357 | - v, | |
| 3358 | - width, | |
| 3359 | - height, | |
| 3360 | - rotation.steps(), | |
| 3361 | - turned, | |
| 3362 | - ); | |
| 3363 | - (&turned.0[..], &turned.1[..], &turned.2[..], w, h) | |
| 3364 | - } | |
| 3365 | - }; | |
| 3366 | 3347 | // The pixel aspect, applied exactly once -- the same single conversion to |
| 3367 | 3348 | // the square-pixel raster the decode funnel makes, in the same order |
| 3368 | - // (stretch after turn), so the encoder is fed the pixels the preview | |
| 3369 | - // showed to the byte. A square-pixel file -- every file whose samples are | |
| 3370 | - // one to one -- borrows the planes through. | |
| 3349 | + // (stretch the coded width, *then* turn, [`crate::decode::Render::frame`]'s | |
| 3350 | + // comment carries the 1440x1440 failure the other order produces). A | |
| 3351 | + // square-pixel file -- every file whose samples are one to one -- borrows | |
| 3352 | + // the planes through. | |
| 3371 | 3353 | let (y, u, v, width, height) = if pixel_aspect.is_square() { |
| 3372 | 3354 | (y, u, v, width, height) |
| 3373 | 3355 | } else { |
| @@ -3399,6 +3381,25 @@ fn place_picture<'a>( | ||
| 3399 | 3381 | height, |
| 3400 | 3382 | ) |
| 3401 | 3383 | }; |
| 3384 | + // The file's own turn, taken exactly as playback's decode funnel takes it | |
| 3385 | + // ([`crate::decode`]): a phone's portrait video is landscape in the file, | |
| 3386 | + // and the encoder must be fed what the preview showed. A file that states | |
| 3387 | + // no turn skips this entirely -- the planes go on borrowed. | |
| 3388 | + let (y, u, v, width, height) = match rotation.is_none() { | |
| 3389 | + true => (y, u, v, width, height), | |
| 3390 | + false => { | |
| 3391 | + let (w, h) = crate::scale::rotate_i420_90s_into( | |
| 3392 | + y, | |
| 3393 | + u, | |
| 3394 | + v, | |
| 3395 | + width, | |
| 3396 | + height, | |
| 3397 | + rotation.steps(), | |
| 3398 | + turned, | |
| 3399 | + ); | |
| 3400 | + (&turned.0[..], &turned.1[..], &turned.2[..], w, h) | |
| 3401 | + } | |
| 3402 | + }; | |
| 3402 | 3403 | // The planes are borrowed from the decoder (and `Black::picture` hands the |
| 3403 | 3404 | // same slice as both u and v), so a grade cannot be applied in place: it |
| 3404 | 3405 | // goes onto a copy, which the encoder then reads instead. |
crates/engine/tests/anamorphic.rsmodified+145 -19
| @@ -135,13 +135,15 @@ fn the_container_aspect_is_probed_off_both_doors() { | ||
| 135 | 135 | ); |
| 136 | 136 | } |
| 137 | 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. | |
| 138 | + // a turn: the composition is stretch the coded width, then swap -- the | |
| 139 | + // order the render applies ([`crate::decode::Render::frame`]). The | |
| 140 | + // fixture carries a REAL display matrix (a `-display_rotation 90` remux, | |
| 141 | + // the gen_fixtures pattern), because a `setsar`-only file asserts nothing | |
| 142 | + // about the turn -- the case this whole block exists for. | |
| 141 | 143 | let turned = asset(".anamorphic_turned.mp4"); |
| 142 | - if std::process::Command::new("ffmpeg") | |
| 144 | + let made = std::process::Command::new("ffmpeg") | |
| 143 | 145 | .args(["-y", "-v", "error", "-f", "lavfi", "-i"]) |
| 144 | - .arg("testsrc2=size=180x320:rate=30:duration=1") | |
| 146 | + .arg("testsrc2=size=1440x1080:rate=30:duration=1") | |
| 145 | 147 | .args([ |
| 146 | 148 | "-vf", |
| 147 | 149 | "setsar=4/3", |
| @@ -154,21 +156,73 @@ fn the_container_aspect_is_probed_off_both_doors() { | ||
| 154 | 156 | ]) |
| 155 | 157 | .arg(&turned) |
| 156 | 158 | .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 | - ); | |
| 159 | + .is_ok_and(|s| s.success()); | |
| 160 | + if made { | |
| 161 | + // The same file with a 90-degree display matrix asked of it. | |
| 162 | + let matrix_on = asset(".anamorphic_turned90.mp4"); | |
| 163 | + let remuxed = std::process::Command::new("ffmpeg") | |
| 164 | + .args(["-y", "-v", "error", "-display_rotation", "90", "-i"]) | |
| 165 | + .arg(&turned) | |
| 166 | + .args(["-c", "copy"]) | |
| 167 | + .arg(&matrix_on) | |
| 168 | + .status() | |
| 169 | + .is_ok_and(|s| s.success()); | |
| 170 | + if remuxed { | |
| 171 | + let (meta, _) = Demuxer::open(&matrix_on).expect("open the turned fixture"); | |
| 172 | + // The probe: coded 1440x1080 stays; the SAR-4/3 widen of the | |
| 173 | + // coded width (1920) is what the turn swaps into the height, so | |
| 174 | + // the displayed pair is 1080x1920 -- DAR 9:16, the ratio ffprobe | |
| 175 | + // reports for the same file turned. A turn-then-stretch probe | |
| 176 | + // would declare 1440x1440 here; a stretch-on-the-wrong-axis | |
| 177 | + // render paints exactly that square into this 9:16 canvas. | |
| 178 | + assert_eq!( | |
| 179 | + (meta.coded_width, meta.coded_height), | |
| 180 | + (1440, 1080), | |
| 181 | + "the turned fixture's samples" | |
| 182 | + ); | |
| 183 | + assert_eq!( | |
| 184 | + meta.rotation, | |
| 185 | + engine::Rotation::Cw270, | |
| 186 | + "the matrix was read as a quarter turn" | |
| 187 | + ); | |
| 188 | + assert_eq!( | |
| 189 | + (meta.width, meta.height), | |
| 190 | + (1080, 1920), | |
| 191 | + "the aspect widens the coded width; the turn swaps the stretched pair" | |
| 192 | + ); | |
| 193 | + // ...and the render agrees with the probe: the composed picture | |
| 194 | + // is handed out at the displayed pair, filled to the corners -- | |
| 195 | + // the 1440x1440 failure the other order produces would fail both | |
| 196 | + // assertions. | |
| 197 | + let (_, frames, _) = | |
| 198 | + engine::DecodeSession::open_at(&matrix_on, 0).expect("open for a frame"); | |
| 199 | + let frame = frames.recv().expect("a first frame"); | |
| 200 | + assert_eq!( | |
| 201 | + (frame.width, frame.height), | |
| 202 | + (1080, 1920), | |
| 203 | + "the rendered picture is the displayed pair" | |
| 204 | + ); | |
| 205 | + let (w, h) = (frame.width as usize, frame.height as usize); | |
| 206 | + let at = |x: usize, y: usize| { | |
| 207 | + let i = (y * w + x) * 4; | |
| 208 | + (frame.bgra[i], frame.bgra[i + 1], frame.bgra[i + 2]) | |
| 209 | + }; | |
| 210 | + let m = w / 10; | |
| 211 | + let corners = [ | |
| 212 | + at(m, m), | |
| 213 | + at(w - 1 - m, m), | |
| 214 | + at(w - 1 - m, h - 1 - m), | |
| 215 | + at(m, h - 1 - m), | |
| 216 | + ]; | |
| 217 | + assert!( | |
| 218 | + corners | |
| 219 | + .iter() | |
| 220 | + .all(|&(b, g, r)| u32::from(r) + u32::from(g) + u32::from(b) > 48), | |
| 221 | + "the corners are picture, not black: {corners:?}" | |
| 222 | + ); | |
| 223 | + } | |
| 171 | 224 | let _ = std::fs::remove_file(&turned); |
| 225 | + let _ = std::fs::remove_file(&matrix_on); | |
| 172 | 226 | } |
| 173 | 227 | } |
| 174 | 228 | |
| @@ -268,6 +322,78 @@ fn an_export_of_an_anamorphic_file_is_drawn_and_square() { | ||
| 268 | 322 | mean < 2.0, |
| 269 | 323 | "the export is {mean:.2} mean codes from the preview -- not the same picture" |
| 270 | 324 | ); |
| 325 | + | |
| 326 | + // The same contract for a ROTATED anamorphic source: the fixture carries a | |
| 327 | + // real display matrix, the probe declares 1080x1920, and the export must | |
| 328 | + // be that shape with the picture the preview showed -- the case a | |
| 329 | + // turn-then-stretch render filled with 1440x1440 and baked bars into. | |
| 330 | + let plain = asset(".anamorphic_export_src.mp4"); | |
| 331 | + let matrix_on = asset(".anamorphic_export_rot.mp4"); | |
| 332 | + let made = std::process::Command::new("ffmpeg") | |
| 333 | + .args(["-y", "-v", "error", "-f", "lavfi", "-i"]) | |
| 334 | + .arg("testsrc2=size=1440x1080:rate=30:duration=1") | |
| 335 | + .args([ | |
| 336 | + "-vf", | |
| 337 | + "setsar=4/3", | |
| 338 | + "-c:v", | |
| 339 | + "libx264", | |
| 340 | + "-profile:v", | |
| 341 | + "baseline", | |
| 342 | + "-pix_fmt", | |
| 343 | + "yuv420p", | |
| 344 | + ]) | |
| 345 | + .arg(&plain) | |
| 346 | + .status() | |
| 347 | + .is_ok_and(|s| s.success()) | |
| 348 | + && std::process::Command::new("ffmpeg") | |
| 349 | + .args(["-y", "-v", "error", "-display_rotation", "90", "-i"]) | |
| 350 | + .arg(&plain) | |
| 351 | + .args(["-c", "copy"]) | |
| 352 | + .arg(&matrix_on) | |
| 353 | + .status() | |
| 354 | + .is_ok_and(|s| s.success()); | |
| 355 | + if made { | |
| 356 | + let mut session = PlaybackSession::open(&matrix_on).expect("open the turned fixture"); | |
| 357 | + session.pause(); | |
| 358 | + let preview = loop { | |
| 359 | + if let Some(frame) = session.try_frame() { | |
| 360 | + break frame; | |
| 361 | + } | |
| 362 | + std::thread::sleep(Duration::from_millis(10)); | |
| 363 | + }; | |
| 364 | + assert_eq!((preview.width, preview.height), (1080, 1920)); | |
| 365 | + let out = Scratch::file("ve_anamorphic_rot_export", "mp4"); | |
| 366 | + let handle = session.export_to_with(&out, &ExportSettings::default()); | |
| 367 | + wait(&handle, Duration::from_secs(120)).expect("export"); | |
| 368 | + let (meta, _) = Demuxer::open(&out).expect("reopen the export"); | |
| 369 | + assert_eq!( | |
| 370 | + (meta.coded_width, meta.coded_height), | |
| 371 | + (1080, 1920), | |
| 372 | + "the rotated export is coded at the displayed shape" | |
| 373 | + ); | |
| 374 | + assert!(meta.pixel_aspect.is_square(), "no aspect left to state"); | |
| 375 | + let mut reopened = PlaybackSession::open(&out).expect("reopen the export"); | |
| 376 | + let exported = loop { | |
| 377 | + if let Some(frame) = reopened.try_frame() { | |
| 378 | + break frame; | |
| 379 | + } | |
| 380 | + std::thread::sleep(Duration::from_millis(10)); | |
| 381 | + }; | |
| 382 | + assert_eq!((exported.width, exported.height), (1080, 1920)); | |
| 383 | + let moved = preview | |
| 384 | + .bgra | |
| 385 | + .iter() | |
| 386 | + .zip(&exported.bgra) | |
| 387 | + .map(|(a, b)| u32::from(a.abs_diff(*b))) | |
| 388 | + .sum::<u32>(); | |
| 389 | + let mean = moved as f64 / preview.bgra.len() as f64; | |
| 390 | + assert!( | |
| 391 | + mean < 2.0, | |
| 392 | + "the rotated export is {mean:.2} mean codes from the preview" | |
| 393 | + ); | |
| 394 | + let _ = std::fs::remove_file(&plain); | |
| 395 | + let _ = std::fs::remove_file(&matrix_on); | |
| 396 | + } | |
| 271 | 397 | } |
| 272 | 398 | |
| 273 | 399 | /// The copy door, Matroska side: an anamorphic source's Exact row copies the |