forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

rotation_parity.rs251 lines, 8.5 KiBHistoryRawDownload
1//! The mp4 display matrix, read three ways that must agree: the engine's
2//! reading (`engine::demux::Demuxer`), the container demuxer's own
3//! (`ec_mp4`'s track rotation), and ffprobe's side data.
4//!
5//! Every fixture is generated here with ffmpeg, so nothing depends on a file
6//! someone happened to keep in `assets/`. The expectations are the turns the
7//! engine reported before its reading moved to `ec_mp4`, which
8//! `tests/rotation.rs` already pins against ffmpeg's own autorotation.
9
10use std::fs::File;
11use std::io::BufReader;
12use std::path::Path;
13use std::process::Command;
14
15use ec_core::Rotation as EcRotation;
16use engine::Rotation;
17use engine::scratch::Scratch;
18
19fn run(tool: &str, args: &[&str]) -> String {
20 let out = Command::new(tool)
21 .args(args)
22 .output()
23 .unwrap_or_else(|e| panic!("run {tool}: {e}"));
24 assert!(
25 out.status.success(),
26 "{tool} {args:?} failed: {}",
27 String::from_utf8_lossy(&out.stderr)
28 );
29 String::from_utf8_lossy(&out.stdout).into_owned()
30}
31
32/// One 320x180 H.264 mp4 of ffmpeg's test pattern, 10 frames, no rotation.
33fn plain_source(name: &str) -> Scratch {
34 let scratch = Scratch::file(name, "mp4");
35 run(
36 "ffmpeg",
37 &[
38 "-v",
39 "error",
40 "-y",
41 "-f",
42 "lavfi",
43 "-i",
44 "testsrc=size=320x180:rate=10",
45 "-frames:v",
46 "10",
47 "-c:v",
48 "libx264",
49 "-pix_fmt",
50 "yuv420p",
51 scratch.to_str().expect("utf-8 scratch path"),
52 ],
53 );
54 scratch
55}
56
57/// The same stream remuxed with `-display_rotation <degrees>`, the way
58/// `scripts/gen_fixtures.sh` writes its `test_rotation*.mp4` files.
59fn with_display_rotation(source: &Path, degrees: u32) -> Scratch {
60 let out = Scratch::file(&format!("rotation_parity_{degrees}"), "mp4");
61 run(
62 "ffmpeg",
63 &[
64 "-v",
65 "error",
66 "-y",
67 "-display_rotation",
68 &degrees.to_string(),
69 "-i",
70 source.to_str().expect("utf-8 source path"),
71 "-c",
72 "copy",
73 out.to_str().expect("utf-8 output path"),
74 ],
75 );
76 out
77}
78
79/// The rotation ffprobe's `side_data` states, in its own convention: the
80/// counter-clockwise angle, as ffprobe prints it (`90` for a file that is
81/// shown a quarter turn clockwise). Absent side data is no turn.
82fn ffprobe_rotation(path: &Path) -> i32 {
83 let text = run(
84 "ffprobe",
85 &[
86 "-v",
87 "error",
88 "-select_streams",
89 "v:0",
90 "-show_entries",
91 "side_data=rotation",
92 "-of",
93 "csv=p=0",
94 path.to_str().expect("utf-8 path"),
95 ],
96 );
97 match text.lines().map(str::trim).find(|line| !line.is_empty()) {
98 None => 0,
99 Some(line) => line
100 .parse()
101 .unwrap_or_else(|_| panic!("ffprobe rotation {line:?}")),
102 }
103}
104
105/// ffprobe's counter-clockwise angle, turned into the clockwise quarter turn
106/// the engine names.
107fn clockwise_from_ffprobe(ccw: i32) -> Rotation {
108 match (360 - ccw.rem_euclid(360)) % 360 {
109 0 => Rotation::None,
110 90 => Rotation::Cw90,
111 180 => Rotation::Cw180,
112 270 => Rotation::Cw270,
113 other => panic!("ffprobe states a {other}-degree clockwise turn, not a quarter turn"),
114 }
115}
116
117/// The rotation `ec_mp4` reports for the file's track, read from the container
118/// by the demuxer alone.
119fn ec_mp4_rotation(path: &Path) -> EcRotation {
120 let file = BufReader::new(File::open(path).expect("open the fixture"));
121 let demuxer = ec_mp4::Mp4Demuxer::new(file).expect("ec-mp4 opens the fixture");
122 let tracks = demuxer.tracks();
123 assert_eq!(tracks.len(), 1, "the fixture has one track");
124 tracks[0].rotation
125}
126
127/// What the engine reports for `degrees` of `-display_rotation`, and what
128/// `ec_mp4` reports for the same matrix. These are the values the engine's own
129/// `tkhd` parse gave before the switch.
130fn expected(degrees: u32) -> (Rotation, EcRotation) {
131 match degrees {
132 0 => (Rotation::None, EcRotation::None),
133 90 => (Rotation::Cw270, EcRotation::Cw270),
134 180 => (Rotation::Cw180, EcRotation::Cw180),
135 270 => (Rotation::Cw90, EcRotation::Cw90),
136 other => panic!("no expectation for {other} degrees"),
137 }
138}
139
140#[test]
141fn the_engine_matches_ec_mp4_and_ffprobe_for_every_quarter_turn() {
142 let source = plain_source("rotation_parity_source");
143 for degrees in [0, 90, 180, 270] {
144 let file = with_display_rotation(&source, degrees);
145 let (engine_turn, ec_turn) = expected(degrees);
146
147 let (meta, _) = engine::demux::Demuxer::open(&file).expect("open the fixture");
148 assert_eq!(
149 meta.rotation, engine_turn,
150 "-display_rotation {degrees}: the engine reads the turn it always read"
151 );
152 assert_eq!(
153 ec_mp4_rotation(&file),
154 ec_turn,
155 "-display_rotation {degrees}: ec-mp4 reads the same matrix"
156 );
157 let ffprobe = ffprobe_rotation(&file);
158 assert_eq!(
159 clockwise_from_ffprobe(ffprobe),
160 engine_turn,
161 "-display_rotation {degrees}: ffprobe side_data says {ffprobe}"
162 );
163 }
164}
165
166/// A mirror is not a turn: `ec_mp4` carries its four terms verbatim, and the
167/// engine still refuses it by name, with the same angle in the sentence as
168/// before. The matrix is written into a copy of the unrotated fixture's `tkhd`
169/// (`a = -1`, `d = 1`), which ffmpeg has no option to state.
170#[test]
171fn a_horizontal_mirror_is_refused_by_name_as_before() {
172 let source = plain_source("rotation_parity_hflip_source");
173 let file = with_display_rotation(&source, 0);
174 write_matrix(&file, [-65536, 0, 0, 65536]);
175
176 assert_eq!(
177 ec_mp4_rotation(&file),
178 EcRotation::Other {
179 a: -65536,
180 b: 0,
181 c: 0,
182 d: 65536
183 },
184 "ec-mp4 keeps the mirror's terms verbatim"
185 );
186 let error = match engine::demux::Demuxer::open(&file) {
187 Ok(_) => panic!("a mirror is not a turn, and opening it must not succeed"),
188 Err(error) => error,
189 };
190 assert!(
191 engine::UnsupportedRotation::is_it(&error),
192 "the refusal is the named one: {error}"
193 );
194 assert!(
195 error.to_string().contains("180.0°"),
196 "the refusal keeps the angle it had before: {error}"
197 );
198}
199
200/// Overwrites the `tkhd` display matrix of a version-0 mp4 with the four linear
201/// terms `[a, b, c, d]` in 16.16 fixed point. The FullBox payload starts after the
202/// fourcc; the matrix sits at byte 40 of it, and `a`, `b`, `u`, `c`, `d` are
203/// consecutive words there. ffmpeg has no option that writes an arbitrary matrix.
204fn write_matrix(file: &Path, [a, b, c, d]: [i32; 4]) {
205 let mut bytes = std::fs::read(file).expect("read the fixture");
206 let tkhd = bytes
207 .windows(4)
208 .position(|window| window == b"tkhd")
209 .expect("the fixture has a tkhd box");
210 let payload = tkhd + 4;
211 assert_eq!(bytes[payload], 0, "a version-0 tkhd, as ffmpeg writes it");
212 let matrix = payload + 40;
213 for (word, value) in [(0, a), (1, b), (3, c), (4, d)] {
214 let at = matrix + word * 4;
215 bytes[at..at + 4].copy_from_slice(&value.to_be_bytes());
216 }
217 std::fs::write(file, &bytes).expect("write the patched fixture");
218}
219
220/// A matrix 0.3 degrees past a quarter turn is still that quarter turn: the
221/// engine has always tolerated half a degree, and `ec-mp4` snaps the same way
222/// (its track rotation is already quantised, so the engine takes it as it is).
223/// Byte-patched into an ffmpeg-made file, `a = cos 90.3`, `b = sin 90.3`.
224#[test]
225fn a_matrix_within_half_a_degree_of_a_quarter_turn_snaps_to_it() {
226 let source = plain_source("rotation_parity_snap_source");
227 let file = with_display_rotation(&source, 0);
228 let theta = 90.3f64.to_radians();
229 let (a, b) = (
230 (theta.cos() * 65536.0).round() as i32,
231 (theta.sin() * 65536.0).round() as i32,
232 );
233 write_matrix(&file, [a, b, -b, a]);
234
235 assert_eq!(
236 ec_mp4_rotation(&file),
237 EcRotation::Cw90,
238 "ec-mp4 snaps a matrix within half a degree of a quarter turn"
239 );
240 let (meta, _) = engine::demux::Demuxer::open(&file).expect("a 90.3 degree turn opens");
241 assert_eq!(
242 meta.rotation,
243 Rotation::Cw90,
244 "snapped to the 90 degree turn"
245 );
246 assert_eq!(
247 [meta.width, meta.height],
248 [180, 320],
249 "and shown as the quarter turn it snaps to"
250 );
251}