forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli4 days ago4021 commits

rotation.rs69 lines, 2.6 KiBHistoryRawDownload
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
10use std::fs::File;
11use std::io::BufReader;
12use std::path::{Path, PathBuf};
13
14use ec_core::{Demuxer, Rotation};
15use ec_mp4::Mp4Demuxer;
16
17fn 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.
24fn 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]
35fn 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]
45fn 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]
54fn 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}