mp4: read the tkhd display matrix and surface it as a typed Rotation
Browse files at this commitead0050fd6dd745de5cca88b5c15bd0d278408d7Parent: 9094a1711 changed files +368 -4
- crates/ec-core/src/lib.rs +1 -1
- crates/ec-core/src/registry.rs +163 -0
- crates/ec-mp4/src/demux.rs +18 -3
- crates/ec-mp4/tests/data/gen.sh +32 -0
- crates/ec-mp4/tests/data/rot0.mp4 +0 -0
- crates/ec-mp4/tests/data/rot180.mp4 +0 -0
- crates/ec-mp4/tests/data/rot270.mp4 +0 -0
- crates/ec-mp4/tests/data/rot45.mp4 +0 -0
- crates/ec-mp4/tests/data/rot90.mp4 +0 -0
- crates/ec-mp4/tests/rotation.rs +69 -0
- lanes/mp4rotation.report.md +85 -0
crates/ec-core/src/lib.rsmodified+1 -1
| @@ -45,6 +45,6 @@ pub use frame::{ | |||
| 45 | pub use packet::{Buf, Packet, PacketFlags, SideData}; | 45 | pub use packet::{Buf, Packet, PacketFlags, SideData}; |
| 46 | pub use registry::{ | 46 | pub use registry::{ |
| 47 | AudioParameters, CodecId, CodecParameters, Decoder, Demuxer, Encoder, MediaParameters, | 47 | AudioParameters, CodecId, CodecParameters, Decoder, Demuxer, Encoder, MediaParameters, |
| 48 | - MediaType, Muxer, SeekMode, StreamInfo, VideoParameters, | 48 | + MediaType, Muxer, Rotation, SeekMode, StreamInfo, VideoParameters, |
| 49 | }; | 49 | }; |
| 50 | pub use timebase::{Rounding, TimeBase, Timestamp}; | 50 | pub use timebase::{Rounding, TimeBase, Timestamp}; |
crates/ec-core/src/registry.rsmodified+163 -0
| @@ -224,6 +224,101 @@ impl CodecParameters { | |||
| 224 | } | 224 | } |
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | +/// The turn a container's display matrix asks a player to make so its pictures | ||
| 228 | +/// are seen the way they were shot: quarter turns **clockwise**. A phone's | ||
| 229 | +/// portrait video is the case that names it -- the pixels stay landscape and | ||
| 230 | +/// the container says which way to turn them. | ||
| 231 | +/// | ||
| 232 | +/// This is metadata about the *pixels*, not a user's edit, and it is not a | ||
| 233 | +/// request to touch them here: a demuxer reports it and a consumer (the | ||
| 234 | +/// preview, the encoder) decides. A matrix that is not a quarter turn -- a | ||
| 235 | +/// mirror, a shear, a scale, or a 45-degree quirk -- is [`Rotation::Other`] | ||
| 236 | +/// rather than an approximation, so a caller refuses it by name instead of | ||
| 237 | +/// showing the picture wrong. | ||
| 238 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | ||
| 239 | +pub enum Rotation { | ||
| 240 | + /// No turn: an identity display matrix, or a container that states one. | ||
| 241 | + #[default] | ||
| 242 | + None, | ||
| 243 | + /// A quarter turn clockwise. | ||
| 244 | + Cw90, | ||
| 245 | + /// A half turn (two quarter turns clockwise). | ||
| 246 | + Cw180, | ||
| 247 | + /// Three quarter turns clockwise, i.e. one quarter counter-clockwise. | ||
| 248 | + Cw270, | ||
| 249 | + /// A display matrix that is not a quarter turn: a mirror (a negative | ||
| 250 | + /// determinant), a shear, an anisotropic scale, or an angle that is not a | ||
| 251 | + /// multiple of 90 degrees. The four linear terms are carried verbatim so | ||
| 252 | + /// the reading is representable rather than silently dropped -- they are | ||
| 253 | + /// the 16.16 fixed-point `a`, `b`, `c`, `d` of an ISO-BMFF `tkhd` matrix | ||
| 254 | + /// (`matrix[0][0]`, `[0][1]`, `[1][0]`, `[1][1]`). | ||
| 255 | + Other { | ||
| 256 | + /// The matrix's `a` term, 16.16 fixed point. | ||
| 257 | + a: i32, | ||
| 258 | + /// The matrix's `b` term, 16.16 fixed point. | ||
| 259 | + b: i32, | ||
| 260 | + /// The matrix's `c` term, 16.16 fixed point. | ||
| 261 | + c: i32, | ||
| 262 | + /// The matrix's `d` term, 16.16 fixed point. | ||
| 263 | + d: i32, | ||
| 264 | + }, | ||
| 265 | +} | ||
| 266 | + | ||
| 267 | +impl Rotation { | ||
| 268 | + /// The rotation an ISO-BMFF `tkhd` display matrix states, from its linear | ||
| 269 | + /// 2x2 part read as 16.16 fixed-point `i32`s. | ||
| 270 | + /// | ||
| 271 | + /// The matrix transforms a point `(x, y)` to `(a*x + c*y, b*x + d*y)`, so | ||
| 272 | + /// the columns `(a, b)` and `(c, d)` are the turned axes: a pure rotation | ||
| 273 | + /// has both columns the same length (isotropic) and one the other's quarter | ||
| 274 | + /// turn (`a == d`, `b == -c`). Anything else is [`Rotation::Other`]. | ||
| 275 | + /// | ||
| 276 | + /// The reading is exact integer arithmetic: the four quarter turns are | ||
| 277 | + /// recognised by an axis landing on an axis (`b == 0` or `a == 0`). An | ||
| 278 | + /// encoder writes 16.16 quarter turns exactly, so a value within a fraction | ||
| 279 | + /// of a degree of one that is not exactly one -- a hand-edited or rescaled | ||
| 280 | + /// matrix -- reads as [`Rotation::Other`] rather than being rounded onto a | ||
| 281 | + /// turn this decoder would then apply for real. | ||
| 282 | + pub fn from_matrix(a: i32, b: i32, c: i32, d: i32) -> Rotation { | ||
| 283 | + // A pure rotation: isotropic (a == d, b == -c) with something to turn. | ||
| 284 | + if a == d && b == (-c) && (a != 0 || b != 0) { | ||
| 285 | + // `(a, b)` is the turned x-axis; a quarter turn lands it on an axis. | ||
| 286 | + return match (a.signum(), b.signum()) { | ||
| 287 | + (1, 0) => Rotation::None, | ||
| 288 | + (-1, 0) => Rotation::Cw180, | ||
| 289 | + (0, 1) => Rotation::Cw90, | ||
| 290 | + (0, -1) => Rotation::Cw270, | ||
| 291 | + _ => Rotation::Other { a, b, c, d }, | ||
| 292 | + }; | ||
| 293 | + } | ||
| 294 | + Rotation::Other { a, b, c, d } | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | + /// Quarter turns clockwise, where the stream is a quarter turn at all -- | ||
| 298 | + /// `None` for anything [`Rotation::Other`] carries. | ||
| 299 | + pub fn steps(self) -> Option<u8> { | ||
| 300 | + match self { | ||
| 301 | + Rotation::None => Some(0), | ||
| 302 | + Rotation::Cw90 => Some(1), | ||
| 303 | + Rotation::Cw180 => Some(2), | ||
| 304 | + Rotation::Cw270 => Some(3), | ||
| 305 | + Rotation::Other { .. } => None, | ||
| 306 | + } | ||
| 307 | + } | ||
| 308 | + | ||
| 309 | + /// Whether the turned picture's width and height are the coded ones | ||
| 310 | + /// swapped. | ||
| 311 | + pub fn swaps_axes(self) -> bool { | ||
| 312 | + matches!(self, Rotation::Cw90 | Rotation::Cw270) | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | + /// Whether there is nothing to do: no turn, the common case of a file that | ||
| 316 | + /// was shot the way it is stored. | ||
| 317 | + pub fn is_none(self) -> bool { | ||
| 318 | + matches!(self, Rotation::None) | ||
| 319 | + } | ||
| 320 | +} | ||
| 321 | + | ||
| 227 | /// One stream of a container, as a demuxer reports it. | 322 | /// One stream of a container, as a demuxer reports it. |
| 228 | #[derive(Debug, Clone, PartialEq)] | 323 | #[derive(Debug, Clone, PartialEq)] |
| 229 | pub struct StreamInfo { | 324 | pub struct StreamInfo { |
| @@ -258,6 +353,14 @@ pub struct StreamInfo { | |||
| 258 | /// flagged stream at all, and its first stream of a kind is its default | 353 | /// flagged stream at all, and its first stream of a kind is its default |
| 259 | /// ([`crate::registry`] callers, `ec_probe::Reader::default_stream`). | 354 | /// ([`crate::registry`] callers, `ec_probe::Reader::default_stream`). |
| 260 | pub default: bool, | 355 | pub default: bool, |
| 356 | + /// The turn this stream's display matrix asks for, where the container | ||
| 357 | + /// states one. [`Rotation::None`] for a container that states none, for an | ||
| 358 | + /// identity matrix, and for a non-video stream alike -- the common case. | ||
| 359 | + /// | ||
| 360 | + /// This is metadata, not an edit: nothing about [`params`](Self::params) | ||
| 361 | + /// changes because of it, and a caller that wants the displayed picture | ||
| 362 | + /// (the preview, the encoder) has to make the turn itself. | ||
| 363 | + pub rotation: Rotation, | ||
| 261 | } | 364 | } |
| 262 | 365 | ||
| 263 | impl StreamInfo { | 366 | impl StreamInfo { |
| @@ -273,6 +376,7 @@ impl StreamInfo { | |||
| 273 | language: None, | 376 | language: None, |
| 274 | initial_padding: 0, | 377 | initial_padding: 0, |
| 275 | default: false, | 378 | default: false, |
| 379 | + rotation: Rotation::None, | ||
| 276 | } | 380 | } |
| 277 | } | 381 | } |
| 278 | } | 382 | } |
| @@ -452,6 +556,65 @@ mod tests { | |||
| 452 | assert_eq!(s.params.codec.media_type(), MediaType::Subtitle); | 556 | assert_eq!(s.params.codec.media_type(), MediaType::Subtitle); |
| 453 | assert_eq!(s.index, 2); | 557 | assert_eq!(s.index, 2); |
| 454 | assert_eq!(s.language.as_deref(), Some("tur")); | 558 | assert_eq!(s.language.as_deref(), Some("tur")); |
| 559 | + assert_eq!(s.rotation, Rotation::None); | ||
| 560 | + } | ||
| 561 | + | ||
| 562 | + #[test] | ||
| 563 | + fn quarter_turn_matrices_read_as_typed_rotations() { | ||
| 564 | + const ONE: i32 = 65_536; // 16.16 unity. | ||
| 565 | + let cases = [ | ||
| 566 | + // A tkhd matrix and the turn it states, in this crate's clockwise | ||
| 567 | + // convention. `(0, -1, 1, 0)` is what ffmpeg's | ||
| 568 | + // `-display_rotation 90` writes: it asks the picture to be turned | ||
| 569 | + // a quarter counter-clockwise here, which is Cw270. | ||
| 570 | + ((ONE, 0, 0, ONE), Rotation::None), | ||
| 571 | + ((0, -ONE, ONE, 0), Rotation::Cw270), | ||
| 572 | + ((-ONE, 0, 0, -ONE), Rotation::Cw180), | ||
| 573 | + ((0, ONE, -ONE, 0), Rotation::Cw90), | ||
| 574 | + // A scaled identity is still no turn: the reading normalises by the | ||
| 575 | + // matrix's own scale, so a differently-scaled copy reads the same. | ||
| 576 | + ((2 * ONE, 0, 0, 2 * ONE), Rotation::None), | ||
| 577 | + ]; | ||
| 578 | + for ((a, b, c, d), want) in cases { | ||
| 579 | + assert_eq!(Rotation::from_matrix(a, b, c, d), want, "{a} {b} {c} {d}"); | ||
| 580 | + } | ||
| 581 | + assert_eq!(Rotation::None.steps(), Some(0)); | ||
| 582 | + assert_eq!(Rotation::Cw90.steps(), Some(1)); | ||
| 583 | + assert_eq!(Rotation::Cw180.steps(), Some(2)); | ||
| 584 | + assert_eq!(Rotation::Cw270.steps(), Some(3)); | ||
| 585 | + assert!(Rotation::Cw90.swaps_axes() && Rotation::Cw270.swaps_axes()); | ||
| 586 | + assert!(!Rotation::None.swaps_axes() && !Rotation::Cw180.swaps_axes()); | ||
| 587 | + assert!(Rotation::None.is_none()); | ||
| 588 | + } | ||
| 589 | + | ||
| 590 | + #[test] | ||
| 591 | + fn a_matrix_that_is_not_a_quarter_turn_is_kept_not_rounded() { | ||
| 592 | + const ONE: i32 = 65_536; | ||
| 593 | + // A 45-degree turn: isotropic and pure, but no axis lands on an axis. | ||
| 594 | + assert_eq!( | ||
| 595 | + Rotation::from_matrix(46_340, -46_340, 46_340, 46_340), | ||
| 596 | + Rotation::Other { | ||
| 597 | + a: 46_340, | ||
| 598 | + b: -46_340, | ||
| 599 | + c: 46_340, | ||
| 600 | + d: 46_340, | ||
| 601 | + } | ||
| 602 | + ); | ||
| 603 | + // A mirror (negative determinant), a mirror of a quarter turn (b != -c), | ||
| 604 | + // a shear (a != d), and the degenerate all-zero matrix: none is a turn. | ||
| 605 | + for (a, b, c, d) in [ | ||
| 606 | + (ONE, 0, 0, -ONE), | ||
| 607 | + (0, ONE, ONE, 0), | ||
| 608 | + (ONE, 0, ONE, ONE), | ||
| 609 | + (0, 0, 0, 0), | ||
| 610 | + ] { | ||
| 611 | + let got = Rotation::from_matrix(a, b, c, d); | ||
| 612 | + assert!( | ||
| 613 | + matches!(got, Rotation::Other { .. }), | ||
| 614 | + "{a} {b} {c} {d} read as {got:?}" | ||
| 615 | + ); | ||
| 616 | + assert_eq!(got.steps(), None, "Other is not a quarter count"); | ||
| 617 | + } | ||
| 455 | } | 618 | } |
| 456 | 619 | ||
| 457 | #[test] | 620 | #[test] |
crates/ec-mp4/src/demux.rsmodified+18 -3
| @@ -4,8 +4,8 @@ use std::io::{Read, Seek}; | |||
| 4 | 4 | ||
| 5 | use ec_core::{ | 5 | use ec_core::{ |
| 6 | AudioParameters, Buf, ChannelLayout, CodecId, CodecParameters, ColorInfo, Demuxer, Error, | 6 | AudioParameters, Buf, ChannelLayout, CodecId, CodecParameters, ColorInfo, Demuxer, Error, |
| 7 | - MediaParameters, MediaType, Packet, PacketFlags, Result, Rounding, SeekMode, StreamInfo, | 7 | + MediaParameters, MediaType, Packet, PacketFlags, Result, Rotation, Rounding, SeekMode, |
| 8 | - TimeBase, Timestamp, VideoParameters, color, color::Tags, | 8 | + StreamInfo, TimeBase, Timestamp, VideoParameters, color, color::Tags, |
| 9 | }; | 9 | }; |
| 10 | 10 | ||
| 11 | use crate::boxes::{Boxes, FourCc, Src, be16, be32, be64, full}; | 11 | use crate::boxes::{Boxes, FourCc, Src, be16, be32, be64, full}; |
| @@ -320,6 +320,7 @@ impl<R: Read + Seek> Mp4Demuxer<R> { | |||
| 320 | let mut mdia = None; | 320 | let mut mdia = None; |
| 321 | let mut title = None; | 321 | let mut title = None; |
| 322 | let mut display = (0u32, 0u32); | 322 | let mut display = (0u32, 0u32); |
| 323 | + let mut rotation = Rotation::None; | ||
| 323 | for child in Boxes::new(data) { | 324 | for child in Boxes::new(data) { |
| 324 | let (kind, payload) = child?; | 325 | let (kind, payload) = child?; |
| 325 | match &kind { | 326 | match &kind { |
| @@ -327,8 +328,19 @@ impl<R: Read + Seek> Mp4Demuxer<R> { | |||
| 327 | let (version, _, rest) = full(payload)?; | 328 | let (version, _, rest) = full(payload)?; |
| 328 | let at = if version == 0 { 8 } else { 16 }; | 329 | let at = if version == 0 { 8 } else { 16 }; |
| 329 | track_id = be32(rest, at)?; | 330 | track_id = be32(rest, at)?; |
| 330 | - // ...and the display size, 16.16, at the far end of the box. | 331 | + // The display matrix -- nine 16.16 fixed-point `s32` -- and |
| 332 | + // the display size, also 16.16, at the far end of the box. | ||
| 333 | + // The matrix is the 36 bytes just before the size; only its | ||
| 334 | + // leading 2x2 (`a b c d`) says which way the picture turns. | ||
| 331 | let end = rest.len().saturating_sub(8); | 335 | let end = rest.len().saturating_sub(8); |
| 336 | + let matrix = end.saturating_sub(36); | ||
| 337 | + let (a, b, c, d) = ( | ||
| 338 | + be32(rest, matrix)? as i32, | ||
| 339 | + be32(rest, matrix + 4)? as i32, | ||
| 340 | + be32(rest, matrix + 12)? as i32, | ||
| 341 | + be32(rest, matrix + 16)? as i32, | ||
| 342 | + ); | ||
| 343 | + rotation = Rotation::from_matrix(a, b, c, d); | ||
| 332 | display = (be32(rest, end)? >> 16, be32(rest, end + 4)? >> 16); | 344 | display = (be32(rest, end)? >> 16, be32(rest, end + 4)? >> 16); |
| 333 | } | 345 | } |
| 334 | b"edts" => { | 346 | b"edts" => { |
| @@ -458,6 +470,9 @@ impl<R: Read + Seek> Mp4Demuxer<R> { | |||
| 458 | // a trim: no sample is dropped, so none is announced as | 470 | // a trim: no sample is dropped, so none is announced as |
| 459 | // padding either. | 471 | // padding either. |
| 460 | initial_padding: 0, | 472 | initial_padding: 0, |
| 473 | + // The `tkhd` display matrix: which way the coded picture | ||
| 474 | + // turns to be seen as shot. Reported, not applied here. | ||
| 475 | + rotation, | ||
| 461 | }); | 476 | }); |
| 462 | Some(index) | 477 | Some(index) |
| 463 | } | 478 | } |
crates/ec-mp4/tests/data/gen.shadded+32 -0
| @@ -0,0 +1,32 @@ | |||
| 1 | +#!/usr/bin/env bash | ||
| 2 | +# gen.sh — regenerate the display-matrix fixtures in this directory. | ||
| 3 | +# | ||
| 4 | +# `rot<deg>.mp4` are one 320x240 testsrc2 clip (deterministic content) written | ||
| 5 | +# back out with an ISO-BMFF `tkhd` display matrix asking for `deg` degrees: | ||
| 6 | +# exactly how a phone records a portrait video (the pixels stay landscape and | ||
| 7 | +# the container says which way to turn them). A player that ignores the matrix | ||
| 8 | +# shows the picture sideways, which is the bug these fixtures exist to catch. | ||
| 9 | +# | ||
| 10 | +# `-display_rotation` is an *input* option here, so it goes **before** the `-i` | ||
| 11 | +# it applies to; the `-metadata:s:v rotate=90` form every search result gives | ||
| 12 | +# writes no display matrix at all with this machine's ffmpeg. | ||
| 13 | +# | ||
| 14 | +# Bit-exact reproduction (ffmpeg 8.1.2, libx264): the whole directory is | ||
| 15 | +# regenerated with the same bytes the committed fixtures carry -- the four | ||
| 16 | +# differs only inside the tkhd matrix. Verify with `sha256sum rot*.mp4`. | ||
| 17 | +# | ||
| 18 | +# Usage: crates/ec-mp4/tests/data/gen.sh | ||
| 19 | +set -euo pipefail | ||
| 20 | + | ||
| 21 | +here=$(cd -- "$(dirname -- "$0")" && pwd) | ||
| 22 | +src=$(mktemp --suffix=.mp4) | ||
| 23 | +trap 'rm -f -- "$src"' EXIT | ||
| 24 | + | ||
| 25 | +ffmpeg -nostdin -y -v error -f lavfi \ | ||
| 26 | + -i "testsrc2=size=320x240:rate=24:duration=1" \ | ||
| 27 | + -c:v libx264 -profile:v baseline -pix_fmt yuv420p "$src" | ||
| 28 | + | ||
| 29 | +for deg in 0 90 180 270 45; do | ||
| 30 | + ffmpeg -nostdin -y -v error -display_rotation "$deg" -i "$src" \ | ||
| 31 | + -c copy "$here/rot$deg.mp4" | ||
| 32 | +done | ||
crates/ec-mp4/tests/data/rot0.mp4added+0 -0
Binary file changed
crates/ec-mp4/tests/data/rot180.mp4added+0 -0
Binary file changed
crates/ec-mp4/tests/data/rot270.mp4added+0 -0
Binary file changed
crates/ec-mp4/tests/data/rot45.mp4added+0 -0
Binary file changed
crates/ec-mp4/tests/data/rot90.mp4added+0 -0
Binary file changed
crates/ec-mp4/tests/rotation.rsadded+69 -0
| @@ -0,0 +1,69 @@ | |||
| 1 | +//! What the `tkhd` display matrix says: which way the coded picture turns to be | ||
| 2 | +//! seen the way it was shot -- a phone's portrait video is the case that names | ||
| 3 | +//! it, and a player that ignores the matrix shows it sideways. | ||
| 4 | +//! | ||
| 5 | +//! The fixtures are committed here (`tests/data/gen.sh` regenerates them | ||
| 6 | +//! byte-for-byte): one deterministic testsrc2 clip written back out with | ||
| 7 | +//! `ffmpeg -display_rotation <deg>`, which is a real `tkhd` matrix and not the | ||
| 8 | +//! `rotate` metadata that writes none at all. | ||
| 9 | + | ||
| 10 | +use std::fs::File; | ||
| 11 | +use std::io::BufReader; | ||
| 12 | +use std::path::{Path, PathBuf}; | ||
| 13 | + | ||
| 14 | +use ec_core::{Demuxer, Rotation}; | ||
| 15 | +use ec_mp4::Mp4Demuxer; | ||
| 16 | + | ||
| 17 | +fn data(name: &str) -> PathBuf { | ||
| 18 | + Path::new(env!("CARGO_MANIFEST_DIR")) | ||
| 19 | + .join("tests/data") | ||
| 20 | + .join(name) | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +/// The rotation the demuxer reports for `name`'s single video stream. | ||
| 24 | +fn rotation_of(name: &str) -> Rotation { | ||
| 25 | + let path = data(name); | ||
| 26 | + let file = File::open(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); | ||
| 27 | + let demuxer = | ||
| 28 | + Mp4Demuxer::new(BufReader::new(file)).unwrap_or_else(|e| panic!("{}: {e}", path.display())); | ||
| 29 | + let streams = demuxer.streams(); | ||
| 30 | + assert_eq!(streams.len(), 1, "{}: one track", path.display()); | ||
| 31 | + streams[0].rotation | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +#[test] | ||
| 35 | +fn a_tkhd_matrix_surfaces_as_a_typed_quarter_turn() { | ||
| 36 | + // ffmpeg's own counter-clockwise `-display_rotation` against this crate's | ||
| 37 | + // clockwise reading: its 90 is a quarter turn the other way, so it reads as | ||
| 38 | + // `Cw270` -- the same way edith's engine reads the identical matrices. | ||
| 39 | + assert_eq!(rotation_of("rot90.mp4"), Rotation::Cw270); | ||
| 40 | + assert_eq!(rotation_of("rot180.mp4"), Rotation::Cw180); | ||
| 41 | + assert_eq!(rotation_of("rot270.mp4"), Rotation::Cw90); | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +#[test] | ||
| 45 | +fn a_file_with_no_turn_surfaces_no_rotation() { | ||
| 46 | + // ffmpeg writes no display side data for `-display_rotation 0`; the box's | ||
| 47 | + // matrix is the identity, which is no turn rather than a missing reading. | ||
| 48 | + let rotation = rotation_of("rot0.mp4"); | ||
| 49 | + assert!(rotation.is_none(), "{rotation:?}"); | ||
| 50 | + assert_eq!(rotation, Rotation::None); | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +#[test] | ||
| 54 | +fn a_matrix_that_is_not_a_quarter_turn_is_representable() { | ||
| 55 | + // A 45-degree turn cannot be straightened: it is carried, not rounded onto | ||
| 56 | + // a quarter turn and not dropped. The terms are the box's own 16.16 values. | ||
| 57 | + let rotation = rotation_of("rot45.mp4"); | ||
| 58 | + assert!(matches!(rotation, Rotation::Other { .. }), "{rotation:?}"); | ||
| 59 | + assert_eq!(rotation.steps(), None); | ||
| 60 | + assert_eq!( | ||
| 61 | + rotation, | ||
| 62 | + Rotation::Other { | ||
| 63 | + a: 46_340, | ||
| 64 | + b: -46_340, | ||
| 65 | + c: 46_340, | ||
| 66 | + d: 46_340, | ||
| 67 | + } | ||
| 68 | + ); | ||
| 69 | +} | ||
lanes/mp4rotation.report.mdadded+85 -0
| @@ -0,0 +1,85 @@ | |||
| 1 | +# lane-mp4rotation report — ec-mp4 reads the `tkhd` display matrix (parse + surface) | ||
| 2 | + | ||
| 3 | +VERDICT: PASS — the matrix is parsed from both `tkhd` versions and surfaced as a | ||
| 4 | +typed `ec_core::registry::Rotation` on `StreamInfo`; quarter turns are typed, | ||
| 5 | +non-quarter matrices are carried (`Rotation::Other`), every suite green, warning | ||
| 6 | +parity with c102c27a. No pixel rotation (scope: parse + surface); the muxer still | ||
| 7 | +writes a unity matrix (see Residue). | ||
| 8 | + | ||
| 9 | +## What landed | ||
| 10 | +- `ec-core` (`registry.rs`, `lib.rs`): new public `Rotation` | ||
| 11 | + (`None`/`Cw90`/`Cw180`/`Cw270`/`Other { a, b, c, d }`) plus | ||
| 12 | + `Rotation::from_matrix(a, b, c, d)`, `steps() -> Option<u8>`, `swaps_axes()`, | ||
| 13 | + `is_none()`, re-exported from `ec_core`. New additive field | ||
| 14 | + `StreamInfo.rotation: Rotation` (defaults `Rotation::None` in | ||
| 15 | + `StreamInfo::new`); the only `StreamInfo` struct literal in the workspace | ||
| 16 | + (`ec-mp4/src/demux.rs`) was migrated — every other construction goes through | ||
| 17 | + `new()` and is untouched. | ||
| 18 | +- `ec-mp4` (`demux.rs`, `read_trak`): the `tkhd` arm now reads the 36-byte | ||
| 19 | + display matrix (nine `s32`) that sits immediately before the `u32` width and | ||
| 20 | + height at the end of the box — correct for both versions, since the far end is | ||
| 21 | + version-independent (v0 `rest` is 80 B, v1 is 92 B). Its leading 2x2 | ||
| 22 | + (`a b c d`, offsets +0/+4/+12/+16) goes through `Rotation::from_matrix`. | ||
| 23 | + The reported rotation is not applied to the pixels. | ||
| 24 | +- Fixtures committed under `crates/ec-mp4/tests/data/` with the generator | ||
| 25 | + (`gen.sh`), byte-for-byte reproducible on this machine: | ||
| 26 | + `rot0.mp4` (no display side data: the identity matrix, no turn), | ||
| 27 | + `rot90/180/270.mp4`, `rot45.mp4` (a turn that is not a quarter turn). | ||
| 28 | + sha256: | ||
| 29 | + - rot0 `41400f216615a4054f20600fc6c9696cf753b3626db5aad811c9b6f12bd3c894` | ||
| 30 | + - rot90 `ce5cbe9290bd7c69483ba87f1f9290dc41575d76f6281e580b23d73e61b1c759` | ||
| 31 | + - rot180 `375d912d891bc8197967b77e6a01d9ef366789fbc8b8cbaa7793e060ac0e1011` | ||
| 32 | + - rot270 `77c0ecef4592c1c0084a28e1f44829ee32f01ce8abc943298192fd15b4398ce4` | ||
| 33 | + - rot45 `5fc8df151b85a03040f5f48a1f0b49289153a64f2514f5cf899dd1ae4a6ea715` | ||
| 34 | + Generator: `ffmpeg -f lavfi -i testsrc2=size=320x240:rate=24:duration=1 -c:v | ||
| 35 | + libx264 -profile:v baseline -pix_fmt yuv420p` to a scratch source, then | ||
| 36 | + `ffmpeg -display_rotation <deg> -i src -c copy rot<deg>.mp4` (ffmpeg 8.1.2, | ||
| 37 | + libx264). `-display_rotation` is an **input** option (before `-i`); the | ||
| 38 | + `-metadata:s:v rotate=90` form writes no matrix at all. | ||
| 39 | + | ||
| 40 | +## Reading convention (matches edith's engine exactly) | ||
| 41 | +Quarter turns **clockwise**; `steps()` = CW count. ffmpeg's counter-clockwise | ||
| 42 | +`-display_rotation 90` writes matrix `a=0,b=-1,c=1,d=0`, which is edith's | ||
| 43 | +`Rotation::Cw270` (edith `crates/engine/src/demux.rs::rotation_of_matrix`, | ||
| 44 | +asserted in its `tests/rotation.rs`). This crate reads the same matrices the same | ||
| 45 | +way, so edith can delete its workaround and consume `StreamInfo.rotation` — the | ||
| 46 | +mapping is the drop-in point. | ||
| 47 | + | ||
| 48 | +Classification is exact integer arithmetic: a pure rotation satisfies `a == d` | ||
| 49 | +and `b == -c`; a quarter turn then lands the turned x-axis `(a, b)` on an axis, | ||
| 50 | +so `b == 0` (sign of `a`) or `a == 0` (sign of `b`) decides `None`/`Cw180` vs | ||
| 51 | +`Cw90`/`Cw270`. Anything else — a mirror, a shear, an anisotropic scale, a | ||
| 52 | +45-degree turn, a degenerate all-zero matrix — is `Rotation::Other { a, b, c, d }` | ||
| 53 | +with the raw 16.16 terms, never rounded onto a turn this decoder would apply for | ||
| 54 | +real. A scaled identity (`2*65536, 0, 0, 2*65536`) still reads `None`, matching | ||
| 55 | +edith's scale normalisation. | ||
| 56 | + | ||
| 57 | +## Verification | ||
| 58 | +- `cargo test -p ec-core -p ec-mp4` (CARGO_TARGET_DIR private to the lane): | ||
| 59 | + ec-core 43/0, ec-mp4 lib 18/0, `tests/mp4.rs` 15/0 (1 ignored, the real-library | ||
| 60 | + gate), `tests/rotation.rs` 3/0. | ||
| 61 | +- New tests: `registry::tests::quarter_turn_matrices_read_as_typed_rotations`, | ||
| 62 | + `registry::tests::a_matrix_that_is_not_a_quarter_turn_is_kept_not_rounded` | ||
| 63 | + (ec-core, unit: 90/180/270/identity/scaled-identity + mirror/shear/zero/45), | ||
| 64 | + and `tests/rotation.rs` (integration, media: rot90→Cw270, rot180→Cw180, | ||
| 65 | + rot270→Cw90, rot0→None, rot45→Other{46340,-46340,46340,46340}). | ||
| 66 | +- **Fail-pre-fix (non-vacuity)**: neutralising the parse (forcing | ||
| 67 | + `rotation = Rotation::None`) fails 2 of 3 integration tests | ||
| 68 | + (`a_tkhd_matrix_surfaces_as_a_typed_quarter_turn`, | ||
| 69 | + `a_matrix_that_is_not_a_quarter_turn_is_representable`); the no-turn test | ||
| 70 | + passes either way, as its expectation is `None`. Restored, re-green. | ||
| 71 | +- `cargo check --workspace --all-targets`: clean; warning set is byte-identical | ||
| 72 | + to c102c27a (ec-vorbis 1, ec-vp9 5, both pre-existing). | ||
| 73 | +- `cargo clippy -p ec-core -p ec-mp4 --all-targets`: no new warnings (one | ||
| 74 | + pre-existing `needless_borrow` in `tests/mp4.rs:249`, untouched). | ||
| 75 | +- `cargo doc -p ec-core -p ec-mp4 --no-deps`: no rustdoc warnings. | ||
| 76 | +- rustfmt (edition 2024, scoped to the touched leaf files): clean. | ||
| 77 | + | ||
| 78 | +## Residue / deferred | ||
| 79 | +- **deferred(follow-up)**: `ec-mp4`'s muxer writes `UNITY_MATRIX` into every | ||
| 80 | + `tkhd` (`src/mux.rs::write_trak`), so a demux→mux round trip drops a rotation | ||
| 81 | + carried on `StreamInfo`. Out of this lane's parse+surface scope; the demuxer | ||
| 82 | + now has the value the muxer would need to write. | ||
| 83 | +- Non-mp4 demuxers (matroska/ogg/riff) construct through `StreamInfo::new` and | ||
| 84 | + report `Rotation::None`; Matroska's `ProjectionPoseRoll` is not read (out of | ||
| 85 | + scope). | ||