diff --git a/crates/engine/src/demux.rs b/crates/engine/src/demux.rs index a030aa8..17c5b78 100644 --- a/crates/engine/src/demux.rs +++ b/crates/engine/src/demux.rs @@ -371,13 +371,17 @@ pub struct VideoMeta { /// which range -- resolved off the container's tags, the bitstream's, and /// the resolution, in that order. See [`crate::colorspace`]. pub color: ColorDescription, - /// How far the picture must be turned to be seen right, off the - /// container's display matrix. [`Rotation::None`] for everything whose - /// container states no turn, and for a Matroska file: only the mp4 half is - /// read today, because Matroska's own spelling (a `ProjectionPoseYaw` - /// element) is written by nobody in practice while the `tkhd` matrix is how - /// every phone records it. That is a known gap and not a silent one: - /// [`MkvDemuxer::open`] says where a reader for it would go. + /// How far the picture must be turned to be seen right, off the container's + /// display matrix: an mp4 `tkhd` matrix, or a Matroska `Projection` roll on + /// a rectangular projection -- the same quarter turns read off either. + /// [`Rotation::None`] for everything whose container states no turn, and for + /// a projection that is not rectangular: on a spherical or cubemap one those + /// same pose elements describe how the *sphere* is oriented, which is no turn + /// of the picture, so the pose is deliberately not applied and the file + /// paints as a flat frame -- what every player does with a 360 clip, and + /// cheaper than a refusal that would cost a file that plays. A turn that is + /// not a quarter turn -- a mirror, a shear, an angle in between -- is refused + /// at the door ([`UnsupportedRotation`]) rather than shown wrong. pub rotation: Rotation, } @@ -1309,25 +1313,37 @@ impl MkvDemuxer { } } }; + // The coded size, swapped where the turn is a quarter one: a portrait + // `.mkv` stores a landscape picture plus the turn, so the displayed + // shape is the coded one turned -- the same two numbers the mp4 door + // swaps, off the same kind of turn. + let (width, height) = match video.rotation.swaps_axes() { + true => (video.height, video.width), + false => (video.width, video.height), + }; let meta = VideoMeta { - width: video.width, - height: video.height, + width, + height, frame_rate, frame_count: mkv.frames as u32, codec: video.codec, // `config` is the parameter sets by now -- Annex-B for H.264 and // HEVC, the sequence header OBU for AV1 -- which is what the // bitstream tier reads. + // The height the picture is *shown* at, which is what the mp4 door + // feeds here and the whole reason both doors resolve their size the + // same way: the 720-line rule below is a guess about the material, and + // the material is the picture as displayed -- a device-held `.mkv` is + // 640 coded but 1280 shown, and the two sides of that rule disagree. color: ColorDescription::resolve( video.tags, bitstream_tags(video.codec, &mkv.config), - video.height, + height, ), - // FLAG: Matroska's turn is not read yet -- see [`VideoMeta::rotation`]. - // Nothing in this tree writes a `ProjectionPoseYaw`, and the mkv - // tracks a phone or a camera app produces are mp4s; a `.mkv` that - // does carry one would play sideways until this is written. - rotation: Rotation::None, + // The turn off the track's `Projection` element ([`projection_rotation`]), + // which is where a Matroska muxer states what an mp4 states with its + // `tkhd` matrix. + rotation: video.rotation, }; Ok((meta, mkv)) } @@ -1784,7 +1800,11 @@ impl MkvDemuxer { Ok(Some(CodedBlock { bytes: &self.scratch, key: block.key, - ts_ns: block.ts * self.timestamp_scale as i64, + // Widened and fallible, exactly as the subtitle walk's tick scale + // is ([`ticks_scaled`]): `block.ts` is whatever the file says, so the + // multiply overflows an `i64` on a crafted cluster and a copy would + // write a block time that wrapped negative. + ts_ns: ticks_scaled(block.ts, self.timestamp_scale, 1)?, })) } } @@ -1994,6 +2014,9 @@ struct MkvVideo { number: u64, width: u32, height: u32, + /// The quarter turn the track's `Projection` asks for; + /// [`Rotation::None`] where it states none. + rotation: Rotation, /// What the track's `Colour` element declared, empty when it has none. tags: Tags, /// What that same element said about brightness (MaxCLL and the mastering @@ -2141,6 +2164,20 @@ const DEFAULT_DURATION: u32 = 0x23E383; const VIDEO: u32 = 0xE0; const PIXEL_WIDTH: u32 = 0xB0; const PIXEL_HEIGHT: u32 = 0xBA; +// The track's projection: how the flat picture sits in space. A roll on the +// rectangular projection (type 0) is how a Matroska muxer states what an mp4 +// states with its `tkhd` matrix -- mkvmerge has translated display matrices +// into roll since v95, and ffmpeg's Matroska muxer writes a `Projection` for +// rotated input -- so the same quarter turns are read off it. Anything else (a +// spherical or mesh projection, a yaw/pitch pose) is not a turn this engine can +// make, and is refused by name like the mp4 door refuses a matrix that is not a +// quarter turn; a spherical one's pose is the sphere's orientation rather than +// the picture's and is left alone ([`projection_rotation`]). +const PROJECTION: u32 = 0x7670; +const PROJECTION_TYPE: u32 = 0x7671; +const PROJECTION_POSE_YAW: u32 = 0x7673; +const PROJECTION_POSE_PITCH: u32 = 0x7674; +const PROJECTION_POSE_ROLL: u32 = 0x7675; // The `Colour` element and the three of its children this reads. Byte-verified // against real files rather than taken off a spec table: published tables that // list Range as 0x55B3 are describing ChromaSubsamplingHorz. @@ -2391,6 +2428,7 @@ fn mkv_track_entry( ) -> crate::Result { let (mut number, mut kind, mut codec, mut default_duration) = (0, 0, String::new(), None); let (mut width, mut height, mut config) = (0, 0, Vec::new()); + let mut rotation = Rotation::None; let (mut language, mut name, mut bcp47) = (String::new(), String::new(), String::new()); let mut tags = Tags::default(); let mut light = ContentLight::default(); @@ -2417,6 +2455,26 @@ fn mkv_track_entry( match e.0 { PIXEL_WIDTH => width = ebml_uint(file, e.1, e.2)? as u32, PIXEL_HEIGHT => height = ebml_uint(file, e.1, e.2)? as u32, + PROJECTION => { + let (mut kind, mut yaw, mut pitch, mut roll) = + (0u64, 0.0f64, 0.0f64, 0.0f64); + let mut at = e.1; + while let Some(c) = ebml_element(file, at, e.2)? { + match c.0 { + PROJECTION_TYPE => kind = ebml_uint(file, c.1, c.2)?, + PROJECTION_POSE_YAW => yaw = ebml_float(file, c.1, c.2)?, + PROJECTION_POSE_PITCH => pitch = ebml_float(file, c.1, c.2)?, + PROJECTION_POSE_ROLL => roll = ebml_float(file, c.1, c.2)?, + // ProjectionPrivate: the spherical/mesh + // payload, which only matters where the + // type is one of those -- and those refuse + // below before it could be read. + _ => {} + } + at = c.2; + } + rotation = projection_rotation(kind, yaw, pitch, roll)?; + } // `Colour`: what the file says its picture's numbers // mean, in the same H.273 code points the bitstream // uses. An element the file leaves out stays 0, which @@ -2541,6 +2599,7 @@ fn mkv_track_entry( number, width, height, + rotation, tags, light, default_duration, @@ -2684,6 +2743,74 @@ fn rotation_of_matrix(a: i32, b: i32, c: i32, d: i32) -> crate::Result }) } +/// The quarter turn a Matroska `ProjectionPoseRoll` asks for -- the mkv door's +/// half of [`rotation_of_matrix`], with the same half-degree tolerance and the +/// same refusal by name. +/// +/// **Measured against ffmpeg's own autorotate**, the same arbiter +/// [`rotation_of_matrix`] was measured against: `test_rotation{90,180,270}.mkv` +/// (`scripts/gen_fixtures.sh`) are the very quadrant source their `.mp4` twins +/// are, remuxed with `-display_rotation` asked for, and ffmpeg 8.1.2's matroska +/// muxer writes this element for it -- `+90.0`, `+180.0` and **`-90.0`**, an +/// 8-byte big-endian float, with no `ProjectionType` element at all (RFC 9559's +/// default of 0, rectangular). Its demuxer reads each of those back as the very +/// same `tkhd` matrix the mp4 twin carries, so both files autorotate to the same +/// four quadrants -- which is what `tests/rotation.rs` asserts, corner by corner. +/// +/// Hence the negation below: the element counts **counter-clockwise** degrees +/// about the viewing axis (RFC 9559 says so, and the measured layouts confirm +/// it), while the matrix this engine counts its quarter turns off is the +/// clockwise one `-display_rotation 90` writes -- so a roll of +90 is +/// [`Rotation::Cw270`] here, exactly as `a=0, b=-65536, c=65536, d=0` is. +fn rotation_of_roll(degrees: f64) -> crate::Result { + let steps = -(degrees / 90.0).round(); + if !degrees.is_finite() || (degrees + steps * 90.0).abs() > 0.5 { + return Err(UnsupportedRotation { + degrees: degrees.is_finite().then_some(degrees), + } + .into()); + } + Ok(match steps.rem_euclid(4.0) as u8 { + 0 => Rotation::None, + 1 => Rotation::Cw90, + 2 => Rotation::Cw180, + _ => Rotation::Cw270, + }) +} + +/// The turn a track's `Projection` asks for, or [`Rotation::None`] where it does +/// not ask for a turn of the picture at all. +/// +/// Only a **rectangular** projection has a pose that turns the *picture*, and +/// rectangular (0) is what an absent `ProjectionType` means. On an +/// equirectangular or cubemap one those same three elements state how the sphere +/// is oriented, which is not a turn of anything 2D: the pose is left alone, as a +/// flat player shows a 360° file, rather than refused -- refusing would cost a +/// file that plays right now and would not show it any righter. +/// +/// A rectangular projection whose **yaw or pitch** is off zero is refused by +/// name, though, and that is mkvmerge's spelling to watch for: since v95 it +/// translates an mp4 display matrix into roll *and* yaw, using yaw for a mirror. +/// A picture mirrored, or turned out of its own plane, is the one thing no +/// quarter turn here can make -- the same missing capability the mp4 door +/// refuses a 45-degree matrix for, so it gets the same refusal rather than a +/// sideways picture. +fn projection_rotation(kind: u64, yaw: f64, pitch: f64, roll: f64) -> crate::Result { + if kind != 0 { + return Ok(Rotation::None); + } + let off_plane = [yaw, pitch] + .into_iter() + .any(|degrees| !degrees.is_finite() || degrees.abs() > 0.5); + match off_plane { + // No angle to name: a pose out of the picture's own plane -- a mirror + // through it, or a tilt of it -- is not a rotation in that plane whatever + // number it states, which is what the sentence without one says. + true => Err(UnsupportedRotation { degrees: None }.into()), + false => rotation_of_roll(roll), + } +} + /// Why an mp4 came back with no picture, by the fourcc of the video track it /// does carry: `avc3` and `mp4v` are files with video in them, and telling their /// owner there is none sends them looking for a fault that is not in the file. @@ -3105,7 +3232,7 @@ fn mkv_cluster( if block.number != number { continue; } - let ts = cluster_ts + i64::from(block.rel); + let ts = block_ticks(cluster_ts, block.rel)?; if block.flags & 0x06 == 0 { blocks.push(Block { at: block.at, @@ -4312,7 +4439,10 @@ fn mkv_subtitle_blocks( const BLOCK_DURATION: u32 = 0x9B; // Ticks to microseconds, the unit `MkvCue` keeps. `TimestampScale` is // nanoseconds per tick and is a millisecond in every file anything writes. - let us = |ticks: i64| ticks * timestamp_scale as i64 / 1_000; + // [`ticks_scaled`] does the arithmetic: it is widened and fallible, and a + // corrupt cluster timestamp is refused rather than wrapped into a negative + // cue time -- see the test beside it. + let us = |ticks: i64| ticks_scaled(ticks, timestamp_scale, 1_000); let mut at = segment.0; while let Some((id, body, stop)) = ebml_element(file, at, segment.1)? { // The import's second walk, stopped by the same Cancel as its first. @@ -4376,8 +4506,13 @@ fn mkv_subtitle_blocks( read_exact_at(file, at, &mut payload)?; track.unpack.frame(&mut payload)?; track.cues.push(MkvCue { - start_us: us(cluster_ts + i64::from(block.rel)), - duration_us: duration.map(us), + // The cluster's timestamp plus the block's own signed + // offset, checked by [`block_ticks`]: the pair is where a + // corrupt file's two numbers can still overflow what an + // `i64` holds, one step before the scale above would refuse + // the result. + start_us: us(block_ticks(cluster_ts, block.rel)?)?, + duration_us: duration.map(us).transpose()?, payload, }); } @@ -4386,6 +4521,39 @@ fn mkv_subtitle_blocks( Ok(()) } +/// A block's absolute timestamp in ticks: the cluster's own, plus the block's +/// signed offset from it -- refused where the sum leaves what an `i64` holds. +/// +/// Both walks need it: the video block index a copy reads and the subtitle cues. +/// Both get `cluster_ts` from an EBML uint the file writes with no bound this +/// reader applies, so a crafted cluster near `i64::MAX` overflows the add one +/// step *before* [`ticks_scaled`] could refuse the product -- and the block would +/// be taken at a time that wrapped negative. +fn block_ticks(cluster_ts: i64, rel: i16) -> crate::Result { + cluster_ts + .checked_add(i64::from(rel)) + .ok_or_else(|| "a Matroska block at no timestamp a clock can hold".into()) +} + +/// A tick count scaled out of the file's own clock, or the refusal a corrupt one +/// earns: `ticks * TimestampScale / per_tick`, which is microseconds for the +/// subtitle walk (`per_tick` 1_000) and nanoseconds for the block walk a copy +/// reads (`per_tick` 1). +/// +/// The multiply is widened to `i128` rather than done in `i64`, and a result that +/// does not fit is refused rather than saturated: both numbers are the file's -- +/// a cluster or block timestamp is an EBML uint nothing bounds, and +/// `TimestampScale` is a scale it chooses -- so `ticks * timestamp_scale` +/// overflows on a crafted file, which is a panic in debug and a *negative* +/// timestamp in release, silently. Widened no product can overflow; a timestamp +/// that then does not fit its unit is the same class of corrupt as the oversized +/// block refused above, and gets the same loud refusal rather than a time no +/// clock can hold. +fn ticks_scaled(ticks: i64, timestamp_scale: u64, per_tick: u64) -> crate::Result { + i64::try_from(i128::from(ticks) * i128::from(timestamp_scale) / i128::from(per_tick)) + .map_err(|_| "a Matroska timestamp no clock can hold".into()) +} + /// A string element, without the trailing NULs a `Name` may be padded with. fn string_of(file: &mut File, body: u64, stop: u64) -> crate::Result { let bytes = ebml_bytes(file, body, stop)?; @@ -4398,6 +4566,41 @@ fn string_of(file: &mut File, body: u64, stop: u64) -> crate::Result { mod tests { use super::*; + /// Both walks that scale a file's own clock: the subtitle one + /// (microseconds, `per_tick` 1_000) and the block one a copy reads + /// (nanoseconds, `per_tick` 1). The product is where an `i64` overflows, so + /// these are the assertions that fail -- a panic in debug, a negative + /// timestamp in release -- without the widening. + #[test] + fn a_timestamp_no_clock_can_hold_is_refused() { + // A millisecond scale, which is every file anything writes: 1000 ticks + // is one second, in either unit. + assert_eq!(ticks_scaled(1_000, 1_000_000, 1_000).unwrap(), 1_000_000); + assert_eq!(ticks_scaled(0, 1_000_000, 1_000).unwrap(), 0); + assert_eq!(ticks_scaled(1_000, 1_000_000, 1).unwrap(), 1_000_000_000); + // A nanosecond scale and the largest tick count an EBML uint can cast + // to: tens of thousands of years of nanoseconds, which no `i64` holds. + assert!( + ticks_scaled(i64::MAX, 1_000_000_000, 1).is_err(), + "the block walk's product must not wrap into a negative time" + ); + assert!( + ticks_scaled(i64::MAX, 1_000_000_000, 1_000).is_err(), + "the subtitle walk's product must not wrap into a negative cue time" + ); + assert!(ticks_scaled(i64::MIN, 1_000_000_000, 1_000).is_err()); + // ...and the add that comes before either scale: a cluster timestamp near + // the top of what an `i64` holds plus a block's own offset still has to + // fit before any product is computed at all. + assert_eq!(block_ticks(1_000, -5).unwrap(), 995); + assert_eq!(block_ticks(0, 32_767).unwrap(), 32_767); + assert!( + block_ticks(i64::MAX, 1).is_err(), + "the add must not wrap negative" + ); + assert!(block_ticks(i64::MIN, -1).is_err()); + } + fn asset(name: &str) -> std::path::PathBuf { std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("../../assets") diff --git a/crates/engine/tests/rotation.rs b/crates/engine/tests/rotation.rs index 23ceb5a..c81fe91 100644 --- a/crates/engine/tests/rotation.rs +++ b/crates/engine/tests/rotation.rs @@ -92,7 +92,12 @@ fn corners(width: u32, height: u32, bgra: &[u8]) -> [&'static str; 4] { tint(&bgra[index..index + 4]) }; let (qx, qy) = (width / 4, height / 4); - [at(qx, qy), at(3 * qx, qy), at(3 * qx, 3 * qy), at(qx, 3 * qy)] + [ + at(qx, qy), + at(3 * qx, qy), + at(3 * qx, 3 * qy), + at(qx, 3 * qy), + ] } /// What the coded picture holds, before anything turns it: this is the layout @@ -112,17 +117,17 @@ fn first_frame(path: &Path) -> (u32, u32, Vec) { /// machine, which is the arbiter a display matrix is read against. fn expected(file: &str) -> ([u32; 2], Rotation, [&'static str; 4]) { match file { - "test_rotation90.mp4" => ( + "test_rotation90.mp4" | "test_rotation90.mkv" => ( [180, 320], Rotation::Cw270, ["green", "yellow", "blue", "red"], ), - "test_rotation180.mp4" => ( + "test_rotation180.mp4" | "test_rotation180.mkv" => ( [320, 180], Rotation::Cw180, ["yellow", "blue", "red", "green"], ), - "test_rotation270.mp4" => ( + "test_rotation270.mp4" | "test_rotation270.mkv" => ( [180, 320], Rotation::Cw90, ["blue", "red", "green", "yellow"], @@ -135,10 +140,18 @@ fn expected(file: &str) -> ([u32; 2], Rotation, [&'static str; 4]) { /// displayed size and names the turn, and a half turn does neither. #[test] fn a_display_matrix_is_read_as_a_quarter_turn() { + // Both doors, the same turn: the mkv twins are the same quadrant source + // remuxed with the same rotation asked for (`scripts/gen_fixtures.sh`), one + // stating it in a `Projection` roll and one in a `tkhd` matrix, and a roll + // read the wrong way round is a file that plays on its side in exactly one + // of the two. for file in [ "test_rotation90.mp4", + "test_rotation90.mkv", "test_rotation180.mp4", + "test_rotation180.mkv", "test_rotation270.mp4", + "test_rotation270.mkv", ] { let (size, rotation, _) = expected(file); let (meta, _) = engine::demux::Demuxer::open(&asset(file)).expect("open a fixture"); @@ -147,7 +160,10 @@ fn a_display_matrix_is_read_as_a_quarter_turn() { size, "{file}: the *displayed* size is what the engine reports" ); - assert_eq!(meta.rotation, rotation, "{file}: which turn the file asks for"); + assert_eq!( + meta.rotation, rotation, + "{file}: which turn the file asks for" + ); assert_eq!( rotation.swaps_axes(), size == [180, 320], @@ -168,10 +184,18 @@ fn a_display_matrix_is_read_as_a_quarter_turn() { #[test] fn the_preview_shows_a_turned_file_upright() { let _seat = pin_software(); + // Both doors, the same turn: the mkv twins are the same quadrant source + // remuxed with the same rotation asked for (`scripts/gen_fixtures.sh`), one + // stating it in a `Projection` roll and one in a `tkhd` matrix, and a roll + // read the wrong way round is a file that plays on its side in exactly one + // of the two. for file in [ "test_rotation90.mp4", + "test_rotation90.mkv", "test_rotation180.mp4", + "test_rotation180.mkv", "test_rotation270.mp4", + "test_rotation270.mkv", ] { let (size, _, corners_expected) = expected(file); let (width, height, bgra) = first_frame(&asset(file)); @@ -195,58 +219,106 @@ fn the_preview_shows_a_turned_file_upright() { #[test] fn an_export_of_a_turned_file_is_upright_and_portrait() { let _seat = pin_software(); - let source = asset("test_rotation90.mp4"); - let mut session = PlaybackSession::open(&source).expect("open the fixture"); - session.pause(); - assert_eq!( - [session.meta().width, session.meta().height], - [180, 320], - "the timeline is the displayed shape, not the coded one" - ); - let out = out_path("upright"); - let handle = session.export_to_with(&out, &ExportSettings::default()); - wait(&handle, Duration::from_secs(60)).expect("export"); - - let (meta, _) = engine::demux::Demuxer::open(&out).expect("reopen the export"); - assert_eq!( - [meta.width, meta.height], - [180, 320], - "the written file is portrait" - ); - assert_eq!( - meta.rotation, - Rotation::None, - "and states no matrix: the turn is in the pixels" - ); - assert_eq!(meta.frame_count, 30, "every timeline frame was written"); - - let (width, height, bgra) = first_frame(&out); - assert_eq!([width, height], [180, 320]); - assert_eq!( - corners(width, height, &bgra), - ["green", "yellow", "blue", "red"], - "the exported pixels are the ones the preview showed" - ); + // Both doors, because the turn reaches the export through exactly one number + // -- the source's `Rotation` in the export's rate table -- and an mkv states + // it with a `Projection` roll where an mp4 states a `tkhd` matrix: a door + // that read its own spelling into that number differently would bake a + // different turn into the pixels, and nothing but this test would show it. + for file in ["test_rotation90.mp4", "test_rotation90.mkv"] { + let source = asset(file); + let mut session = PlaybackSession::open(&source).expect("open the fixture"); + session.pause(); + assert_eq!( + [session.meta().width, session.meta().height], + [180, 320], + "{file}: the timeline is the displayed shape, not the coded one" + ); + let out = out_path(&file.replace('.', "_")); + let handle = session.export_to_with(&out, &ExportSettings::default()); + wait(&handle, Duration::from_secs(60)).expect("export"); + + let (meta, _) = engine::demux::Demuxer::open(&out).expect("reopen the export"); + assert_eq!( + [meta.width, meta.height], + [180, 320], + "{file}: the written file is portrait" + ); + assert_eq!( + meta.rotation, + Rotation::None, + "{file}: and states no matrix: the turn is in the pixels" + ); + assert_eq!( + meta.frame_count, 30, + "{file}: every timeline frame was written" + ); + + let (width, height, bgra) = first_frame(&out); + assert_eq!([width, height], [180, 320]); + assert_eq!( + corners(width, height, &bgra), + ["green", "yellow", "blue", "red"], + "{file}: the exported pixels are the ones the preview showed" + ); + } } -/// A matrix that is not a quarter turn is refused by name at the door, with the -/// angle in the sentence -- the honest answer to a picture this engine has no -/// way to show right. The capability really is absent: nothing here turns by an -/// arbitrary angle ([`engine::scale::rotate_i420_90s`] takes whole quarters), -/// and the refusal is what says so instead of a sideways picture. +/// A turn that is not a quarter turn is refused by name at the door, with the +/// angle in the sentence -- the honest answer to a picture this engine has no way +/// to show right. The capability really is absent: nothing here turns by an +/// arbitrary angle ([`engine::scale::rotate_i420_90s`] takes whole quarters), and +/// the refusal is what says so instead of a sideways picture. +/// +/// **Both doors**, because they are two spellings of one question: an mp4 states +/// the turn in a `tkhd` matrix and an mkv in a `Projection` roll, and a file +/// whose picture is 45 degrees off is the same missing capability either way -- +/// the mkv twin exists for exactly this assertion. #[test] -fn a_matrix_that_is_not_a_quarter_turn_is_refused_by_name() { - let error = match engine::demux::Demuxer::open(&asset("test_rotation45.mp4")) { - Ok(_) => panic!("45 degrees is not a quarter turn, and opening it must not succeed"), - Err(error) => error, - }; - assert!( - engine::UnsupportedRotation::is_it(&error), - "the refusal is the named one, not a generic failure: {error}" - ); - let said = error.to_string(); - assert!( - said.contains("45.0°") && said.contains("quarter turn"), - "the refusal names the angle it cannot make: {said}" - ); +fn a_turn_that_is_not_a_quarter_turn_is_refused_by_name() { + for file in ["test_rotation45.mp4", "test_rotation45.mkv"] { + let error = match engine::demux::Demuxer::open(&asset(file)) { + Ok(_) => { + panic!("45 degrees is not a quarter turn, and opening {file} must not succeed") + } + Err(error) => error, + }; + assert!( + engine::UnsupportedRotation::is_it(&error), + "{file}: the refusal is the named one, not a generic failure: {error}" + ); + let said = error.to_string(); + assert!( + said.contains("45.0°") && said.contains("quarter turn"), + "{file}: the refusal names the angle it cannot make: {said}" + ); + } +} + +/// A rotated file whose *coded* height and *displayed* height sit on opposite +/// sides of the 720-line rule an untagged stream's matrix is guessed from +/// ([`engine::colorspace::ColorDescription::resolve`]): `test_rotation90tall` is +/// 1280x640 as stored and 640x1280 as shown, and neither container tags it (the +/// mkv's `Colour` element carries only chroma siting, the mp4 has no `colr` +/// box), so that guess is the only thing deciding bt601 against bt709. +/// +/// Both doors must feed the height the picture is *shown* at -- bt709 here -- and +/// this is the assertion that fails if a door reads the coded height instead +/// (`video.height`, 640, is bt601). The two doors agreeing is the whole point of +/// resolving the size the same way in both. +#[test] +fn a_turned_file_guesses_its_matrix_from_the_height_it_is_shown_at() { + for file in ["test_rotation90tall.mp4", "test_rotation90tall.mkv"] { + let (meta, _) = engine::demux::Demuxer::open(&asset(file)).expect("open a fixture"); + assert_eq!( + [meta.width, meta.height], + [640, 1280], + "{file}: the shown shape -- 1280x640 as stored, turned by the file" + ); + assert_eq!( + meta.color.matrix, + engine::colorspace::Matrix::Bt709, + "{file}: the 1280-line *displayed* height is HD material; the 640-line coded one \ + would guess bt601, so this is the coded-height bug showing" + ); + } } diff --git a/scripts/gen_fixtures.sh b/scripts/gen_fixtures.sh index 8e287c5..0df6b04 100755 --- a/scripts/gen_fixtures.sh +++ b/scripts/gen_fixtures.sh @@ -730,6 +730,44 @@ for deg in 90 180 270 45; do ffmpeg -y -display_rotation "$deg" -i assets/.rotation_src.mp4 \ -c copy "assets/test_rotation$deg.mp4" done +# ...and the very same turn asked for inside a Matroska, from the very same +# source: one set of expected layouts then covers both containers +# (`tests/rotation.rs`), which is the point -- a roll read the wrong way round is +# a file that plays on its side in exactly one of the two. +# +# What the muxer actually writes, byte-verified by walking the header rather than +# taken off a table: a `Projection` master (`0x7670`) holding one +# `ProjectionPoseRoll` (`0x7675`), an 8-byte big-endian float -- `+90.0`, `+180.0` +# and *`-90.0`* for `-display_rotation 90/180/270` -- and no `ProjectionType` +# element at all, which is the spec's default of 0 (rectangular). `ffprobe` +# reports the same display matrix as the mp4 twin, which is what makes the +# layouts above ffmpeg's own autorotation of either file. The roll counts +# counter-clockwise where the matrix counts clockwise turns, which is why the +# engine negates it. +for deg in 90 180 270 45; do + ffmpeg -y -display_rotation "$deg" -i assets/.rotation_src.mp4 \ + -c copy "assets/test_rotation$deg.mkv" +done rm -f assets/.rotation_src.mp4 +# The one fixture where the coded and the displayed height disagree about what the +# material is: `test_rotation90tall` is 1280x640 as stored and 640x1280 as shown, +# and neither container tags its colour (the mkv's `Colour` element carries only +# chroma siting, the mp4 has no `colr` box), so the 720-line rule an untagged +# stream's matrix is guessed from reads a different side of itself depending on +# which height a door feeds it. +ffmpeg -y -f lavfi -i "color=c=red:s=640x320:d=1:r=30,format=yuv420p" \ + -f lavfi -i "color=c=green:s=640x320:d=1:r=30,format=yuv420p" \ + -f lavfi -i "color=c=blue:s=640x320:d=1:r=30,format=yuv420p" \ + -f lavfi -i "color=c=yellow:s=640x320:d=1:r=30,format=yuv420p" \ + -filter_complex "[0:v][1:v]hstack[top];[2:v][3:v]hstack[bot];[top][bot]vstack[v]" \ + -map "[v]" -frames:v 30 -c:v libx264 -profile:v baseline -pix_fmt yuv420p \ + assets/.rotation_tall_src.mp4 +for ext in mp4 mkv; do + ffmpeg -y -display_rotation 90 -i assets/.rotation_tall_src.mp4 \ + -c copy "assets/test_rotation90tall.$ext" +done +rm -f assets/.rotation_tall_src.mp4 + + echo "fixtures written to assets/"